Fix crash condition when an X server connection is broken. (Reported by Vincent Bernat.)
[emacs.git] / src / xfaces.c
blob025139377aa0b74dc985c448c4cb202a3eda913b
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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>
198 #include <stdio.h> /* This needs to be before termchar.h */
200 #include "lisp.h"
201 #include "charset.h"
202 #include "keyboard.h"
203 #include "frame.h"
204 #include "termhooks.h"
206 #ifdef HAVE_WINDOW_SYSTEM
207 #include "fontset.h"
208 #endif /* HAVE_WINDOW_SYSTEM */
210 #ifdef HAVE_X_WINDOWS
211 #include "xterm.h"
212 #ifdef USE_MOTIF
213 #include <Xm/Xm.h>
214 #include <Xm/XmStrDefs.h>
215 #endif /* USE_MOTIF */
216 #endif /* HAVE_X_WINDOWS */
218 #ifdef MSDOS
219 #include "dosfns.h"
220 #endif
222 #ifdef WINDOWSNT
223 #include "w32term.h"
224 #include "fontset.h"
225 /* Redefine X specifics to W32 equivalents to avoid cluttering the
226 code with #ifdef blocks. */
227 #undef FRAME_X_DISPLAY_INFO
228 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
229 #define x_display_info w32_display_info
230 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
231 #define check_x check_w32
232 #define x_list_fonts w32_list_fonts
233 #define GCGraphicsExposures 0
234 #endif /* WINDOWSNT */
236 #ifdef MAC_OS
237 #include "macterm.h"
238 #define x_display_info mac_display_info
239 #define check_x check_mac
240 #endif /* MAC_OS */
242 #include "buffer.h"
243 #include "dispextern.h"
244 #include "blockinput.h"
245 #include "window.h"
246 #include "intervals.h"
247 #include "termchar.h"
249 #ifdef HAVE_X_WINDOWS
251 /* Compensate for a bug in Xos.h on some systems, on which it requires
252 time.h. On some such systems, Xos.h tries to redefine struct
253 timeval and struct timezone if USG is #defined while it is
254 #included. */
256 #ifdef XOS_NEEDS_TIME_H
257 #include <time.h>
258 #undef USG
259 #include <X11/Xos.h>
260 #define USG
261 #define __TIMEVAL__
262 #else /* not XOS_NEEDS_TIME_H */
263 #include <X11/Xos.h>
264 #endif /* not XOS_NEEDS_TIME_H */
266 #endif /* HAVE_X_WINDOWS */
268 #include <ctype.h>
270 #define abs(X) ((X) < 0 ? -(X) : (X))
272 /* Number of pt per inch (from the TeXbook). */
274 #define PT_PER_INCH 72.27
276 /* Non-zero if face attribute ATTR is unspecified. */
278 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
280 /* Value is the number of elements of VECTOR. */
282 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
284 /* Make a copy of string S on the stack using alloca. Value is a pointer
285 to the copy. */
287 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
289 /* Make a copy of the contents of Lisp string S on the stack using
290 alloca. Value is a pointer to the copy. */
292 #define LSTRDUPA(S) STRDUPA (SDATA ((S)))
294 /* Size of hash table of realized faces in face caches (should be a
295 prime number). */
297 #define FACE_CACHE_BUCKETS_SIZE 1001
299 /* Keyword symbols used for face attribute names. */
301 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
302 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
303 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
304 Lisp_Object QCreverse_video;
305 Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
307 /* Symbols used for attribute values. */
309 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
310 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
311 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
312 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
313 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
314 Lisp_Object Qultra_expanded;
315 Lisp_Object Qreleased_button, Qpressed_button;
316 Lisp_Object QCstyle, QCcolor, QCline_width;
317 Lisp_Object Qunspecified;
319 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
321 /* The name of the function to call when the background of the frame
322 has changed, frame_update_face_colors. */
324 Lisp_Object Qframe_update_face_colors;
326 /* Names of basic faces. */
328 Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
329 Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
330 Lisp_Object Qmode_line_inactive;
331 extern Lisp_Object Qmode_line;
333 /* The symbol `face-alias'. A symbols having that property is an
334 alias for another face. Value of the property is the name of
335 the aliased face. */
337 Lisp_Object Qface_alias;
339 /* Default stipple pattern used on monochrome displays. This stipple
340 pattern is used on monochrome displays instead of shades of gray
341 for a face background color. See `set-face-stipple' for possible
342 values for this variable. */
344 Lisp_Object Vface_default_stipple;
346 /* Alist of alternative font families. Each element is of the form
347 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
348 try FAMILY1, then FAMILY2, ... */
350 Lisp_Object Vface_alternative_font_family_alist;
352 /* Alist of alternative font registries. Each element is of the form
353 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
354 loaded, try REGISTRY1, then REGISTRY2, ... */
356 Lisp_Object Vface_alternative_font_registry_alist;
358 /* Allowed scalable fonts. A value of nil means don't allow any
359 scalable fonts. A value of t means allow the use of any scalable
360 font. Otherwise, value must be a list of regular expressions. A
361 font may be scaled if its name matches a regular expression in the
362 list. */
364 Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
366 /* List of regular expressions that matches names of fonts to ignore. */
368 Lisp_Object Vface_ignored_fonts;
370 /* Alist of font name patterns vs the rescaling factor. */
372 Lisp_Object Vface_font_rescale_alist;
374 /* Maximum number of fonts to consider in font_list. If not an
375 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
377 Lisp_Object Vfont_list_limit;
378 #define DEFAULT_FONT_LIST_LIMIT 100
380 /* The symbols `foreground-color' and `background-color' which can be
381 used as part of a `face' property. This is for compatibility with
382 Emacs 20.2. */
384 Lisp_Object Qforeground_color, Qbackground_color;
386 /* The symbols `face' and `mouse-face' used as text properties. */
388 Lisp_Object Qface;
389 extern Lisp_Object Qmouse_face;
391 /* Property for basic faces which other faces cannot inherit. */
393 Lisp_Object Qface_no_inherit;
395 /* Error symbol for wrong_type_argument in load_pixmap. */
397 Lisp_Object Qbitmap_spec_p;
399 /* Alist of global face definitions. Each element is of the form
400 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
401 is a Lisp vector of face attributes. These faces are used
402 to initialize faces for new frames. */
404 Lisp_Object Vface_new_frame_defaults;
406 /* The next ID to assign to Lisp faces. */
408 static int next_lface_id;
410 /* A vector mapping Lisp face Id's to face names. */
412 static Lisp_Object *lface_id_to_name;
413 static int lface_id_to_name_size;
415 /* TTY color-related functions (defined in tty-colors.el). */
417 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
419 /* The name of the function used to compute colors on TTYs. */
421 Lisp_Object Qtty_color_alist;
423 /* An alist of defined terminal colors and their RGB values. */
425 Lisp_Object Vtty_defined_color_alist;
427 /* Counter for calls to clear_face_cache. If this counter reaches
428 CLEAR_FONT_TABLE_COUNT, and a frame has more than
429 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
431 static int clear_font_table_count;
432 #define CLEAR_FONT_TABLE_COUNT 100
433 #define CLEAR_FONT_TABLE_NFONTS 10
435 /* Non-zero means face attributes have been changed since the last
436 redisplay. Used in redisplay_internal. */
438 int face_change_count;
440 /* Non-zero means don't display bold text if a face's foreground
441 and background colors are the inverse of the default colors of the
442 display. This is a kluge to suppress `bold black' foreground text
443 which is hard to read on an LCD monitor. */
445 int tty_suppress_bold_inverse_default_colors_p;
447 /* A list of the form `((x . y))' used to avoid consing in
448 Finternal_set_lisp_face_attribute. */
450 static Lisp_Object Vparam_value_alist;
452 /* The total number of colors currently allocated. */
454 #if GLYPH_DEBUG
455 static int ncolors_allocated;
456 static int npixmaps_allocated;
457 static int ngcs;
458 #endif
460 /* Non-zero means the definition of the `menu' face for new frames has
461 been changed. */
463 int menu_face_changed_default;
466 /* Function prototypes. */
468 struct font_name;
469 struct table_entry;
470 struct named_merge_point;
472 static void map_tty_color P_ ((struct frame *, struct face *,
473 enum lface_attribute_index, int *));
474 static Lisp_Object resolve_face_name P_ ((Lisp_Object));
475 static int may_use_scalable_font_p P_ ((const char *));
476 static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
477 static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
478 int, int));
479 static int x_face_list_fonts P_ ((struct frame *, char *,
480 struct font_name **, int, int));
481 static int font_scalable_p P_ ((struct font_name *));
482 static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
483 static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
484 static unsigned char *xstrlwr P_ ((unsigned char *));
485 static void signal_error P_ ((char *, Lisp_Object));
486 static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
487 static void load_face_font P_ ((struct frame *, struct face *, int));
488 static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
489 static void free_face_colors P_ ((struct frame *, struct face *));
490 static int face_color_gray_p P_ ((struct frame *, char *));
491 static char *build_font_name P_ ((struct font_name *));
492 static void free_font_names P_ ((struct font_name *, int));
493 static int sorted_font_list P_ ((struct frame *, char *,
494 int (*cmpfn) P_ ((const void *, const void *)),
495 struct font_name **));
496 static int font_list_1 P_ ((struct frame *, Lisp_Object, Lisp_Object,
497 Lisp_Object, struct font_name **));
498 static int font_list P_ ((struct frame *, Lisp_Object, Lisp_Object,
499 Lisp_Object, struct font_name **));
500 static int try_font_list P_ ((struct frame *, Lisp_Object *,
501 Lisp_Object, Lisp_Object, struct font_name **,
502 int));
503 static int try_alternative_families P_ ((struct frame *f, Lisp_Object,
504 Lisp_Object, struct font_name **));
505 static int cmp_font_names P_ ((const void *, const void *));
506 static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *, int,
507 struct face *, int));
508 static struct face *realize_x_face P_ ((struct face_cache *,
509 Lisp_Object *, int, struct face *));
510 static struct face *realize_tty_face P_ ((struct face_cache *,
511 Lisp_Object *, int));
512 static int realize_basic_faces P_ ((struct frame *));
513 static int realize_default_face P_ ((struct frame *));
514 static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
515 static int lface_fully_specified_p P_ ((Lisp_Object *));
516 static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
517 static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
518 static unsigned lface_hash P_ ((Lisp_Object *));
519 static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
520 static struct face_cache *make_face_cache P_ ((struct frame *));
521 static void free_realized_face P_ ((struct frame *, struct face *));
522 static void clear_face_gcs P_ ((struct face_cache *));
523 static void free_face_cache P_ ((struct face_cache *));
524 static int face_numeric_weight P_ ((Lisp_Object));
525 static int face_numeric_slant P_ ((Lisp_Object));
526 static int face_numeric_swidth P_ ((Lisp_Object));
527 static int face_fontset P_ ((Lisp_Object *));
528 static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int, int, int*));
529 static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*,
530 struct named_merge_point *));
531 static int merge_face_ref P_ ((struct frame *, Lisp_Object, Lisp_Object *,
532 int, struct named_merge_point *));
533 static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object,
534 Lisp_Object, int, int));
535 static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
536 static struct face *make_realized_face P_ ((Lisp_Object *));
537 static void free_realized_faces P_ ((struct face_cache *));
538 static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
539 struct font_name *, int, int, int *));
540 static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
541 static void uncache_face P_ ((struct face_cache *, struct face *));
542 static int xlfd_numeric_slant P_ ((struct font_name *));
543 static int xlfd_numeric_weight P_ ((struct font_name *));
544 static int xlfd_numeric_swidth P_ ((struct font_name *));
545 static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
546 static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
547 static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
548 static int xlfd_fixed_p P_ ((struct font_name *));
549 static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
550 int, int));
551 static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
552 struct font_name *, int,
553 Lisp_Object));
554 static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
555 struct font_name *, int));
557 #ifdef HAVE_WINDOW_SYSTEM
559 static int split_font_name P_ ((struct frame *, struct font_name *, int));
560 static int xlfd_point_size P_ ((struct frame *, struct font_name *));
561 static void sort_fonts P_ ((struct frame *, struct font_name *, int,
562 int (*cmpfn) P_ ((const void *, const void *))));
563 static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
564 static void x_free_gc P_ ((struct frame *, GC));
565 static void clear_font_table P_ ((struct x_display_info *));
567 #ifdef WINDOWSNT
568 extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
569 #endif /* WINDOWSNT */
571 #ifdef USE_X_TOOLKIT
572 static void x_update_menu_appearance P_ ((struct frame *));
574 extern void free_frame_menubar P_ ((struct frame *));
575 #endif /* USE_X_TOOLKIT */
577 #endif /* HAVE_WINDOW_SYSTEM */
580 /***********************************************************************
581 Utilities
582 ***********************************************************************/
584 #ifdef HAVE_X_WINDOWS
586 #ifdef DEBUG_X_COLORS
588 /* The following is a poor mans infrastructure for debugging X color
589 allocation problems on displays with PseudoColor-8. Some X servers
590 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
591 color reference counts completely so that they don't signal an
592 error when a color is freed whose reference count is already 0.
593 Other X servers do. To help me debug this, the following code
594 implements a simple reference counting schema of its own, for a
595 single display/screen. --gerd. */
597 /* Reference counts for pixel colors. */
599 int color_count[256];
601 /* Register color PIXEL as allocated. */
603 void
604 register_color (pixel)
605 unsigned long pixel;
607 xassert (pixel < 256);
608 ++color_count[pixel];
612 /* Register color PIXEL as deallocated. */
614 void
615 unregister_color (pixel)
616 unsigned long pixel;
618 xassert (pixel < 256);
619 if (color_count[pixel] > 0)
620 --color_count[pixel];
621 else
622 abort ();
626 /* Register N colors from PIXELS as deallocated. */
628 void
629 unregister_colors (pixels, n)
630 unsigned long *pixels;
631 int n;
633 int i;
634 for (i = 0; i < n; ++i)
635 unregister_color (pixels[i]);
639 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
640 doc: /* Dump currently allocated colors to stderr. */)
643 int i, n;
645 fputc ('\n', stderr);
647 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
648 if (color_count[i])
650 fprintf (stderr, "%3d: %5d", i, color_count[i]);
651 ++n;
652 if (n % 5 == 0)
653 fputc ('\n', stderr);
654 else
655 fputc ('\t', stderr);
658 if (n % 5 != 0)
659 fputc ('\n', stderr);
660 return Qnil;
663 #endif /* DEBUG_X_COLORS */
666 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
667 color values. Interrupt input must be blocked when this function
668 is called. */
670 void
671 x_free_colors (f, pixels, npixels)
672 struct frame *f;
673 unsigned long *pixels;
674 int npixels;
676 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
678 /* If display has an immutable color map, freeing colors is not
679 necessary and some servers don't allow it. So don't do it. */
680 if (class != StaticColor && class != StaticGray && class != TrueColor)
682 #ifdef DEBUG_X_COLORS
683 unregister_colors (pixels, npixels);
684 #endif
685 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
686 pixels, npixels, 0);
691 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
692 color values. Interrupt input must be blocked when this function
693 is called. */
695 void
696 x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
697 Display *dpy;
698 Screen *screen;
699 Colormap cmap;
700 unsigned long *pixels;
701 int npixels;
703 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
704 int class = dpyinfo->visual->class;
706 /* If display has an immutable color map, freeing colors is not
707 necessary and some servers don't allow it. So don't do it. */
708 if (class != StaticColor && class != StaticGray && class != TrueColor)
710 #ifdef DEBUG_X_COLORS
711 unregister_colors (pixels, npixels);
712 #endif
713 XFreeColors (dpy, cmap, pixels, npixels, 0);
718 /* Create and return a GC for use on frame F. GC values and mask
719 are given by XGCV and MASK. */
721 static INLINE GC
722 x_create_gc (f, mask, xgcv)
723 struct frame *f;
724 unsigned long mask;
725 XGCValues *xgcv;
727 GC gc;
728 BLOCK_INPUT;
729 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
730 UNBLOCK_INPUT;
731 IF_DEBUG (++ngcs);
732 return gc;
736 /* Free GC which was used on frame F. */
738 static INLINE void
739 x_free_gc (f, gc)
740 struct frame *f;
741 GC gc;
743 BLOCK_INPUT;
744 IF_DEBUG (xassert (--ngcs >= 0));
745 XFreeGC (FRAME_X_DISPLAY (f), gc);
746 UNBLOCK_INPUT;
749 #endif /* HAVE_X_WINDOWS */
751 #ifdef WINDOWSNT
752 /* W32 emulation of GCs */
754 static INLINE GC
755 x_create_gc (f, mask, xgcv)
756 struct frame *f;
757 unsigned long mask;
758 XGCValues *xgcv;
760 GC gc;
761 BLOCK_INPUT;
762 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
763 UNBLOCK_INPUT;
764 IF_DEBUG (++ngcs);
765 return gc;
769 /* Free GC which was used on frame F. */
771 static INLINE void
772 x_free_gc (f, gc)
773 struct frame *f;
774 GC gc;
776 BLOCK_INPUT;
777 IF_DEBUG (xassert (--ngcs >= 0));
778 xfree (gc);
779 UNBLOCK_INPUT;
782 #endif /* WINDOWSNT */
784 #ifdef MAC_OS
785 /* Mac OS emulation of GCs */
787 extern XGCValues *XCreateGC (void *, Window, unsigned long, XGCValues *);
789 static INLINE GC
790 x_create_gc (f, mask, xgcv)
791 struct frame *f;
792 unsigned long mask;
793 XGCValues *xgcv;
795 GC gc;
796 gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
797 return gc;
800 static INLINE void
801 x_free_gc (f, gc)
802 struct frame *f;
803 GC gc;
805 XFreeGC (FRAME_MAC_DISPLAY (f), gc);
808 #endif /* MAC_OS */
810 /* Like stricmp. Used to compare parts of font names which are in
811 ISO8859-1. */
814 xstricmp (s1, s2)
815 const unsigned char *s1, *s2;
817 while (*s1 && *s2)
819 unsigned char c1 = tolower (*s1);
820 unsigned char c2 = tolower (*s2);
821 if (c1 != c2)
822 return c1 < c2 ? -1 : 1;
823 ++s1, ++s2;
826 if (*s1 == 0)
827 return *s2 == 0 ? 0 : -1;
828 return 1;
832 /* Like strlwr, which might not always be available. */
834 static unsigned char *
835 xstrlwr (s)
836 unsigned char *s;
838 unsigned char *p = s;
840 for (p = s; *p; ++p)
841 *p = tolower (*p);
843 return s;
847 /* Signal `error' with message S, and additional argument ARG. */
849 static void
850 signal_error (s, arg)
851 char *s;
852 Lisp_Object arg;
854 Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
858 /* If FRAME is nil, return a pointer to the selected frame.
859 Otherwise, check that FRAME is a live frame, and return a pointer
860 to it. NPARAM is the parameter number of FRAME, for
861 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
862 Lisp function definitions. */
864 static INLINE struct frame *
865 frame_or_selected_frame (frame, nparam)
866 Lisp_Object frame;
867 int nparam;
869 if (NILP (frame))
870 frame = selected_frame;
872 CHECK_LIVE_FRAME (frame);
873 return XFRAME (frame);
877 /***********************************************************************
878 Frames and faces
879 ***********************************************************************/
881 /* Initialize face cache and basic faces for frame F. */
883 void
884 init_frame_faces (f)
885 struct frame *f;
887 /* Make a face cache, if F doesn't have one. */
888 if (FRAME_FACE_CACHE (f) == NULL)
889 FRAME_FACE_CACHE (f) = make_face_cache (f);
891 #ifdef HAVE_WINDOW_SYSTEM
892 /* Make the image cache. */
893 if (FRAME_WINDOW_P (f))
895 if (FRAME_X_IMAGE_CACHE (f) == NULL)
896 FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
897 ++FRAME_X_IMAGE_CACHE (f)->refcount;
899 #endif /* HAVE_WINDOW_SYSTEM */
901 /* Realize basic faces. Must have enough information in frame
902 parameters to realize basic faces at this point. */
903 #ifdef HAVE_X_WINDOWS
904 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
905 #endif
906 #ifdef WINDOWSNT
907 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
908 #endif
909 #ifdef MAC_OS
910 if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
911 #endif
912 if (!realize_basic_faces (f))
913 abort ();
917 /* Free face cache of frame F. Called from Fdelete_frame. */
919 void
920 free_frame_faces (f)
921 struct frame *f;
923 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
925 if (face_cache)
927 free_face_cache (face_cache);
928 FRAME_FACE_CACHE (f) = NULL;
931 #ifdef HAVE_WINDOW_SYSTEM
932 if (FRAME_WINDOW_P (f))
934 struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
935 if (image_cache)
937 --image_cache->refcount;
938 if (image_cache->refcount == 0)
939 free_image_cache (f);
942 #endif /* HAVE_WINDOW_SYSTEM */
946 /* Clear face caches, and recompute basic faces for frame F. Call
947 this after changing frame parameters on which those faces depend,
948 or when realized faces have been freed due to changing attributes
949 of named faces. */
951 void
952 recompute_basic_faces (f)
953 struct frame *f;
955 if (FRAME_FACE_CACHE (f))
957 clear_face_cache (0);
958 if (!realize_basic_faces (f))
959 abort ();
964 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
965 try to free unused fonts, too. */
967 void
968 clear_face_cache (clear_fonts_p)
969 int clear_fonts_p;
971 #ifdef HAVE_WINDOW_SYSTEM
972 Lisp_Object tail, frame;
973 struct frame *f;
975 if (clear_fonts_p
976 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
978 struct x_display_info *dpyinfo;
980 /* Fonts are common for frames on one display, i.e. on
981 one X screen. */
982 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
983 if (dpyinfo->n_fonts > CLEAR_FONT_TABLE_NFONTS)
984 clear_font_table (dpyinfo);
986 /* From time to time see if we can unload some fonts. This also
987 frees all realized faces on all frames. Fonts needed by
988 faces will be loaded again when faces are realized again. */
989 clear_font_table_count = 0;
991 FOR_EACH_FRAME (tail, frame)
993 struct frame *f = XFRAME (frame);
994 if (FRAME_WINDOW_P (f)
995 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
996 free_all_realized_faces (frame);
999 else
1001 /* Clear GCs of realized faces. */
1002 FOR_EACH_FRAME (tail, frame)
1004 f = XFRAME (frame);
1005 if (FRAME_WINDOW_P (f))
1007 clear_face_gcs (FRAME_FACE_CACHE (f));
1008 clear_image_cache (f, 0);
1012 #endif /* HAVE_WINDOW_SYSTEM */
1016 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
1017 doc: /* Clear face caches on all frames.
1018 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
1019 (thoroughly)
1020 Lisp_Object thoroughly;
1022 clear_face_cache (!NILP (thoroughly));
1023 ++face_change_count;
1024 ++windows_or_buffers_changed;
1025 return Qnil;
1030 #ifdef HAVE_WINDOW_SYSTEM
1033 /* Remove fonts from the font table of DPYINFO except for the default
1034 ASCII fonts of frames on that display. Called from clear_face_cache
1035 from time to time. */
1037 static void
1038 clear_font_table (dpyinfo)
1039 struct x_display_info *dpyinfo;
1041 int i;
1043 /* Free those fonts that are not used by frames on DPYINFO. */
1044 for (i = 0; i < dpyinfo->n_fonts; ++i)
1046 struct font_info *font_info = dpyinfo->font_table + i;
1047 Lisp_Object tail, frame;
1049 /* Check if slot is already free. */
1050 if (font_info->name == NULL)
1051 continue;
1053 /* Don't free a default font of some frame. */
1054 FOR_EACH_FRAME (tail, frame)
1056 struct frame *f = XFRAME (frame);
1057 if (FRAME_WINDOW_P (f)
1058 && font_info->font == FRAME_FONT (f))
1059 break;
1062 if (!NILP (tail))
1063 continue;
1065 /* Free names. */
1066 if (font_info->full_name != font_info->name)
1067 xfree (font_info->full_name);
1068 xfree (font_info->name);
1070 /* Free the font. */
1071 BLOCK_INPUT;
1072 #ifdef HAVE_X_WINDOWS
1073 XFreeFont (dpyinfo->display, font_info->font);
1074 #endif
1075 #ifdef WINDOWSNT
1076 w32_unload_font (dpyinfo, font_info->font);
1077 #endif
1078 #ifdef MAC_OS
1079 mac_unload_font (dpyinfo, font_info->font);
1080 #endif
1081 UNBLOCK_INPUT;
1083 /* Mark font table slot free. */
1084 font_info->font = NULL;
1085 font_info->name = font_info->full_name = NULL;
1089 #endif /* HAVE_WINDOW_SYSTEM */
1093 /***********************************************************************
1094 X Pixmaps
1095 ***********************************************************************/
1097 #ifdef HAVE_WINDOW_SYSTEM
1099 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
1100 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
1101 A bitmap specification is either a string, a file name, or a list
1102 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
1103 HEIGHT is its height, and DATA is a string containing the bits of
1104 the pixmap. Bits are stored row by row, each row occupies
1105 \(WIDTH + 7)/8 bytes. */)
1106 (object)
1107 Lisp_Object object;
1109 int pixmap_p = 0;
1111 if (STRINGP (object))
1112 /* If OBJECT is a string, it's a file name. */
1113 pixmap_p = 1;
1114 else if (CONSP (object))
1116 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
1117 HEIGHT must be integers > 0, and DATA must be string large
1118 enough to hold a bitmap of the specified size. */
1119 Lisp_Object width, height, data;
1121 height = width = data = Qnil;
1123 if (CONSP (object))
1125 width = XCAR (object);
1126 object = XCDR (object);
1127 if (CONSP (object))
1129 height = XCAR (object);
1130 object = XCDR (object);
1131 if (CONSP (object))
1132 data = XCAR (object);
1136 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1138 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1139 / BITS_PER_CHAR);
1140 if (SBYTES (data) >= bytes_per_row * XINT (height))
1141 pixmap_p = 1;
1145 return pixmap_p ? Qt : Qnil;
1149 /* Load a bitmap according to NAME (which is either a file name or a
1150 pixmap spec) for use on frame F. Value is the bitmap_id (see
1151 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1152 bitmap cannot be loaded, display a message saying so, and return
1153 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1154 if these pointers are not null. */
1156 static int
1157 load_pixmap (f, name, w_ptr, h_ptr)
1158 FRAME_PTR f;
1159 Lisp_Object name;
1160 unsigned int *w_ptr, *h_ptr;
1162 int bitmap_id;
1163 Lisp_Object tem;
1165 if (NILP (name))
1166 return 0;
1168 tem = Fbitmap_spec_p (name);
1169 if (NILP (tem))
1170 wrong_type_argument (Qbitmap_spec_p, name);
1172 BLOCK_INPUT;
1173 if (CONSP (name))
1175 /* Decode a bitmap spec into a bitmap. */
1177 int h, w;
1178 Lisp_Object bits;
1180 w = XINT (Fcar (name));
1181 h = XINT (Fcar (Fcdr (name)));
1182 bits = Fcar (Fcdr (Fcdr (name)));
1184 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1185 w, h);
1187 else
1189 /* It must be a string -- a file name. */
1190 bitmap_id = x_create_bitmap_from_file (f, name);
1192 UNBLOCK_INPUT;
1194 if (bitmap_id < 0)
1196 add_to_log ("Invalid or undefined bitmap %s", name, Qnil);
1197 bitmap_id = 0;
1199 if (w_ptr)
1200 *w_ptr = 0;
1201 if (h_ptr)
1202 *h_ptr = 0;
1204 else
1206 #if GLYPH_DEBUG
1207 ++npixmaps_allocated;
1208 #endif
1209 if (w_ptr)
1210 *w_ptr = x_bitmap_width (f, bitmap_id);
1212 if (h_ptr)
1213 *h_ptr = x_bitmap_height (f, bitmap_id);
1216 return bitmap_id;
1219 #endif /* HAVE_WINDOW_SYSTEM */
1223 /***********************************************************************
1224 Fonts
1225 ***********************************************************************/
1227 #ifdef HAVE_WINDOW_SYSTEM
1229 /* Load font of face FACE which is used on frame F to display
1230 character C. The name of the font to load is determined by lface
1231 and fontset of FACE. */
1233 static void
1234 load_face_font (f, face, c)
1235 struct frame *f;
1236 struct face *face;
1237 int c;
1239 struct font_info *font_info = NULL;
1240 char *font_name;
1241 int needs_overstrike;
1243 face->font_info_id = -1;
1244 face->font = NULL;
1246 font_name = choose_face_font (f, face->lface, face->fontset, c,
1247 &needs_overstrike);
1248 if (!font_name)
1249 return;
1251 BLOCK_INPUT;
1252 font_info = FS_LOAD_FACE_FONT (f, c, font_name, face);
1253 UNBLOCK_INPUT;
1255 if (font_info)
1257 face->font_info_id = font_info->font_idx;
1258 face->font = font_info->font;
1259 face->font_name = font_info->full_name;
1260 face->overstrike = needs_overstrike;
1261 if (face->gc)
1263 x_free_gc (f, face->gc);
1264 face->gc = 0;
1267 else
1268 add_to_log ("Unable to load font %s",
1269 build_string (font_name), Qnil);
1270 xfree (font_name);
1273 #endif /* HAVE_WINDOW_SYSTEM */
1277 /***********************************************************************
1278 X Colors
1279 ***********************************************************************/
1281 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1282 RGB_LIST should contain (at least) 3 lisp integers.
1283 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1285 static int
1286 parse_rgb_list (rgb_list, color)
1287 Lisp_Object rgb_list;
1288 XColor *color;
1290 #define PARSE_RGB_LIST_FIELD(field) \
1291 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1293 color->field = XINT (XCAR (rgb_list)); \
1294 rgb_list = XCDR (rgb_list); \
1296 else \
1297 return 0;
1299 PARSE_RGB_LIST_FIELD (red);
1300 PARSE_RGB_LIST_FIELD (green);
1301 PARSE_RGB_LIST_FIELD (blue);
1303 return 1;
1307 /* Lookup on frame F the color described by the lisp string COLOR.
1308 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1309 non-zero, then the `standard' definition of the same color is
1310 returned in it. */
1312 static int
1313 tty_lookup_color (f, color, tty_color, std_color)
1314 struct frame *f;
1315 Lisp_Object color;
1316 XColor *tty_color, *std_color;
1318 Lisp_Object frame, color_desc;
1320 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1321 return 0;
1323 XSETFRAME (frame, f);
1325 color_desc = call2 (Qtty_color_desc, color, frame);
1326 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1328 Lisp_Object rgb;
1330 if (! INTEGERP (XCAR (XCDR (color_desc))))
1331 return 0;
1333 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1335 rgb = XCDR (XCDR (color_desc));
1336 if (! parse_rgb_list (rgb, tty_color))
1337 return 0;
1339 /* Should we fill in STD_COLOR too? */
1340 if (std_color)
1342 /* Default STD_COLOR to the same as TTY_COLOR. */
1343 *std_color = *tty_color;
1345 /* Do a quick check to see if the returned descriptor is
1346 actually _exactly_ equal to COLOR, otherwise we have to
1347 lookup STD_COLOR separately. If it's impossible to lookup
1348 a standard color, we just give up and use TTY_COLOR. */
1349 if ((!STRINGP (XCAR (color_desc))
1350 || NILP (Fstring_equal (color, XCAR (color_desc))))
1351 && !NILP (Ffboundp (Qtty_color_standard_values)))
1353 /* Look up STD_COLOR separately. */
1354 rgb = call1 (Qtty_color_standard_values, color);
1355 if (! parse_rgb_list (rgb, std_color))
1356 return 0;
1360 return 1;
1362 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1363 /* We were called early during startup, and the colors are not
1364 yet set up in tty-defined-color-alist. Don't return a failure
1365 indication, since this produces the annoying "Unable to
1366 load color" messages in the *Messages* buffer. */
1367 return 1;
1368 else
1369 /* tty-color-desc seems to have returned a bad value. */
1370 return 0;
1373 /* A version of defined_color for non-X frames. */
1376 tty_defined_color (f, color_name, color_def, alloc)
1377 struct frame *f;
1378 char *color_name;
1379 XColor *color_def;
1380 int alloc;
1382 int status = 1;
1384 /* Defaults. */
1385 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1386 color_def->red = 0;
1387 color_def->blue = 0;
1388 color_def->green = 0;
1390 if (*color_name)
1391 status = tty_lookup_color (f, build_string (color_name), color_def, 0);
1393 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1395 if (strcmp (color_name, "unspecified-fg") == 0)
1396 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1397 else if (strcmp (color_name, "unspecified-bg") == 0)
1398 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1401 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1402 status = 1;
1404 return status;
1408 /* Decide if color named COLOR_NAME is valid for the display
1409 associated with the frame F; if so, return the rgb values in
1410 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1412 This does the right thing for any type of frame. */
1415 defined_color (f, color_name, color_def, alloc)
1416 struct frame *f;
1417 char *color_name;
1418 XColor *color_def;
1419 int alloc;
1421 if (!FRAME_WINDOW_P (f))
1422 return tty_defined_color (f, color_name, color_def, alloc);
1423 #ifdef HAVE_X_WINDOWS
1424 else if (FRAME_X_P (f))
1425 return x_defined_color (f, color_name, color_def, alloc);
1426 #endif
1427 #ifdef WINDOWSNT
1428 else if (FRAME_W32_P (f))
1429 return w32_defined_color (f, color_name, color_def, alloc);
1430 #endif
1431 #ifdef MAC_OS
1432 else if (FRAME_MAC_P (f))
1433 return mac_defined_color (f, color_name, color_def, alloc);
1434 #endif
1435 else
1436 abort ();
1440 /* Given the index IDX of a tty color on frame F, return its name, a
1441 Lisp string. */
1443 Lisp_Object
1444 tty_color_name (f, idx)
1445 struct frame *f;
1446 int idx;
1448 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1450 Lisp_Object frame;
1451 Lisp_Object coldesc;
1453 XSETFRAME (frame, f);
1454 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1456 if (!NILP (coldesc))
1457 return XCAR (coldesc);
1459 #ifdef MSDOS
1460 /* We can have an MSDOG frame under -nw for a short window of
1461 opportunity before internal_terminal_init is called. DTRT. */
1462 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1463 return msdos_stdcolor_name (idx);
1464 #endif
1466 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1467 return build_string (unspecified_fg);
1468 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1469 return build_string (unspecified_bg);
1471 #ifdef WINDOWSNT
1472 return vga_stdcolor_name (idx);
1473 #endif
1475 return Qunspecified;
1479 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1480 black) on frame F. The algorithm is taken from 20.2 faces.el. */
1482 static int
1483 face_color_gray_p (f, color_name)
1484 struct frame *f;
1485 char *color_name;
1487 XColor color;
1488 int gray_p;
1490 if (defined_color (f, color_name, &color, 0))
1491 gray_p = ((abs (color.red - color.green)
1492 < max (color.red, color.green) / 20)
1493 && (abs (color.green - color.blue)
1494 < max (color.green, color.blue) / 20)
1495 && (abs (color.blue - color.red)
1496 < max (color.blue, color.red) / 20));
1497 else
1498 gray_p = 0;
1500 return gray_p;
1504 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1505 BACKGROUND_P non-zero means the color will be used as background
1506 color. */
1508 static int
1509 face_color_supported_p (f, color_name, background_p)
1510 struct frame *f;
1511 char *color_name;
1512 int background_p;
1514 Lisp_Object frame;
1515 XColor not_used;
1517 XSETFRAME (frame, f);
1518 return
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 FRAME_WINDOW_P (f)
1521 ? (!NILP (Fxw_display_color_p (frame))
1522 || xstricmp (color_name, "black") == 0
1523 || xstricmp (color_name, "white") == 0
1524 || (background_p
1525 && face_color_gray_p (f, color_name))
1526 || (!NILP (Fx_display_grayscale_p (frame))
1527 && face_color_gray_p (f, color_name)))
1529 #endif
1530 tty_defined_color (f, color_name, &not_used, 0);
1534 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1535 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1536 FRAME specifies the frame and thus the display for interpreting COLOR.
1537 If FRAME is nil or omitted, use the selected frame. */)
1538 (color, frame)
1539 Lisp_Object color, frame;
1541 struct frame *f;
1543 CHECK_STRING (color);
1544 if (NILP (frame))
1545 frame = selected_frame;
1546 else
1547 CHECK_FRAME (frame);
1548 f = XFRAME (frame);
1549 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
1553 DEFUN ("color-supported-p", Fcolor_supported_p,
1554 Scolor_supported_p, 1, 3, 0,
1555 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1556 BACKGROUND-P non-nil means COLOR is used as a background.
1557 Otherwise, this function tells whether it can be used as a foreground.
1558 If FRAME is nil or omitted, use the selected frame.
1559 COLOR must be a valid color name. */)
1560 (color, frame, background_p)
1561 Lisp_Object frame, color, background_p;
1563 struct frame *f;
1565 CHECK_STRING (color);
1566 if (NILP (frame))
1567 frame = selected_frame;
1568 else
1569 CHECK_FRAME (frame);
1570 f = XFRAME (frame);
1571 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
1572 return Qt;
1573 return Qnil;
1577 /* Load color with name NAME for use by face FACE on frame F.
1578 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1579 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1580 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1581 pixel color. If color cannot be loaded, display a message, and
1582 return the foreground, background or underline color of F, but
1583 record that fact in flags of the face so that we don't try to free
1584 these colors. */
1586 unsigned long
1587 load_color (f, face, name, target_index)
1588 struct frame *f;
1589 struct face *face;
1590 Lisp_Object name;
1591 enum lface_attribute_index target_index;
1593 XColor color;
1595 xassert (STRINGP (name));
1596 xassert (target_index == LFACE_FOREGROUND_INDEX
1597 || target_index == LFACE_BACKGROUND_INDEX
1598 || target_index == LFACE_UNDERLINE_INDEX
1599 || target_index == LFACE_OVERLINE_INDEX
1600 || target_index == LFACE_STRIKE_THROUGH_INDEX
1601 || target_index == LFACE_BOX_INDEX);
1603 /* if the color map is full, defined_color will return a best match
1604 to the values in an existing cell. */
1605 if (!defined_color (f, SDATA (name), &color, 1))
1607 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1609 switch (target_index)
1611 case LFACE_FOREGROUND_INDEX:
1612 face->foreground_defaulted_p = 1;
1613 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1614 break;
1616 case LFACE_BACKGROUND_INDEX:
1617 face->background_defaulted_p = 1;
1618 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1619 break;
1621 case LFACE_UNDERLINE_INDEX:
1622 face->underline_defaulted_p = 1;
1623 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1624 break;
1626 case LFACE_OVERLINE_INDEX:
1627 face->overline_color_defaulted_p = 1;
1628 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1629 break;
1631 case LFACE_STRIKE_THROUGH_INDEX:
1632 face->strike_through_color_defaulted_p = 1;
1633 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1634 break;
1636 case LFACE_BOX_INDEX:
1637 face->box_color_defaulted_p = 1;
1638 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1639 break;
1641 default:
1642 abort ();
1645 #if GLYPH_DEBUG
1646 else
1647 ++ncolors_allocated;
1648 #endif
1650 return color.pixel;
1654 #ifdef HAVE_WINDOW_SYSTEM
1656 /* Load colors for face FACE which is used on frame F. Colors are
1657 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1658 of ATTRS. If the background color specified is not supported on F,
1659 try to emulate gray colors with a stipple from Vface_default_stipple. */
1661 static void
1662 load_face_colors (f, face, attrs)
1663 struct frame *f;
1664 struct face *face;
1665 Lisp_Object *attrs;
1667 Lisp_Object fg, bg;
1669 bg = attrs[LFACE_BACKGROUND_INDEX];
1670 fg = attrs[LFACE_FOREGROUND_INDEX];
1672 /* Swap colors if face is inverse-video. */
1673 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1675 Lisp_Object tmp;
1676 tmp = fg;
1677 fg = bg;
1678 bg = tmp;
1681 /* Check for support for foreground, not for background because
1682 face_color_supported_p is smart enough to know that grays are
1683 "supported" as background because we are supposed to use stipple
1684 for them. */
1685 if (!face_color_supported_p (f, SDATA (bg), 0)
1686 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1688 x_destroy_bitmap (f, face->stipple);
1689 face->stipple = load_pixmap (f, Vface_default_stipple,
1690 &face->pixmap_w, &face->pixmap_h);
1693 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1694 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1698 /* Free color PIXEL on frame F. */
1700 void
1701 unload_color (f, pixel)
1702 struct frame *f;
1703 unsigned long pixel;
1705 #ifdef HAVE_X_WINDOWS
1706 if (pixel != -1)
1708 BLOCK_INPUT;
1709 x_free_colors (f, &pixel, 1);
1710 UNBLOCK_INPUT;
1712 #endif
1716 /* Free colors allocated for FACE. */
1718 static void
1719 free_face_colors (f, face)
1720 struct frame *f;
1721 struct face *face;
1723 #ifdef HAVE_X_WINDOWS
1724 if (face->colors_copied_bitwise_p)
1725 return;
1727 BLOCK_INPUT;
1729 if (!face->foreground_defaulted_p)
1731 x_free_colors (f, &face->foreground, 1);
1732 IF_DEBUG (--ncolors_allocated);
1735 if (!face->background_defaulted_p)
1737 x_free_colors (f, &face->background, 1);
1738 IF_DEBUG (--ncolors_allocated);
1741 if (face->underline_p
1742 && !face->underline_defaulted_p)
1744 x_free_colors (f, &face->underline_color, 1);
1745 IF_DEBUG (--ncolors_allocated);
1748 if (face->overline_p
1749 && !face->overline_color_defaulted_p)
1751 x_free_colors (f, &face->overline_color, 1);
1752 IF_DEBUG (--ncolors_allocated);
1755 if (face->strike_through_p
1756 && !face->strike_through_color_defaulted_p)
1758 x_free_colors (f, &face->strike_through_color, 1);
1759 IF_DEBUG (--ncolors_allocated);
1762 if (face->box != FACE_NO_BOX
1763 && !face->box_color_defaulted_p)
1765 x_free_colors (f, &face->box_color, 1);
1766 IF_DEBUG (--ncolors_allocated);
1769 UNBLOCK_INPUT;
1770 #endif /* HAVE_X_WINDOWS */
1773 #endif /* HAVE_WINDOW_SYSTEM */
1777 /***********************************************************************
1778 XLFD Font Names
1779 ***********************************************************************/
1781 /* An enumerator for each field of an XLFD font name. */
1783 enum xlfd_field
1785 XLFD_FOUNDRY,
1786 XLFD_FAMILY,
1787 XLFD_WEIGHT,
1788 XLFD_SLANT,
1789 XLFD_SWIDTH,
1790 XLFD_ADSTYLE,
1791 XLFD_PIXEL_SIZE,
1792 XLFD_POINT_SIZE,
1793 XLFD_RESX,
1794 XLFD_RESY,
1795 XLFD_SPACING,
1796 XLFD_AVGWIDTH,
1797 XLFD_REGISTRY,
1798 XLFD_ENCODING,
1799 XLFD_LAST
1802 /* An enumerator for each possible slant value of a font. Taken from
1803 the XLFD specification. */
1805 enum xlfd_slant
1807 XLFD_SLANT_UNKNOWN,
1808 XLFD_SLANT_ROMAN,
1809 XLFD_SLANT_ITALIC,
1810 XLFD_SLANT_OBLIQUE,
1811 XLFD_SLANT_REVERSE_ITALIC,
1812 XLFD_SLANT_REVERSE_OBLIQUE,
1813 XLFD_SLANT_OTHER
1816 /* Relative font weight according to XLFD documentation. */
1818 enum xlfd_weight
1820 XLFD_WEIGHT_UNKNOWN,
1821 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1822 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1823 XLFD_WEIGHT_LIGHT, /* 30 */
1824 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1825 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1826 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1827 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1828 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1829 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1832 /* Relative proportionate width. */
1834 enum xlfd_swidth
1836 XLFD_SWIDTH_UNKNOWN,
1837 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1838 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1839 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1840 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1841 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1842 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1843 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1844 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1845 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1848 /* Structure used for tables mapping XLFD weight, slant, and width
1849 names to numeric and symbolic values. */
1851 struct table_entry
1853 char *name;
1854 int numeric;
1855 Lisp_Object *symbol;
1858 /* Table of XLFD slant names and their numeric and symbolic
1859 representations. This table must be sorted by slant names in
1860 ascending order. */
1862 static struct table_entry slant_table[] =
1864 {"i", XLFD_SLANT_ITALIC, &Qitalic},
1865 {"o", XLFD_SLANT_OBLIQUE, &Qoblique},
1866 {"ot", XLFD_SLANT_OTHER, &Qitalic},
1867 {"r", XLFD_SLANT_ROMAN, &Qnormal},
1868 {"ri", XLFD_SLANT_REVERSE_ITALIC, &Qreverse_italic},
1869 {"ro", XLFD_SLANT_REVERSE_OBLIQUE, &Qreverse_oblique}
1872 /* Table of XLFD weight names. This table must be sorted by weight
1873 names in ascending order. */
1875 static struct table_entry weight_table[] =
1877 {"black", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold},
1878 {"bold", XLFD_WEIGHT_BOLD, &Qbold},
1879 {"book", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1880 {"demi", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1881 {"demibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1882 {"extralight", XLFD_WEIGHT_EXTRA_LIGHT, &Qextra_light},
1883 {"extrabold", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1884 {"heavy", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1885 {"light", XLFD_WEIGHT_LIGHT, &Qlight},
1886 {"medium", XLFD_WEIGHT_MEDIUM, &Qnormal},
1887 {"normal", XLFD_WEIGHT_MEDIUM, &Qnormal},
1888 {"regular", XLFD_WEIGHT_MEDIUM, &Qnormal},
1889 {"semibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1890 {"semilight", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1891 {"ultralight", XLFD_WEIGHT_ULTRA_LIGHT, &Qultra_light},
1892 {"ultrabold", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold}
1895 /* Table of XLFD width names. This table must be sorted by width
1896 names in ascending order. */
1898 static struct table_entry swidth_table[] =
1900 {"compressed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1901 {"condensed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1902 {"demiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1903 {"expanded", XLFD_SWIDTH_EXPANDED, &Qexpanded},
1904 {"extracondensed", XLFD_SWIDTH_EXTRA_CONDENSED, &Qextra_condensed},
1905 {"extraexpanded", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded},
1906 {"medium", XLFD_SWIDTH_MEDIUM, &Qnormal},
1907 {"narrow", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1908 {"normal", XLFD_SWIDTH_MEDIUM, &Qnormal},
1909 {"regular", XLFD_SWIDTH_MEDIUM, &Qnormal},
1910 {"semicondensed", XLFD_SWIDTH_SEMI_CONDENSED, &Qsemi_condensed},
1911 {"semiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1912 {"ultracondensed", XLFD_SWIDTH_ULTRA_CONDENSED, &Qultra_condensed},
1913 {"ultraexpanded", XLFD_SWIDTH_ULTRA_EXPANDED, &Qultra_expanded},
1914 {"wide", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded}
1917 /* Structure used to hold the result of splitting font names in XLFD
1918 format into their fields. */
1920 struct font_name
1922 /* The original name which is modified destructively by
1923 split_font_name. The pointer is kept here to be able to free it
1924 if it was allocated from the heap. */
1925 char *name;
1927 /* Font name fields. Each vector element points into `name' above.
1928 Fields are NUL-terminated. */
1929 char *fields[XLFD_LAST];
1931 /* Numeric values for those fields that interest us. See
1932 split_font_name for which these are. */
1933 int numeric[XLFD_LAST];
1935 /* If the original name matches one of Vface_font_rescale_alist,
1936 the value is the corresponding rescale ratio. Otherwise, the
1937 value is 1.0. */
1938 double rescale_ratio;
1940 /* Lower value mean higher priority. */
1941 int registry_priority;
1944 /* The frame in effect when sorting font names. Set temporarily in
1945 sort_fonts so that it is available in font comparison functions. */
1947 static struct frame *font_frame;
1949 /* Order by which font selection chooses fonts. The default values
1950 mean `first, find a best match for the font width, then for the
1951 font height, then for weight, then for slant.' This variable can be
1952 set via set-face-font-sort-order. */
1954 #ifdef MAC_OS
1955 static int font_sort_order[4] = {
1956 XLFD_SWIDTH, XLFD_POINT_SIZE, XLFD_WEIGHT, XLFD_SLANT
1958 #else
1959 static int font_sort_order[4];
1960 #endif
1962 /* Look up FONT.fields[FIELD_INDEX] in TABLE which has DIM entries.
1963 TABLE must be sorted by TABLE[i]->name in ascending order. Value
1964 is a pointer to the matching table entry or null if no table entry
1965 matches. */
1967 static struct table_entry *
1968 xlfd_lookup_field_contents (table, dim, font, field_index)
1969 struct table_entry *table;
1970 int dim;
1971 struct font_name *font;
1972 int field_index;
1974 /* Function split_font_name converts fields to lower-case, so there
1975 is no need to use xstrlwr or xstricmp here. */
1976 char *s = font->fields[field_index];
1977 int low, mid, high, cmp;
1979 low = 0;
1980 high = dim - 1;
1982 while (low <= high)
1984 mid = (low + high) / 2;
1985 cmp = strcmp (table[mid].name, s);
1987 if (cmp < 0)
1988 low = mid + 1;
1989 else if (cmp > 0)
1990 high = mid - 1;
1991 else
1992 return table + mid;
1995 return NULL;
1999 /* Return a numeric representation for font name field
2000 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2001 has DIM entries. Value is the numeric value found or DFLT if no
2002 table entry matches. This function is used to translate weight,
2003 slant, and swidth names of XLFD font names to numeric values. */
2005 static INLINE int
2006 xlfd_numeric_value (table, dim, font, field_index, dflt)
2007 struct table_entry *table;
2008 int dim;
2009 struct font_name *font;
2010 int field_index;
2011 int dflt;
2013 struct table_entry *p;
2014 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2015 return p ? p->numeric : dflt;
2019 /* Return a symbolic representation for font name field
2020 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2021 has DIM entries. Value is the symbolic value found or DFLT if no
2022 table entry matches. This function is used to translate weight,
2023 slant, and swidth names of XLFD font names to symbols. */
2025 static INLINE Lisp_Object
2026 xlfd_symbolic_value (table, dim, font, field_index, dflt)
2027 struct table_entry *table;
2028 int dim;
2029 struct font_name *font;
2030 int field_index;
2031 Lisp_Object dflt;
2033 struct table_entry *p;
2034 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2035 return p ? *p->symbol : dflt;
2039 /* Return a numeric value for the slant of the font given by FONT. */
2041 static INLINE int
2042 xlfd_numeric_slant (font)
2043 struct font_name *font;
2045 return xlfd_numeric_value (slant_table, DIM (slant_table),
2046 font, XLFD_SLANT, XLFD_SLANT_ROMAN);
2050 /* Return a symbol representing the weight of the font given by FONT. */
2052 static INLINE Lisp_Object
2053 xlfd_symbolic_slant (font)
2054 struct font_name *font;
2056 return xlfd_symbolic_value (slant_table, DIM (slant_table),
2057 font, XLFD_SLANT, Qnormal);
2061 /* Return a numeric value for the weight of the font given by FONT. */
2063 static INLINE int
2064 xlfd_numeric_weight (font)
2065 struct font_name *font;
2067 return xlfd_numeric_value (weight_table, DIM (weight_table),
2068 font, XLFD_WEIGHT, XLFD_WEIGHT_MEDIUM);
2072 /* Return a symbol representing the slant of the font given by FONT. */
2074 static INLINE Lisp_Object
2075 xlfd_symbolic_weight (font)
2076 struct font_name *font;
2078 return xlfd_symbolic_value (weight_table, DIM (weight_table),
2079 font, XLFD_WEIGHT, Qnormal);
2083 /* Return a numeric value for the swidth of the font whose XLFD font
2084 name fields are found in FONT. */
2086 static INLINE int
2087 xlfd_numeric_swidth (font)
2088 struct font_name *font;
2090 return xlfd_numeric_value (swidth_table, DIM (swidth_table),
2091 font, XLFD_SWIDTH, XLFD_SWIDTH_MEDIUM);
2095 /* Return a symbolic value for the swidth of FONT. */
2097 static INLINE Lisp_Object
2098 xlfd_symbolic_swidth (font)
2099 struct font_name *font;
2101 return xlfd_symbolic_value (swidth_table, DIM (swidth_table),
2102 font, XLFD_SWIDTH, Qnormal);
2106 /* Look up the entry of SYMBOL in the vector TABLE which has DIM
2107 entries. Value is a pointer to the matching table entry or null if
2108 no element of TABLE contains SYMBOL. */
2110 static struct table_entry *
2111 face_value (table, dim, symbol)
2112 struct table_entry *table;
2113 int dim;
2114 Lisp_Object symbol;
2116 int i;
2118 xassert (SYMBOLP (symbol));
2120 for (i = 0; i < dim; ++i)
2121 if (EQ (*table[i].symbol, symbol))
2122 break;
2124 return i < dim ? table + i : NULL;
2128 /* Return a numeric value for SYMBOL in the vector TABLE which has DIM
2129 entries. Value is -1 if SYMBOL is not found in TABLE. */
2131 static INLINE int
2132 face_numeric_value (table, dim, symbol)
2133 struct table_entry *table;
2134 int dim;
2135 Lisp_Object symbol;
2137 struct table_entry *p = face_value (table, dim, symbol);
2138 return p ? p->numeric : -1;
2142 /* Return a numeric value representing the weight specified by Lisp
2143 symbol WEIGHT. Value is one of the enumerators of enum
2144 xlfd_weight. */
2146 static INLINE int
2147 face_numeric_weight (weight)
2148 Lisp_Object weight;
2150 return face_numeric_value (weight_table, DIM (weight_table), weight);
2154 /* Return a numeric value representing the slant specified by Lisp
2155 symbol SLANT. Value is one of the enumerators of enum xlfd_slant. */
2157 static INLINE int
2158 face_numeric_slant (slant)
2159 Lisp_Object slant;
2161 return face_numeric_value (slant_table, DIM (slant_table), slant);
2165 /* Return a numeric value representing the swidth specified by Lisp
2166 symbol WIDTH. Value is one of the enumerators of enum xlfd_swidth. */
2168 static int
2169 face_numeric_swidth (width)
2170 Lisp_Object width;
2172 return face_numeric_value (swidth_table, DIM (swidth_table), width);
2176 #ifdef HAVE_WINDOW_SYSTEM
2178 /* Return non-zero if FONT is the name of a fixed-pitch font. */
2180 static INLINE int
2181 xlfd_fixed_p (font)
2182 struct font_name *font;
2184 /* Function split_font_name converts fields to lower-case, so there
2185 is no need to use tolower here. */
2186 return *font->fields[XLFD_SPACING] != 'p';
2190 /* Return the point size of FONT on frame F, measured in 1/10 pt.
2192 The actual height of the font when displayed on F depends on the
2193 resolution of both the font and frame. For example, a 10pt font
2194 designed for a 100dpi display will display larger than 10pt on a
2195 75dpi display. (It's not unusual to use fonts not designed for the
2196 display one is using. For example, some intlfonts are available in
2197 72dpi versions, only.)
2199 Value is the real point size of FONT on frame F, or 0 if it cannot
2200 be determined. */
2202 static INLINE int
2203 xlfd_point_size (f, font)
2204 struct frame *f;
2205 struct font_name *font;
2207 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2208 char *pixel_field = font->fields[XLFD_PIXEL_SIZE];
2209 double pixel;
2210 int real_pt;
2212 if (*pixel_field == '[')
2214 /* The pixel size field is `[A B C D]' which specifies
2215 a transformation matrix.
2217 A B 0
2218 C D 0
2219 0 0 1
2221 by which all glyphs of the font are transformed. The spec
2222 says that s scalar value N for the pixel size is equivalent
2223 to A = N * resx/resy, B = C = 0, D = N. */
2224 char *start = pixel_field + 1, *end;
2225 double matrix[4];
2226 int i;
2228 for (i = 0; i < 4; ++i)
2230 matrix[i] = strtod (start, &end);
2231 start = end;
2234 pixel = matrix[3];
2236 else
2237 pixel = atoi (pixel_field);
2239 if (pixel == 0)
2240 real_pt = 0;
2241 else
2242 real_pt = PT_PER_INCH * 10.0 * pixel / resy + 0.5;
2244 return real_pt;
2248 /* Return point size of PIXEL dots while considering Y-resultion (DPI)
2249 of frame F. This function is used to guess a point size of font
2250 when only the pixel height of the font is available. */
2252 static INLINE int
2253 pixel_point_size (f, pixel)
2254 struct frame *f;
2255 int pixel;
2257 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2258 double real_pt;
2259 int int_pt;
2261 /* As one inch is PT_PER_INCH points, PT_PER_INCH/RESY gives the
2262 point size of one dot. */
2263 real_pt = pixel * PT_PER_INCH / resy;
2264 int_pt = real_pt + 0.5;
2266 return int_pt;
2270 /* Return a rescaling ratio of a font of NAME. */
2272 static double
2273 font_rescale_ratio (name)
2274 char *name;
2276 Lisp_Object tail, elt;
2278 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2280 elt = XCAR (tail);
2281 if (STRINGP (XCAR (elt)) && FLOATP (XCDR (elt))
2282 && fast_c_string_match_ignore_case (XCAR (elt), name) >= 0)
2283 return XFLOAT_DATA (XCDR (elt));
2285 return 1.0;
2289 /* Split XLFD font name FONT->name destructively into NUL-terminated,
2290 lower-case fields in FONT->fields. NUMERIC_P non-zero means
2291 compute numeric values for fields XLFD_POINT_SIZE, XLFD_SWIDTH,
2292 XLFD_RESY, XLFD_SLANT, and XLFD_WEIGHT in FONT->numeric. Value is
2293 zero if the font name doesn't have the format we expect. The
2294 expected format is a font name that starts with a `-' and has
2295 XLFD_LAST fields separated by `-'. */
2297 static int
2298 split_font_name (f, font, numeric_p)
2299 struct frame *f;
2300 struct font_name *font;
2301 int numeric_p;
2303 int i = 0;
2304 int success_p;
2305 double rescale_ratio;
2307 if (numeric_p)
2308 /* This must be done before splitting the font name. */
2309 rescale_ratio = font_rescale_ratio (font->name);
2311 if (*font->name == '-')
2313 char *p = xstrlwr (font->name) + 1;
2315 while (i < XLFD_LAST)
2317 font->fields[i] = p;
2318 ++i;
2320 /* Pixel and point size may be of the form `[....]'. For
2321 BNF, see XLFD spec, chapter 4. Negative values are
2322 indicated by tilde characters which we replace with
2323 `-' characters, here. */
2324 if (*p == '['
2325 && (i - 1 == XLFD_PIXEL_SIZE
2326 || i - 1 == XLFD_POINT_SIZE))
2328 char *start, *end;
2329 int j;
2331 for (++p; *p && *p != ']'; ++p)
2332 if (*p == '~')
2333 *p = '-';
2335 /* Check that the matrix contains 4 floating point
2336 numbers. */
2337 for (j = 0, start = font->fields[i - 1] + 1;
2338 j < 4;
2339 ++j, start = end)
2340 if (strtod (start, &end) == 0 && start == end)
2341 break;
2343 if (j < 4)
2344 break;
2347 while (*p && *p != '-')
2348 ++p;
2350 if (*p != '-')
2351 break;
2353 *p++ = 0;
2357 success_p = i == XLFD_LAST;
2359 /* If requested, and font name was in the expected format,
2360 compute numeric values for some fields. */
2361 if (numeric_p && success_p)
2363 font->numeric[XLFD_POINT_SIZE] = xlfd_point_size (f, font);
2364 font->numeric[XLFD_RESY] = atoi (font->fields[XLFD_RESY]);
2365 font->numeric[XLFD_SLANT] = xlfd_numeric_slant (font);
2366 font->numeric[XLFD_WEIGHT] = xlfd_numeric_weight (font);
2367 font->numeric[XLFD_SWIDTH] = xlfd_numeric_swidth (font);
2368 font->numeric[XLFD_AVGWIDTH] = atoi (font->fields[XLFD_AVGWIDTH]);
2369 font->rescale_ratio = rescale_ratio;
2372 /* Initialize it to zero. It will be overridden by font_list while
2373 trying alternate registries. */
2374 font->registry_priority = 0;
2376 return success_p;
2380 /* Build an XLFD font name from font name fields in FONT. Value is a
2381 pointer to the font name, which is allocated via xmalloc. */
2383 static char *
2384 build_font_name (font)
2385 struct font_name *font;
2387 int i;
2388 int size = 100;
2389 char *font_name = (char *) xmalloc (size);
2390 int total_length = 0;
2392 for (i = 0; i < XLFD_LAST; ++i)
2394 /* Add 1 because of the leading `-'. */
2395 int len = strlen (font->fields[i]) + 1;
2397 /* Reallocate font_name if necessary. Add 1 for the final
2398 NUL-byte. */
2399 if (total_length + len + 1 >= size)
2401 int new_size = max (2 * size, size + len + 1);
2402 int sz = new_size * sizeof *font_name;
2403 font_name = (char *) xrealloc (font_name, sz);
2404 size = new_size;
2407 font_name[total_length] = '-';
2408 bcopy (font->fields[i], font_name + total_length + 1, len - 1);
2409 total_length += len;
2412 font_name[total_length] = 0;
2413 return font_name;
2417 /* Free an array FONTS of N font_name structures. This frees FONTS
2418 itself and all `name' fields in its elements. */
2420 static INLINE void
2421 free_font_names (fonts, n)
2422 struct font_name *fonts;
2423 int n;
2425 while (n)
2426 xfree (fonts[--n].name);
2427 xfree (fonts);
2431 /* Sort vector FONTS of font_name structures which contains NFONTS
2432 elements using qsort and comparison function CMPFN. F is the frame
2433 on which the fonts will be used. The global variable font_frame
2434 is temporarily set to F to make it available in CMPFN. */
2436 static INLINE void
2437 sort_fonts (f, fonts, nfonts, cmpfn)
2438 struct frame *f;
2439 struct font_name *fonts;
2440 int nfonts;
2441 int (*cmpfn) P_ ((const void *, const void *));
2443 font_frame = f;
2444 qsort (fonts, nfonts, sizeof *fonts, cmpfn);
2445 font_frame = NULL;
2449 /* Get fonts matching PATTERN on frame F. If F is null, use the first
2450 display in x_display_list. FONTS is a pointer to a vector of
2451 NFONTS font_name structures. TRY_ALTERNATIVES_P non-zero means try
2452 alternative patterns from Valternate_fontname_alist if no fonts are
2453 found matching PATTERN.
2455 For all fonts found, set FONTS[i].name to the name of the font,
2456 allocated via xmalloc, and split font names into fields. Ignore
2457 fonts that we can't parse. Value is the number of fonts found. */
2459 static int
2460 x_face_list_fonts (f, pattern, pfonts, nfonts, try_alternatives_p)
2461 struct frame *f;
2462 char *pattern;
2463 struct font_name **pfonts;
2464 int nfonts, try_alternatives_p;
2466 int n, nignored;
2468 /* NTEMACS_TODO : currently this uses w32_list_fonts, but it may be
2469 better to do it the other way around. */
2470 Lisp_Object lfonts;
2471 Lisp_Object lpattern, tem;
2472 struct font_name *fonts = 0;
2473 int num_fonts = nfonts;
2475 *pfonts = 0;
2476 lpattern = build_string (pattern);
2478 /* Get the list of fonts matching PATTERN. */
2479 #ifdef WINDOWSNT
2480 BLOCK_INPUT;
2481 lfonts = w32_list_fonts (f, lpattern, 0, nfonts);
2482 UNBLOCK_INPUT;
2483 #else
2484 lfonts = x_list_fonts (f, lpattern, -1, nfonts);
2485 #endif
2487 if (nfonts < 0 && CONSP (lfonts))
2488 num_fonts = XFASTINT (Flength (lfonts));
2490 /* Make a copy of the font names we got from X, and
2491 split them into fields. */
2492 n = nignored = 0;
2493 for (tem = lfonts; CONSP (tem) && n < num_fonts; tem = XCDR (tem))
2495 Lisp_Object elt, tail;
2496 const char *name = SDATA (XCAR (tem));
2498 /* Ignore fonts matching a pattern from face-ignored-fonts. */
2499 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2501 elt = XCAR (tail);
2502 if (STRINGP (elt)
2503 && fast_c_string_match_ignore_case (elt, name) >= 0)
2504 break;
2506 if (!NILP (tail))
2508 ++nignored;
2509 continue;
2512 if (! fonts)
2514 *pfonts = (struct font_name *) xmalloc (num_fonts * sizeof **pfonts);
2515 fonts = *pfonts;
2518 /* Make a copy of the font name. */
2519 fonts[n].name = xstrdup (name);
2521 if (split_font_name (f, fonts + n, 1))
2523 if (font_scalable_p (fonts + n)
2524 && !may_use_scalable_font_p (name))
2526 ++nignored;
2527 xfree (fonts[n].name);
2529 else
2530 ++n;
2532 else
2533 xfree (fonts[n].name);
2536 /* If no fonts found, try patterns from Valternate_fontname_alist. */
2537 if (n == 0 && try_alternatives_p)
2539 Lisp_Object list = Valternate_fontname_alist;
2541 if (*pfonts)
2543 xfree (*pfonts);
2544 *pfonts = 0;
2547 while (CONSP (list))
2549 Lisp_Object entry = XCAR (list);
2550 if (CONSP (entry)
2551 && STRINGP (XCAR (entry))
2552 && strcmp (SDATA (XCAR (entry)), pattern) == 0)
2553 break;
2554 list = XCDR (list);
2557 if (CONSP (list))
2559 Lisp_Object patterns = XCAR (list);
2560 Lisp_Object name;
2562 while (CONSP (patterns)
2563 /* If list is screwed up, give up. */
2564 && (name = XCAR (patterns),
2565 STRINGP (name))
2566 /* Ignore patterns equal to PATTERN because we tried that
2567 already with no success. */
2568 && (strcmp (SDATA (name), pattern) == 0
2569 || (n = x_face_list_fonts (f, SDATA (name),
2570 pfonts, nfonts, 0),
2571 n == 0)))
2572 patterns = XCDR (patterns);
2576 return n;
2580 /* Check if a font matching pattern_offset_t on frame F is available
2581 or not. PATTERN may be a cons (FAMILY . REGISTRY), in which case,
2582 a font name pattern is generated from FAMILY and REGISTRY. */
2585 face_font_available_p (f, pattern)
2586 struct frame *f;
2587 Lisp_Object pattern;
2589 Lisp_Object fonts;
2591 if (! STRINGP (pattern))
2593 Lisp_Object family, registry;
2594 char *family_str, *registry_str, *pattern_str;
2596 CHECK_CONS (pattern);
2597 family = XCAR (pattern);
2598 if (NILP (family))
2599 family_str = "*";
2600 else
2602 CHECK_STRING (family);
2603 family_str = (char *) SDATA (family);
2605 registry = XCDR (pattern);
2606 if (NILP (registry))
2607 registry_str = "*";
2608 else
2610 CHECK_STRING (registry);
2611 registry_str = (char *) SDATA (registry);
2614 pattern_str = (char *) alloca (strlen (family_str)
2615 + strlen (registry_str)
2616 + 10);
2617 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2618 strcat (pattern_str, family_str);
2619 strcat (pattern_str, "-*-");
2620 strcat (pattern_str, registry_str);
2621 if (!index (registry_str, '-'))
2623 if (registry_str[strlen (registry_str) - 1] == '*')
2624 strcat (pattern_str, "-*");
2625 else
2626 strcat (pattern_str, "*-*");
2628 pattern = build_string (pattern_str);
2631 /* Get the list of fonts matching PATTERN. */
2632 #ifdef WINDOWSNT
2633 BLOCK_INPUT;
2634 fonts = w32_list_fonts (f, pattern, 0, 1);
2635 UNBLOCK_INPUT;
2636 #else
2637 fonts = x_list_fonts (f, pattern, -1, 1);
2638 #endif
2639 return XINT (Flength (fonts));
2643 /* Determine fonts matching PATTERN on frame F. Sort resulting fonts
2644 using comparison function CMPFN. Value is the number of fonts
2645 found. If value is non-zero, *FONTS is set to a vector of
2646 font_name structures allocated from the heap containing matching
2647 fonts. Each element of *FONTS contains a name member that is also
2648 allocated from the heap. Font names in these structures are split
2649 into fields. Use free_font_names to free such an array. */
2651 static int
2652 sorted_font_list (f, pattern, cmpfn, fonts)
2653 struct frame *f;
2654 char *pattern;
2655 int (*cmpfn) P_ ((const void *, const void *));
2656 struct font_name **fonts;
2658 int nfonts;
2660 /* Get the list of fonts matching pattern. 100 should suffice. */
2661 nfonts = DEFAULT_FONT_LIST_LIMIT;
2662 if (INTEGERP (Vfont_list_limit))
2663 nfonts = XINT (Vfont_list_limit);
2665 *fonts = NULL;
2666 nfonts = x_face_list_fonts (f, pattern, fonts, nfonts, 1);
2668 /* Sort the resulting array and return it in *FONTS. If no
2669 fonts were found, make sure to set *FONTS to null. */
2670 if (nfonts)
2671 sort_fonts (f, *fonts, nfonts, cmpfn);
2672 else if (*fonts)
2674 xfree (*fonts);
2675 *fonts = NULL;
2678 return nfonts;
2682 /* Compare two font_name structures *A and *B. Value is analogous to
2683 strcmp. Sort order is given by the global variable
2684 font_sort_order. Font names are sorted so that, everything else
2685 being equal, fonts with a resolution closer to that of the frame on
2686 which they are used are listed first. The global variable
2687 font_frame is the frame on which we operate. */
2689 static int
2690 cmp_font_names (a, b)
2691 const void *a, *b;
2693 struct font_name *x = (struct font_name *) a;
2694 struct font_name *y = (struct font_name *) b;
2695 int cmp;
2697 /* All strings have been converted to lower-case by split_font_name,
2698 so we can use strcmp here. */
2699 cmp = strcmp (x->fields[XLFD_FAMILY], y->fields[XLFD_FAMILY]);
2700 if (cmp == 0)
2702 int i;
2704 for (i = 0; i < DIM (font_sort_order) && cmp == 0; ++i)
2706 int j = font_sort_order[i];
2707 cmp = x->numeric[j] - y->numeric[j];
2710 if (cmp == 0)
2712 /* Everything else being equal, we prefer fonts with an
2713 y-resolution closer to that of the frame. */
2714 int resy = FRAME_X_DISPLAY_INFO (font_frame)->resy;
2715 int x_resy = x->numeric[XLFD_RESY];
2716 int y_resy = y->numeric[XLFD_RESY];
2717 cmp = abs (resy - x_resy) - abs (resy - y_resy);
2721 return cmp;
2725 /* Get a sorted list of fonts of family FAMILY on frame F. If PATTERN
2726 is non-nil list fonts matching that pattern. Otherwise, if
2727 REGISTRY is non-nil return only fonts with that registry, otherwise
2728 return fonts of any registry. Set *FONTS to a vector of font_name
2729 structures allocated from the heap containing the fonts found.
2730 Value is the number of fonts found. */
2732 static int
2733 font_list_1 (f, pattern, family, registry, fonts)
2734 struct frame *f;
2735 Lisp_Object pattern, family, registry;
2736 struct font_name **fonts;
2738 char *pattern_str, *family_str, *registry_str;
2740 if (NILP (pattern))
2742 family_str = (NILP (family) ? "*" : (char *) SDATA (family));
2743 registry_str = (NILP (registry) ? "*" : (char *) SDATA (registry));
2745 pattern_str = (char *) alloca (strlen (family_str)
2746 + strlen (registry_str)
2747 + 10);
2748 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2749 strcat (pattern_str, family_str);
2750 strcat (pattern_str, "-*-");
2751 strcat (pattern_str, registry_str);
2752 if (!index (registry_str, '-'))
2754 if (registry_str[strlen (registry_str) - 1] == '*')
2755 strcat (pattern_str, "-*");
2756 else
2757 strcat (pattern_str, "*-*");
2760 else
2761 pattern_str = (char *) SDATA (pattern);
2763 return sorted_font_list (f, pattern_str, cmp_font_names, fonts);
2767 /* Concatenate font list FONTS1 and FONTS2. FONTS1 and FONTS2
2768 contains NFONTS1 fonts and NFONTS2 fonts respectively. Return a
2769 pointer to a newly allocated font list. FONTS1 and FONTS2 are
2770 freed. */
2772 static struct font_name *
2773 concat_font_list (fonts1, nfonts1, fonts2, nfonts2)
2774 struct font_name *fonts1, *fonts2;
2775 int nfonts1, nfonts2;
2777 int new_nfonts = nfonts1 + nfonts2;
2778 struct font_name *new_fonts;
2780 new_fonts = (struct font_name *) xmalloc (sizeof *new_fonts * new_nfonts);
2781 bcopy (fonts1, new_fonts, sizeof *new_fonts * nfonts1);
2782 bcopy (fonts2, new_fonts + nfonts1, sizeof *new_fonts * nfonts2);
2783 xfree (fonts1);
2784 xfree (fonts2);
2785 return new_fonts;
2789 /* Get a sorted list of fonts of family FAMILY on frame F.
2791 If PATTERN is non-nil list fonts matching that pattern.
2793 If REGISTRY is non-nil, return fonts with that registry and the
2794 alternative registries from Vface_alternative_font_registry_alist.
2796 If REGISTRY is nil return fonts of any registry.
2798 Set *FONTS to a vector of font_name structures allocated from the
2799 heap containing the fonts found. Value is the number of fonts
2800 found. */
2802 static int
2803 font_list (f, pattern, family, registry, fonts)
2804 struct frame *f;
2805 Lisp_Object pattern, family, registry;
2806 struct font_name **fonts;
2808 int nfonts = font_list_1 (f, pattern, family, registry, fonts);
2810 if (!NILP (registry)
2811 && CONSP (Vface_alternative_font_registry_alist))
2813 Lisp_Object alter;
2815 alter = Fassoc (registry, Vface_alternative_font_registry_alist);
2816 if (CONSP (alter))
2818 int reg_prio, i;
2820 for (alter = XCDR (alter), reg_prio = 1;
2821 CONSP (alter);
2822 alter = XCDR (alter), reg_prio++)
2823 if (STRINGP (XCAR (alter)))
2825 int nfonts2;
2826 struct font_name *fonts2;
2828 nfonts2 = font_list_1 (f, pattern, family, XCAR (alter),
2829 &fonts2);
2830 for (i = 0; i < nfonts2; i++)
2831 fonts2[i].registry_priority = reg_prio;
2832 *fonts = (nfonts > 0
2833 ? concat_font_list (*fonts, nfonts, fonts2, nfonts2)
2834 : fonts2);
2835 nfonts += nfonts2;
2840 return nfonts;
2844 /* Remove elements from LIST whose cars are `equal'. Called from
2845 x-family-fonts and x-font-family-list to remove duplicate font
2846 entries. */
2848 static void
2849 remove_duplicates (list)
2850 Lisp_Object list;
2852 Lisp_Object tail = list;
2854 while (!NILP (tail) && !NILP (XCDR (tail)))
2856 Lisp_Object next = XCDR (tail);
2857 if (!NILP (Fequal (XCAR (next), XCAR (tail))))
2858 XSETCDR (tail, XCDR (next));
2859 else
2860 tail = XCDR (tail);
2865 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
2866 doc: /* Return a list of available fonts of family FAMILY on FRAME.
2867 If FAMILY is omitted or nil, list all families.
2868 Otherwise, FAMILY must be a string, possibly containing wildcards
2869 `?' and `*'.
2870 If FRAME is omitted or nil, use the selected frame.
2871 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
2872 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
2873 FAMILY is the font family name. POINT-SIZE is the size of the
2874 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
2875 width, weight and slant of the font. These symbols are the same as for
2876 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
2877 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
2878 giving the registry and encoding of the font.
2879 The result list is sorted according to the current setting of
2880 the face font sort order. */)
2881 (family, frame)
2882 Lisp_Object family, frame;
2884 struct frame *f = check_x_frame (frame);
2885 struct font_name *fonts;
2886 int i, nfonts;
2887 Lisp_Object result;
2888 struct gcpro gcpro1;
2890 if (!NILP (family))
2891 CHECK_STRING (family);
2893 result = Qnil;
2894 GCPRO1 (result);
2895 nfonts = font_list (f, Qnil, family, Qnil, &fonts);
2896 for (i = nfonts - 1; i >= 0; --i)
2898 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
2899 char *tem;
2901 ASET (v, 0, build_string (fonts[i].fields[XLFD_FAMILY]));
2902 ASET (v, 1, xlfd_symbolic_swidth (fonts + i));
2903 ASET (v, 2, make_number (xlfd_point_size (f, fonts + i)));
2904 ASET (v, 3, xlfd_symbolic_weight (fonts + i));
2905 ASET (v, 4, xlfd_symbolic_slant (fonts + i));
2906 ASET (v, 5, xlfd_fixed_p (fonts + i) ? Qt : Qnil);
2907 tem = build_font_name (fonts + i);
2908 ASET (v, 6, build_string (tem));
2909 sprintf (tem, "%s-%s", fonts[i].fields[XLFD_REGISTRY],
2910 fonts[i].fields[XLFD_ENCODING]);
2911 ASET (v, 7, build_string (tem));
2912 xfree (tem);
2914 result = Fcons (v, result);
2917 remove_duplicates (result);
2918 free_font_names (fonts, nfonts);
2919 UNGCPRO;
2920 return result;
2924 DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
2925 0, 1, 0,
2926 doc: /* Return a list of available font families on FRAME.
2927 If FRAME is omitted or nil, use the selected frame.
2928 Value is a list of conses (FAMILY . FIXED-P) where FAMILY
2929 is a font family, and FIXED-P is non-nil if fonts of that family
2930 are fixed-pitch. */)
2931 (frame)
2932 Lisp_Object frame;
2934 struct frame *f = check_x_frame (frame);
2935 int nfonts, i;
2936 struct font_name *fonts;
2937 Lisp_Object result;
2938 struct gcpro gcpro1;
2939 int count = SPECPDL_INDEX ();
2941 /* Let's consider all fonts. */
2942 specbind (intern ("font-list-limit"), make_number (-1));
2943 nfonts = font_list (f, Qnil, Qnil, Qnil, &fonts);
2945 result = Qnil;
2946 GCPRO1 (result);
2947 for (i = nfonts - 1; i >= 0; --i)
2948 result = Fcons (Fcons (build_string (fonts[i].fields[XLFD_FAMILY]),
2949 xlfd_fixed_p (fonts + i) ? Qt : Qnil),
2950 result);
2952 remove_duplicates (result);
2953 free_font_names (fonts, nfonts);
2954 UNGCPRO;
2955 return unbind_to (count, result);
2959 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
2960 doc: /* Return a list of the names of available fonts matching PATTERN.
2961 If optional arguments FACE and FRAME are specified, return only fonts
2962 the same size as FACE on FRAME.
2963 PATTERN is a string, perhaps with wildcard characters;
2964 the * character matches any substring, and
2965 the ? character matches any single character.
2966 PATTERN is case-insensitive.
2967 FACE is a face name--a symbol.
2969 The return value is a list of strings, suitable as arguments to
2970 set-face-font.
2972 Fonts Emacs can't use may or may not be excluded
2973 even if they match PATTERN and FACE.
2974 The optional fourth argument MAXIMUM sets a limit on how many
2975 fonts to match. The first MAXIMUM fonts are reported.
2976 The optional fifth argument WIDTH, if specified, is a number of columns
2977 occupied by a character of a font. In that case, return only fonts
2978 the WIDTH times as wide as FACE on FRAME. */)
2979 (pattern, face, frame, maximum, width)
2980 Lisp_Object pattern, face, frame, maximum, width;
2982 struct frame *f;
2983 int size;
2984 int maxnames;
2986 check_x ();
2987 CHECK_STRING (pattern);
2989 if (NILP (maximum))
2990 maxnames = -1;
2991 else
2993 CHECK_NATNUM (maximum);
2994 maxnames = XINT (maximum);
2997 if (!NILP (width))
2998 CHECK_NUMBER (width);
3000 /* We can't simply call check_x_frame because this function may be
3001 called before any frame is created. */
3002 f = frame_or_selected_frame (frame, 2);
3003 if (!FRAME_WINDOW_P (f))
3005 /* Perhaps we have not yet created any frame. */
3006 f = NULL;
3007 face = Qnil;
3010 /* Determine the width standard for comparison with the fonts we find. */
3012 if (NILP (face))
3013 size = 0;
3014 else
3016 /* This is of limited utility since it works with character
3017 widths. Keep it for compatibility. --gerd. */
3018 int face_id = lookup_named_face (f, face, 0, 0);
3019 struct face *face = (face_id < 0
3020 ? NULL
3021 : FACE_FROM_ID (f, face_id));
3023 #ifdef WINDOWSNT
3024 /* For historic reasons, FONT_WIDTH refers to average width on W32,
3025 not maximum as on X. Redefine here. */
3026 #undef FONT_WIDTH
3027 #define FONT_WIDTH FONT_MAX_WIDTH
3028 #endif
3030 if (face && face->font)
3031 size = FONT_WIDTH (face->font);
3032 else
3033 size = FONT_WIDTH (FRAME_FONT (f)); /* FRAME_COLUMN_WIDTH (f) */
3035 if (!NILP (width))
3036 size *= XINT (width);
3040 Lisp_Object args[2];
3042 args[0] = x_list_fonts (f, pattern, size, maxnames);
3043 if (f == NULL)
3044 /* We don't have to check fontsets. */
3045 return args[0];
3046 args[1] = list_fontsets (f, pattern, size);
3047 return Fnconc (2, args);
3051 #endif /* HAVE_WINDOW_SYSTEM */
3055 /***********************************************************************
3056 Lisp Faces
3057 ***********************************************************************/
3059 /* Access face attributes of face LFACE, a Lisp vector. */
3061 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
3062 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
3063 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
3064 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
3065 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
3066 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
3067 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
3068 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
3069 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
3070 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
3071 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
3072 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
3073 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
3074 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
3075 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
3076 #define LFACE_AVGWIDTH(LFACE) AREF ((LFACE), LFACE_AVGWIDTH_INDEX)
3078 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
3079 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
3081 #define LFACEP(LFACE) \
3082 (VECTORP (LFACE) \
3083 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
3084 && EQ (AREF (LFACE, 0), Qface))
3087 #if GLYPH_DEBUG
3089 /* Check consistency of Lisp face attribute vector ATTRS. */
3091 static void
3092 check_lface_attrs (attrs)
3093 Lisp_Object *attrs;
3095 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
3096 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
3097 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
3098 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
3099 xassert (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
3100 || INTEGERP (attrs[LFACE_AVGWIDTH_INDEX]));
3101 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
3102 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
3103 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
3104 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
3105 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
3106 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
3107 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
3108 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
3109 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
3110 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
3111 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
3112 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
3113 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
3114 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
3115 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3116 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3117 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
3118 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
3119 || SYMBOLP (attrs[LFACE_BOX_INDEX])
3120 || STRINGP (attrs[LFACE_BOX_INDEX])
3121 || INTEGERP (attrs[LFACE_BOX_INDEX])
3122 || CONSP (attrs[LFACE_BOX_INDEX]));
3123 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
3124 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
3125 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
3126 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
3127 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
3128 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
3129 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
3130 || NILP (attrs[LFACE_INHERIT_INDEX])
3131 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
3132 || CONSP (attrs[LFACE_INHERIT_INDEX]));
3133 #ifdef HAVE_WINDOW_SYSTEM
3134 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
3135 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
3136 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
3137 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
3138 || NILP (attrs[LFACE_FONT_INDEX])
3139 || STRINGP (attrs[LFACE_FONT_INDEX]));
3140 #endif
3144 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
3146 static void
3147 check_lface (lface)
3148 Lisp_Object lface;
3150 if (!NILP (lface))
3152 xassert (LFACEP (lface));
3153 check_lface_attrs (XVECTOR (lface)->contents);
3157 #else /* GLYPH_DEBUG == 0 */
3159 #define check_lface_attrs(attrs) (void) 0
3160 #define check_lface(lface) (void) 0
3162 #endif /* GLYPH_DEBUG == 0 */
3166 /* Face-merge cycle checking. */
3168 /* A `named merge point' is simply a point during face-merging where we
3169 look up a face by name. We keep a stack of which named lookups we're
3170 currently processing so that we can easily detect cycles, using a
3171 linked- list of struct named_merge_point structures, typically
3172 allocated on the stack frame of the named lookup functions which are
3173 active (so no consing is required). */
3174 struct named_merge_point
3176 Lisp_Object face_name;
3177 struct named_merge_point *prev;
3181 /* If a face merging cycle is detected for FACE_NAME, return 0,
3182 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
3183 FACE_NAME, as the head of the linked list pointed to by
3184 NAMED_MERGE_POINTS, and return 1. */
3186 static INLINE int
3187 push_named_merge_point (struct named_merge_point *new_named_merge_point,
3188 Lisp_Object face_name,
3189 struct named_merge_point **named_merge_points)
3191 struct named_merge_point *prev;
3193 for (prev = *named_merge_points; prev; prev = prev->prev)
3194 if (EQ (face_name, prev->face_name))
3195 return 0;
3197 new_named_merge_point->face_name = face_name;
3198 new_named_merge_point->prev = *named_merge_points;
3200 *named_merge_points = new_named_merge_point;
3202 return 1;
3208 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
3209 to make it a symvol. If FACE_NAME is an alias for another face,
3210 return that face's name. */
3212 static Lisp_Object
3213 resolve_face_name (face_name)
3214 Lisp_Object face_name;
3216 Lisp_Object aliased;
3218 if (STRINGP (face_name))
3219 face_name = intern (SDATA (face_name));
3221 while (SYMBOLP (face_name))
3223 aliased = Fget (face_name, Qface_alias);
3224 if (NILP (aliased))
3225 break;
3226 else
3227 face_name = aliased;
3230 return face_name;
3234 /* Return the face definition of FACE_NAME on frame F. F null means
3235 return the definition for new frames. FACE_NAME may be a string or
3236 a symbol (apparently Emacs 20.2 allowed strings as face names in
3237 face text properties; Ediff uses that). If FACE_NAME is an alias
3238 for another face, return that face's definition. If SIGNAL_P is
3239 non-zero, signal an error if FACE_NAME is not a valid face name.
3240 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
3241 name. */
3243 static INLINE Lisp_Object
3244 lface_from_face_name (f, face_name, signal_p)
3245 struct frame *f;
3246 Lisp_Object face_name;
3247 int signal_p;
3249 Lisp_Object lface;
3251 face_name = resolve_face_name (face_name);
3253 if (f)
3254 lface = assq_no_quit (face_name, f->face_alist);
3255 else
3256 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
3258 if (CONSP (lface))
3259 lface = XCDR (lface);
3260 else if (signal_p)
3261 signal_error ("Invalid face", face_name);
3263 check_lface (lface);
3264 return lface;
3268 /* Get face attributes of face FACE_NAME from frame-local faces on
3269 frame F. Store the resulting attributes in ATTRS which must point
3270 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
3271 is non-zero, signal an error if FACE_NAME does not name a face.
3272 Otherwise, value is zero if FACE_NAME is not a face. */
3274 static INLINE int
3275 get_lface_attributes (f, face_name, attrs, signal_p)
3276 struct frame *f;
3277 Lisp_Object face_name;
3278 Lisp_Object *attrs;
3279 int signal_p;
3281 Lisp_Object lface;
3282 int success_p;
3284 lface = lface_from_face_name (f, face_name, signal_p);
3285 if (!NILP (lface))
3287 bcopy (XVECTOR (lface)->contents, attrs,
3288 LFACE_VECTOR_SIZE * sizeof *attrs);
3289 success_p = 1;
3291 else
3292 success_p = 0;
3294 return success_p;
3298 /* Non-zero if all attributes in face attribute vector ATTRS are
3299 specified, i.e. are non-nil. */
3301 static int
3302 lface_fully_specified_p (attrs)
3303 Lisp_Object *attrs;
3305 int i;
3307 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3308 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
3309 && i != LFACE_AVGWIDTH_INDEX)
3310 if (UNSPECIFIEDP (attrs[i])
3311 #ifdef MAC_OS
3312 /* MAC_TODO: No stipple support on Mac OS yet, this index is
3313 always unspecified. */
3314 && i != LFACE_STIPPLE_INDEX
3315 #endif
3317 break;
3319 return i == LFACE_VECTOR_SIZE;
3322 #ifdef HAVE_WINDOW_SYSTEM
3324 /* Set font-related attributes of Lisp face LFACE from the fullname of
3325 the font opened by FONTNAME. If FORCE_P is zero, set only
3326 unspecified attributes of LFACE. The exception is `font'
3327 attribute. It is set to FONTNAME as is regardless of FORCE_P.
3329 If FONTNAME is not available on frame F,
3330 return 0 if MAY_FAIL_P is non-zero, otherwise abort.
3331 If the fullname is not in a valid XLFD format,
3332 return 0 if MAY_FAIL_P is non-zero, otherwise set normal values
3333 in LFACE and return 1.
3334 Otherwise, return 1. */
3336 static int
3337 set_lface_from_font_name (f, lface, fontname, force_p, may_fail_p)
3338 struct frame *f;
3339 Lisp_Object lface;
3340 Lisp_Object fontname;
3341 int force_p, may_fail_p;
3343 struct font_name font;
3344 char *buffer;
3345 int pt;
3346 int have_xlfd_p;
3347 int fontset;
3348 char *font_name = SDATA (fontname);
3349 struct font_info *font_info;
3351 /* If FONTNAME is actually a fontset name, get ASCII font name of it. */
3352 fontset = fs_query_fontset (fontname, 0);
3353 if (fontset >= 0)
3354 font_name = SDATA (fontset_ascii (fontset));
3356 /* Check if FONT_NAME is surely available on the system. Usually
3357 FONT_NAME is already cached for the frame F and FS_LOAD_FONT
3358 returns quickly. But, even if FONT_NAME is not yet cached,
3359 caching it now is not futail because we anyway load the font
3360 later. */
3361 BLOCK_INPUT;
3362 font_info = FS_LOAD_FONT (f, 0, font_name, -1);
3363 UNBLOCK_INPUT;
3365 if (!font_info)
3367 if (may_fail_p)
3368 return 0;
3369 abort ();
3372 font.name = STRDUPA (font_info->full_name);
3373 have_xlfd_p = split_font_name (f, &font, 1);
3375 /* Set attributes only if unspecified, otherwise face defaults for
3376 new frames would never take effect. If we couldn't get a font
3377 name conforming to XLFD, set normal values. */
3379 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
3381 Lisp_Object val;
3382 if (have_xlfd_p)
3384 buffer = (char *) alloca (strlen (font.fields[XLFD_FAMILY])
3385 + strlen (font.fields[XLFD_FOUNDRY])
3386 + 2);
3387 sprintf (buffer, "%s-%s", font.fields[XLFD_FOUNDRY],
3388 font.fields[XLFD_FAMILY]);
3389 val = build_string (buffer);
3391 else
3392 val = build_string ("*");
3393 LFACE_FAMILY (lface) = val;
3396 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
3398 if (have_xlfd_p)
3399 pt = xlfd_point_size (f, &font);
3400 else
3401 pt = pixel_point_size (f, font_info->height * 10);
3402 xassert (pt > 0);
3403 LFACE_HEIGHT (lface) = make_number (pt);
3406 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
3407 LFACE_SWIDTH (lface)
3408 = have_xlfd_p ? xlfd_symbolic_swidth (&font) : Qnormal;
3410 if (force_p || UNSPECIFIEDP (LFACE_AVGWIDTH (lface)))
3411 LFACE_AVGWIDTH (lface)
3412 = (have_xlfd_p
3413 ? make_number (font.numeric[XLFD_AVGWIDTH])
3414 : Qunspecified);
3416 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
3417 LFACE_WEIGHT (lface)
3418 = have_xlfd_p ? xlfd_symbolic_weight (&font) : Qnormal;
3420 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
3421 LFACE_SLANT (lface)
3422 = have_xlfd_p ? xlfd_symbolic_slant (&font) : Qnormal;
3424 LFACE_FONT (lface) = fontname;
3426 return 1;
3429 #endif /* HAVE_WINDOW_SYSTEM */
3432 /* Merges the face height FROM with the face height TO, and returns the
3433 merged height. If FROM is an invalid height, then INVALID is
3434 returned instead. FROM and TO may be either absolute face heights or
3435 `relative' heights; the returned value is always an absolute height
3436 unless both FROM and TO are relative. GCPRO is a lisp value that
3437 will be protected from garbage-collection if this function makes a
3438 call into lisp. */
3440 Lisp_Object
3441 merge_face_heights (from, to, invalid)
3442 Lisp_Object from, to, invalid;
3444 Lisp_Object result = invalid;
3446 if (INTEGERP (from))
3447 /* FROM is absolute, just use it as is. */
3448 result = from;
3449 else if (FLOATP (from))
3450 /* FROM is a scale, use it to adjust TO. */
3452 if (INTEGERP (to))
3453 /* relative X absolute => absolute */
3454 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
3455 else if (FLOATP (to))
3456 /* relative X relative => relative */
3457 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
3458 else if (UNSPECIFIEDP (to))
3459 result = from;
3461 else if (FUNCTIONP (from))
3462 /* FROM is a function, which use to adjust TO. */
3464 /* Call function with current height as argument.
3465 From is the new height. */
3466 Lisp_Object args[2];
3468 args[0] = from;
3469 args[1] = to;
3470 result = safe_call (2, args);
3472 /* Ensure that if TO was absolute, so is the result. */
3473 if (INTEGERP (to) && !INTEGERP (result))
3474 result = invalid;
3477 return result;
3481 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
3482 store the resulting attributes in TO, which must be already be
3483 completely specified and contain only absolute attributes. Every
3484 specified attribute of FROM overrides the corresponding attribute of
3485 TO; relative attributes in FROM are merged with the absolute value in
3486 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
3487 loops in face inheritance; it should be 0 when called from other
3488 places. */
3490 static INLINE void
3491 merge_face_vectors (f, from, to, named_merge_points)
3492 struct frame *f;
3493 Lisp_Object *from, *to;
3494 struct named_merge_point *named_merge_points;
3496 int i;
3498 /* If FROM inherits from some other faces, merge their attributes into
3499 TO before merging FROM's direct attributes. Note that an :inherit
3500 attribute of `unspecified' is the same as one of nil; we never
3501 merge :inherit attributes, so nil is more correct, but lots of
3502 other code uses `unspecified' as a generic value for face attributes. */
3503 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
3504 && !NILP (from[LFACE_INHERIT_INDEX]))
3505 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
3507 /* If TO specifies a :font attribute, and FROM specifies some
3508 font-related attribute, we need to clear TO's :font attribute
3509 (because it will be inconsistent with whatever FROM specifies, and
3510 FROM takes precedence). */
3511 if (!NILP (to[LFACE_FONT_INDEX])
3512 && (!UNSPECIFIEDP (from[LFACE_FAMILY_INDEX])
3513 || !UNSPECIFIEDP (from[LFACE_HEIGHT_INDEX])
3514 || !UNSPECIFIEDP (from[LFACE_WEIGHT_INDEX])
3515 || !UNSPECIFIEDP (from[LFACE_SLANT_INDEX])
3516 || !UNSPECIFIEDP (from[LFACE_SWIDTH_INDEX])
3517 || !UNSPECIFIEDP (from[LFACE_AVGWIDTH_INDEX])))
3518 to[LFACE_FONT_INDEX] = Qnil;
3520 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3521 if (!UNSPECIFIEDP (from[i]))
3523 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
3524 to[i] = merge_face_heights (from[i], to[i], to[i]);
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 struct gcpro gcpro1;
3552 Lisp_Object from[LFACE_VECTOR_SIZE];
3553 int ok = get_lface_attributes (f, face_name, from, 0);
3555 if (ok)
3557 GCPRO1 (named_merge_point.face_name);
3558 merge_face_vectors (f, from, to, named_merge_points);
3559 UNGCPRO;
3562 return ok;
3564 else
3565 return 0;
3569 /* Merge face attributes from the lisp `face reference' FACE_REF on
3570 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
3571 problems with FACE_REF cause an error message to be shown. Return
3572 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
3573 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
3574 list structure; it may be 0 for most callers.
3576 FACE_REF may be a single face specification or a list of such
3577 specifications. Each face specification can be:
3579 1. A symbol or string naming a Lisp face.
3581 2. A property list of the form (KEYWORD VALUE ...) where each
3582 KEYWORD is a face attribute name, and value is an appropriate value
3583 for that attribute.
3585 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
3586 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
3587 for compatibility with 20.2.
3589 Face specifications earlier in lists take precedence over later
3590 specifications. */
3592 static int
3593 merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
3594 struct frame *f;
3595 Lisp_Object face_ref;
3596 Lisp_Object *to;
3597 int err_msgs;
3598 struct named_merge_point *named_merge_points;
3600 int ok = 1; /* Succeed without an error? */
3602 if (CONSP (face_ref))
3604 Lisp_Object first = XCAR (face_ref);
3606 if (EQ (first, Qforeground_color)
3607 || EQ (first, Qbackground_color))
3609 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
3610 . COLOR). COLOR must be a string. */
3611 Lisp_Object color_name = XCDR (face_ref);
3612 Lisp_Object color = first;
3614 if (STRINGP (color_name))
3616 if (EQ (color, Qforeground_color))
3617 to[LFACE_FOREGROUND_INDEX] = color_name;
3618 else
3619 to[LFACE_BACKGROUND_INDEX] = color_name;
3621 else
3623 if (err_msgs)
3624 add_to_log ("Invalid face color", color_name, Qnil);
3625 ok = 0;
3628 else if (SYMBOLP (first)
3629 && *SDATA (SYMBOL_NAME (first)) == ':')
3631 /* Assume this is the property list form. */
3632 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
3634 Lisp_Object keyword = XCAR (face_ref);
3635 Lisp_Object value = XCAR (XCDR (face_ref));
3636 int err = 0;
3638 /* Specifying `unspecified' is a no-op. */
3639 if (EQ (value, Qunspecified))
3641 else if (EQ (keyword, QCfamily))
3643 if (STRINGP (value))
3644 to[LFACE_FAMILY_INDEX] = value;
3645 else
3646 err = 1;
3648 else if (EQ (keyword, QCheight))
3650 Lisp_Object new_height =
3651 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
3653 if (! NILP (new_height))
3654 to[LFACE_HEIGHT_INDEX] = new_height;
3655 else
3656 err = 1;
3658 else if (EQ (keyword, QCweight))
3660 if (SYMBOLP (value)
3661 && face_numeric_weight (value) >= 0)
3662 to[LFACE_WEIGHT_INDEX] = value;
3663 else
3664 err = 1;
3666 else if (EQ (keyword, QCslant))
3668 if (SYMBOLP (value)
3669 && face_numeric_slant (value) >= 0)
3670 to[LFACE_SLANT_INDEX] = value;
3671 else
3672 err = 1;
3674 else if (EQ (keyword, QCunderline))
3676 if (EQ (value, Qt)
3677 || NILP (value)
3678 || STRINGP (value))
3679 to[LFACE_UNDERLINE_INDEX] = value;
3680 else
3681 err = 1;
3683 else if (EQ (keyword, QCoverline))
3685 if (EQ (value, Qt)
3686 || NILP (value)
3687 || STRINGP (value))
3688 to[LFACE_OVERLINE_INDEX] = value;
3689 else
3690 err = 1;
3692 else if (EQ (keyword, QCstrike_through))
3694 if (EQ (value, Qt)
3695 || NILP (value)
3696 || STRINGP (value))
3697 to[LFACE_STRIKE_THROUGH_INDEX] = value;
3698 else
3699 err = 1;
3701 else if (EQ (keyword, QCbox))
3703 if (EQ (value, Qt))
3704 value = make_number (1);
3705 if (INTEGERP (value)
3706 || STRINGP (value)
3707 || CONSP (value)
3708 || NILP (value))
3709 to[LFACE_BOX_INDEX] = value;
3710 else
3711 err = 1;
3713 else if (EQ (keyword, QCinverse_video)
3714 || EQ (keyword, QCreverse_video))
3716 if (EQ (value, Qt) || NILP (value))
3717 to[LFACE_INVERSE_INDEX] = value;
3718 else
3719 err = 1;
3721 else if (EQ (keyword, QCforeground))
3723 if (STRINGP (value))
3724 to[LFACE_FOREGROUND_INDEX] = value;
3725 else
3726 err = 1;
3728 else if (EQ (keyword, QCbackground))
3730 if (STRINGP (value))
3731 to[LFACE_BACKGROUND_INDEX] = value;
3732 else
3733 err = 1;
3735 else if (EQ (keyword, QCstipple))
3737 #ifdef HAVE_X_WINDOWS
3738 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
3739 if (!NILP (pixmap_p))
3740 to[LFACE_STIPPLE_INDEX] = value;
3741 else
3742 err = 1;
3743 #endif
3745 else if (EQ (keyword, QCwidth))
3747 if (SYMBOLP (value)
3748 && face_numeric_swidth (value) >= 0)
3749 to[LFACE_SWIDTH_INDEX] = value;
3750 else
3751 err = 1;
3753 else if (EQ (keyword, QCinherit))
3755 /* This is not really very useful; it's just like a
3756 normal face reference. */
3757 if (! merge_face_ref (f, value, to,
3758 err_msgs, named_merge_points))
3759 err = 1;
3761 else
3762 err = 1;
3764 if (err)
3766 add_to_log ("Invalid face attribute %S %S", keyword, value);
3767 ok = 0;
3770 face_ref = XCDR (XCDR (face_ref));
3773 else
3775 /* This is a list of face refs. Those at the beginning of the
3776 list take precedence over what follows, so we have to merge
3777 from the end backwards. */
3778 Lisp_Object next = XCDR (face_ref);
3780 if (! NILP (next))
3781 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
3783 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
3784 ok = 0;
3787 else
3789 /* FACE_REF ought to be a face name. */
3790 ok = merge_named_face (f, face_ref, to, named_merge_points);
3791 if (!ok && err_msgs)
3792 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
3795 return ok;
3799 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
3800 Sinternal_make_lisp_face, 1, 2, 0,
3801 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
3802 If FACE was not known as a face before, create a new one.
3803 If optional argument FRAME is specified, make a frame-local face
3804 for that frame. Otherwise operate on the global face definition.
3805 Value is a vector of face attributes. */)
3806 (face, frame)
3807 Lisp_Object face, frame;
3809 Lisp_Object global_lface, lface;
3810 struct frame *f;
3811 int i;
3813 CHECK_SYMBOL (face);
3814 global_lface = lface_from_face_name (NULL, face, 0);
3816 if (!NILP (frame))
3818 CHECK_LIVE_FRAME (frame);
3819 f = XFRAME (frame);
3820 lface = lface_from_face_name (f, face, 0);
3822 else
3823 f = NULL, lface = Qnil;
3825 /* Add a global definition if there is none. */
3826 if (NILP (global_lface))
3828 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3829 Qunspecified);
3830 AREF (global_lface, 0) = Qface;
3831 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
3832 Vface_new_frame_defaults);
3834 /* Assign the new Lisp face a unique ID. The mapping from Lisp
3835 face id to Lisp face is given by the vector lface_id_to_name.
3836 The mapping from Lisp face to Lisp face id is given by the
3837 property `face' of the Lisp face name. */
3838 if (next_lface_id == lface_id_to_name_size)
3840 int new_size = max (50, 2 * lface_id_to_name_size);
3841 int sz = new_size * sizeof *lface_id_to_name;
3842 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
3843 lface_id_to_name_size = new_size;
3846 lface_id_to_name[next_lface_id] = face;
3847 Fput (face, Qface, make_number (next_lface_id));
3848 ++next_lface_id;
3850 else if (f == NULL)
3851 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3852 AREF (global_lface, i) = Qunspecified;
3854 /* Add a frame-local definition. */
3855 if (f)
3857 if (NILP (lface))
3859 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3860 Qunspecified);
3861 AREF (lface, 0) = Qface;
3862 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
3864 else
3865 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3866 AREF (lface, i) = Qunspecified;
3868 else
3869 lface = global_lface;
3871 /* Changing a named face means that all realized faces depending on
3872 that face are invalid. Since we cannot tell which realized faces
3873 depend on the face, make sure they are all removed. This is done
3874 by incrementing face_change_count. The next call to
3875 init_iterator will then free realized faces. */
3876 if (NILP (Fget (face, Qface_no_inherit)))
3878 ++face_change_count;
3879 ++windows_or_buffers_changed;
3882 xassert (LFACEP (lface));
3883 check_lface (lface);
3884 return lface;
3888 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
3889 Sinternal_lisp_face_p, 1, 2, 0,
3890 doc: /* Return non-nil if FACE names a face.
3891 If optional second parameter FRAME is non-nil, check for the
3892 existence of a frame-local face with name FACE on that frame.
3893 Otherwise check for the existence of a global face. */)
3894 (face, frame)
3895 Lisp_Object face, frame;
3897 Lisp_Object lface;
3899 if (!NILP (frame))
3901 CHECK_LIVE_FRAME (frame);
3902 lface = lface_from_face_name (XFRAME (frame), face, 0);
3904 else
3905 lface = lface_from_face_name (NULL, face, 0);
3907 return lface;
3911 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
3912 Sinternal_copy_lisp_face, 4, 4, 0,
3913 doc: /* Copy face FROM to TO.
3914 If FRAME is t, copy the global face definition of FROM.
3915 Otherwise, copy the frame-local definition of FROM on FRAME.
3916 If NEW-FRAME is a frame, copy that data into the frame-local
3917 definition of TO on NEW-FRAME. If NEW-FRAME is nil.
3918 FRAME controls where the data is copied to.
3920 The value is TO. */)
3921 (from, to, frame, new_frame)
3922 Lisp_Object from, to, frame, new_frame;
3924 Lisp_Object lface, copy;
3926 CHECK_SYMBOL (from);
3927 CHECK_SYMBOL (to);
3929 if (EQ (frame, Qt))
3931 /* Copy global definition of FROM. We don't make copies of
3932 strings etc. because 20.2 didn't do it either. */
3933 lface = lface_from_face_name (NULL, from, 1);
3934 copy = Finternal_make_lisp_face (to, Qnil);
3936 else
3938 /* Copy frame-local definition of FROM. */
3939 if (NILP (new_frame))
3940 new_frame = frame;
3941 CHECK_LIVE_FRAME (frame);
3942 CHECK_LIVE_FRAME (new_frame);
3943 lface = lface_from_face_name (XFRAME (frame), from, 1);
3944 copy = Finternal_make_lisp_face (to, new_frame);
3947 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3948 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
3950 /* Changing a named face means that all realized faces depending on
3951 that face are invalid. Since we cannot tell which realized faces
3952 depend on the face, make sure they are all removed. This is done
3953 by incrementing face_change_count. The next call to
3954 init_iterator will then free realized faces. */
3955 if (NILP (Fget (to, Qface_no_inherit)))
3957 ++face_change_count;
3958 ++windows_or_buffers_changed;
3961 return to;
3965 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3966 Sinternal_set_lisp_face_attribute, 3, 4, 0,
3967 doc: /* Set attribute ATTR of FACE to VALUE.
3968 FRAME being a frame means change the face on that frame.
3969 FRAME nil means change the face of the selected frame.
3970 FRAME t means change the default for new frames.
3971 FRAME 0 means change the face on all frames, and change the default
3972 for new frames. */)
3973 (face, attr, value, frame)
3974 Lisp_Object face, attr, value, frame;
3976 Lisp_Object lface;
3977 Lisp_Object old_value = Qnil;
3978 /* Set 1 if ATTR is QCfont. */
3979 int font_attr_p = 0;
3980 /* Set 1 if ATTR is one of font-related attributes other than QCfont. */
3981 int font_related_attr_p = 0;
3983 CHECK_SYMBOL (face);
3984 CHECK_SYMBOL (attr);
3986 face = resolve_face_name (face);
3988 /* If FRAME is 0, change face on all frames, and change the
3989 default for new frames. */
3990 if (INTEGERP (frame) && XINT (frame) == 0)
3992 Lisp_Object tail;
3993 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
3994 FOR_EACH_FRAME (tail, frame)
3995 Finternal_set_lisp_face_attribute (face, attr, value, frame);
3996 return face;
3999 /* Set lface to the Lisp attribute vector of FACE. */
4000 if (EQ (frame, Qt))
4001 lface = lface_from_face_name (NULL, face, 1);
4002 else
4004 if (NILP (frame))
4005 frame = selected_frame;
4007 CHECK_LIVE_FRAME (frame);
4008 lface = lface_from_face_name (XFRAME (frame), face, 0);
4010 /* If a frame-local face doesn't exist yet, create one. */
4011 if (NILP (lface))
4012 lface = Finternal_make_lisp_face (face, frame);
4015 if (EQ (attr, QCfamily))
4017 if (!UNSPECIFIEDP (value))
4019 CHECK_STRING (value);
4020 if (SCHARS (value) == 0)
4021 signal_error ("Invalid face family", value);
4023 old_value = LFACE_FAMILY (lface);
4024 LFACE_FAMILY (lface) = value;
4025 font_related_attr_p = 1;
4027 else if (EQ (attr, QCheight))
4029 if (!UNSPECIFIEDP (value))
4031 Lisp_Object test;
4033 test = (EQ (face, Qdefault)
4034 ? value
4035 /* The default face must have an absolute size,
4036 otherwise, we do a test merge with a random
4037 height to see if VALUE's ok. */
4038 : merge_face_heights (value, make_number (10), Qnil));
4040 if (!INTEGERP (test) || XINT (test) <= 0)
4041 signal_error ("Invalid face height", value);
4044 old_value = LFACE_HEIGHT (lface);
4045 LFACE_HEIGHT (lface) = value;
4046 font_related_attr_p = 1;
4048 else if (EQ (attr, QCweight))
4050 if (!UNSPECIFIEDP (value))
4052 CHECK_SYMBOL (value);
4053 if (face_numeric_weight (value) < 0)
4054 signal_error ("Invalid face weight", value);
4056 old_value = LFACE_WEIGHT (lface);
4057 LFACE_WEIGHT (lface) = value;
4058 font_related_attr_p = 1;
4060 else if (EQ (attr, QCslant))
4062 if (!UNSPECIFIEDP (value))
4064 CHECK_SYMBOL (value);
4065 if (face_numeric_slant (value) < 0)
4066 signal_error ("Invalid face slant", value);
4068 old_value = LFACE_SLANT (lface);
4069 LFACE_SLANT (lface) = value;
4070 font_related_attr_p = 1;
4072 else if (EQ (attr, QCunderline))
4074 if (!UNSPECIFIEDP (value))
4075 if ((SYMBOLP (value)
4076 && !EQ (value, Qt)
4077 && !EQ (value, Qnil))
4078 /* Underline color. */
4079 || (STRINGP (value)
4080 && SCHARS (value) == 0))
4081 signal_error ("Invalid face underline", value);
4083 old_value = LFACE_UNDERLINE (lface);
4084 LFACE_UNDERLINE (lface) = value;
4086 else if (EQ (attr, QCoverline))
4088 if (!UNSPECIFIEDP (value))
4089 if ((SYMBOLP (value)
4090 && !EQ (value, Qt)
4091 && !EQ (value, Qnil))
4092 /* Overline color. */
4093 || (STRINGP (value)
4094 && SCHARS (value) == 0))
4095 signal_error ("Invalid face overline", value);
4097 old_value = LFACE_OVERLINE (lface);
4098 LFACE_OVERLINE (lface) = value;
4100 else if (EQ (attr, QCstrike_through))
4102 if (!UNSPECIFIEDP (value))
4103 if ((SYMBOLP (value)
4104 && !EQ (value, Qt)
4105 && !EQ (value, Qnil))
4106 /* Strike-through color. */
4107 || (STRINGP (value)
4108 && SCHARS (value) == 0))
4109 signal_error ("Invalid face strike-through", value);
4111 old_value = LFACE_STRIKE_THROUGH (lface);
4112 LFACE_STRIKE_THROUGH (lface) = value;
4114 else if (EQ (attr, QCbox))
4116 int valid_p;
4118 /* Allow t meaning a simple box of width 1 in foreground color
4119 of the face. */
4120 if (EQ (value, Qt))
4121 value = make_number (1);
4123 if (UNSPECIFIEDP (value))
4124 valid_p = 1;
4125 else if (NILP (value))
4126 valid_p = 1;
4127 else if (INTEGERP (value))
4128 valid_p = XINT (value) != 0;
4129 else if (STRINGP (value))
4130 valid_p = SCHARS (value) > 0;
4131 else if (CONSP (value))
4133 Lisp_Object tem;
4135 tem = value;
4136 while (CONSP (tem))
4138 Lisp_Object k, v;
4140 k = XCAR (tem);
4141 tem = XCDR (tem);
4142 if (!CONSP (tem))
4143 break;
4144 v = XCAR (tem);
4145 tem = XCDR (tem);
4147 if (EQ (k, QCline_width))
4149 if (!INTEGERP (v) || XINT (v) == 0)
4150 break;
4152 else if (EQ (k, QCcolor))
4154 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
4155 break;
4157 else if (EQ (k, QCstyle))
4159 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
4160 break;
4162 else
4163 break;
4166 valid_p = NILP (tem);
4168 else
4169 valid_p = 0;
4171 if (!valid_p)
4172 signal_error ("Invalid face box", value);
4174 old_value = LFACE_BOX (lface);
4175 LFACE_BOX (lface) = value;
4177 else if (EQ (attr, QCinverse_video)
4178 || EQ (attr, QCreverse_video))
4180 if (!UNSPECIFIEDP (value))
4182 CHECK_SYMBOL (value);
4183 if (!EQ (value, Qt) && !NILP (value))
4184 signal_error ("Invalid inverse-video face attribute value", value);
4186 old_value = LFACE_INVERSE (lface);
4187 LFACE_INVERSE (lface) = value;
4189 else if (EQ (attr, QCforeground))
4191 if (!UNSPECIFIEDP (value))
4193 /* Don't check for valid color names here because it depends
4194 on the frame (display) whether the color will be valid
4195 when the face is realized. */
4196 CHECK_STRING (value);
4197 if (SCHARS (value) == 0)
4198 signal_error ("Empty foreground color value", value);
4200 old_value = LFACE_FOREGROUND (lface);
4201 LFACE_FOREGROUND (lface) = value;
4203 else if (EQ (attr, QCbackground))
4205 if (!UNSPECIFIEDP (value))
4207 /* Don't check for valid color names here because it depends
4208 on the frame (display) whether the color will be valid
4209 when the face is realized. */
4210 CHECK_STRING (value);
4211 if (SCHARS (value) == 0)
4212 signal_error ("Empty background color value", value);
4214 old_value = LFACE_BACKGROUND (lface);
4215 LFACE_BACKGROUND (lface) = value;
4217 else if (EQ (attr, QCstipple))
4219 #ifdef HAVE_X_WINDOWS
4220 if (!UNSPECIFIEDP (value)
4221 && !NILP (value)
4222 && NILP (Fbitmap_spec_p (value)))
4223 signal_error ("Invalid stipple attribute", value);
4224 old_value = LFACE_STIPPLE (lface);
4225 LFACE_STIPPLE (lface) = value;
4226 #endif /* HAVE_X_WINDOWS */
4228 else if (EQ (attr, QCwidth))
4230 if (!UNSPECIFIEDP (value))
4232 CHECK_SYMBOL (value);
4233 if (face_numeric_swidth (value) < 0)
4234 signal_error ("Invalid face width", value);
4236 old_value = LFACE_SWIDTH (lface);
4237 LFACE_SWIDTH (lface) = value;
4238 font_related_attr_p = 1;
4240 else if (EQ (attr, QCfont))
4242 #ifdef HAVE_WINDOW_SYSTEM
4243 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
4245 /* Set font-related attributes of the Lisp face from an XLFD
4246 font name. */
4247 struct frame *f;
4248 Lisp_Object tmp;
4250 if (EQ (frame, Qt))
4251 f = SELECTED_FRAME ();
4252 else
4253 f = check_x_frame (frame);
4255 if (!UNSPECIFIEDP (value))
4257 CHECK_STRING (value);
4259 /* VALUE may be a fontset name or an alias of fontset. In
4260 such a case, use the base fontset name. */
4261 tmp = Fquery_fontset (value, Qnil);
4262 if (!NILP (tmp))
4263 value = tmp;
4265 if (!set_lface_from_font_name (f, lface, value, 1, 1))
4266 signal_error ("Invalid font or fontset name", value);
4269 font_attr_p = 1;
4271 #endif /* HAVE_WINDOW_SYSTEM */
4273 else if (EQ (attr, QCinherit))
4275 Lisp_Object tail;
4276 if (SYMBOLP (value))
4277 tail = Qnil;
4278 else
4279 for (tail = value; CONSP (tail); tail = XCDR (tail))
4280 if (!SYMBOLP (XCAR (tail)))
4281 break;
4282 if (NILP (tail))
4283 LFACE_INHERIT (lface) = value;
4284 else
4285 signal_error ("Invalid face inheritance", value);
4287 else if (EQ (attr, QCbold))
4289 old_value = LFACE_WEIGHT (lface);
4290 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
4291 font_related_attr_p = 1;
4293 else if (EQ (attr, QCitalic))
4295 old_value = LFACE_SLANT (lface);
4296 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
4297 font_related_attr_p = 1;
4299 else
4300 signal_error ("Invalid face attribute name", attr);
4302 if (font_related_attr_p
4303 && !UNSPECIFIEDP (value))
4304 /* If a font-related attribute other than QCfont is specified, the
4305 original `font' attribute nor that of default face is useless
4306 to determine a new font. Thus, we set it to nil so that font
4307 selection mechanism doesn't use it. */
4308 LFACE_FONT (lface) = Qnil;
4310 /* Changing a named face means that all realized faces depending on
4311 that face are invalid. Since we cannot tell which realized faces
4312 depend on the face, make sure they are all removed. This is done
4313 by incrementing face_change_count. The next call to
4314 init_iterator will then free realized faces. */
4315 if (!EQ (frame, Qt)
4316 && NILP (Fget (face, Qface_no_inherit))
4317 && (EQ (attr, QCfont)
4318 || NILP (Fequal (old_value, value))))
4320 ++face_change_count;
4321 ++windows_or_buffers_changed;
4324 if (!UNSPECIFIEDP (value)
4325 && NILP (Fequal (old_value, value)))
4327 Lisp_Object param;
4329 param = Qnil;
4331 if (EQ (face, Qdefault))
4333 #ifdef HAVE_WINDOW_SYSTEM
4334 /* Changed font-related attributes of the `default' face are
4335 reflected in changed `font' frame parameters. */
4336 if (FRAMEP (frame)
4337 && (font_related_attr_p || font_attr_p)
4338 && lface_fully_specified_p (XVECTOR (lface)->contents))
4339 set_font_frame_param (frame, lface);
4340 else
4341 #endif /* HAVE_WINDOW_SYSTEM */
4343 if (EQ (attr, QCforeground))
4344 param = Qforeground_color;
4345 else if (EQ (attr, QCbackground))
4346 param = Qbackground_color;
4348 #ifdef HAVE_WINDOW_SYSTEM
4349 #ifndef WINDOWSNT
4350 else if (EQ (face, Qscroll_bar))
4352 /* Changing the colors of `scroll-bar' sets frame parameters
4353 `scroll-bar-foreground' and `scroll-bar-background'. */
4354 if (EQ (attr, QCforeground))
4355 param = Qscroll_bar_foreground;
4356 else if (EQ (attr, QCbackground))
4357 param = Qscroll_bar_background;
4359 #endif /* not WINDOWSNT */
4360 else if (EQ (face, Qborder))
4362 /* Changing background color of `border' sets frame parameter
4363 `border-color'. */
4364 if (EQ (attr, QCbackground))
4365 param = Qborder_color;
4367 else if (EQ (face, Qcursor))
4369 /* Changing background color of `cursor' sets frame parameter
4370 `cursor-color'. */
4371 if (EQ (attr, QCbackground))
4372 param = Qcursor_color;
4374 else if (EQ (face, Qmouse))
4376 /* Changing background color of `mouse' sets frame parameter
4377 `mouse-color'. */
4378 if (EQ (attr, QCbackground))
4379 param = Qmouse_color;
4381 #endif /* HAVE_WINDOW_SYSTEM */
4382 else if (EQ (face, Qmenu))
4384 /* Indicate that we have to update the menu bar when
4385 realizing faces on FRAME. FRAME t change the
4386 default for new frames. We do this by setting
4387 setting the flag in new face caches */
4388 if (FRAMEP (frame))
4390 struct frame *f = XFRAME (frame);
4391 if (FRAME_FACE_CACHE (f) == NULL)
4392 FRAME_FACE_CACHE (f) = make_face_cache (f);
4393 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
4395 else
4396 menu_face_changed_default = 1;
4399 if (!NILP (param))
4401 if (EQ (frame, Qt))
4402 /* Update `default-frame-alist', which is used for new frames. */
4404 store_in_alist (&Vdefault_frame_alist, param, value);
4406 else
4407 /* Update the current frame's parameters. */
4409 Lisp_Object cons;
4410 cons = XCAR (Vparam_value_alist);
4411 XSETCAR (cons, param);
4412 XSETCDR (cons, value);
4413 Fmodify_frame_parameters (frame, Vparam_value_alist);
4418 return face;
4422 #ifdef HAVE_WINDOW_SYSTEM
4424 /* Set the `font' frame parameter of FRAME determined from `default'
4425 face attributes LFACE. If a face or fontset name is explicitely
4426 specfied in LFACE, use it as is. Otherwise, determine a font name
4427 from the other font-related atrributes of LFACE. In that case, if
4428 there's no matching font, signals an error. */
4430 static void
4431 set_font_frame_param (frame, lface)
4432 Lisp_Object frame, lface;
4434 struct frame *f = XFRAME (frame);
4436 if (FRAME_WINDOW_P (f))
4438 Lisp_Object font_name;
4439 char *font;
4441 if (STRINGP (LFACE_FONT (lface)))
4442 font_name = LFACE_FONT (lface);
4443 else
4445 /* Choose a font name that reflects LFACE's attributes and has
4446 the registry and encoding pattern specified in the default
4447 fontset (3rd arg: -1) for ASCII characters (4th arg: 0). */
4448 font = choose_face_font (f, XVECTOR (lface)->contents, -1, 0, 0);
4449 if (!font)
4450 error ("No font matches the specified attribute");
4451 font_name = build_string (font);
4452 xfree (font);
4455 f->default_face_done_p = 0;
4456 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font_name), Qnil));
4461 /* Update the corresponding face when frame parameter PARAM on frame F
4462 has been assigned the value NEW_VALUE. */
4464 void
4465 update_face_from_frame_parameter (f, param, new_value)
4466 struct frame *f;
4467 Lisp_Object param, new_value;
4469 Lisp_Object face = Qnil;
4470 Lisp_Object lface;
4472 /* If there are no faces yet, give up. This is the case when called
4473 from Fx_create_frame, and we do the necessary things later in
4474 face-set-after-frame-defaults. */
4475 if (NILP (f->face_alist))
4476 return;
4478 if (EQ (param, Qforeground_color))
4480 face = Qdefault;
4481 lface = lface_from_face_name (f, face, 1);
4482 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
4483 ? new_value : Qunspecified);
4484 realize_basic_faces (f);
4486 else if (EQ (param, Qbackground_color))
4488 Lisp_Object frame;
4490 /* Changing the background color might change the background
4491 mode, so that we have to load new defface specs. Call
4492 frame-update-face-colors to do that. */
4493 XSETFRAME (frame, f);
4494 call1 (Qframe_update_face_colors, frame);
4496 face = Qdefault;
4497 lface = lface_from_face_name (f, face, 1);
4498 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4499 ? new_value : Qunspecified);
4500 realize_basic_faces (f);
4502 else if (EQ (param, Qborder_color))
4504 face = Qborder;
4505 lface = lface_from_face_name (f, face, 1);
4506 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4507 ? new_value : Qunspecified);
4509 else if (EQ (param, Qcursor_color))
4511 face = Qcursor;
4512 lface = lface_from_face_name (f, face, 1);
4513 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4514 ? new_value : Qunspecified);
4516 else if (EQ (param, Qmouse_color))
4518 face = Qmouse;
4519 lface = lface_from_face_name (f, face, 1);
4520 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4521 ? new_value : Qunspecified);
4524 /* Changing a named face means that all realized faces depending on
4525 that face are invalid. Since we cannot tell which realized faces
4526 depend on the face, make sure they are all removed. This is done
4527 by incrementing face_change_count. The next call to
4528 init_iterator will then free realized faces. */
4529 if (!NILP (face)
4530 && NILP (Fget (face, Qface_no_inherit)))
4532 ++face_change_count;
4533 ++windows_or_buffers_changed;
4538 /* Get the value of X resource RESOURCE, class CLASS for the display
4539 of frame FRAME. This is here because ordinary `x-get-resource'
4540 doesn't take a frame argument. */
4542 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
4543 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
4544 (resource, class, frame)
4545 Lisp_Object resource, class, frame;
4547 Lisp_Object value = Qnil;
4548 CHECK_STRING (resource);
4549 CHECK_STRING (class);
4550 CHECK_LIVE_FRAME (frame);
4551 BLOCK_INPUT;
4552 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
4553 resource, class, Qnil, Qnil);
4554 UNBLOCK_INPUT;
4555 return value;
4559 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
4560 If VALUE is "on" or "true", return t. If VALUE is "off" or
4561 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
4562 error; if SIGNAL_P is zero, return 0. */
4564 static Lisp_Object
4565 face_boolean_x_resource_value (value, signal_p)
4566 Lisp_Object value;
4567 int signal_p;
4569 Lisp_Object result = make_number (0);
4571 xassert (STRINGP (value));
4573 if (xstricmp (SDATA (value), "on") == 0
4574 || xstricmp (SDATA (value), "true") == 0)
4575 result = Qt;
4576 else if (xstricmp (SDATA (value), "off") == 0
4577 || xstricmp (SDATA (value), "false") == 0)
4578 result = Qnil;
4579 else if (xstricmp (SDATA (value), "unspecified") == 0)
4580 result = Qunspecified;
4581 else if (signal_p)
4582 signal_error ("Invalid face attribute value from X resource", value);
4584 return result;
4588 DEFUN ("internal-set-lisp-face-attribute-from-resource",
4589 Finternal_set_lisp_face_attribute_from_resource,
4590 Sinternal_set_lisp_face_attribute_from_resource,
4591 3, 4, 0, doc: /* */)
4592 (face, attr, value, frame)
4593 Lisp_Object face, attr, value, frame;
4595 CHECK_SYMBOL (face);
4596 CHECK_SYMBOL (attr);
4597 CHECK_STRING (value);
4599 if (xstricmp (SDATA (value), "unspecified") == 0)
4600 value = Qunspecified;
4601 else if (EQ (attr, QCheight))
4603 value = Fstring_to_number (value, make_number (10));
4604 if (XINT (value) <= 0)
4605 signal_error ("Invalid face height from X resource", value);
4607 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
4608 value = face_boolean_x_resource_value (value, 1);
4609 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
4610 value = intern (SDATA (value));
4611 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
4612 value = face_boolean_x_resource_value (value, 1);
4613 else if (EQ (attr, QCunderline)
4614 || EQ (attr, QCoverline)
4615 || EQ (attr, QCstrike_through))
4617 Lisp_Object boolean_value;
4619 /* If the result of face_boolean_x_resource_value is t or nil,
4620 VALUE does NOT specify a color. */
4621 boolean_value = face_boolean_x_resource_value (value, 0);
4622 if (SYMBOLP (boolean_value))
4623 value = boolean_value;
4625 else if (EQ (attr, QCbox))
4626 value = Fcar (Fread_from_string (value, Qnil, Qnil));
4628 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
4631 #endif /* HAVE_WINDOW_SYSTEM */
4634 /***********************************************************************
4635 Menu face
4636 ***********************************************************************/
4638 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
4640 /* Make menus on frame F appear as specified by the `menu' face. */
4642 static void
4643 x_update_menu_appearance (f)
4644 struct frame *f;
4646 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4647 XrmDatabase rdb;
4649 if (dpyinfo
4650 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
4651 rdb != NULL))
4653 char line[512];
4654 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
4655 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
4656 const char *myname = SDATA (Vx_resource_name);
4657 int changed_p = 0;
4658 #ifdef USE_MOTIF
4659 const char *popup_path = "popup_menu";
4660 #else
4661 const char *popup_path = "menu.popup";
4662 #endif
4664 if (STRINGP (LFACE_FOREGROUND (lface)))
4666 sprintf (line, "%s.%s*foreground: %s",
4667 myname, popup_path,
4668 SDATA (LFACE_FOREGROUND (lface)));
4669 XrmPutLineResource (&rdb, line);
4670 sprintf (line, "%s.pane.menubar*foreground: %s",
4671 myname, SDATA (LFACE_FOREGROUND (lface)));
4672 XrmPutLineResource (&rdb, line);
4673 changed_p = 1;
4676 if (STRINGP (LFACE_BACKGROUND (lface)))
4678 sprintf (line, "%s.%s*background: %s",
4679 myname, popup_path,
4680 SDATA (LFACE_BACKGROUND (lface)));
4681 XrmPutLineResource (&rdb, line);
4682 sprintf (line, "%s.pane.menubar*background: %s",
4683 myname, SDATA (LFACE_BACKGROUND (lface)));
4684 XrmPutLineResource (&rdb, line);
4685 changed_p = 1;
4688 if (face->font_name
4689 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
4690 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
4691 || !UNSPECIFIEDP (LFACE_AVGWIDTH (lface))
4692 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
4693 || !UNSPECIFIEDP (LFACE_SLANT (lface))
4694 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
4696 #ifdef USE_MOTIF
4697 const char *suffix = "List";
4698 #else
4699 const char *suffix = "";
4700 #endif
4701 sprintf (line, "%s.pane.menubar*font%s: %s",
4702 myname, suffix, face->font_name);
4703 XrmPutLineResource (&rdb, line);
4704 sprintf (line, "%s.%s*font%s: %s",
4705 myname, popup_path, suffix, face->font_name);
4706 XrmPutLineResource (&rdb, line);
4707 changed_p = 1;
4710 if (changed_p && f->output_data.x->menubar_widget)
4711 free_frame_menubar (f);
4715 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
4718 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
4719 Sface_attribute_relative_p,
4720 2, 2, 0,
4721 doc: /* Return non-nil if face ATTRIBUTE VALUE is relative. */)
4722 (attribute, value)
4723 Lisp_Object attribute, value;
4725 if (EQ (value, Qunspecified))
4726 return Qt;
4727 else if (EQ (attribute, QCheight))
4728 return INTEGERP (value) ? Qnil : Qt;
4729 else
4730 return Qnil;
4733 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
4734 3, 3, 0,
4735 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
4736 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
4737 the result will be absolute, otherwise it will be relative. */)
4738 (attribute, value1, value2)
4739 Lisp_Object attribute, value1, value2;
4741 if (EQ (value1, Qunspecified))
4742 return value2;
4743 else if (EQ (attribute, QCheight))
4744 return merge_face_heights (value1, value2, value1);
4745 else
4746 return value1;
4750 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
4751 Sinternal_get_lisp_face_attribute,
4752 2, 3, 0,
4753 doc: /* Return face attribute KEYWORD of face SYMBOL.
4754 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
4755 face attribute name, signal an error.
4756 If the optional argument FRAME is given, report on face SYMBOL in that
4757 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
4758 frames). If FRAME is omitted or nil, use the selected frame. */)
4759 (symbol, keyword, frame)
4760 Lisp_Object symbol, keyword, frame;
4762 Lisp_Object lface, value = Qnil;
4764 CHECK_SYMBOL (symbol);
4765 CHECK_SYMBOL (keyword);
4767 if (EQ (frame, Qt))
4768 lface = lface_from_face_name (NULL, symbol, 1);
4769 else
4771 if (NILP (frame))
4772 frame = selected_frame;
4773 CHECK_LIVE_FRAME (frame);
4774 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
4777 if (EQ (keyword, QCfamily))
4778 value = LFACE_FAMILY (lface);
4779 else if (EQ (keyword, QCheight))
4780 value = LFACE_HEIGHT (lface);
4781 else if (EQ (keyword, QCweight))
4782 value = LFACE_WEIGHT (lface);
4783 else if (EQ (keyword, QCslant))
4784 value = LFACE_SLANT (lface);
4785 else if (EQ (keyword, QCunderline))
4786 value = LFACE_UNDERLINE (lface);
4787 else if (EQ (keyword, QCoverline))
4788 value = LFACE_OVERLINE (lface);
4789 else if (EQ (keyword, QCstrike_through))
4790 value = LFACE_STRIKE_THROUGH (lface);
4791 else if (EQ (keyword, QCbox))
4792 value = LFACE_BOX (lface);
4793 else if (EQ (keyword, QCinverse_video)
4794 || EQ (keyword, QCreverse_video))
4795 value = LFACE_INVERSE (lface);
4796 else if (EQ (keyword, QCforeground))
4797 value = LFACE_FOREGROUND (lface);
4798 else if (EQ (keyword, QCbackground))
4799 value = LFACE_BACKGROUND (lface);
4800 else if (EQ (keyword, QCstipple))
4801 value = LFACE_STIPPLE (lface);
4802 else if (EQ (keyword, QCwidth))
4803 value = LFACE_SWIDTH (lface);
4804 else if (EQ (keyword, QCinherit))
4805 value = LFACE_INHERIT (lface);
4806 else if (EQ (keyword, QCfont))
4807 value = LFACE_FONT (lface);
4808 else
4809 signal_error ("Invalid face attribute name", keyword);
4811 return value;
4815 DEFUN ("internal-lisp-face-attribute-values",
4816 Finternal_lisp_face_attribute_values,
4817 Sinternal_lisp_face_attribute_values, 1, 1, 0,
4818 doc: /* Return a list of valid discrete values for face attribute ATTR.
4819 Value is nil if ATTR doesn't have a discrete set of valid values. */)
4820 (attr)
4821 Lisp_Object attr;
4823 Lisp_Object result = Qnil;
4825 CHECK_SYMBOL (attr);
4827 if (EQ (attr, QCweight)
4828 || EQ (attr, QCslant)
4829 || EQ (attr, QCwidth))
4831 /* Extract permissible symbols from tables. */
4832 struct table_entry *table;
4833 int i, dim;
4835 if (EQ (attr, QCweight))
4836 table = weight_table, dim = DIM (weight_table);
4837 else if (EQ (attr, QCslant))
4838 table = slant_table, dim = DIM (slant_table);
4839 else
4840 table = swidth_table, dim = DIM (swidth_table);
4842 for (i = 0; i < dim; ++i)
4844 Lisp_Object symbol = *table[i].symbol;
4845 Lisp_Object tail = result;
4847 while (!NILP (tail)
4848 && !EQ (XCAR (tail), symbol))
4849 tail = XCDR (tail);
4851 if (NILP (tail))
4852 result = Fcons (symbol, result);
4855 else if (EQ (attr, QCunderline))
4856 result = Fcons (Qt, Fcons (Qnil, Qnil));
4857 else if (EQ (attr, QCoverline))
4858 result = Fcons (Qt, Fcons (Qnil, Qnil));
4859 else if (EQ (attr, QCstrike_through))
4860 result = Fcons (Qt, Fcons (Qnil, Qnil));
4861 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
4862 result = Fcons (Qt, Fcons (Qnil, Qnil));
4864 return result;
4868 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
4869 Sinternal_merge_in_global_face, 2, 2, 0,
4870 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
4871 Default face attributes override any local face attributes. */)
4872 (face, frame)
4873 Lisp_Object face, frame;
4875 int i;
4876 Lisp_Object global_lface, local_lface, *gvec, *lvec;
4878 CHECK_LIVE_FRAME (frame);
4879 global_lface = lface_from_face_name (NULL, face, 1);
4880 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
4881 if (NILP (local_lface))
4882 local_lface = Finternal_make_lisp_face (face, frame);
4884 /* Make every specified global attribute override the local one.
4885 BEWARE!! This is only used from `face-set-after-frame-default' where
4886 the local frame is defined from default specs in `face-defface-spec'
4887 and those should be overridden by global settings. Hence the strange
4888 "global before local" priority. */
4889 lvec = XVECTOR (local_lface)->contents;
4890 gvec = XVECTOR (global_lface)->contents;
4891 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4892 if (! UNSPECIFIEDP (gvec[i]))
4893 lvec[i] = gvec[i];
4895 return Qnil;
4899 /* The following function is implemented for compatibility with 20.2.
4900 The function is used in x-resolve-fonts when it is asked to
4901 return fonts with the same size as the font of a face. This is
4902 done in fontset.el. */
4904 DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
4905 doc: /* Return the font name of face FACE, or nil if it is unspecified.
4906 If the optional argument FRAME is given, report on face FACE in that frame.
4907 If FRAME is t, report on the defaults for face FACE (for new frames).
4908 The font default for a face is either nil, or a list
4909 of the form (bold), (italic) or (bold italic).
4910 If FRAME is omitted or nil, use the selected frame. */)
4911 (face, frame)
4912 Lisp_Object face, frame;
4914 if (EQ (frame, Qt))
4916 Lisp_Object result = Qnil;
4917 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
4919 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
4920 && !EQ (LFACE_WEIGHT (lface), Qnormal))
4921 result = Fcons (Qbold, result);
4923 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
4924 && !EQ (LFACE_SLANT (lface), Qnormal))
4925 result = Fcons (Qitalic, result);
4927 return result;
4929 else
4931 struct frame *f = frame_or_selected_frame (frame, 1);
4932 int face_id = lookup_named_face (f, face, 0, 1);
4933 struct face *face = FACE_FROM_ID (f, face_id);
4934 return face ? build_string (face->font_name) : Qnil;
4939 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
4940 all attributes are `equal'. Tries to be fast because this function
4941 is called quite often. */
4943 static INLINE int
4944 face_attr_equal_p (v1, v2)
4945 Lisp_Object v1, v2;
4947 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
4948 and the other is specified. */
4949 if (XTYPE (v1) != XTYPE (v2))
4950 return 0;
4952 if (EQ (v1, v2))
4953 return 1;
4955 switch (XTYPE (v1))
4957 case Lisp_String:
4958 if (SBYTES (v1) != SBYTES (v2))
4959 return 0;
4961 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
4963 case Lisp_Int:
4964 case Lisp_Symbol:
4965 return 0;
4967 default:
4968 return !NILP (Fequal (v1, v2));
4973 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
4974 all attributes are `equal'. Tries to be fast because this function
4975 is called quite often. */
4977 static INLINE int
4978 lface_equal_p (v1, v2)
4979 Lisp_Object *v1, *v2;
4981 int i, equal_p = 1;
4983 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
4984 equal_p = face_attr_equal_p (v1[i], v2[i]);
4986 return equal_p;
4990 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
4991 Sinternal_lisp_face_equal_p, 2, 3, 0,
4992 doc: /* True if FACE1 and FACE2 are equal.
4993 If the optional argument FRAME is given, report on face FACE in that frame.
4994 If FRAME is t, report on the defaults for face FACE (for new frames).
4995 If FRAME is omitted or nil, use the selected frame. */)
4996 (face1, face2, frame)
4997 Lisp_Object face1, face2, frame;
4999 int equal_p;
5000 struct frame *f;
5001 Lisp_Object lface1, lface2;
5003 if (EQ (frame, Qt))
5004 f = NULL;
5005 else
5006 /* Don't use check_x_frame here because this function is called
5007 before X frames exist. At that time, if FRAME is nil,
5008 selected_frame will be used which is the frame dumped with
5009 Emacs. That frame is not an X frame. */
5010 f = frame_or_selected_frame (frame, 2);
5012 lface1 = lface_from_face_name (NULL, face1, 1);
5013 lface2 = lface_from_face_name (NULL, face2, 1);
5014 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
5015 XVECTOR (lface2)->contents);
5016 return equal_p ? Qt : Qnil;
5020 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
5021 Sinternal_lisp_face_empty_p, 1, 2, 0,
5022 doc: /* True if FACE has no attribute specified.
5023 If the optional argument FRAME is given, report on face FACE in that frame.
5024 If FRAME is t, report on the defaults for face FACE (for new frames).
5025 If FRAME is omitted or nil, use the selected frame. */)
5026 (face, frame)
5027 Lisp_Object face, frame;
5029 struct frame *f;
5030 Lisp_Object lface;
5031 int i;
5033 if (NILP (frame))
5034 frame = selected_frame;
5035 CHECK_LIVE_FRAME (frame);
5036 f = XFRAME (frame);
5038 if (EQ (frame, Qt))
5039 lface = lface_from_face_name (NULL, face, 1);
5040 else
5041 lface = lface_from_face_name (f, face, 1);
5043 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
5044 if (!UNSPECIFIEDP (AREF (lface, i)))
5045 break;
5047 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
5051 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
5052 0, 1, 0,
5053 doc: /* Return an alist of frame-local faces defined on FRAME.
5054 For internal use only. */)
5055 (frame)
5056 Lisp_Object frame;
5058 struct frame *f = frame_or_selected_frame (frame, 0);
5059 return f->face_alist;
5063 /* Return a hash code for Lisp string STRING with case ignored. Used
5064 below in computing a hash value for a Lisp face. */
5066 static INLINE unsigned
5067 hash_string_case_insensitive (string)
5068 Lisp_Object string;
5070 const unsigned char *s;
5071 unsigned hash = 0;
5072 xassert (STRINGP (string));
5073 for (s = SDATA (string); *s; ++s)
5074 hash = (hash << 1) ^ tolower (*s);
5075 return hash;
5079 /* Return a hash code for face attribute vector V. */
5081 static INLINE unsigned
5082 lface_hash (v)
5083 Lisp_Object *v;
5085 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
5086 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
5087 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
5088 ^ XFASTINT (v[LFACE_WEIGHT_INDEX])
5089 ^ XFASTINT (v[LFACE_SLANT_INDEX])
5090 ^ XFASTINT (v[LFACE_SWIDTH_INDEX])
5091 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
5095 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
5096 considering charsets/registries). They do if they specify the same
5097 family, point size, weight, width, slant, and fontset. Both LFACE1
5098 and LFACE2 must be fully-specified. */
5100 static INLINE int
5101 lface_same_font_attributes_p (lface1, lface2)
5102 Lisp_Object *lface1, *lface2;
5104 xassert (lface_fully_specified_p (lface1)
5105 && lface_fully_specified_p (lface2));
5106 return (xstricmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
5107 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
5108 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
5109 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
5110 && EQ (lface1[LFACE_AVGWIDTH_INDEX], lface2[LFACE_AVGWIDTH_INDEX])
5111 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
5112 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
5113 && (EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
5114 || (STRINGP (lface1[LFACE_FONT_INDEX])
5115 && STRINGP (lface2[LFACE_FONT_INDEX])
5116 && xstricmp (SDATA (lface1[LFACE_FONT_INDEX]),
5117 SDATA (lface2[LFACE_FONT_INDEX])))));
5122 /***********************************************************************
5123 Realized Faces
5124 ***********************************************************************/
5126 /* Allocate and return a new realized face for Lisp face attribute
5127 vector ATTR. */
5129 static struct face *
5130 make_realized_face (attr)
5131 Lisp_Object *attr;
5133 struct face *face = (struct face *) xmalloc (sizeof *face);
5134 bzero (face, sizeof *face);
5135 face->ascii_face = face;
5136 bcopy (attr, face->lface, sizeof face->lface);
5137 return face;
5141 /* Free realized face FACE, including its X resources. FACE may
5142 be null. */
5144 static void
5145 free_realized_face (f, face)
5146 struct frame *f;
5147 struct face *face;
5149 if (face)
5151 #ifdef HAVE_WINDOW_SYSTEM
5152 if (FRAME_WINDOW_P (f))
5154 /* Free fontset of FACE if it is ASCII face. */
5155 if (face->fontset >= 0 && face == face->ascii_face)
5156 free_face_fontset (f, face);
5157 if (face->gc)
5159 x_free_gc (f, face->gc);
5160 face->gc = 0;
5163 free_face_colors (f, face);
5164 x_destroy_bitmap (f, face->stipple);
5166 #endif /* HAVE_WINDOW_SYSTEM */
5168 xfree (face);
5173 /* Prepare face FACE for subsequent display on frame F. This
5174 allocated GCs if they haven't been allocated yet or have been freed
5175 by clearing the face cache. */
5177 void
5178 prepare_face_for_display (f, face)
5179 struct frame *f;
5180 struct face *face;
5182 #ifdef HAVE_WINDOW_SYSTEM
5183 xassert (FRAME_WINDOW_P (f));
5185 if (face->gc == 0)
5187 XGCValues xgcv;
5188 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
5190 xgcv.foreground = face->foreground;
5191 xgcv.background = face->background;
5192 #ifdef HAVE_X_WINDOWS
5193 xgcv.graphics_exposures = False;
5194 #endif
5195 /* The font of FACE may be null if we couldn't load it. */
5196 if (face->font)
5198 #ifdef HAVE_X_WINDOWS
5199 xgcv.font = face->font->fid;
5200 #endif
5201 #ifdef WINDOWSNT
5202 xgcv.font = face->font;
5203 #endif
5204 #ifdef MAC_OS
5205 xgcv.font = face->font;
5206 #endif
5207 mask |= GCFont;
5210 BLOCK_INPUT;
5211 #ifdef HAVE_X_WINDOWS
5212 if (face->stipple)
5214 xgcv.fill_style = FillOpaqueStippled;
5215 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
5216 mask |= GCFillStyle | GCStipple;
5218 #endif
5219 face->gc = x_create_gc (f, mask, &xgcv);
5220 UNBLOCK_INPUT;
5222 #endif /* HAVE_WINDOW_SYSTEM */
5226 /* Returns the `distance' between the colors X and Y. */
5228 static int
5229 color_distance (x, y)
5230 XColor *x, *y;
5232 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
5233 Quoting from that paper:
5235 This formula has results that are very close to L*u*v* (with the
5236 modified lightness curve) and, more importantly, it is a more even
5237 algorithm: it does not have a range of colours where it suddenly
5238 gives far from optimal results.
5240 See <http://www.compuphase.com/cmetric.htm> for more info. */
5242 long r = (x->red - y->red) >> 8;
5243 long g = (x->green - y->green) >> 8;
5244 long b = (x->blue - y->blue) >> 8;
5245 long r_mean = (x->red + y->red) >> 9;
5247 return
5248 (((512 + r_mean) * r * r) >> 8)
5249 + 4 * g * g
5250 + (((767 - r_mean) * b * b) >> 8);
5254 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
5255 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
5256 COLOR1 and COLOR2 may be either strings containing the color name,
5257 or lists of the form (RED GREEN BLUE).
5258 If FRAME is unspecified or nil, the current frame is used. */)
5259 (color1, color2, frame)
5260 Lisp_Object color1, color2, frame;
5262 struct frame *f;
5263 XColor cdef1, cdef2;
5265 if (NILP (frame))
5266 frame = selected_frame;
5267 CHECK_LIVE_FRAME (frame);
5268 f = XFRAME (frame);
5270 if ((CONSP (color1) && !parse_rgb_list (color1, &cdef1))
5271 || !STRINGP (color1)
5272 || !defined_color (f, SDATA (color1), &cdef1, 0))
5273 signal_error ("Invalid color", color1);
5274 if ((CONSP (color2) && !parse_rgb_list (color2, &cdef2))
5275 || !STRINGP (color2)
5276 || !defined_color (f, SDATA (color2), &cdef2, 0))
5277 signal_error ("Invalid color", color2);
5279 return make_number (color_distance (&cdef1, &cdef2));
5283 /***********************************************************************
5284 Face Cache
5285 ***********************************************************************/
5287 /* Return a new face cache for frame F. */
5289 static struct face_cache *
5290 make_face_cache (f)
5291 struct frame *f;
5293 struct face_cache *c;
5294 int size;
5296 c = (struct face_cache *) xmalloc (sizeof *c);
5297 bzero (c, sizeof *c);
5298 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5299 c->buckets = (struct face **) xmalloc (size);
5300 bzero (c->buckets, size);
5301 c->size = 50;
5302 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
5303 c->f = f;
5304 c->menu_face_changed_p = menu_face_changed_default;
5305 return c;
5309 /* Clear out all graphics contexts for all realized faces, except for
5310 the basic faces. This should be done from time to time just to avoid
5311 keeping too many graphics contexts that are no longer needed. */
5313 static void
5314 clear_face_gcs (c)
5315 struct face_cache *c;
5317 if (c && FRAME_WINDOW_P (c->f))
5319 #ifdef HAVE_WINDOW_SYSTEM
5320 int i;
5321 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
5323 struct face *face = c->faces_by_id[i];
5324 if (face && face->gc)
5326 x_free_gc (c->f, face->gc);
5327 face->gc = 0;
5330 #endif /* HAVE_WINDOW_SYSTEM */
5335 /* Free all realized faces in face cache C, including basic faces.
5336 C may be null. If faces are freed, make sure the frame's current
5337 matrix is marked invalid, so that a display caused by an expose
5338 event doesn't try to use faces we destroyed. */
5340 static void
5341 free_realized_faces (c)
5342 struct face_cache *c;
5344 if (c && c->used)
5346 int i, size;
5347 struct frame *f = c->f;
5349 /* We must block input here because we can't process X events
5350 safely while only some faces are freed, or when the frame's
5351 current matrix still references freed faces. */
5352 BLOCK_INPUT;
5354 for (i = 0; i < c->used; ++i)
5356 free_realized_face (f, c->faces_by_id[i]);
5357 c->faces_by_id[i] = NULL;
5360 c->used = 0;
5361 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5362 bzero (c->buckets, size);
5364 /* Must do a thorough redisplay the next time. Mark current
5365 matrices as invalid because they will reference faces freed
5366 above. This function is also called when a frame is
5367 destroyed. In this case, the root window of F is nil. */
5368 if (WINDOWP (f->root_window))
5370 clear_current_matrices (f);
5371 ++windows_or_buffers_changed;
5374 UNBLOCK_INPUT;
5379 /* Free all faces realized for multibyte characters on frame F that
5380 has FONTSET. */
5382 void
5383 free_realized_multibyte_face (f, fontset)
5384 struct frame *f;
5385 int fontset;
5387 struct face_cache *cache = FRAME_FACE_CACHE (f);
5388 struct face *face;
5389 int i;
5391 /* We must block input here because we can't process X events safely
5392 while only some faces are freed, or when the frame's current
5393 matrix still references freed faces. */
5394 BLOCK_INPUT;
5396 for (i = 0; i < cache->used; i++)
5398 face = cache->faces_by_id[i];
5399 if (face
5400 && face != face->ascii_face
5401 && face->fontset == fontset)
5403 uncache_face (cache, face);
5404 free_realized_face (f, face);
5408 /* Must do a thorough redisplay the next time. Mark current
5409 matrices as invalid because they will reference faces freed
5410 above. This function is also called when a frame is destroyed.
5411 In this case, the root window of F is nil. */
5412 if (WINDOWP (f->root_window))
5414 clear_current_matrices (f);
5415 ++windows_or_buffers_changed;
5418 UNBLOCK_INPUT;
5422 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
5423 This is done after attributes of a named face have been changed,
5424 because we can't tell which realized faces depend on that face. */
5426 void
5427 free_all_realized_faces (frame)
5428 Lisp_Object frame;
5430 if (NILP (frame))
5432 Lisp_Object rest;
5433 FOR_EACH_FRAME (rest, frame)
5434 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5436 else
5437 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5441 /* Free face cache C and faces in it, including their X resources. */
5443 static void
5444 free_face_cache (c)
5445 struct face_cache *c;
5447 if (c)
5449 free_realized_faces (c);
5450 xfree (c->buckets);
5451 xfree (c->faces_by_id);
5452 xfree (c);
5457 /* Cache realized face FACE in face cache C. HASH is the hash value
5458 of FACE. If FACE->fontset >= 0, add the new face to the end of the
5459 collision list of the face hash table of C. This is done because
5460 otherwise lookup_face would find FACE for every character, even if
5461 faces with the same attributes but for specific characters exist. */
5463 static void
5464 cache_face (c, face, hash)
5465 struct face_cache *c;
5466 struct face *face;
5467 unsigned hash;
5469 int i = hash % FACE_CACHE_BUCKETS_SIZE;
5471 face->hash = hash;
5473 if (face->fontset >= 0)
5475 struct face *last = c->buckets[i];
5476 if (last)
5478 while (last->next)
5479 last = last->next;
5480 last->next = face;
5481 face->prev = last;
5482 face->next = NULL;
5484 else
5486 c->buckets[i] = face;
5487 face->prev = face->next = NULL;
5490 else
5492 face->prev = NULL;
5493 face->next = c->buckets[i];
5494 if (face->next)
5495 face->next->prev = face;
5496 c->buckets[i] = face;
5499 /* Find a free slot in C->faces_by_id and use the index of the free
5500 slot as FACE->id. */
5501 for (i = 0; i < c->used; ++i)
5502 if (c->faces_by_id[i] == NULL)
5503 break;
5504 face->id = i;
5506 /* Maybe enlarge C->faces_by_id. */
5507 if (i == c->used)
5509 if (c->used == c->size)
5511 int new_size, sz;
5512 new_size = min (2 * c->size, MAX_FACE_ID);
5513 if (new_size == c->size)
5514 abort (); /* Alternatives? ++kfs */
5515 sz = new_size * sizeof *c->faces_by_id;
5516 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
5517 c->size = new_size;
5519 c->used++;
5522 #if GLYPH_DEBUG
5523 /* Check that FACE got a unique id. */
5525 int j, n;
5526 struct face *face;
5528 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
5529 for (face = c->buckets[j]; face; face = face->next)
5530 if (face->id == i)
5531 ++n;
5533 xassert (n == 1);
5535 #endif /* GLYPH_DEBUG */
5537 c->faces_by_id[i] = face;
5541 /* Remove face FACE from cache C. */
5543 static void
5544 uncache_face (c, face)
5545 struct face_cache *c;
5546 struct face *face;
5548 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
5550 if (face->prev)
5551 face->prev->next = face->next;
5552 else
5553 c->buckets[i] = face->next;
5555 if (face->next)
5556 face->next->prev = face->prev;
5558 c->faces_by_id[face->id] = NULL;
5559 if (face->id == c->used)
5560 --c->used;
5564 /* Look up a realized face with face attributes ATTR in the face cache
5565 of frame F. The face will be used to display character C. Value
5566 is the ID of the face found. If no suitable face is found, realize
5567 a new one. In that case, if C is a multibyte character, BASE_FACE
5568 is a face that has the same attributes. */
5570 INLINE int
5571 lookup_face (f, attr, c, base_face)
5572 struct frame *f;
5573 Lisp_Object *attr;
5574 int c;
5575 struct face *base_face;
5577 struct face_cache *cache = FRAME_FACE_CACHE (f);
5578 unsigned hash;
5579 int i;
5580 struct face *face;
5582 xassert (cache != NULL);
5583 check_lface_attrs (attr);
5585 /* Look up ATTR in the face cache. */
5586 hash = lface_hash (attr);
5587 i = hash % FACE_CACHE_BUCKETS_SIZE;
5589 for (face = cache->buckets[i]; face; face = face->next)
5590 if (face->hash == hash
5591 && (!FRAME_WINDOW_P (f)
5592 || FACE_SUITABLE_FOR_CHAR_P (face, c))
5593 && lface_equal_p (face->lface, attr))
5594 break;
5596 /* If not found, realize a new face. */
5597 if (face == NULL)
5598 face = realize_face (cache, attr, c, base_face, -1);
5600 #if GLYPH_DEBUG
5601 xassert (face == FACE_FROM_ID (f, face->id));
5603 /* When this function is called from face_for_char (in this case, C is
5604 a multibyte character), a fontset of a face returned by
5605 realize_face is not yet set, i.e. FACE_SUITABLE_FOR_CHAR_P (FACE,
5606 C) is not sutisfied. The fontset is set for this face by
5607 face_for_char later. */
5608 #if 0
5609 if (FRAME_WINDOW_P (f))
5610 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
5611 #endif
5612 #endif /* GLYPH_DEBUG */
5614 return face->id;
5618 /* Return the face id of the realized face for named face SYMBOL on
5619 frame F suitable for displaying character C. Value is -1 if the
5620 face couldn't be determined, which might happen if the default face
5621 isn't realized and cannot be realized. */
5624 lookup_named_face (f, symbol, c, signal_p)
5625 struct frame *f;
5626 Lisp_Object symbol;
5627 int c;
5628 int signal_p;
5630 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5631 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5632 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5634 if (default_face == NULL)
5636 if (!realize_basic_faces (f))
5637 return -1;
5638 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5641 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p))
5642 return -1;
5644 bcopy (default_face->lface, attrs, sizeof attrs);
5645 merge_face_vectors (f, symbol_attrs, attrs, 0);
5647 return lookup_face (f, attrs, c, NULL);
5651 /* Return the ID of the realized ASCII face of Lisp face with ID
5652 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
5655 ascii_face_of_lisp_face (f, lface_id)
5656 struct frame *f;
5657 int lface_id;
5659 int face_id;
5661 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
5663 Lisp_Object face_name = lface_id_to_name[lface_id];
5664 face_id = lookup_named_face (f, face_name, 0, 1);
5666 else
5667 face_id = -1;
5669 return face_id;
5673 /* Return a face for charset ASCII that is like the face with id
5674 FACE_ID on frame F, but has a font that is STEPS steps smaller.
5675 STEPS < 0 means larger. Value is the id of the face. */
5678 smaller_face (f, face_id, steps)
5679 struct frame *f;
5680 int face_id, steps;
5682 #ifdef HAVE_WINDOW_SYSTEM
5683 struct face *face;
5684 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5685 int pt, last_pt, last_height;
5686 int delta;
5687 int new_face_id;
5688 struct face *new_face;
5690 /* If not called for an X frame, just return the original face. */
5691 if (FRAME_TERMCAP_P (f))
5692 return face_id;
5694 /* Try in increments of 1/2 pt. */
5695 delta = steps < 0 ? 5 : -5;
5696 steps = abs (steps);
5698 face = FACE_FROM_ID (f, face_id);
5699 bcopy (face->lface, attrs, sizeof attrs);
5700 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
5701 new_face_id = face_id;
5702 last_height = FONT_HEIGHT (face->font);
5704 while (steps
5705 && pt + delta > 0
5706 /* Give up if we cannot find a font within 10pt. */
5707 && abs (last_pt - pt) < 100)
5709 /* Look up a face for a slightly smaller/larger font. */
5710 pt += delta;
5711 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
5712 new_face_id = lookup_face (f, attrs, 0, NULL);
5713 new_face = FACE_FROM_ID (f, new_face_id);
5715 /* If height changes, count that as one step. */
5716 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
5717 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
5719 --steps;
5720 last_height = FONT_HEIGHT (new_face->font);
5721 last_pt = pt;
5725 return new_face_id;
5727 #else /* not HAVE_WINDOW_SYSTEM */
5729 return face_id;
5731 #endif /* not HAVE_WINDOW_SYSTEM */
5735 /* Return a face for charset ASCII that is like the face with id
5736 FACE_ID on frame F, but has height HEIGHT. */
5739 face_with_height (f, face_id, height)
5740 struct frame *f;
5741 int face_id;
5742 int height;
5744 #ifdef HAVE_WINDOW_SYSTEM
5745 struct face *face;
5746 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5748 if (FRAME_TERMCAP_P (f)
5749 || height <= 0)
5750 return face_id;
5752 face = FACE_FROM_ID (f, face_id);
5753 bcopy (face->lface, attrs, sizeof attrs);
5754 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
5755 face_id = lookup_face (f, attrs, 0, NULL);
5756 #endif /* HAVE_WINDOW_SYSTEM */
5758 return face_id;
5762 /* Return the face id of the realized face for named face SYMBOL on
5763 frame F suitable for displaying character C, and use attributes of
5764 the face FACE_ID for attributes that aren't completely specified by
5765 SYMBOL. This is like lookup_named_face, except that the default
5766 attributes come from FACE_ID, not from the default face. FACE_ID
5767 is assumed to be already realized. */
5770 lookup_derived_face (f, symbol, c, face_id)
5771 struct frame *f;
5772 Lisp_Object symbol;
5773 int c;
5774 int face_id;
5776 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5777 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5778 struct face *default_face = FACE_FROM_ID (f, face_id);
5780 if (!default_face)
5781 abort ();
5783 get_lface_attributes (f, symbol, symbol_attrs, 1);
5784 bcopy (default_face->lface, attrs, sizeof attrs);
5785 merge_face_vectors (f, symbol_attrs, attrs, 0);
5786 return lookup_face (f, attrs, c, default_face);
5789 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
5790 Sface_attributes_as_vector, 1, 1, 0,
5791 doc: /* Return a vector of face attributes corresponding to PLIST. */)
5792 (plist)
5793 Lisp_Object plist;
5795 Lisp_Object lface;
5796 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
5797 Qunspecified);
5798 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
5799 1, 0);
5800 return lface;
5805 /***********************************************************************
5806 Face capability testing
5807 ***********************************************************************/
5810 /* If the distance (as returned by color_distance) between two colors is
5811 less than this, then they are considered the same, for determining
5812 whether a color is supported or not. The range of values is 0-65535. */
5814 #define TTY_SAME_COLOR_THRESHOLD 10000
5816 #ifdef HAVE_WINDOW_SYSTEM
5818 /* Return non-zero if all the face attributes in ATTRS are supported
5819 on the window-system frame F.
5821 The definition of `supported' is somewhat heuristic, but basically means
5822 that a face containing all the attributes in ATTRS, when merged with the
5823 default face for display, can be represented in a way that's
5825 \(1) different in appearance than the default face, and
5826 \(2) `close in spirit' to what the attributes specify, if not exact. */
5828 static int
5829 x_supports_face_attributes_p (f, attrs, def_face)
5830 struct frame *f;
5831 Lisp_Object *attrs;
5832 struct face *def_face;
5834 Lisp_Object *def_attrs = def_face->lface;
5836 /* Check that other specified attributes are different that the default
5837 face. */
5838 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
5839 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
5840 def_attrs[LFACE_UNDERLINE_INDEX]))
5841 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
5842 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
5843 def_attrs[LFACE_INVERSE_INDEX]))
5844 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
5845 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
5846 def_attrs[LFACE_FOREGROUND_INDEX]))
5847 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
5848 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
5849 def_attrs[LFACE_BACKGROUND_INDEX]))
5850 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5851 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
5852 def_attrs[LFACE_STIPPLE_INDEX]))
5853 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5854 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
5855 def_attrs[LFACE_OVERLINE_INDEX]))
5856 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5857 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
5858 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
5859 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5860 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
5861 def_attrs[LFACE_BOX_INDEX])))
5862 return 0;
5864 /* Check font-related attributes, as those are the most commonly
5865 "unsupported" on a window-system (because of missing fonts). */
5866 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5867 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5868 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
5869 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
5870 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5871 || !UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX]))
5873 struct face *face;
5874 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
5876 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
5878 merge_face_vectors (f, attrs, merged_attrs, 0);
5880 face = FACE_FROM_ID (f, lookup_face (f, merged_attrs, 0, 0));
5882 if (! face)
5883 error ("cannot make face");
5885 /* If the font is the same, then not supported. */
5886 if (face->font == def_face->font)
5887 return 0;
5890 /* Everything checks out, this face is supported. */
5891 return 1;
5894 #endif /* HAVE_WINDOW_SYSTEM */
5896 /* Return non-zero if all the face attributes in ATTRS are supported
5897 on the tty frame F.
5899 The definition of `supported' is somewhat heuristic, but basically means
5900 that a face containing all the attributes in ATTRS, when merged
5901 with the default face for display, can be represented in a way that's
5903 \(1) different in appearance than the default face, and
5904 \(2) `close in spirit' to what the attributes specify, if not exact.
5906 Point (2) implies that a `:weight black' attribute will be satisfied
5907 by any terminal that can display bold, and a `:foreground "yellow"' as
5908 long as the terminal can display a yellowish color, but `:slant italic'
5909 will _not_ be satisfied by the tty display code's automatic
5910 substitution of a `dim' face for italic. */
5912 static int
5913 tty_supports_face_attributes_p (f, attrs, def_face)
5914 struct frame *f;
5915 Lisp_Object *attrs;
5916 struct face *def_face;
5918 int weight;
5919 Lisp_Object val, fg, bg;
5920 XColor fg_tty_color, fg_std_color;
5921 XColor bg_tty_color, bg_std_color;
5922 unsigned test_caps = 0;
5923 Lisp_Object *def_attrs = def_face->lface;
5926 /* First check some easy-to-check stuff; ttys support none of the
5927 following attributes, so we can just return false if any are requested
5928 (even if `nominal' values are specified, we should still return false,
5929 as that will be the same value that the default face uses). We
5930 consider :slant unsupportable on ttys, even though the face code
5931 actually `fakes' them using a dim attribute if possible. This is
5932 because the faked result is too different from what the face
5933 specifies. */
5934 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5935 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5936 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5937 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5938 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5939 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5940 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5941 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5942 return 0;
5945 /* Test for terminal `capabilities' (non-color character attributes). */
5947 /* font weight (bold/dim) */
5948 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5949 if (weight >= 0)
5951 int def_weight = face_numeric_weight (def_attrs[LFACE_WEIGHT_INDEX]);
5953 if (weight > XLFD_WEIGHT_MEDIUM)
5955 if (def_weight > XLFD_WEIGHT_MEDIUM)
5956 return 0; /* same as default */
5957 test_caps = TTY_CAP_BOLD;
5959 else if (weight < XLFD_WEIGHT_MEDIUM)
5961 if (def_weight < XLFD_WEIGHT_MEDIUM)
5962 return 0; /* same as default */
5963 test_caps = TTY_CAP_DIM;
5965 else if (def_weight == XLFD_WEIGHT_MEDIUM)
5966 return 0; /* same as default */
5969 /* underlining */
5970 val = attrs[LFACE_UNDERLINE_INDEX];
5971 if (!UNSPECIFIEDP (val))
5973 if (STRINGP (val))
5974 return 0; /* ttys can't use colored underlines */
5975 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5976 return 0; /* same as default */
5977 else
5978 test_caps |= TTY_CAP_UNDERLINE;
5981 /* inverse video */
5982 val = attrs[LFACE_INVERSE_INDEX];
5983 if (!UNSPECIFIEDP (val))
5985 if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5986 return 0; /* same as default */
5987 else
5988 test_caps |= TTY_CAP_INVERSE;
5992 /* Color testing. */
5994 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5995 we use them when calling `tty_capable_p' below, even if the face
5996 specifies no colors. */
5997 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5998 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
6000 /* Check if foreground color is close enough. */
6001 fg = attrs[LFACE_FOREGROUND_INDEX];
6002 if (STRINGP (fg))
6004 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
6006 if (face_attr_equal_p (fg, def_fg))
6007 return 0; /* same as default */
6008 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
6009 return 0; /* not a valid color */
6010 else if (color_distance (&fg_tty_color, &fg_std_color)
6011 > TTY_SAME_COLOR_THRESHOLD)
6012 return 0; /* displayed color is too different */
6013 else
6014 /* Make sure the color is really different than the default. */
6016 XColor def_fg_color;
6017 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
6018 && (color_distance (&fg_tty_color, &def_fg_color)
6019 <= TTY_SAME_COLOR_THRESHOLD))
6020 return 0;
6024 /* Check if background color is close enough. */
6025 bg = attrs[LFACE_BACKGROUND_INDEX];
6026 if (STRINGP (bg))
6028 Lisp_Object def_bg = def_attrs[LFACE_FOREGROUND_INDEX];
6030 if (face_attr_equal_p (bg, def_bg))
6031 return 0; /* same as default */
6032 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
6033 return 0; /* not a valid color */
6034 else if (color_distance (&bg_tty_color, &bg_std_color)
6035 > TTY_SAME_COLOR_THRESHOLD)
6036 return 0; /* displayed color is too different */
6037 else
6038 /* Make sure the color is really different than the default. */
6040 XColor def_bg_color;
6041 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
6042 && (color_distance (&bg_tty_color, &def_bg_color)
6043 <= TTY_SAME_COLOR_THRESHOLD))
6044 return 0;
6048 /* If both foreground and background are requested, see if the
6049 distance between them is OK. We just check to see if the distance
6050 between the tty's foreground and background is close enough to the
6051 distance between the standard foreground and background. */
6052 if (STRINGP (fg) && STRINGP (bg))
6054 int delta_delta
6055 = (color_distance (&fg_std_color, &bg_std_color)
6056 - color_distance (&fg_tty_color, &bg_tty_color));
6057 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
6058 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
6059 return 0;
6063 /* See if the capabilities we selected above are supported, with the
6064 given colors. */
6065 if (test_caps != 0 &&
6066 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
6067 return 0;
6070 /* Hmmm, everything checks out, this terminal must support this face. */
6071 return 1;
6075 DEFUN ("display-supports-face-attributes-p",
6076 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
6077 1, 2, 0,
6078 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
6079 The optional argument DISPLAY can be a display name, a frame, or
6080 nil (meaning the selected frame's display)
6082 The definition of `supported' is somewhat heuristic, but basically means
6083 that a face containing all the attributes in ATTRIBUTES, when merged
6084 with the default face for display, can be represented in a way that's
6086 \(1) different in appearance than the default face, and
6087 \(2) `close in spirit' to what the attributes specify, if not exact.
6089 Point (2) implies that a `:weight black' attribute will be satisfied by
6090 any display that can display bold, and a `:foreground \"yellow\"' as long
6091 as it can display a yellowish color, but `:slant italic' will _not_ be
6092 satisfied by the tty display code's automatic substitution of a `dim'
6093 face for italic. */)
6094 (attributes, display)
6095 Lisp_Object attributes, display;
6097 int supports, i;
6098 Lisp_Object frame;
6099 struct frame *f;
6100 struct face *def_face;
6101 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6103 if (noninteractive || !initialized)
6104 /* We may not be able to access low-level face information in batch
6105 mode, or before being dumped, and this function is not going to
6106 be very useful in those cases anyway, so just give up. */
6107 return Qnil;
6109 if (NILP (display))
6110 frame = selected_frame;
6111 else if (FRAMEP (display))
6112 frame = display;
6113 else
6115 /* Find any frame on DISPLAY. */
6116 Lisp_Object fl_tail;
6118 frame = Qnil;
6119 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
6121 frame = XCAR (fl_tail);
6122 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
6123 XFRAME (frame)->param_alist)),
6124 display)))
6125 break;
6129 CHECK_LIVE_FRAME (frame);
6130 f = XFRAME (frame);
6132 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
6133 attrs[i] = Qunspecified;
6134 merge_face_ref (f, attributes, attrs, 1, 0);
6136 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6137 if (def_face == NULL)
6139 if (! realize_basic_faces (f))
6140 error ("Cannot realize default face");
6141 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6144 /* Dispatch to the appropriate handler. */
6145 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6146 supports = tty_supports_face_attributes_p (f, attrs, def_face);
6147 #ifdef HAVE_WINDOW_SYSTEM
6148 else
6149 supports = x_supports_face_attributes_p (f, attrs, def_face);
6150 #endif
6152 return supports ? Qt : Qnil;
6156 /***********************************************************************
6157 Font selection
6158 ***********************************************************************/
6160 DEFUN ("internal-set-font-selection-order",
6161 Finternal_set_font_selection_order,
6162 Sinternal_set_font_selection_order, 1, 1, 0,
6163 doc: /* Set font selection order for face font selection to ORDER.
6164 ORDER must be a list of length 4 containing the symbols `:width',
6165 `:height', `:weight', and `:slant'. Face attributes appearing
6166 first in ORDER are matched first, e.g. if `:height' appears before
6167 `:weight' in ORDER, font selection first tries to find a font with
6168 a suitable height, and then tries to match the font weight.
6169 Value is ORDER. */)
6170 (order)
6171 Lisp_Object order;
6173 Lisp_Object list;
6174 int i;
6175 int indices[DIM (font_sort_order)];
6177 CHECK_LIST (order);
6178 bzero (indices, sizeof indices);
6179 i = 0;
6181 for (list = order;
6182 CONSP (list) && i < DIM (indices);
6183 list = XCDR (list), ++i)
6185 Lisp_Object attr = XCAR (list);
6186 int xlfd;
6188 if (EQ (attr, QCwidth))
6189 xlfd = XLFD_SWIDTH;
6190 else if (EQ (attr, QCheight))
6191 xlfd = XLFD_POINT_SIZE;
6192 else if (EQ (attr, QCweight))
6193 xlfd = XLFD_WEIGHT;
6194 else if (EQ (attr, QCslant))
6195 xlfd = XLFD_SLANT;
6196 else
6197 break;
6199 if (indices[i] != 0)
6200 break;
6201 indices[i] = xlfd;
6204 if (!NILP (list) || i != DIM (indices))
6205 signal_error ("Invalid font sort order", order);
6206 for (i = 0; i < DIM (font_sort_order); ++i)
6207 if (indices[i] == 0)
6208 signal_error ("Invalid font sort order", order);
6210 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
6212 bcopy (indices, font_sort_order, sizeof font_sort_order);
6213 free_all_realized_faces (Qnil);
6216 return Qnil;
6220 DEFUN ("internal-set-alternative-font-family-alist",
6221 Finternal_set_alternative_font_family_alist,
6222 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
6223 doc: /* Define alternative font families to try in face font selection.
6224 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6225 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
6226 be found. Value is ALIST. */)
6227 (alist)
6228 Lisp_Object alist;
6230 CHECK_LIST (alist);
6231 Vface_alternative_font_family_alist = alist;
6232 free_all_realized_faces (Qnil);
6233 return alist;
6237 DEFUN ("internal-set-alternative-font-registry-alist",
6238 Finternal_set_alternative_font_registry_alist,
6239 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
6240 doc: /* Define alternative font registries to try in face font selection.
6241 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6242 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
6243 be found. Value is ALIST. */)
6244 (alist)
6245 Lisp_Object alist;
6247 CHECK_LIST (alist);
6248 Vface_alternative_font_registry_alist = alist;
6249 free_all_realized_faces (Qnil);
6250 return alist;
6254 #ifdef HAVE_WINDOW_SYSTEM
6256 /* Value is non-zero if FONT is the name of a scalable font. The
6257 X11R6 XLFD spec says that point size, pixel size, and average width
6258 are zero for scalable fonts. Intlfonts contain at least one
6259 scalable font ("*-muleindian-1") for which this isn't true, so we
6260 just test average width. */
6262 static int
6263 font_scalable_p (font)
6264 struct font_name *font;
6266 char *s = font->fields[XLFD_AVGWIDTH];
6267 return (*s == '0' && *(s + 1) == '\0')
6268 #ifdef WINDOWSNT
6269 /* Windows implementation of XLFD is slightly broken for backward
6270 compatibility with previous broken versions, so test for
6271 wildcards as well as 0. */
6272 || *s == '*'
6273 #endif
6278 /* Ignore the difference of font point size less than this value. */
6280 #define FONT_POINT_SIZE_QUANTUM 5
6282 /* Value is non-zero if FONT1 is a better match for font attributes
6283 VALUES than FONT2. VALUES is an array of face attribute values in
6284 font sort order. COMPARE_PT_P zero means don't compare point
6285 sizes. AVGWIDTH, if not zero, is a specified font average width
6286 to compare with. */
6288 static int
6289 better_font_p (values, font1, font2, compare_pt_p, avgwidth)
6290 int *values;
6291 struct font_name *font1, *font2;
6292 int compare_pt_p, avgwidth;
6294 int i;
6296 for (i = 0; i < DIM (font_sort_order); ++i)
6298 int xlfd_idx = font_sort_order[i];
6300 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
6302 int delta1, delta2;
6304 if (xlfd_idx == XLFD_POINT_SIZE)
6306 delta1 = abs (values[i] - (font1->numeric[xlfd_idx]
6307 / font1->rescale_ratio));
6308 delta2 = abs (values[i] - (font2->numeric[xlfd_idx]
6309 / font2->rescale_ratio));
6310 if (abs (delta1 - delta2) < FONT_POINT_SIZE_QUANTUM)
6311 continue;
6313 else
6315 delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
6316 delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
6319 if (delta1 > delta2)
6320 return 0;
6321 else if (delta1 < delta2)
6322 return 1;
6323 else
6325 /* The difference may be equal because, e.g., the face
6326 specifies `italic' but we have only `regular' and
6327 `oblique'. Prefer `oblique' in this case. */
6328 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
6329 && font1->numeric[xlfd_idx] > values[i]
6330 && font2->numeric[xlfd_idx] < values[i])
6331 return 1;
6336 if (avgwidth)
6338 int delta1 = abs (avgwidth - font1->numeric[XLFD_AVGWIDTH]);
6339 int delta2 = abs (avgwidth - font2->numeric[XLFD_AVGWIDTH]);
6340 if (delta1 > delta2)
6341 return 0;
6342 else if (delta1 < delta2)
6343 return 1;
6346 if (! compare_pt_p)
6348 /* We prefer a real scalable font; i.e. not what autoscaled. */
6349 int auto_scaled_1 = (font1->numeric[XLFD_POINT_SIZE] == 0
6350 && font1->numeric[XLFD_RESY] > 0);
6351 int auto_scaled_2 = (font2->numeric[XLFD_POINT_SIZE] == 0
6352 && font2->numeric[XLFD_RESY] > 0);
6354 if (auto_scaled_1 != auto_scaled_2)
6355 return auto_scaled_2;
6358 return font1->registry_priority < font2->registry_priority;
6362 /* Value is non-zero if FONT is an exact match for face attributes in
6363 SPECIFIED. SPECIFIED is an array of face attribute values in font
6364 sort order. AVGWIDTH, if non-zero, is an average width to compare
6365 with. */
6367 static int
6368 exact_face_match_p (specified, font, avgwidth)
6369 int *specified;
6370 struct font_name *font;
6371 int avgwidth;
6373 int i;
6375 for (i = 0; i < DIM (font_sort_order); ++i)
6376 if (specified[i] != font->numeric[font_sort_order[i]])
6377 break;
6379 return (i == DIM (font_sort_order)
6380 && (avgwidth <= 0
6381 || avgwidth == font->numeric[XLFD_AVGWIDTH]));
6385 /* Value is the name of a scaled font, generated from scalable font
6386 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
6387 Value is allocated from heap. */
6389 static char *
6390 build_scalable_font_name (f, font, specified_pt)
6391 struct frame *f;
6392 struct font_name *font;
6393 int specified_pt;
6395 char pixel_size[20];
6396 int pixel_value;
6397 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
6398 double pt;
6400 /* If scalable font is for a specific resolution, compute
6401 the point size we must specify from the resolution of
6402 the display and the specified resolution of the font. */
6403 if (font->numeric[XLFD_RESY] != 0)
6405 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
6406 pixel_value = font->numeric[XLFD_RESY] / (PT_PER_INCH * 10.0) * pt;
6408 else
6410 pt = specified_pt;
6411 pixel_value = resy / (PT_PER_INCH * 10.0) * pt;
6413 /* We may need a font of the different size. */
6414 pixel_value *= font->rescale_ratio;
6416 /* We should keep POINT_SIZE 0. Otherwise, X server can't open a
6417 font of the specified PIXEL_SIZE. */
6418 #if 0
6419 { /* Set point size of the font. */
6420 char point_size[20];
6421 sprintf (point_size, "%d", (int) pt);
6422 font->fields[XLFD_POINT_SIZE] = point_size;
6423 font->numeric[XLFD_POINT_SIZE] = pt;
6425 #endif
6427 /* Set pixel size. */
6428 sprintf (pixel_size, "%d", pixel_value);
6429 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
6430 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
6432 /* If font doesn't specify its resolution, use the
6433 resolution of the display. */
6434 if (font->numeric[XLFD_RESY] == 0)
6436 char buffer[20];
6437 sprintf (buffer, "%d", (int) resy);
6438 font->fields[XLFD_RESY] = buffer;
6439 font->numeric[XLFD_RESY] = resy;
6442 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
6444 char buffer[20];
6445 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
6446 sprintf (buffer, "%d", resx);
6447 font->fields[XLFD_RESX] = buffer;
6448 font->numeric[XLFD_RESX] = resx;
6451 return build_font_name (font);
6455 /* Value is non-zero if we are allowed to use scalable font FONT. We
6456 can't run a Lisp function here since this function may be called
6457 with input blocked. */
6459 static int
6460 may_use_scalable_font_p (font)
6461 const char *font;
6463 if (EQ (Vscalable_fonts_allowed, Qt))
6464 return 1;
6465 else if (CONSP (Vscalable_fonts_allowed))
6467 Lisp_Object tail, regexp;
6469 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
6471 regexp = XCAR (tail);
6472 if (STRINGP (regexp)
6473 && fast_c_string_match_ignore_case (regexp, font) >= 0)
6474 return 1;
6478 return 0;
6483 /* Return the name of the best matching font for face attributes ATTRS
6484 in the array of font_name structures FONTS which contains NFONTS
6485 elements. WIDTH_RATIO is a factor with which to multiply average
6486 widths if ATTRS specifies such a width.
6488 Value is a font name which is allocated from the heap. FONTS is
6489 freed by this function.
6491 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6492 indicate whether the resulting font should be drawn using overstrike
6493 to simulate bold-face. */
6495 static char *
6496 best_matching_font (f, attrs, fonts, nfonts, width_ratio, needs_overstrike)
6497 struct frame *f;
6498 Lisp_Object *attrs;
6499 struct font_name *fonts;
6500 int nfonts;
6501 int width_ratio;
6502 int *needs_overstrike;
6504 char *font_name;
6505 struct font_name *best;
6506 int i, pt = 0;
6507 int specified[5];
6508 int exact_p, avgwidth;
6510 if (nfonts == 0)
6511 return NULL;
6513 /* Make specified font attributes available in `specified',
6514 indexed by sort order. */
6515 for (i = 0; i < DIM (font_sort_order); ++i)
6517 int xlfd_idx = font_sort_order[i];
6519 if (xlfd_idx == XLFD_SWIDTH)
6520 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
6521 else if (xlfd_idx == XLFD_POINT_SIZE)
6522 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
6523 else if (xlfd_idx == XLFD_WEIGHT)
6524 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
6525 else if (xlfd_idx == XLFD_SLANT)
6526 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
6527 else
6528 abort ();
6531 avgwidth = (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
6533 : XFASTINT (attrs[LFACE_AVGWIDTH_INDEX]) * width_ratio);
6535 exact_p = 0;
6537 if (needs_overstrike)
6538 *needs_overstrike = 0;
6540 /* Start with the first non-scalable font in the list. */
6541 for (i = 0; i < nfonts; ++i)
6542 if (!font_scalable_p (fonts + i))
6543 break;
6545 /* Find the best match among the non-scalable fonts. */
6546 if (i < nfonts)
6548 best = fonts + i;
6550 for (i = 1; i < nfonts; ++i)
6551 if (!font_scalable_p (fonts + i)
6552 && better_font_p (specified, fonts + i, best, 1, avgwidth))
6554 best = fonts + i;
6556 exact_p = exact_face_match_p (specified, best, avgwidth);
6557 if (exact_p)
6558 break;
6561 else
6562 best = NULL;
6564 /* Unless we found an exact match among non-scalable fonts, see if
6565 we can find a better match among scalable fonts. */
6566 if (!exact_p)
6568 /* A scalable font is better if
6570 1. its weight, slant, swidth attributes are better, or.
6572 2. the best non-scalable font doesn't have the required
6573 point size, and the scalable fonts weight, slant, swidth
6574 isn't worse. */
6576 int non_scalable_has_exact_height_p;
6578 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
6579 non_scalable_has_exact_height_p = 1;
6580 else
6581 non_scalable_has_exact_height_p = 0;
6583 for (i = 0; i < nfonts; ++i)
6584 if (font_scalable_p (fonts + i))
6586 if (best == NULL
6587 || better_font_p (specified, fonts + i, best, 0, 0)
6588 || (!non_scalable_has_exact_height_p
6589 && !better_font_p (specified, best, fonts + i, 0, 0)))
6591 non_scalable_has_exact_height_p = 1;
6592 best = fonts + i;
6596 if (needs_overstrike)
6598 enum xlfd_weight want_weight = specified[XLFD_WEIGHT];
6599 enum xlfd_weight got_weight = best->numeric[XLFD_WEIGHT];
6601 if (want_weight > XLFD_WEIGHT_MEDIUM && want_weight > got_weight)
6603 /* We want a bold font, but didn't get one; try to use
6604 overstriking instead to simulate bold-face. However,
6605 don't overstrike an already-bold fontn unless the
6606 desired weight grossly exceeds the available weight. */
6607 if (got_weight > XLFD_WEIGHT_MEDIUM)
6608 *needs_overstrike = (got_weight - want_weight) > 2;
6609 else
6610 *needs_overstrike = 1;
6615 if (font_scalable_p (best))
6616 font_name = build_scalable_font_name (f, best, pt);
6617 else
6618 font_name = build_font_name (best);
6620 /* Free font_name structures. */
6621 free_font_names (fonts, nfonts);
6623 return font_name;
6627 /* Get a list of matching fonts on frame F, considering FAMILY
6628 and alternative font families from Vface_alternative_font_registry_alist.
6630 FAMILY is the font family whose alternatives are considered.
6632 REGISTRY, if a string, specifies a font registry and encoding to
6633 match. A value of nil means include fonts of any registry and
6634 encoding.
6636 Return in *FONTS a pointer to a vector of font_name structures for
6637 the fonts matched. Value is the number of fonts found. */
6639 static int
6640 try_alternative_families (f, family, registry, fonts)
6641 struct frame *f;
6642 Lisp_Object family, registry;
6643 struct font_name **fonts;
6645 Lisp_Object alter;
6646 int nfonts = 0;
6648 nfonts = font_list (f, Qnil, family, registry, fonts);
6649 if (nfonts == 0)
6651 /* Try alternative font families. */
6652 alter = Fassoc (family, Vface_alternative_font_family_alist);
6653 if (CONSP (alter))
6655 for (alter = XCDR (alter);
6656 CONSP (alter) && nfonts == 0;
6657 alter = XCDR (alter))
6659 if (STRINGP (XCAR (alter)))
6660 nfonts = font_list (f, Qnil, XCAR (alter), registry, fonts);
6664 /* Try all scalable fonts before giving up. */
6665 if (nfonts == 0 && ! EQ (Vscalable_fonts_allowed, Qt))
6667 int count = SPECPDL_INDEX ();
6668 specbind (Qscalable_fonts_allowed, Qt);
6669 nfonts = try_alternative_families (f, family, registry, fonts);
6670 unbind_to (count, Qnil);
6673 return nfonts;
6677 /* Get a list of matching fonts on frame F.
6679 FAMILY, if a string, specifies a font family derived from the fontset.
6680 It is only used if the face does not specify any family in ATTRS or
6681 if we cannot find any font of the face's family.
6683 REGISTRY, if a string, specifies a font registry and encoding to
6684 match. A value of nil means include fonts of any registry and
6685 encoding.
6687 If PREFER_FACE_FAMILY is nonzero, perfer face's family to FAMILY.
6688 Otherwise, prefer FAMILY.
6690 Return in *FONTS a pointer to a vector of font_name structures for
6691 the fonts matched. Value is the number of fonts found. */
6693 static int
6694 try_font_list (f, attrs, family, registry, fonts, prefer_face_family)
6695 struct frame *f;
6696 Lisp_Object *attrs;
6697 Lisp_Object family, registry;
6698 struct font_name **fonts;
6699 int prefer_face_family;
6701 int nfonts = 0;
6702 Lisp_Object face_family = attrs[LFACE_FAMILY_INDEX];
6703 Lisp_Object try_family;
6705 try_family = (prefer_face_family || NILP (family)) ? face_family : family;
6707 if (STRINGP (try_family))
6708 nfonts = try_alternative_families (f, try_family, registry, fonts);
6710 #ifdef MAC_OS
6711 /* When realizing the default face and a font spec does not matched
6712 exactly, Emacs looks for ones with the same registry as the
6713 default font. On the Mac, this is mac-roman, which does not work
6714 if the family is -etl-fixed, e.g. The following widens the
6715 choices and fixes that problem. */
6716 if (nfonts == 0 && STRINGP (try_family) && STRINGP (registry)
6717 && xstricmp (SDATA (registry), "mac-roman") == 0)
6718 nfonts = try_alternative_families (f, try_family, Qnil, fonts);
6719 #endif
6721 if (EQ (try_family, family))
6722 family = face_family;
6724 if (nfonts == 0 && STRINGP (family))
6725 nfonts = try_alternative_families (f, family, registry, fonts);
6727 /* Try font family of the default face or "fixed". */
6728 if (nfonts == 0)
6730 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6731 if (default_face)
6732 family = default_face->lface[LFACE_FAMILY_INDEX];
6733 else
6734 family = build_string ("fixed");
6735 nfonts = font_list (f, Qnil, family, registry, fonts);
6738 /* Try any family with the given registry. */
6739 if (nfonts == 0)
6740 nfonts = try_alternative_families (f, Qnil, registry, fonts);
6742 return nfonts;
6746 /* Return the fontset id of the base fontset name or alias name given
6747 by the fontset attribute of ATTRS. Value is -1 if the fontset
6748 attribute of ATTRS doesn't name a fontset. */
6750 static int
6751 face_fontset (attrs)
6752 Lisp_Object *attrs;
6754 Lisp_Object name;
6756 name = attrs[LFACE_FONT_INDEX];
6757 if (!STRINGP (name))
6758 return -1;
6759 return fs_query_fontset (name, 0);
6763 /* Choose a name of font to use on frame F to display character C with
6764 Lisp face attributes specified by ATTRS. The font name is
6765 determined by the font-related attributes in ATTRS and the name
6766 pattern for C in FONTSET. Value is the font name which is
6767 allocated from the heap and must be freed by the caller, or NULL if
6768 we can get no information about the font name of C. It is assured
6769 that we always get some information for a single byte
6770 character.
6772 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6773 indicate whether the resulting font should be drawn using overstrike
6774 to simulate bold-face. */
6776 static char *
6777 choose_face_font (f, attrs, fontset, c, needs_overstrike)
6778 struct frame *f;
6779 Lisp_Object *attrs;
6780 int fontset, c;
6781 int *needs_overstrike;
6783 Lisp_Object pattern;
6784 char *font_name = NULL;
6785 struct font_name *fonts;
6786 int nfonts, width_ratio;
6788 if (needs_overstrike)
6789 *needs_overstrike = 0;
6791 /* Get (foundry and) family name and registry (and encoding) name of
6792 a font for C. */
6793 pattern = fontset_font_pattern (f, fontset, c);
6794 if (NILP (pattern))
6796 xassert (!SINGLE_BYTE_CHAR_P (c));
6797 return NULL;
6800 /* If what we got is a name pattern, return it. */
6801 if (STRINGP (pattern))
6802 return xstrdup (SDATA (pattern));
6804 /* Get a list of fonts matching that pattern and choose the
6805 best match for the specified face attributes from it. */
6806 nfonts = try_font_list (f, attrs, XCAR (pattern), XCDR (pattern), &fonts,
6807 (SINGLE_BYTE_CHAR_P (c)
6808 || CHAR_CHARSET (c) == charset_latin_iso8859_1));
6809 width_ratio = (SINGLE_BYTE_CHAR_P (c)
6811 : CHARSET_WIDTH (CHAR_CHARSET (c)));
6812 font_name = best_matching_font (f, attrs, fonts, nfonts, width_ratio,
6813 needs_overstrike);
6814 return font_name;
6817 #endif /* HAVE_WINDOW_SYSTEM */
6821 /***********************************************************************
6822 Face Realization
6823 ***********************************************************************/
6825 /* Realize basic faces on frame F. Value is zero if frame parameters
6826 of F don't contain enough information needed to realize the default
6827 face. */
6829 static int
6830 realize_basic_faces (f)
6831 struct frame *f;
6833 int success_p = 0;
6834 int count = SPECPDL_INDEX ();
6836 /* Block input here so that we won't be surprised by an X expose
6837 event, for instance, without having the faces set up. */
6838 BLOCK_INPUT;
6839 specbind (Qscalable_fonts_allowed, Qt);
6841 if (realize_default_face (f))
6843 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
6844 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
6845 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
6846 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
6847 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
6848 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
6849 realize_named_face (f, Qborder, BORDER_FACE_ID);
6850 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
6851 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
6852 realize_named_face (f, Qmenu, MENU_FACE_ID);
6854 /* Reflect changes in the `menu' face in menu bars. */
6855 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
6857 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
6858 #ifdef USE_X_TOOLKIT
6859 if (FRAME_WINDOW_P (f))
6860 x_update_menu_appearance (f);
6861 #endif
6864 success_p = 1;
6867 unbind_to (count, Qnil);
6868 UNBLOCK_INPUT;
6869 return success_p;
6873 /* Realize the default face on frame F. If the face is not fully
6874 specified, make it fully-specified. Attributes of the default face
6875 that are not explicitly specified are taken from frame parameters. */
6877 static int
6878 realize_default_face (f)
6879 struct frame *f;
6881 struct face_cache *c = FRAME_FACE_CACHE (f);
6882 Lisp_Object lface;
6883 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6884 Lisp_Object frame_font;
6885 struct face *face;
6887 /* If the `default' face is not yet known, create it. */
6888 lface = lface_from_face_name (f, Qdefault, 0);
6889 if (NILP (lface))
6891 Lisp_Object frame;
6892 XSETFRAME (frame, f);
6893 lface = Finternal_make_lisp_face (Qdefault, frame);
6897 #ifdef HAVE_WINDOW_SYSTEM
6898 if (FRAME_WINDOW_P (f))
6900 /* Set frame_font to the value of the `font' frame parameter. */
6901 frame_font = Fassq (Qfont, f->param_alist);
6902 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
6903 frame_font = XCDR (frame_font);
6904 set_lface_from_font_name (f, lface, frame_font,
6905 f->default_face_done_p, 1);
6906 f->default_face_done_p = 1;
6908 #endif /* HAVE_WINDOW_SYSTEM */
6910 if (!FRAME_WINDOW_P (f))
6912 LFACE_FAMILY (lface) = build_string ("default");
6913 LFACE_SWIDTH (lface) = Qnormal;
6914 LFACE_HEIGHT (lface) = make_number (1);
6915 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
6916 LFACE_WEIGHT (lface) = Qnormal;
6917 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
6918 LFACE_SLANT (lface) = Qnormal;
6919 LFACE_AVGWIDTH (lface) = Qunspecified;
6922 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
6923 LFACE_UNDERLINE (lface) = Qnil;
6925 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
6926 LFACE_OVERLINE (lface) = Qnil;
6928 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
6929 LFACE_STRIKE_THROUGH (lface) = Qnil;
6931 if (UNSPECIFIEDP (LFACE_BOX (lface)))
6932 LFACE_BOX (lface) = Qnil;
6934 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
6935 LFACE_INVERSE (lface) = Qnil;
6937 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
6939 /* This function is called so early that colors are not yet
6940 set in the frame parameter list. */
6941 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
6943 if (CONSP (color) && STRINGP (XCDR (color)))
6944 LFACE_FOREGROUND (lface) = XCDR (color);
6945 else if (FRAME_WINDOW_P (f))
6946 return 0;
6947 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6948 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
6949 else
6950 abort ();
6953 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
6955 /* This function is called so early that colors are not yet
6956 set in the frame parameter list. */
6957 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
6958 if (CONSP (color) && STRINGP (XCDR (color)))
6959 LFACE_BACKGROUND (lface) = XCDR (color);
6960 else if (FRAME_WINDOW_P (f))
6961 return 0;
6962 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6963 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
6964 else
6965 abort ();
6968 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
6969 LFACE_STIPPLE (lface) = Qnil;
6971 /* Realize the face; it must be fully-specified now. */
6972 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
6973 check_lface (lface);
6974 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
6975 face = realize_face (c, attrs, 0, NULL, DEFAULT_FACE_ID);
6976 return 1;
6980 /* Realize basic faces other than the default face in face cache C.
6981 SYMBOL is the face name, ID is the face id the realized face must
6982 have. The default face must have been realized already. */
6984 static void
6985 realize_named_face (f, symbol, id)
6986 struct frame *f;
6987 Lisp_Object symbol;
6988 int id;
6990 struct face_cache *c = FRAME_FACE_CACHE (f);
6991 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
6992 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6993 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
6994 struct face *new_face;
6996 /* The default face must exist and be fully specified. */
6997 get_lface_attributes (f, Qdefault, attrs, 1);
6998 check_lface_attrs (attrs);
6999 xassert (lface_fully_specified_p (attrs));
7001 /* If SYMBOL isn't know as a face, create it. */
7002 if (NILP (lface))
7004 Lisp_Object frame;
7005 XSETFRAME (frame, f);
7006 lface = Finternal_make_lisp_face (symbol, frame);
7009 /* Merge SYMBOL's face with the default face. */
7010 get_lface_attributes (f, symbol, symbol_attrs, 1);
7011 merge_face_vectors (f, symbol_attrs, attrs, 0);
7013 /* Realize the face. */
7014 new_face = realize_face (c, attrs, 0, NULL, id);
7018 /* Realize the fully-specified face with attributes ATTRS in face
7019 cache CACHE for character C. If C is a multibyte character,
7020 BASE_FACE is a face that has the same attributes. Otherwise,
7021 BASE_FACE is ignored. If FORMER_FACE_ID is non-negative, it is an
7022 ID of face to remove before caching the new face. Value is a
7023 pointer to the newly created realized face. */
7025 static struct face *
7026 realize_face (cache, attrs, c, base_face, former_face_id)
7027 struct face_cache *cache;
7028 Lisp_Object *attrs;
7029 int c;
7030 struct face *base_face;
7031 int former_face_id;
7033 struct face *face;
7035 /* LFACE must be fully specified. */
7036 xassert (cache != NULL);
7037 check_lface_attrs (attrs);
7039 if (former_face_id >= 0 && cache->used > former_face_id)
7041 /* Remove the former face. */
7042 struct face *former_face = cache->faces_by_id[former_face_id];
7043 uncache_face (cache, former_face);
7044 free_realized_face (cache->f, former_face);
7047 if (FRAME_WINDOW_P (cache->f))
7048 face = realize_x_face (cache, attrs, c, base_face);
7049 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
7050 face = realize_tty_face (cache, attrs, c);
7051 else if (FRAME_INITIAL_P (cache->f))
7053 /* Create a dummy face. */
7054 face = make_realized_face (attrs);
7056 else
7057 abort ();
7059 /* Insert the new face. */
7060 cache_face (cache, face, lface_hash (attrs));
7061 #ifdef HAVE_WINDOW_SYSTEM
7062 if (FRAME_WINDOW_P (cache->f) && face->font == NULL)
7063 load_face_font (cache->f, face, c);
7064 #endif /* HAVE_WINDOW_SYSTEM */
7065 return face;
7069 /* Realize the fully-specified face with attributes ATTRS in face
7070 cache CACHE for character C. Do it for X frame CACHE->f. If C is
7071 a multibyte character, BASE_FACE is a face that has the same
7072 attributes. Otherwise, BASE_FACE is ignored. If the new face
7073 doesn't share font with the default face, a fontname is allocated
7074 from the heap and set in `font_name' of the new face, but it is not
7075 yet loaded here. Value is a pointer to the newly created realized
7076 face. */
7078 static struct face *
7079 realize_x_face (cache, attrs, c, base_face)
7080 struct face_cache *cache;
7081 Lisp_Object *attrs;
7082 int c;
7083 struct face *base_face;
7085 #ifdef HAVE_WINDOW_SYSTEM
7086 struct face *face, *default_face;
7087 struct frame *f;
7088 Lisp_Object stipple, overline, strike_through, box;
7090 xassert (FRAME_WINDOW_P (cache->f));
7091 xassert (SINGLE_BYTE_CHAR_P (c)
7092 || base_face);
7094 /* Allocate a new realized face. */
7095 face = make_realized_face (attrs);
7097 f = cache->f;
7099 /* If C is a multibyte character, we share all face attirbutes with
7100 BASE_FACE including the realized fontset. But, we must load a
7101 different font. */
7102 if (!SINGLE_BYTE_CHAR_P (c))
7104 bcopy (base_face, face, sizeof *face);
7105 face->gc = 0;
7107 /* Don't try to free the colors copied bitwise from BASE_FACE. */
7108 face->colors_copied_bitwise_p = 1;
7110 /* to force realize_face to load font */
7111 face->font = NULL;
7112 return face;
7115 /* Now we are realizing a face for ASCII (and unibyte) characters. */
7117 /* Determine the font to use. Most of the time, the font will be
7118 the same as the font of the default face, so try that first. */
7119 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7120 if (default_face
7121 && FACE_SUITABLE_FOR_CHAR_P (default_face, c)
7122 && lface_same_font_attributes_p (default_face->lface, attrs))
7124 face->font = default_face->font;
7125 face->fontset = default_face->fontset;
7126 face->font_info_id = default_face->font_info_id;
7127 face->font_name = default_face->font_name;
7128 face->ascii_face = face;
7130 /* But, as we can't share the fontset, make a new realized
7131 fontset that has the same base fontset as of the default
7132 face. */
7133 face->fontset
7134 = make_fontset_for_ascii_face (f, default_face->fontset);
7136 else
7138 /* If the face attribute ATTRS specifies a fontset, use it as
7139 the base of a new realized fontset. Otherwise, use the same
7140 base fontset as of the default face. The base determines
7141 registry and encoding of a font. It may also determine
7142 foundry and family. The other fields of font name pattern
7143 are constructed from ATTRS. */
7144 int fontset = face_fontset (attrs);
7146 if ((fontset == -1) && default_face)
7147 fontset = default_face->fontset;
7148 face->fontset = make_fontset_for_ascii_face (f, fontset);
7149 face->font = NULL; /* to force realize_face to load font */
7152 /* Load colors, and set remaining attributes. */
7154 load_face_colors (f, face, attrs);
7156 /* Set up box. */
7157 box = attrs[LFACE_BOX_INDEX];
7158 if (STRINGP (box))
7160 /* A simple box of line width 1 drawn in color given by
7161 the string. */
7162 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
7163 LFACE_BOX_INDEX);
7164 face->box = FACE_SIMPLE_BOX;
7165 face->box_line_width = 1;
7167 else if (INTEGERP (box))
7169 /* Simple box of specified line width in foreground color of the
7170 face. */
7171 xassert (XINT (box) != 0);
7172 face->box = FACE_SIMPLE_BOX;
7173 face->box_line_width = XINT (box);
7174 face->box_color = face->foreground;
7175 face->box_color_defaulted_p = 1;
7177 else if (CONSP (box))
7179 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
7180 being one of `raised' or `sunken'. */
7181 face->box = FACE_SIMPLE_BOX;
7182 face->box_color = face->foreground;
7183 face->box_color_defaulted_p = 1;
7184 face->box_line_width = 1;
7186 while (CONSP (box))
7188 Lisp_Object keyword, value;
7190 keyword = XCAR (box);
7191 box = XCDR (box);
7193 if (!CONSP (box))
7194 break;
7195 value = XCAR (box);
7196 box = XCDR (box);
7198 if (EQ (keyword, QCline_width))
7200 if (INTEGERP (value) && XINT (value) != 0)
7201 face->box_line_width = XINT (value);
7203 else if (EQ (keyword, QCcolor))
7205 if (STRINGP (value))
7207 face->box_color = load_color (f, face, value,
7208 LFACE_BOX_INDEX);
7209 face->use_box_color_for_shadows_p = 1;
7212 else if (EQ (keyword, QCstyle))
7214 if (EQ (value, Qreleased_button))
7215 face->box = FACE_RAISED_BOX;
7216 else if (EQ (value, Qpressed_button))
7217 face->box = FACE_SUNKEN_BOX;
7222 /* Text underline, overline, strike-through. */
7224 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
7226 /* Use default color (same as foreground color). */
7227 face->underline_p = 1;
7228 face->underline_defaulted_p = 1;
7229 face->underline_color = 0;
7231 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
7233 /* Use specified color. */
7234 face->underline_p = 1;
7235 face->underline_defaulted_p = 0;
7236 face->underline_color
7237 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
7238 LFACE_UNDERLINE_INDEX);
7240 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
7242 face->underline_p = 0;
7243 face->underline_defaulted_p = 0;
7244 face->underline_color = 0;
7247 overline = attrs[LFACE_OVERLINE_INDEX];
7248 if (STRINGP (overline))
7250 face->overline_color
7251 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
7252 LFACE_OVERLINE_INDEX);
7253 face->overline_p = 1;
7255 else if (EQ (overline, Qt))
7257 face->overline_color = face->foreground;
7258 face->overline_color_defaulted_p = 1;
7259 face->overline_p = 1;
7262 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
7263 if (STRINGP (strike_through))
7265 face->strike_through_color
7266 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
7267 LFACE_STRIKE_THROUGH_INDEX);
7268 face->strike_through_p = 1;
7270 else if (EQ (strike_through, Qt))
7272 face->strike_through_color = face->foreground;
7273 face->strike_through_color_defaulted_p = 1;
7274 face->strike_through_p = 1;
7277 stipple = attrs[LFACE_STIPPLE_INDEX];
7278 if (!NILP (stipple))
7279 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
7281 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
7282 return face;
7283 #endif /* HAVE_WINDOW_SYSTEM */
7287 /* Map a specified color of face FACE on frame F to a tty color index.
7288 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
7289 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
7290 default foreground/background colors. */
7292 static void
7293 map_tty_color (f, face, idx, defaulted)
7294 struct frame *f;
7295 struct face *face;
7296 enum lface_attribute_index idx;
7297 int *defaulted;
7299 Lisp_Object frame, color, def;
7300 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
7301 unsigned long default_pixel, default_other_pixel, pixel;
7303 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
7305 if (foreground_p)
7307 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7308 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7310 else
7312 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7313 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7316 XSETFRAME (frame, f);
7317 color = face->lface[idx];
7319 if (STRINGP (color)
7320 && SCHARS (color)
7321 && CONSP (Vtty_defined_color_alist)
7322 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
7323 CONSP (def)))
7325 /* Associations in tty-defined-color-alist are of the form
7326 (NAME INDEX R G B). We need the INDEX part. */
7327 pixel = XINT (XCAR (XCDR (def)));
7330 if (pixel == default_pixel && STRINGP (color))
7332 pixel = load_color (f, face, color, idx);
7334 #if defined (MSDOS) || defined (WINDOWSNT)
7335 /* If the foreground of the default face is the default color,
7336 use the foreground color defined by the frame. */
7337 #ifdef MSDOS
7338 if (FRAME_MSDOS_P (f))
7340 #endif /* MSDOS */
7341 if (pixel == default_pixel
7342 || pixel == FACE_TTY_DEFAULT_COLOR)
7344 if (foreground_p)
7345 pixel = FRAME_FOREGROUND_PIXEL (f);
7346 else
7347 pixel = FRAME_BACKGROUND_PIXEL (f);
7348 face->lface[idx] = tty_color_name (f, pixel);
7349 *defaulted = 1;
7351 else if (pixel == default_other_pixel)
7353 if (foreground_p)
7354 pixel = FRAME_BACKGROUND_PIXEL (f);
7355 else
7356 pixel = FRAME_FOREGROUND_PIXEL (f);
7357 face->lface[idx] = tty_color_name (f, pixel);
7358 *defaulted = 1;
7360 #ifdef MSDOS
7362 #endif
7363 #endif /* MSDOS or WINDOWSNT */
7366 if (foreground_p)
7367 face->foreground = pixel;
7368 else
7369 face->background = pixel;
7373 /* Realize the fully-specified face with attributes ATTRS in face
7374 cache CACHE for character C. Do it for TTY frame CACHE->f. Value is a
7375 pointer to the newly created realized face. */
7377 static struct face *
7378 realize_tty_face (cache, attrs, c)
7379 struct face_cache *cache;
7380 Lisp_Object *attrs;
7381 int c;
7383 struct face *face;
7384 int weight, slant;
7385 int face_colors_defaulted = 0;
7386 struct frame *f = cache->f;
7388 /* Frame must be a termcap frame. */
7389 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
7391 /* Allocate a new realized face. */
7392 face = make_realized_face (attrs);
7393 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
7395 /* Map face attributes to TTY appearances. We map slant to
7396 dimmed text because we want italic text to appear differently
7397 and because dimmed text is probably used infrequently. */
7398 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
7399 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
7401 if (weight > XLFD_WEIGHT_MEDIUM)
7402 face->tty_bold_p = 1;
7403 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
7404 face->tty_dim_p = 1;
7405 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
7406 face->tty_underline_p = 1;
7407 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
7408 face->tty_reverse_p = 1;
7410 /* Map color names to color indices. */
7411 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
7412 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
7414 /* Swap colors if face is inverse-video. If the colors are taken
7415 from the frame colors, they are already inverted, since the
7416 frame-creation function calls x-handle-reverse-video. */
7417 if (face->tty_reverse_p && !face_colors_defaulted)
7419 unsigned long tem = face->foreground;
7420 face->foreground = face->background;
7421 face->background = tem;
7424 if (tty_suppress_bold_inverse_default_colors_p
7425 && face->tty_bold_p
7426 && face->background == FACE_TTY_DEFAULT_FG_COLOR
7427 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
7428 face->tty_bold_p = 0;
7430 return face;
7434 DEFUN ("tty-suppress-bold-inverse-default-colors",
7435 Ftty_suppress_bold_inverse_default_colors,
7436 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
7437 doc: /* Suppress/allow boldness of faces with inverse default colors.
7438 SUPPRESS non-nil means suppress it.
7439 This affects bold faces on TTYs whose foreground is the default background
7440 color of the display and whose background is the default foreground color.
7441 For such faces, the bold face attribute is ignored if this variable
7442 is non-nil. */)
7443 (suppress)
7444 Lisp_Object suppress;
7446 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
7447 ++face_change_count;
7448 return suppress;
7453 /***********************************************************************
7454 Computing Faces
7455 ***********************************************************************/
7457 /* Return the ID of the face to use to display character CH with face
7458 property PROP on frame F in current_buffer. */
7461 compute_char_face (f, ch, prop)
7462 struct frame *f;
7463 int ch;
7464 Lisp_Object prop;
7466 int face_id;
7468 if (NILP (current_buffer->enable_multibyte_characters))
7469 ch = 0;
7471 if (NILP (prop))
7473 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7474 face_id = FACE_FOR_CHAR (f, face, ch);
7476 else
7478 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7479 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7480 bcopy (default_face->lface, attrs, sizeof attrs);
7481 merge_face_ref (f, prop, attrs, 1, 0);
7482 face_id = lookup_face (f, attrs, ch, NULL);
7485 return face_id;
7488 /* Return the face ID associated with buffer position POS for
7489 displaying ASCII characters. Return in *ENDPTR the position at
7490 which a different face is needed, as far as text properties and
7491 overlays are concerned. W is a window displaying current_buffer.
7493 REGION_BEG, REGION_END delimit the region, so it can be
7494 highlighted.
7496 LIMIT is a position not to scan beyond. That is to limit the time
7497 this function can take.
7499 If MOUSE is non-zero, use the character's mouse-face, not its face.
7501 The face returned is suitable for displaying ASCII characters. */
7504 face_at_buffer_position (w, pos, region_beg, region_end,
7505 endptr, limit, mouse)
7506 struct window *w;
7507 int pos;
7508 int region_beg, region_end;
7509 int *endptr;
7510 int limit;
7511 int mouse;
7513 struct frame *f = XFRAME (w->frame);
7514 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7515 Lisp_Object prop, position;
7516 int i, noverlays;
7517 Lisp_Object *overlay_vec;
7518 Lisp_Object frame;
7519 int endpos;
7520 Lisp_Object propname = mouse ? Qmouse_face : Qface;
7521 Lisp_Object limit1, end;
7522 struct face *default_face;
7524 /* W must display the current buffer. We could write this function
7525 to use the frame and buffer of W, but right now it doesn't. */
7526 /* xassert (XBUFFER (w->buffer) == current_buffer); */
7528 XSETFRAME (frame, f);
7529 XSETFASTINT (position, pos);
7531 endpos = ZV;
7532 if (pos < region_beg && region_beg < endpos)
7533 endpos = region_beg;
7535 /* Get the `face' or `mouse_face' text property at POS, and
7536 determine the next position at which the property changes. */
7537 prop = Fget_text_property (position, propname, w->buffer);
7538 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
7539 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
7540 if (INTEGERP (end))
7541 endpos = XINT (end);
7543 /* Look at properties from overlays. */
7545 int next_overlay;
7547 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
7548 if (next_overlay < endpos)
7549 endpos = next_overlay;
7552 *endptr = endpos;
7554 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7556 /* Optimize common cases where we can use the default face. */
7557 if (noverlays == 0
7558 && NILP (prop)
7559 && !(pos >= region_beg && pos < region_end))
7560 return DEFAULT_FACE_ID;
7562 /* Begin with attributes from the default face. */
7563 bcopy (default_face->lface, attrs, sizeof attrs);
7565 /* Merge in attributes specified via text properties. */
7566 if (!NILP (prop))
7567 merge_face_ref (f, prop, attrs, 1, 0);
7569 /* Now merge the overlay data. */
7570 noverlays = sort_overlays (overlay_vec, noverlays, w);
7571 for (i = 0; i < noverlays; i++)
7573 Lisp_Object oend;
7574 int oendpos;
7576 prop = Foverlay_get (overlay_vec[i], propname);
7577 if (!NILP (prop))
7578 merge_face_ref (f, prop, attrs, 1, 0);
7580 oend = OVERLAY_END (overlay_vec[i]);
7581 oendpos = OVERLAY_POSITION (oend);
7582 if (oendpos < endpos)
7583 endpos = oendpos;
7586 /* If in the region, merge in the region face. */
7587 if (pos >= region_beg && pos < region_end)
7589 merge_named_face (f, Qregion, attrs, 0);
7591 if (region_end < endpos)
7592 endpos = region_end;
7595 *endptr = endpos;
7597 /* Look up a realized face with the given face attributes,
7598 or realize a new one for ASCII characters. */
7599 return lookup_face (f, attrs, 0, NULL);
7603 /* Compute the face at character position POS in Lisp string STRING on
7604 window W, for ASCII characters.
7606 If STRING is an overlay string, it comes from position BUFPOS in
7607 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
7608 not an overlay string. W must display the current buffer.
7609 REGION_BEG and REGION_END give the start and end positions of the
7610 region; both are -1 if no region is visible.
7612 BASE_FACE_ID is the id of a face to merge with. For strings coming
7613 from overlays or the `display' property it is the face at BUFPOS.
7615 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
7617 Set *ENDPTR to the next position where to check for faces in
7618 STRING; -1 if the face is constant from POS to the end of the
7619 string.
7621 Value is the id of the face to use. The face returned is suitable
7622 for displaying ASCII characters. */
7625 face_at_string_position (w, string, pos, bufpos, region_beg,
7626 region_end, endptr, base_face_id, mouse_p)
7627 struct window *w;
7628 Lisp_Object string;
7629 int pos, bufpos;
7630 int region_beg, region_end;
7631 int *endptr;
7632 enum face_id base_face_id;
7633 int mouse_p;
7635 Lisp_Object prop, position, end, limit;
7636 struct frame *f = XFRAME (WINDOW_FRAME (w));
7637 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7638 struct face *base_face;
7639 int multibyte_p = STRING_MULTIBYTE (string);
7640 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
7642 /* Get the value of the face property at the current position within
7643 STRING. Value is nil if there is no face property. */
7644 XSETFASTINT (position, pos);
7645 prop = Fget_text_property (position, prop_name, string);
7647 /* Get the next position at which to check for faces. Value of end
7648 is nil if face is constant all the way to the end of the string.
7649 Otherwise it is a string position where to check faces next.
7650 Limit is the maximum position up to which to check for property
7651 changes in Fnext_single_property_change. Strings are usually
7652 short, so set the limit to the end of the string. */
7653 XSETFASTINT (limit, SCHARS (string));
7654 end = Fnext_single_property_change (position, prop_name, string, limit);
7655 if (INTEGERP (end))
7656 *endptr = XFASTINT (end);
7657 else
7658 *endptr = -1;
7660 base_face = FACE_FROM_ID (f, base_face_id);
7661 xassert (base_face);
7663 /* Optimize the default case that there is no face property and we
7664 are not in the region. */
7665 if (NILP (prop)
7666 && (base_face_id != DEFAULT_FACE_ID
7667 /* BUFPOS <= 0 means STRING is not an overlay string, so
7668 that the region doesn't have to be taken into account. */
7669 || bufpos <= 0
7670 || bufpos < region_beg
7671 || bufpos >= region_end)
7672 && (multibyte_p
7673 /* We can't realize faces for different charsets differently
7674 if we don't have fonts, so we can stop here if not working
7675 on a window-system frame. */
7676 || !FRAME_WINDOW_P (f)
7677 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
7678 return base_face->id;
7680 /* Begin with attributes from the base face. */
7681 bcopy (base_face->lface, attrs, sizeof attrs);
7683 /* Merge in attributes specified via text properties. */
7684 if (!NILP (prop))
7685 merge_face_ref (f, prop, attrs, 1, 0);
7687 /* If in the region, merge in the region face. */
7688 if (bufpos
7689 && bufpos >= region_beg
7690 && bufpos < region_end)
7691 merge_named_face (f, Qregion, attrs, 0);
7693 /* Look up a realized face with the given face attributes,
7694 or realize a new one for ASCII characters. */
7695 return lookup_face (f, attrs, 0, NULL);
7699 /* Merge a face into a realized face.
7701 F is frame where faces are (to be) realized.
7703 FACE_NAME is named face to merge.
7705 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
7707 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
7709 BASE_FACE_ID is realized face to merge into.
7711 Return new face id.
7715 merge_faces (f, face_name, face_id, base_face_id)
7716 struct frame *f;
7717 Lisp_Object face_name;
7718 int face_id, base_face_id;
7720 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7721 struct face *base_face;
7723 base_face = FACE_FROM_ID (f, base_face_id);
7724 if (!base_face)
7725 return base_face_id;
7727 if (EQ (face_name, Qt))
7729 if (face_id < 0 || face_id >= lface_id_to_name_size)
7730 return base_face_id;
7731 face_name = lface_id_to_name[face_id];
7732 face_id = lookup_derived_face (f, face_name, 0, base_face_id);
7733 if (face_id >= 0)
7734 return face_id;
7735 return base_face_id;
7738 /* Begin with attributes from the base face. */
7739 bcopy (base_face->lface, attrs, sizeof attrs);
7741 if (!NILP (face_name))
7743 if (!merge_named_face (f, face_name, attrs, 0))
7744 return base_face_id;
7746 else
7748 struct face *face;
7749 if (face_id < 0)
7750 return base_face_id;
7751 face = FACE_FROM_ID (f, face_id);
7752 if (!face)
7753 return base_face_id;
7754 merge_face_vectors (f, face->lface, attrs, 0);
7757 /* Look up a realized face with the given face attributes,
7758 or realize a new one for ASCII characters. */
7759 return lookup_face (f, attrs, 0, NULL);
7763 /***********************************************************************
7764 Tests
7765 ***********************************************************************/
7767 #if GLYPH_DEBUG
7769 /* Print the contents of the realized face FACE to stderr. */
7771 static void
7772 dump_realized_face (face)
7773 struct face *face;
7775 fprintf (stderr, "ID: %d\n", face->id);
7776 #ifdef HAVE_X_WINDOWS
7777 fprintf (stderr, "gc: %d\n", (int) face->gc);
7778 #endif
7779 fprintf (stderr, "foreground: 0x%lx (%s)\n",
7780 face->foreground,
7781 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
7782 fprintf (stderr, "background: 0x%lx (%s)\n",
7783 face->background,
7784 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
7785 fprintf (stderr, "font_name: %s (%s)\n",
7786 face->font_name,
7787 SDATA (face->lface[LFACE_FAMILY_INDEX]));
7788 #ifdef HAVE_X_WINDOWS
7789 fprintf (stderr, "font = %p\n", face->font);
7790 #endif
7791 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
7792 fprintf (stderr, "fontset: %d\n", face->fontset);
7793 fprintf (stderr, "underline: %d (%s)\n",
7794 face->underline_p,
7795 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
7796 fprintf (stderr, "hash: %d\n", face->hash);
7797 fprintf (stderr, "charset: %d\n", face->charset);
7801 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
7803 Lisp_Object n;
7805 if (NILP (n))
7807 int i;
7809 fprintf (stderr, "font selection order: ");
7810 for (i = 0; i < DIM (font_sort_order); ++i)
7811 fprintf (stderr, "%d ", font_sort_order[i]);
7812 fprintf (stderr, "\n");
7814 fprintf (stderr, "alternative fonts: ");
7815 debug_print (Vface_alternative_font_family_alist);
7816 fprintf (stderr, "\n");
7818 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
7819 Fdump_face (make_number (i));
7821 else
7823 struct face *face;
7824 CHECK_NUMBER (n);
7825 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
7826 if (face == NULL)
7827 error ("Not a valid face");
7828 dump_realized_face (face);
7831 return Qnil;
7835 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
7836 0, 0, 0, doc: /* */)
7839 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
7840 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
7841 fprintf (stderr, "number of GCs = %d\n", ngcs);
7842 return Qnil;
7845 #endif /* GLYPH_DEBUG != 0 */
7849 /***********************************************************************
7850 Initialization
7851 ***********************************************************************/
7853 void
7854 syms_of_xfaces ()
7856 Qface = intern ("face");
7857 staticpro (&Qface);
7858 Qface_no_inherit = intern ("face-no-inherit");
7859 staticpro (&Qface_no_inherit);
7860 Qbitmap_spec_p = intern ("bitmap-spec-p");
7861 staticpro (&Qbitmap_spec_p);
7862 Qframe_update_face_colors = intern ("frame-update-face-colors");
7863 staticpro (&Qframe_update_face_colors);
7865 /* Lisp face attribute keywords. */
7866 QCfamily = intern (":family");
7867 staticpro (&QCfamily);
7868 QCheight = intern (":height");
7869 staticpro (&QCheight);
7870 QCweight = intern (":weight");
7871 staticpro (&QCweight);
7872 QCslant = intern (":slant");
7873 staticpro (&QCslant);
7874 QCunderline = intern (":underline");
7875 staticpro (&QCunderline);
7876 QCinverse_video = intern (":inverse-video");
7877 staticpro (&QCinverse_video);
7878 QCreverse_video = intern (":reverse-video");
7879 staticpro (&QCreverse_video);
7880 QCforeground = intern (":foreground");
7881 staticpro (&QCforeground);
7882 QCbackground = intern (":background");
7883 staticpro (&QCbackground);
7884 QCstipple = intern (":stipple");;
7885 staticpro (&QCstipple);
7886 QCwidth = intern (":width");
7887 staticpro (&QCwidth);
7888 QCfont = intern (":font");
7889 staticpro (&QCfont);
7890 QCbold = intern (":bold");
7891 staticpro (&QCbold);
7892 QCitalic = intern (":italic");
7893 staticpro (&QCitalic);
7894 QCoverline = intern (":overline");
7895 staticpro (&QCoverline);
7896 QCstrike_through = intern (":strike-through");
7897 staticpro (&QCstrike_through);
7898 QCbox = intern (":box");
7899 staticpro (&QCbox);
7900 QCinherit = intern (":inherit");
7901 staticpro (&QCinherit);
7903 /* Symbols used for Lisp face attribute values. */
7904 QCcolor = intern (":color");
7905 staticpro (&QCcolor);
7906 QCline_width = intern (":line-width");
7907 staticpro (&QCline_width);
7908 QCstyle = intern (":style");
7909 staticpro (&QCstyle);
7910 Qreleased_button = intern ("released-button");
7911 staticpro (&Qreleased_button);
7912 Qpressed_button = intern ("pressed-button");
7913 staticpro (&Qpressed_button);
7914 Qnormal = intern ("normal");
7915 staticpro (&Qnormal);
7916 Qultra_light = intern ("ultra-light");
7917 staticpro (&Qultra_light);
7918 Qextra_light = intern ("extra-light");
7919 staticpro (&Qextra_light);
7920 Qlight = intern ("light");
7921 staticpro (&Qlight);
7922 Qsemi_light = intern ("semi-light");
7923 staticpro (&Qsemi_light);
7924 Qsemi_bold = intern ("semi-bold");
7925 staticpro (&Qsemi_bold);
7926 Qbold = intern ("bold");
7927 staticpro (&Qbold);
7928 Qextra_bold = intern ("extra-bold");
7929 staticpro (&Qextra_bold);
7930 Qultra_bold = intern ("ultra-bold");
7931 staticpro (&Qultra_bold);
7932 Qoblique = intern ("oblique");
7933 staticpro (&Qoblique);
7934 Qitalic = intern ("italic");
7935 staticpro (&Qitalic);
7936 Qreverse_oblique = intern ("reverse-oblique");
7937 staticpro (&Qreverse_oblique);
7938 Qreverse_italic = intern ("reverse-italic");
7939 staticpro (&Qreverse_italic);
7940 Qultra_condensed = intern ("ultra-condensed");
7941 staticpro (&Qultra_condensed);
7942 Qextra_condensed = intern ("extra-condensed");
7943 staticpro (&Qextra_condensed);
7944 Qcondensed = intern ("condensed");
7945 staticpro (&Qcondensed);
7946 Qsemi_condensed = intern ("semi-condensed");
7947 staticpro (&Qsemi_condensed);
7948 Qsemi_expanded = intern ("semi-expanded");
7949 staticpro (&Qsemi_expanded);
7950 Qexpanded = intern ("expanded");
7951 staticpro (&Qexpanded);
7952 Qextra_expanded = intern ("extra-expanded");
7953 staticpro (&Qextra_expanded);
7954 Qultra_expanded = intern ("ultra-expanded");
7955 staticpro (&Qultra_expanded);
7956 Qbackground_color = intern ("background-color");
7957 staticpro (&Qbackground_color);
7958 Qforeground_color = intern ("foreground-color");
7959 staticpro (&Qforeground_color);
7960 Qunspecified = intern ("unspecified");
7961 staticpro (&Qunspecified);
7963 Qface_alias = intern ("face-alias");
7964 staticpro (&Qface_alias);
7965 Qdefault = intern ("default");
7966 staticpro (&Qdefault);
7967 Qtool_bar = intern ("tool-bar");
7968 staticpro (&Qtool_bar);
7969 Qregion = intern ("region");
7970 staticpro (&Qregion);
7971 Qfringe = intern ("fringe");
7972 staticpro (&Qfringe);
7973 Qheader_line = intern ("header-line");
7974 staticpro (&Qheader_line);
7975 Qscroll_bar = intern ("scroll-bar");
7976 staticpro (&Qscroll_bar);
7977 Qmenu = intern ("menu");
7978 staticpro (&Qmenu);
7979 Qcursor = intern ("cursor");
7980 staticpro (&Qcursor);
7981 Qborder = intern ("border");
7982 staticpro (&Qborder);
7983 Qmouse = intern ("mouse");
7984 staticpro (&Qmouse);
7985 Qmode_line_inactive = intern ("mode-line-inactive");
7986 staticpro (&Qmode_line_inactive);
7987 Qtty_color_desc = intern ("tty-color-desc");
7988 staticpro (&Qtty_color_desc);
7989 Qtty_color_standard_values = intern ("tty-color-standard-values");
7990 staticpro (&Qtty_color_standard_values);
7991 Qtty_color_by_index = intern ("tty-color-by-index");
7992 staticpro (&Qtty_color_by_index);
7993 Qtty_color_alist = intern ("tty-color-alist");
7994 staticpro (&Qtty_color_alist);
7995 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
7996 staticpro (&Qscalable_fonts_allowed);
7998 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
7999 staticpro (&Vparam_value_alist);
8000 Vface_alternative_font_family_alist = Qnil;
8001 staticpro (&Vface_alternative_font_family_alist);
8002 Vface_alternative_font_registry_alist = Qnil;
8003 staticpro (&Vface_alternative_font_registry_alist);
8005 defsubr (&Sinternal_make_lisp_face);
8006 defsubr (&Sinternal_lisp_face_p);
8007 defsubr (&Sinternal_set_lisp_face_attribute);
8008 #ifdef HAVE_WINDOW_SYSTEM
8009 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
8010 #endif
8011 defsubr (&Scolor_gray_p);
8012 defsubr (&Scolor_supported_p);
8013 defsubr (&Sface_attribute_relative_p);
8014 defsubr (&Smerge_face_attribute);
8015 defsubr (&Sinternal_get_lisp_face_attribute);
8016 defsubr (&Sinternal_lisp_face_attribute_values);
8017 defsubr (&Sinternal_lisp_face_equal_p);
8018 defsubr (&Sinternal_lisp_face_empty_p);
8019 defsubr (&Sinternal_copy_lisp_face);
8020 defsubr (&Sinternal_merge_in_global_face);
8021 defsubr (&Sface_font);
8022 defsubr (&Sframe_face_alist);
8023 defsubr (&Sdisplay_supports_face_attributes_p);
8024 defsubr (&Scolor_distance);
8025 defsubr (&Sinternal_set_font_selection_order);
8026 defsubr (&Sinternal_set_alternative_font_family_alist);
8027 defsubr (&Sinternal_set_alternative_font_registry_alist);
8028 defsubr (&Sface_attributes_as_vector);
8029 #if GLYPH_DEBUG
8030 defsubr (&Sdump_face);
8031 defsubr (&Sshow_face_resources);
8032 #endif /* GLYPH_DEBUG */
8033 defsubr (&Sclear_face_cache);
8034 defsubr (&Stty_suppress_bold_inverse_default_colors);
8036 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
8037 defsubr (&Sdump_colors);
8038 #endif
8040 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
8041 doc: /* *Limit for font matching.
8042 If an integer > 0, font matching functions won't load more than
8043 that number of fonts when searching for a matching font. */);
8044 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
8046 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
8047 doc: /* List of global face definitions (for internal use only.) */);
8048 Vface_new_frame_defaults = Qnil;
8050 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
8051 doc: /* *Default stipple pattern used on monochrome displays.
8052 This stipple pattern is used on monochrome displays
8053 instead of shades of gray for a face background color.
8054 See `set-face-stipple' for possible values for this variable. */);
8055 Vface_default_stipple = build_string ("gray3");
8057 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
8058 doc: /* An alist of defined terminal colors and their RGB values. */);
8059 Vtty_defined_color_alist = Qnil;
8061 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
8062 doc: /* Allowed scalable fonts.
8063 A value of nil means don't allow any scalable fonts.
8064 A value of t means allow any scalable font.
8065 Otherwise, value must be a list of regular expressions. A font may be
8066 scaled if its name matches a regular expression in the list.
8067 Note that if value is nil, a scalable font might still be used, if no
8068 other font of the appropriate family and registry is available. */);
8069 Vscalable_fonts_allowed = Qnil;
8071 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
8072 doc: /* List of ignored fonts.
8073 Each element is a regular expression that matches names of fonts to
8074 ignore. */);
8075 Vface_ignored_fonts = Qnil;
8077 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
8078 doc: /* Alist of fonts vs the rescaling factors.
8079 Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
8080 FONT-NAME-PATTERN is a regular expression matching a font name, and
8081 RESCALE-RATIO is a floating point number to specify how much larger
8082 \(or smaller) font we should use. For instance, if a face requests
8083 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
8084 Vface_font_rescale_alist = Qnil;
8086 #ifdef HAVE_WINDOW_SYSTEM
8087 defsubr (&Sbitmap_spec_p);
8088 defsubr (&Sx_list_fonts);
8089 defsubr (&Sinternal_face_x_get_resource);
8090 defsubr (&Sx_family_fonts);
8091 defsubr (&Sx_font_family_list);
8092 #endif /* HAVE_WINDOW_SYSTEM */
8095 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
8096 (do not change this comment) */