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