(define-widget): Add `doc-string' declaration.
[emacs.git] / src / xfaces.c
blobf13fdf7490906348b31dc964abeae3dad751402d
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
24 /* Faces.
26 When using Emacs with X, the display style of characters can be
27 changed by defining `faces'. Each face can specify the following
28 display attributes:
30 1. Font family name.
32 2. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 3. Font height in 1/10pt.
37 4. Font weight, e.g. `bold'.
39 5. Font slant, e.g. `italic'.
41 6. Foreground color.
43 7. Background color.
45 8. Whether or not characters should be underlined, and in what color.
47 9. Whether or not characters should be displayed in inverse video.
49 10. A background stipple, a bitmap.
51 11. Whether or not characters should be overlined, and in what color.
53 12. Whether or not characters should be strike-through, and in what
54 color.
56 13. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 14. Font or fontset pattern, or nil. This is a special attribute.
60 When this attribute is specified, the face uses a font opened by
61 that pattern as is. In addition, all the other font-related
62 attributes (1st thru 5th) are generated from the opened font name.
63 On the other hand, if one of the other font-related attributes are
64 specified, this attribute is set to nil. In that case, the face
65 doesn't inherit this attribute from the `default' face, and uses a
66 font determined by the other attributes (those may be inherited
67 from the `default' face).
69 15. A face name or list of face names from which to inherit attributes.
71 16. A specified average font width, which is invisible from Lisp,
72 and is used to ensure that a font specified on the command line,
73 for example, can be matched exactly.
75 Faces are frame-local by nature because Emacs allows to define the
76 same named face (face names are symbols) differently for different
77 frames. Each frame has an alist of face definitions for all named
78 faces. The value of a named face in such an alist is a Lisp vector
79 with the symbol `face' in slot 0, and a slot for each of the face
80 attributes mentioned above.
82 There is also a global face alist `Vface_new_frame_defaults'. Face
83 definitions from this list are used to initialize faces of newly
84 created frames.
86 A face doesn't have to specify all attributes. Those not specified
87 have a value of `unspecified'. Faces specifying all attributes but
88 the 14th are called `fully-specified'.
91 Face merging.
93 The display style of a given character in the text is determined by
94 combining several faces. This process is called `face merging'.
95 Any aspect of the display style that isn't specified by overlays or
96 text properties is taken from the `default' face. Since it is made
97 sure that the default face is always fully-specified, face merging
98 always results in a fully-specified face.
101 Face realization.
103 After all face attributes for a character have been determined by
104 merging faces of that character, that face is `realized'. The
105 realization process maps face attributes to what is physically
106 available on the system where Emacs runs. The result is a
107 `realized face' in form of a struct face which is stored in the
108 face cache of the frame on which it was realized.
110 Face realization is done in the context of the character to display
111 because different fonts may be used for different characters. In
112 other words, for characters that have different font
113 specifications, different realized faces are needed to display
114 them.
116 Font specification is done by fontsets. See the comment in
117 fontset.c for the details. In the current implementation, all ASCII
118 characters share the same font in a fontset.
120 Faces are at first realized for ASCII characters, and, at that
121 time, assigned a specific realized fontset. Hereafter, we call
122 such a face as `ASCII face'. When a face for a multibyte character
123 is realized, it inherits (thus shares) a fontset of an ASCII face
124 that has the same attributes other than font-related ones.
126 Thus, all realized face have a realized fontset.
129 Unibyte text.
131 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
132 font as ASCII characters. That is because it is expected that
133 unibyte text users specify a font that is suitable both for ASCII
134 and raw 8-bit characters.
137 Font selection.
139 Font selection tries to find the best available matching font for a
140 given (character, face) combination.
142 If the face specifies a fontset name, that fontset determines a
143 pattern for fonts of the given character. If the face specifies a
144 font name or the other font-related attributes, a fontset is
145 realized from the default fontset. In that case, that
146 specification determines a pattern for ASCII characters and the
147 default fontset determines a pattern for multibyte characters.
149 Available fonts on the system on which Emacs runs are then matched
150 against the font pattern. The result of font selection is the best
151 match for the given face attributes in this font list.
153 Font selection can be influenced by the user.
155 1. The user can specify the relative importance he gives the face
156 attributes width, height, weight, and slant by setting
157 face-font-selection-order (faces.el) to a list of face attribute
158 names. The default is '(:width :height :weight :slant), and means
159 that font selection first tries to find a good match for the font
160 width specified by a face, then---within fonts with that
161 width---tries to find a best match for the specified font height,
162 etc.
164 2. Setting face-font-family-alternatives allows the user to
165 specify alternative font families to try if a family specified by a
166 face doesn't exist.
168 3. Setting face-font-registry-alternatives allows the user to
169 specify all alternative font registries to try for a face
170 specifying a registry.
172 4. Setting face-ignored-fonts allows the user to ignore specific
173 fonts.
176 Character composition.
178 Usually, the realization process is already finished when Emacs
179 actually reflects the desired glyph matrix on the screen. However,
180 on displaying a composition (sequence of characters to be composed
181 on the screen), a suitable font for the components of the
182 composition is selected and realized while drawing them on the
183 screen, i.e. the realization process is delayed but in principle
184 the same.
187 Initialization of basic faces.
189 The faces `default', `modeline' are considered `basic faces'.
190 When redisplay happens the first time for a newly created frame,
191 basic faces are realized for CHARSET_ASCII. Frame parameters are
192 used to fill in unspecified attributes of the default face. */
194 #include <config.h>
195 #include <stdio.h>
196 #include <sys/types.h>
197 #include <sys/stat.h>
199 #include "lisp.h"
200 #include "charset.h"
201 #include "keyboard.h"
202 #include "frame.h"
204 #ifdef HAVE_WINDOW_SYSTEM
205 #include "fontset.h"
206 #endif /* HAVE_WINDOW_SYSTEM */
208 #ifdef HAVE_X_WINDOWS
209 #include "xterm.h"
210 #ifdef USE_MOTIF
211 #include <Xm/Xm.h>
212 #include <Xm/XmStrDefs.h>
213 #endif /* USE_MOTIF */
214 #endif /* HAVE_X_WINDOWS */
216 #ifdef MSDOS
217 #include "dosfns.h"
218 #endif
220 #ifdef WINDOWSNT
221 #include "w32term.h"
222 #include "fontset.h"
223 /* Redefine X specifics to W32 equivalents to avoid cluttering the
224 code with #ifdef blocks. */
225 #undef FRAME_X_DISPLAY_INFO
226 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
227 #define x_display_info w32_display_info
228 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
229 #define check_x check_w32
230 #define x_list_fonts w32_list_fonts
231 #define GCGraphicsExposures 0
232 #endif /* WINDOWSNT */
234 #ifdef MAC_OS
235 #include "macterm.h"
236 #define x_display_info mac_display_info
237 #define check_x check_mac
238 #endif /* MAC_OS */
240 #include "buffer.h"
241 #include "dispextern.h"
242 #include "blockinput.h"
243 #include "window.h"
244 #include "intervals.h"
246 #ifdef HAVE_X_WINDOWS
248 /* Compensate for a bug in Xos.h on some systems, on which it requires
249 time.h. On some such systems, Xos.h tries to redefine struct
250 timeval and struct timezone if USG is #defined while it is
251 #included. */
253 #ifdef XOS_NEEDS_TIME_H
254 #include <time.h>
255 #undef USG
256 #include <X11/Xos.h>
257 #define USG
258 #define __TIMEVAL__
259 #else /* not XOS_NEEDS_TIME_H */
260 #include <X11/Xos.h>
261 #endif /* not XOS_NEEDS_TIME_H */
263 #endif /* HAVE_X_WINDOWS */
265 #include <ctype.h>
267 #define abs(X) ((X) < 0 ? -(X) : (X))
269 /* Number of pt per inch (from the TeXbook). */
271 #define PT_PER_INCH 72.27
273 /* Non-zero if face attribute ATTR is unspecified. */
275 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
277 /* Value is the number of elements of VECTOR. */
279 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
281 /* Make a copy of string S on the stack using alloca. Value is a pointer
282 to the copy. */
284 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
286 /* Make a copy of the contents of Lisp string S on the stack using
287 alloca. Value is a pointer to the copy. */
289 #define LSTRDUPA(S) STRDUPA (SDATA ((S)))
291 /* Size of hash table of realized faces in face caches (should be a
292 prime number). */
294 #define FACE_CACHE_BUCKETS_SIZE 1001
296 /* Keyword symbols used for face attribute names. */
298 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
299 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
300 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
301 Lisp_Object QCreverse_video;
302 Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
304 /* Symbols used for attribute values. */
306 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
307 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
308 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
309 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
310 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
311 Lisp_Object Qultra_expanded;
312 Lisp_Object Qreleased_button, Qpressed_button;
313 Lisp_Object QCstyle, QCcolor, QCline_width;
314 Lisp_Object Qunspecified;
316 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
318 /* The name of the function to call when the background of the frame
319 has changed, frame_set_background_mode. */
321 Lisp_Object Qframe_set_background_mode;
323 /* Names of basic faces. */
325 Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
326 Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
327 Lisp_Object Qmode_line_inactive, Qvertical_border;
328 extern Lisp_Object Qmode_line;
330 /* The symbol `face-alias'. A symbols having that property is an
331 alias for another face. Value of the property is the name of
332 the aliased face. */
334 Lisp_Object Qface_alias;
336 extern Lisp_Object Qcircular_list;
338 /* Default stipple pattern used on monochrome displays. This stipple
339 pattern is used on monochrome displays instead of shades of gray
340 for a face background color. See `set-face-stipple' for possible
341 values for this variable. */
343 Lisp_Object Vface_default_stipple;
345 /* Alist of alternative font families. Each element is of the form
346 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
347 try FAMILY1, then FAMILY2, ... */
349 Lisp_Object Vface_alternative_font_family_alist;
351 /* Alist of alternative font registries. Each element is of the form
352 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
353 loaded, try REGISTRY1, then REGISTRY2, ... */
355 Lisp_Object Vface_alternative_font_registry_alist;
357 /* Allowed scalable fonts. A value of nil means don't allow any
358 scalable fonts. A value of t means allow the use of any scalable
359 font. Otherwise, value must be a list of regular expressions. A
360 font may be scaled if its name matches a regular expression in the
361 list. */
363 Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
365 /* List of regular expressions that matches names of fonts to ignore. */
367 Lisp_Object Vface_ignored_fonts;
369 /* Alist of font name patterns vs the rescaling factor. */
371 Lisp_Object Vface_font_rescale_alist;
373 /* Maximum number of fonts to consider in font_list. If not an
374 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
376 Lisp_Object Vfont_list_limit;
377 #define DEFAULT_FONT_LIST_LIMIT 100
379 /* The symbols `foreground-color' and `background-color' which can be
380 used as part of a `face' property. This is for compatibility with
381 Emacs 20.2. */
383 Lisp_Object Qforeground_color, Qbackground_color;
385 /* The symbols `face' and `mouse-face' used as text properties. */
387 Lisp_Object Qface;
388 extern Lisp_Object Qmouse_face;
390 /* Property for basic faces which other faces cannot inherit. */
392 Lisp_Object Qface_no_inherit;
394 /* Error symbol for wrong_type_argument in load_pixmap. */
396 Lisp_Object Qbitmap_spec_p;
398 /* Alist of global face definitions. Each element is of the form
399 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
400 is a Lisp vector of face attributes. These faces are used
401 to initialize faces for new frames. */
403 Lisp_Object Vface_new_frame_defaults;
405 /* The next ID to assign to Lisp faces. */
407 static int next_lface_id;
409 /* A vector mapping Lisp face Id's to face names. */
411 static Lisp_Object *lface_id_to_name;
412 static int lface_id_to_name_size;
414 /* TTY color-related functions (defined in tty-colors.el). */
416 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
418 /* The name of the function used to compute colors on TTYs. */
420 Lisp_Object Qtty_color_alist;
422 /* An alist of defined terminal colors and their RGB values. */
424 Lisp_Object Vtty_defined_color_alist;
426 /* Counter for calls to clear_face_cache. If this counter reaches
427 CLEAR_FONT_TABLE_COUNT, and a frame has more than
428 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
430 static int clear_font_table_count;
431 #define CLEAR_FONT_TABLE_COUNT 100
432 #define CLEAR_FONT_TABLE_NFONTS 10
434 /* Non-zero means face attributes have been changed since the last
435 redisplay. Used in redisplay_internal. */
437 int face_change_count;
439 /* Non-zero means don't display bold text if a face's foreground
440 and background colors are the inverse of the default colors of the
441 display. This is a kluge to suppress `bold black' foreground text
442 which is hard to read on an LCD monitor. */
444 int tty_suppress_bold_inverse_default_colors_p;
446 /* A list of the form `((x . y))' used to avoid consing in
447 Finternal_set_lisp_face_attribute. */
449 static Lisp_Object Vparam_value_alist;
451 /* The total number of colors currently allocated. */
453 #if GLYPH_DEBUG
454 static int ncolors_allocated;
455 static int npixmaps_allocated;
456 static int ngcs;
457 #endif
459 /* Non-zero means the definition of the `menu' face for new frames has
460 been changed. */
462 int menu_face_changed_default;
465 /* Function prototypes. */
467 struct font_name;
468 struct table_entry;
469 struct named_merge_point;
471 static void map_tty_color P_ ((struct frame *, struct face *,
472 enum lface_attribute_index, int *));
473 static Lisp_Object resolve_face_name P_ ((Lisp_Object, int));
474 static int may_use_scalable_font_p P_ ((const char *));
475 static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
476 static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
477 int, int));
478 static int x_face_list_fonts P_ ((struct frame *, char *,
479 struct font_name **, int, int));
480 static int font_scalable_p P_ ((struct font_name *));
481 static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
482 static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
483 static unsigned char *xstrlwr P_ ((unsigned char *));
484 static void signal_error P_ ((char *, Lisp_Object));
485 static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
486 static void load_face_font P_ ((struct frame *, struct face *, int));
487 static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
488 static void free_face_colors P_ ((struct frame *, struct face *));
489 static int face_color_gray_p P_ ((struct frame *, char *));
490 static char *build_font_name P_ ((struct font_name *));
491 static void free_font_names P_ ((struct font_name *, int));
492 static int sorted_font_list P_ ((struct frame *, char *,
493 int (*cmpfn) P_ ((const void *, const void *)),
494 struct font_name **));
495 static int font_list_1 P_ ((struct frame *, Lisp_Object, Lisp_Object,
496 Lisp_Object, struct font_name **));
497 static int font_list P_ ((struct frame *, Lisp_Object, Lisp_Object,
498 Lisp_Object, struct font_name **));
499 static int try_font_list P_ ((struct frame *, Lisp_Object *,
500 Lisp_Object, Lisp_Object, struct font_name **,
501 int));
502 static int try_alternative_families P_ ((struct frame *f, Lisp_Object,
503 Lisp_Object, struct font_name **));
504 static int cmp_font_names P_ ((const void *, const void *));
505 static struct face *realize_face P_ ((struct face_cache *, Lisp_Object *, int,
506 struct face *, int));
507 static struct face *realize_x_face P_ ((struct face_cache *,
508 Lisp_Object *, int, struct face *));
509 static struct face *realize_tty_face P_ ((struct face_cache *,
510 Lisp_Object *, int));
511 static int realize_basic_faces P_ ((struct frame *));
512 static int realize_default_face P_ ((struct frame *));
513 static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
514 static int lface_fully_specified_p P_ ((Lisp_Object *));
515 static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
516 static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
517 static unsigned lface_hash P_ ((Lisp_Object *));
518 static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
519 static struct face_cache *make_face_cache P_ ((struct frame *));
520 static void free_realized_face P_ ((struct frame *, struct face *));
521 static void clear_face_gcs P_ ((struct face_cache *));
522 static void free_face_cache P_ ((struct face_cache *));
523 static int face_numeric_weight P_ ((Lisp_Object));
524 static int face_numeric_slant P_ ((Lisp_Object));
525 static int face_numeric_swidth P_ ((Lisp_Object));
526 static int face_fontset P_ ((Lisp_Object *));
527 static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int, int, int*));
528 static void merge_face_vectors P_ ((struct frame *, Lisp_Object *, Lisp_Object*,
529 struct named_merge_point *));
530 static int merge_face_ref P_ ((struct frame *, Lisp_Object, Lisp_Object *,
531 int, struct named_merge_point *));
532 static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object,
533 Lisp_Object, int, int));
534 static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
535 static struct face *make_realized_face P_ ((Lisp_Object *));
536 static void free_realized_faces P_ ((struct face_cache *));
537 static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
538 struct font_name *, int, int, int *));
539 static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
540 static void uncache_face P_ ((struct face_cache *, struct face *));
541 static int xlfd_numeric_slant P_ ((struct font_name *));
542 static int xlfd_numeric_weight P_ ((struct font_name *));
543 static int xlfd_numeric_swidth P_ ((struct font_name *));
544 static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
545 static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
546 static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
547 static int xlfd_fixed_p P_ ((struct font_name *));
548 static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
549 int, int));
550 static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
551 struct font_name *, int,
552 Lisp_Object));
553 static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
554 struct font_name *, int));
556 #ifdef HAVE_WINDOW_SYSTEM
558 static int split_font_name P_ ((struct frame *, struct font_name *, int));
559 static int xlfd_point_size P_ ((struct frame *, struct font_name *));
560 static void sort_fonts P_ ((struct frame *, struct font_name *, int,
561 int (*cmpfn) P_ ((const void *, const void *))));
562 static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
563 static void x_free_gc P_ ((struct frame *, GC));
564 static void clear_font_table P_ ((struct x_display_info *));
566 #ifdef WINDOWSNT
567 extern Lisp_Object w32_list_fonts P_ ((struct frame *, Lisp_Object, int, int));
568 #endif /* WINDOWSNT */
570 #ifdef USE_X_TOOLKIT
571 static void x_update_menu_appearance P_ ((struct frame *));
573 extern void free_frame_menubar P_ ((struct frame *));
574 #endif /* USE_X_TOOLKIT */
576 #endif /* HAVE_WINDOW_SYSTEM */
579 /***********************************************************************
580 Utilities
581 ***********************************************************************/
583 #ifdef HAVE_X_WINDOWS
585 #ifdef DEBUG_X_COLORS
587 /* The following is a poor mans infrastructure for debugging X color
588 allocation problems on displays with PseudoColor-8. Some X servers
589 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
590 color reference counts completely so that they don't signal an
591 error when a color is freed whose reference count is already 0.
592 Other X servers do. To help me debug this, the following code
593 implements a simple reference counting schema of its own, for a
594 single display/screen. --gerd. */
596 /* Reference counts for pixel colors. */
598 int color_count[256];
600 /* Register color PIXEL as allocated. */
602 void
603 register_color (pixel)
604 unsigned long pixel;
606 xassert (pixel < 256);
607 ++color_count[pixel];
611 /* Register color PIXEL as deallocated. */
613 void
614 unregister_color (pixel)
615 unsigned long pixel;
617 xassert (pixel < 256);
618 if (color_count[pixel] > 0)
619 --color_count[pixel];
620 else
621 abort ();
625 /* Register N colors from PIXELS as deallocated. */
627 void
628 unregister_colors (pixels, n)
629 unsigned long *pixels;
630 int n;
632 int i;
633 for (i = 0; i < n; ++i)
634 unregister_color (pixels[i]);
638 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
639 doc: /* Dump currently allocated colors to stderr. */)
642 int i, n;
644 fputc ('\n', stderr);
646 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
647 if (color_count[i])
649 fprintf (stderr, "%3d: %5d", i, color_count[i]);
650 ++n;
651 if (n % 5 == 0)
652 fputc ('\n', stderr);
653 else
654 fputc ('\t', stderr);
657 if (n % 5 != 0)
658 fputc ('\n', stderr);
659 return Qnil;
662 #endif /* DEBUG_X_COLORS */
665 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
666 color values. Interrupt input must be blocked when this function
667 is called. */
669 void
670 x_free_colors (f, pixels, npixels)
671 struct frame *f;
672 unsigned long *pixels;
673 int npixels;
675 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
677 /* If display has an immutable color map, freeing colors is not
678 necessary and some servers don't allow it. So don't do it. */
679 if (class != StaticColor && class != StaticGray && class != TrueColor)
681 #ifdef DEBUG_X_COLORS
682 unregister_colors (pixels, npixels);
683 #endif
684 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
685 pixels, npixels, 0);
690 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
691 color values. Interrupt input must be blocked when this function
692 is called. */
694 void
695 x_free_dpy_colors (dpy, screen, cmap, pixels, npixels)
696 Display *dpy;
697 Screen *screen;
698 Colormap cmap;
699 unsigned long *pixels;
700 int npixels;
702 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
703 int class = dpyinfo->visual->class;
705 /* If display has an immutable color map, freeing colors is not
706 necessary and some servers don't allow it. So don't do it. */
707 if (class != StaticColor && class != StaticGray && class != TrueColor)
709 #ifdef DEBUG_X_COLORS
710 unregister_colors (pixels, npixels);
711 #endif
712 XFreeColors (dpy, cmap, pixels, npixels, 0);
717 /* Create and return a GC for use on frame F. GC values and mask
718 are given by XGCV and MASK. */
720 static INLINE GC
721 x_create_gc (f, mask, xgcv)
722 struct frame *f;
723 unsigned long mask;
724 XGCValues *xgcv;
726 GC gc;
727 BLOCK_INPUT;
728 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
729 UNBLOCK_INPUT;
730 IF_DEBUG (++ngcs);
731 return gc;
735 /* Free GC which was used on frame F. */
737 static INLINE void
738 x_free_gc (f, gc)
739 struct frame *f;
740 GC gc;
742 BLOCK_INPUT;
743 IF_DEBUG (xassert (--ngcs >= 0));
744 XFreeGC (FRAME_X_DISPLAY (f), gc);
745 UNBLOCK_INPUT;
748 #endif /* HAVE_X_WINDOWS */
750 #ifdef WINDOWSNT
751 /* W32 emulation of GCs */
753 static INLINE GC
754 x_create_gc (f, mask, xgcv)
755 struct frame *f;
756 unsigned long mask;
757 XGCValues *xgcv;
759 GC gc;
760 BLOCK_INPUT;
761 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
762 UNBLOCK_INPUT;
763 IF_DEBUG (++ngcs);
764 return gc;
768 /* Free GC which was used on frame F. */
770 static INLINE void
771 x_free_gc (f, gc)
772 struct frame *f;
773 GC gc;
775 BLOCK_INPUT;
776 IF_DEBUG (xassert (--ngcs >= 0));
777 xfree (gc);
778 UNBLOCK_INPUT;
781 #endif /* WINDOWSNT */
783 #ifdef MAC_OS
784 /* Mac OS emulation of GCs */
786 static INLINE GC
787 x_create_gc (f, mask, xgcv)
788 struct frame *f;
789 unsigned long mask;
790 XGCValues *xgcv;
792 GC gc;
793 BLOCK_INPUT;
794 gc = XCreateGC (FRAME_MAC_DISPLAY (f), FRAME_MAC_WINDOW (f), mask, xgcv);
795 UNBLOCK_INPUT;
796 IF_DEBUG (++ngcs);
797 return gc;
800 static INLINE void
801 x_free_gc (f, gc)
802 struct frame *f;
803 GC gc;
805 BLOCK_INPUT;
806 IF_DEBUG (xassert (--ngcs >= 0));
807 XFreeGC (FRAME_MAC_DISPLAY (f), gc);
808 UNBLOCK_INPUT;
811 #endif /* MAC_OS */
813 /* Like stricmp. Used to compare parts of font names which are in
814 ISO8859-1. */
817 xstricmp (s1, s2)
818 const unsigned char *s1, *s2;
820 while (*s1 && *s2)
822 unsigned char c1 = tolower (*s1);
823 unsigned char c2 = tolower (*s2);
824 if (c1 != c2)
825 return c1 < c2 ? -1 : 1;
826 ++s1, ++s2;
829 if (*s1 == 0)
830 return *s2 == 0 ? 0 : -1;
831 return 1;
835 /* Like strlwr, which might not always be available. */
837 static unsigned char *
838 xstrlwr (s)
839 unsigned char *s;
841 unsigned char *p = s;
843 for (p = s; *p; ++p)
844 /* On Mac OS X 10.3, tolower also converts non-ASCII characters
845 for some locales. */
846 if (isascii (*p))
847 *p = tolower (*p);
849 return s;
853 /* Signal `error' with message S, and additional argument ARG. */
855 static void
856 signal_error (s, arg)
857 char *s;
858 Lisp_Object arg;
860 Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
864 /* If FRAME is nil, return a pointer to the selected frame.
865 Otherwise, check that FRAME is a live frame, and return a pointer
866 to it. NPARAM is the parameter number of FRAME, for
867 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
868 Lisp function definitions. */
870 static INLINE struct frame *
871 frame_or_selected_frame (frame, nparam)
872 Lisp_Object frame;
873 int nparam;
875 if (NILP (frame))
876 frame = selected_frame;
878 CHECK_LIVE_FRAME (frame);
879 return XFRAME (frame);
883 /***********************************************************************
884 Frames and faces
885 ***********************************************************************/
887 /* Initialize face cache and basic faces for frame F. */
889 void
890 init_frame_faces (f)
891 struct frame *f;
893 /* Make a face cache, if F doesn't have one. */
894 if (FRAME_FACE_CACHE (f) == NULL)
895 FRAME_FACE_CACHE (f) = make_face_cache (f);
897 #ifdef HAVE_WINDOW_SYSTEM
898 /* Make the image cache. */
899 if (FRAME_WINDOW_P (f))
901 if (FRAME_X_IMAGE_CACHE (f) == NULL)
902 FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
903 ++FRAME_X_IMAGE_CACHE (f)->refcount;
905 #endif /* HAVE_WINDOW_SYSTEM */
907 /* Realize basic faces. Must have enough information in frame
908 parameters to realize basic faces at this point. */
909 #ifdef HAVE_X_WINDOWS
910 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
911 #endif
912 #ifdef WINDOWSNT
913 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
914 #endif
915 #ifdef MAC_OS
916 if (!FRAME_MAC_P (f) || FRAME_MAC_WINDOW (f))
917 #endif
918 if (!realize_basic_faces (f))
919 abort ();
923 /* Free face cache of frame F. Called from Fdelete_frame. */
925 void
926 free_frame_faces (f)
927 struct frame *f;
929 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
931 if (face_cache)
933 free_face_cache (face_cache);
934 FRAME_FACE_CACHE (f) = NULL;
937 #ifdef HAVE_WINDOW_SYSTEM
938 if (FRAME_WINDOW_P (f))
940 struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
941 if (image_cache)
943 --image_cache->refcount;
944 if (image_cache->refcount == 0)
945 free_image_cache (f);
948 #endif /* HAVE_WINDOW_SYSTEM */
952 /* Clear face caches, and recompute basic faces for frame F. Call
953 this after changing frame parameters on which those faces depend,
954 or when realized faces have been freed due to changing attributes
955 of named faces. */
957 void
958 recompute_basic_faces (f)
959 struct frame *f;
961 if (FRAME_FACE_CACHE (f))
963 clear_face_cache (0);
964 if (!realize_basic_faces (f))
965 abort ();
970 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
971 try to free unused fonts, too. */
973 void
974 clear_face_cache (clear_fonts_p)
975 int clear_fonts_p;
977 #ifdef HAVE_WINDOW_SYSTEM
978 Lisp_Object tail, frame;
979 struct frame *f;
981 if (clear_fonts_p
982 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
984 struct x_display_info *dpyinfo;
986 /* Fonts are common for frames on one display, i.e. on
987 one X screen. */
988 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
989 if (dpyinfo->n_fonts > CLEAR_FONT_TABLE_NFONTS)
990 clear_font_table (dpyinfo);
992 /* From time to time see if we can unload some fonts. This also
993 frees all realized faces on all frames. Fonts needed by
994 faces will be loaded again when faces are realized again. */
995 clear_font_table_count = 0;
997 FOR_EACH_FRAME (tail, frame)
999 struct frame *f = XFRAME (frame);
1000 if (FRAME_WINDOW_P (f)
1001 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
1002 free_all_realized_faces (frame);
1005 else
1007 /* Clear GCs of realized faces. */
1008 FOR_EACH_FRAME (tail, frame)
1010 f = XFRAME (frame);
1011 if (FRAME_WINDOW_P (f))
1013 clear_face_gcs (FRAME_FACE_CACHE (f));
1014 clear_image_cache (f, 0);
1018 #endif /* HAVE_WINDOW_SYSTEM */
1022 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
1023 doc: /* Clear face caches on all frames.
1024 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
1025 (thoroughly)
1026 Lisp_Object thoroughly;
1028 clear_face_cache (!NILP (thoroughly));
1029 ++face_change_count;
1030 ++windows_or_buffers_changed;
1031 return Qnil;
1036 #ifdef HAVE_WINDOW_SYSTEM
1039 /* Remove fonts from the font table of DPYINFO except for the default
1040 ASCII fonts of frames on that display. Called from clear_face_cache
1041 from time to time. */
1043 static void
1044 clear_font_table (dpyinfo)
1045 struct x_display_info *dpyinfo;
1047 int i;
1049 /* Free those fonts that are not used by frames on DPYINFO. */
1050 for (i = 0; i < dpyinfo->n_fonts; ++i)
1052 struct font_info *font_info = dpyinfo->font_table + i;
1053 Lisp_Object tail, frame;
1055 /* Check if slot is already free. */
1056 if (font_info->name == NULL)
1057 continue;
1059 /* Don't free a default font of some frame. */
1060 FOR_EACH_FRAME (tail, frame)
1062 struct frame *f = XFRAME (frame);
1063 if (FRAME_WINDOW_P (f)
1064 && font_info->font == FRAME_FONT (f))
1065 break;
1068 if (!NILP (tail))
1069 continue;
1071 /* Free names. */
1072 if (font_info->full_name != font_info->name)
1073 xfree (font_info->full_name);
1074 xfree (font_info->name);
1076 /* Free the font. */
1077 BLOCK_INPUT;
1078 #ifdef HAVE_X_WINDOWS
1079 XFreeFont (dpyinfo->display, font_info->font);
1080 #endif
1081 #ifdef WINDOWSNT
1082 w32_unload_font (dpyinfo, font_info->font);
1083 #endif
1084 #ifdef MAC_OS
1085 mac_unload_font (dpyinfo, font_info->font);
1086 #endif
1087 UNBLOCK_INPUT;
1089 /* Mark font table slot free. */
1090 font_info->font = NULL;
1091 font_info->name = font_info->full_name = NULL;
1095 #endif /* HAVE_WINDOW_SYSTEM */
1099 /***********************************************************************
1100 X Pixmaps
1101 ***********************************************************************/
1103 #ifdef HAVE_WINDOW_SYSTEM
1105 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
1106 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
1107 A bitmap specification is either a string, a file name, or a list
1108 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
1109 HEIGHT is its height, and DATA is a string containing the bits of
1110 the pixmap. Bits are stored row by row, each row occupies
1111 \(WIDTH + 7)/8 bytes. */)
1112 (object)
1113 Lisp_Object object;
1115 int pixmap_p = 0;
1117 if (STRINGP (object))
1118 /* If OBJECT is a string, it's a file name. */
1119 pixmap_p = 1;
1120 else if (CONSP (object))
1122 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
1123 HEIGHT must be integers > 0, and DATA must be string large
1124 enough to hold a bitmap of the specified size. */
1125 Lisp_Object width, height, data;
1127 height = width = data = Qnil;
1129 if (CONSP (object))
1131 width = XCAR (object);
1132 object = XCDR (object);
1133 if (CONSP (object))
1135 height = XCAR (object);
1136 object = XCDR (object);
1137 if (CONSP (object))
1138 data = XCAR (object);
1142 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1144 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1145 / BITS_PER_CHAR);
1146 if (SBYTES (data) >= bytes_per_row * XINT (height))
1147 pixmap_p = 1;
1151 return pixmap_p ? Qt : Qnil;
1155 /* Load a bitmap according to NAME (which is either a file name or a
1156 pixmap spec) for use on frame F. Value is the bitmap_id (see
1157 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1158 bitmap cannot be loaded, display a message saying so, and return
1159 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1160 if these pointers are not null. */
1162 static int
1163 load_pixmap (f, name, w_ptr, h_ptr)
1164 FRAME_PTR f;
1165 Lisp_Object name;
1166 unsigned int *w_ptr, *h_ptr;
1168 int bitmap_id;
1169 Lisp_Object tem;
1171 if (NILP (name))
1172 return 0;
1174 tem = Fbitmap_spec_p (name);
1175 if (NILP (tem))
1176 wrong_type_argument (Qbitmap_spec_p, name);
1178 BLOCK_INPUT;
1179 if (CONSP (name))
1181 /* Decode a bitmap spec into a bitmap. */
1183 int h, w;
1184 Lisp_Object bits;
1186 w = XINT (Fcar (name));
1187 h = XINT (Fcar (Fcdr (name)));
1188 bits = Fcar (Fcdr (Fcdr (name)));
1190 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1191 w, h);
1193 else
1195 /* It must be a string -- a file name. */
1196 bitmap_id = x_create_bitmap_from_file (f, name);
1198 UNBLOCK_INPUT;
1200 if (bitmap_id < 0)
1202 add_to_log ("Invalid or undefined bitmap %s", name, Qnil);
1203 bitmap_id = 0;
1205 if (w_ptr)
1206 *w_ptr = 0;
1207 if (h_ptr)
1208 *h_ptr = 0;
1210 else
1212 #if GLYPH_DEBUG
1213 ++npixmaps_allocated;
1214 #endif
1215 if (w_ptr)
1216 *w_ptr = x_bitmap_width (f, bitmap_id);
1218 if (h_ptr)
1219 *h_ptr = x_bitmap_height (f, bitmap_id);
1222 return bitmap_id;
1225 #endif /* HAVE_WINDOW_SYSTEM */
1229 /***********************************************************************
1230 Fonts
1231 ***********************************************************************/
1233 #ifdef HAVE_WINDOW_SYSTEM
1235 /* Load font of face FACE which is used on frame F to display
1236 character C. The name of the font to load is determined by lface
1237 and fontset of FACE. */
1239 static void
1240 load_face_font (f, face, c)
1241 struct frame *f;
1242 struct face *face;
1243 int c;
1245 struct font_info *font_info = NULL;
1246 char *font_name;
1247 int needs_overstrike;
1249 face->font_info_id = -1;
1250 face->font = NULL;
1252 font_name = choose_face_font (f, face->lface, face->fontset, c,
1253 &needs_overstrike);
1254 if (!font_name)
1255 return;
1257 BLOCK_INPUT;
1258 font_info = FS_LOAD_FACE_FONT (f, c, font_name, face);
1259 UNBLOCK_INPUT;
1261 if (font_info)
1263 face->font_info_id = font_info->font_idx;
1264 face->font = font_info->font;
1265 face->font_name = font_info->full_name;
1266 face->overstrike = needs_overstrike;
1267 if (face->gc)
1269 x_free_gc (f, face->gc);
1270 face->gc = 0;
1273 else
1274 add_to_log ("Unable to load font %s",
1275 build_string (font_name), Qnil);
1276 xfree (font_name);
1279 #endif /* HAVE_WINDOW_SYSTEM */
1283 /***********************************************************************
1284 X Colors
1285 ***********************************************************************/
1287 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1288 RGB_LIST should contain (at least) 3 lisp integers.
1289 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1291 static int
1292 parse_rgb_list (rgb_list, color)
1293 Lisp_Object rgb_list;
1294 XColor *color;
1296 #define PARSE_RGB_LIST_FIELD(field) \
1297 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1299 color->field = XINT (XCAR (rgb_list)); \
1300 rgb_list = XCDR (rgb_list); \
1302 else \
1303 return 0;
1305 PARSE_RGB_LIST_FIELD (red);
1306 PARSE_RGB_LIST_FIELD (green);
1307 PARSE_RGB_LIST_FIELD (blue);
1309 return 1;
1313 /* Lookup on frame F the color described by the lisp string COLOR.
1314 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1315 non-zero, then the `standard' definition of the same color is
1316 returned in it. */
1318 static int
1319 tty_lookup_color (f, color, tty_color, std_color)
1320 struct frame *f;
1321 Lisp_Object color;
1322 XColor *tty_color, *std_color;
1324 Lisp_Object frame, color_desc;
1326 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1327 return 0;
1329 XSETFRAME (frame, f);
1331 color_desc = call2 (Qtty_color_desc, color, frame);
1332 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1334 Lisp_Object rgb;
1336 if (! INTEGERP (XCAR (XCDR (color_desc))))
1337 return 0;
1339 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1341 rgb = XCDR (XCDR (color_desc));
1342 if (! parse_rgb_list (rgb, tty_color))
1343 return 0;
1345 /* Should we fill in STD_COLOR too? */
1346 if (std_color)
1348 /* Default STD_COLOR to the same as TTY_COLOR. */
1349 *std_color = *tty_color;
1351 /* Do a quick check to see if the returned descriptor is
1352 actually _exactly_ equal to COLOR, otherwise we have to
1353 lookup STD_COLOR separately. If it's impossible to lookup
1354 a standard color, we just give up and use TTY_COLOR. */
1355 if ((!STRINGP (XCAR (color_desc))
1356 || NILP (Fstring_equal (color, XCAR (color_desc))))
1357 && !NILP (Ffboundp (Qtty_color_standard_values)))
1359 /* Look up STD_COLOR separately. */
1360 rgb = call1 (Qtty_color_standard_values, color);
1361 if (! parse_rgb_list (rgb, std_color))
1362 return 0;
1366 return 1;
1368 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1369 /* We were called early during startup, and the colors are not
1370 yet set up in tty-defined-color-alist. Don't return a failure
1371 indication, since this produces the annoying "Unable to
1372 load color" messages in the *Messages* buffer. */
1373 return 1;
1374 else
1375 /* tty-color-desc seems to have returned a bad value. */
1376 return 0;
1379 /* A version of defined_color for non-X frames. */
1382 tty_defined_color (f, color_name, color_def, alloc)
1383 struct frame *f;
1384 char *color_name;
1385 XColor *color_def;
1386 int alloc;
1388 int status = 1;
1390 /* Defaults. */
1391 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1392 color_def->red = 0;
1393 color_def->blue = 0;
1394 color_def->green = 0;
1396 if (*color_name)
1397 status = tty_lookup_color (f, build_string (color_name), color_def, 0);
1399 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1401 if (strcmp (color_name, "unspecified-fg") == 0)
1402 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1403 else if (strcmp (color_name, "unspecified-bg") == 0)
1404 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1407 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1408 status = 1;
1410 return status;
1414 /* Decide if color named COLOR_NAME is valid for the display
1415 associated with the frame F; if so, return the rgb values in
1416 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1418 This does the right thing for any type of frame. */
1421 defined_color (f, color_name, color_def, alloc)
1422 struct frame *f;
1423 char *color_name;
1424 XColor *color_def;
1425 int alloc;
1427 if (!FRAME_WINDOW_P (f))
1428 return tty_defined_color (f, color_name, color_def, alloc);
1429 #ifdef HAVE_X_WINDOWS
1430 else if (FRAME_X_P (f))
1431 return x_defined_color (f, color_name, color_def, alloc);
1432 #endif
1433 #ifdef WINDOWSNT
1434 else if (FRAME_W32_P (f))
1435 return w32_defined_color (f, color_name, color_def, alloc);
1436 #endif
1437 #ifdef MAC_OS
1438 else if (FRAME_MAC_P (f))
1439 return mac_defined_color (f, color_name, color_def, alloc);
1440 #endif
1441 else
1442 abort ();
1446 /* Given the index IDX of a tty color on frame F, return its name, a
1447 Lisp string. */
1449 Lisp_Object
1450 tty_color_name (f, idx)
1451 struct frame *f;
1452 int idx;
1454 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1456 Lisp_Object frame;
1457 Lisp_Object coldesc;
1459 XSETFRAME (frame, f);
1460 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1462 if (!NILP (coldesc))
1463 return XCAR (coldesc);
1465 #ifdef MSDOS
1466 /* We can have an MSDOG frame under -nw for a short window of
1467 opportunity before internal_terminal_init is called. DTRT. */
1468 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1469 return msdos_stdcolor_name (idx);
1470 #endif
1472 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1473 return build_string (unspecified_fg);
1474 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1475 return build_string (unspecified_bg);
1477 #ifdef WINDOWSNT
1478 return vga_stdcolor_name (idx);
1479 #endif
1481 return Qunspecified;
1485 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1486 black) on frame F.
1488 The criterion implemented here is not a terribly sophisticated one. */
1490 static int
1491 face_color_gray_p (f, color_name)
1492 struct frame *f;
1493 char *color_name;
1495 XColor color;
1496 int gray_p;
1498 if (defined_color (f, color_name, &color, 0))
1499 gray_p = (/* Any color sufficiently close to black counts as grey. */
1500 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1502 ((abs (color.red - color.green)
1503 < max (color.red, color.green) / 20)
1504 && (abs (color.green - color.blue)
1505 < max (color.green, color.blue) / 20)
1506 && (abs (color.blue - color.red)
1507 < max (color.blue, color.red) / 20)));
1508 else
1509 gray_p = 0;
1511 return gray_p;
1515 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1516 BACKGROUND_P non-zero means the color will be used as background
1517 color. */
1519 static int
1520 face_color_supported_p (f, color_name, background_p)
1521 struct frame *f;
1522 char *color_name;
1523 int background_p;
1525 Lisp_Object frame;
1526 XColor not_used;
1528 XSETFRAME (frame, f);
1529 return
1530 #ifdef HAVE_WINDOW_SYSTEM
1531 FRAME_WINDOW_P (f)
1532 ? (!NILP (Fxw_display_color_p (frame))
1533 || xstricmp (color_name, "black") == 0
1534 || xstricmp (color_name, "white") == 0
1535 || (background_p
1536 && face_color_gray_p (f, color_name))
1537 || (!NILP (Fx_display_grayscale_p (frame))
1538 && face_color_gray_p (f, color_name)))
1540 #endif
1541 tty_defined_color (f, color_name, &not_used, 0);
1545 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1546 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1547 FRAME specifies the frame and thus the display for interpreting COLOR.
1548 If FRAME is nil or omitted, use the selected frame. */)
1549 (color, frame)
1550 Lisp_Object color, frame;
1552 struct frame *f;
1554 CHECK_STRING (color);
1555 if (NILP (frame))
1556 frame = selected_frame;
1557 else
1558 CHECK_FRAME (frame);
1559 f = XFRAME (frame);
1560 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
1564 DEFUN ("color-supported-p", Fcolor_supported_p,
1565 Scolor_supported_p, 1, 3, 0,
1566 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1567 BACKGROUND-P non-nil means COLOR is used as a background.
1568 Otherwise, this function tells whether it can be used as a foreground.
1569 If FRAME is nil or omitted, use the selected frame.
1570 COLOR must be a valid color name. */)
1571 (color, frame, background_p)
1572 Lisp_Object frame, color, background_p;
1574 struct frame *f;
1576 CHECK_STRING (color);
1577 if (NILP (frame))
1578 frame = selected_frame;
1579 else
1580 CHECK_FRAME (frame);
1581 f = XFRAME (frame);
1582 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
1583 return Qt;
1584 return Qnil;
1588 /* Load color with name NAME for use by face FACE on frame F.
1589 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1590 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1591 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1592 pixel color. If color cannot be loaded, display a message, and
1593 return the foreground, background or underline color of F, but
1594 record that fact in flags of the face so that we don't try to free
1595 these colors. */
1597 unsigned long
1598 load_color (f, face, name, target_index)
1599 struct frame *f;
1600 struct face *face;
1601 Lisp_Object name;
1602 enum lface_attribute_index target_index;
1604 XColor color;
1606 xassert (STRINGP (name));
1607 xassert (target_index == LFACE_FOREGROUND_INDEX
1608 || target_index == LFACE_BACKGROUND_INDEX
1609 || target_index == LFACE_UNDERLINE_INDEX
1610 || target_index == LFACE_OVERLINE_INDEX
1611 || target_index == LFACE_STRIKE_THROUGH_INDEX
1612 || target_index == LFACE_BOX_INDEX);
1614 /* if the color map is full, defined_color will return a best match
1615 to the values in an existing cell. */
1616 if (!defined_color (f, SDATA (name), &color, 1))
1618 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1620 switch (target_index)
1622 case LFACE_FOREGROUND_INDEX:
1623 face->foreground_defaulted_p = 1;
1624 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1625 break;
1627 case LFACE_BACKGROUND_INDEX:
1628 face->background_defaulted_p = 1;
1629 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1630 break;
1632 case LFACE_UNDERLINE_INDEX:
1633 face->underline_defaulted_p = 1;
1634 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1635 break;
1637 case LFACE_OVERLINE_INDEX:
1638 face->overline_color_defaulted_p = 1;
1639 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1640 break;
1642 case LFACE_STRIKE_THROUGH_INDEX:
1643 face->strike_through_color_defaulted_p = 1;
1644 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1645 break;
1647 case LFACE_BOX_INDEX:
1648 face->box_color_defaulted_p = 1;
1649 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1650 break;
1652 default:
1653 abort ();
1656 #if GLYPH_DEBUG
1657 else
1658 ++ncolors_allocated;
1659 #endif
1661 return color.pixel;
1665 #ifdef HAVE_WINDOW_SYSTEM
1667 /* Load colors for face FACE which is used on frame F. Colors are
1668 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1669 of ATTRS. If the background color specified is not supported on F,
1670 try to emulate gray colors with a stipple from Vface_default_stipple. */
1672 static void
1673 load_face_colors (f, face, attrs)
1674 struct frame *f;
1675 struct face *face;
1676 Lisp_Object *attrs;
1678 Lisp_Object fg, bg;
1680 bg = attrs[LFACE_BACKGROUND_INDEX];
1681 fg = attrs[LFACE_FOREGROUND_INDEX];
1683 /* Swap colors if face is inverse-video. */
1684 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1686 Lisp_Object tmp;
1687 tmp = fg;
1688 fg = bg;
1689 bg = tmp;
1692 /* Check for support for foreground, not for background because
1693 face_color_supported_p is smart enough to know that grays are
1694 "supported" as background because we are supposed to use stipple
1695 for them. */
1696 if (!face_color_supported_p (f, SDATA (bg), 0)
1697 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1699 x_destroy_bitmap (f, face->stipple);
1700 face->stipple = load_pixmap (f, Vface_default_stipple,
1701 &face->pixmap_w, &face->pixmap_h);
1704 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1705 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1709 /* Free color PIXEL on frame F. */
1711 void
1712 unload_color (f, pixel)
1713 struct frame *f;
1714 unsigned long pixel;
1716 #ifdef HAVE_X_WINDOWS
1717 if (pixel != -1)
1719 BLOCK_INPUT;
1720 x_free_colors (f, &pixel, 1);
1721 UNBLOCK_INPUT;
1723 #endif
1727 /* Free colors allocated for FACE. */
1729 static void
1730 free_face_colors (f, face)
1731 struct frame *f;
1732 struct face *face;
1734 #ifdef HAVE_X_WINDOWS
1735 if (face->colors_copied_bitwise_p)
1736 return;
1738 BLOCK_INPUT;
1740 if (!face->foreground_defaulted_p)
1742 x_free_colors (f, &face->foreground, 1);
1743 IF_DEBUG (--ncolors_allocated);
1746 if (!face->background_defaulted_p)
1748 x_free_colors (f, &face->background, 1);
1749 IF_DEBUG (--ncolors_allocated);
1752 if (face->underline_p
1753 && !face->underline_defaulted_p)
1755 x_free_colors (f, &face->underline_color, 1);
1756 IF_DEBUG (--ncolors_allocated);
1759 if (face->overline_p
1760 && !face->overline_color_defaulted_p)
1762 x_free_colors (f, &face->overline_color, 1);
1763 IF_DEBUG (--ncolors_allocated);
1766 if (face->strike_through_p
1767 && !face->strike_through_color_defaulted_p)
1769 x_free_colors (f, &face->strike_through_color, 1);
1770 IF_DEBUG (--ncolors_allocated);
1773 if (face->box != FACE_NO_BOX
1774 && !face->box_color_defaulted_p)
1776 x_free_colors (f, &face->box_color, 1);
1777 IF_DEBUG (--ncolors_allocated);
1780 UNBLOCK_INPUT;
1781 #endif /* HAVE_X_WINDOWS */
1784 #endif /* HAVE_WINDOW_SYSTEM */
1788 /***********************************************************************
1789 XLFD Font Names
1790 ***********************************************************************/
1792 /* An enumerator for each field of an XLFD font name. */
1794 enum xlfd_field
1796 XLFD_FOUNDRY,
1797 XLFD_FAMILY,
1798 XLFD_WEIGHT,
1799 XLFD_SLANT,
1800 XLFD_SWIDTH,
1801 XLFD_ADSTYLE,
1802 XLFD_PIXEL_SIZE,
1803 XLFD_POINT_SIZE,
1804 XLFD_RESX,
1805 XLFD_RESY,
1806 XLFD_SPACING,
1807 XLFD_AVGWIDTH,
1808 XLFD_REGISTRY,
1809 XLFD_ENCODING,
1810 XLFD_LAST
1813 /* An enumerator for each possible slant value of a font. Taken from
1814 the XLFD specification. */
1816 enum xlfd_slant
1818 XLFD_SLANT_UNKNOWN,
1819 XLFD_SLANT_ROMAN,
1820 XLFD_SLANT_ITALIC,
1821 XLFD_SLANT_OBLIQUE,
1822 XLFD_SLANT_REVERSE_ITALIC,
1823 XLFD_SLANT_REVERSE_OBLIQUE,
1824 XLFD_SLANT_OTHER
1827 /* Relative font weight according to XLFD documentation. */
1829 enum xlfd_weight
1831 XLFD_WEIGHT_UNKNOWN,
1832 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1833 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1834 XLFD_WEIGHT_LIGHT, /* 30 */
1835 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1836 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1837 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1838 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1839 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1840 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1843 /* Relative proportionate width. */
1845 enum xlfd_swidth
1847 XLFD_SWIDTH_UNKNOWN,
1848 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1849 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1850 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1851 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1852 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1853 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1854 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1855 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1856 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1859 /* Structure used for tables mapping XLFD weight, slant, and width
1860 names to numeric and symbolic values. */
1862 struct table_entry
1864 char *name;
1865 int numeric;
1866 Lisp_Object *symbol;
1869 /* Table of XLFD slant names and their numeric and symbolic
1870 representations. This table must be sorted by slant names in
1871 ascending order. */
1873 static struct table_entry slant_table[] =
1875 {"i", XLFD_SLANT_ITALIC, &Qitalic},
1876 {"o", XLFD_SLANT_OBLIQUE, &Qoblique},
1877 {"ot", XLFD_SLANT_OTHER, &Qitalic},
1878 {"r", XLFD_SLANT_ROMAN, &Qnormal},
1879 {"ri", XLFD_SLANT_REVERSE_ITALIC, &Qreverse_italic},
1880 {"ro", XLFD_SLANT_REVERSE_OBLIQUE, &Qreverse_oblique}
1883 /* Table of XLFD weight names. This table must be sorted by weight
1884 names in ascending order. */
1886 static struct table_entry weight_table[] =
1888 {"black", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold},
1889 {"bold", XLFD_WEIGHT_BOLD, &Qbold},
1890 {"book", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1891 {"demi", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1892 {"demibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1893 {"extralight", XLFD_WEIGHT_EXTRA_LIGHT, &Qextra_light},
1894 {"extrabold", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1895 {"heavy", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1896 {"light", XLFD_WEIGHT_LIGHT, &Qlight},
1897 {"medium", XLFD_WEIGHT_MEDIUM, &Qnormal},
1898 {"normal", XLFD_WEIGHT_MEDIUM, &Qnormal},
1899 {"regular", XLFD_WEIGHT_MEDIUM, &Qnormal},
1900 {"semibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1901 {"semilight", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1902 {"ultralight", XLFD_WEIGHT_ULTRA_LIGHT, &Qultra_light},
1903 {"ultrabold", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold}
1906 /* Table of XLFD width names. This table must be sorted by width
1907 names in ascending order. */
1909 static struct table_entry swidth_table[] =
1911 {"compressed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1912 {"condensed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1913 {"demiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1914 {"expanded", XLFD_SWIDTH_EXPANDED, &Qexpanded},
1915 {"extracondensed", XLFD_SWIDTH_EXTRA_CONDENSED, &Qextra_condensed},
1916 {"extraexpanded", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded},
1917 {"medium", XLFD_SWIDTH_MEDIUM, &Qnormal},
1918 {"narrow", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1919 {"normal", XLFD_SWIDTH_MEDIUM, &Qnormal},
1920 {"regular", XLFD_SWIDTH_MEDIUM, &Qnormal},
1921 {"semicondensed", XLFD_SWIDTH_SEMI_CONDENSED, &Qsemi_condensed},
1922 {"semiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1923 {"ultracondensed", XLFD_SWIDTH_ULTRA_CONDENSED, &Qultra_condensed},
1924 {"ultraexpanded", XLFD_SWIDTH_ULTRA_EXPANDED, &Qultra_expanded},
1925 {"wide", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded}
1928 /* Structure used to hold the result of splitting font names in XLFD
1929 format into their fields. */
1931 struct font_name
1933 /* The original name which is modified destructively by
1934 split_font_name. The pointer is kept here to be able to free it
1935 if it was allocated from the heap. */
1936 char *name;
1938 /* Font name fields. Each vector element points into `name' above.
1939 Fields are NUL-terminated. */
1940 char *fields[XLFD_LAST];
1942 /* Numeric values for those fields that interest us. See
1943 split_font_name for which these are. */
1944 int numeric[XLFD_LAST];
1946 /* If the original name matches one of Vface_font_rescale_alist,
1947 the value is the corresponding rescale ratio. Otherwise, the
1948 value is 1.0. */
1949 double rescale_ratio;
1951 /* Lower value mean higher priority. */
1952 int registry_priority;
1955 /* The frame in effect when sorting font names. Set temporarily in
1956 sort_fonts so that it is available in font comparison functions. */
1958 static struct frame *font_frame;
1960 /* Order by which font selection chooses fonts. The default values
1961 mean `first, find a best match for the font width, then for the
1962 font height, then for weight, then for slant.' This variable can be
1963 set via set-face-font-sort-order. */
1965 #ifdef MAC_OS
1966 static int font_sort_order[4] = {
1967 XLFD_SWIDTH, XLFD_POINT_SIZE, XLFD_WEIGHT, XLFD_SLANT
1969 #else
1970 static int font_sort_order[4];
1971 #endif
1973 /* Look up FONT.fields[FIELD_INDEX] in TABLE which has DIM entries.
1974 TABLE must be sorted by TABLE[i]->name in ascending order. Value
1975 is a pointer to the matching table entry or null if no table entry
1976 matches. */
1978 static struct table_entry *
1979 xlfd_lookup_field_contents (table, dim, font, field_index)
1980 struct table_entry *table;
1981 int dim;
1982 struct font_name *font;
1983 int field_index;
1985 /* Function split_font_name converts fields to lower-case, so there
1986 is no need to use xstrlwr or xstricmp here. */
1987 char *s = font->fields[field_index];
1988 int low, mid, high, cmp;
1990 low = 0;
1991 high = dim - 1;
1993 while (low <= high)
1995 mid = (low + high) / 2;
1996 cmp = strcmp (table[mid].name, s);
1998 if (cmp < 0)
1999 low = mid + 1;
2000 else if (cmp > 0)
2001 high = mid - 1;
2002 else
2003 return table + mid;
2006 return NULL;
2010 /* Return a numeric representation for font name field
2011 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2012 has DIM entries. Value is the numeric value found or DFLT if no
2013 table entry matches. This function is used to translate weight,
2014 slant, and swidth names of XLFD font names to numeric values. */
2016 static INLINE int
2017 xlfd_numeric_value (table, dim, font, field_index, dflt)
2018 struct table_entry *table;
2019 int dim;
2020 struct font_name *font;
2021 int field_index;
2022 int dflt;
2024 struct table_entry *p;
2025 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2026 return p ? p->numeric : dflt;
2030 /* Return a symbolic representation for font name field
2031 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
2032 has DIM entries. Value is the symbolic value found or DFLT if no
2033 table entry matches. This function is used to translate weight,
2034 slant, and swidth names of XLFD font names to symbols. */
2036 static INLINE Lisp_Object
2037 xlfd_symbolic_value (table, dim, font, field_index, dflt)
2038 struct table_entry *table;
2039 int dim;
2040 struct font_name *font;
2041 int field_index;
2042 Lisp_Object dflt;
2044 struct table_entry *p;
2045 p = xlfd_lookup_field_contents (table, dim, font, field_index);
2046 return p ? *p->symbol : dflt;
2050 /* Return a numeric value for the slant of the font given by FONT. */
2052 static INLINE int
2053 xlfd_numeric_slant (font)
2054 struct font_name *font;
2056 return xlfd_numeric_value (slant_table, DIM (slant_table),
2057 font, XLFD_SLANT, XLFD_SLANT_ROMAN);
2061 /* Return a symbol representing the weight of the font given by FONT. */
2063 static INLINE Lisp_Object
2064 xlfd_symbolic_slant (font)
2065 struct font_name *font;
2067 return xlfd_symbolic_value (slant_table, DIM (slant_table),
2068 font, XLFD_SLANT, Qnormal);
2072 /* Return a numeric value for the weight of the font given by FONT. */
2074 static INLINE int
2075 xlfd_numeric_weight (font)
2076 struct font_name *font;
2078 return xlfd_numeric_value (weight_table, DIM (weight_table),
2079 font, XLFD_WEIGHT, XLFD_WEIGHT_MEDIUM);
2083 /* Return a symbol representing the slant of the font given by FONT. */
2085 static INLINE Lisp_Object
2086 xlfd_symbolic_weight (font)
2087 struct font_name *font;
2089 return xlfd_symbolic_value (weight_table, DIM (weight_table),
2090 font, XLFD_WEIGHT, Qnormal);
2094 /* Return a numeric value for the swidth of the font whose XLFD font
2095 name fields are found in FONT. */
2097 static INLINE int
2098 xlfd_numeric_swidth (font)
2099 struct font_name *font;
2101 return xlfd_numeric_value (swidth_table, DIM (swidth_table),
2102 font, XLFD_SWIDTH, XLFD_SWIDTH_MEDIUM);
2106 /* Return a symbolic value for the swidth of FONT. */
2108 static INLINE Lisp_Object
2109 xlfd_symbolic_swidth (font)
2110 struct font_name *font;
2112 return xlfd_symbolic_value (swidth_table, DIM (swidth_table),
2113 font, XLFD_SWIDTH, Qnormal);
2117 /* Look up the entry of SYMBOL in the vector TABLE which has DIM
2118 entries. Value is a pointer to the matching table entry or null if
2119 no element of TABLE contains SYMBOL. */
2121 static struct table_entry *
2122 face_value (table, dim, symbol)
2123 struct table_entry *table;
2124 int dim;
2125 Lisp_Object symbol;
2127 int i;
2129 xassert (SYMBOLP (symbol));
2131 for (i = 0; i < dim; ++i)
2132 if (EQ (*table[i].symbol, symbol))
2133 break;
2135 return i < dim ? table + i : NULL;
2139 /* Return a numeric value for SYMBOL in the vector TABLE which has DIM
2140 entries. Value is -1 if SYMBOL is not found in TABLE. */
2142 static INLINE int
2143 face_numeric_value (table, dim, symbol)
2144 struct table_entry *table;
2145 int dim;
2146 Lisp_Object symbol;
2148 struct table_entry *p = face_value (table, dim, symbol);
2149 return p ? p->numeric : -1;
2153 /* Return a numeric value representing the weight specified by Lisp
2154 symbol WEIGHT. Value is one of the enumerators of enum
2155 xlfd_weight. */
2157 static INLINE int
2158 face_numeric_weight (weight)
2159 Lisp_Object weight;
2161 return face_numeric_value (weight_table, DIM (weight_table), weight);
2165 /* Return a numeric value representing the slant specified by Lisp
2166 symbol SLANT. Value is one of the enumerators of enum xlfd_slant. */
2168 static INLINE int
2169 face_numeric_slant (slant)
2170 Lisp_Object slant;
2172 return face_numeric_value (slant_table, DIM (slant_table), slant);
2176 /* Return a numeric value representing the swidth specified by Lisp
2177 symbol WIDTH. Value is one of the enumerators of enum xlfd_swidth. */
2179 static int
2180 face_numeric_swidth (width)
2181 Lisp_Object width;
2183 return face_numeric_value (swidth_table, DIM (swidth_table), width);
2187 #ifdef HAVE_WINDOW_SYSTEM
2189 /* Return non-zero if FONT is the name of a fixed-pitch font. */
2191 static INLINE int
2192 xlfd_fixed_p (font)
2193 struct font_name *font;
2195 /* Function split_font_name converts fields to lower-case, so there
2196 is no need to use tolower here. */
2197 return *font->fields[XLFD_SPACING] != 'p';
2201 /* Return the point size of FONT on frame F, measured in 1/10 pt.
2203 The actual height of the font when displayed on F depends on the
2204 resolution of both the font and frame. For example, a 10pt font
2205 designed for a 100dpi display will display larger than 10pt on a
2206 75dpi display. (It's not unusual to use fonts not designed for the
2207 display one is using. For example, some intlfonts are available in
2208 72dpi versions, only.)
2210 Value is the real point size of FONT on frame F, or 0 if it cannot
2211 be determined. */
2213 static INLINE int
2214 xlfd_point_size (f, font)
2215 struct frame *f;
2216 struct font_name *font;
2218 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2219 char *pixel_field = font->fields[XLFD_PIXEL_SIZE];
2220 double pixel;
2221 int real_pt;
2223 if (*pixel_field == '[')
2225 /* The pixel size field is `[A B C D]' which specifies
2226 a transformation matrix.
2228 A B 0
2229 C D 0
2230 0 0 1
2232 by which all glyphs of the font are transformed. The spec
2233 says that s scalar value N for the pixel size is equivalent
2234 to A = N * resx/resy, B = C = 0, D = N. */
2235 char *start = pixel_field + 1, *end;
2236 double matrix[4];
2237 int i;
2239 for (i = 0; i < 4; ++i)
2241 matrix[i] = strtod (start, &end);
2242 start = end;
2245 pixel = matrix[3];
2247 else
2248 pixel = atoi (pixel_field);
2250 if (pixel == 0)
2251 real_pt = 0;
2252 else
2253 real_pt = PT_PER_INCH * 10.0 * pixel / resy + 0.5;
2255 return real_pt;
2259 /* Return point size of PIXEL dots while considering Y-resultion (DPI)
2260 of frame F. This function is used to guess a point size of font
2261 when only the pixel height of the font is available. */
2263 static INLINE int
2264 pixel_point_size (f, pixel)
2265 struct frame *f;
2266 int pixel;
2268 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
2269 double real_pt;
2270 int int_pt;
2272 /* As one inch is PT_PER_INCH points, PT_PER_INCH/RESY gives the
2273 point size of one dot. */
2274 real_pt = pixel * PT_PER_INCH / resy;
2275 int_pt = real_pt + 0.5;
2277 return int_pt;
2281 /* Return a rescaling ratio of a font of NAME. */
2283 static double
2284 font_rescale_ratio (name)
2285 char *name;
2287 Lisp_Object tail, elt;
2289 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2291 elt = XCAR (tail);
2292 if (STRINGP (XCAR (elt)) && FLOATP (XCDR (elt))
2293 && fast_c_string_match_ignore_case (XCAR (elt), name) >= 0)
2294 return XFLOAT_DATA (XCDR (elt));
2296 return 1.0;
2300 /* Split XLFD font name FONT->name destructively into NUL-terminated,
2301 lower-case fields in FONT->fields. NUMERIC_P non-zero means
2302 compute numeric values for fields XLFD_POINT_SIZE, XLFD_SWIDTH,
2303 XLFD_RESY, XLFD_SLANT, and XLFD_WEIGHT in FONT->numeric. Value is
2304 zero if the font name doesn't have the format we expect. The
2305 expected format is a font name that starts with a `-' and has
2306 XLFD_LAST fields separated by `-'. */
2308 static int
2309 split_font_name (f, font, numeric_p)
2310 struct frame *f;
2311 struct font_name *font;
2312 int numeric_p;
2314 int i = 0;
2315 int success_p;
2316 double rescale_ratio;
2318 if (numeric_p)
2319 /* This must be done before splitting the font name. */
2320 rescale_ratio = font_rescale_ratio (font->name);
2322 if (*font->name == '-')
2324 char *p = xstrlwr (font->name) + 1;
2326 while (i < XLFD_LAST)
2328 font->fields[i] = p;
2329 ++i;
2331 /* Pixel and point size may be of the form `[....]'. For
2332 BNF, see XLFD spec, chapter 4. Negative values are
2333 indicated by tilde characters which we replace with
2334 `-' characters, here. */
2335 if (*p == '['
2336 && (i - 1 == XLFD_PIXEL_SIZE
2337 || i - 1 == XLFD_POINT_SIZE))
2339 char *start, *end;
2340 int j;
2342 for (++p; *p && *p != ']'; ++p)
2343 if (*p == '~')
2344 *p = '-';
2346 /* Check that the matrix contains 4 floating point
2347 numbers. */
2348 for (j = 0, start = font->fields[i - 1] + 1;
2349 j < 4;
2350 ++j, start = end)
2351 if (strtod (start, &end) == 0 && start == end)
2352 break;
2354 if (j < 4)
2355 break;
2358 while (*p && *p != '-')
2359 ++p;
2361 if (*p != '-')
2362 break;
2364 *p++ = 0;
2368 success_p = i == XLFD_LAST;
2370 /* If requested, and font name was in the expected format,
2371 compute numeric values for some fields. */
2372 if (numeric_p && success_p)
2374 font->numeric[XLFD_POINT_SIZE] = xlfd_point_size (f, font);
2375 font->numeric[XLFD_RESY] = atoi (font->fields[XLFD_RESY]);
2376 font->numeric[XLFD_SLANT] = xlfd_numeric_slant (font);
2377 font->numeric[XLFD_WEIGHT] = xlfd_numeric_weight (font);
2378 font->numeric[XLFD_SWIDTH] = xlfd_numeric_swidth (font);
2379 font->numeric[XLFD_AVGWIDTH] = atoi (font->fields[XLFD_AVGWIDTH]);
2380 font->rescale_ratio = rescale_ratio;
2383 /* Initialize it to zero. It will be overridden by font_list while
2384 trying alternate registries. */
2385 font->registry_priority = 0;
2387 return success_p;
2391 /* Build an XLFD font name from font name fields in FONT. Value is a
2392 pointer to the font name, which is allocated via xmalloc. */
2394 static char *
2395 build_font_name (font)
2396 struct font_name *font;
2398 int i;
2399 int size = 100;
2400 char *font_name = (char *) xmalloc (size);
2401 int total_length = 0;
2403 for (i = 0; i < XLFD_LAST; ++i)
2405 /* Add 1 because of the leading `-'. */
2406 int len = strlen (font->fields[i]) + 1;
2408 /* Reallocate font_name if necessary. Add 1 for the final
2409 NUL-byte. */
2410 if (total_length + len + 1 >= size)
2412 int new_size = max (2 * size, size + len + 1);
2413 int sz = new_size * sizeof *font_name;
2414 font_name = (char *) xrealloc (font_name, sz);
2415 size = new_size;
2418 font_name[total_length] = '-';
2419 bcopy (font->fields[i], font_name + total_length + 1, len - 1);
2420 total_length += len;
2423 font_name[total_length] = 0;
2424 return font_name;
2428 /* Free an array FONTS of N font_name structures. This frees FONTS
2429 itself and all `name' fields in its elements. */
2431 static INLINE void
2432 free_font_names (fonts, n)
2433 struct font_name *fonts;
2434 int n;
2436 while (n)
2437 xfree (fonts[--n].name);
2438 xfree (fonts);
2442 /* Sort vector FONTS of font_name structures which contains NFONTS
2443 elements using qsort and comparison function CMPFN. F is the frame
2444 on which the fonts will be used. The global variable font_frame
2445 is temporarily set to F to make it available in CMPFN. */
2447 static INLINE void
2448 sort_fonts (f, fonts, nfonts, cmpfn)
2449 struct frame *f;
2450 struct font_name *fonts;
2451 int nfonts;
2452 int (*cmpfn) P_ ((const void *, const void *));
2454 font_frame = f;
2455 qsort (fonts, nfonts, sizeof *fonts, cmpfn);
2456 font_frame = NULL;
2460 /* Get fonts matching PATTERN on frame F. If F is null, use the first
2461 display in x_display_list. FONTS is a pointer to a vector of
2462 NFONTS font_name structures. TRY_ALTERNATIVES_P non-zero means try
2463 alternative patterns from Valternate_fontname_alist if no fonts are
2464 found matching PATTERN.
2466 For all fonts found, set FONTS[i].name to the name of the font,
2467 allocated via xmalloc, and split font names into fields. Ignore
2468 fonts that we can't parse. Value is the number of fonts found. */
2470 static int
2471 x_face_list_fonts (f, pattern, pfonts, nfonts, try_alternatives_p)
2472 struct frame *f;
2473 char *pattern;
2474 struct font_name **pfonts;
2475 int nfonts, try_alternatives_p;
2477 int n, nignored;
2479 /* NTEMACS_TODO : currently this uses w32_list_fonts, but it may be
2480 better to do it the other way around. */
2481 Lisp_Object lfonts;
2482 Lisp_Object lpattern, tem;
2483 struct font_name *fonts = 0;
2484 int num_fonts = nfonts;
2486 *pfonts = 0;
2487 lpattern = build_string (pattern);
2489 /* Get the list of fonts matching PATTERN. */
2490 #ifdef WINDOWSNT
2491 BLOCK_INPUT;
2492 lfonts = w32_list_fonts (f, lpattern, 0, nfonts);
2493 UNBLOCK_INPUT;
2494 #else
2495 lfonts = x_list_fonts (f, lpattern, -1, nfonts);
2496 #endif
2498 if (nfonts < 0 && CONSP (lfonts))
2499 num_fonts = XFASTINT (Flength (lfonts));
2501 /* Make a copy of the font names we got from X, and
2502 split them into fields. */
2503 n = nignored = 0;
2504 for (tem = lfonts; CONSP (tem) && n < num_fonts; tem = XCDR (tem))
2506 Lisp_Object elt, tail;
2507 const char *name = SDATA (XCAR (tem));
2509 /* Ignore fonts matching a pattern from face-ignored-fonts. */
2510 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2512 elt = XCAR (tail);
2513 if (STRINGP (elt)
2514 && fast_c_string_match_ignore_case (elt, name) >= 0)
2515 break;
2517 if (!NILP (tail))
2519 ++nignored;
2520 continue;
2523 if (! fonts)
2525 *pfonts = (struct font_name *) xmalloc (num_fonts * sizeof **pfonts);
2526 fonts = *pfonts;
2529 /* Make a copy of the font name. */
2530 fonts[n].name = xstrdup (name);
2532 if (split_font_name (f, fonts + n, 1))
2534 if (font_scalable_p (fonts + n)
2535 && !may_use_scalable_font_p (name))
2537 ++nignored;
2538 xfree (fonts[n].name);
2540 else
2541 ++n;
2543 else
2544 xfree (fonts[n].name);
2547 /* If no fonts found, try patterns from Valternate_fontname_alist. */
2548 if (n == 0 && try_alternatives_p)
2550 Lisp_Object list = Valternate_fontname_alist;
2552 if (*pfonts)
2554 xfree (*pfonts);
2555 *pfonts = 0;
2558 while (CONSP (list))
2560 Lisp_Object entry = XCAR (list);
2561 if (CONSP (entry)
2562 && STRINGP (XCAR (entry))
2563 && strcmp (SDATA (XCAR (entry)), pattern) == 0)
2564 break;
2565 list = XCDR (list);
2568 if (CONSP (list))
2570 Lisp_Object patterns = XCAR (list);
2571 Lisp_Object name;
2573 while (CONSP (patterns)
2574 /* If list is screwed up, give up. */
2575 && (name = XCAR (patterns),
2576 STRINGP (name))
2577 /* Ignore patterns equal to PATTERN because we tried that
2578 already with no success. */
2579 && (strcmp (SDATA (name), pattern) == 0
2580 || (n = x_face_list_fonts (f, SDATA (name),
2581 pfonts, nfonts, 0),
2582 n == 0)))
2583 patterns = XCDR (patterns);
2587 return n;
2591 /* Check if a font matching pattern_offset_t on frame F is available
2592 or not. PATTERN may be a cons (FAMILY . REGISTRY), in which case,
2593 a font name pattern is generated from FAMILY and REGISTRY. */
2596 face_font_available_p (f, pattern)
2597 struct frame *f;
2598 Lisp_Object pattern;
2600 Lisp_Object fonts;
2602 if (! STRINGP (pattern))
2604 Lisp_Object family, registry;
2605 char *family_str, *registry_str, *pattern_str;
2607 CHECK_CONS (pattern);
2608 family = XCAR (pattern);
2609 if (NILP (family))
2610 family_str = "*";
2611 else
2613 CHECK_STRING (family);
2614 family_str = (char *) SDATA (family);
2616 registry = XCDR (pattern);
2617 if (NILP (registry))
2618 registry_str = "*";
2619 else
2621 CHECK_STRING (registry);
2622 registry_str = (char *) SDATA (registry);
2625 pattern_str = (char *) alloca (strlen (family_str)
2626 + strlen (registry_str)
2627 + 10);
2628 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2629 strcat (pattern_str, family_str);
2630 strcat (pattern_str, "-*-");
2631 strcat (pattern_str, registry_str);
2632 if (!index (registry_str, '-'))
2634 if (registry_str[strlen (registry_str) - 1] == '*')
2635 strcat (pattern_str, "-*");
2636 else
2637 strcat (pattern_str, "*-*");
2639 pattern = build_string (pattern_str);
2642 /* Get the list of fonts matching PATTERN. */
2643 #ifdef WINDOWSNT
2644 BLOCK_INPUT;
2645 fonts = w32_list_fonts (f, pattern, 0, 1);
2646 UNBLOCK_INPUT;
2647 #else
2648 fonts = x_list_fonts (f, pattern, -1, 1);
2649 #endif
2650 return XINT (Flength (fonts));
2654 /* Determine fonts matching PATTERN on frame F. Sort resulting fonts
2655 using comparison function CMPFN. Value is the number of fonts
2656 found. If value is non-zero, *FONTS is set to a vector of
2657 font_name structures allocated from the heap containing matching
2658 fonts. Each element of *FONTS contains a name member that is also
2659 allocated from the heap. Font names in these structures are split
2660 into fields. Use free_font_names to free such an array. */
2662 static int
2663 sorted_font_list (f, pattern, cmpfn, fonts)
2664 struct frame *f;
2665 char *pattern;
2666 int (*cmpfn) P_ ((const void *, const void *));
2667 struct font_name **fonts;
2669 int nfonts;
2671 /* Get the list of fonts matching pattern. 100 should suffice. */
2672 nfonts = DEFAULT_FONT_LIST_LIMIT;
2673 if (INTEGERP (Vfont_list_limit))
2674 nfonts = XINT (Vfont_list_limit);
2676 *fonts = NULL;
2677 nfonts = x_face_list_fonts (f, pattern, fonts, nfonts, 1);
2679 /* Sort the resulting array and return it in *FONTS. If no
2680 fonts were found, make sure to set *FONTS to null. */
2681 if (nfonts)
2682 sort_fonts (f, *fonts, nfonts, cmpfn);
2683 else if (*fonts)
2685 xfree (*fonts);
2686 *fonts = NULL;
2689 return nfonts;
2693 /* Compare two font_name structures *A and *B. Value is analogous to
2694 strcmp. Sort order is given by the global variable
2695 font_sort_order. Font names are sorted so that, everything else
2696 being equal, fonts with a resolution closer to that of the frame on
2697 which they are used are listed first. The global variable
2698 font_frame is the frame on which we operate. */
2700 static int
2701 cmp_font_names (a, b)
2702 const void *a, *b;
2704 struct font_name *x = (struct font_name *) a;
2705 struct font_name *y = (struct font_name *) b;
2706 int cmp;
2708 /* All strings have been converted to lower-case by split_font_name,
2709 so we can use strcmp here. */
2710 cmp = strcmp (x->fields[XLFD_FAMILY], y->fields[XLFD_FAMILY]);
2711 if (cmp == 0)
2713 int i;
2715 for (i = 0; i < DIM (font_sort_order) && cmp == 0; ++i)
2717 int j = font_sort_order[i];
2718 cmp = x->numeric[j] - y->numeric[j];
2721 if (cmp == 0)
2723 /* Everything else being equal, we prefer fonts with an
2724 y-resolution closer to that of the frame. */
2725 int resy = FRAME_X_DISPLAY_INFO (font_frame)->resy;
2726 int x_resy = x->numeric[XLFD_RESY];
2727 int y_resy = y->numeric[XLFD_RESY];
2728 cmp = abs (resy - x_resy) - abs (resy - y_resy);
2732 return cmp;
2736 /* Get a sorted list of fonts of family FAMILY on frame F. If PATTERN
2737 is non-nil list fonts matching that pattern. Otherwise, if
2738 REGISTRY is non-nil return only fonts with that registry, otherwise
2739 return fonts of any registry. Set *FONTS to a vector of font_name
2740 structures allocated from the heap containing the fonts found.
2741 Value is the number of fonts found. */
2743 static int
2744 font_list_1 (f, pattern, family, registry, fonts)
2745 struct frame *f;
2746 Lisp_Object pattern, family, registry;
2747 struct font_name **fonts;
2749 char *pattern_str, *family_str, *registry_str;
2751 if (NILP (pattern))
2753 family_str = (NILP (family) ? "*" : (char *) SDATA (family));
2754 registry_str = (NILP (registry) ? "*" : (char *) SDATA (registry));
2756 pattern_str = (char *) alloca (strlen (family_str)
2757 + strlen (registry_str)
2758 + 10);
2759 strcpy (pattern_str, index (family_str, '-') ? "-" : "-*-");
2760 strcat (pattern_str, family_str);
2761 strcat (pattern_str, "-*-");
2762 strcat (pattern_str, registry_str);
2763 if (!index (registry_str, '-'))
2765 if (registry_str[strlen (registry_str) - 1] == '*')
2766 strcat (pattern_str, "-*");
2767 else
2768 strcat (pattern_str, "*-*");
2771 else
2772 pattern_str = (char *) SDATA (pattern);
2774 return sorted_font_list (f, pattern_str, cmp_font_names, fonts);
2778 /* Concatenate font list FONTS1 and FONTS2. FONTS1 and FONTS2
2779 contains NFONTS1 fonts and NFONTS2 fonts respectively. Return a
2780 pointer to a newly allocated font list. FONTS1 and FONTS2 are
2781 freed. */
2783 static struct font_name *
2784 concat_font_list (fonts1, nfonts1, fonts2, nfonts2)
2785 struct font_name *fonts1, *fonts2;
2786 int nfonts1, nfonts2;
2788 int new_nfonts = nfonts1 + nfonts2;
2789 struct font_name *new_fonts;
2791 new_fonts = (struct font_name *) xmalloc (sizeof *new_fonts * new_nfonts);
2792 bcopy (fonts1, new_fonts, sizeof *new_fonts * nfonts1);
2793 bcopy (fonts2, new_fonts + nfonts1, sizeof *new_fonts * nfonts2);
2794 xfree (fonts1);
2795 xfree (fonts2);
2796 return new_fonts;
2800 /* Get a sorted list of fonts of family FAMILY on frame F.
2802 If PATTERN is non-nil list fonts matching that pattern.
2804 If REGISTRY is non-nil, return fonts with that registry and the
2805 alternative registries from Vface_alternative_font_registry_alist.
2807 If REGISTRY is nil return fonts of any registry.
2809 Set *FONTS to a vector of font_name structures allocated from the
2810 heap containing the fonts found. Value is the number of fonts
2811 found. */
2813 static int
2814 font_list (f, pattern, family, registry, fonts)
2815 struct frame *f;
2816 Lisp_Object pattern, family, registry;
2817 struct font_name **fonts;
2819 int nfonts = font_list_1 (f, pattern, family, registry, fonts);
2821 if (!NILP (registry)
2822 && CONSP (Vface_alternative_font_registry_alist))
2824 Lisp_Object alter;
2826 alter = Fassoc (registry, Vface_alternative_font_registry_alist);
2827 if (CONSP (alter))
2829 int reg_prio, i;
2831 for (alter = XCDR (alter), reg_prio = 1;
2832 CONSP (alter);
2833 alter = XCDR (alter), reg_prio++)
2834 if (STRINGP (XCAR (alter)))
2836 int nfonts2;
2837 struct font_name *fonts2;
2839 nfonts2 = font_list_1 (f, pattern, family, XCAR (alter),
2840 &fonts2);
2841 for (i = 0; i < nfonts2; i++)
2842 fonts2[i].registry_priority = reg_prio;
2843 *fonts = (nfonts > 0
2844 ? concat_font_list (*fonts, nfonts, fonts2, nfonts2)
2845 : fonts2);
2846 nfonts += nfonts2;
2851 return nfonts;
2855 /* Remove elements from LIST whose cars are `equal'. Called from
2856 x-family-fonts and x-font-family-list to remove duplicate font
2857 entries. */
2859 static void
2860 remove_duplicates (list)
2861 Lisp_Object list;
2863 Lisp_Object tail = list;
2865 while (!NILP (tail) && !NILP (XCDR (tail)))
2867 Lisp_Object next = XCDR (tail);
2868 if (!NILP (Fequal (XCAR (next), XCAR (tail))))
2869 XSETCDR (tail, XCDR (next));
2870 else
2871 tail = XCDR (tail);
2876 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
2877 doc: /* Return a list of available fonts of family FAMILY on FRAME.
2878 If FAMILY is omitted or nil, list all families.
2879 Otherwise, FAMILY must be a string, possibly containing wildcards
2880 `?' and `*'.
2881 If FRAME is omitted or nil, use the selected frame.
2882 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
2883 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
2884 FAMILY is the font family name. POINT-SIZE is the size of the
2885 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
2886 width, weight and slant of the font. These symbols are the same as for
2887 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
2888 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
2889 giving the registry and encoding of the font.
2890 The result list is sorted according to the current setting of
2891 the face font sort order. */)
2892 (family, frame)
2893 Lisp_Object family, frame;
2895 struct frame *f = check_x_frame (frame);
2896 struct font_name *fonts;
2897 int i, nfonts;
2898 Lisp_Object result;
2899 struct gcpro gcpro1;
2901 if (!NILP (family))
2902 CHECK_STRING (family);
2904 result = Qnil;
2905 GCPRO1 (result);
2906 nfonts = font_list (f, Qnil, family, Qnil, &fonts);
2907 for (i = nfonts - 1; i >= 0; --i)
2909 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
2910 char *tem;
2912 ASET (v, 0, build_string (fonts[i].fields[XLFD_FAMILY]));
2913 ASET (v, 1, xlfd_symbolic_swidth (fonts + i));
2914 ASET (v, 2, make_number (xlfd_point_size (f, fonts + i)));
2915 ASET (v, 3, xlfd_symbolic_weight (fonts + i));
2916 ASET (v, 4, xlfd_symbolic_slant (fonts + i));
2917 ASET (v, 5, xlfd_fixed_p (fonts + i) ? Qt : Qnil);
2918 tem = build_font_name (fonts + i);
2919 ASET (v, 6, build_string (tem));
2920 sprintf (tem, "%s-%s", fonts[i].fields[XLFD_REGISTRY],
2921 fonts[i].fields[XLFD_ENCODING]);
2922 ASET (v, 7, build_string (tem));
2923 xfree (tem);
2925 result = Fcons (v, result);
2928 remove_duplicates (result);
2929 free_font_names (fonts, nfonts);
2930 UNGCPRO;
2931 return result;
2935 DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
2936 0, 1, 0,
2937 doc: /* Return a list of available font families on FRAME.
2938 If FRAME is omitted or nil, use the selected frame.
2939 Value is a list of conses (FAMILY . FIXED-P) where FAMILY
2940 is a font family, and FIXED-P is non-nil if fonts of that family
2941 are fixed-pitch. */)
2942 (frame)
2943 Lisp_Object frame;
2945 struct frame *f = check_x_frame (frame);
2946 int nfonts, i;
2947 struct font_name *fonts;
2948 Lisp_Object result;
2949 struct gcpro gcpro1;
2950 int count = SPECPDL_INDEX ();
2952 /* Let's consider all fonts. */
2953 specbind (intern ("font-list-limit"), make_number (-1));
2954 nfonts = font_list (f, Qnil, Qnil, Qnil, &fonts);
2956 result = Qnil;
2957 GCPRO1 (result);
2958 for (i = nfonts - 1; i >= 0; --i)
2959 result = Fcons (Fcons (build_string (fonts[i].fields[XLFD_FAMILY]),
2960 xlfd_fixed_p (fonts + i) ? Qt : Qnil),
2961 result);
2963 remove_duplicates (result);
2964 free_font_names (fonts, nfonts);
2965 UNGCPRO;
2966 return unbind_to (count, result);
2970 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
2971 doc: /* Return a list of the names of available fonts matching PATTERN.
2972 If optional arguments FACE and FRAME are specified, return only fonts
2973 the same size as FACE on FRAME.
2974 PATTERN is a string, perhaps with wildcard characters;
2975 the * character matches any substring, and
2976 the ? character matches any single character.
2977 PATTERN is case-insensitive.
2978 FACE is a face name--a symbol.
2980 The return value is a list of strings, suitable as arguments to
2981 set-face-font.
2983 Fonts Emacs can't use may or may not be excluded
2984 even if they match PATTERN and FACE.
2985 The optional fourth argument MAXIMUM sets a limit on how many
2986 fonts to match. The first MAXIMUM fonts are reported.
2987 The optional fifth argument WIDTH, if specified, is a number of columns
2988 occupied by a character of a font. In that case, return only fonts
2989 the WIDTH times as wide as FACE on FRAME. */)
2990 (pattern, face, frame, maximum, width)
2991 Lisp_Object pattern, face, frame, maximum, width;
2993 struct frame *f;
2994 int size;
2995 int maxnames;
2997 check_x ();
2998 CHECK_STRING (pattern);
3000 if (NILP (maximum))
3001 maxnames = -1;
3002 else
3004 CHECK_NATNUM (maximum);
3005 maxnames = XINT (maximum);
3008 if (!NILP (width))
3009 CHECK_NUMBER (width);
3011 /* We can't simply call check_x_frame because this function may be
3012 called before any frame is created. */
3013 f = frame_or_selected_frame (frame, 2);
3014 if (!FRAME_WINDOW_P (f))
3016 /* Perhaps we have not yet created any frame. */
3017 f = NULL;
3018 face = Qnil;
3021 /* Determine the width standard for comparison with the fonts we find. */
3023 if (NILP (face))
3024 size = 0;
3025 else
3027 /* This is of limited utility since it works with character
3028 widths. Keep it for compatibility. --gerd. */
3029 int face_id = lookup_named_face (f, face, 0, 0);
3030 struct face *face = (face_id < 0
3031 ? NULL
3032 : FACE_FROM_ID (f, face_id));
3034 #ifdef WINDOWSNT
3035 /* For historic reasons, FONT_WIDTH refers to average width on W32,
3036 not maximum as on X. Redefine here. */
3037 #undef FONT_WIDTH
3038 #define FONT_WIDTH FONT_MAX_WIDTH
3039 #endif
3041 if (face && face->font)
3042 size = FONT_WIDTH (face->font);
3043 else
3044 size = FONT_WIDTH (FRAME_FONT (f)); /* FRAME_COLUMN_WIDTH (f) */
3046 if (!NILP (width))
3047 size *= XINT (width);
3051 Lisp_Object args[2];
3053 args[0] = x_list_fonts (f, pattern, size, maxnames);
3054 if (f == NULL)
3055 /* We don't have to check fontsets. */
3056 return args[0];
3057 args[1] = list_fontsets (f, pattern, size);
3058 return Fnconc (2, args);
3062 #endif /* HAVE_WINDOW_SYSTEM */
3066 /***********************************************************************
3067 Lisp Faces
3068 ***********************************************************************/
3070 /* Access face attributes of face LFACE, a Lisp vector. */
3072 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
3073 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
3074 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
3075 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
3076 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
3077 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
3078 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
3079 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
3080 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
3081 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
3082 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
3083 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
3084 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
3085 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
3086 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
3087 #define LFACE_AVGWIDTH(LFACE) AREF ((LFACE), LFACE_AVGWIDTH_INDEX)
3089 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
3090 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
3092 #define LFACEP(LFACE) \
3093 (VECTORP (LFACE) \
3094 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
3095 && EQ (AREF (LFACE, 0), Qface))
3098 #if GLYPH_DEBUG
3100 /* Check consistency of Lisp face attribute vector ATTRS. */
3102 static void
3103 check_lface_attrs (attrs)
3104 Lisp_Object *attrs;
3106 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
3107 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
3108 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
3109 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
3110 xassert (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
3111 || INTEGERP (attrs[LFACE_AVGWIDTH_INDEX]));
3112 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
3113 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
3114 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
3115 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
3116 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
3117 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
3118 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
3119 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
3120 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
3121 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
3122 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
3123 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
3124 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
3125 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
3126 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3127 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
3128 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
3129 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
3130 || SYMBOLP (attrs[LFACE_BOX_INDEX])
3131 || STRINGP (attrs[LFACE_BOX_INDEX])
3132 || INTEGERP (attrs[LFACE_BOX_INDEX])
3133 || CONSP (attrs[LFACE_BOX_INDEX]));
3134 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
3135 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
3136 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
3137 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
3138 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
3139 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
3140 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
3141 || NILP (attrs[LFACE_INHERIT_INDEX])
3142 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
3143 || CONSP (attrs[LFACE_INHERIT_INDEX]));
3144 #ifdef HAVE_WINDOW_SYSTEM
3145 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
3146 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
3147 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
3148 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
3149 || NILP (attrs[LFACE_FONT_INDEX])
3150 || STRINGP (attrs[LFACE_FONT_INDEX]));
3151 #endif
3155 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
3157 static void
3158 check_lface (lface)
3159 Lisp_Object lface;
3161 if (!NILP (lface))
3163 xassert (LFACEP (lface));
3164 check_lface_attrs (XVECTOR (lface)->contents);
3168 #else /* GLYPH_DEBUG == 0 */
3170 #define check_lface_attrs(attrs) (void) 0
3171 #define check_lface(lface) (void) 0
3173 #endif /* GLYPH_DEBUG == 0 */
3177 /* Face-merge cycle checking. */
3179 /* A `named merge point' is simply a point during face-merging where we
3180 look up a face by name. We keep a stack of which named lookups we're
3181 currently processing so that we can easily detect cycles, using a
3182 linked- list of struct named_merge_point structures, typically
3183 allocated on the stack frame of the named lookup functions which are
3184 active (so no consing is required). */
3185 struct named_merge_point
3187 Lisp_Object face_name;
3188 struct named_merge_point *prev;
3192 /* If a face merging cycle is detected for FACE_NAME, return 0,
3193 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
3194 FACE_NAME, as the head of the linked list pointed to by
3195 NAMED_MERGE_POINTS, and return 1. */
3197 static INLINE int
3198 push_named_merge_point (struct named_merge_point *new_named_merge_point,
3199 Lisp_Object face_name,
3200 struct named_merge_point **named_merge_points)
3202 struct named_merge_point *prev;
3204 for (prev = *named_merge_points; prev; prev = prev->prev)
3205 if (EQ (face_name, prev->face_name))
3206 return 0;
3208 new_named_merge_point->face_name = face_name;
3209 new_named_merge_point->prev = *named_merge_points;
3211 *named_merge_points = new_named_merge_point;
3213 return 1;
3219 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
3220 to make it a symbol. If FACE_NAME is an alias for another face,
3221 return that face's name.
3223 Return default face in case of errors. */
3225 static Lisp_Object
3226 resolve_face_name (face_name, signal_p)
3227 Lisp_Object face_name;
3228 int signal_p;
3230 Lisp_Object orig_face;
3231 Lisp_Object tortoise, hare;
3233 if (STRINGP (face_name))
3234 face_name = intern (SDATA (face_name));
3236 if (NILP (face_name) || !SYMBOLP (face_name))
3237 return face_name;
3239 orig_face = face_name;
3240 tortoise = hare = face_name;
3242 while (1)
3244 face_name = hare;
3245 hare = Fget (hare, Qface_alias);
3246 if (NILP (hare) || !SYMBOLP (hare))
3247 break;
3249 face_name = hare;
3250 hare = Fget (hare, Qface_alias);
3251 if (NILP (hare) || !SYMBOLP (hare))
3252 break;
3254 tortoise = Fget (tortoise, Qface_alias);
3255 if (EQ (hare, tortoise))
3257 if (signal_p)
3258 Fsignal (Qcircular_list, Fcons (orig_face, Qnil));
3259 return Qdefault;
3263 return face_name;
3267 /* Return the face definition of FACE_NAME on frame F. F null means
3268 return the definition for new frames. FACE_NAME may be a string or
3269 a symbol (apparently Emacs 20.2 allowed strings as face names in
3270 face text properties; Ediff uses that). If FACE_NAME is an alias
3271 for another face, return that face's definition. If SIGNAL_P is
3272 non-zero, signal an error if FACE_NAME is not a valid face name.
3273 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
3274 name. */
3276 static INLINE Lisp_Object
3277 lface_from_face_name (f, face_name, signal_p)
3278 struct frame *f;
3279 Lisp_Object face_name;
3280 int signal_p;
3282 Lisp_Object lface;
3284 face_name = resolve_face_name (face_name, signal_p);
3286 if (f)
3287 lface = assq_no_quit (face_name, f->face_alist);
3288 else
3289 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
3291 if (CONSP (lface))
3292 lface = XCDR (lface);
3293 else if (signal_p)
3294 signal_error ("Invalid face", face_name);
3296 check_lface (lface);
3297 return lface;
3301 /* Get face attributes of face FACE_NAME from frame-local faces on
3302 frame F. Store the resulting attributes in ATTRS which must point
3303 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
3304 is non-zero, signal an error if FACE_NAME does not name a face.
3305 Otherwise, value is zero if FACE_NAME is not a face. */
3307 static INLINE int
3308 get_lface_attributes (f, face_name, attrs, signal_p)
3309 struct frame *f;
3310 Lisp_Object face_name;
3311 Lisp_Object *attrs;
3312 int signal_p;
3314 Lisp_Object lface;
3315 int success_p;
3317 lface = lface_from_face_name (f, face_name, signal_p);
3318 if (!NILP (lface))
3320 bcopy (XVECTOR (lface)->contents, attrs,
3321 LFACE_VECTOR_SIZE * sizeof *attrs);
3322 success_p = 1;
3324 else
3325 success_p = 0;
3327 return success_p;
3331 /* Non-zero if all attributes in face attribute vector ATTRS are
3332 specified, i.e. are non-nil. */
3334 static int
3335 lface_fully_specified_p (attrs)
3336 Lisp_Object *attrs;
3338 int i;
3340 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3341 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
3342 && i != LFACE_AVGWIDTH_INDEX)
3343 if (UNSPECIFIEDP (attrs[i])
3344 #ifdef MAC_OS
3345 /* MAC_TODO: No stipple support on Mac OS yet, this index is
3346 always unspecified. */
3347 && i != LFACE_STIPPLE_INDEX
3348 #endif
3350 break;
3352 return i == LFACE_VECTOR_SIZE;
3355 #ifdef HAVE_WINDOW_SYSTEM
3357 /* Set font-related attributes of Lisp face LFACE from the fullname of
3358 the font opened by FONTNAME. If FORCE_P is zero, set only
3359 unspecified attributes of LFACE. The exception is `font'
3360 attribute. It is set to FONTNAME as is regardless of FORCE_P.
3362 If FONTNAME is not available on frame F,
3363 return 0 if MAY_FAIL_P is non-zero, otherwise abort.
3364 If the fullname is not in a valid XLFD format,
3365 return 0 if MAY_FAIL_P is non-zero, otherwise set normal values
3366 in LFACE and return 1.
3367 Otherwise, return 1. */
3369 static int
3370 set_lface_from_font_name (f, lface, fontname, force_p, may_fail_p)
3371 struct frame *f;
3372 Lisp_Object lface;
3373 Lisp_Object fontname;
3374 int force_p, may_fail_p;
3376 struct font_name font;
3377 char *buffer;
3378 int pt;
3379 int have_xlfd_p;
3380 int fontset;
3381 char *font_name = SDATA (fontname);
3382 struct font_info *font_info;
3384 /* If FONTNAME is actually a fontset name, get ASCII font name of it. */
3385 fontset = fs_query_fontset (fontname, 0);
3386 if (fontset >= 0)
3387 font_name = SDATA (fontset_ascii (fontset));
3389 /* Check if FONT_NAME is surely available on the system. Usually
3390 FONT_NAME is already cached for the frame F and FS_LOAD_FONT
3391 returns quickly. But, even if FONT_NAME is not yet cached,
3392 caching it now is not futail because we anyway load the font
3393 later. */
3394 BLOCK_INPUT;
3395 font_info = FS_LOAD_FONT (f, 0, font_name, -1);
3396 UNBLOCK_INPUT;
3398 if (!font_info)
3400 if (may_fail_p)
3401 return 0;
3402 abort ();
3405 font.name = STRDUPA (font_info->full_name);
3406 have_xlfd_p = split_font_name (f, &font, 1);
3408 /* Set attributes only if unspecified, otherwise face defaults for
3409 new frames would never take effect. If we couldn't get a font
3410 name conforming to XLFD, set normal values. */
3412 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
3414 Lisp_Object val;
3415 if (have_xlfd_p)
3417 buffer = (char *) alloca (strlen (font.fields[XLFD_FAMILY])
3418 + strlen (font.fields[XLFD_FOUNDRY])
3419 + 2);
3420 sprintf (buffer, "%s-%s", font.fields[XLFD_FOUNDRY],
3421 font.fields[XLFD_FAMILY]);
3422 val = build_string (buffer);
3424 else
3425 val = build_string ("*");
3426 LFACE_FAMILY (lface) = val;
3429 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
3431 if (have_xlfd_p)
3432 pt = xlfd_point_size (f, &font);
3433 else
3434 pt = pixel_point_size (f, font_info->height * 10);
3435 xassert (pt > 0);
3436 LFACE_HEIGHT (lface) = make_number (pt);
3439 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
3440 LFACE_SWIDTH (lface)
3441 = have_xlfd_p ? xlfd_symbolic_swidth (&font) : Qnormal;
3443 if (force_p || UNSPECIFIEDP (LFACE_AVGWIDTH (lface)))
3444 LFACE_AVGWIDTH (lface)
3445 = (have_xlfd_p
3446 ? make_number (font.numeric[XLFD_AVGWIDTH])
3447 : Qunspecified);
3449 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
3450 LFACE_WEIGHT (lface)
3451 = have_xlfd_p ? xlfd_symbolic_weight (&font) : Qnormal;
3453 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
3454 LFACE_SLANT (lface)
3455 = have_xlfd_p ? xlfd_symbolic_slant (&font) : Qnormal;
3457 LFACE_FONT (lface) = fontname;
3459 return 1;
3462 #endif /* HAVE_WINDOW_SYSTEM */
3465 /* Merges the face height FROM with the face height TO, and returns the
3466 merged height. If FROM is an invalid height, then INVALID is
3467 returned instead. FROM and TO may be either absolute face heights or
3468 `relative' heights; the returned value is always an absolute height
3469 unless both FROM and TO are relative. GCPRO is a lisp value that
3470 will be protected from garbage-collection if this function makes a
3471 call into lisp. */
3473 Lisp_Object
3474 merge_face_heights (from, to, invalid)
3475 Lisp_Object from, to, invalid;
3477 Lisp_Object result = invalid;
3479 if (INTEGERP (from))
3480 /* FROM is absolute, just use it as is. */
3481 result = from;
3482 else if (FLOATP (from))
3483 /* FROM is a scale, use it to adjust TO. */
3485 if (INTEGERP (to))
3486 /* relative X absolute => absolute */
3487 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
3488 else if (FLOATP (to))
3489 /* relative X relative => relative */
3490 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
3491 else if (UNSPECIFIEDP (to))
3492 result = from;
3494 else if (FUNCTIONP (from))
3495 /* FROM is a function, which use to adjust TO. */
3497 /* Call function with current height as argument.
3498 From is the new height. */
3499 Lisp_Object args[2];
3501 args[0] = from;
3502 args[1] = to;
3503 result = safe_call (2, args);
3505 /* Ensure that if TO was absolute, so is the result. */
3506 if (INTEGERP (to) && !INTEGERP (result))
3507 result = invalid;
3510 return result;
3514 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
3515 store the resulting attributes in TO, which must be already be
3516 completely specified and contain only absolute attributes. Every
3517 specified attribute of FROM overrides the corresponding attribute of
3518 TO; relative attributes in FROM are merged with the absolute value in
3519 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
3520 loops in face inheritance; it should be 0 when called from other
3521 places. */
3523 static INLINE void
3524 merge_face_vectors (f, from, to, named_merge_points)
3525 struct frame *f;
3526 Lisp_Object *from, *to;
3527 struct named_merge_point *named_merge_points;
3529 int i;
3531 /* If FROM inherits from some other faces, merge their attributes into
3532 TO before merging FROM's direct attributes. Note that an :inherit
3533 attribute of `unspecified' is the same as one of nil; we never
3534 merge :inherit attributes, so nil is more correct, but lots of
3535 other code uses `unspecified' as a generic value for face attributes. */
3536 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
3537 && !NILP (from[LFACE_INHERIT_INDEX]))
3538 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
3540 /* If TO specifies a :font attribute, and FROM specifies some
3541 font-related attribute, we need to clear TO's :font attribute
3542 (because it will be inconsistent with whatever FROM specifies, and
3543 FROM takes precedence). */
3544 if (!NILP (to[LFACE_FONT_INDEX])
3545 && (!UNSPECIFIEDP (from[LFACE_FAMILY_INDEX])
3546 || !UNSPECIFIEDP (from[LFACE_HEIGHT_INDEX])
3547 || !UNSPECIFIEDP (from[LFACE_WEIGHT_INDEX])
3548 || !UNSPECIFIEDP (from[LFACE_SLANT_INDEX])
3549 || !UNSPECIFIEDP (from[LFACE_SWIDTH_INDEX])
3550 || !UNSPECIFIEDP (from[LFACE_AVGWIDTH_INDEX])))
3551 to[LFACE_FONT_INDEX] = Qnil;
3553 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3554 if (!UNSPECIFIEDP (from[i]))
3556 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
3557 to[i] = merge_face_heights (from[i], to[i], to[i]);
3558 else
3559 to[i] = from[i];
3562 /* TO is always an absolute face, which should inherit from nothing.
3563 We blindly copy the :inherit attribute above and fix it up here. */
3564 to[LFACE_INHERIT_INDEX] = Qnil;
3567 /* Merge the named face FACE_NAME on frame F, into the vector of face
3568 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
3569 inheritance. Returns true if FACE_NAME is a valid face name and
3570 merging succeeded. */
3572 static int
3573 merge_named_face (f, face_name, to, named_merge_points)
3574 struct frame *f;
3575 Lisp_Object face_name;
3576 Lisp_Object *to;
3577 struct named_merge_point *named_merge_points;
3579 struct named_merge_point named_merge_point;
3581 if (push_named_merge_point (&named_merge_point,
3582 face_name, &named_merge_points))
3584 struct gcpro gcpro1;
3585 Lisp_Object from[LFACE_VECTOR_SIZE];
3586 int ok = get_lface_attributes (f, face_name, from, 0);
3588 if (ok)
3590 GCPRO1 (named_merge_point.face_name);
3591 merge_face_vectors (f, from, to, named_merge_points);
3592 UNGCPRO;
3595 return ok;
3597 else
3598 return 0;
3602 /* Merge face attributes from the lisp `face reference' FACE_REF on
3603 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
3604 problems with FACE_REF cause an error message to be shown. Return
3605 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
3606 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
3607 list structure; it may be 0 for most callers.
3609 FACE_REF may be a single face specification or a list of such
3610 specifications. Each face specification can be:
3612 1. A symbol or string naming a Lisp face.
3614 2. A property list of the form (KEYWORD VALUE ...) where each
3615 KEYWORD is a face attribute name, and value is an appropriate value
3616 for that attribute.
3618 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
3619 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
3620 for compatibility with 20.2.
3622 Face specifications earlier in lists take precedence over later
3623 specifications. */
3625 static int
3626 merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
3627 struct frame *f;
3628 Lisp_Object face_ref;
3629 Lisp_Object *to;
3630 int err_msgs;
3631 struct named_merge_point *named_merge_points;
3633 int ok = 1; /* Succeed without an error? */
3635 if (CONSP (face_ref))
3637 Lisp_Object first = XCAR (face_ref);
3639 if (EQ (first, Qforeground_color)
3640 || EQ (first, Qbackground_color))
3642 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
3643 . COLOR). COLOR must be a string. */
3644 Lisp_Object color_name = XCDR (face_ref);
3645 Lisp_Object color = first;
3647 if (STRINGP (color_name))
3649 if (EQ (color, Qforeground_color))
3650 to[LFACE_FOREGROUND_INDEX] = color_name;
3651 else
3652 to[LFACE_BACKGROUND_INDEX] = color_name;
3654 else
3656 if (err_msgs)
3657 add_to_log ("Invalid face color", color_name, Qnil);
3658 ok = 0;
3661 else if (SYMBOLP (first)
3662 && *SDATA (SYMBOL_NAME (first)) == ':')
3664 /* Assume this is the property list form. */
3665 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
3667 Lisp_Object keyword = XCAR (face_ref);
3668 Lisp_Object value = XCAR (XCDR (face_ref));
3669 int err = 0;
3671 /* Specifying `unspecified' is a no-op. */
3672 if (EQ (value, Qunspecified))
3674 else if (EQ (keyword, QCfamily))
3676 if (STRINGP (value))
3677 to[LFACE_FAMILY_INDEX] = value;
3678 else
3679 err = 1;
3681 else if (EQ (keyword, QCheight))
3683 Lisp_Object new_height =
3684 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
3686 if (! NILP (new_height))
3687 to[LFACE_HEIGHT_INDEX] = new_height;
3688 else
3689 err = 1;
3691 else if (EQ (keyword, QCweight))
3693 if (SYMBOLP (value)
3694 && face_numeric_weight (value) >= 0)
3695 to[LFACE_WEIGHT_INDEX] = value;
3696 else
3697 err = 1;
3699 else if (EQ (keyword, QCslant))
3701 if (SYMBOLP (value)
3702 && face_numeric_slant (value) >= 0)
3703 to[LFACE_SLANT_INDEX] = value;
3704 else
3705 err = 1;
3707 else if (EQ (keyword, QCunderline))
3709 if (EQ (value, Qt)
3710 || NILP (value)
3711 || STRINGP (value))
3712 to[LFACE_UNDERLINE_INDEX] = value;
3713 else
3714 err = 1;
3716 else if (EQ (keyword, QCoverline))
3718 if (EQ (value, Qt)
3719 || NILP (value)
3720 || STRINGP (value))
3721 to[LFACE_OVERLINE_INDEX] = value;
3722 else
3723 err = 1;
3725 else if (EQ (keyword, QCstrike_through))
3727 if (EQ (value, Qt)
3728 || NILP (value)
3729 || STRINGP (value))
3730 to[LFACE_STRIKE_THROUGH_INDEX] = value;
3731 else
3732 err = 1;
3734 else if (EQ (keyword, QCbox))
3736 if (EQ (value, Qt))
3737 value = make_number (1);
3738 if (INTEGERP (value)
3739 || STRINGP (value)
3740 || CONSP (value)
3741 || NILP (value))
3742 to[LFACE_BOX_INDEX] = value;
3743 else
3744 err = 1;
3746 else if (EQ (keyword, QCinverse_video)
3747 || EQ (keyword, QCreverse_video))
3749 if (EQ (value, Qt) || NILP (value))
3750 to[LFACE_INVERSE_INDEX] = value;
3751 else
3752 err = 1;
3754 else if (EQ (keyword, QCforeground))
3756 if (STRINGP (value))
3757 to[LFACE_FOREGROUND_INDEX] = value;
3758 else
3759 err = 1;
3761 else if (EQ (keyword, QCbackground))
3763 if (STRINGP (value))
3764 to[LFACE_BACKGROUND_INDEX] = value;
3765 else
3766 err = 1;
3768 else if (EQ (keyword, QCstipple))
3770 #ifdef HAVE_X_WINDOWS
3771 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
3772 if (!NILP (pixmap_p))
3773 to[LFACE_STIPPLE_INDEX] = value;
3774 else
3775 err = 1;
3776 #endif
3778 else if (EQ (keyword, QCwidth))
3780 if (SYMBOLP (value)
3781 && face_numeric_swidth (value) >= 0)
3782 to[LFACE_SWIDTH_INDEX] = value;
3783 else
3784 err = 1;
3786 else if (EQ (keyword, QCinherit))
3788 /* This is not really very useful; it's just like a
3789 normal face reference. */
3790 if (! merge_face_ref (f, value, to,
3791 err_msgs, named_merge_points))
3792 err = 1;
3794 else
3795 err = 1;
3797 if (err)
3799 add_to_log ("Invalid face attribute %S %S", keyword, value);
3800 ok = 0;
3803 face_ref = XCDR (XCDR (face_ref));
3806 else
3808 /* This is a list of face refs. Those at the beginning of the
3809 list take precedence over what follows, so we have to merge
3810 from the end backwards. */
3811 Lisp_Object next = XCDR (face_ref);
3813 if (! NILP (next))
3814 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
3816 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
3817 ok = 0;
3820 else
3822 /* FACE_REF ought to be a face name. */
3823 ok = merge_named_face (f, face_ref, to, named_merge_points);
3824 if (!ok && err_msgs)
3825 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
3828 return ok;
3832 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
3833 Sinternal_make_lisp_face, 1, 2, 0,
3834 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
3835 If FACE was not known as a face before, create a new one.
3836 If optional argument FRAME is specified, make a frame-local face
3837 for that frame. Otherwise operate on the global face definition.
3838 Value is a vector of face attributes. */)
3839 (face, frame)
3840 Lisp_Object face, frame;
3842 Lisp_Object global_lface, lface;
3843 struct frame *f;
3844 int i;
3846 CHECK_SYMBOL (face);
3847 global_lface = lface_from_face_name (NULL, face, 0);
3849 if (!NILP (frame))
3851 CHECK_LIVE_FRAME (frame);
3852 f = XFRAME (frame);
3853 lface = lface_from_face_name (f, face, 0);
3855 else
3856 f = NULL, lface = Qnil;
3858 /* Add a global definition if there is none. */
3859 if (NILP (global_lface))
3861 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3862 Qunspecified);
3863 AREF (global_lface, 0) = Qface;
3864 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
3865 Vface_new_frame_defaults);
3867 /* Assign the new Lisp face a unique ID. The mapping from Lisp
3868 face id to Lisp face is given by the vector lface_id_to_name.
3869 The mapping from Lisp face to Lisp face id is given by the
3870 property `face' of the Lisp face name. */
3871 if (next_lface_id == lface_id_to_name_size)
3873 int new_size = max (50, 2 * lface_id_to_name_size);
3874 int sz = new_size * sizeof *lface_id_to_name;
3875 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
3876 lface_id_to_name_size = new_size;
3879 lface_id_to_name[next_lface_id] = face;
3880 Fput (face, Qface, make_number (next_lface_id));
3881 ++next_lface_id;
3883 else if (f == NULL)
3884 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3885 AREF (global_lface, i) = Qunspecified;
3887 /* Add a frame-local definition. */
3888 if (f)
3890 if (NILP (lface))
3892 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3893 Qunspecified);
3894 AREF (lface, 0) = Qface;
3895 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
3897 else
3898 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3899 AREF (lface, i) = Qunspecified;
3901 else
3902 lface = global_lface;
3904 /* Changing a named face means that all realized faces depending on
3905 that face are invalid. Since we cannot tell which realized faces
3906 depend on the face, make sure they are all removed. This is done
3907 by incrementing face_change_count. The next call to
3908 init_iterator will then free realized faces. */
3909 if (NILP (Fget (face, Qface_no_inherit)))
3911 ++face_change_count;
3912 ++windows_or_buffers_changed;
3915 xassert (LFACEP (lface));
3916 check_lface (lface);
3917 return lface;
3921 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
3922 Sinternal_lisp_face_p, 1, 2, 0,
3923 doc: /* Return non-nil if FACE names a face.
3924 If optional second argument FRAME is non-nil, check for the
3925 existence of a frame-local face with name FACE on that frame.
3926 Otherwise check for the existence of a global face. */)
3927 (face, frame)
3928 Lisp_Object face, frame;
3930 Lisp_Object lface;
3932 if (!NILP (frame))
3934 CHECK_LIVE_FRAME (frame);
3935 lface = lface_from_face_name (XFRAME (frame), face, 0);
3937 else
3938 lface = lface_from_face_name (NULL, face, 0);
3940 return lface;
3944 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
3945 Sinternal_copy_lisp_face, 4, 4, 0,
3946 doc: /* Copy face FROM to TO.
3947 If FRAME is t, copy the global face definition of FROM.
3948 Otherwise, copy the frame-local definition of FROM on FRAME.
3949 If NEW-FRAME is a frame, copy that data into the frame-local
3950 definition of TO on NEW-FRAME. If NEW-FRAME is nil.
3951 FRAME controls where the data is copied to.
3953 The value is TO. */)
3954 (from, to, frame, new_frame)
3955 Lisp_Object from, to, frame, new_frame;
3957 Lisp_Object lface, copy;
3959 CHECK_SYMBOL (from);
3960 CHECK_SYMBOL (to);
3962 if (EQ (frame, Qt))
3964 /* Copy global definition of FROM. We don't make copies of
3965 strings etc. because 20.2 didn't do it either. */
3966 lface = lface_from_face_name (NULL, from, 1);
3967 copy = Finternal_make_lisp_face (to, Qnil);
3969 else
3971 /* Copy frame-local definition of FROM. */
3972 if (NILP (new_frame))
3973 new_frame = frame;
3974 CHECK_LIVE_FRAME (frame);
3975 CHECK_LIVE_FRAME (new_frame);
3976 lface = lface_from_face_name (XFRAME (frame), from, 1);
3977 copy = Finternal_make_lisp_face (to, new_frame);
3980 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3981 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
3983 /* Changing a named face means that all realized faces depending on
3984 that face are invalid. Since we cannot tell which realized faces
3985 depend on the face, make sure they are all removed. This is done
3986 by incrementing face_change_count. The next call to
3987 init_iterator will then free realized faces. */
3988 if (NILP (Fget (to, Qface_no_inherit)))
3990 ++face_change_count;
3991 ++windows_or_buffers_changed;
3994 return to;
3998 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3999 Sinternal_set_lisp_face_attribute, 3, 4, 0,
4000 doc: /* Set attribute ATTR of FACE to VALUE.
4001 FRAME being a frame means change the face on that frame.
4002 FRAME nil means change the face of the selected frame.
4003 FRAME t means change the default for new frames.
4004 FRAME 0 means change the face on all frames, and change the default
4005 for new frames. */)
4006 (face, attr, value, frame)
4007 Lisp_Object face, attr, value, frame;
4009 Lisp_Object lface;
4010 Lisp_Object old_value = Qnil;
4011 /* Set 1 if ATTR is QCfont. */
4012 int font_attr_p = 0;
4013 /* Set 1 if ATTR is one of font-related attributes other than QCfont. */
4014 int font_related_attr_p = 0;
4016 CHECK_SYMBOL (face);
4017 CHECK_SYMBOL (attr);
4019 face = resolve_face_name (face, 1);
4021 /* If FRAME is 0, change face on all frames, and change the
4022 default for new frames. */
4023 if (INTEGERP (frame) && XINT (frame) == 0)
4025 Lisp_Object tail;
4026 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
4027 FOR_EACH_FRAME (tail, frame)
4028 Finternal_set_lisp_face_attribute (face, attr, value, frame);
4029 return face;
4032 /* Set lface to the Lisp attribute vector of FACE. */
4033 if (EQ (frame, Qt))
4034 lface = lface_from_face_name (NULL, face, 1);
4035 else
4037 if (NILP (frame))
4038 frame = selected_frame;
4040 CHECK_LIVE_FRAME (frame);
4041 lface = lface_from_face_name (XFRAME (frame), face, 0);
4043 /* If a frame-local face doesn't exist yet, create one. */
4044 if (NILP (lface))
4045 lface = Finternal_make_lisp_face (face, frame);
4048 if (EQ (attr, QCfamily))
4050 if (!UNSPECIFIEDP (value))
4052 CHECK_STRING (value);
4053 if (SCHARS (value) == 0)
4054 signal_error ("Invalid face family", value);
4056 old_value = LFACE_FAMILY (lface);
4057 LFACE_FAMILY (lface) = value;
4058 font_related_attr_p = 1;
4060 else if (EQ (attr, QCheight))
4062 if (!UNSPECIFIEDP (value))
4064 Lisp_Object test;
4066 test = (EQ (face, Qdefault)
4067 ? value
4068 /* The default face must have an absolute size,
4069 otherwise, we do a test merge with a random
4070 height to see if VALUE's ok. */
4071 : merge_face_heights (value, make_number (10), Qnil));
4073 if (!INTEGERP (test) || XINT (test) <= 0)
4074 signal_error ("Invalid face height", value);
4077 old_value = LFACE_HEIGHT (lface);
4078 LFACE_HEIGHT (lface) = value;
4079 font_related_attr_p = 1;
4081 else if (EQ (attr, QCweight))
4083 if (!UNSPECIFIEDP (value))
4085 CHECK_SYMBOL (value);
4086 if (face_numeric_weight (value) < 0)
4087 signal_error ("Invalid face weight", value);
4089 old_value = LFACE_WEIGHT (lface);
4090 LFACE_WEIGHT (lface) = value;
4091 font_related_attr_p = 1;
4093 else if (EQ (attr, QCslant))
4095 if (!UNSPECIFIEDP (value))
4097 CHECK_SYMBOL (value);
4098 if (face_numeric_slant (value) < 0)
4099 signal_error ("Invalid face slant", value);
4101 old_value = LFACE_SLANT (lface);
4102 LFACE_SLANT (lface) = value;
4103 font_related_attr_p = 1;
4105 else if (EQ (attr, QCunderline))
4107 if (!UNSPECIFIEDP (value))
4108 if ((SYMBOLP (value)
4109 && !EQ (value, Qt)
4110 && !EQ (value, Qnil))
4111 /* Underline color. */
4112 || (STRINGP (value)
4113 && SCHARS (value) == 0))
4114 signal_error ("Invalid face underline", value);
4116 old_value = LFACE_UNDERLINE (lface);
4117 LFACE_UNDERLINE (lface) = value;
4119 else if (EQ (attr, QCoverline))
4121 if (!UNSPECIFIEDP (value))
4122 if ((SYMBOLP (value)
4123 && !EQ (value, Qt)
4124 && !EQ (value, Qnil))
4125 /* Overline color. */
4126 || (STRINGP (value)
4127 && SCHARS (value) == 0))
4128 signal_error ("Invalid face overline", value);
4130 old_value = LFACE_OVERLINE (lface);
4131 LFACE_OVERLINE (lface) = value;
4133 else if (EQ (attr, QCstrike_through))
4135 if (!UNSPECIFIEDP (value))
4136 if ((SYMBOLP (value)
4137 && !EQ (value, Qt)
4138 && !EQ (value, Qnil))
4139 /* Strike-through color. */
4140 || (STRINGP (value)
4141 && SCHARS (value) == 0))
4142 signal_error ("Invalid face strike-through", value);
4144 old_value = LFACE_STRIKE_THROUGH (lface);
4145 LFACE_STRIKE_THROUGH (lface) = value;
4147 else if (EQ (attr, QCbox))
4149 int valid_p;
4151 /* Allow t meaning a simple box of width 1 in foreground color
4152 of the face. */
4153 if (EQ (value, Qt))
4154 value = make_number (1);
4156 if (UNSPECIFIEDP (value))
4157 valid_p = 1;
4158 else if (NILP (value))
4159 valid_p = 1;
4160 else if (INTEGERP (value))
4161 valid_p = XINT (value) != 0;
4162 else if (STRINGP (value))
4163 valid_p = SCHARS (value) > 0;
4164 else if (CONSP (value))
4166 Lisp_Object tem;
4168 tem = value;
4169 while (CONSP (tem))
4171 Lisp_Object k, v;
4173 k = XCAR (tem);
4174 tem = XCDR (tem);
4175 if (!CONSP (tem))
4176 break;
4177 v = XCAR (tem);
4178 tem = XCDR (tem);
4180 if (EQ (k, QCline_width))
4182 if (!INTEGERP (v) || XINT (v) == 0)
4183 break;
4185 else if (EQ (k, QCcolor))
4187 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
4188 break;
4190 else if (EQ (k, QCstyle))
4192 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
4193 break;
4195 else
4196 break;
4199 valid_p = NILP (tem);
4201 else
4202 valid_p = 0;
4204 if (!valid_p)
4205 signal_error ("Invalid face box", value);
4207 old_value = LFACE_BOX (lface);
4208 LFACE_BOX (lface) = value;
4210 else if (EQ (attr, QCinverse_video)
4211 || EQ (attr, QCreverse_video))
4213 if (!UNSPECIFIEDP (value))
4215 CHECK_SYMBOL (value);
4216 if (!EQ (value, Qt) && !NILP (value))
4217 signal_error ("Invalid inverse-video face attribute value", value);
4219 old_value = LFACE_INVERSE (lface);
4220 LFACE_INVERSE (lface) = value;
4222 else if (EQ (attr, QCforeground))
4224 if (!UNSPECIFIEDP (value))
4226 /* Don't check for valid color names here because it depends
4227 on the frame (display) whether the color will be valid
4228 when the face is realized. */
4229 CHECK_STRING (value);
4230 if (SCHARS (value) == 0)
4231 signal_error ("Empty foreground color value", value);
4233 old_value = LFACE_FOREGROUND (lface);
4234 LFACE_FOREGROUND (lface) = value;
4236 else if (EQ (attr, QCbackground))
4238 if (!UNSPECIFIEDP (value))
4240 /* Don't check for valid color names here because it depends
4241 on the frame (display) whether the color will be valid
4242 when the face is realized. */
4243 CHECK_STRING (value);
4244 if (SCHARS (value) == 0)
4245 signal_error ("Empty background color value", value);
4247 old_value = LFACE_BACKGROUND (lface);
4248 LFACE_BACKGROUND (lface) = value;
4250 else if (EQ (attr, QCstipple))
4252 #ifdef HAVE_X_WINDOWS
4253 if (!UNSPECIFIEDP (value)
4254 && !NILP (value)
4255 && NILP (Fbitmap_spec_p (value)))
4256 signal_error ("Invalid stipple attribute", value);
4257 old_value = LFACE_STIPPLE (lface);
4258 LFACE_STIPPLE (lface) = value;
4259 #endif /* HAVE_X_WINDOWS */
4261 else if (EQ (attr, QCwidth))
4263 if (!UNSPECIFIEDP (value))
4265 CHECK_SYMBOL (value);
4266 if (face_numeric_swidth (value) < 0)
4267 signal_error ("Invalid face width", value);
4269 old_value = LFACE_SWIDTH (lface);
4270 LFACE_SWIDTH (lface) = value;
4271 font_related_attr_p = 1;
4273 else if (EQ (attr, QCfont))
4275 #ifdef HAVE_WINDOW_SYSTEM
4276 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
4278 /* Set font-related attributes of the Lisp face from an XLFD
4279 font name. */
4280 struct frame *f;
4281 Lisp_Object tmp;
4283 if (EQ (frame, Qt))
4284 f = SELECTED_FRAME ();
4285 else
4286 f = check_x_frame (frame);
4288 if (!UNSPECIFIEDP (value))
4290 CHECK_STRING (value);
4292 /* VALUE may be a fontset name or an alias of fontset. In
4293 such a case, use the base fontset name. */
4294 tmp = Fquery_fontset (value, Qnil);
4295 if (!NILP (tmp))
4296 value = tmp;
4298 if (!set_lface_from_font_name (f, lface, value, 1, 1))
4299 signal_error ("Invalid font or fontset name", value);
4302 font_attr_p = 1;
4304 #endif /* HAVE_WINDOW_SYSTEM */
4306 else if (EQ (attr, QCinherit))
4308 Lisp_Object tail;
4309 if (SYMBOLP (value))
4310 tail = Qnil;
4311 else
4312 for (tail = value; CONSP (tail); tail = XCDR (tail))
4313 if (!SYMBOLP (XCAR (tail)))
4314 break;
4315 if (NILP (tail))
4316 LFACE_INHERIT (lface) = value;
4317 else
4318 signal_error ("Invalid face inheritance", value);
4320 else if (EQ (attr, QCbold))
4322 old_value = LFACE_WEIGHT (lface);
4323 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
4324 font_related_attr_p = 1;
4326 else if (EQ (attr, QCitalic))
4328 old_value = LFACE_SLANT (lface);
4329 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
4330 font_related_attr_p = 1;
4332 else
4333 signal_error ("Invalid face attribute name", attr);
4335 if (font_related_attr_p
4336 && !UNSPECIFIEDP (value))
4337 /* If a font-related attribute other than QCfont is specified, the
4338 original `font' attribute nor that of default face is useless
4339 to determine a new font. Thus, we set it to nil so that font
4340 selection mechanism doesn't use it. */
4341 LFACE_FONT (lface) = Qnil;
4343 /* Changing a named face means that all realized faces depending on
4344 that face are invalid. Since we cannot tell which realized faces
4345 depend on the face, make sure they are all removed. This is done
4346 by incrementing face_change_count. The next call to
4347 init_iterator will then free realized faces. */
4348 if (!EQ (frame, Qt)
4349 && NILP (Fget (face, Qface_no_inherit))
4350 && (EQ (attr, QCfont)
4351 || NILP (Fequal (old_value, value))))
4353 ++face_change_count;
4354 ++windows_or_buffers_changed;
4357 if (!UNSPECIFIEDP (value)
4358 && NILP (Fequal (old_value, value)))
4360 Lisp_Object param;
4362 param = Qnil;
4364 if (EQ (face, Qdefault))
4366 #ifdef HAVE_WINDOW_SYSTEM
4367 /* Changed font-related attributes of the `default' face are
4368 reflected in changed `font' frame parameters. */
4369 if (FRAMEP (frame)
4370 && (font_related_attr_p || font_attr_p)
4371 && lface_fully_specified_p (XVECTOR (lface)->contents))
4372 set_font_frame_param (frame, lface);
4373 else
4374 #endif /* HAVE_WINDOW_SYSTEM */
4376 if (EQ (attr, QCforeground))
4377 param = Qforeground_color;
4378 else if (EQ (attr, QCbackground))
4379 param = Qbackground_color;
4381 #ifdef HAVE_WINDOW_SYSTEM
4382 #ifndef WINDOWSNT
4383 else if (EQ (face, Qscroll_bar))
4385 /* Changing the colors of `scroll-bar' sets frame parameters
4386 `scroll-bar-foreground' and `scroll-bar-background'. */
4387 if (EQ (attr, QCforeground))
4388 param = Qscroll_bar_foreground;
4389 else if (EQ (attr, QCbackground))
4390 param = Qscroll_bar_background;
4392 #endif /* not WINDOWSNT */
4393 else if (EQ (face, Qborder))
4395 /* Changing background color of `border' sets frame parameter
4396 `border-color'. */
4397 if (EQ (attr, QCbackground))
4398 param = Qborder_color;
4400 else if (EQ (face, Qcursor))
4402 /* Changing background color of `cursor' sets frame parameter
4403 `cursor-color'. */
4404 if (EQ (attr, QCbackground))
4405 param = Qcursor_color;
4407 else if (EQ (face, Qmouse))
4409 /* Changing background color of `mouse' sets frame parameter
4410 `mouse-color'. */
4411 if (EQ (attr, QCbackground))
4412 param = Qmouse_color;
4414 #endif /* HAVE_WINDOW_SYSTEM */
4415 else if (EQ (face, Qmenu))
4417 /* Indicate that we have to update the menu bar when
4418 realizing faces on FRAME. FRAME t change the
4419 default for new frames. We do this by setting
4420 setting the flag in new face caches */
4421 if (FRAMEP (frame))
4423 struct frame *f = XFRAME (frame);
4424 if (FRAME_FACE_CACHE (f) == NULL)
4425 FRAME_FACE_CACHE (f) = make_face_cache (f);
4426 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
4428 else
4429 menu_face_changed_default = 1;
4432 if (!NILP (param))
4434 if (EQ (frame, Qt))
4435 /* Update `default-frame-alist', which is used for new frames. */
4437 store_in_alist (&Vdefault_frame_alist, param, value);
4439 else
4440 /* Update the current frame's parameters. */
4442 Lisp_Object cons;
4443 cons = XCAR (Vparam_value_alist);
4444 XSETCAR (cons, param);
4445 XSETCDR (cons, value);
4446 Fmodify_frame_parameters (frame, Vparam_value_alist);
4451 return face;
4455 #ifdef HAVE_WINDOW_SYSTEM
4457 /* Set the `font' frame parameter of FRAME determined from `default'
4458 face attributes LFACE. If a face or fontset name is explicitely
4459 specfied in LFACE, use it as is. Otherwise, determine a font name
4460 from the other font-related atrributes of LFACE. In that case, if
4461 there's no matching font, signals an error. */
4463 static void
4464 set_font_frame_param (frame, lface)
4465 Lisp_Object frame, lface;
4467 struct frame *f = XFRAME (frame);
4469 if (FRAME_WINDOW_P (f))
4471 Lisp_Object font_name;
4472 char *font;
4474 if (STRINGP (LFACE_FONT (lface)))
4475 font_name = LFACE_FONT (lface);
4476 else
4478 /* Choose a font name that reflects LFACE's attributes and has
4479 the registry and encoding pattern specified in the default
4480 fontset (3rd arg: -1) for ASCII characters (4th arg: 0). */
4481 font = choose_face_font (f, XVECTOR (lface)->contents, -1, 0, 0);
4482 if (!font)
4483 error ("No font matches the specified attribute");
4484 font_name = build_string (font);
4485 xfree (font);
4488 f->default_face_done_p = 0;
4489 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font_name), Qnil));
4494 /* Update the corresponding face when frame parameter PARAM on frame F
4495 has been assigned the value NEW_VALUE. */
4497 void
4498 update_face_from_frame_parameter (f, param, new_value)
4499 struct frame *f;
4500 Lisp_Object param, new_value;
4502 Lisp_Object face = Qnil;
4503 Lisp_Object lface;
4505 /* If there are no faces yet, give up. This is the case when called
4506 from Fx_create_frame, and we do the necessary things later in
4507 face-set-after-frame-defaults. */
4508 if (NILP (f->face_alist))
4509 return;
4511 if (EQ (param, Qforeground_color))
4513 face = Qdefault;
4514 lface = lface_from_face_name (f, face, 1);
4515 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
4516 ? new_value : Qunspecified);
4517 realize_basic_faces (f);
4519 else if (EQ (param, Qbackground_color))
4521 Lisp_Object frame;
4523 /* Changing the background color might change the background
4524 mode, so that we have to load new defface specs.
4525 Call frame-set-background-mode to do that. */
4526 XSETFRAME (frame, f);
4527 call1 (Qframe_set_background_mode, frame);
4529 face = Qdefault;
4530 lface = lface_from_face_name (f, face, 1);
4531 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4532 ? new_value : Qunspecified);
4533 realize_basic_faces (f);
4535 else if (EQ (param, Qborder_color))
4537 face = Qborder;
4538 lface = lface_from_face_name (f, face, 1);
4539 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4540 ? new_value : Qunspecified);
4542 else if (EQ (param, Qcursor_color))
4544 face = Qcursor;
4545 lface = lface_from_face_name (f, face, 1);
4546 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4547 ? new_value : Qunspecified);
4549 else if (EQ (param, Qmouse_color))
4551 face = Qmouse;
4552 lface = lface_from_face_name (f, face, 1);
4553 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4554 ? new_value : Qunspecified);
4557 /* Changing a named face means that all realized faces depending on
4558 that face are invalid. Since we cannot tell which realized faces
4559 depend on the face, make sure they are all removed. This is done
4560 by incrementing face_change_count. The next call to
4561 init_iterator will then free realized faces. */
4562 if (!NILP (face)
4563 && NILP (Fget (face, Qface_no_inherit)))
4565 ++face_change_count;
4566 ++windows_or_buffers_changed;
4571 /* Get the value of X resource RESOURCE, class CLASS for the display
4572 of frame FRAME. This is here because ordinary `x-get-resource'
4573 doesn't take a frame argument. */
4575 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
4576 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
4577 (resource, class, frame)
4578 Lisp_Object resource, class, frame;
4580 Lisp_Object value = Qnil;
4581 CHECK_STRING (resource);
4582 CHECK_STRING (class);
4583 CHECK_LIVE_FRAME (frame);
4584 BLOCK_INPUT;
4585 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
4586 resource, class, Qnil, Qnil);
4587 UNBLOCK_INPUT;
4588 return value;
4592 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
4593 If VALUE is "on" or "true", return t. If VALUE is "off" or
4594 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
4595 error; if SIGNAL_P is zero, return 0. */
4597 static Lisp_Object
4598 face_boolean_x_resource_value (value, signal_p)
4599 Lisp_Object value;
4600 int signal_p;
4602 Lisp_Object result = make_number (0);
4604 xassert (STRINGP (value));
4606 if (xstricmp (SDATA (value), "on") == 0
4607 || xstricmp (SDATA (value), "true") == 0)
4608 result = Qt;
4609 else if (xstricmp (SDATA (value), "off") == 0
4610 || xstricmp (SDATA (value), "false") == 0)
4611 result = Qnil;
4612 else if (xstricmp (SDATA (value), "unspecified") == 0)
4613 result = Qunspecified;
4614 else if (signal_p)
4615 signal_error ("Invalid face attribute value from X resource", value);
4617 return result;
4621 DEFUN ("internal-set-lisp-face-attribute-from-resource",
4622 Finternal_set_lisp_face_attribute_from_resource,
4623 Sinternal_set_lisp_face_attribute_from_resource,
4624 3, 4, 0, doc: /* */)
4625 (face, attr, value, frame)
4626 Lisp_Object face, attr, value, frame;
4628 CHECK_SYMBOL (face);
4629 CHECK_SYMBOL (attr);
4630 CHECK_STRING (value);
4632 if (xstricmp (SDATA (value), "unspecified") == 0)
4633 value = Qunspecified;
4634 else if (EQ (attr, QCheight))
4636 value = Fstring_to_number (value, make_number (10));
4637 if (XINT (value) <= 0)
4638 signal_error ("Invalid face height from X resource", value);
4640 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
4641 value = face_boolean_x_resource_value (value, 1);
4642 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
4643 value = intern (SDATA (value));
4644 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
4645 value = face_boolean_x_resource_value (value, 1);
4646 else if (EQ (attr, QCunderline)
4647 || EQ (attr, QCoverline)
4648 || EQ (attr, QCstrike_through))
4650 Lisp_Object boolean_value;
4652 /* If the result of face_boolean_x_resource_value is t or nil,
4653 VALUE does NOT specify a color. */
4654 boolean_value = face_boolean_x_resource_value (value, 0);
4655 if (SYMBOLP (boolean_value))
4656 value = boolean_value;
4658 else if (EQ (attr, QCbox))
4659 value = Fcar (Fread_from_string (value, Qnil, Qnil));
4661 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
4664 #endif /* HAVE_WINDOW_SYSTEM */
4667 /***********************************************************************
4668 Menu face
4669 ***********************************************************************/
4671 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
4673 /* Make menus on frame F appear as specified by the `menu' face. */
4675 static void
4676 x_update_menu_appearance (f)
4677 struct frame *f;
4679 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4680 XrmDatabase rdb;
4682 if (dpyinfo
4683 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
4684 rdb != NULL))
4686 char line[512];
4687 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
4688 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
4689 const char *myname = SDATA (Vx_resource_name);
4690 int changed_p = 0;
4691 #ifdef USE_MOTIF
4692 const char *popup_path = "popup_menu";
4693 #else
4694 const char *popup_path = "menu.popup";
4695 #endif
4697 if (STRINGP (LFACE_FOREGROUND (lface)))
4699 sprintf (line, "%s.%s*foreground: %s",
4700 myname, popup_path,
4701 SDATA (LFACE_FOREGROUND (lface)));
4702 XrmPutLineResource (&rdb, line);
4703 sprintf (line, "%s.pane.menubar*foreground: %s",
4704 myname, SDATA (LFACE_FOREGROUND (lface)));
4705 XrmPutLineResource (&rdb, line);
4706 changed_p = 1;
4709 if (STRINGP (LFACE_BACKGROUND (lface)))
4711 sprintf (line, "%s.%s*background: %s",
4712 myname, popup_path,
4713 SDATA (LFACE_BACKGROUND (lface)));
4714 XrmPutLineResource (&rdb, line);
4715 sprintf (line, "%s.pane.menubar*background: %s",
4716 myname, SDATA (LFACE_BACKGROUND (lface)));
4717 XrmPutLineResource (&rdb, line);
4718 changed_p = 1;
4721 if (face->font_name
4722 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
4723 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
4724 || !UNSPECIFIEDP (LFACE_AVGWIDTH (lface))
4725 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
4726 || !UNSPECIFIEDP (LFACE_SLANT (lface))
4727 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
4729 #ifdef USE_MOTIF
4730 const char *suffix = "List";
4731 Bool motif = True;
4732 #else
4733 #if defined HAVE_X_I18N
4735 const char *suffix = "Set";
4736 #else
4737 const char *suffix = "";
4738 #endif
4739 Bool motif = False;
4740 #endif
4741 #if defined HAVE_X_I18N
4742 extern char *xic_create_fontsetname
4743 P_ ((char *base_fontname, Bool motif));
4744 char *fontsetname = xic_create_fontsetname (face->font_name, motif);
4745 #else
4746 char *fontsetname = face->font_name;
4747 #endif
4748 sprintf (line, "%s.pane.menubar*font%s: %s",
4749 myname, suffix, fontsetname);
4750 XrmPutLineResource (&rdb, line);
4751 sprintf (line, "%s.%s*font%s: %s",
4752 myname, popup_path, suffix, fontsetname);
4753 XrmPutLineResource (&rdb, line);
4754 changed_p = 1;
4755 if (fontsetname != face->font_name)
4756 xfree (fontsetname);
4759 if (changed_p && f->output_data.x->menubar_widget)
4760 free_frame_menubar (f);
4764 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
4767 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
4768 Sface_attribute_relative_p,
4769 2, 2, 0,
4770 doc: /* Return non-nil if face ATTRIBUTE VALUE is relative. */)
4771 (attribute, value)
4772 Lisp_Object attribute, value;
4774 if (EQ (value, Qunspecified))
4775 return Qt;
4776 else if (EQ (attribute, QCheight))
4777 return INTEGERP (value) ? Qnil : Qt;
4778 else
4779 return Qnil;
4782 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
4783 3, 3, 0,
4784 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
4785 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
4786 the result will be absolute, otherwise it will be relative. */)
4787 (attribute, value1, value2)
4788 Lisp_Object attribute, value1, value2;
4790 if (EQ (value1, Qunspecified))
4791 return value2;
4792 else if (EQ (attribute, QCheight))
4793 return merge_face_heights (value1, value2, value1);
4794 else
4795 return value1;
4799 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
4800 Sinternal_get_lisp_face_attribute,
4801 2, 3, 0,
4802 doc: /* Return face attribute KEYWORD of face SYMBOL.
4803 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
4804 face attribute name, signal an error.
4805 If the optional argument FRAME is given, report on face SYMBOL in that
4806 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
4807 frames). If FRAME is omitted or nil, use the selected frame. */)
4808 (symbol, keyword, frame)
4809 Lisp_Object symbol, keyword, frame;
4811 Lisp_Object lface, value = Qnil;
4813 CHECK_SYMBOL (symbol);
4814 CHECK_SYMBOL (keyword);
4816 if (EQ (frame, Qt))
4817 lface = lface_from_face_name (NULL, symbol, 1);
4818 else
4820 if (NILP (frame))
4821 frame = selected_frame;
4822 CHECK_LIVE_FRAME (frame);
4823 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
4826 if (EQ (keyword, QCfamily))
4827 value = LFACE_FAMILY (lface);
4828 else if (EQ (keyword, QCheight))
4829 value = LFACE_HEIGHT (lface);
4830 else if (EQ (keyword, QCweight))
4831 value = LFACE_WEIGHT (lface);
4832 else if (EQ (keyword, QCslant))
4833 value = LFACE_SLANT (lface);
4834 else if (EQ (keyword, QCunderline))
4835 value = LFACE_UNDERLINE (lface);
4836 else if (EQ (keyword, QCoverline))
4837 value = LFACE_OVERLINE (lface);
4838 else if (EQ (keyword, QCstrike_through))
4839 value = LFACE_STRIKE_THROUGH (lface);
4840 else if (EQ (keyword, QCbox))
4841 value = LFACE_BOX (lface);
4842 else if (EQ (keyword, QCinverse_video)
4843 || EQ (keyword, QCreverse_video))
4844 value = LFACE_INVERSE (lface);
4845 else if (EQ (keyword, QCforeground))
4846 value = LFACE_FOREGROUND (lface);
4847 else if (EQ (keyword, QCbackground))
4848 value = LFACE_BACKGROUND (lface);
4849 else if (EQ (keyword, QCstipple))
4850 value = LFACE_STIPPLE (lface);
4851 else if (EQ (keyword, QCwidth))
4852 value = LFACE_SWIDTH (lface);
4853 else if (EQ (keyword, QCinherit))
4854 value = LFACE_INHERIT (lface);
4855 else if (EQ (keyword, QCfont))
4856 value = LFACE_FONT (lface);
4857 else
4858 signal_error ("Invalid face attribute name", keyword);
4860 return value;
4864 DEFUN ("internal-lisp-face-attribute-values",
4865 Finternal_lisp_face_attribute_values,
4866 Sinternal_lisp_face_attribute_values, 1, 1, 0,
4867 doc: /* Return a list of valid discrete values for face attribute ATTR.
4868 Value is nil if ATTR doesn't have a discrete set of valid values. */)
4869 (attr)
4870 Lisp_Object attr;
4872 Lisp_Object result = Qnil;
4874 CHECK_SYMBOL (attr);
4876 if (EQ (attr, QCweight)
4877 || EQ (attr, QCslant)
4878 || EQ (attr, QCwidth))
4880 /* Extract permissible symbols from tables. */
4881 struct table_entry *table;
4882 int i, dim;
4884 if (EQ (attr, QCweight))
4885 table = weight_table, dim = DIM (weight_table);
4886 else if (EQ (attr, QCslant))
4887 table = slant_table, dim = DIM (slant_table);
4888 else
4889 table = swidth_table, dim = DIM (swidth_table);
4891 for (i = 0; i < dim; ++i)
4893 Lisp_Object symbol = *table[i].symbol;
4894 Lisp_Object tail = result;
4896 while (!NILP (tail)
4897 && !EQ (XCAR (tail), symbol))
4898 tail = XCDR (tail);
4900 if (NILP (tail))
4901 result = Fcons (symbol, result);
4904 else if (EQ (attr, QCunderline))
4905 result = Fcons (Qt, Fcons (Qnil, Qnil));
4906 else if (EQ (attr, QCoverline))
4907 result = Fcons (Qt, Fcons (Qnil, Qnil));
4908 else if (EQ (attr, QCstrike_through))
4909 result = Fcons (Qt, Fcons (Qnil, Qnil));
4910 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
4911 result = Fcons (Qt, Fcons (Qnil, Qnil));
4913 return result;
4917 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
4918 Sinternal_merge_in_global_face, 2, 2, 0,
4919 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
4920 Default face attributes override any local face attributes. */)
4921 (face, frame)
4922 Lisp_Object face, frame;
4924 int i;
4925 Lisp_Object global_lface, local_lface, *gvec, *lvec;
4927 CHECK_LIVE_FRAME (frame);
4928 global_lface = lface_from_face_name (NULL, face, 1);
4929 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
4930 if (NILP (local_lface))
4931 local_lface = Finternal_make_lisp_face (face, frame);
4933 /* Make every specified global attribute override the local one.
4934 BEWARE!! This is only used from `face-set-after-frame-default' where
4935 the local frame is defined from default specs in `face-defface-spec'
4936 and those should be overridden by global settings. Hence the strange
4937 "global before local" priority. */
4938 lvec = XVECTOR (local_lface)->contents;
4939 gvec = XVECTOR (global_lface)->contents;
4940 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4941 if (! UNSPECIFIEDP (gvec[i]))
4942 lvec[i] = gvec[i];
4944 return Qnil;
4948 /* The following function is implemented for compatibility with 20.2.
4949 The function is used in x-resolve-fonts when it is asked to
4950 return fonts with the same size as the font of a face. This is
4951 done in fontset.el. */
4953 DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
4954 doc: /* Return the font name of face FACE, or nil if it is unspecified.
4955 If the optional argument FRAME is given, report on face FACE in that frame.
4956 If FRAME is t, report on the defaults for face FACE (for new frames).
4957 The font default for a face is either nil, or a list
4958 of the form (bold), (italic) or (bold italic).
4959 If FRAME is omitted or nil, use the selected frame. */)
4960 (face, frame)
4961 Lisp_Object face, frame;
4963 if (EQ (frame, Qt))
4965 Lisp_Object result = Qnil;
4966 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
4968 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
4969 && !EQ (LFACE_WEIGHT (lface), Qnormal))
4970 result = Fcons (Qbold, result);
4972 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
4973 && !EQ (LFACE_SLANT (lface), Qnormal))
4974 result = Fcons (Qitalic, result);
4976 return result;
4978 else
4980 struct frame *f = frame_or_selected_frame (frame, 1);
4981 int face_id = lookup_named_face (f, face, 0, 1);
4982 struct face *face = FACE_FROM_ID (f, face_id);
4983 return face ? build_string (face->font_name) : Qnil;
4988 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
4989 all attributes are `equal'. Tries to be fast because this function
4990 is called quite often. */
4992 static INLINE int
4993 face_attr_equal_p (v1, v2)
4994 Lisp_Object v1, v2;
4996 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
4997 and the other is specified. */
4998 if (XTYPE (v1) != XTYPE (v2))
4999 return 0;
5001 if (EQ (v1, v2))
5002 return 1;
5004 switch (XTYPE (v1))
5006 case Lisp_String:
5007 if (SBYTES (v1) != SBYTES (v2))
5008 return 0;
5010 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
5012 case Lisp_Int:
5013 case Lisp_Symbol:
5014 return 0;
5016 default:
5017 return !NILP (Fequal (v1, v2));
5022 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
5023 all attributes are `equal'. Tries to be fast because this function
5024 is called quite often. */
5026 static INLINE int
5027 lface_equal_p (v1, v2)
5028 Lisp_Object *v1, *v2;
5030 int i, equal_p = 1;
5032 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
5033 equal_p = face_attr_equal_p (v1[i], v2[i]);
5035 return equal_p;
5039 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
5040 Sinternal_lisp_face_equal_p, 2, 3, 0,
5041 doc: /* True if FACE1 and FACE2 are equal.
5042 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
5043 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
5044 If FRAME is omitted or nil, use the selected frame. */)
5045 (face1, face2, frame)
5046 Lisp_Object face1, face2, frame;
5048 int equal_p;
5049 struct frame *f;
5050 Lisp_Object lface1, lface2;
5052 if (EQ (frame, Qt))
5053 f = NULL;
5054 else
5055 /* Don't use check_x_frame here because this function is called
5056 before X frames exist. At that time, if FRAME is nil,
5057 selected_frame will be used which is the frame dumped with
5058 Emacs. That frame is not an X frame. */
5059 f = frame_or_selected_frame (frame, 2);
5061 lface1 = lface_from_face_name (f, face1, 1);
5062 lface2 = lface_from_face_name (f, face2, 1);
5063 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
5064 XVECTOR (lface2)->contents);
5065 return equal_p ? Qt : Qnil;
5069 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
5070 Sinternal_lisp_face_empty_p, 1, 2, 0,
5071 doc: /* True if FACE has no attribute specified.
5072 If the optional argument FRAME is given, report on face FACE in that frame.
5073 If FRAME is t, report on the defaults for face FACE (for new frames).
5074 If FRAME is omitted or nil, use the selected frame. */)
5075 (face, frame)
5076 Lisp_Object face, frame;
5078 struct frame *f;
5079 Lisp_Object lface;
5080 int i;
5082 if (NILP (frame))
5083 frame = selected_frame;
5084 CHECK_LIVE_FRAME (frame);
5085 f = XFRAME (frame);
5087 if (EQ (frame, Qt))
5088 lface = lface_from_face_name (NULL, face, 1);
5089 else
5090 lface = lface_from_face_name (f, face, 1);
5092 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
5093 if (!UNSPECIFIEDP (AREF (lface, i)))
5094 break;
5096 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
5100 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
5101 0, 1, 0,
5102 doc: /* Return an alist of frame-local faces defined on FRAME.
5103 For internal use only. */)
5104 (frame)
5105 Lisp_Object frame;
5107 struct frame *f = frame_or_selected_frame (frame, 0);
5108 return f->face_alist;
5112 /* Return a hash code for Lisp string STRING with case ignored. Used
5113 below in computing a hash value for a Lisp face. */
5115 static INLINE unsigned
5116 hash_string_case_insensitive (string)
5117 Lisp_Object string;
5119 const unsigned char *s;
5120 unsigned hash = 0;
5121 xassert (STRINGP (string));
5122 for (s = SDATA (string); *s; ++s)
5123 hash = (hash << 1) ^ tolower (*s);
5124 return hash;
5128 /* Return a hash code for face attribute vector V. */
5130 static INLINE unsigned
5131 lface_hash (v)
5132 Lisp_Object *v;
5134 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
5135 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
5136 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
5137 ^ XFASTINT (v[LFACE_WEIGHT_INDEX])
5138 ^ XFASTINT (v[LFACE_SLANT_INDEX])
5139 ^ XFASTINT (v[LFACE_SWIDTH_INDEX])
5140 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
5144 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
5145 considering charsets/registries). They do if they specify the same
5146 family, point size, weight, width, slant, and fontset. Both LFACE1
5147 and LFACE2 must be fully-specified. */
5149 static INLINE int
5150 lface_same_font_attributes_p (lface1, lface2)
5151 Lisp_Object *lface1, *lface2;
5153 xassert (lface_fully_specified_p (lface1)
5154 && lface_fully_specified_p (lface2));
5155 return (xstricmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
5156 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
5157 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
5158 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
5159 && EQ (lface1[LFACE_AVGWIDTH_INDEX], lface2[LFACE_AVGWIDTH_INDEX])
5160 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
5161 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
5162 && (EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
5163 || (STRINGP (lface1[LFACE_FONT_INDEX])
5164 && STRINGP (lface2[LFACE_FONT_INDEX])
5165 && xstricmp (SDATA (lface1[LFACE_FONT_INDEX]),
5166 SDATA (lface2[LFACE_FONT_INDEX])))));
5171 /***********************************************************************
5172 Realized Faces
5173 ***********************************************************************/
5175 /* Allocate and return a new realized face for Lisp face attribute
5176 vector ATTR. */
5178 static struct face *
5179 make_realized_face (attr)
5180 Lisp_Object *attr;
5182 struct face *face = (struct face *) xmalloc (sizeof *face);
5183 bzero (face, sizeof *face);
5184 face->ascii_face = face;
5185 bcopy (attr, face->lface, sizeof face->lface);
5186 return face;
5190 /* Free realized face FACE, including its X resources. FACE may
5191 be null. */
5193 static void
5194 free_realized_face (f, face)
5195 struct frame *f;
5196 struct face *face;
5198 if (face)
5200 #ifdef HAVE_WINDOW_SYSTEM
5201 if (FRAME_WINDOW_P (f))
5203 /* Free fontset of FACE if it is ASCII face. */
5204 if (face->fontset >= 0 && face == face->ascii_face)
5205 free_face_fontset (f, face);
5206 if (face->gc)
5208 x_free_gc (f, face->gc);
5209 face->gc = 0;
5212 free_face_colors (f, face);
5213 x_destroy_bitmap (f, face->stipple);
5215 #endif /* HAVE_WINDOW_SYSTEM */
5217 xfree (face);
5222 /* Prepare face FACE for subsequent display on frame F. This
5223 allocated GCs if they haven't been allocated yet or have been freed
5224 by clearing the face cache. */
5226 void
5227 prepare_face_for_display (f, face)
5228 struct frame *f;
5229 struct face *face;
5231 #ifdef HAVE_WINDOW_SYSTEM
5232 xassert (FRAME_WINDOW_P (f));
5234 if (face->gc == 0)
5236 XGCValues xgcv;
5237 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
5239 xgcv.foreground = face->foreground;
5240 xgcv.background = face->background;
5241 #ifdef HAVE_X_WINDOWS
5242 xgcv.graphics_exposures = False;
5243 #endif
5244 /* The font of FACE may be null if we couldn't load it. */
5245 if (face->font)
5247 #ifdef HAVE_X_WINDOWS
5248 xgcv.font = face->font->fid;
5249 #endif
5250 #ifdef WINDOWSNT
5251 xgcv.font = face->font;
5252 #endif
5253 #ifdef MAC_OS
5254 xgcv.font = face->font;
5255 #endif
5256 mask |= GCFont;
5259 BLOCK_INPUT;
5260 #ifdef HAVE_X_WINDOWS
5261 if (face->stipple)
5263 xgcv.fill_style = FillOpaqueStippled;
5264 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
5265 mask |= GCFillStyle | GCStipple;
5267 #endif
5268 face->gc = x_create_gc (f, mask, &xgcv);
5269 UNBLOCK_INPUT;
5271 #endif /* HAVE_WINDOW_SYSTEM */
5275 /* Returns the `distance' between the colors X and Y. */
5277 static int
5278 color_distance (x, y)
5279 XColor *x, *y;
5281 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
5282 Quoting from that paper:
5284 This formula has results that are very close to L*u*v* (with the
5285 modified lightness curve) and, more importantly, it is a more even
5286 algorithm: it does not have a range of colours where it suddenly
5287 gives far from optimal results.
5289 See <http://www.compuphase.com/cmetric.htm> for more info. */
5291 long r = (x->red - y->red) >> 8;
5292 long g = (x->green - y->green) >> 8;
5293 long b = (x->blue - y->blue) >> 8;
5294 long r_mean = (x->red + y->red) >> 9;
5296 return
5297 (((512 + r_mean) * r * r) >> 8)
5298 + 4 * g * g
5299 + (((767 - r_mean) * b * b) >> 8);
5303 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
5304 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
5305 COLOR1 and COLOR2 may be either strings containing the color name,
5306 or lists of the form (RED GREEN BLUE).
5307 If FRAME is unspecified or nil, the current frame is used. */)
5308 (color1, color2, frame)
5309 Lisp_Object color1, color2, frame;
5311 struct frame *f;
5312 XColor cdef1, cdef2;
5314 if (NILP (frame))
5315 frame = selected_frame;
5316 CHECK_LIVE_FRAME (frame);
5317 f = XFRAME (frame);
5319 if ((CONSP (color1) && !parse_rgb_list (color1, &cdef1))
5320 || !STRINGP (color1)
5321 || !defined_color (f, SDATA (color1), &cdef1, 0))
5322 signal_error ("Invalid color", color1);
5323 if ((CONSP (color2) && !parse_rgb_list (color2, &cdef2))
5324 || !STRINGP (color2)
5325 || !defined_color (f, SDATA (color2), &cdef2, 0))
5326 signal_error ("Invalid color", color2);
5328 return make_number (color_distance (&cdef1, &cdef2));
5332 /***********************************************************************
5333 Face Cache
5334 ***********************************************************************/
5336 /* Return a new face cache for frame F. */
5338 static struct face_cache *
5339 make_face_cache (f)
5340 struct frame *f;
5342 struct face_cache *c;
5343 int size;
5345 c = (struct face_cache *) xmalloc (sizeof *c);
5346 bzero (c, sizeof *c);
5347 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5348 c->buckets = (struct face **) xmalloc (size);
5349 bzero (c->buckets, size);
5350 c->size = 50;
5351 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
5352 c->f = f;
5353 c->menu_face_changed_p = menu_face_changed_default;
5354 return c;
5358 /* Clear out all graphics contexts for all realized faces, except for
5359 the basic faces. This should be done from time to time just to avoid
5360 keeping too many graphics contexts that are no longer needed. */
5362 static void
5363 clear_face_gcs (c)
5364 struct face_cache *c;
5366 if (c && FRAME_WINDOW_P (c->f))
5368 #ifdef HAVE_WINDOW_SYSTEM
5369 int i;
5370 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
5372 struct face *face = c->faces_by_id[i];
5373 if (face && face->gc)
5375 x_free_gc (c->f, face->gc);
5376 face->gc = 0;
5379 #endif /* HAVE_WINDOW_SYSTEM */
5384 /* Free all realized faces in face cache C, including basic faces.
5385 C may be null. If faces are freed, make sure the frame's current
5386 matrix is marked invalid, so that a display caused by an expose
5387 event doesn't try to use faces we destroyed. */
5389 static void
5390 free_realized_faces (c)
5391 struct face_cache *c;
5393 if (c && c->used)
5395 int i, size;
5396 struct frame *f = c->f;
5398 /* We must block input here because we can't process X events
5399 safely while only some faces are freed, or when the frame's
5400 current matrix still references freed faces. */
5401 BLOCK_INPUT;
5403 for (i = 0; i < c->used; ++i)
5405 free_realized_face (f, c->faces_by_id[i]);
5406 c->faces_by_id[i] = NULL;
5409 c->used = 0;
5410 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5411 bzero (c->buckets, size);
5413 /* Must do a thorough redisplay the next time. Mark current
5414 matrices as invalid because they will reference faces freed
5415 above. This function is also called when a frame is
5416 destroyed. In this case, the root window of F is nil. */
5417 if (WINDOWP (f->root_window))
5419 clear_current_matrices (f);
5420 ++windows_or_buffers_changed;
5423 UNBLOCK_INPUT;
5428 /* Free all faces realized for multibyte characters on frame F that
5429 has FONTSET. */
5431 void
5432 free_realized_multibyte_face (f, fontset)
5433 struct frame *f;
5434 int fontset;
5436 struct face_cache *cache = FRAME_FACE_CACHE (f);
5437 struct face *face;
5438 int i;
5440 /* We must block input here because we can't process X events safely
5441 while only some faces are freed, or when the frame's current
5442 matrix still references freed faces. */
5443 BLOCK_INPUT;
5445 for (i = 0; i < cache->used; i++)
5447 face = cache->faces_by_id[i];
5448 if (face
5449 && face != face->ascii_face
5450 && face->fontset == fontset)
5452 uncache_face (cache, face);
5453 free_realized_face (f, face);
5457 /* Must do a thorough redisplay the next time. Mark current
5458 matrices as invalid because they will reference faces freed
5459 above. This function is also called when a frame is destroyed.
5460 In this case, the root window of F is nil. */
5461 if (WINDOWP (f->root_window))
5463 clear_current_matrices (f);
5464 ++windows_or_buffers_changed;
5467 UNBLOCK_INPUT;
5471 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
5472 This is done after attributes of a named face have been changed,
5473 because we can't tell which realized faces depend on that face. */
5475 void
5476 free_all_realized_faces (frame)
5477 Lisp_Object frame;
5479 if (NILP (frame))
5481 Lisp_Object rest;
5482 FOR_EACH_FRAME (rest, frame)
5483 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5485 else
5486 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5490 /* Free face cache C and faces in it, including their X resources. */
5492 static void
5493 free_face_cache (c)
5494 struct face_cache *c;
5496 if (c)
5498 free_realized_faces (c);
5499 xfree (c->buckets);
5500 xfree (c->faces_by_id);
5501 xfree (c);
5506 /* Cache realized face FACE in face cache C. HASH is the hash value
5507 of FACE. If FACE->fontset >= 0, add the new face to the end of the
5508 collision list of the face hash table of C. This is done because
5509 otherwise lookup_face would find FACE for every character, even if
5510 faces with the same attributes but for specific characters exist. */
5512 static void
5513 cache_face (c, face, hash)
5514 struct face_cache *c;
5515 struct face *face;
5516 unsigned hash;
5518 int i = hash % FACE_CACHE_BUCKETS_SIZE;
5520 face->hash = hash;
5522 if (face->fontset >= 0)
5524 struct face *last = c->buckets[i];
5525 if (last)
5527 while (last->next)
5528 last = last->next;
5529 last->next = face;
5530 face->prev = last;
5531 face->next = NULL;
5533 else
5535 c->buckets[i] = face;
5536 face->prev = face->next = NULL;
5539 else
5541 face->prev = NULL;
5542 face->next = c->buckets[i];
5543 if (face->next)
5544 face->next->prev = face;
5545 c->buckets[i] = face;
5548 /* Find a free slot in C->faces_by_id and use the index of the free
5549 slot as FACE->id. */
5550 for (i = 0; i < c->used; ++i)
5551 if (c->faces_by_id[i] == NULL)
5552 break;
5553 face->id = i;
5555 /* Maybe enlarge C->faces_by_id. */
5556 if (i == c->used)
5558 if (c->used == c->size)
5560 int new_size, sz;
5561 new_size = min (2 * c->size, MAX_FACE_ID);
5562 if (new_size == c->size)
5563 abort (); /* Alternatives? ++kfs */
5564 sz = new_size * sizeof *c->faces_by_id;
5565 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
5566 c->size = new_size;
5568 c->used++;
5571 #if GLYPH_DEBUG
5572 /* Check that FACE got a unique id. */
5574 int j, n;
5575 struct face *face;
5577 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
5578 for (face = c->buckets[j]; face; face = face->next)
5579 if (face->id == i)
5580 ++n;
5582 xassert (n == 1);
5584 #endif /* GLYPH_DEBUG */
5586 c->faces_by_id[i] = face;
5590 /* Remove face FACE from cache C. */
5592 static void
5593 uncache_face (c, face)
5594 struct face_cache *c;
5595 struct face *face;
5597 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
5599 if (face->prev)
5600 face->prev->next = face->next;
5601 else
5602 c->buckets[i] = face->next;
5604 if (face->next)
5605 face->next->prev = face->prev;
5607 c->faces_by_id[face->id] = NULL;
5608 if (face->id == c->used)
5609 --c->used;
5613 /* Look up a realized face with face attributes ATTR in the face cache
5614 of frame F. The face will be used to display character C. Value
5615 is the ID of the face found. If no suitable face is found, realize
5616 a new one. In that case, if C is a multibyte character, BASE_FACE
5617 is a face that has the same attributes. */
5619 INLINE int
5620 lookup_face (f, attr, c, base_face)
5621 struct frame *f;
5622 Lisp_Object *attr;
5623 int c;
5624 struct face *base_face;
5626 struct face_cache *cache = FRAME_FACE_CACHE (f);
5627 unsigned hash;
5628 int i;
5629 struct face *face;
5631 xassert (cache != NULL);
5632 check_lface_attrs (attr);
5634 /* Look up ATTR in the face cache. */
5635 hash = lface_hash (attr);
5636 i = hash % FACE_CACHE_BUCKETS_SIZE;
5638 for (face = cache->buckets[i]; face; face = face->next)
5639 if (face->hash == hash
5640 && (!FRAME_WINDOW_P (f)
5641 || FACE_SUITABLE_FOR_CHAR_P (face, c))
5642 && lface_equal_p (face->lface, attr))
5643 break;
5645 /* If not found, realize a new face. */
5646 if (face == NULL)
5647 face = realize_face (cache, attr, c, base_face, -1);
5649 #if GLYPH_DEBUG
5650 xassert (face == FACE_FROM_ID (f, face->id));
5652 /* When this function is called from face_for_char (in this case, C is
5653 a multibyte character), a fontset of a face returned by
5654 realize_face is not yet set, i.e. FACE_SUITABLE_FOR_CHAR_P (FACE,
5655 C) is not sutisfied. The fontset is set for this face by
5656 face_for_char later. */
5657 #if 0
5658 if (FRAME_WINDOW_P (f))
5659 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
5660 #endif
5661 #endif /* GLYPH_DEBUG */
5663 return face->id;
5667 /* Return the face id of the realized face for named face SYMBOL on
5668 frame F suitable for displaying character C. Value is -1 if the
5669 face couldn't be determined, which might happen if the default face
5670 isn't realized and cannot be realized. */
5673 lookup_named_face (f, symbol, c, signal_p)
5674 struct frame *f;
5675 Lisp_Object symbol;
5676 int c;
5677 int signal_p;
5679 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5680 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5681 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5683 if (default_face == NULL)
5685 if (!realize_basic_faces (f))
5686 return -1;
5687 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5690 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p))
5691 return -1;
5693 bcopy (default_face->lface, attrs, sizeof attrs);
5694 merge_face_vectors (f, symbol_attrs, attrs, 0);
5696 return lookup_face (f, attrs, c, NULL);
5700 /* Return the ID of the realized ASCII face of Lisp face with ID
5701 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
5704 ascii_face_of_lisp_face (f, lface_id)
5705 struct frame *f;
5706 int lface_id;
5708 int face_id;
5710 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
5712 Lisp_Object face_name = lface_id_to_name[lface_id];
5713 face_id = lookup_named_face (f, face_name, 0, 1);
5715 else
5716 face_id = -1;
5718 return face_id;
5722 /* Return a face for charset ASCII that is like the face with id
5723 FACE_ID on frame F, but has a font that is STEPS steps smaller.
5724 STEPS < 0 means larger. Value is the id of the face. */
5727 smaller_face (f, face_id, steps)
5728 struct frame *f;
5729 int face_id, steps;
5731 #ifdef HAVE_WINDOW_SYSTEM
5732 struct face *face;
5733 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5734 int pt, last_pt, last_height;
5735 int delta;
5736 int new_face_id;
5737 struct face *new_face;
5739 /* If not called for an X frame, just return the original face. */
5740 if (FRAME_TERMCAP_P (f))
5741 return face_id;
5743 /* Try in increments of 1/2 pt. */
5744 delta = steps < 0 ? 5 : -5;
5745 steps = abs (steps);
5747 face = FACE_FROM_ID (f, face_id);
5748 bcopy (face->lface, attrs, sizeof attrs);
5749 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
5750 new_face_id = face_id;
5751 last_height = FONT_HEIGHT (face->font);
5753 while (steps
5754 && pt + delta > 0
5755 /* Give up if we cannot find a font within 10pt. */
5756 && abs (last_pt - pt) < 100)
5758 /* Look up a face for a slightly smaller/larger font. */
5759 pt += delta;
5760 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
5761 new_face_id = lookup_face (f, attrs, 0, NULL);
5762 new_face = FACE_FROM_ID (f, new_face_id);
5764 /* If height changes, count that as one step. */
5765 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
5766 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
5768 --steps;
5769 last_height = FONT_HEIGHT (new_face->font);
5770 last_pt = pt;
5774 return new_face_id;
5776 #else /* not HAVE_WINDOW_SYSTEM */
5778 return face_id;
5780 #endif /* not HAVE_WINDOW_SYSTEM */
5784 /* Return a face for charset ASCII that is like the face with id
5785 FACE_ID on frame F, but has height HEIGHT. */
5788 face_with_height (f, face_id, height)
5789 struct frame *f;
5790 int face_id;
5791 int height;
5793 #ifdef HAVE_WINDOW_SYSTEM
5794 struct face *face;
5795 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5797 if (FRAME_TERMCAP_P (f)
5798 || height <= 0)
5799 return face_id;
5801 face = FACE_FROM_ID (f, face_id);
5802 bcopy (face->lface, attrs, sizeof attrs);
5803 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
5804 face_id = lookup_face (f, attrs, 0, NULL);
5805 #endif /* HAVE_WINDOW_SYSTEM */
5807 return face_id;
5811 /* Return the face id of the realized face for named face SYMBOL on
5812 frame F suitable for displaying character C, and use attributes of
5813 the face FACE_ID for attributes that aren't completely specified by
5814 SYMBOL. This is like lookup_named_face, except that the default
5815 attributes come from FACE_ID, not from the default face. FACE_ID
5816 is assumed to be already realized. */
5819 lookup_derived_face (f, symbol, c, face_id, signal_p)
5820 struct frame *f;
5821 Lisp_Object symbol;
5822 int c;
5823 int face_id;
5824 int signal_p;
5826 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5827 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5828 struct face *default_face = FACE_FROM_ID (f, face_id);
5830 if (!default_face)
5831 abort ();
5833 get_lface_attributes (f, symbol, symbol_attrs, signal_p);
5834 bcopy (default_face->lface, attrs, sizeof attrs);
5835 merge_face_vectors (f, symbol_attrs, attrs, 0);
5836 return lookup_face (f, attrs, c, default_face);
5839 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
5840 Sface_attributes_as_vector, 1, 1, 0,
5841 doc: /* Return a vector of face attributes corresponding to PLIST. */)
5842 (plist)
5843 Lisp_Object plist;
5845 Lisp_Object lface;
5846 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
5847 Qunspecified);
5848 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
5849 1, 0);
5850 return lface;
5855 /***********************************************************************
5856 Face capability testing
5857 ***********************************************************************/
5860 /* If the distance (as returned by color_distance) between two colors is
5861 less than this, then they are considered the same, for determining
5862 whether a color is supported or not. The range of values is 0-65535. */
5864 #define TTY_SAME_COLOR_THRESHOLD 10000
5866 #ifdef HAVE_WINDOW_SYSTEM
5868 /* Return non-zero if all the face attributes in ATTRS are supported
5869 on the window-system frame F.
5871 The definition of `supported' is somewhat heuristic, but basically means
5872 that a face containing all the attributes in ATTRS, when merged with the
5873 default face for display, can be represented in a way that's
5875 \(1) different in appearance than the default face, and
5876 \(2) `close in spirit' to what the attributes specify, if not exact. */
5878 static int
5879 x_supports_face_attributes_p (f, attrs, def_face)
5880 struct frame *f;
5881 Lisp_Object *attrs;
5882 struct face *def_face;
5884 Lisp_Object *def_attrs = def_face->lface;
5886 /* Check that other specified attributes are different that the default
5887 face. */
5888 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
5889 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
5890 def_attrs[LFACE_UNDERLINE_INDEX]))
5891 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
5892 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
5893 def_attrs[LFACE_INVERSE_INDEX]))
5894 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
5895 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
5896 def_attrs[LFACE_FOREGROUND_INDEX]))
5897 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
5898 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
5899 def_attrs[LFACE_BACKGROUND_INDEX]))
5900 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5901 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
5902 def_attrs[LFACE_STIPPLE_INDEX]))
5903 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5904 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
5905 def_attrs[LFACE_OVERLINE_INDEX]))
5906 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5907 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
5908 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
5909 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5910 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
5911 def_attrs[LFACE_BOX_INDEX])))
5912 return 0;
5914 /* Check font-related attributes, as those are the most commonly
5915 "unsupported" on a window-system (because of missing fonts). */
5916 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5917 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5918 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
5919 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
5920 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5921 || !UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX]))
5923 struct face *face;
5924 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
5926 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
5928 merge_face_vectors (f, attrs, merged_attrs, 0);
5930 face = FACE_FROM_ID (f, lookup_face (f, merged_attrs, 0, 0));
5932 if (! face)
5933 error ("Cannot make face");
5935 /* If the font is the same, then not supported. */
5936 if (face->font == def_face->font)
5937 return 0;
5940 /* Everything checks out, this face is supported. */
5941 return 1;
5944 #endif /* HAVE_WINDOW_SYSTEM */
5946 /* Return non-zero if all the face attributes in ATTRS are supported
5947 on the tty frame F.
5949 The definition of `supported' is somewhat heuristic, but basically means
5950 that a face containing all the attributes in ATTRS, when merged
5951 with the default face for display, can be represented in a way that's
5953 \(1) different in appearance than the default face, and
5954 \(2) `close in spirit' to what the attributes specify, if not exact.
5956 Point (2) implies that a `:weight black' attribute will be satisfied
5957 by any terminal that can display bold, and a `:foreground "yellow"' as
5958 long as the terminal can display a yellowish color, but `:slant italic'
5959 will _not_ be satisfied by the tty display code's automatic
5960 substitution of a `dim' face for italic. */
5962 static int
5963 tty_supports_face_attributes_p (f, attrs, def_face)
5964 struct frame *f;
5965 Lisp_Object *attrs;
5966 struct face *def_face;
5968 int weight;
5969 Lisp_Object val, fg, bg;
5970 XColor fg_tty_color, fg_std_color;
5971 XColor bg_tty_color, bg_std_color;
5972 unsigned test_caps = 0;
5973 Lisp_Object *def_attrs = def_face->lface;
5976 /* First check some easy-to-check stuff; ttys support none of the
5977 following attributes, so we can just return false if any are requested
5978 (even if `nominal' values are specified, we should still return false,
5979 as that will be the same value that the default face uses). We
5980 consider :slant unsupportable on ttys, even though the face code
5981 actually `fakes' them using a dim attribute if possible. This is
5982 because the faked result is too different from what the face
5983 specifies. */
5984 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5985 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5986 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5987 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5988 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5989 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5990 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5991 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5992 return 0;
5995 /* Test for terminal `capabilities' (non-color character attributes). */
5997 /* font weight (bold/dim) */
5998 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5999 if (weight >= 0)
6001 int def_weight = face_numeric_weight (def_attrs[LFACE_WEIGHT_INDEX]);
6003 if (weight > XLFD_WEIGHT_MEDIUM)
6005 if (def_weight > XLFD_WEIGHT_MEDIUM)
6006 return 0; /* same as default */
6007 test_caps = TTY_CAP_BOLD;
6009 else if (weight < XLFD_WEIGHT_MEDIUM)
6011 if (def_weight < XLFD_WEIGHT_MEDIUM)
6012 return 0; /* same as default */
6013 test_caps = TTY_CAP_DIM;
6015 else if (def_weight == XLFD_WEIGHT_MEDIUM)
6016 return 0; /* same as default */
6019 /* underlining */
6020 val = attrs[LFACE_UNDERLINE_INDEX];
6021 if (!UNSPECIFIEDP (val))
6023 if (STRINGP (val))
6024 return 0; /* ttys can't use colored underlines */
6025 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
6026 return 0; /* same as default */
6027 else
6028 test_caps |= TTY_CAP_UNDERLINE;
6031 /* inverse video */
6032 val = attrs[LFACE_INVERSE_INDEX];
6033 if (!UNSPECIFIEDP (val))
6035 if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
6036 return 0; /* same as default */
6037 else
6038 test_caps |= TTY_CAP_INVERSE;
6042 /* Color testing. */
6044 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
6045 we use them when calling `tty_capable_p' below, even if the face
6046 specifies no colors. */
6047 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
6048 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
6050 /* Check if foreground color is close enough. */
6051 fg = attrs[LFACE_FOREGROUND_INDEX];
6052 if (STRINGP (fg))
6054 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
6056 if (face_attr_equal_p (fg, def_fg))
6057 return 0; /* same as default */
6058 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
6059 return 0; /* not a valid color */
6060 else if (color_distance (&fg_tty_color, &fg_std_color)
6061 > TTY_SAME_COLOR_THRESHOLD)
6062 return 0; /* displayed color is too different */
6063 else
6064 /* Make sure the color is really different than the default. */
6066 XColor def_fg_color;
6067 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
6068 && (color_distance (&fg_tty_color, &def_fg_color)
6069 <= TTY_SAME_COLOR_THRESHOLD))
6070 return 0;
6074 /* Check if background color is close enough. */
6075 bg = attrs[LFACE_BACKGROUND_INDEX];
6076 if (STRINGP (bg))
6078 Lisp_Object def_bg = def_attrs[LFACE_FOREGROUND_INDEX];
6080 if (face_attr_equal_p (bg, def_bg))
6081 return 0; /* same as default */
6082 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
6083 return 0; /* not a valid color */
6084 else if (color_distance (&bg_tty_color, &bg_std_color)
6085 > TTY_SAME_COLOR_THRESHOLD)
6086 return 0; /* displayed color is too different */
6087 else
6088 /* Make sure the color is really different than the default. */
6090 XColor def_bg_color;
6091 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
6092 && (color_distance (&bg_tty_color, &def_bg_color)
6093 <= TTY_SAME_COLOR_THRESHOLD))
6094 return 0;
6098 /* If both foreground and background are requested, see if the
6099 distance between them is OK. We just check to see if the distance
6100 between the tty's foreground and background is close enough to the
6101 distance between the standard foreground and background. */
6102 if (STRINGP (fg) && STRINGP (bg))
6104 int delta_delta
6105 = (color_distance (&fg_std_color, &bg_std_color)
6106 - color_distance (&fg_tty_color, &bg_tty_color));
6107 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
6108 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
6109 return 0;
6113 /* See if the capabilities we selected above are supported, with the
6114 given colors. */
6115 if (test_caps != 0 &&
6116 ! tty_capable_p (f, test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
6117 return 0;
6120 /* Hmmm, everything checks out, this terminal must support this face. */
6121 return 1;
6125 DEFUN ("display-supports-face-attributes-p",
6126 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
6127 1, 2, 0,
6128 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
6129 The optional argument DISPLAY can be a display name, a frame, or
6130 nil (meaning the selected frame's display).
6132 The definition of `supported' is somewhat heuristic, but basically means
6133 that a face containing all the attributes in ATTRIBUTES, when merged
6134 with the default face for display, can be represented in a way that's
6136 \(1) different in appearance than the default face, and
6137 \(2) `close in spirit' to what the attributes specify, if not exact.
6139 Point (2) implies that a `:weight black' attribute will be satisfied by
6140 any display that can display bold, and a `:foreground \"yellow\"' as long
6141 as it can display a yellowish color, but `:slant italic' will _not_ be
6142 satisfied by the tty display code's automatic substitution of a `dim'
6143 face for italic. */)
6144 (attributes, display)
6145 Lisp_Object attributes, display;
6147 int supports, i;
6148 Lisp_Object frame;
6149 struct frame *f;
6150 struct face *def_face;
6151 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6153 if (noninteractive || !initialized)
6154 /* We may not be able to access low-level face information in batch
6155 mode, or before being dumped, and this function is not going to
6156 be very useful in those cases anyway, so just give up. */
6157 return Qnil;
6159 if (NILP (display))
6160 frame = selected_frame;
6161 else if (FRAMEP (display))
6162 frame = display;
6163 else
6165 /* Find any frame on DISPLAY. */
6166 Lisp_Object fl_tail;
6168 frame = Qnil;
6169 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
6171 frame = XCAR (fl_tail);
6172 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
6173 XFRAME (frame)->param_alist)),
6174 display)))
6175 break;
6179 CHECK_LIVE_FRAME (frame);
6180 f = XFRAME (frame);
6182 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
6183 attrs[i] = Qunspecified;
6184 merge_face_ref (f, attributes, attrs, 1, 0);
6186 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6187 if (def_face == NULL)
6189 if (! realize_basic_faces (f))
6190 error ("Cannot realize default face");
6191 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6194 /* Dispatch to the appropriate handler. */
6195 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6196 supports = tty_supports_face_attributes_p (f, attrs, def_face);
6197 #ifdef HAVE_WINDOW_SYSTEM
6198 else
6199 supports = x_supports_face_attributes_p (f, attrs, def_face);
6200 #endif
6202 return supports ? Qt : Qnil;
6206 /***********************************************************************
6207 Font selection
6208 ***********************************************************************/
6210 DEFUN ("internal-set-font-selection-order",
6211 Finternal_set_font_selection_order,
6212 Sinternal_set_font_selection_order, 1, 1, 0,
6213 doc: /* Set font selection order for face font selection to ORDER.
6214 ORDER must be a list of length 4 containing the symbols `:width',
6215 `:height', `:weight', and `:slant'. Face attributes appearing
6216 first in ORDER are matched first, e.g. if `:height' appears before
6217 `:weight' in ORDER, font selection first tries to find a font with
6218 a suitable height, and then tries to match the font weight.
6219 Value is ORDER. */)
6220 (order)
6221 Lisp_Object order;
6223 Lisp_Object list;
6224 int i;
6225 int indices[DIM (font_sort_order)];
6227 CHECK_LIST (order);
6228 bzero (indices, sizeof indices);
6229 i = 0;
6231 for (list = order;
6232 CONSP (list) && i < DIM (indices);
6233 list = XCDR (list), ++i)
6235 Lisp_Object attr = XCAR (list);
6236 int xlfd;
6238 if (EQ (attr, QCwidth))
6239 xlfd = XLFD_SWIDTH;
6240 else if (EQ (attr, QCheight))
6241 xlfd = XLFD_POINT_SIZE;
6242 else if (EQ (attr, QCweight))
6243 xlfd = XLFD_WEIGHT;
6244 else if (EQ (attr, QCslant))
6245 xlfd = XLFD_SLANT;
6246 else
6247 break;
6249 if (indices[i] != 0)
6250 break;
6251 indices[i] = xlfd;
6254 if (!NILP (list) || i != DIM (indices))
6255 signal_error ("Invalid font sort order", order);
6256 for (i = 0; i < DIM (font_sort_order); ++i)
6257 if (indices[i] == 0)
6258 signal_error ("Invalid font sort order", order);
6260 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
6262 bcopy (indices, font_sort_order, sizeof font_sort_order);
6263 free_all_realized_faces (Qnil);
6266 return Qnil;
6270 DEFUN ("internal-set-alternative-font-family-alist",
6271 Finternal_set_alternative_font_family_alist,
6272 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
6273 doc: /* Define alternative font families to try in face font selection.
6274 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6275 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
6276 be found. Value is ALIST. */)
6277 (alist)
6278 Lisp_Object alist;
6280 CHECK_LIST (alist);
6281 Vface_alternative_font_family_alist = alist;
6282 free_all_realized_faces (Qnil);
6283 return alist;
6287 DEFUN ("internal-set-alternative-font-registry-alist",
6288 Finternal_set_alternative_font_registry_alist,
6289 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
6290 doc: /* Define alternative font registries to try in face font selection.
6291 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6292 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
6293 be found. Value is ALIST. */)
6294 (alist)
6295 Lisp_Object alist;
6297 CHECK_LIST (alist);
6298 Vface_alternative_font_registry_alist = alist;
6299 free_all_realized_faces (Qnil);
6300 return alist;
6304 #ifdef HAVE_WINDOW_SYSTEM
6306 /* Value is non-zero if FONT is the name of a scalable font. The
6307 X11R6 XLFD spec says that point size, pixel size, and average width
6308 are zero for scalable fonts. Intlfonts contain at least one
6309 scalable font ("*-muleindian-1") for which this isn't true, so we
6310 just test average width. */
6312 static int
6313 font_scalable_p (font)
6314 struct font_name *font;
6316 char *s = font->fields[XLFD_AVGWIDTH];
6317 return (*s == '0' && *(s + 1) == '\0')
6318 #ifdef WINDOWSNT
6319 /* Windows implementation of XLFD is slightly broken for backward
6320 compatibility with previous broken versions, so test for
6321 wildcards as well as 0. */
6322 || *s == '*'
6323 #endif
6328 /* Ignore the difference of font point size less than this value. */
6330 #define FONT_POINT_SIZE_QUANTUM 5
6332 /* Value is non-zero if FONT1 is a better match for font attributes
6333 VALUES than FONT2. VALUES is an array of face attribute values in
6334 font sort order. COMPARE_PT_P zero means don't compare point
6335 sizes. AVGWIDTH, if not zero, is a specified font average width
6336 to compare with. */
6338 static int
6339 better_font_p (values, font1, font2, compare_pt_p, avgwidth)
6340 int *values;
6341 struct font_name *font1, *font2;
6342 int compare_pt_p, avgwidth;
6344 int i;
6346 for (i = 0; i < DIM (font_sort_order); ++i)
6348 int xlfd_idx = font_sort_order[i];
6350 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
6352 int delta1, delta2;
6354 if (xlfd_idx == XLFD_POINT_SIZE)
6356 delta1 = abs (values[i] - (font1->numeric[xlfd_idx]
6357 / font1->rescale_ratio));
6358 delta2 = abs (values[i] - (font2->numeric[xlfd_idx]
6359 / font2->rescale_ratio));
6360 if (abs (delta1 - delta2) < FONT_POINT_SIZE_QUANTUM)
6361 continue;
6363 else
6365 delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
6366 delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
6369 if (delta1 > delta2)
6370 return 0;
6371 else if (delta1 < delta2)
6372 return 1;
6373 else
6375 /* The difference may be equal because, e.g., the face
6376 specifies `italic' but we have only `regular' and
6377 `oblique'. Prefer `oblique' in this case. */
6378 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
6379 && font1->numeric[xlfd_idx] > values[i]
6380 && font2->numeric[xlfd_idx] < values[i])
6381 return 1;
6386 if (avgwidth)
6388 int delta1 = abs (avgwidth - font1->numeric[XLFD_AVGWIDTH]);
6389 int delta2 = abs (avgwidth - font2->numeric[XLFD_AVGWIDTH]);
6390 if (delta1 > delta2)
6391 return 0;
6392 else if (delta1 < delta2)
6393 return 1;
6396 if (! compare_pt_p)
6398 /* We prefer a real scalable font; i.e. not what autoscaled. */
6399 int auto_scaled_1 = (font1->numeric[XLFD_POINT_SIZE] == 0
6400 && font1->numeric[XLFD_RESY] > 0);
6401 int auto_scaled_2 = (font2->numeric[XLFD_POINT_SIZE] == 0
6402 && font2->numeric[XLFD_RESY] > 0);
6404 if (auto_scaled_1 != auto_scaled_2)
6405 return auto_scaled_2;
6408 return font1->registry_priority < font2->registry_priority;
6412 /* Value is non-zero if FONT is an exact match for face attributes in
6413 SPECIFIED. SPECIFIED is an array of face attribute values in font
6414 sort order. AVGWIDTH, if non-zero, is an average width to compare
6415 with. */
6417 static int
6418 exact_face_match_p (specified, font, avgwidth)
6419 int *specified;
6420 struct font_name *font;
6421 int avgwidth;
6423 int i;
6425 for (i = 0; i < DIM (font_sort_order); ++i)
6426 if (specified[i] != font->numeric[font_sort_order[i]])
6427 break;
6429 return (i == DIM (font_sort_order)
6430 && (avgwidth <= 0
6431 || avgwidth == font->numeric[XLFD_AVGWIDTH]));
6435 /* Value is the name of a scaled font, generated from scalable font
6436 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
6437 Value is allocated from heap. */
6439 static char *
6440 build_scalable_font_name (f, font, specified_pt)
6441 struct frame *f;
6442 struct font_name *font;
6443 int specified_pt;
6445 char pixel_size[20];
6446 int pixel_value;
6447 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
6448 double pt;
6450 /* If scalable font is for a specific resolution, compute
6451 the point size we must specify from the resolution of
6452 the display and the specified resolution of the font. */
6453 if (font->numeric[XLFD_RESY] != 0)
6455 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
6456 pixel_value = font->numeric[XLFD_RESY] / (PT_PER_INCH * 10.0) * pt + 0.5;
6458 else
6460 pt = specified_pt;
6461 pixel_value = resy / (PT_PER_INCH * 10.0) * pt + 0.5;
6463 /* We may need a font of the different size. */
6464 pixel_value *= font->rescale_ratio;
6466 /* We should keep POINT_SIZE 0. Otherwise, X server can't open a
6467 font of the specified PIXEL_SIZE. */
6468 #if 0
6469 { /* Set point size of the font. */
6470 char point_size[20];
6471 sprintf (point_size, "%d", (int) pt);
6472 font->fields[XLFD_POINT_SIZE] = point_size;
6473 font->numeric[XLFD_POINT_SIZE] = pt;
6475 #endif
6477 /* Set pixel size. */
6478 sprintf (pixel_size, "%d", pixel_value);
6479 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
6480 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
6482 /* If font doesn't specify its resolution, use the
6483 resolution of the display. */
6484 if (font->numeric[XLFD_RESY] == 0)
6486 char buffer[20];
6487 sprintf (buffer, "%d", (int) resy);
6488 font->fields[XLFD_RESY] = buffer;
6489 font->numeric[XLFD_RESY] = resy;
6492 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
6494 char buffer[20];
6495 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
6496 sprintf (buffer, "%d", resx);
6497 font->fields[XLFD_RESX] = buffer;
6498 font->numeric[XLFD_RESX] = resx;
6501 return build_font_name (font);
6505 /* Value is non-zero if we are allowed to use scalable font FONT. We
6506 can't run a Lisp function here since this function may be called
6507 with input blocked. */
6509 static int
6510 may_use_scalable_font_p (font)
6511 const char *font;
6513 if (EQ (Vscalable_fonts_allowed, Qt))
6514 return 1;
6515 else if (CONSP (Vscalable_fonts_allowed))
6517 Lisp_Object tail, regexp;
6519 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
6521 regexp = XCAR (tail);
6522 if (STRINGP (regexp)
6523 && fast_c_string_match_ignore_case (regexp, font) >= 0)
6524 return 1;
6528 return 0;
6533 /* Return the name of the best matching font for face attributes ATTRS
6534 in the array of font_name structures FONTS which contains NFONTS
6535 elements. WIDTH_RATIO is a factor with which to multiply average
6536 widths if ATTRS specifies such a width.
6538 Value is a font name which is allocated from the heap. FONTS is
6539 freed by this function.
6541 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6542 indicate whether the resulting font should be drawn using overstrike
6543 to simulate bold-face. */
6545 static char *
6546 best_matching_font (f, attrs, fonts, nfonts, width_ratio, needs_overstrike)
6547 struct frame *f;
6548 Lisp_Object *attrs;
6549 struct font_name *fonts;
6550 int nfonts;
6551 int width_ratio;
6552 int *needs_overstrike;
6554 char *font_name;
6555 struct font_name *best;
6556 int i, pt = 0;
6557 int specified[5];
6558 int exact_p, avgwidth;
6560 if (nfonts == 0)
6561 return NULL;
6563 /* Make specified font attributes available in `specified',
6564 indexed by sort order. */
6565 for (i = 0; i < DIM (font_sort_order); ++i)
6567 int xlfd_idx = font_sort_order[i];
6569 if (xlfd_idx == XLFD_SWIDTH)
6570 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
6571 else if (xlfd_idx == XLFD_POINT_SIZE)
6572 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
6573 else if (xlfd_idx == XLFD_WEIGHT)
6574 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
6575 else if (xlfd_idx == XLFD_SLANT)
6576 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
6577 else
6578 abort ();
6581 avgwidth = (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
6583 : XFASTINT (attrs[LFACE_AVGWIDTH_INDEX]) * width_ratio);
6585 exact_p = 0;
6587 if (needs_overstrike)
6588 *needs_overstrike = 0;
6590 /* Start with the first non-scalable font in the list. */
6591 for (i = 0; i < nfonts; ++i)
6592 if (!font_scalable_p (fonts + i))
6593 break;
6595 /* Find the best match among the non-scalable fonts. */
6596 if (i < nfonts)
6598 best = fonts + i;
6600 for (i = 1; i < nfonts; ++i)
6601 if (!font_scalable_p (fonts + i)
6602 && better_font_p (specified, fonts + i, best, 1, avgwidth))
6604 best = fonts + i;
6606 exact_p = exact_face_match_p (specified, best, avgwidth);
6607 if (exact_p)
6608 break;
6611 else
6612 best = NULL;
6614 /* Unless we found an exact match among non-scalable fonts, see if
6615 we can find a better match among scalable fonts. */
6616 if (!exact_p)
6618 /* A scalable font is better if
6620 1. its weight, slant, swidth attributes are better, or.
6622 2. the best non-scalable font doesn't have the required
6623 point size, and the scalable fonts weight, slant, swidth
6624 isn't worse. */
6626 int non_scalable_has_exact_height_p;
6628 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
6629 non_scalable_has_exact_height_p = 1;
6630 else
6631 non_scalable_has_exact_height_p = 0;
6633 for (i = 0; i < nfonts; ++i)
6634 if (font_scalable_p (fonts + i))
6636 if (best == NULL
6637 || better_font_p (specified, fonts + i, best, 0, 0)
6638 || (!non_scalable_has_exact_height_p
6639 && !better_font_p (specified, best, fonts + i, 0, 0)))
6641 non_scalable_has_exact_height_p = 1;
6642 best = fonts + i;
6646 if (needs_overstrike)
6648 enum xlfd_weight want_weight = specified[XLFD_WEIGHT];
6649 enum xlfd_weight got_weight = best->numeric[XLFD_WEIGHT];
6651 if (want_weight > XLFD_WEIGHT_MEDIUM && want_weight > got_weight)
6653 /* We want a bold font, but didn't get one; try to use
6654 overstriking instead to simulate bold-face. However,
6655 don't overstrike an already-bold fontn unless the
6656 desired weight grossly exceeds the available weight. */
6657 if (got_weight > XLFD_WEIGHT_MEDIUM)
6658 *needs_overstrike = (got_weight - want_weight) > 2;
6659 else
6660 *needs_overstrike = 1;
6665 if (font_scalable_p (best))
6666 font_name = build_scalable_font_name (f, best, pt);
6667 else
6668 font_name = build_font_name (best);
6670 /* Free font_name structures. */
6671 free_font_names (fonts, nfonts);
6673 return font_name;
6677 /* Get a list of matching fonts on frame F, considering FAMILY
6678 and alternative font families from Vface_alternative_font_registry_alist.
6680 FAMILY is the font family whose alternatives are considered.
6682 REGISTRY, if a string, specifies a font registry and encoding to
6683 match. A value of nil means include fonts of any registry and
6684 encoding.
6686 Return in *FONTS a pointer to a vector of font_name structures for
6687 the fonts matched. Value is the number of fonts found. */
6689 static int
6690 try_alternative_families (f, family, registry, fonts)
6691 struct frame *f;
6692 Lisp_Object family, registry;
6693 struct font_name **fonts;
6695 Lisp_Object alter;
6696 int nfonts = 0;
6698 nfonts = font_list (f, Qnil, family, registry, fonts);
6699 if (nfonts == 0)
6701 /* Try alternative font families. */
6702 alter = Fassoc (family, Vface_alternative_font_family_alist);
6703 if (CONSP (alter))
6705 for (alter = XCDR (alter);
6706 CONSP (alter) && nfonts == 0;
6707 alter = XCDR (alter))
6709 if (STRINGP (XCAR (alter)))
6710 nfonts = font_list (f, Qnil, XCAR (alter), registry, fonts);
6714 /* Try all scalable fonts before giving up. */
6715 if (nfonts == 0 && ! EQ (Vscalable_fonts_allowed, Qt))
6717 int count = SPECPDL_INDEX ();
6718 specbind (Qscalable_fonts_allowed, Qt);
6719 nfonts = try_alternative_families (f, family, registry, fonts);
6720 unbind_to (count, Qnil);
6723 return nfonts;
6727 /* Get a list of matching fonts on frame F.
6729 FAMILY, if a string, specifies a font family derived from the fontset.
6730 It is only used if the face does not specify any family in ATTRS or
6731 if we cannot find any font of the face's family.
6733 REGISTRY, if a string, specifies a font registry and encoding to
6734 match. A value of nil means include fonts of any registry and
6735 encoding.
6737 If PREFER_FACE_FAMILY is nonzero, perfer face's family to FAMILY.
6738 Otherwise, prefer FAMILY.
6740 Return in *FONTS a pointer to a vector of font_name structures for
6741 the fonts matched. Value is the number of fonts found. */
6743 static int
6744 try_font_list (f, attrs, family, registry, fonts, prefer_face_family)
6745 struct frame *f;
6746 Lisp_Object *attrs;
6747 Lisp_Object family, registry;
6748 struct font_name **fonts;
6749 int prefer_face_family;
6751 int nfonts = 0;
6752 Lisp_Object face_family = attrs[LFACE_FAMILY_INDEX];
6753 Lisp_Object try_family;
6755 try_family = (prefer_face_family || NILP (family)) ? face_family : family;
6757 if (STRINGP (try_family))
6758 nfonts = try_alternative_families (f, try_family, registry, fonts);
6760 #ifdef MAC_OS
6761 if (nfonts == 0 && STRINGP (try_family) && STRINGP (registry))
6762 if (xstricmp (SDATA (registry), "mac-roman") == 0)
6763 /* When realizing the default face and a font spec does not
6764 matched exactly, Emacs looks for ones with the same registry
6765 as the default font. On the Mac, this is mac-roman, which
6766 does not work if the family is -etl-fixed, e.g. The
6767 following widens the choices and fixes that problem. */
6768 nfonts = try_alternative_families (f, try_family, Qnil, fonts);
6769 else if (SBYTES (try_family) > 0
6770 && SREF (try_family, SBYTES (try_family) - 1) != '*')
6771 /* Some Central European/Cyrillic font family names have the
6772 Roman counterpart name as their prefix. */
6773 nfonts = try_alternative_families (f, concat2 (try_family,
6774 build_string ("*")),
6775 registry, fonts);
6776 #endif
6778 if (EQ (try_family, family))
6779 family = face_family;
6781 if (nfonts == 0 && STRINGP (family))
6782 nfonts = try_alternative_families (f, family, registry, fonts);
6784 /* Try font family of the default face or "fixed". */
6785 if (nfonts == 0)
6787 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6788 if (default_face)
6789 family = default_face->lface[LFACE_FAMILY_INDEX];
6790 else
6791 family = build_string ("fixed");
6792 nfonts = font_list (f, Qnil, family, registry, fonts);
6795 /* Try any family with the given registry. */
6796 if (nfonts == 0)
6797 nfonts = try_alternative_families (f, Qnil, registry, fonts);
6799 return nfonts;
6803 /* Return the fontset id of the base fontset name or alias name given
6804 by the fontset attribute of ATTRS. Value is -1 if the fontset
6805 attribute of ATTRS doesn't name a fontset. */
6807 static int
6808 face_fontset (attrs)
6809 Lisp_Object *attrs;
6811 Lisp_Object name;
6813 name = attrs[LFACE_FONT_INDEX];
6814 if (!STRINGP (name))
6815 return -1;
6816 return fs_query_fontset (name, 0);
6820 /* Choose a name of font to use on frame F to display character C with
6821 Lisp face attributes specified by ATTRS. The font name is
6822 determined by the font-related attributes in ATTRS and the name
6823 pattern for C in FONTSET. Value is the font name which is
6824 allocated from the heap and must be freed by the caller, or NULL if
6825 we can get no information about the font name of C. It is assured
6826 that we always get some information for a single byte
6827 character.
6829 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6830 indicate whether the resulting font should be drawn using overstrike
6831 to simulate bold-face. */
6833 static char *
6834 choose_face_font (f, attrs, fontset, c, needs_overstrike)
6835 struct frame *f;
6836 Lisp_Object *attrs;
6837 int fontset, c;
6838 int *needs_overstrike;
6840 Lisp_Object pattern;
6841 char *font_name = NULL;
6842 struct font_name *fonts;
6843 int nfonts, width_ratio;
6845 if (needs_overstrike)
6846 *needs_overstrike = 0;
6848 /* Get (foundry and) family name and registry (and encoding) name of
6849 a font for C. */
6850 pattern = fontset_font_pattern (f, fontset, c);
6851 if (NILP (pattern))
6853 xassert (!SINGLE_BYTE_CHAR_P (c));
6854 return NULL;
6857 /* If what we got is a name pattern, return it. */
6858 if (STRINGP (pattern))
6859 return xstrdup (SDATA (pattern));
6861 /* Get a list of fonts matching that pattern and choose the
6862 best match for the specified face attributes from it. */
6863 nfonts = try_font_list (f, attrs, XCAR (pattern), XCDR (pattern), &fonts,
6864 (SINGLE_BYTE_CHAR_P (c)
6865 || CHAR_CHARSET (c) == charset_latin_iso8859_1));
6866 width_ratio = (SINGLE_BYTE_CHAR_P (c)
6868 : CHARSET_WIDTH (CHAR_CHARSET (c)));
6869 font_name = best_matching_font (f, attrs, fonts, nfonts, width_ratio,
6870 needs_overstrike);
6871 return font_name;
6874 #endif /* HAVE_WINDOW_SYSTEM */
6878 /***********************************************************************
6879 Face Realization
6880 ***********************************************************************/
6882 /* Realize basic faces on frame F. Value is zero if frame parameters
6883 of F don't contain enough information needed to realize the default
6884 face. */
6886 static int
6887 realize_basic_faces (f)
6888 struct frame *f;
6890 int success_p = 0;
6891 int count = SPECPDL_INDEX ();
6893 /* Block input here so that we won't be surprised by an X expose
6894 event, for instance, without having the faces set up. */
6895 BLOCK_INPUT;
6896 specbind (Qscalable_fonts_allowed, Qt);
6898 if (realize_default_face (f))
6900 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
6901 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
6902 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
6903 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
6904 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
6905 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
6906 realize_named_face (f, Qborder, BORDER_FACE_ID);
6907 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
6908 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
6909 realize_named_face (f, Qmenu, MENU_FACE_ID);
6910 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
6912 /* Reflect changes in the `menu' face in menu bars. */
6913 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
6915 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
6916 #ifdef USE_X_TOOLKIT
6917 x_update_menu_appearance (f);
6918 #endif
6921 success_p = 1;
6924 unbind_to (count, Qnil);
6925 UNBLOCK_INPUT;
6926 return success_p;
6930 /* Realize the default face on frame F. If the face is not fully
6931 specified, make it fully-specified. Attributes of the default face
6932 that are not explicitly specified are taken from frame parameters. */
6934 static int
6935 realize_default_face (f)
6936 struct frame *f;
6938 struct face_cache *c = FRAME_FACE_CACHE (f);
6939 Lisp_Object lface;
6940 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6941 Lisp_Object frame_font;
6942 struct face *face;
6944 /* If the `default' face is not yet known, create it. */
6945 lface = lface_from_face_name (f, Qdefault, 0);
6946 if (NILP (lface))
6948 Lisp_Object frame;
6949 XSETFRAME (frame, f);
6950 lface = Finternal_make_lisp_face (Qdefault, frame);
6954 #ifdef HAVE_WINDOW_SYSTEM
6955 if (FRAME_WINDOW_P (f))
6957 /* Set frame_font to the value of the `font' frame parameter. */
6958 frame_font = Fassq (Qfont, f->param_alist);
6959 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
6960 frame_font = XCDR (frame_font);
6961 set_lface_from_font_name (f, lface, frame_font,
6962 f->default_face_done_p, 1);
6963 f->default_face_done_p = 1;
6965 #endif /* HAVE_WINDOW_SYSTEM */
6967 if (!FRAME_WINDOW_P (f))
6969 LFACE_FAMILY (lface) = build_string ("default");
6970 LFACE_SWIDTH (lface) = Qnormal;
6971 LFACE_HEIGHT (lface) = make_number (1);
6972 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
6973 LFACE_WEIGHT (lface) = Qnormal;
6974 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
6975 LFACE_SLANT (lface) = Qnormal;
6976 LFACE_AVGWIDTH (lface) = Qunspecified;
6979 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
6980 LFACE_UNDERLINE (lface) = Qnil;
6982 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
6983 LFACE_OVERLINE (lface) = Qnil;
6985 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
6986 LFACE_STRIKE_THROUGH (lface) = Qnil;
6988 if (UNSPECIFIEDP (LFACE_BOX (lface)))
6989 LFACE_BOX (lface) = Qnil;
6991 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
6992 LFACE_INVERSE (lface) = Qnil;
6994 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
6996 /* This function is called so early that colors are not yet
6997 set in the frame parameter list. */
6998 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
7000 if (CONSP (color) && STRINGP (XCDR (color)))
7001 LFACE_FOREGROUND (lface) = XCDR (color);
7002 else if (FRAME_WINDOW_P (f))
7003 return 0;
7004 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
7005 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
7006 else
7007 abort ();
7010 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
7012 /* This function is called so early that colors are not yet
7013 set in the frame parameter list. */
7014 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
7015 if (CONSP (color) && STRINGP (XCDR (color)))
7016 LFACE_BACKGROUND (lface) = XCDR (color);
7017 else if (FRAME_WINDOW_P (f))
7018 return 0;
7019 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
7020 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
7021 else
7022 abort ();
7025 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
7026 LFACE_STIPPLE (lface) = Qnil;
7028 /* Realize the face; it must be fully-specified now. */
7029 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
7030 check_lface (lface);
7031 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
7032 face = realize_face (c, attrs, 0, NULL, DEFAULT_FACE_ID);
7033 return 1;
7037 /* Realize basic faces other than the default face in face cache C.
7038 SYMBOL is the face name, ID is the face id the realized face must
7039 have. The default face must have been realized already. */
7041 static void
7042 realize_named_face (f, symbol, id)
7043 struct frame *f;
7044 Lisp_Object symbol;
7045 int id;
7047 struct face_cache *c = FRAME_FACE_CACHE (f);
7048 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
7049 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7050 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
7051 struct face *new_face;
7053 /* The default face must exist and be fully specified. */
7054 get_lface_attributes (f, Qdefault, attrs, 1);
7055 check_lface_attrs (attrs);
7056 xassert (lface_fully_specified_p (attrs));
7058 /* If SYMBOL isn't know as a face, create it. */
7059 if (NILP (lface))
7061 Lisp_Object frame;
7062 XSETFRAME (frame, f);
7063 lface = Finternal_make_lisp_face (symbol, frame);
7066 /* Merge SYMBOL's face with the default face. */
7067 get_lface_attributes (f, symbol, symbol_attrs, 1);
7068 merge_face_vectors (f, symbol_attrs, attrs, 0);
7070 /* Realize the face. */
7071 new_face = realize_face (c, attrs, 0, NULL, id);
7075 /* Realize the fully-specified face with attributes ATTRS in face
7076 cache CACHE for character C. If C is a multibyte character,
7077 BASE_FACE is a face that has the same attributes. Otherwise,
7078 BASE_FACE is ignored. If FORMER_FACE_ID is non-negative, it is an
7079 ID of face to remove before caching the new face. Value is a
7080 pointer to the newly created realized face. */
7082 static struct face *
7083 realize_face (cache, attrs, c, base_face, former_face_id)
7084 struct face_cache *cache;
7085 Lisp_Object *attrs;
7086 int c;
7087 struct face *base_face;
7088 int former_face_id;
7090 struct face *face;
7092 /* LFACE must be fully specified. */
7093 xassert (cache != NULL);
7094 check_lface_attrs (attrs);
7096 if (former_face_id >= 0 && cache->used > former_face_id)
7098 /* Remove the former face. */
7099 struct face *former_face = cache->faces_by_id[former_face_id];
7100 uncache_face (cache, former_face);
7101 free_realized_face (cache->f, former_face);
7104 if (FRAME_WINDOW_P (cache->f))
7105 face = realize_x_face (cache, attrs, c, base_face);
7106 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
7107 face = realize_tty_face (cache, attrs, c);
7108 else
7109 abort ();
7111 /* Insert the new face. */
7112 cache_face (cache, face, lface_hash (attrs));
7113 #ifdef HAVE_WINDOW_SYSTEM
7114 if (FRAME_WINDOW_P (cache->f) && face->font == NULL)
7115 load_face_font (cache->f, face, c);
7116 #endif /* HAVE_WINDOW_SYSTEM */
7117 return face;
7121 /* Realize the fully-specified face with attributes ATTRS in face
7122 cache CACHE for character C. Do it for X frame CACHE->f. If C is
7123 a multibyte character, BASE_FACE is a face that has the same
7124 attributes. Otherwise, BASE_FACE is ignored. If the new face
7125 doesn't share font with the default face, a fontname is allocated
7126 from the heap and set in `font_name' of the new face, but it is not
7127 yet loaded here. Value is a pointer to the newly created realized
7128 face. */
7130 static struct face *
7131 realize_x_face (cache, attrs, c, base_face)
7132 struct face_cache *cache;
7133 Lisp_Object *attrs;
7134 int c;
7135 struct face *base_face;
7137 struct face *face = NULL;
7138 #ifdef HAVE_WINDOW_SYSTEM
7139 struct face *default_face;
7140 struct frame *f;
7141 Lisp_Object stipple, overline, strike_through, box;
7143 xassert (FRAME_WINDOW_P (cache->f));
7144 xassert (SINGLE_BYTE_CHAR_P (c)
7145 || base_face);
7147 /* Allocate a new realized face. */
7148 face = make_realized_face (attrs);
7150 f = cache->f;
7152 /* If C is a multibyte character, we share all face attirbutes with
7153 BASE_FACE including the realized fontset. But, we must load a
7154 different font. */
7155 if (!SINGLE_BYTE_CHAR_P (c))
7157 bcopy (base_face, face, sizeof *face);
7158 face->gc = 0;
7160 /* Don't try to free the colors copied bitwise from BASE_FACE. */
7161 face->colors_copied_bitwise_p = 1;
7163 /* to force realize_face to load font */
7164 face->font = NULL;
7165 return face;
7168 /* Now we are realizing a face for ASCII (and unibyte) characters. */
7170 /* Determine the font to use. Most of the time, the font will be
7171 the same as the font of the default face, so try that first. */
7172 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7173 if (default_face
7174 && FACE_SUITABLE_FOR_CHAR_P (default_face, c)
7175 && lface_same_font_attributes_p (default_face->lface, attrs))
7177 face->font = default_face->font;
7178 face->fontset = default_face->fontset;
7179 face->font_info_id = default_face->font_info_id;
7180 face->font_name = default_face->font_name;
7181 face->ascii_face = face;
7183 /* But, as we can't share the fontset, make a new realized
7184 fontset that has the same base fontset as of the default
7185 face. */
7186 face->fontset
7187 = make_fontset_for_ascii_face (f, default_face->fontset);
7189 else
7191 /* If the face attribute ATTRS specifies a fontset, use it as
7192 the base of a new realized fontset. Otherwise, use the same
7193 base fontset as of the default face. The base determines
7194 registry and encoding of a font. It may also determine
7195 foundry and family. The other fields of font name pattern
7196 are constructed from ATTRS. */
7197 int fontset = face_fontset (attrs);
7199 if ((fontset == -1) && default_face)
7200 fontset = default_face->fontset;
7201 face->fontset = make_fontset_for_ascii_face (f, fontset);
7202 face->font = NULL; /* to force realize_face to load font */
7205 /* Load colors, and set remaining attributes. */
7207 load_face_colors (f, face, attrs);
7209 /* Set up box. */
7210 box = attrs[LFACE_BOX_INDEX];
7211 if (STRINGP (box))
7213 /* A simple box of line width 1 drawn in color given by
7214 the string. */
7215 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
7216 LFACE_BOX_INDEX);
7217 face->box = FACE_SIMPLE_BOX;
7218 face->box_line_width = 1;
7220 else if (INTEGERP (box))
7222 /* Simple box of specified line width in foreground color of the
7223 face. */
7224 xassert (XINT (box) != 0);
7225 face->box = FACE_SIMPLE_BOX;
7226 face->box_line_width = XINT (box);
7227 face->box_color = face->foreground;
7228 face->box_color_defaulted_p = 1;
7230 else if (CONSP (box))
7232 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
7233 being one of `raised' or `sunken'. */
7234 face->box = FACE_SIMPLE_BOX;
7235 face->box_color = face->foreground;
7236 face->box_color_defaulted_p = 1;
7237 face->box_line_width = 1;
7239 while (CONSP (box))
7241 Lisp_Object keyword, value;
7243 keyword = XCAR (box);
7244 box = XCDR (box);
7246 if (!CONSP (box))
7247 break;
7248 value = XCAR (box);
7249 box = XCDR (box);
7251 if (EQ (keyword, QCline_width))
7253 if (INTEGERP (value) && XINT (value) != 0)
7254 face->box_line_width = XINT (value);
7256 else if (EQ (keyword, QCcolor))
7258 if (STRINGP (value))
7260 face->box_color = load_color (f, face, value,
7261 LFACE_BOX_INDEX);
7262 face->use_box_color_for_shadows_p = 1;
7265 else if (EQ (keyword, QCstyle))
7267 if (EQ (value, Qreleased_button))
7268 face->box = FACE_RAISED_BOX;
7269 else if (EQ (value, Qpressed_button))
7270 face->box = FACE_SUNKEN_BOX;
7275 /* Text underline, overline, strike-through. */
7277 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
7279 /* Use default color (same as foreground color). */
7280 face->underline_p = 1;
7281 face->underline_defaulted_p = 1;
7282 face->underline_color = 0;
7284 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
7286 /* Use specified color. */
7287 face->underline_p = 1;
7288 face->underline_defaulted_p = 0;
7289 face->underline_color
7290 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
7291 LFACE_UNDERLINE_INDEX);
7293 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
7295 face->underline_p = 0;
7296 face->underline_defaulted_p = 0;
7297 face->underline_color = 0;
7300 overline = attrs[LFACE_OVERLINE_INDEX];
7301 if (STRINGP (overline))
7303 face->overline_color
7304 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
7305 LFACE_OVERLINE_INDEX);
7306 face->overline_p = 1;
7308 else if (EQ (overline, Qt))
7310 face->overline_color = face->foreground;
7311 face->overline_color_defaulted_p = 1;
7312 face->overline_p = 1;
7315 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
7316 if (STRINGP (strike_through))
7318 face->strike_through_color
7319 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
7320 LFACE_STRIKE_THROUGH_INDEX);
7321 face->strike_through_p = 1;
7323 else if (EQ (strike_through, Qt))
7325 face->strike_through_color = face->foreground;
7326 face->strike_through_color_defaulted_p = 1;
7327 face->strike_through_p = 1;
7330 stipple = attrs[LFACE_STIPPLE_INDEX];
7331 if (!NILP (stipple))
7332 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
7334 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
7335 #endif /* HAVE_WINDOW_SYSTEM */
7336 return face;
7340 /* Map a specified color of face FACE on frame F to a tty color index.
7341 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
7342 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
7343 default foreground/background colors. */
7345 static void
7346 map_tty_color (f, face, idx, defaulted)
7347 struct frame *f;
7348 struct face *face;
7349 enum lface_attribute_index idx;
7350 int *defaulted;
7352 Lisp_Object frame, color, def;
7353 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
7354 unsigned long default_pixel, default_other_pixel, pixel;
7356 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
7358 if (foreground_p)
7360 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7361 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7363 else
7365 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7366 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7369 XSETFRAME (frame, f);
7370 color = face->lface[idx];
7372 if (STRINGP (color)
7373 && SCHARS (color)
7374 && CONSP (Vtty_defined_color_alist)
7375 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
7376 CONSP (def)))
7378 /* Associations in tty-defined-color-alist are of the form
7379 (NAME INDEX R G B). We need the INDEX part. */
7380 pixel = XINT (XCAR (XCDR (def)));
7383 if (pixel == default_pixel && STRINGP (color))
7385 pixel = load_color (f, face, color, idx);
7387 #if defined (MSDOS) || defined (WINDOWSNT)
7388 /* If the foreground of the default face is the default color,
7389 use the foreground color defined by the frame. */
7390 #ifdef MSDOS
7391 if (FRAME_MSDOS_P (f))
7393 #endif /* MSDOS */
7394 if (pixel == default_pixel
7395 || pixel == FACE_TTY_DEFAULT_COLOR)
7397 if (foreground_p)
7398 pixel = FRAME_FOREGROUND_PIXEL (f);
7399 else
7400 pixel = FRAME_BACKGROUND_PIXEL (f);
7401 face->lface[idx] = tty_color_name (f, pixel);
7402 *defaulted = 1;
7404 else if (pixel == default_other_pixel)
7406 if (foreground_p)
7407 pixel = FRAME_BACKGROUND_PIXEL (f);
7408 else
7409 pixel = FRAME_FOREGROUND_PIXEL (f);
7410 face->lface[idx] = tty_color_name (f, pixel);
7411 *defaulted = 1;
7413 #ifdef MSDOS
7415 #endif
7416 #endif /* MSDOS or WINDOWSNT */
7419 if (foreground_p)
7420 face->foreground = pixel;
7421 else
7422 face->background = pixel;
7426 /* Realize the fully-specified face with attributes ATTRS in face
7427 cache CACHE for character C. Do it for TTY frame CACHE->f. Value is a
7428 pointer to the newly created realized face. */
7430 static struct face *
7431 realize_tty_face (cache, attrs, c)
7432 struct face_cache *cache;
7433 Lisp_Object *attrs;
7434 int c;
7436 struct face *face;
7437 int weight, slant;
7438 int face_colors_defaulted = 0;
7439 struct frame *f = cache->f;
7441 /* Frame must be a termcap frame. */
7442 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
7444 /* Allocate a new realized face. */
7445 face = make_realized_face (attrs);
7446 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
7448 /* Map face attributes to TTY appearances. We map slant to
7449 dimmed text because we want italic text to appear differently
7450 and because dimmed text is probably used infrequently. */
7451 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
7452 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
7454 if (weight > XLFD_WEIGHT_MEDIUM)
7455 face->tty_bold_p = 1;
7456 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
7457 face->tty_dim_p = 1;
7458 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
7459 face->tty_underline_p = 1;
7460 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
7461 face->tty_reverse_p = 1;
7463 /* Map color names to color indices. */
7464 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
7465 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
7467 /* Swap colors if face is inverse-video. If the colors are taken
7468 from the frame colors, they are already inverted, since the
7469 frame-creation function calls x-handle-reverse-video. */
7470 if (face->tty_reverse_p && !face_colors_defaulted)
7472 unsigned long tem = face->foreground;
7473 face->foreground = face->background;
7474 face->background = tem;
7477 if (tty_suppress_bold_inverse_default_colors_p
7478 && face->tty_bold_p
7479 && face->background == FACE_TTY_DEFAULT_FG_COLOR
7480 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
7481 face->tty_bold_p = 0;
7483 return face;
7487 DEFUN ("tty-suppress-bold-inverse-default-colors",
7488 Ftty_suppress_bold_inverse_default_colors,
7489 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
7490 doc: /* Suppress/allow boldness of faces with inverse default colors.
7491 SUPPRESS non-nil means suppress it.
7492 This affects bold faces on TTYs whose foreground is the default background
7493 color of the display and whose background is the default foreground color.
7494 For such faces, the bold face attribute is ignored if this variable
7495 is non-nil. */)
7496 (suppress)
7497 Lisp_Object suppress;
7499 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
7500 ++face_change_count;
7501 return suppress;
7506 /***********************************************************************
7507 Computing Faces
7508 ***********************************************************************/
7510 /* Return the ID of the face to use to display character CH with face
7511 property PROP on frame F in current_buffer. */
7514 compute_char_face (f, ch, prop)
7515 struct frame *f;
7516 int ch;
7517 Lisp_Object prop;
7519 int face_id;
7521 if (NILP (current_buffer->enable_multibyte_characters))
7522 ch = 0;
7524 if (NILP (prop))
7526 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7527 face_id = FACE_FOR_CHAR (f, face, ch);
7529 else
7531 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7532 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7533 bcopy (default_face->lface, attrs, sizeof attrs);
7534 merge_face_ref (f, prop, attrs, 1, 0);
7535 face_id = lookup_face (f, attrs, ch, NULL);
7538 return face_id;
7541 /* Return the face ID associated with buffer position POS for
7542 displaying ASCII characters. Return in *ENDPTR the position at
7543 which a different face is needed, as far as text properties and
7544 overlays are concerned. W is a window displaying current_buffer.
7546 REGION_BEG, REGION_END delimit the region, so it can be
7547 highlighted.
7549 LIMIT is a position not to scan beyond. That is to limit the time
7550 this function can take.
7552 If MOUSE is non-zero, use the character's mouse-face, not its face.
7554 The face returned is suitable for displaying ASCII characters. */
7557 face_at_buffer_position (w, pos, region_beg, region_end,
7558 endptr, limit, mouse)
7559 struct window *w;
7560 int pos;
7561 int region_beg, region_end;
7562 int *endptr;
7563 int limit;
7564 int mouse;
7566 struct frame *f = XFRAME (w->frame);
7567 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7568 Lisp_Object prop, position;
7569 int i, noverlays;
7570 Lisp_Object *overlay_vec;
7571 Lisp_Object frame;
7572 int endpos;
7573 Lisp_Object propname = mouse ? Qmouse_face : Qface;
7574 Lisp_Object limit1, end;
7575 struct face *default_face;
7577 /* W must display the current buffer. We could write this function
7578 to use the frame and buffer of W, but right now it doesn't. */
7579 /* xassert (XBUFFER (w->buffer) == current_buffer); */
7581 XSETFRAME (frame, f);
7582 XSETFASTINT (position, pos);
7584 endpos = ZV;
7585 if (pos < region_beg && region_beg < endpos)
7586 endpos = region_beg;
7588 /* Get the `face' or `mouse_face' text property at POS, and
7589 determine the next position at which the property changes. */
7590 prop = Fget_text_property (position, propname, w->buffer);
7591 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
7592 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
7593 if (INTEGERP (end))
7594 endpos = XINT (end);
7596 /* Look at properties from overlays. */
7598 int next_overlay;
7600 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
7601 if (next_overlay < endpos)
7602 endpos = next_overlay;
7605 *endptr = endpos;
7607 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7609 /* Optimize common cases where we can use the default face. */
7610 if (noverlays == 0
7611 && NILP (prop)
7612 && !(pos >= region_beg && pos < region_end))
7613 return DEFAULT_FACE_ID;
7615 /* Begin with attributes from the default face. */
7616 bcopy (default_face->lface, attrs, sizeof attrs);
7618 /* Merge in attributes specified via text properties. */
7619 if (!NILP (prop))
7620 merge_face_ref (f, prop, attrs, 1, 0);
7622 /* Now merge the overlay data. */
7623 noverlays = sort_overlays (overlay_vec, noverlays, w);
7624 for (i = 0; i < noverlays; i++)
7626 Lisp_Object oend;
7627 int oendpos;
7629 prop = Foverlay_get (overlay_vec[i], propname);
7630 if (!NILP (prop))
7631 merge_face_ref (f, prop, attrs, 1, 0);
7633 oend = OVERLAY_END (overlay_vec[i]);
7634 oendpos = OVERLAY_POSITION (oend);
7635 if (oendpos < endpos)
7636 endpos = oendpos;
7639 /* If in the region, merge in the region face. */
7640 if (pos >= region_beg && pos < region_end)
7642 merge_named_face (f, Qregion, attrs, 0);
7644 if (region_end < endpos)
7645 endpos = region_end;
7648 *endptr = endpos;
7650 /* Look up a realized face with the given face attributes,
7651 or realize a new one for ASCII characters. */
7652 return lookup_face (f, attrs, 0, NULL);
7656 /* Compute the face at character position POS in Lisp string STRING on
7657 window W, for ASCII characters.
7659 If STRING is an overlay string, it comes from position BUFPOS in
7660 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
7661 not an overlay string. W must display the current buffer.
7662 REGION_BEG and REGION_END give the start and end positions of the
7663 region; both are -1 if no region is visible.
7665 BASE_FACE_ID is the id of a face to merge with. For strings coming
7666 from overlays or the `display' property it is the face at BUFPOS.
7668 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
7670 Set *ENDPTR to the next position where to check for faces in
7671 STRING; -1 if the face is constant from POS to the end of the
7672 string.
7674 Value is the id of the face to use. The face returned is suitable
7675 for displaying ASCII characters. */
7678 face_at_string_position (w, string, pos, bufpos, region_beg,
7679 region_end, endptr, base_face_id, mouse_p)
7680 struct window *w;
7681 Lisp_Object string;
7682 int pos, bufpos;
7683 int region_beg, region_end;
7684 int *endptr;
7685 enum face_id base_face_id;
7686 int mouse_p;
7688 Lisp_Object prop, position, end, limit;
7689 struct frame *f = XFRAME (WINDOW_FRAME (w));
7690 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7691 struct face *base_face;
7692 int multibyte_p = STRING_MULTIBYTE (string);
7693 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
7695 /* Get the value of the face property at the current position within
7696 STRING. Value is nil if there is no face property. */
7697 XSETFASTINT (position, pos);
7698 prop = Fget_text_property (position, prop_name, string);
7700 /* Get the next position at which to check for faces. Value of end
7701 is nil if face is constant all the way to the end of the string.
7702 Otherwise it is a string position where to check faces next.
7703 Limit is the maximum position up to which to check for property
7704 changes in Fnext_single_property_change. Strings are usually
7705 short, so set the limit to the end of the string. */
7706 XSETFASTINT (limit, SCHARS (string));
7707 end = Fnext_single_property_change (position, prop_name, string, limit);
7708 if (INTEGERP (end))
7709 *endptr = XFASTINT (end);
7710 else
7711 *endptr = -1;
7713 base_face = FACE_FROM_ID (f, base_face_id);
7714 xassert (base_face);
7716 /* Optimize the default case that there is no face property and we
7717 are not in the region. */
7718 if (NILP (prop)
7719 && (base_face_id != DEFAULT_FACE_ID
7720 /* BUFPOS <= 0 means STRING is not an overlay string, so
7721 that the region doesn't have to be taken into account. */
7722 || bufpos <= 0
7723 || bufpos < region_beg
7724 || bufpos >= region_end)
7725 && (multibyte_p
7726 /* We can't realize faces for different charsets differently
7727 if we don't have fonts, so we can stop here if not working
7728 on a window-system frame. */
7729 || !FRAME_WINDOW_P (f)
7730 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
7731 return base_face->id;
7733 /* Begin with attributes from the base face. */
7734 bcopy (base_face->lface, attrs, sizeof attrs);
7736 /* Merge in attributes specified via text properties. */
7737 if (!NILP (prop))
7738 merge_face_ref (f, prop, attrs, 1, 0);
7740 /* If in the region, merge in the region face. */
7741 if (bufpos
7742 && bufpos >= region_beg
7743 && bufpos < region_end)
7744 merge_named_face (f, Qregion, attrs, 0);
7746 /* Look up a realized face with the given face attributes,
7747 or realize a new one for ASCII characters. */
7748 return lookup_face (f, attrs, 0, NULL);
7752 /* Merge a face into a realized face.
7754 F is frame where faces are (to be) realized.
7756 FACE_NAME is named face to merge.
7758 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
7760 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
7762 BASE_FACE_ID is realized face to merge into.
7764 Return new face id.
7768 merge_faces (f, face_name, face_id, base_face_id)
7769 struct frame *f;
7770 Lisp_Object face_name;
7771 int face_id, base_face_id;
7773 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7774 struct face *base_face;
7776 base_face = FACE_FROM_ID (f, base_face_id);
7777 if (!base_face)
7778 return base_face_id;
7780 if (EQ (face_name, Qt))
7782 if (face_id < 0 || face_id >= lface_id_to_name_size)
7783 return base_face_id;
7784 face_name = lface_id_to_name[face_id];
7785 face_id = lookup_derived_face (f, face_name, 0, base_face_id, 1);
7786 if (face_id >= 0)
7787 return face_id;
7788 return base_face_id;
7791 /* Begin with attributes from the base face. */
7792 bcopy (base_face->lface, attrs, sizeof attrs);
7794 if (!NILP (face_name))
7796 if (!merge_named_face (f, face_name, attrs, 0))
7797 return base_face_id;
7799 else
7801 struct face *face;
7802 if (face_id < 0)
7803 return base_face_id;
7804 face = FACE_FROM_ID (f, face_id);
7805 if (!face)
7806 return base_face_id;
7807 merge_face_vectors (f, face->lface, attrs, 0);
7810 /* Look up a realized face with the given face attributes,
7811 or realize a new one for ASCII characters. */
7812 return lookup_face (f, attrs, 0, NULL);
7816 /***********************************************************************
7817 Tests
7818 ***********************************************************************/
7820 #if GLYPH_DEBUG
7822 /* Print the contents of the realized face FACE to stderr. */
7824 static void
7825 dump_realized_face (face)
7826 struct face *face;
7828 fprintf (stderr, "ID: %d\n", face->id);
7829 #ifdef HAVE_X_WINDOWS
7830 fprintf (stderr, "gc: %ld\n", (long) face->gc);
7831 #endif
7832 fprintf (stderr, "foreground: 0x%lx (%s)\n",
7833 face->foreground,
7834 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
7835 fprintf (stderr, "background: 0x%lx (%s)\n",
7836 face->background,
7837 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
7838 fprintf (stderr, "font_name: %s (%s)\n",
7839 face->font_name,
7840 SDATA (face->lface[LFACE_FAMILY_INDEX]));
7841 #ifdef HAVE_X_WINDOWS
7842 fprintf (stderr, "font = %p\n", face->font);
7843 #endif
7844 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
7845 fprintf (stderr, "fontset: %d\n", face->fontset);
7846 fprintf (stderr, "underline: %d (%s)\n",
7847 face->underline_p,
7848 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
7849 fprintf (stderr, "hash: %d\n", face->hash);
7850 fprintf (stderr, "charset: %d\n", face->charset);
7854 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
7856 Lisp_Object n;
7858 if (NILP (n))
7860 int i;
7862 fprintf (stderr, "font selection order: ");
7863 for (i = 0; i < DIM (font_sort_order); ++i)
7864 fprintf (stderr, "%d ", font_sort_order[i]);
7865 fprintf (stderr, "\n");
7867 fprintf (stderr, "alternative fonts: ");
7868 debug_print (Vface_alternative_font_family_alist);
7869 fprintf (stderr, "\n");
7871 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
7872 Fdump_face (make_number (i));
7874 else
7876 struct face *face;
7877 CHECK_NUMBER (n);
7878 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
7879 if (face == NULL)
7880 error ("Not a valid face");
7881 dump_realized_face (face);
7884 return Qnil;
7888 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
7889 0, 0, 0, doc: /* */)
7892 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
7893 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
7894 fprintf (stderr, "number of GCs = %d\n", ngcs);
7895 return Qnil;
7898 #endif /* GLYPH_DEBUG != 0 */
7902 /***********************************************************************
7903 Initialization
7904 ***********************************************************************/
7906 void
7907 syms_of_xfaces ()
7909 Qface = intern ("face");
7910 staticpro (&Qface);
7911 Qface_no_inherit = intern ("face-no-inherit");
7912 staticpro (&Qface_no_inherit);
7913 Qbitmap_spec_p = intern ("bitmap-spec-p");
7914 staticpro (&Qbitmap_spec_p);
7915 Qframe_set_background_mode = intern ("frame-set-background-mode");
7916 staticpro (&Qframe_set_background_mode);
7918 /* Lisp face attribute keywords. */
7919 QCfamily = intern (":family");
7920 staticpro (&QCfamily);
7921 QCheight = intern (":height");
7922 staticpro (&QCheight);
7923 QCweight = intern (":weight");
7924 staticpro (&QCweight);
7925 QCslant = intern (":slant");
7926 staticpro (&QCslant);
7927 QCunderline = intern (":underline");
7928 staticpro (&QCunderline);
7929 QCinverse_video = intern (":inverse-video");
7930 staticpro (&QCinverse_video);
7931 QCreverse_video = intern (":reverse-video");
7932 staticpro (&QCreverse_video);
7933 QCforeground = intern (":foreground");
7934 staticpro (&QCforeground);
7935 QCbackground = intern (":background");
7936 staticpro (&QCbackground);
7937 QCstipple = intern (":stipple");;
7938 staticpro (&QCstipple);
7939 QCwidth = intern (":width");
7940 staticpro (&QCwidth);
7941 QCfont = intern (":font");
7942 staticpro (&QCfont);
7943 QCbold = intern (":bold");
7944 staticpro (&QCbold);
7945 QCitalic = intern (":italic");
7946 staticpro (&QCitalic);
7947 QCoverline = intern (":overline");
7948 staticpro (&QCoverline);
7949 QCstrike_through = intern (":strike-through");
7950 staticpro (&QCstrike_through);
7951 QCbox = intern (":box");
7952 staticpro (&QCbox);
7953 QCinherit = intern (":inherit");
7954 staticpro (&QCinherit);
7956 /* Symbols used for Lisp face attribute values. */
7957 QCcolor = intern (":color");
7958 staticpro (&QCcolor);
7959 QCline_width = intern (":line-width");
7960 staticpro (&QCline_width);
7961 QCstyle = intern (":style");
7962 staticpro (&QCstyle);
7963 Qreleased_button = intern ("released-button");
7964 staticpro (&Qreleased_button);
7965 Qpressed_button = intern ("pressed-button");
7966 staticpro (&Qpressed_button);
7967 Qnormal = intern ("normal");
7968 staticpro (&Qnormal);
7969 Qultra_light = intern ("ultra-light");
7970 staticpro (&Qultra_light);
7971 Qextra_light = intern ("extra-light");
7972 staticpro (&Qextra_light);
7973 Qlight = intern ("light");
7974 staticpro (&Qlight);
7975 Qsemi_light = intern ("semi-light");
7976 staticpro (&Qsemi_light);
7977 Qsemi_bold = intern ("semi-bold");
7978 staticpro (&Qsemi_bold);
7979 Qbold = intern ("bold");
7980 staticpro (&Qbold);
7981 Qextra_bold = intern ("extra-bold");
7982 staticpro (&Qextra_bold);
7983 Qultra_bold = intern ("ultra-bold");
7984 staticpro (&Qultra_bold);
7985 Qoblique = intern ("oblique");
7986 staticpro (&Qoblique);
7987 Qitalic = intern ("italic");
7988 staticpro (&Qitalic);
7989 Qreverse_oblique = intern ("reverse-oblique");
7990 staticpro (&Qreverse_oblique);
7991 Qreverse_italic = intern ("reverse-italic");
7992 staticpro (&Qreverse_italic);
7993 Qultra_condensed = intern ("ultra-condensed");
7994 staticpro (&Qultra_condensed);
7995 Qextra_condensed = intern ("extra-condensed");
7996 staticpro (&Qextra_condensed);
7997 Qcondensed = intern ("condensed");
7998 staticpro (&Qcondensed);
7999 Qsemi_condensed = intern ("semi-condensed");
8000 staticpro (&Qsemi_condensed);
8001 Qsemi_expanded = intern ("semi-expanded");
8002 staticpro (&Qsemi_expanded);
8003 Qexpanded = intern ("expanded");
8004 staticpro (&Qexpanded);
8005 Qextra_expanded = intern ("extra-expanded");
8006 staticpro (&Qextra_expanded);
8007 Qultra_expanded = intern ("ultra-expanded");
8008 staticpro (&Qultra_expanded);
8009 Qbackground_color = intern ("background-color");
8010 staticpro (&Qbackground_color);
8011 Qforeground_color = intern ("foreground-color");
8012 staticpro (&Qforeground_color);
8013 Qunspecified = intern ("unspecified");
8014 staticpro (&Qunspecified);
8016 Qface_alias = intern ("face-alias");
8017 staticpro (&Qface_alias);
8018 Qdefault = intern ("default");
8019 staticpro (&Qdefault);
8020 Qtool_bar = intern ("tool-bar");
8021 staticpro (&Qtool_bar);
8022 Qregion = intern ("region");
8023 staticpro (&Qregion);
8024 Qfringe = intern ("fringe");
8025 staticpro (&Qfringe);
8026 Qheader_line = intern ("header-line");
8027 staticpro (&Qheader_line);
8028 Qscroll_bar = intern ("scroll-bar");
8029 staticpro (&Qscroll_bar);
8030 Qmenu = intern ("menu");
8031 staticpro (&Qmenu);
8032 Qcursor = intern ("cursor");
8033 staticpro (&Qcursor);
8034 Qborder = intern ("border");
8035 staticpro (&Qborder);
8036 Qmouse = intern ("mouse");
8037 staticpro (&Qmouse);
8038 Qmode_line_inactive = intern ("mode-line-inactive");
8039 staticpro (&Qmode_line_inactive);
8040 Qvertical_border = intern ("vertical-border");
8041 staticpro (&Qvertical_border);
8042 Qtty_color_desc = intern ("tty-color-desc");
8043 staticpro (&Qtty_color_desc);
8044 Qtty_color_standard_values = intern ("tty-color-standard-values");
8045 staticpro (&Qtty_color_standard_values);
8046 Qtty_color_by_index = intern ("tty-color-by-index");
8047 staticpro (&Qtty_color_by_index);
8048 Qtty_color_alist = intern ("tty-color-alist");
8049 staticpro (&Qtty_color_alist);
8050 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
8051 staticpro (&Qscalable_fonts_allowed);
8053 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
8054 staticpro (&Vparam_value_alist);
8055 Vface_alternative_font_family_alist = Qnil;
8056 staticpro (&Vface_alternative_font_family_alist);
8057 Vface_alternative_font_registry_alist = Qnil;
8058 staticpro (&Vface_alternative_font_registry_alist);
8060 defsubr (&Sinternal_make_lisp_face);
8061 defsubr (&Sinternal_lisp_face_p);
8062 defsubr (&Sinternal_set_lisp_face_attribute);
8063 #ifdef HAVE_WINDOW_SYSTEM
8064 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
8065 #endif
8066 defsubr (&Scolor_gray_p);
8067 defsubr (&Scolor_supported_p);
8068 defsubr (&Sface_attribute_relative_p);
8069 defsubr (&Smerge_face_attribute);
8070 defsubr (&Sinternal_get_lisp_face_attribute);
8071 defsubr (&Sinternal_lisp_face_attribute_values);
8072 defsubr (&Sinternal_lisp_face_equal_p);
8073 defsubr (&Sinternal_lisp_face_empty_p);
8074 defsubr (&Sinternal_copy_lisp_face);
8075 defsubr (&Sinternal_merge_in_global_face);
8076 defsubr (&Sface_font);
8077 defsubr (&Sframe_face_alist);
8078 defsubr (&Sdisplay_supports_face_attributes_p);
8079 defsubr (&Scolor_distance);
8080 defsubr (&Sinternal_set_font_selection_order);
8081 defsubr (&Sinternal_set_alternative_font_family_alist);
8082 defsubr (&Sinternal_set_alternative_font_registry_alist);
8083 defsubr (&Sface_attributes_as_vector);
8084 #if GLYPH_DEBUG
8085 defsubr (&Sdump_face);
8086 defsubr (&Sshow_face_resources);
8087 #endif /* GLYPH_DEBUG */
8088 defsubr (&Sclear_face_cache);
8089 defsubr (&Stty_suppress_bold_inverse_default_colors);
8091 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
8092 defsubr (&Sdump_colors);
8093 #endif
8095 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
8096 doc: /* *Limit for font matching.
8097 If an integer > 0, font matching functions won't load more than
8098 that number of fonts when searching for a matching font. */);
8099 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
8101 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
8102 doc: /* List of global face definitions (for internal use only.) */);
8103 Vface_new_frame_defaults = Qnil;
8105 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
8106 doc: /* *Default stipple pattern used on monochrome displays.
8107 This stipple pattern is used on monochrome displays
8108 instead of shades of gray for a face background color.
8109 See `set-face-stipple' for possible values for this variable. */);
8110 Vface_default_stipple = build_string ("gray3");
8112 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
8113 doc: /* An alist of defined terminal colors and their RGB values. */);
8114 Vtty_defined_color_alist = Qnil;
8116 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
8117 doc: /* Allowed scalable fonts.
8118 A value of nil means don't allow any scalable fonts.
8119 A value of t means allow any scalable font.
8120 Otherwise, value must be a list of regular expressions. A font may be
8121 scaled if its name matches a regular expression in the list.
8122 Note that if value is nil, a scalable font might still be used, if no
8123 other font of the appropriate family and registry is available. */);
8124 Vscalable_fonts_allowed = Qnil;
8126 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
8127 doc: /* List of ignored fonts.
8128 Each element is a regular expression that matches names of fonts to
8129 ignore. */);
8130 Vface_ignored_fonts = Qnil;
8132 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
8133 doc: /* Alist of fonts vs the rescaling factors.
8134 Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
8135 FONT-NAME-PATTERN is a regular expression matching a font name, and
8136 RESCALE-RATIO is a floating point number to specify how much larger
8137 \(or smaller) font we should use. For instance, if a face requests
8138 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
8139 Vface_font_rescale_alist = Qnil;
8141 #ifdef HAVE_WINDOW_SYSTEM
8142 defsubr (&Sbitmap_spec_p);
8143 defsubr (&Sx_list_fonts);
8144 defsubr (&Sinternal_face_x_get_resource);
8145 defsubr (&Sx_family_fonts);
8146 defsubr (&Sx_font_family_list);
8147 #endif /* HAVE_WINDOW_SYSTEM */
8150 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
8151 (do not change this comment) */