Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-549
[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, gcpro)
3436 Lisp_Object from, to, invalid, gcpro;
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 struct gcpro gcpro1;
3462
3463 GCPRO1 (gcpro);
3464
3465 args[0] = from;
3466 args[1] = to;
3467 result = safe_call (2, args);
3468
3469 UNGCPRO;
3470
3471 /* Ensure that if TO was absolute, so is the result. */
3472 if (INTEGERP (to) && !INTEGERP (result))
3473 result = invalid;
3474 }
3475
3476 return result;
3477 }
3478
3479
3480 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
3481 store the resulting attributes in TO, which must be already be
3482 completely specified and contain only absolute attributes. Every
3483 specified attribute of FROM overrides the corresponding attribute of
3484 TO; relative attributes in FROM are merged with the absolute value in
3485 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
3486 loops in face inheritance; it should be 0 when called from other
3487 places. */
3488
3489 static INLINE void
3490 merge_face_vectors (f, from, to, named_merge_points)
3491 struct frame *f;
3492 Lisp_Object *from, *to;
3493 struct named_merge_point *named_merge_points;
3494 {
3495 int i;
3496
3497 /* If FROM inherits from some other faces, merge their attributes into
3498 TO before merging FROM's direct attributes. Note that an :inherit
3499 attribute of `unspecified' is the same as one of nil; we never
3500 merge :inherit attributes, so nil is more correct, but lots of
3501 other code uses `unspecified' as a generic value for face attributes. */
3502 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
3503 && !NILP (from[LFACE_INHERIT_INDEX]))
3504 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
3505
3506 /* If TO specifies a :font attribute, and FROM specifies some
3507 font-related attribute, we need to clear TO's :font attribute
3508 (because it will be inconsistent with whatever FROM specifies, and
3509 FROM takes precedence). */
3510 if (!NILP (to[LFACE_FONT_INDEX])
3511 && (!UNSPECIFIEDP (from[LFACE_FAMILY_INDEX])
3512 || !UNSPECIFIEDP (from[LFACE_HEIGHT_INDEX])
3513 || !UNSPECIFIEDP (from[LFACE_WEIGHT_INDEX])
3514 || !UNSPECIFIEDP (from[LFACE_SLANT_INDEX])
3515 || !UNSPECIFIEDP (from[LFACE_SWIDTH_INDEX])
3516 || !UNSPECIFIEDP (from[LFACE_AVGWIDTH_INDEX])))
3517 to[LFACE_FONT_INDEX] = Qnil;
3518
3519 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3520 if (!UNSPECIFIEDP (from[i]))
3521 {
3522 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
3523 to[i] = merge_face_heights (from[i], to[i], to[i],
3524 named_merge_points);
3525 else
3526 to[i] = from[i];
3527 }
3528
3529 /* TO is always an absolute face, which should inherit from nothing.
3530 We blindly copy the :inherit attribute above and fix it up here. */
3531 to[LFACE_INHERIT_INDEX] = Qnil;
3532 }
3533
3534 /* Merge the named face FACE_NAME on frame F, into the vector of face
3535 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
3536 inheritance. Returns true if FACE_NAME is a valid face name and
3537 merging succeeded. */
3538
3539 static int
3540 merge_named_face (f, face_name, to, named_merge_points)
3541 struct frame *f;
3542 Lisp_Object face_name;
3543 Lisp_Object *to;
3544 struct named_merge_point *named_merge_points;
3545 {
3546 struct named_merge_point named_merge_point;
3547
3548 if (push_named_merge_point (&named_merge_point,
3549 face_name, &named_merge_points))
3550 {
3551 Lisp_Object from[LFACE_VECTOR_SIZE];
3552 int ok = get_lface_attributes (f, face_name, from, 0);
3553
3554 if (ok)
3555 merge_face_vectors (f, from, to, named_merge_points);
3556
3557 return ok;
3558 }
3559 else
3560 return 0;
3561 }
3562
3563
3564 /* Merge face attributes from the lisp `face reference' FACE_REF on
3565 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
3566 problems with FACE_REF cause an error message to be shown. Return
3567 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
3568 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
3569 list structure; it may be 0 for most callers.
3570
3571 FACE_REF may be a single face specification or a list of such
3572 specifications. Each face specification can be:
3573
3574 1. A symbol or string naming a Lisp face.
3575
3576 2. A property list of the form (KEYWORD VALUE ...) where each
3577 KEYWORD is a face attribute name, and value is an appropriate value
3578 for that attribute.
3579
3580 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
3581 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
3582 for compatibility with 20.2.
3583
3584 Face specifications earlier in lists take precedence over later
3585 specifications. */
3586
3587 static int
3588 merge_face_ref (f, face_ref, to, err_msgs, named_merge_points)
3589 struct frame *f;
3590 Lisp_Object face_ref;
3591 Lisp_Object *to;
3592 int err_msgs;
3593 struct named_merge_point *named_merge_points;
3594 {
3595 int ok = 1; /* Succeed without an error? */
3596
3597 if (CONSP (face_ref))
3598 {
3599 Lisp_Object first = XCAR (face_ref);
3600
3601 if (EQ (first, Qforeground_color)
3602 || EQ (first, Qbackground_color))
3603 {
3604 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
3605 . COLOR). COLOR must be a string. */
3606 Lisp_Object color_name = XCDR (face_ref);
3607 Lisp_Object color = first;
3608
3609 if (STRINGP (color_name))
3610 {
3611 if (EQ (color, Qforeground_color))
3612 to[LFACE_FOREGROUND_INDEX] = color_name;
3613 else
3614 to[LFACE_BACKGROUND_INDEX] = color_name;
3615 }
3616 else
3617 {
3618 if (err_msgs)
3619 add_to_log ("Invalid face color", color_name, Qnil);
3620 ok = 0;
3621 }
3622 }
3623 else if (SYMBOLP (first)
3624 && *SDATA (SYMBOL_NAME (first)) == ':')
3625 {
3626 /* Assume this is the property list form. */
3627 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
3628 {
3629 Lisp_Object keyword = XCAR (face_ref);
3630 Lisp_Object value = XCAR (XCDR (face_ref));
3631 int err = 0;
3632
3633 /* Specifying `unspecified' is a no-op. */
3634 if (EQ (value, Qunspecified))
3635 ;
3636 else if (EQ (keyword, QCfamily))
3637 {
3638 if (STRINGP (value))
3639 to[LFACE_FAMILY_INDEX] = value;
3640 else
3641 err = 1;
3642 }
3643 else if (EQ (keyword, QCheight))
3644 {
3645 Lisp_Object new_height =
3646 merge_face_heights (value, to[LFACE_HEIGHT_INDEX],
3647 Qnil, Qnil);
3648
3649 if (! NILP (new_height))
3650 to[LFACE_HEIGHT_INDEX] = new_height;
3651 else
3652 err = 1;
3653 }
3654 else if (EQ (keyword, QCweight))
3655 {
3656 if (SYMBOLP (value)
3657 && face_numeric_weight (value) >= 0)
3658 to[LFACE_WEIGHT_INDEX] = value;
3659 else
3660 err = 1;
3661 }
3662 else if (EQ (keyword, QCslant))
3663 {
3664 if (SYMBOLP (value)
3665 && face_numeric_slant (value) >= 0)
3666 to[LFACE_SLANT_INDEX] = value;
3667 else
3668 err = 1;
3669 }
3670 else if (EQ (keyword, QCunderline))
3671 {
3672 if (EQ (value, Qt)
3673 || NILP (value)
3674 || STRINGP (value))
3675 to[LFACE_UNDERLINE_INDEX] = value;
3676 else
3677 err = 1;
3678 }
3679 else if (EQ (keyword, QCoverline))
3680 {
3681 if (EQ (value, Qt)
3682 || NILP (value)
3683 || STRINGP (value))
3684 to[LFACE_OVERLINE_INDEX] = value;
3685 else
3686 err = 1;
3687 }
3688 else if (EQ (keyword, QCstrike_through))
3689 {
3690 if (EQ (value, Qt)
3691 || NILP (value)
3692 || STRINGP (value))
3693 to[LFACE_STRIKE_THROUGH_INDEX] = value;
3694 else
3695 err = 1;
3696 }
3697 else if (EQ (keyword, QCbox))
3698 {
3699 if (EQ (value, Qt))
3700 value = make_number (1);
3701 if (INTEGERP (value)
3702 || STRINGP (value)
3703 || CONSP (value)
3704 || NILP (value))
3705 to[LFACE_BOX_INDEX] = value;
3706 else
3707 err = 1;
3708 }
3709 else if (EQ (keyword, QCinverse_video)
3710 || EQ (keyword, QCreverse_video))
3711 {
3712 if (EQ (value, Qt) || NILP (value))
3713 to[LFACE_INVERSE_INDEX] = value;
3714 else
3715 err = 1;
3716 }
3717 else if (EQ (keyword, QCforeground))
3718 {
3719 if (STRINGP (value))
3720 to[LFACE_FOREGROUND_INDEX] = value;
3721 else
3722 err = 1;
3723 }
3724 else if (EQ (keyword, QCbackground))
3725 {
3726 if (STRINGP (value))
3727 to[LFACE_BACKGROUND_INDEX] = value;
3728 else
3729 err = 1;
3730 }
3731 else if (EQ (keyword, QCstipple))
3732 {
3733 #ifdef HAVE_X_WINDOWS
3734 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
3735 if (!NILP (pixmap_p))
3736 to[LFACE_STIPPLE_INDEX] = value;
3737 else
3738 err = 1;
3739 #endif
3740 }
3741 else if (EQ (keyword, QCwidth))
3742 {
3743 if (SYMBOLP (value)
3744 && face_numeric_swidth (value) >= 0)
3745 to[LFACE_SWIDTH_INDEX] = value;
3746 else
3747 err = 1;
3748 }
3749 else if (EQ (keyword, QCinherit))
3750 {
3751 /* This is not really very useful; it's just like a
3752 normal face reference. */
3753 if (! merge_face_ref (f, value, to,
3754 err_msgs, named_merge_points))
3755 err = 1;
3756 }
3757 else
3758 err = 1;
3759
3760 if (err)
3761 {
3762 add_to_log ("Invalid face attribute %S %S", keyword, value);
3763 ok = 0;
3764 }
3765
3766 face_ref = XCDR (XCDR (face_ref));
3767 }
3768 }
3769 else
3770 {
3771 /* This is a list of face refs. Those at the beginning of the
3772 list take precedence over what follows, so we have to merge
3773 from the end backwards. */
3774 Lisp_Object next = XCDR (face_ref);
3775
3776 if (! NILP (next))
3777 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
3778
3779 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
3780 ok = 0;
3781 }
3782 }
3783 else
3784 {
3785 /* FACE_REF ought to be a face name. */
3786 ok = merge_named_face (f, face_ref, to, named_merge_points);
3787 if (!ok && err_msgs)
3788 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
3789 }
3790
3791 return ok;
3792 }
3793
3794
3795 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
3796 Sinternal_make_lisp_face, 1, 2, 0,
3797 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
3798 If FACE was not known as a face before, create a new one.
3799 If optional argument FRAME is specified, make a frame-local face
3800 for that frame. Otherwise operate on the global face definition.
3801 Value is a vector of face attributes. */)
3802 (face, frame)
3803 Lisp_Object face, frame;
3804 {
3805 Lisp_Object global_lface, lface;
3806 struct frame *f;
3807 int i;
3808
3809 CHECK_SYMBOL (face);
3810 global_lface = lface_from_face_name (NULL, face, 0);
3811
3812 if (!NILP (frame))
3813 {
3814 CHECK_LIVE_FRAME (frame);
3815 f = XFRAME (frame);
3816 lface = lface_from_face_name (f, face, 0);
3817 }
3818 else
3819 f = NULL, lface = Qnil;
3820
3821 /* Add a global definition if there is none. */
3822 if (NILP (global_lface))
3823 {
3824 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3825 Qunspecified);
3826 AREF (global_lface, 0) = Qface;
3827 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
3828 Vface_new_frame_defaults);
3829
3830 /* Assign the new Lisp face a unique ID. The mapping from Lisp
3831 face id to Lisp face is given by the vector lface_id_to_name.
3832 The mapping from Lisp face to Lisp face id is given by the
3833 property `face' of the Lisp face name. */
3834 if (next_lface_id == lface_id_to_name_size)
3835 {
3836 int new_size = max (50, 2 * lface_id_to_name_size);
3837 int sz = new_size * sizeof *lface_id_to_name;
3838 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
3839 lface_id_to_name_size = new_size;
3840 }
3841
3842 lface_id_to_name[next_lface_id] = face;
3843 Fput (face, Qface, make_number (next_lface_id));
3844 ++next_lface_id;
3845 }
3846 else if (f == NULL)
3847 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3848 AREF (global_lface, i) = Qunspecified;
3849
3850 /* Add a frame-local definition. */
3851 if (f)
3852 {
3853 if (NILP (lface))
3854 {
3855 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
3856 Qunspecified);
3857 AREF (lface, 0) = Qface;
3858 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
3859 }
3860 else
3861 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3862 AREF (lface, i) = Qunspecified;
3863 }
3864 else
3865 lface = global_lface;
3866
3867 /* Changing a named face means that all realized faces depending on
3868 that face are invalid. Since we cannot tell which realized faces
3869 depend on the face, make sure they are all removed. This is done
3870 by incrementing face_change_count. The next call to
3871 init_iterator will then free realized faces. */
3872 if (NILP (Fget (face, Qface_no_inherit)))
3873 {
3874 ++face_change_count;
3875 ++windows_or_buffers_changed;
3876 }
3877
3878 xassert (LFACEP (lface));
3879 check_lface (lface);
3880 return lface;
3881 }
3882
3883
3884 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
3885 Sinternal_lisp_face_p, 1, 2, 0,
3886 doc: /* Return non-nil if FACE names a face.
3887 If optional second parameter FRAME is non-nil, check for the
3888 existence of a frame-local face with name FACE on that frame.
3889 Otherwise check for the existence of a global face. */)
3890 (face, frame)
3891 Lisp_Object face, frame;
3892 {
3893 Lisp_Object lface;
3894
3895 if (!NILP (frame))
3896 {
3897 CHECK_LIVE_FRAME (frame);
3898 lface = lface_from_face_name (XFRAME (frame), face, 0);
3899 }
3900 else
3901 lface = lface_from_face_name (NULL, face, 0);
3902
3903 return lface;
3904 }
3905
3906
3907 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
3908 Sinternal_copy_lisp_face, 4, 4, 0,
3909 doc: /* Copy face FROM to TO.
3910 If FRAME is t, copy the global face definition of FROM.
3911 Otherwise, copy the frame-local definition of FROM on FRAME.
3912 If NEW-FRAME is a frame, copy that data into the frame-local
3913 definition of TO on NEW-FRAME. If NEW-FRAME is nil.
3914 FRAME controls where the data is copied to.
3915
3916 The value is TO. */)
3917 (from, to, frame, new_frame)
3918 Lisp_Object from, to, frame, new_frame;
3919 {
3920 Lisp_Object lface, copy;
3921
3922 CHECK_SYMBOL (from);
3923 CHECK_SYMBOL (to);
3924
3925 if (EQ (frame, Qt))
3926 {
3927 /* Copy global definition of FROM. We don't make copies of
3928 strings etc. because 20.2 didn't do it either. */
3929 lface = lface_from_face_name (NULL, from, 1);
3930 copy = Finternal_make_lisp_face (to, Qnil);
3931 }
3932 else
3933 {
3934 /* Copy frame-local definition of FROM. */
3935 if (NILP (new_frame))
3936 new_frame = frame;
3937 CHECK_LIVE_FRAME (frame);
3938 CHECK_LIVE_FRAME (new_frame);
3939 lface = lface_from_face_name (XFRAME (frame), from, 1);
3940 copy = Finternal_make_lisp_face (to, new_frame);
3941 }
3942
3943 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3944 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
3945
3946 /* Changing a named face means that all realized faces depending on
3947 that face are invalid. Since we cannot tell which realized faces
3948 depend on the face, make sure they are all removed. This is done
3949 by incrementing face_change_count. The next call to
3950 init_iterator will then free realized faces. */
3951 if (NILP (Fget (to, Qface_no_inherit)))
3952 {
3953 ++face_change_count;
3954 ++windows_or_buffers_changed;
3955 }
3956
3957 return to;
3958 }
3959
3960
3961 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3962 Sinternal_set_lisp_face_attribute, 3, 4, 0,
3963 doc: /* Set attribute ATTR of FACE to VALUE.
3964 FRAME being a frame means change the face on that frame.
3965 FRAME nil means change the face of the selected frame.
3966 FRAME t means change the default for new frames.
3967 FRAME 0 means change the face on all frames, and change the default
3968 for new frames. */)
3969 (face, attr, value, frame)
3970 Lisp_Object face, attr, value, frame;
3971 {
3972 Lisp_Object lface;
3973 Lisp_Object old_value = Qnil;
3974 /* Set 1 if ATTR is QCfont. */
3975 int font_attr_p = 0;
3976 /* Set 1 if ATTR is one of font-related attributes other than QCfont. */
3977 int font_related_attr_p = 0;
3978
3979 CHECK_SYMBOL (face);
3980 CHECK_SYMBOL (attr);
3981
3982 face = resolve_face_name (face);
3983
3984 /* If FRAME is 0, change face on all frames, and change the
3985 default for new frames. */
3986 if (INTEGERP (frame) && XINT (frame) == 0)
3987 {
3988 Lisp_Object tail;
3989 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
3990 FOR_EACH_FRAME (tail, frame)
3991 Finternal_set_lisp_face_attribute (face, attr, value, frame);
3992 return face;
3993 }
3994
3995 /* Set lface to the Lisp attribute vector of FACE. */
3996 if (EQ (frame, Qt))
3997 lface = lface_from_face_name (NULL, face, 1);
3998 else
3999 {
4000 if (NILP (frame))
4001 frame = selected_frame;
4002
4003 CHECK_LIVE_FRAME (frame);
4004 lface = lface_from_face_name (XFRAME (frame), face, 0);
4005
4006 /* If a frame-local face doesn't exist yet, create one. */
4007 if (NILP (lface))
4008 lface = Finternal_make_lisp_face (face, frame);
4009 }
4010
4011 if (EQ (attr, QCfamily))
4012 {
4013 if (!UNSPECIFIEDP (value))
4014 {
4015 CHECK_STRING (value);
4016 if (SCHARS (value) == 0)
4017 signal_error ("Invalid face family", value);
4018 }
4019 old_value = LFACE_FAMILY (lface);
4020 LFACE_FAMILY (lface) = value;
4021 font_related_attr_p = 1;
4022 }
4023 else if (EQ (attr, QCheight))
4024 {
4025 if (!UNSPECIFIEDP (value))
4026 {
4027 Lisp_Object test;
4028
4029 test = (EQ (face, Qdefault)
4030 ? value
4031 /* The default face must have an absolute size,
4032 otherwise, we do a test merge with a random
4033 height to see if VALUE's ok. */
4034 : merge_face_heights (value, make_number (10), Qnil, Qnil));
4035
4036 if (!INTEGERP (test) || XINT (test) <= 0)
4037 signal_error ("Invalid face height", value);
4038 }
4039
4040 old_value = LFACE_HEIGHT (lface);
4041 LFACE_HEIGHT (lface) = value;
4042 font_related_attr_p = 1;
4043 }
4044 else if (EQ (attr, QCweight))
4045 {
4046 if (!UNSPECIFIEDP (value))
4047 {
4048 CHECK_SYMBOL (value);
4049 if (face_numeric_weight (value) < 0)
4050 signal_error ("Invalid face weight", value);
4051 }
4052 old_value = LFACE_WEIGHT (lface);
4053 LFACE_WEIGHT (lface) = value;
4054 font_related_attr_p = 1;
4055 }
4056 else if (EQ (attr, QCslant))
4057 {
4058 if (!UNSPECIFIEDP (value))
4059 {
4060 CHECK_SYMBOL (value);
4061 if (face_numeric_slant (value) < 0)
4062 signal_error ("Invalid face slant", value);
4063 }
4064 old_value = LFACE_SLANT (lface);
4065 LFACE_SLANT (lface) = value;
4066 font_related_attr_p = 1;
4067 }
4068 else if (EQ (attr, QCunderline))
4069 {
4070 if (!UNSPECIFIEDP (value))
4071 if ((SYMBOLP (value)
4072 && !EQ (value, Qt)
4073 && !EQ (value, Qnil))
4074 /* Underline color. */
4075 || (STRINGP (value)
4076 && SCHARS (value) == 0))
4077 signal_error ("Invalid face underline", value);
4078
4079 old_value = LFACE_UNDERLINE (lface);
4080 LFACE_UNDERLINE (lface) = value;
4081 }
4082 else if (EQ (attr, QCoverline))
4083 {
4084 if (!UNSPECIFIEDP (value))
4085 if ((SYMBOLP (value)
4086 && !EQ (value, Qt)
4087 && !EQ (value, Qnil))
4088 /* Overline color. */
4089 || (STRINGP (value)
4090 && SCHARS (value) == 0))
4091 signal_error ("Invalid face overline", value);
4092
4093 old_value = LFACE_OVERLINE (lface);
4094 LFACE_OVERLINE (lface) = value;
4095 }
4096 else if (EQ (attr, QCstrike_through))
4097 {
4098 if (!UNSPECIFIEDP (value))
4099 if ((SYMBOLP (value)
4100 && !EQ (value, Qt)
4101 && !EQ (value, Qnil))
4102 /* Strike-through color. */
4103 || (STRINGP (value)
4104 && SCHARS (value) == 0))
4105 signal_error ("Invalid face strike-through", value);
4106
4107 old_value = LFACE_STRIKE_THROUGH (lface);
4108 LFACE_STRIKE_THROUGH (lface) = value;
4109 }
4110 else if (EQ (attr, QCbox))
4111 {
4112 int valid_p;
4113
4114 /* Allow t meaning a simple box of width 1 in foreground color
4115 of the face. */
4116 if (EQ (value, Qt))
4117 value = make_number (1);
4118
4119 if (UNSPECIFIEDP (value))
4120 valid_p = 1;
4121 else if (NILP (value))
4122 valid_p = 1;
4123 else if (INTEGERP (value))
4124 valid_p = XINT (value) != 0;
4125 else if (STRINGP (value))
4126 valid_p = SCHARS (value) > 0;
4127 else if (CONSP (value))
4128 {
4129 Lisp_Object tem;
4130
4131 tem = value;
4132 while (CONSP (tem))
4133 {
4134 Lisp_Object k, v;
4135
4136 k = XCAR (tem);
4137 tem = XCDR (tem);
4138 if (!CONSP (tem))
4139 break;
4140 v = XCAR (tem);
4141 tem = XCDR (tem);
4142
4143 if (EQ (k, QCline_width))
4144 {
4145 if (!INTEGERP (v) || XINT (v) == 0)
4146 break;
4147 }
4148 else if (EQ (k, QCcolor))
4149 {
4150 if (!STRINGP (v) || SCHARS (v) == 0)
4151 break;
4152 }
4153 else if (EQ (k, QCstyle))
4154 {
4155 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
4156 break;
4157 }
4158 else
4159 break;
4160 }
4161
4162 valid_p = NILP (tem);
4163 }
4164 else
4165 valid_p = 0;
4166
4167 if (!valid_p)
4168 signal_error ("Invalid face box", value);
4169
4170 old_value = LFACE_BOX (lface);
4171 LFACE_BOX (lface) = value;
4172 }
4173 else if (EQ (attr, QCinverse_video)
4174 || EQ (attr, QCreverse_video))
4175 {
4176 if (!UNSPECIFIEDP (value))
4177 {
4178 CHECK_SYMBOL (value);
4179 if (!EQ (value, Qt) && !NILP (value))
4180 signal_error ("Invalid inverse-video face attribute value", value);
4181 }
4182 old_value = LFACE_INVERSE (lface);
4183 LFACE_INVERSE (lface) = value;
4184 }
4185 else if (EQ (attr, QCforeground))
4186 {
4187 if (!UNSPECIFIEDP (value))
4188 {
4189 /* Don't check for valid color names here because it depends
4190 on the frame (display) whether the color will be valid
4191 when the face is realized. */
4192 CHECK_STRING (value);
4193 if (SCHARS (value) == 0)
4194 signal_error ("Empty foreground color value", value);
4195 }
4196 old_value = LFACE_FOREGROUND (lface);
4197 LFACE_FOREGROUND (lface) = value;
4198 }
4199 else if (EQ (attr, QCbackground))
4200 {
4201 if (!UNSPECIFIEDP (value))
4202 {
4203 /* Don't check for valid color names here because it depends
4204 on the frame (display) whether the color will be valid
4205 when the face is realized. */
4206 CHECK_STRING (value);
4207 if (SCHARS (value) == 0)
4208 signal_error ("Empty background color value", value);
4209 }
4210 old_value = LFACE_BACKGROUND (lface);
4211 LFACE_BACKGROUND (lface) = value;
4212 }
4213 else if (EQ (attr, QCstipple))
4214 {
4215 #ifdef HAVE_X_WINDOWS
4216 if (!UNSPECIFIEDP (value)
4217 && !NILP (value)
4218 && NILP (Fbitmap_spec_p (value)))
4219 signal_error ("Invalid stipple attribute", value);
4220 old_value = LFACE_STIPPLE (lface);
4221 LFACE_STIPPLE (lface) = value;
4222 #endif /* HAVE_X_WINDOWS */
4223 }
4224 else if (EQ (attr, QCwidth))
4225 {
4226 if (!UNSPECIFIEDP (value))
4227 {
4228 CHECK_SYMBOL (value);
4229 if (face_numeric_swidth (value) < 0)
4230 signal_error ("Invalid face width", value);
4231 }
4232 old_value = LFACE_SWIDTH (lface);
4233 LFACE_SWIDTH (lface) = value;
4234 font_related_attr_p = 1;
4235 }
4236 else if (EQ (attr, QCfont))
4237 {
4238 #ifdef HAVE_WINDOW_SYSTEM
4239 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
4240 {
4241 /* Set font-related attributes of the Lisp face from an XLFD
4242 font name. */
4243 struct frame *f;
4244 Lisp_Object tmp;
4245
4246 if (EQ (frame, Qt))
4247 f = SELECTED_FRAME ();
4248 else
4249 f = check_x_frame (frame);
4250
4251 if (!UNSPECIFIEDP (value))
4252 {
4253 CHECK_STRING (value);
4254
4255 /* VALUE may be a fontset name or an alias of fontset. In
4256 such a case, use the base fontset name. */
4257 tmp = Fquery_fontset (value, Qnil);
4258 if (!NILP (tmp))
4259 value = tmp;
4260
4261 if (!set_lface_from_font_name (f, lface, value, 1, 1))
4262 signal_error ("Invalid font or fontset name", value);
4263 }
4264
4265 font_attr_p = 1;
4266 }
4267 #endif /* HAVE_WINDOW_SYSTEM */
4268 }
4269 else if (EQ (attr, QCinherit))
4270 {
4271 Lisp_Object tail;
4272 if (SYMBOLP (value))
4273 tail = Qnil;
4274 else
4275 for (tail = value; CONSP (tail); tail = XCDR (tail))
4276 if (!SYMBOLP (XCAR (tail)))
4277 break;
4278 if (NILP (tail))
4279 LFACE_INHERIT (lface) = value;
4280 else
4281 signal_error ("Invalid face inheritance", value);
4282 }
4283 else if (EQ (attr, QCbold))
4284 {
4285 old_value = LFACE_WEIGHT (lface);
4286 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
4287 font_related_attr_p = 1;
4288 }
4289 else if (EQ (attr, QCitalic))
4290 {
4291 old_value = LFACE_SLANT (lface);
4292 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
4293 font_related_attr_p = 1;
4294 }
4295 else
4296 signal_error ("Invalid face attribute name", attr);
4297
4298 if (font_related_attr_p
4299 && !UNSPECIFIEDP (value))
4300 /* If a font-related attribute other than QCfont is specified, the
4301 original `font' attribute nor that of default face is useless
4302 to determine a new font. Thus, we set it to nil so that font
4303 selection mechanism doesn't use it. */
4304 LFACE_FONT (lface) = Qnil;
4305
4306 /* Changing a named face means that all realized faces depending on
4307 that face are invalid. Since we cannot tell which realized faces
4308 depend on the face, make sure they are all removed. This is done
4309 by incrementing face_change_count. The next call to
4310 init_iterator will then free realized faces. */
4311 if (!EQ (frame, Qt)
4312 && NILP (Fget (face, Qface_no_inherit))
4313 && (EQ (attr, QCfont)
4314 || NILP (Fequal (old_value, value))))
4315 {
4316 ++face_change_count;
4317 ++windows_or_buffers_changed;
4318 }
4319
4320 if (!UNSPECIFIEDP (value)
4321 && NILP (Fequal (old_value, value)))
4322 {
4323 Lisp_Object param;
4324
4325 param = Qnil;
4326
4327 if (EQ (face, Qdefault))
4328 {
4329 #ifdef HAVE_WINDOW_SYSTEM
4330 /* Changed font-related attributes of the `default' face are
4331 reflected in changed `font' frame parameters. */
4332 if (FRAMEP (frame)
4333 && (font_related_attr_p || font_attr_p)
4334 && lface_fully_specified_p (XVECTOR (lface)->contents))
4335 set_font_frame_param (frame, lface);
4336 else
4337 #endif /* HAVE_WINDOW_SYSTEM */
4338
4339 if (EQ (attr, QCforeground))
4340 param = Qforeground_color;
4341 else if (EQ (attr, QCbackground))
4342 param = Qbackground_color;
4343 }
4344 #ifdef HAVE_WINDOW_SYSTEM
4345 #ifndef WINDOWSNT
4346 else if (EQ (face, Qscroll_bar))
4347 {
4348 /* Changing the colors of `scroll-bar' sets frame parameters
4349 `scroll-bar-foreground' and `scroll-bar-background'. */
4350 if (EQ (attr, QCforeground))
4351 param = Qscroll_bar_foreground;
4352 else if (EQ (attr, QCbackground))
4353 param = Qscroll_bar_background;
4354 }
4355 #endif /* not WINDOWSNT */
4356 else if (EQ (face, Qborder))
4357 {
4358 /* Changing background color of `border' sets frame parameter
4359 `border-color'. */
4360 if (EQ (attr, QCbackground))
4361 param = Qborder_color;
4362 }
4363 else if (EQ (face, Qcursor))
4364 {
4365 /* Changing background color of `cursor' sets frame parameter
4366 `cursor-color'. */
4367 if (EQ (attr, QCbackground))
4368 param = Qcursor_color;
4369 }
4370 else if (EQ (face, Qmouse))
4371 {
4372 /* Changing background color of `mouse' sets frame parameter
4373 `mouse-color'. */
4374 if (EQ (attr, QCbackground))
4375 param = Qmouse_color;
4376 }
4377 #endif /* HAVE_WINDOW_SYSTEM */
4378 else if (EQ (face, Qmenu))
4379 {
4380 /* Indicate that we have to update the menu bar when
4381 realizing faces on FRAME. FRAME t change the
4382 default for new frames. We do this by setting
4383 setting the flag in new face caches */
4384 if (FRAMEP (frame))
4385 {
4386 struct frame *f = XFRAME (frame);
4387 if (FRAME_FACE_CACHE (f) == NULL)
4388 FRAME_FACE_CACHE (f) = make_face_cache (f);
4389 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
4390 }
4391 else
4392 menu_face_changed_default = 1;
4393 }
4394
4395 if (!NILP (param))
4396 {
4397 if (EQ (frame, Qt))
4398 /* Update `default-frame-alist', which is used for new frames. */
4399 {
4400 store_in_alist (&Vdefault_frame_alist, param, value);
4401 }
4402 else
4403 /* Update the current frame's parameters. */
4404 {
4405 Lisp_Object cons;
4406 cons = XCAR (Vparam_value_alist);
4407 XSETCAR (cons, param);
4408 XSETCDR (cons, value);
4409 Fmodify_frame_parameters (frame, Vparam_value_alist);
4410 }
4411 }
4412 }
4413
4414 return face;
4415 }
4416
4417
4418 #ifdef HAVE_WINDOW_SYSTEM
4419
4420 /* Set the `font' frame parameter of FRAME determined from `default'
4421 face attributes LFACE. If a face or fontset name is explicitely
4422 specfied in LFACE, use it as is. Otherwise, determine a font name
4423 from the other font-related atrributes of LFACE. In that case, if
4424 there's no matching font, signals an error. */
4425
4426 static void
4427 set_font_frame_param (frame, lface)
4428 Lisp_Object frame, lface;
4429 {
4430 struct frame *f = XFRAME (frame);
4431
4432 if (FRAME_WINDOW_P (f))
4433 {
4434 Lisp_Object font_name;
4435 char *font;
4436
4437 if (STRINGP (LFACE_FONT (lface)))
4438 font_name = LFACE_FONT (lface);
4439 else
4440 {
4441 /* Choose a font name that reflects LFACE's attributes and has
4442 the registry and encoding pattern specified in the default
4443 fontset (3rd arg: -1) for ASCII characters (4th arg: 0). */
4444 font = choose_face_font (f, XVECTOR (lface)->contents, -1, 0, 0);
4445 if (!font)
4446 error ("No font matches the specified attribute");
4447 font_name = build_string (font);
4448 xfree (font);
4449 }
4450
4451 f->default_face_done_p = 0;
4452 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font_name), Qnil));
4453 }
4454 }
4455
4456
4457 /* Update the corresponding face when frame parameter PARAM on frame F
4458 has been assigned the value NEW_VALUE. */
4459
4460 void
4461 update_face_from_frame_parameter (f, param, new_value)
4462 struct frame *f;
4463 Lisp_Object param, new_value;
4464 {
4465 Lisp_Object face = Qnil;
4466 Lisp_Object lface;
4467
4468 /* If there are no faces yet, give up. This is the case when called
4469 from Fx_create_frame, and we do the necessary things later in
4470 face-set-after-frame-defaults. */
4471 if (NILP (f->face_alist))
4472 return;
4473
4474 if (EQ (param, Qforeground_color))
4475 {
4476 face = Qdefault;
4477 lface = lface_from_face_name (f, face, 1);
4478 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
4479 ? new_value : Qunspecified);
4480 realize_basic_faces (f);
4481 }
4482 else if (EQ (param, Qbackground_color))
4483 {
4484 Lisp_Object frame;
4485
4486 /* Changing the background color might change the background
4487 mode, so that we have to load new defface specs. Call
4488 frame-update-face-colors to do that. */
4489 XSETFRAME (frame, f);
4490 call1 (Qframe_update_face_colors, frame);
4491
4492 face = Qdefault;
4493 lface = lface_from_face_name (f, face, 1);
4494 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4495 ? new_value : Qunspecified);
4496 realize_basic_faces (f);
4497 }
4498 else if (EQ (param, Qborder_color))
4499 {
4500 face = Qborder;
4501 lface = lface_from_face_name (f, face, 1);
4502 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4503 ? new_value : Qunspecified);
4504 }
4505 else if (EQ (param, Qcursor_color))
4506 {
4507 face = Qcursor;
4508 lface = lface_from_face_name (f, face, 1);
4509 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4510 ? new_value : Qunspecified);
4511 }
4512 else if (EQ (param, Qmouse_color))
4513 {
4514 face = Qmouse;
4515 lface = lface_from_face_name (f, face, 1);
4516 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
4517 ? new_value : Qunspecified);
4518 }
4519
4520 /* Changing a named face means that all realized faces depending on
4521 that face are invalid. Since we cannot tell which realized faces
4522 depend on the face, make sure they are all removed. This is done
4523 by incrementing face_change_count. The next call to
4524 init_iterator will then free realized faces. */
4525 if (!NILP (face)
4526 && NILP (Fget (face, Qface_no_inherit)))
4527 {
4528 ++face_change_count;
4529 ++windows_or_buffers_changed;
4530 }
4531 }
4532
4533
4534 /* Get the value of X resource RESOURCE, class CLASS for the display
4535 of frame FRAME. This is here because ordinary `x-get-resource'
4536 doesn't take a frame argument. */
4537
4538 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
4539 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
4540 (resource, class, frame)
4541 Lisp_Object resource, class, frame;
4542 {
4543 Lisp_Object value = Qnil;
4544 CHECK_STRING (resource);
4545 CHECK_STRING (class);
4546 CHECK_LIVE_FRAME (frame);
4547 BLOCK_INPUT;
4548 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
4549 resource, class, Qnil, Qnil);
4550 UNBLOCK_INPUT;
4551 return value;
4552 }
4553
4554
4555 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
4556 If VALUE is "on" or "true", return t. If VALUE is "off" or
4557 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
4558 error; if SIGNAL_P is zero, return 0. */
4559
4560 static Lisp_Object
4561 face_boolean_x_resource_value (value, signal_p)
4562 Lisp_Object value;
4563 int signal_p;
4564 {
4565 Lisp_Object result = make_number (0);
4566
4567 xassert (STRINGP (value));
4568
4569 if (xstricmp (SDATA (value), "on") == 0
4570 || xstricmp (SDATA (value), "true") == 0)
4571 result = Qt;
4572 else if (xstricmp (SDATA (value), "off") == 0
4573 || xstricmp (SDATA (value), "false") == 0)
4574 result = Qnil;
4575 else if (xstricmp (SDATA (value), "unspecified") == 0)
4576 result = Qunspecified;
4577 else if (signal_p)
4578 signal_error ("Invalid face attribute value from X resource", value);
4579
4580 return result;
4581 }
4582
4583
4584 DEFUN ("internal-set-lisp-face-attribute-from-resource",
4585 Finternal_set_lisp_face_attribute_from_resource,
4586 Sinternal_set_lisp_face_attribute_from_resource,
4587 3, 4, 0, doc: /* */)
4588 (face, attr, value, frame)
4589 Lisp_Object face, attr, value, frame;
4590 {
4591 CHECK_SYMBOL (face);
4592 CHECK_SYMBOL (attr);
4593 CHECK_STRING (value);
4594
4595 if (xstricmp (SDATA (value), "unspecified") == 0)
4596 value = Qunspecified;
4597 else if (EQ (attr, QCheight))
4598 {
4599 value = Fstring_to_number (value, make_number (10));
4600 if (XINT (value) <= 0)
4601 signal_error ("Invalid face height from X resource", value);
4602 }
4603 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
4604 value = face_boolean_x_resource_value (value, 1);
4605 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
4606 value = intern (SDATA (value));
4607 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
4608 value = face_boolean_x_resource_value (value, 1);
4609 else if (EQ (attr, QCunderline)
4610 || EQ (attr, QCoverline)
4611 || EQ (attr, QCstrike_through))
4612 {
4613 Lisp_Object boolean_value;
4614
4615 /* If the result of face_boolean_x_resource_value is t or nil,
4616 VALUE does NOT specify a color. */
4617 boolean_value = face_boolean_x_resource_value (value, 0);
4618 if (SYMBOLP (boolean_value))
4619 value = boolean_value;
4620 }
4621 else if (EQ (attr, QCbox))
4622 value = Fcar (Fread_from_string (value, Qnil, Qnil));
4623
4624 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
4625 }
4626
4627 #endif /* HAVE_WINDOW_SYSTEM */
4628
4629 \f
4630 /***********************************************************************
4631 Menu face
4632 ***********************************************************************/
4633
4634 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
4635
4636 /* Make menus on frame F appear as specified by the `menu' face. */
4637
4638 static void
4639 x_update_menu_appearance (f)
4640 struct frame *f;
4641 {
4642 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4643 XrmDatabase rdb;
4644
4645 if (dpyinfo
4646 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
4647 rdb != NULL))
4648 {
4649 char line[512];
4650 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
4651 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
4652 const char *myname = SDATA (Vx_resource_name);
4653 int changed_p = 0;
4654 #ifdef USE_MOTIF
4655 const char *popup_path = "popup_menu";
4656 #else
4657 const char *popup_path = "menu.popup";
4658 #endif
4659
4660 if (STRINGP (LFACE_FOREGROUND (lface)))
4661 {
4662 sprintf (line, "%s.%s*foreground: %s",
4663 myname, popup_path,
4664 SDATA (LFACE_FOREGROUND (lface)));
4665 XrmPutLineResource (&rdb, line);
4666 sprintf (line, "%s.pane.menubar*foreground: %s",
4667 myname, SDATA (LFACE_FOREGROUND (lface)));
4668 XrmPutLineResource (&rdb, line);
4669 changed_p = 1;
4670 }
4671
4672 if (STRINGP (LFACE_BACKGROUND (lface)))
4673 {
4674 sprintf (line, "%s.%s*background: %s",
4675 myname, popup_path,
4676 SDATA (LFACE_BACKGROUND (lface)));
4677 XrmPutLineResource (&rdb, line);
4678 sprintf (line, "%s.pane.menubar*background: %s",
4679 myname, SDATA (LFACE_BACKGROUND (lface)));
4680 XrmPutLineResource (&rdb, line);
4681 changed_p = 1;
4682 }
4683
4684 if (face->font_name
4685 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
4686 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
4687 || !UNSPECIFIEDP (LFACE_AVGWIDTH (lface))
4688 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
4689 || !UNSPECIFIEDP (LFACE_SLANT (lface))
4690 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
4691 {
4692 #ifdef USE_MOTIF
4693 const char *suffix = "List";
4694 #else
4695 const char *suffix = "";
4696 #endif
4697 sprintf (line, "%s.pane.menubar*font%s: %s",
4698 myname, suffix, face->font_name);
4699 XrmPutLineResource (&rdb, line);
4700 sprintf (line, "%s.%s*font%s: %s",
4701 myname, popup_path, suffix, face->font_name);
4702 XrmPutLineResource (&rdb, line);
4703 changed_p = 1;
4704 }
4705
4706 if (changed_p && f->output_data.x->menubar_widget)
4707 free_frame_menubar (f);
4708 }
4709 }
4710
4711 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
4712
4713
4714 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
4715 Sface_attribute_relative_p,
4716 2, 2, 0,
4717 doc: /* Return non-nil if face ATTRIBUTE VALUE is relative. */)
4718 (attribute, value)
4719 Lisp_Object attribute, value;
4720 {
4721 if (EQ (value, Qunspecified))
4722 return Qt;
4723 else if (EQ (attribute, QCheight))
4724 return INTEGERP (value) ? Qnil : Qt;
4725 else
4726 return Qnil;
4727 }
4728
4729 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
4730 3, 3, 0,
4731 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
4732 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
4733 the result will be absolute, otherwise it will be relative. */)
4734 (attribute, value1, value2)
4735 Lisp_Object attribute, value1, value2;
4736 {
4737 if (EQ (value1, Qunspecified))
4738 return value2;
4739 else if (EQ (attribute, QCheight))
4740 return merge_face_heights (value1, value2, value1, Qnil);
4741 else
4742 return value1;
4743 }
4744
4745
4746 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
4747 Sinternal_get_lisp_face_attribute,
4748 2, 3, 0,
4749 doc: /* Return face attribute KEYWORD of face SYMBOL.
4750 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
4751 face attribute name, signal an error.
4752 If the optional argument FRAME is given, report on face SYMBOL in that
4753 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
4754 frames). If FRAME is omitted or nil, use the selected frame. */)
4755 (symbol, keyword, frame)
4756 Lisp_Object symbol, keyword, frame;
4757 {
4758 Lisp_Object lface, value = Qnil;
4759
4760 CHECK_SYMBOL (symbol);
4761 CHECK_SYMBOL (keyword);
4762
4763 if (EQ (frame, Qt))
4764 lface = lface_from_face_name (NULL, symbol, 1);
4765 else
4766 {
4767 if (NILP (frame))
4768 frame = selected_frame;
4769 CHECK_LIVE_FRAME (frame);
4770 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
4771 }
4772
4773 if (EQ (keyword, QCfamily))
4774 value = LFACE_FAMILY (lface);
4775 else if (EQ (keyword, QCheight))
4776 value = LFACE_HEIGHT (lface);
4777 else if (EQ (keyword, QCweight))
4778 value = LFACE_WEIGHT (lface);
4779 else if (EQ (keyword, QCslant))
4780 value = LFACE_SLANT (lface);
4781 else if (EQ (keyword, QCunderline))
4782 value = LFACE_UNDERLINE (lface);
4783 else if (EQ (keyword, QCoverline))
4784 value = LFACE_OVERLINE (lface);
4785 else if (EQ (keyword, QCstrike_through))
4786 value = LFACE_STRIKE_THROUGH (lface);
4787 else if (EQ (keyword, QCbox))
4788 value = LFACE_BOX (lface);
4789 else if (EQ (keyword, QCinverse_video)
4790 || EQ (keyword, QCreverse_video))
4791 value = LFACE_INVERSE (lface);
4792 else if (EQ (keyword, QCforeground))
4793 value = LFACE_FOREGROUND (lface);
4794 else if (EQ (keyword, QCbackground))
4795 value = LFACE_BACKGROUND (lface);
4796 else if (EQ (keyword, QCstipple))
4797 value = LFACE_STIPPLE (lface);
4798 else if (EQ (keyword, QCwidth))
4799 value = LFACE_SWIDTH (lface);
4800 else if (EQ (keyword, QCinherit))
4801 value = LFACE_INHERIT (lface);
4802 else if (EQ (keyword, QCfont))
4803 value = LFACE_FONT (lface);
4804 else
4805 signal_error ("Invalid face attribute name", keyword);
4806
4807 return value;
4808 }
4809
4810
4811 DEFUN ("internal-lisp-face-attribute-values",
4812 Finternal_lisp_face_attribute_values,
4813 Sinternal_lisp_face_attribute_values, 1, 1, 0,
4814 doc: /* Return a list of valid discrete values for face attribute ATTR.
4815 Value is nil if ATTR doesn't have a discrete set of valid values. */)
4816 (attr)
4817 Lisp_Object attr;
4818 {
4819 Lisp_Object result = Qnil;
4820
4821 CHECK_SYMBOL (attr);
4822
4823 if (EQ (attr, QCweight)
4824 || EQ (attr, QCslant)
4825 || EQ (attr, QCwidth))
4826 {
4827 /* Extract permissible symbols from tables. */
4828 struct table_entry *table;
4829 int i, dim;
4830
4831 if (EQ (attr, QCweight))
4832 table = weight_table, dim = DIM (weight_table);
4833 else if (EQ (attr, QCslant))
4834 table = slant_table, dim = DIM (slant_table);
4835 else
4836 table = swidth_table, dim = DIM (swidth_table);
4837
4838 for (i = 0; i < dim; ++i)
4839 {
4840 Lisp_Object symbol = *table[i].symbol;
4841 Lisp_Object tail = result;
4842
4843 while (!NILP (tail)
4844 && !EQ (XCAR (tail), symbol))
4845 tail = XCDR (tail);
4846
4847 if (NILP (tail))
4848 result = Fcons (symbol, result);
4849 }
4850 }
4851 else if (EQ (attr, QCunderline))
4852 result = Fcons (Qt, Fcons (Qnil, Qnil));
4853 else if (EQ (attr, QCoverline))
4854 result = Fcons (Qt, Fcons (Qnil, Qnil));
4855 else if (EQ (attr, QCstrike_through))
4856 result = Fcons (Qt, Fcons (Qnil, Qnil));
4857 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
4858 result = Fcons (Qt, Fcons (Qnil, Qnil));
4859
4860 return result;
4861 }
4862
4863
4864 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
4865 Sinternal_merge_in_global_face, 2, 2, 0,
4866 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
4867 Default face attributes override any local face attributes. */)
4868 (face, frame)
4869 Lisp_Object face, frame;
4870 {
4871 int i;
4872 Lisp_Object global_lface, local_lface, *gvec, *lvec;
4873
4874 CHECK_LIVE_FRAME (frame);
4875 global_lface = lface_from_face_name (NULL, face, 1);
4876 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
4877 if (NILP (local_lface))
4878 local_lface = Finternal_make_lisp_face (face, frame);
4879
4880 /* Make every specified global attribute override the local one.
4881 BEWARE!! This is only used from `face-set-after-frame-default' where
4882 the local frame is defined from default specs in `face-defface-spec'
4883 and those should be overridden by global settings. Hence the strange
4884 "global before local" priority. */
4885 lvec = XVECTOR (local_lface)->contents;
4886 gvec = XVECTOR (global_lface)->contents;
4887 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4888 if (! UNSPECIFIEDP (gvec[i]))
4889 lvec[i] = gvec[i];
4890
4891 return Qnil;
4892 }
4893
4894
4895 /* The following function is implemented for compatibility with 20.2.
4896 The function is used in x-resolve-fonts when it is asked to
4897 return fonts with the same size as the font of a face. This is
4898 done in fontset.el. */
4899
4900 DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
4901 doc: /* Return the font name of face FACE, or nil if it is unspecified.
4902 If the optional argument FRAME is given, report on face FACE in that frame.
4903 If FRAME is t, report on the defaults for face FACE (for new frames).
4904 The font default for a face is either nil, or a list
4905 of the form (bold), (italic) or (bold italic).
4906 If FRAME is omitted or nil, use the selected frame. */)
4907 (face, frame)
4908 Lisp_Object face, frame;
4909 {
4910 if (EQ (frame, Qt))
4911 {
4912 Lisp_Object result = Qnil;
4913 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
4914
4915 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
4916 && !EQ (LFACE_WEIGHT (lface), Qnormal))
4917 result = Fcons (Qbold, result);
4918
4919 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
4920 && !EQ (LFACE_SLANT (lface), Qnormal))
4921 result = Fcons (Qitalic, result);
4922
4923 return result;
4924 }
4925 else
4926 {
4927 struct frame *f = frame_or_selected_frame (frame, 1);
4928 int face_id = lookup_named_face (f, face, 0);
4929 struct face *face = FACE_FROM_ID (f, face_id);
4930 return face ? build_string (face->font_name) : Qnil;
4931 }
4932 }
4933
4934
4935 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
4936 all attributes are `equal'. Tries to be fast because this function
4937 is called quite often. */
4938
4939 static INLINE int
4940 face_attr_equal_p (v1, v2)
4941 Lisp_Object v1, v2;
4942 {
4943 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
4944 and the other is specified. */
4945 if (XTYPE (v1) != XTYPE (v2))
4946 return 0;
4947
4948 if (EQ (v1, v2))
4949 return 1;
4950
4951 switch (XTYPE (v1))
4952 {
4953 case Lisp_String:
4954 if (SBYTES (v1) != SBYTES (v2))
4955 return 0;
4956
4957 return bcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
4958
4959 case Lisp_Int:
4960 case Lisp_Symbol:
4961 return 0;
4962
4963 default:
4964 return !NILP (Fequal (v1, v2));
4965 }
4966 }
4967
4968
4969 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
4970 all attributes are `equal'. Tries to be fast because this function
4971 is called quite often. */
4972
4973 static INLINE int
4974 lface_equal_p (v1, v2)
4975 Lisp_Object *v1, *v2;
4976 {
4977 int i, equal_p = 1;
4978
4979 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
4980 equal_p = face_attr_equal_p (v1[i], v2[i]);
4981
4982 return equal_p;
4983 }
4984
4985
4986 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
4987 Sinternal_lisp_face_equal_p, 2, 3, 0,
4988 doc: /* True if FACE1 and FACE2 are equal.
4989 If the optional argument FRAME is given, report on face FACE in that frame.
4990 If FRAME is t, report on the defaults for face FACE (for new frames).
4991 If FRAME is omitted or nil, use the selected frame. */)
4992 (face1, face2, frame)
4993 Lisp_Object face1, face2, frame;
4994 {
4995 int equal_p;
4996 struct frame *f;
4997 Lisp_Object lface1, lface2;
4998
4999 if (EQ (frame, Qt))
5000 f = NULL;
5001 else
5002 /* Don't use check_x_frame here because this function is called
5003 before X frames exist. At that time, if FRAME is nil,
5004 selected_frame will be used which is the frame dumped with
5005 Emacs. That frame is not an X frame. */
5006 f = frame_or_selected_frame (frame, 2);
5007
5008 lface1 = lface_from_face_name (NULL, face1, 1);
5009 lface2 = lface_from_face_name (NULL, face2, 1);
5010 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
5011 XVECTOR (lface2)->contents);
5012 return equal_p ? Qt : Qnil;
5013 }
5014
5015
5016 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
5017 Sinternal_lisp_face_empty_p, 1, 2, 0,
5018 doc: /* True if FACE has no attribute specified.
5019 If the optional argument FRAME is given, report on face FACE in that frame.
5020 If FRAME is t, report on the defaults for face FACE (for new frames).
5021 If FRAME is omitted or nil, use the selected frame. */)
5022 (face, frame)
5023 Lisp_Object face, frame;
5024 {
5025 struct frame *f;
5026 Lisp_Object lface;
5027 int i;
5028
5029 if (NILP (frame))
5030 frame = selected_frame;
5031 CHECK_LIVE_FRAME (frame);
5032 f = XFRAME (frame);
5033
5034 if (EQ (frame, Qt))
5035 lface = lface_from_face_name (NULL, face, 1);
5036 else
5037 lface = lface_from_face_name (f, face, 1);
5038
5039 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
5040 if (!UNSPECIFIEDP (AREF (lface, i)))
5041 break;
5042
5043 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
5044 }
5045
5046
5047 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
5048 0, 1, 0,
5049 doc: /* Return an alist of frame-local faces defined on FRAME.
5050 For internal use only. */)
5051 (frame)
5052 Lisp_Object frame;
5053 {
5054 struct frame *f = frame_or_selected_frame (frame, 0);
5055 return f->face_alist;
5056 }
5057
5058
5059 /* Return a hash code for Lisp string STRING with case ignored. Used
5060 below in computing a hash value for a Lisp face. */
5061
5062 static INLINE unsigned
5063 hash_string_case_insensitive (string)
5064 Lisp_Object string;
5065 {
5066 const unsigned char *s;
5067 unsigned hash = 0;
5068 xassert (STRINGP (string));
5069 for (s = SDATA (string); *s; ++s)
5070 hash = (hash << 1) ^ tolower (*s);
5071 return hash;
5072 }
5073
5074
5075 /* Return a hash code for face attribute vector V. */
5076
5077 static INLINE unsigned
5078 lface_hash (v)
5079 Lisp_Object *v;
5080 {
5081 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
5082 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
5083 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
5084 ^ XFASTINT (v[LFACE_WEIGHT_INDEX])
5085 ^ XFASTINT (v[LFACE_SLANT_INDEX])
5086 ^ XFASTINT (v[LFACE_SWIDTH_INDEX])
5087 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
5088 }
5089
5090
5091 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
5092 considering charsets/registries). They do if they specify the same
5093 family, point size, weight, width, slant, and fontset. Both LFACE1
5094 and LFACE2 must be fully-specified. */
5095
5096 static INLINE int
5097 lface_same_font_attributes_p (lface1, lface2)
5098 Lisp_Object *lface1, *lface2;
5099 {
5100 xassert (lface_fully_specified_p (lface1)
5101 && lface_fully_specified_p (lface2));
5102 return (xstricmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
5103 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
5104 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
5105 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
5106 && EQ (lface1[LFACE_AVGWIDTH_INDEX], lface2[LFACE_AVGWIDTH_INDEX])
5107 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
5108 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
5109 && (EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
5110 || (STRINGP (lface1[LFACE_FONT_INDEX])
5111 && STRINGP (lface2[LFACE_FONT_INDEX])
5112 && xstricmp (SDATA (lface1[LFACE_FONT_INDEX]),
5113 SDATA (lface2[LFACE_FONT_INDEX])))));
5114 }
5115
5116
5117 \f
5118 /***********************************************************************
5119 Realized Faces
5120 ***********************************************************************/
5121
5122 /* Allocate and return a new realized face for Lisp face attribute
5123 vector ATTR. */
5124
5125 static struct face *
5126 make_realized_face (attr)
5127 Lisp_Object *attr;
5128 {
5129 struct face *face = (struct face *) xmalloc (sizeof *face);
5130 bzero (face, sizeof *face);
5131 face->ascii_face = face;
5132 bcopy (attr, face->lface, sizeof face->lface);
5133 return face;
5134 }
5135
5136
5137 /* Free realized face FACE, including its X resources. FACE may
5138 be null. */
5139
5140 static void
5141 free_realized_face (f, face)
5142 struct frame *f;
5143 struct face *face;
5144 {
5145 if (face)
5146 {
5147 #ifdef HAVE_WINDOW_SYSTEM
5148 if (FRAME_WINDOW_P (f))
5149 {
5150 /* Free fontset of FACE if it is ASCII face. */
5151 if (face->fontset >= 0 && face == face->ascii_face)
5152 free_face_fontset (f, face);
5153 if (face->gc)
5154 {
5155 x_free_gc (f, face->gc);
5156 face->gc = 0;
5157 }
5158
5159 free_face_colors (f, face);
5160 x_destroy_bitmap (f, face->stipple);
5161 }
5162 #endif /* HAVE_WINDOW_SYSTEM */
5163
5164 xfree (face);
5165 }
5166 }
5167
5168
5169 /* Prepare face FACE for subsequent display on frame F. This
5170 allocated GCs if they haven't been allocated yet or have been freed
5171 by clearing the face cache. */
5172
5173 void
5174 prepare_face_for_display (f, face)
5175 struct frame *f;
5176 struct face *face;
5177 {
5178 #ifdef HAVE_WINDOW_SYSTEM
5179 xassert (FRAME_WINDOW_P (f));
5180
5181 if (face->gc == 0)
5182 {
5183 XGCValues xgcv;
5184 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
5185
5186 xgcv.foreground = face->foreground;
5187 xgcv.background = face->background;
5188 #ifdef HAVE_X_WINDOWS
5189 xgcv.graphics_exposures = False;
5190 #endif
5191 /* The font of FACE may be null if we couldn't load it. */
5192 if (face->font)
5193 {
5194 #ifdef HAVE_X_WINDOWS
5195 xgcv.font = face->font->fid;
5196 #endif
5197 #ifdef WINDOWSNT
5198 xgcv.font = face->font;
5199 #endif
5200 #ifdef MAC_OS
5201 xgcv.font = face->font;
5202 #endif
5203 mask |= GCFont;
5204 }
5205
5206 BLOCK_INPUT;
5207 #ifdef HAVE_X_WINDOWS
5208 if (face->stipple)
5209 {
5210 xgcv.fill_style = FillOpaqueStippled;
5211 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
5212 mask |= GCFillStyle | GCStipple;
5213 }
5214 #endif
5215 face->gc = x_create_gc (f, mask, &xgcv);
5216 UNBLOCK_INPUT;
5217 }
5218 #endif /* HAVE_WINDOW_SYSTEM */
5219 }
5220
5221 \f
5222 /* Returns the `distance' between the colors X and Y. */
5223
5224 static int
5225 color_distance (x, y)
5226 XColor *x, *y;
5227 {
5228 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
5229 Quoting from that paper:
5230
5231 This formula has results that are very close to L*u*v* (with the
5232 modified lightness curve) and, more importantly, it is a more even
5233 algorithm: it does not have a range of colours where it suddenly
5234 gives far from optimal results.
5235
5236 See <http://www.compuphase.com/cmetric.htm> for more info. */
5237
5238 long r = (x->red - y->red) >> 8;
5239 long g = (x->green - y->green) >> 8;
5240 long b = (x->blue - y->blue) >> 8;
5241 long r_mean = (x->red + y->red) >> 9;
5242
5243 return
5244 (((512 + r_mean) * r * r) >> 8)
5245 + 4 * g * g
5246 + (((767 - r_mean) * b * b) >> 8);
5247 }
5248
5249
5250 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
5251 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
5252 COLOR1 and COLOR2 may be either strings containing the color name,
5253 or lists of the form (RED GREEN BLUE).
5254 If FRAME is unspecified or nil, the current frame is used. */)
5255 (color1, color2, frame)
5256 Lisp_Object color1, color2, frame;
5257 {
5258 struct frame *f;
5259 XColor cdef1, cdef2;
5260
5261 if (NILP (frame))
5262 frame = selected_frame;
5263 CHECK_LIVE_FRAME (frame);
5264 f = XFRAME (frame);
5265
5266 if ((CONSP (color1) && !parse_rgb_list (color1, &cdef1))
5267 || !STRINGP (color1)
5268 || !defined_color (f, SDATA (color1), &cdef1, 0))
5269 signal_error ("Invalid color", color1);
5270 if ((CONSP (color2) && !parse_rgb_list (color2, &cdef2))
5271 || !STRINGP (color2)
5272 || !defined_color (f, SDATA (color2), &cdef2, 0))
5273 signal_error ("Invalid color", color2);
5274
5275 return make_number (color_distance (&cdef1, &cdef2));
5276 }
5277
5278 \f
5279 /***********************************************************************
5280 Face Cache
5281 ***********************************************************************/
5282
5283 /* Return a new face cache for frame F. */
5284
5285 static struct face_cache *
5286 make_face_cache (f)
5287 struct frame *f;
5288 {
5289 struct face_cache *c;
5290 int size;
5291
5292 c = (struct face_cache *) xmalloc (sizeof *c);
5293 bzero (c, sizeof *c);
5294 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5295 c->buckets = (struct face **) xmalloc (size);
5296 bzero (c->buckets, size);
5297 c->size = 50;
5298 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
5299 c->f = f;
5300 c->menu_face_changed_p = menu_face_changed_default;
5301 return c;
5302 }
5303
5304
5305 /* Clear out all graphics contexts for all realized faces, except for
5306 the basic faces. This should be done from time to time just to avoid
5307 keeping too many graphics contexts that are no longer needed. */
5308
5309 static void
5310 clear_face_gcs (c)
5311 struct face_cache *c;
5312 {
5313 if (c && FRAME_WINDOW_P (c->f))
5314 {
5315 #ifdef HAVE_WINDOW_SYSTEM
5316 int i;
5317 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
5318 {
5319 struct face *face = c->faces_by_id[i];
5320 if (face && face->gc)
5321 {
5322 x_free_gc (c->f, face->gc);
5323 face->gc = 0;
5324 }
5325 }
5326 #endif /* HAVE_WINDOW_SYSTEM */
5327 }
5328 }
5329
5330
5331 /* Free all realized faces in face cache C, including basic faces.
5332 C may be null. If faces are freed, make sure the frame's current
5333 matrix is marked invalid, so that a display caused by an expose
5334 event doesn't try to use faces we destroyed. */
5335
5336 static void
5337 free_realized_faces (c)
5338 struct face_cache *c;
5339 {
5340 if (c && c->used)
5341 {
5342 int i, size;
5343 struct frame *f = c->f;
5344
5345 /* We must block input here because we can't process X events
5346 safely while only some faces are freed, or when the frame's
5347 current matrix still references freed faces. */
5348 BLOCK_INPUT;
5349
5350 for (i = 0; i < c->used; ++i)
5351 {
5352 free_realized_face (f, c->faces_by_id[i]);
5353 c->faces_by_id[i] = NULL;
5354 }
5355
5356 c->used = 0;
5357 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5358 bzero (c->buckets, size);
5359
5360 /* Must do a thorough redisplay the next time. Mark current
5361 matrices as invalid because they will reference faces freed
5362 above. This function is also called when a frame is
5363 destroyed. In this case, the root window of F is nil. */
5364 if (WINDOWP (f->root_window))
5365 {
5366 clear_current_matrices (f);
5367 ++windows_or_buffers_changed;
5368 }
5369
5370 UNBLOCK_INPUT;
5371 }
5372 }
5373
5374
5375 /* Free all faces realized for multibyte characters on frame F that
5376 has FONTSET. */
5377
5378 void
5379 free_realized_multibyte_face (f, fontset)
5380 struct frame *f;
5381 int fontset;
5382 {
5383 struct face_cache *cache = FRAME_FACE_CACHE (f);
5384 struct face *face;
5385 int i;
5386
5387 /* We must block input here because we can't process X events safely
5388 while only some faces are freed, or when the frame's current
5389 matrix still references freed faces. */
5390 BLOCK_INPUT;
5391
5392 for (i = 0; i < cache->used; i++)
5393 {
5394 face = cache->faces_by_id[i];
5395 if (face
5396 && face != face->ascii_face
5397 && face->fontset == fontset)
5398 {
5399 uncache_face (cache, face);
5400 free_realized_face (f, face);
5401 }
5402 }
5403
5404 /* Must do a thorough redisplay the next time. Mark current
5405 matrices as invalid because they will reference faces freed
5406 above. This function is also called when a frame is destroyed.
5407 In this case, the root window of F is nil. */
5408 if (WINDOWP (f->root_window))
5409 {
5410 clear_current_matrices (f);
5411 ++windows_or_buffers_changed;
5412 }
5413
5414 UNBLOCK_INPUT;
5415 }
5416
5417
5418 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
5419 This is done after attributes of a named face have been changed,
5420 because we can't tell which realized faces depend on that face. */
5421
5422 void
5423 free_all_realized_faces (frame)
5424 Lisp_Object frame;
5425 {
5426 if (NILP (frame))
5427 {
5428 Lisp_Object rest;
5429 FOR_EACH_FRAME (rest, frame)
5430 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5431 }
5432 else
5433 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
5434 }
5435
5436
5437 /* Free face cache C and faces in it, including their X resources. */
5438
5439 static void
5440 free_face_cache (c)
5441 struct face_cache *c;
5442 {
5443 if (c)
5444 {
5445 free_realized_faces (c);
5446 xfree (c->buckets);
5447 xfree (c->faces_by_id);
5448 xfree (c);
5449 }
5450 }
5451
5452
5453 /* Cache realized face FACE in face cache C. HASH is the hash value
5454 of FACE. If FACE->fontset >= 0, add the new face to the end of the
5455 collision list of the face hash table of C. This is done because
5456 otherwise lookup_face would find FACE for every character, even if
5457 faces with the same attributes but for specific characters exist. */
5458
5459 static void
5460 cache_face (c, face, hash)
5461 struct face_cache *c;
5462 struct face *face;
5463 unsigned hash;
5464 {
5465 int i = hash % FACE_CACHE_BUCKETS_SIZE;
5466
5467 face->hash = hash;
5468
5469 if (face->fontset >= 0)
5470 {
5471 struct face *last = c->buckets[i];
5472 if (last)
5473 {
5474 while (last->next)
5475 last = last->next;
5476 last->next = face;
5477 face->prev = last;
5478 face->next = NULL;
5479 }
5480 else
5481 {
5482 c->buckets[i] = face;
5483 face->prev = face->next = NULL;
5484 }
5485 }
5486 else
5487 {
5488 face->prev = NULL;
5489 face->next = c->buckets[i];
5490 if (face->next)
5491 face->next->prev = face;
5492 c->buckets[i] = face;
5493 }
5494
5495 /* Find a free slot in C->faces_by_id and use the index of the free
5496 slot as FACE->id. */
5497 for (i = 0; i < c->used; ++i)
5498 if (c->faces_by_id[i] == NULL)
5499 break;
5500 face->id = i;
5501
5502 /* Maybe enlarge C->faces_by_id. */
5503 if (i == c->used)
5504 {
5505 if (c->used == c->size)
5506 {
5507 int new_size, sz;
5508 new_size = min (2 * c->size, MAX_FACE_ID);
5509 if (new_size == c->size)
5510 abort (); /* Alternatives? ++kfs */
5511 sz = new_size * sizeof *c->faces_by_id;
5512 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
5513 c->size = new_size;
5514 }
5515 c->used++;
5516 }
5517
5518 #if GLYPH_DEBUG
5519 /* Check that FACE got a unique id. */
5520 {
5521 int j, n;
5522 struct face *face;
5523
5524 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
5525 for (face = c->buckets[j]; face; face = face->next)
5526 if (face->id == i)
5527 ++n;
5528
5529 xassert (n == 1);
5530 }
5531 #endif /* GLYPH_DEBUG */
5532
5533 c->faces_by_id[i] = face;
5534 }
5535
5536
5537 /* Remove face FACE from cache C. */
5538
5539 static void
5540 uncache_face (c, face)
5541 struct face_cache *c;
5542 struct face *face;
5543 {
5544 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
5545
5546 if (face->prev)
5547 face->prev->next = face->next;
5548 else
5549 c->buckets[i] = face->next;
5550
5551 if (face->next)
5552 face->next->prev = face->prev;
5553
5554 c->faces_by_id[face->id] = NULL;
5555 if (face->id == c->used)
5556 --c->used;
5557 }
5558
5559
5560 /* Look up a realized face with face attributes ATTR in the face cache
5561 of frame F. The face will be used to display character C. Value
5562 is the ID of the face found. If no suitable face is found, realize
5563 a new one. In that case, if C is a multibyte character, BASE_FACE
5564 is a face that has the same attributes. */
5565
5566 INLINE int
5567 lookup_face (f, attr, c, base_face)
5568 struct frame *f;
5569 Lisp_Object *attr;
5570 int c;
5571 struct face *base_face;
5572 {
5573 struct face_cache *cache = FRAME_FACE_CACHE (f);
5574 unsigned hash;
5575 int i;
5576 struct face *face;
5577
5578 xassert (cache != NULL);
5579 check_lface_attrs (attr);
5580
5581 /* Look up ATTR in the face cache. */
5582 hash = lface_hash (attr);
5583 i = hash % FACE_CACHE_BUCKETS_SIZE;
5584
5585 for (face = cache->buckets[i]; face; face = face->next)
5586 if (face->hash == hash
5587 && (!FRAME_WINDOW_P (f)
5588 || FACE_SUITABLE_FOR_CHAR_P (face, c))
5589 && lface_equal_p (face->lface, attr))
5590 break;
5591
5592 /* If not found, realize a new face. */
5593 if (face == NULL)
5594 face = realize_face (cache, attr, c, base_face, -1);
5595
5596 #if GLYPH_DEBUG
5597 xassert (face == FACE_FROM_ID (f, face->id));
5598
5599 /* When this function is called from face_for_char (in this case, C is
5600 a multibyte character), a fontset of a face returned by
5601 realize_face is not yet set, i.e. FACE_SUITABLE_FOR_CHAR_P (FACE,
5602 C) is not sutisfied. The fontset is set for this face by
5603 face_for_char later. */
5604 #if 0
5605 if (FRAME_WINDOW_P (f))
5606 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
5607 #endif
5608 #endif /* GLYPH_DEBUG */
5609
5610 return face->id;
5611 }
5612
5613
5614 /* Return the face id of the realized face for named face SYMBOL on
5615 frame F suitable for displaying character C. Value is -1 if the
5616 face couldn't be determined, which might happen if the default face
5617 isn't realized and cannot be realized. */
5618
5619 int
5620 lookup_named_face (f, symbol, c)
5621 struct frame *f;
5622 Lisp_Object symbol;
5623 int c;
5624 {
5625 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5626 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5627 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5628
5629 if (default_face == NULL)
5630 {
5631 if (!realize_basic_faces (f))
5632 return -1;
5633 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5634 }
5635
5636 get_lface_attributes (f, symbol, symbol_attrs, 1);
5637 bcopy (default_face->lface, attrs, sizeof attrs);
5638 merge_face_vectors (f, symbol_attrs, attrs, 0);
5639
5640 return lookup_face (f, attrs, c, NULL);
5641 }
5642
5643
5644 /* Return the ID of the realized ASCII face of Lisp face with ID
5645 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
5646
5647 int
5648 ascii_face_of_lisp_face (f, lface_id)
5649 struct frame *f;
5650 int lface_id;
5651 {
5652 int face_id;
5653
5654 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
5655 {
5656 Lisp_Object face_name = lface_id_to_name[lface_id];
5657 face_id = lookup_named_face (f, face_name, 0);
5658 }
5659 else
5660 face_id = -1;
5661
5662 return face_id;
5663 }
5664
5665
5666 /* Return a face for charset ASCII that is like the face with id
5667 FACE_ID on frame F, but has a font that is STEPS steps smaller.
5668 STEPS < 0 means larger. Value is the id of the face. */
5669
5670 int
5671 smaller_face (f, face_id, steps)
5672 struct frame *f;
5673 int face_id, steps;
5674 {
5675 #ifdef HAVE_WINDOW_SYSTEM
5676 struct face *face;
5677 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5678 int pt, last_pt, last_height;
5679 int delta;
5680 int new_face_id;
5681 struct face *new_face;
5682
5683 /* If not called for an X frame, just return the original face. */
5684 if (FRAME_TERMCAP_P (f))
5685 return face_id;
5686
5687 /* Try in increments of 1/2 pt. */
5688 delta = steps < 0 ? 5 : -5;
5689 steps = abs (steps);
5690
5691 face = FACE_FROM_ID (f, face_id);
5692 bcopy (face->lface, attrs, sizeof attrs);
5693 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
5694 new_face_id = face_id;
5695 last_height = FONT_HEIGHT (face->font);
5696
5697 while (steps
5698 && pt + delta > 0
5699 /* Give up if we cannot find a font within 10pt. */
5700 && abs (last_pt - pt) < 100)
5701 {
5702 /* Look up a face for a slightly smaller/larger font. */
5703 pt += delta;
5704 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
5705 new_face_id = lookup_face (f, attrs, 0, NULL);
5706 new_face = FACE_FROM_ID (f, new_face_id);
5707
5708 /* If height changes, count that as one step. */
5709 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
5710 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
5711 {
5712 --steps;
5713 last_height = FONT_HEIGHT (new_face->font);
5714 last_pt = pt;
5715 }
5716 }
5717
5718 return new_face_id;
5719
5720 #else /* not HAVE_WINDOW_SYSTEM */
5721
5722 return face_id;
5723
5724 #endif /* not HAVE_WINDOW_SYSTEM */
5725 }
5726
5727
5728 /* Return a face for charset ASCII that is like the face with id
5729 FACE_ID on frame F, but has height HEIGHT. */
5730
5731 int
5732 face_with_height (f, face_id, height)
5733 struct frame *f;
5734 int face_id;
5735 int height;
5736 {
5737 #ifdef HAVE_WINDOW_SYSTEM
5738 struct face *face;
5739 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5740
5741 if (FRAME_TERMCAP_P (f)
5742 || height <= 0)
5743 return face_id;
5744
5745 face = FACE_FROM_ID (f, face_id);
5746 bcopy (face->lface, attrs, sizeof attrs);
5747 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
5748 face_id = lookup_face (f, attrs, 0, NULL);
5749 #endif /* HAVE_WINDOW_SYSTEM */
5750
5751 return face_id;
5752 }
5753
5754
5755 /* Return the face id of the realized face for named face SYMBOL on
5756 frame F suitable for displaying character C, and use attributes of
5757 the face FACE_ID for attributes that aren't completely specified by
5758 SYMBOL. This is like lookup_named_face, except that the default
5759 attributes come from FACE_ID, not from the default face. FACE_ID
5760 is assumed to be already realized. */
5761
5762 int
5763 lookup_derived_face (f, symbol, c, face_id)
5764 struct frame *f;
5765 Lisp_Object symbol;
5766 int c;
5767 int face_id;
5768 {
5769 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5770 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5771 struct face *default_face = FACE_FROM_ID (f, face_id);
5772
5773 if (!default_face)
5774 abort ();
5775
5776 get_lface_attributes (f, symbol, symbol_attrs, 1);
5777 bcopy (default_face->lface, attrs, sizeof attrs);
5778 merge_face_vectors (f, symbol_attrs, attrs, 0);
5779 return lookup_face (f, attrs, c, default_face);
5780 }
5781
5782 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
5783 Sface_attributes_as_vector, 1, 1, 0,
5784 doc: /* Return a vector of face attributes corresponding to PLIST. */)
5785 (plist)
5786 Lisp_Object plist;
5787 {
5788 Lisp_Object lface;
5789 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
5790 Qunspecified);
5791 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
5792 1, 0);
5793 return lface;
5794 }
5795
5796
5797 \f
5798 /***********************************************************************
5799 Face capability testing
5800 ***********************************************************************/
5801
5802
5803 /* If the distance (as returned by color_distance) between two colors is
5804 less than this, then they are considered the same, for determining
5805 whether a color is supported or not. The range of values is 0-65535. */
5806
5807 #define TTY_SAME_COLOR_THRESHOLD 10000
5808
5809 #ifdef HAVE_WINDOW_SYSTEM
5810
5811 /* Return non-zero if all the face attributes in ATTRS are supported
5812 on the window-system frame F.
5813
5814 The definition of `supported' is somewhat heuristic, but basically means
5815 that a face containing all the attributes in ATTRS, when merged with the
5816 default face for display, can be represented in a way that's
5817
5818 \(1) different in appearance than the default face, and
5819 \(2) `close in spirit' to what the attributes specify, if not exact. */
5820
5821 static int
5822 x_supports_face_attributes_p (f, attrs, def_face)
5823 struct frame *f;
5824 Lisp_Object *attrs;
5825 struct face *def_face;
5826 {
5827 Lisp_Object *def_attrs = def_face->lface;
5828
5829 /* Check that other specified attributes are different that the default
5830 face. */
5831 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
5832 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
5833 def_attrs[LFACE_UNDERLINE_INDEX]))
5834 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
5835 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
5836 def_attrs[LFACE_INVERSE_INDEX]))
5837 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
5838 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
5839 def_attrs[LFACE_FOREGROUND_INDEX]))
5840 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
5841 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
5842 def_attrs[LFACE_BACKGROUND_INDEX]))
5843 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5844 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
5845 def_attrs[LFACE_STIPPLE_INDEX]))
5846 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5847 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
5848 def_attrs[LFACE_OVERLINE_INDEX]))
5849 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5850 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
5851 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
5852 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5853 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
5854 def_attrs[LFACE_BOX_INDEX])))
5855 return 0;
5856
5857 /* Check font-related attributes, as those are the most commonly
5858 "unsupported" on a window-system (because of missing fonts). */
5859 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5860 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5861 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
5862 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
5863 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5864 || !UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX]))
5865 {
5866 struct face *face;
5867 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
5868
5869 bcopy (def_attrs, merged_attrs, sizeof merged_attrs);
5870
5871 merge_face_vectors (f, attrs, merged_attrs, 0);
5872
5873 face = FACE_FROM_ID (f, lookup_face (f, merged_attrs, 0, 0));
5874
5875 if (! face)
5876 error ("cannot make face");
5877
5878 /* If the font is the same, then not supported. */
5879 if (face->font == def_face->font)
5880 return 0;
5881 }
5882
5883 /* Everything checks out, this face is supported. */
5884 return 1;
5885 }
5886
5887 #endif /* HAVE_WINDOW_SYSTEM */
5888
5889 /* Return non-zero if all the face attributes in ATTRS are supported
5890 on the tty frame F.
5891
5892 The definition of `supported' is somewhat heuristic, but basically means
5893 that a face containing all the attributes in ATTRS, when merged
5894 with the default face for display, can be represented in a way that's
5895
5896 \(1) different in appearance than the default face, and
5897 \(2) `close in spirit' to what the attributes specify, if not exact.
5898
5899 Point (2) implies that a `:weight black' attribute will be satisfied
5900 by any terminal that can display bold, and a `:foreground "yellow"' as
5901 long as the terminal can display a yellowish color, but `:slant italic'
5902 will _not_ be satisfied by the tty display code's automatic
5903 substitution of a `dim' face for italic. */
5904
5905 static int
5906 tty_supports_face_attributes_p (f, attrs, def_face)
5907 struct frame *f;
5908 Lisp_Object *attrs;
5909 struct face *def_face;
5910 {
5911 int weight;
5912 Lisp_Object val, fg, bg;
5913 XColor fg_tty_color, fg_std_color;
5914 XColor bg_tty_color, bg_std_color;
5915 unsigned test_caps = 0;
5916 Lisp_Object *def_attrs = def_face->lface;
5917
5918
5919 /* First check some easy-to-check stuff; ttys support none of the
5920 following attributes, so we can just return false if any are requested
5921 (even if `nominal' values are specified, we should still return false,
5922 as that will be the same value that the default face uses). We
5923 consider :slant unsupportable on ttys, even though the face code
5924 actually `fakes' them using a dim attribute if possible. This is
5925 because the faked result is too different from what the face
5926 specifies. */
5927 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5928 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5929 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5930 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5931 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5932 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5933 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5934 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5935 return 0;
5936
5937
5938 /* Test for terminal `capabilities' (non-color character attributes). */
5939
5940 /* font weight (bold/dim) */
5941 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5942 if (weight >= 0)
5943 {
5944 int def_weight = face_numeric_weight (def_attrs[LFACE_WEIGHT_INDEX]);
5945
5946 if (weight > XLFD_WEIGHT_MEDIUM)
5947 {
5948 if (def_weight > XLFD_WEIGHT_MEDIUM)
5949 return 0; /* same as default */
5950 test_caps = TTY_CAP_BOLD;
5951 }
5952 else if (weight < XLFD_WEIGHT_MEDIUM)
5953 {
5954 if (def_weight < XLFD_WEIGHT_MEDIUM)
5955 return 0; /* same as default */
5956 test_caps = TTY_CAP_DIM;
5957 }
5958 else if (def_weight == XLFD_WEIGHT_MEDIUM)
5959 return 0; /* same as default */
5960 }
5961
5962 /* underlining */
5963 val = attrs[LFACE_UNDERLINE_INDEX];
5964 if (!UNSPECIFIEDP (val))
5965 {
5966 if (STRINGP (val))
5967 return 0; /* ttys can't use colored underlines */
5968 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5969 return 0; /* same as default */
5970 else
5971 test_caps |= TTY_CAP_UNDERLINE;
5972 }
5973
5974 /* inverse video */
5975 val = attrs[LFACE_INVERSE_INDEX];
5976 if (!UNSPECIFIEDP (val))
5977 {
5978 if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5979 return 0; /* same as default */
5980 else
5981 test_caps |= TTY_CAP_INVERSE;
5982 }
5983
5984
5985 /* Color testing. */
5986
5987 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5988 we use them when calling `tty_capable_p' below, even if the face
5989 specifies no colors. */
5990 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5991 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
5992
5993 /* Check if foreground color is close enough. */
5994 fg = attrs[LFACE_FOREGROUND_INDEX];
5995 if (STRINGP (fg))
5996 {
5997 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
5998
5999 if (face_attr_equal_p (fg, def_fg))
6000 return 0; /* same as default */
6001 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
6002 return 0; /* not a valid color */
6003 else if (color_distance (&fg_tty_color, &fg_std_color)
6004 > TTY_SAME_COLOR_THRESHOLD)
6005 return 0; /* displayed color is too different */
6006 else
6007 /* Make sure the color is really different than the default. */
6008 {
6009 XColor def_fg_color;
6010 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
6011 && (color_distance (&fg_tty_color, &def_fg_color)
6012 <= TTY_SAME_COLOR_THRESHOLD))
6013 return 0;
6014 }
6015 }
6016
6017 /* Check if background color is close enough. */
6018 bg = attrs[LFACE_BACKGROUND_INDEX];
6019 if (STRINGP (bg))
6020 {
6021 Lisp_Object def_bg = def_attrs[LFACE_FOREGROUND_INDEX];
6022
6023 if (face_attr_equal_p (bg, def_bg))
6024 return 0; /* same as default */
6025 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
6026 return 0; /* not a valid color */
6027 else if (color_distance (&bg_tty_color, &bg_std_color)
6028 > TTY_SAME_COLOR_THRESHOLD)
6029 return 0; /* displayed color is too different */
6030 else
6031 /* Make sure the color is really different than the default. */
6032 {
6033 XColor def_bg_color;
6034 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
6035 && (color_distance (&bg_tty_color, &def_bg_color)
6036 <= TTY_SAME_COLOR_THRESHOLD))
6037 return 0;
6038 }
6039 }
6040
6041 /* If both foreground and background are requested, see if the
6042 distance between them is OK. We just check to see if the distance
6043 between the tty's foreground and background is close enough to the
6044 distance between the standard foreground and background. */
6045 if (STRINGP (fg) && STRINGP (bg))
6046 {
6047 int delta_delta
6048 = (color_distance (&fg_std_color, &bg_std_color)
6049 - color_distance (&fg_tty_color, &bg_tty_color));
6050 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
6051 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
6052 return 0;
6053 }
6054
6055
6056 /* See if the capabilities we selected above are supported, with the
6057 given colors. */
6058 if (test_caps != 0 &&
6059 ! tty_capable_p (f, test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
6060 return 0;
6061
6062
6063 /* Hmmm, everything checks out, this terminal must support this face. */
6064 return 1;
6065 }
6066
6067
6068 DEFUN ("display-supports-face-attributes-p",
6069 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
6070 1, 2, 0,
6071 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
6072 The optional argument DISPLAY can be a display name, a frame, or
6073 nil (meaning the selected frame's display)
6074
6075 The definition of `supported' is somewhat heuristic, but basically means
6076 that a face containing all the attributes in ATTRIBUTES, when merged
6077 with the default face for display, can be represented in a way that's
6078
6079 \(1) different in appearance than the default face, and
6080 \(2) `close in spirit' to what the attributes specify, if not exact.
6081
6082 Point (2) implies that a `:weight black' attribute will be satisfied by
6083 any display that can display bold, and a `:foreground \"yellow\"' as long
6084 as it can display a yellowish color, but `:slant italic' will _not_ be
6085 satisfied by the tty display code's automatic substitution of a `dim'
6086 face for italic. */)
6087 (attributes, display)
6088 Lisp_Object attributes, display;
6089 {
6090 int supports, i;
6091 Lisp_Object frame;
6092 struct frame *f;
6093 struct face *def_face;
6094 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6095
6096 if (noninteractive || !initialized)
6097 /* We may not be able to access low-level face information in batch
6098 mode, or before being dumped, and this function is not going to
6099 be very useful in those cases anyway, so just give up. */
6100 return Qnil;
6101
6102 if (NILP (display))
6103 frame = selected_frame;
6104 else if (FRAMEP (display))
6105 frame = display;
6106 else
6107 {
6108 /* Find any frame on DISPLAY. */
6109 Lisp_Object fl_tail;
6110
6111 frame = Qnil;
6112 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
6113 {
6114 frame = XCAR (fl_tail);
6115 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
6116 XFRAME (frame)->param_alist)),
6117 display)))
6118 break;
6119 }
6120 }
6121
6122 CHECK_LIVE_FRAME (frame);
6123 f = XFRAME (frame);
6124
6125 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
6126 attrs[i] = Qunspecified;
6127 merge_face_ref (f, attributes, attrs, 1, 0);
6128
6129 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6130 if (def_face == NULL)
6131 {
6132 if (! realize_basic_faces (f))
6133 error ("Cannot realize default face");
6134 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6135 }
6136
6137 /* Dispatch to the appropriate handler. */
6138 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6139 supports = tty_supports_face_attributes_p (f, attrs, def_face);
6140 #ifdef HAVE_WINDOW_SYSTEM
6141 else
6142 supports = x_supports_face_attributes_p (f, attrs, def_face);
6143 #endif
6144
6145 return supports ? Qt : Qnil;
6146 }
6147
6148 \f
6149 /***********************************************************************
6150 Font selection
6151 ***********************************************************************/
6152
6153 DEFUN ("internal-set-font-selection-order",
6154 Finternal_set_font_selection_order,
6155 Sinternal_set_font_selection_order, 1, 1, 0,
6156 doc: /* Set font selection order for face font selection to ORDER.
6157 ORDER must be a list of length 4 containing the symbols `:width',
6158 `:height', `:weight', and `:slant'. Face attributes appearing
6159 first in ORDER are matched first, e.g. if `:height' appears before
6160 `:weight' in ORDER, font selection first tries to find a font with
6161 a suitable height, and then tries to match the font weight.
6162 Value is ORDER. */)
6163 (order)
6164 Lisp_Object order;
6165 {
6166 Lisp_Object list;
6167 int i;
6168 int indices[DIM (font_sort_order)];
6169
6170 CHECK_LIST (order);
6171 bzero (indices, sizeof indices);
6172 i = 0;
6173
6174 for (list = order;
6175 CONSP (list) && i < DIM (indices);
6176 list = XCDR (list), ++i)
6177 {
6178 Lisp_Object attr = XCAR (list);
6179 int xlfd;
6180
6181 if (EQ (attr, QCwidth))
6182 xlfd = XLFD_SWIDTH;
6183 else if (EQ (attr, QCheight))
6184 xlfd = XLFD_POINT_SIZE;
6185 else if (EQ (attr, QCweight))
6186 xlfd = XLFD_WEIGHT;
6187 else if (EQ (attr, QCslant))
6188 xlfd = XLFD_SLANT;
6189 else
6190 break;
6191
6192 if (indices[i] != 0)
6193 break;
6194 indices[i] = xlfd;
6195 }
6196
6197 if (!NILP (list) || i != DIM (indices))
6198 signal_error ("Invalid font sort order", order);
6199 for (i = 0; i < DIM (font_sort_order); ++i)
6200 if (indices[i] == 0)
6201 signal_error ("Invalid font sort order", order);
6202
6203 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
6204 {
6205 bcopy (indices, font_sort_order, sizeof font_sort_order);
6206 free_all_realized_faces (Qnil);
6207 }
6208
6209 return Qnil;
6210 }
6211
6212
6213 DEFUN ("internal-set-alternative-font-family-alist",
6214 Finternal_set_alternative_font_family_alist,
6215 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
6216 doc: /* Define alternative font families to try in face font selection.
6217 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6218 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
6219 be found. Value is ALIST. */)
6220 (alist)
6221 Lisp_Object alist;
6222 {
6223 CHECK_LIST (alist);
6224 Vface_alternative_font_family_alist = alist;
6225 free_all_realized_faces (Qnil);
6226 return alist;
6227 }
6228
6229
6230 DEFUN ("internal-set-alternative-font-registry-alist",
6231 Finternal_set_alternative_font_registry_alist,
6232 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
6233 doc: /* Define alternative font registries to try in face font selection.
6234 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
6235 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
6236 be found. Value is ALIST. */)
6237 (alist)
6238 Lisp_Object alist;
6239 {
6240 CHECK_LIST (alist);
6241 Vface_alternative_font_registry_alist = alist;
6242 free_all_realized_faces (Qnil);
6243 return alist;
6244 }
6245
6246
6247 #ifdef HAVE_WINDOW_SYSTEM
6248
6249 /* Value is non-zero if FONT is the name of a scalable font. The
6250 X11R6 XLFD spec says that point size, pixel size, and average width
6251 are zero for scalable fonts. Intlfonts contain at least one
6252 scalable font ("*-muleindian-1") for which this isn't true, so we
6253 just test average width. */
6254
6255 static int
6256 font_scalable_p (font)
6257 struct font_name *font;
6258 {
6259 char *s = font->fields[XLFD_AVGWIDTH];
6260 return (*s == '0' && *(s + 1) == '\0')
6261 #ifdef WINDOWSNT
6262 /* Windows implementation of XLFD is slightly broken for backward
6263 compatibility with previous broken versions, so test for
6264 wildcards as well as 0. */
6265 || *s == '*'
6266 #endif
6267 ;
6268 }
6269
6270
6271 /* Ignore the difference of font point size less than this value. */
6272
6273 #define FONT_POINT_SIZE_QUANTUM 5
6274
6275 /* Value is non-zero if FONT1 is a better match for font attributes
6276 VALUES than FONT2. VALUES is an array of face attribute values in
6277 font sort order. COMPARE_PT_P zero means don't compare point
6278 sizes. AVGWIDTH, if not zero, is a specified font average width
6279 to compare with. */
6280
6281 static int
6282 better_font_p (values, font1, font2, compare_pt_p, avgwidth)
6283 int *values;
6284 struct font_name *font1, *font2;
6285 int compare_pt_p, avgwidth;
6286 {
6287 int i;
6288
6289 for (i = 0; i < DIM (font_sort_order); ++i)
6290 {
6291 int xlfd_idx = font_sort_order[i];
6292
6293 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
6294 {
6295 int delta1, delta2;
6296
6297 if (xlfd_idx == XLFD_POINT_SIZE)
6298 {
6299 delta1 = abs (values[i] - (font1->numeric[xlfd_idx]
6300 / font1->rescale_ratio));
6301 delta2 = abs (values[i] - (font2->numeric[xlfd_idx]
6302 / font2->rescale_ratio));
6303 if (abs (delta1 - delta2) < FONT_POINT_SIZE_QUANTUM)
6304 continue;
6305 }
6306 else
6307 {
6308 delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
6309 delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
6310 }
6311
6312 if (delta1 > delta2)
6313 return 0;
6314 else if (delta1 < delta2)
6315 return 1;
6316 else
6317 {
6318 /* The difference may be equal because, e.g., the face
6319 specifies `italic' but we have only `regular' and
6320 `oblique'. Prefer `oblique' in this case. */
6321 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
6322 && font1->numeric[xlfd_idx] > values[i]
6323 && font2->numeric[xlfd_idx] < values[i])
6324 return 1;
6325 }
6326 }
6327 }
6328
6329 if (avgwidth)
6330 {
6331 int delta1 = abs (avgwidth - font1->numeric[XLFD_AVGWIDTH]);
6332 int delta2 = abs (avgwidth - font2->numeric[XLFD_AVGWIDTH]);
6333 if (delta1 > delta2)
6334 return 0;
6335 else if (delta1 < delta2)
6336 return 1;
6337 }
6338
6339 if (! compare_pt_p)
6340 {
6341 /* We prefer a real scalable font; i.e. not what autoscaled. */
6342 int auto_scaled_1 = (font1->numeric[XLFD_POINT_SIZE] == 0
6343 && font1->numeric[XLFD_RESY] > 0);
6344 int auto_scaled_2 = (font2->numeric[XLFD_POINT_SIZE] == 0
6345 && font2->numeric[XLFD_RESY] > 0);
6346
6347 if (auto_scaled_1 != auto_scaled_2)
6348 return auto_scaled_2;
6349 }
6350
6351 return font1->registry_priority < font2->registry_priority;
6352 }
6353
6354
6355 /* Value is non-zero if FONT is an exact match for face attributes in
6356 SPECIFIED. SPECIFIED is an array of face attribute values in font
6357 sort order. AVGWIDTH, if non-zero, is an average width to compare
6358 with. */
6359
6360 static int
6361 exact_face_match_p (specified, font, avgwidth)
6362 int *specified;
6363 struct font_name *font;
6364 int avgwidth;
6365 {
6366 int i;
6367
6368 for (i = 0; i < DIM (font_sort_order); ++i)
6369 if (specified[i] != font->numeric[font_sort_order[i]])
6370 break;
6371
6372 return (i == DIM (font_sort_order)
6373 && (avgwidth <= 0
6374 || avgwidth == font->numeric[XLFD_AVGWIDTH]));
6375 }
6376
6377
6378 /* Value is the name of a scaled font, generated from scalable font
6379 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
6380 Value is allocated from heap. */
6381
6382 static char *
6383 build_scalable_font_name (f, font, specified_pt)
6384 struct frame *f;
6385 struct font_name *font;
6386 int specified_pt;
6387 {
6388 char pixel_size[20];
6389 int pixel_value;
6390 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
6391 double pt;
6392
6393 /* If scalable font is for a specific resolution, compute
6394 the point size we must specify from the resolution of
6395 the display and the specified resolution of the font. */
6396 if (font->numeric[XLFD_RESY] != 0)
6397 {
6398 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
6399 pixel_value = font->numeric[XLFD_RESY] / (PT_PER_INCH * 10.0) * pt;
6400 }
6401 else
6402 {
6403 pt = specified_pt;
6404 pixel_value = resy / (PT_PER_INCH * 10.0) * pt;
6405 }
6406 /* We may need a font of the different size. */
6407 pixel_value *= font->rescale_ratio;
6408
6409 /* We should keep POINT_SIZE 0. Otherwise, X server can't open a
6410 font of the specified PIXEL_SIZE. */
6411 #if 0
6412 { /* Set point size of the font. */
6413 char point_size[20];
6414 sprintf (point_size, "%d", (int) pt);
6415 font->fields[XLFD_POINT_SIZE] = point_size;
6416 font->numeric[XLFD_POINT_SIZE] = pt;
6417 }
6418 #endif
6419
6420 /* Set pixel size. */
6421 sprintf (pixel_size, "%d", pixel_value);
6422 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
6423 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
6424
6425 /* If font doesn't specify its resolution, use the
6426 resolution of the display. */
6427 if (font->numeric[XLFD_RESY] == 0)
6428 {
6429 char buffer[20];
6430 sprintf (buffer, "%d", (int) resy);
6431 font->fields[XLFD_RESY] = buffer;
6432 font->numeric[XLFD_RESY] = resy;
6433 }
6434
6435 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
6436 {
6437 char buffer[20];
6438 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
6439 sprintf (buffer, "%d", resx);
6440 font->fields[XLFD_RESX] = buffer;
6441 font->numeric[XLFD_RESX] = resx;
6442 }
6443
6444 return build_font_name (font);
6445 }
6446
6447
6448 /* Value is non-zero if we are allowed to use scalable font FONT. We
6449 can't run a Lisp function here since this function may be called
6450 with input blocked. */
6451
6452 static int
6453 may_use_scalable_font_p (font)
6454 const char *font;
6455 {
6456 if (EQ (Vscalable_fonts_allowed, Qt))
6457 return 1;
6458 else if (CONSP (Vscalable_fonts_allowed))
6459 {
6460 Lisp_Object tail, regexp;
6461
6462 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
6463 {
6464 regexp = XCAR (tail);
6465 if (STRINGP (regexp)
6466 && fast_c_string_match_ignore_case (regexp, font) >= 0)
6467 return 1;
6468 }
6469 }
6470
6471 return 0;
6472 }
6473
6474
6475
6476 /* Return the name of the best matching font for face attributes ATTRS
6477 in the array of font_name structures FONTS which contains NFONTS
6478 elements. WIDTH_RATIO is a factor with which to multiply average
6479 widths if ATTRS specifies such a width.
6480
6481 Value is a font name which is allocated from the heap. FONTS is
6482 freed by this function.
6483
6484 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6485 indicate whether the resulting font should be drawn using overstrike
6486 to simulate bold-face. */
6487
6488 static char *
6489 best_matching_font (f, attrs, fonts, nfonts, width_ratio, needs_overstrike)
6490 struct frame *f;
6491 Lisp_Object *attrs;
6492 struct font_name *fonts;
6493 int nfonts;
6494 int width_ratio;
6495 int *needs_overstrike;
6496 {
6497 char *font_name;
6498 struct font_name *best;
6499 int i, pt = 0;
6500 int specified[5];
6501 int exact_p, avgwidth;
6502
6503 if (nfonts == 0)
6504 return NULL;
6505
6506 /* Make specified font attributes available in `specified',
6507 indexed by sort order. */
6508 for (i = 0; i < DIM (font_sort_order); ++i)
6509 {
6510 int xlfd_idx = font_sort_order[i];
6511
6512 if (xlfd_idx == XLFD_SWIDTH)
6513 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
6514 else if (xlfd_idx == XLFD_POINT_SIZE)
6515 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
6516 else if (xlfd_idx == XLFD_WEIGHT)
6517 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
6518 else if (xlfd_idx == XLFD_SLANT)
6519 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
6520 else
6521 abort ();
6522 }
6523
6524 avgwidth = (UNSPECIFIEDP (attrs[LFACE_AVGWIDTH_INDEX])
6525 ? 0
6526 : XFASTINT (attrs[LFACE_AVGWIDTH_INDEX]) * width_ratio);
6527
6528 exact_p = 0;
6529
6530 if (needs_overstrike)
6531 *needs_overstrike = 0;
6532
6533 /* Start with the first non-scalable font in the list. */
6534 for (i = 0; i < nfonts; ++i)
6535 if (!font_scalable_p (fonts + i))
6536 break;
6537
6538 /* Find the best match among the non-scalable fonts. */
6539 if (i < nfonts)
6540 {
6541 best = fonts + i;
6542
6543 for (i = 1; i < nfonts; ++i)
6544 if (!font_scalable_p (fonts + i)
6545 && better_font_p (specified, fonts + i, best, 1, avgwidth))
6546 {
6547 best = fonts + i;
6548
6549 exact_p = exact_face_match_p (specified, best, avgwidth);
6550 if (exact_p)
6551 break;
6552 }
6553 }
6554 else
6555 best = NULL;
6556
6557 /* Unless we found an exact match among non-scalable fonts, see if
6558 we can find a better match among scalable fonts. */
6559 if (!exact_p)
6560 {
6561 /* A scalable font is better if
6562
6563 1. its weight, slant, swidth attributes are better, or.
6564
6565 2. the best non-scalable font doesn't have the required
6566 point size, and the scalable fonts weight, slant, swidth
6567 isn't worse. */
6568
6569 int non_scalable_has_exact_height_p;
6570
6571 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
6572 non_scalable_has_exact_height_p = 1;
6573 else
6574 non_scalable_has_exact_height_p = 0;
6575
6576 for (i = 0; i < nfonts; ++i)
6577 if (font_scalable_p (fonts + i))
6578 {
6579 if (best == NULL
6580 || better_font_p (specified, fonts + i, best, 0, 0)
6581 || (!non_scalable_has_exact_height_p
6582 && !better_font_p (specified, best, fonts + i, 0, 0)))
6583 {
6584 non_scalable_has_exact_height_p = 1;
6585 best = fonts + i;
6586 }
6587 }
6588
6589 if (needs_overstrike)
6590 {
6591 enum xlfd_weight want_weight = specified[XLFD_WEIGHT];
6592 enum xlfd_weight got_weight = best->numeric[XLFD_WEIGHT];
6593
6594 if (want_weight > XLFD_WEIGHT_MEDIUM && want_weight > got_weight)
6595 {
6596 /* We want a bold font, but didn't get one; try to use
6597 overstriking instead to simulate bold-face. However,
6598 don't overstrike an already-bold fontn unless the
6599 desired weight grossly exceeds the available weight. */
6600 if (got_weight > XLFD_WEIGHT_MEDIUM)
6601 *needs_overstrike = (got_weight - want_weight) > 2;
6602 else
6603 *needs_overstrike = 1;
6604 }
6605 }
6606 }
6607
6608 if (font_scalable_p (best))
6609 font_name = build_scalable_font_name (f, best, pt);
6610 else
6611 font_name = build_font_name (best);
6612
6613 /* Free font_name structures. */
6614 free_font_names (fonts, nfonts);
6615
6616 return font_name;
6617 }
6618
6619
6620 /* Get a list of matching fonts on frame F, considering FAMILY
6621 and alternative font families from Vface_alternative_font_registry_alist.
6622
6623 FAMILY is the font family whose alternatives are considered.
6624
6625 REGISTRY, if a string, specifies a font registry and encoding to
6626 match. A value of nil means include fonts of any registry and
6627 encoding.
6628
6629 Return in *FONTS a pointer to a vector of font_name structures for
6630 the fonts matched. Value is the number of fonts found. */
6631
6632 static int
6633 try_alternative_families (f, family, registry, fonts)
6634 struct frame *f;
6635 Lisp_Object family, registry;
6636 struct font_name **fonts;
6637 {
6638 Lisp_Object alter;
6639 int nfonts = 0;
6640
6641 nfonts = font_list (f, Qnil, family, registry, fonts);
6642 if (nfonts == 0)
6643 {
6644 /* Try alternative font families. */
6645 alter = Fassoc (family, Vface_alternative_font_family_alist);
6646 if (CONSP (alter))
6647 {
6648 for (alter = XCDR (alter);
6649 CONSP (alter) && nfonts == 0;
6650 alter = XCDR (alter))
6651 {
6652 if (STRINGP (XCAR (alter)))
6653 nfonts = font_list (f, Qnil, XCAR (alter), registry, fonts);
6654 }
6655 }
6656
6657 /* Try all scalable fonts before giving up. */
6658 if (nfonts == 0 && ! EQ (Vscalable_fonts_allowed, Qt))
6659 {
6660 int count = SPECPDL_INDEX ();
6661 specbind (Qscalable_fonts_allowed, Qt);
6662 nfonts = try_alternative_families (f, family, registry, fonts);
6663 unbind_to (count, Qnil);
6664 }
6665 }
6666 return nfonts;
6667 }
6668
6669
6670 /* Get a list of matching fonts on frame F.
6671
6672 FAMILY, if a string, specifies a font family derived from the fontset.
6673 It is only used if the face does not specify any family in ATTRS or
6674 if we cannot find any font of the face's family.
6675
6676 REGISTRY, if a string, specifies a font registry and encoding to
6677 match. A value of nil means include fonts of any registry and
6678 encoding.
6679
6680 If PREFER_FACE_FAMILY is nonzero, perfer face's family to FAMILY.
6681 Otherwise, prefer FAMILY.
6682
6683 Return in *FONTS a pointer to a vector of font_name structures for
6684 the fonts matched. Value is the number of fonts found. */
6685
6686 static int
6687 try_font_list (f, attrs, family, registry, fonts, prefer_face_family)
6688 struct frame *f;
6689 Lisp_Object *attrs;
6690 Lisp_Object family, registry;
6691 struct font_name **fonts;
6692 int prefer_face_family;
6693 {
6694 int nfonts = 0;
6695 Lisp_Object face_family = attrs[LFACE_FAMILY_INDEX];
6696 Lisp_Object try_family;
6697
6698 try_family = (prefer_face_family || NILP (family)) ? face_family : family;
6699
6700 if (STRINGP (try_family))
6701 nfonts = try_alternative_families (f, try_family, registry, fonts);
6702
6703 #ifdef MAC_OS
6704 /* When realizing the default face and a font spec does not matched
6705 exactly, Emacs looks for ones with the same registry as the
6706 default font. On the Mac, this is mac-roman, which does not work
6707 if the family is -etl-fixed, e.g. The following widens the
6708 choices and fixes that problem. */
6709 if (nfonts == 0 && STRINGP (try_family) && STRINGP (registry)
6710 && xstricmp (SDATA (registry), "mac-roman") == 0)
6711 nfonts = try_alternative_families (f, try_family, Qnil, fonts);
6712 #endif
6713
6714 if (EQ (try_family, family))
6715 family = face_family;
6716
6717 if (nfonts == 0 && STRINGP (family))
6718 nfonts = try_alternative_families (f, family, registry, fonts);
6719
6720 /* Try font family of the default face or "fixed". */
6721 if (nfonts == 0)
6722 {
6723 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6724 if (default_face)
6725 family = default_face->lface[LFACE_FAMILY_INDEX];
6726 else
6727 family = build_string ("fixed");
6728 nfonts = font_list (f, Qnil, family, registry, fonts);
6729 }
6730
6731 /* Try any family with the given registry. */
6732 if (nfonts == 0)
6733 nfonts = try_alternative_families (f, Qnil, registry, fonts);
6734
6735 return nfonts;
6736 }
6737
6738
6739 /* Return the fontset id of the base fontset name or alias name given
6740 by the fontset attribute of ATTRS. Value is -1 if the fontset
6741 attribute of ATTRS doesn't name a fontset. */
6742
6743 static int
6744 face_fontset (attrs)
6745 Lisp_Object *attrs;
6746 {
6747 Lisp_Object name;
6748
6749 name = attrs[LFACE_FONT_INDEX];
6750 if (!STRINGP (name))
6751 return -1;
6752 return fs_query_fontset (name, 0);
6753 }
6754
6755
6756 /* Choose a name of font to use on frame F to display character C with
6757 Lisp face attributes specified by ATTRS. The font name is
6758 determined by the font-related attributes in ATTRS and the name
6759 pattern for C in FONTSET. Value is the font name which is
6760 allocated from the heap and must be freed by the caller, or NULL if
6761 we can get no information about the font name of C. It is assured
6762 that we always get some information for a single byte
6763 character.
6764
6765 If NEEDS_OVERSTRIKE is non-zero, a boolean is returned in it to
6766 indicate whether the resulting font should be drawn using overstrike
6767 to simulate bold-face. */
6768
6769 static char *
6770 choose_face_font (f, attrs, fontset, c, needs_overstrike)
6771 struct frame *f;
6772 Lisp_Object *attrs;
6773 int fontset, c;
6774 int *needs_overstrike;
6775 {
6776 Lisp_Object pattern;
6777 char *font_name = NULL;
6778 struct font_name *fonts;
6779 int nfonts, width_ratio;
6780
6781 if (needs_overstrike)
6782 *needs_overstrike = 0;
6783
6784 /* Get (foundry and) family name and registry (and encoding) name of
6785 a font for C. */
6786 pattern = fontset_font_pattern (f, fontset, c);
6787 if (NILP (pattern))
6788 {
6789 xassert (!SINGLE_BYTE_CHAR_P (c));
6790 return NULL;
6791 }
6792
6793 /* If what we got is a name pattern, return it. */
6794 if (STRINGP (pattern))
6795 return xstrdup (SDATA (pattern));
6796
6797 /* Get a list of fonts matching that pattern and choose the
6798 best match for the specified face attributes from it. */
6799 nfonts = try_font_list (f, attrs, XCAR (pattern), XCDR (pattern), &fonts,
6800 (SINGLE_BYTE_CHAR_P (c)
6801 || CHAR_CHARSET (c) == charset_latin_iso8859_1));
6802 width_ratio = (SINGLE_BYTE_CHAR_P (c)
6803 ? 1
6804 : CHARSET_WIDTH (CHAR_CHARSET (c)));
6805 font_name = best_matching_font (f, attrs, fonts, nfonts, width_ratio,
6806 needs_overstrike);
6807 return font_name;
6808 }
6809
6810 #endif /* HAVE_WINDOW_SYSTEM */
6811
6812
6813 \f
6814 /***********************************************************************
6815 Face Realization
6816 ***********************************************************************/
6817
6818 /* Realize basic faces on frame F. Value is zero if frame parameters
6819 of F don't contain enough information needed to realize the default
6820 face. */
6821
6822 static int
6823 realize_basic_faces (f)
6824 struct frame *f;
6825 {
6826 int success_p = 0;
6827 int count = SPECPDL_INDEX ();
6828
6829 /* Block input here so that we won't be surprised by an X expose
6830 event, for instance, without having the faces set up. */
6831 BLOCK_INPUT;
6832 specbind (Qscalable_fonts_allowed, Qt);
6833
6834 if (realize_default_face (f))
6835 {
6836 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
6837 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
6838 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
6839 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
6840 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
6841 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
6842 realize_named_face (f, Qborder, BORDER_FACE_ID);
6843 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
6844 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
6845 realize_named_face (f, Qmenu, MENU_FACE_ID);
6846
6847 /* Reflect changes in the `menu' face in menu bars. */
6848 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
6849 {
6850 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
6851 #ifdef USE_X_TOOLKIT
6852 x_update_menu_appearance (f);
6853 #endif
6854 }
6855
6856 success_p = 1;
6857 }
6858
6859 unbind_to (count, Qnil);
6860 UNBLOCK_INPUT;
6861 return success_p;
6862 }
6863
6864
6865 /* Realize the default face on frame F. If the face is not fully
6866 specified, make it fully-specified. Attributes of the default face
6867 that are not explicitly specified are taken from frame parameters. */
6868
6869 static int
6870 realize_default_face (f)
6871 struct frame *f;
6872 {
6873 struct face_cache *c = FRAME_FACE_CACHE (f);
6874 Lisp_Object lface;
6875 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6876 Lisp_Object frame_font;
6877 struct face *face;
6878
6879 /* If the `default' face is not yet known, create it. */
6880 lface = lface_from_face_name (f, Qdefault, 0);
6881 if (NILP (lface))
6882 {
6883 Lisp_Object frame;
6884 XSETFRAME (frame, f);
6885 lface = Finternal_make_lisp_face (Qdefault, frame);
6886 }
6887
6888
6889 #ifdef HAVE_WINDOW_SYSTEM
6890 if (FRAME_WINDOW_P (f))
6891 {
6892 /* Set frame_font to the value of the `font' frame parameter. */
6893 frame_font = Fassq (Qfont, f->param_alist);
6894 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
6895 frame_font = XCDR (frame_font);
6896 set_lface_from_font_name (f, lface, frame_font,
6897 f->default_face_done_p, 1);
6898 f->default_face_done_p = 1;
6899 }
6900 #endif /* HAVE_WINDOW_SYSTEM */
6901
6902 if (!FRAME_WINDOW_P (f))
6903 {
6904 LFACE_FAMILY (lface) = build_string ("default");
6905 LFACE_SWIDTH (lface) = Qnormal;
6906 LFACE_HEIGHT (lface) = make_number (1);
6907 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
6908 LFACE_WEIGHT (lface) = Qnormal;
6909 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
6910 LFACE_SLANT (lface) = Qnormal;
6911 LFACE_AVGWIDTH (lface) = Qunspecified;
6912 }
6913
6914 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
6915 LFACE_UNDERLINE (lface) = Qnil;
6916
6917 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
6918 LFACE_OVERLINE (lface) = Qnil;
6919
6920 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
6921 LFACE_STRIKE_THROUGH (lface) = Qnil;
6922
6923 if (UNSPECIFIEDP (LFACE_BOX (lface)))
6924 LFACE_BOX (lface) = Qnil;
6925
6926 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
6927 LFACE_INVERSE (lface) = Qnil;
6928
6929 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
6930 {
6931 /* This function is called so early that colors are not yet
6932 set in the frame parameter list. */
6933 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
6934
6935 if (CONSP (color) && STRINGP (XCDR (color)))
6936 LFACE_FOREGROUND (lface) = XCDR (color);
6937 else if (FRAME_WINDOW_P (f))
6938 return 0;
6939 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6940 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
6941 else
6942 abort ();
6943 }
6944
6945 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
6946 {
6947 /* This function is called so early that colors are not yet
6948 set in the frame parameter list. */
6949 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
6950 if (CONSP (color) && STRINGP (XCDR (color)))
6951 LFACE_BACKGROUND (lface) = XCDR (color);
6952 else if (FRAME_WINDOW_P (f))
6953 return 0;
6954 else if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
6955 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
6956 else
6957 abort ();
6958 }
6959
6960 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
6961 LFACE_STIPPLE (lface) = Qnil;
6962
6963 /* Realize the face; it must be fully-specified now. */
6964 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
6965 check_lface (lface);
6966 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
6967 face = realize_face (c, attrs, 0, NULL, DEFAULT_FACE_ID);
6968 return 1;
6969 }
6970
6971
6972 /* Realize basic faces other than the default face in face cache C.
6973 SYMBOL is the face name, ID is the face id the realized face must
6974 have. The default face must have been realized already. */
6975
6976 static void
6977 realize_named_face (f, symbol, id)
6978 struct frame *f;
6979 Lisp_Object symbol;
6980 int id;
6981 {
6982 struct face_cache *c = FRAME_FACE_CACHE (f);
6983 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
6984 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6985 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
6986 struct face *new_face;
6987
6988 /* The default face must exist and be fully specified. */
6989 get_lface_attributes (f, Qdefault, attrs, 1);
6990 check_lface_attrs (attrs);
6991 xassert (lface_fully_specified_p (attrs));
6992
6993 /* If SYMBOL isn't know as a face, create it. */
6994 if (NILP (lface))
6995 {
6996 Lisp_Object frame;
6997 XSETFRAME (frame, f);
6998 lface = Finternal_make_lisp_face (symbol, frame);
6999 }
7000
7001 /* Merge SYMBOL's face with the default face. */
7002 get_lface_attributes (f, symbol, symbol_attrs, 1);
7003 merge_face_vectors (f, symbol_attrs, attrs, 0);
7004
7005 /* Realize the face. */
7006 new_face = realize_face (c, attrs, 0, NULL, id);
7007 }
7008
7009
7010 /* Realize the fully-specified face with attributes ATTRS in face
7011 cache CACHE for character C. If C is a multibyte character,
7012 BASE_FACE is a face that has the same attributes. Otherwise,
7013 BASE_FACE is ignored. If FORMER_FACE_ID is non-negative, it is an
7014 ID of face to remove before caching the new face. Value is a
7015 pointer to the newly created realized face. */
7016
7017 static struct face *
7018 realize_face (cache, attrs, c, base_face, former_face_id)
7019 struct face_cache *cache;
7020 Lisp_Object *attrs;
7021 int c;
7022 struct face *base_face;
7023 int former_face_id;
7024 {
7025 struct face *face;
7026
7027 /* LFACE must be fully specified. */
7028 xassert (cache != NULL);
7029 check_lface_attrs (attrs);
7030
7031 if (former_face_id >= 0 && cache->used > former_face_id)
7032 {
7033 /* Remove the former face. */
7034 struct face *former_face = cache->faces_by_id[former_face_id];
7035 uncache_face (cache, former_face);
7036 free_realized_face (cache->f, former_face);
7037 }
7038
7039 if (FRAME_WINDOW_P (cache->f))
7040 face = realize_x_face (cache, attrs, c, base_face);
7041 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
7042 face = realize_tty_face (cache, attrs, c);
7043 else
7044 abort ();
7045
7046 /* Insert the new face. */
7047 cache_face (cache, face, lface_hash (attrs));
7048 #ifdef HAVE_WINDOW_SYSTEM
7049 if (FRAME_WINDOW_P (cache->f) && face->font == NULL)
7050 load_face_font (cache->f, face, c);
7051 #endif /* HAVE_WINDOW_SYSTEM */
7052 return face;
7053 }
7054
7055
7056 /* Realize the fully-specified face with attributes ATTRS in face
7057 cache CACHE for character C. Do it for X frame CACHE->f. If C is
7058 a multibyte character, BASE_FACE is a face that has the same
7059 attributes. Otherwise, BASE_FACE is ignored. If the new face
7060 doesn't share font with the default face, a fontname is allocated
7061 from the heap and set in `font_name' of the new face, but it is not
7062 yet loaded here. Value is a pointer to the newly created realized
7063 face. */
7064
7065 static struct face *
7066 realize_x_face (cache, attrs, c, base_face)
7067 struct face_cache *cache;
7068 Lisp_Object *attrs;
7069 int c;
7070 struct face *base_face;
7071 {
7072 #ifdef HAVE_WINDOW_SYSTEM
7073 struct face *face, *default_face;
7074 struct frame *f;
7075 Lisp_Object stipple, overline, strike_through, box;
7076
7077 xassert (FRAME_WINDOW_P (cache->f));
7078 xassert (SINGLE_BYTE_CHAR_P (c)
7079 || base_face);
7080
7081 /* Allocate a new realized face. */
7082 face = make_realized_face (attrs);
7083
7084 f = cache->f;
7085
7086 /* If C is a multibyte character, we share all face attirbutes with
7087 BASE_FACE including the realized fontset. But, we must load a
7088 different font. */
7089 if (!SINGLE_BYTE_CHAR_P (c))
7090 {
7091 bcopy (base_face, face, sizeof *face);
7092 face->gc = 0;
7093
7094 /* Don't try to free the colors copied bitwise from BASE_FACE. */
7095 face->colors_copied_bitwise_p = 1;
7096
7097 /* to force realize_face to load font */
7098 face->font = NULL;
7099 return face;
7100 }
7101
7102 /* Now we are realizing a face for ASCII (and unibyte) characters. */
7103
7104 /* Determine the font to use. Most of the time, the font will be
7105 the same as the font of the default face, so try that first. */
7106 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7107 if (default_face
7108 && FACE_SUITABLE_FOR_CHAR_P (default_face, c)
7109 && lface_same_font_attributes_p (default_face->lface, attrs))
7110 {
7111 face->font = default_face->font;
7112 face->fontset = default_face->fontset;
7113 face->font_info_id = default_face->font_info_id;
7114 face->font_name = default_face->font_name;
7115 face->ascii_face = face;
7116
7117 /* But, as we can't share the fontset, make a new realized
7118 fontset that has the same base fontset as of the default
7119 face. */
7120 face->fontset
7121 = make_fontset_for_ascii_face (f, default_face->fontset);
7122 }
7123 else
7124 {
7125 /* If the face attribute ATTRS specifies a fontset, use it as
7126 the base of a new realized fontset. Otherwise, use the same
7127 base fontset as of the default face. The base determines
7128 registry and encoding of a font. It may also determine
7129 foundry and family. The other fields of font name pattern
7130 are constructed from ATTRS. */
7131 int fontset = face_fontset (attrs);
7132
7133 if ((fontset == -1) && default_face)
7134 fontset = default_face->fontset;
7135 face->fontset = make_fontset_for_ascii_face (f, fontset);
7136 face->font = NULL; /* to force realize_face to load font */
7137 }
7138
7139 /* Load colors, and set remaining attributes. */
7140
7141 load_face_colors (f, face, attrs);
7142
7143 /* Set up box. */
7144 box = attrs[LFACE_BOX_INDEX];
7145 if (STRINGP (box))
7146 {
7147 /* A simple box of line width 1 drawn in color given by
7148 the string. */
7149 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
7150 LFACE_BOX_INDEX);
7151 face->box = FACE_SIMPLE_BOX;
7152 face->box_line_width = 1;
7153 }
7154 else if (INTEGERP (box))
7155 {
7156 /* Simple box of specified line width in foreground color of the
7157 face. */
7158 xassert (XINT (box) != 0);
7159 face->box = FACE_SIMPLE_BOX;
7160 face->box_line_width = XINT (box);
7161 face->box_color = face->foreground;
7162 face->box_color_defaulted_p = 1;
7163 }
7164 else if (CONSP (box))
7165 {
7166 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
7167 being one of `raised' or `sunken'. */
7168 face->box = FACE_SIMPLE_BOX;
7169 face->box_color = face->foreground;
7170 face->box_color_defaulted_p = 1;
7171 face->box_line_width = 1;
7172
7173 while (CONSP (box))
7174 {
7175 Lisp_Object keyword, value;
7176
7177 keyword = XCAR (box);
7178 box = XCDR (box);
7179
7180 if (!CONSP (box))
7181 break;
7182 value = XCAR (box);
7183 box = XCDR (box);
7184
7185 if (EQ (keyword, QCline_width))
7186 {
7187 if (INTEGERP (value) && XINT (value) != 0)
7188 face->box_line_width = XINT (value);
7189 }
7190 else if (EQ (keyword, QCcolor))
7191 {
7192 if (STRINGP (value))
7193 {
7194 face->box_color = load_color (f, face, value,
7195 LFACE_BOX_INDEX);
7196 face->use_box_color_for_shadows_p = 1;
7197 }
7198 }
7199 else if (EQ (keyword, QCstyle))
7200 {
7201 if (EQ (value, Qreleased_button))
7202 face->box = FACE_RAISED_BOX;
7203 else if (EQ (value, Qpressed_button))
7204 face->box = FACE_SUNKEN_BOX;
7205 }
7206 }
7207 }
7208
7209 /* Text underline, overline, strike-through. */
7210
7211 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
7212 {
7213 /* Use default color (same as foreground color). */
7214 face->underline_p = 1;
7215 face->underline_defaulted_p = 1;
7216 face->underline_color = 0;
7217 }
7218 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
7219 {
7220 /* Use specified color. */
7221 face->underline_p = 1;
7222 face->underline_defaulted_p = 0;
7223 face->underline_color
7224 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
7225 LFACE_UNDERLINE_INDEX);
7226 }
7227 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
7228 {
7229 face->underline_p = 0;
7230 face->underline_defaulted_p = 0;
7231 face->underline_color = 0;
7232 }
7233
7234 overline = attrs[LFACE_OVERLINE_INDEX];
7235 if (STRINGP (overline))
7236 {
7237 face->overline_color
7238 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
7239 LFACE_OVERLINE_INDEX);
7240 face->overline_p = 1;
7241 }
7242 else if (EQ (overline, Qt))
7243 {
7244 face->overline_color = face->foreground;
7245 face->overline_color_defaulted_p = 1;
7246 face->overline_p = 1;
7247 }
7248
7249 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
7250 if (STRINGP (strike_through))
7251 {
7252 face->strike_through_color
7253 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
7254 LFACE_STRIKE_THROUGH_INDEX);
7255 face->strike_through_p = 1;
7256 }
7257 else if (EQ (strike_through, Qt))
7258 {
7259 face->strike_through_color = face->foreground;
7260 face->strike_through_color_defaulted_p = 1;
7261 face->strike_through_p = 1;
7262 }
7263
7264 stipple = attrs[LFACE_STIPPLE_INDEX];
7265 if (!NILP (stipple))
7266 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
7267
7268 xassert (FACE_SUITABLE_FOR_CHAR_P (face, c));
7269 return face;
7270 #endif /* HAVE_WINDOW_SYSTEM */
7271 }
7272
7273
7274 /* Map a specified color of face FACE on frame F to a tty color index.
7275 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
7276 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
7277 default foreground/background colors. */
7278
7279 static void
7280 map_tty_color (f, face, idx, defaulted)
7281 struct frame *f;
7282 struct face *face;
7283 enum lface_attribute_index idx;
7284 int *defaulted;
7285 {
7286 Lisp_Object frame, color, def;
7287 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
7288 unsigned long default_pixel, default_other_pixel, pixel;
7289
7290 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
7291
7292 if (foreground_p)
7293 {
7294 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7295 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7296 }
7297 else
7298 {
7299 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
7300 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
7301 }
7302
7303 XSETFRAME (frame, f);
7304 color = face->lface[idx];
7305
7306 if (STRINGP (color)
7307 && SCHARS (color)
7308 && CONSP (Vtty_defined_color_alist)
7309 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
7310 CONSP (def)))
7311 {
7312 /* Associations in tty-defined-color-alist are of the form
7313 (NAME INDEX R G B). We need the INDEX part. */
7314 pixel = XINT (XCAR (XCDR (def)));
7315 }
7316
7317 if (pixel == default_pixel && STRINGP (color))
7318 {
7319 pixel = load_color (f, face, color, idx);
7320
7321 #if defined (MSDOS) || defined (WINDOWSNT)
7322 /* If the foreground of the default face is the default color,
7323 use the foreground color defined by the frame. */
7324 #ifdef MSDOS
7325 if (FRAME_MSDOS_P (f))
7326 {
7327 #endif /* MSDOS */
7328 if (pixel == default_pixel
7329 || pixel == FACE_TTY_DEFAULT_COLOR)
7330 {
7331 if (foreground_p)
7332 pixel = FRAME_FOREGROUND_PIXEL (f);
7333 else
7334 pixel = FRAME_BACKGROUND_PIXEL (f);
7335 face->lface[idx] = tty_color_name (f, pixel);
7336 *defaulted = 1;
7337 }
7338 else if (pixel == default_other_pixel)
7339 {
7340 if (foreground_p)
7341 pixel = FRAME_BACKGROUND_PIXEL (f);
7342 else
7343 pixel = FRAME_FOREGROUND_PIXEL (f);
7344 face->lface[idx] = tty_color_name (f, pixel);
7345 *defaulted = 1;
7346 }
7347 #ifdef MSDOS
7348 }
7349 #endif
7350 #endif /* MSDOS or WINDOWSNT */
7351 }
7352
7353 if (foreground_p)
7354 face->foreground = pixel;
7355 else
7356 face->background = pixel;
7357 }
7358
7359
7360 /* Realize the fully-specified face with attributes ATTRS in face
7361 cache CACHE for character C. Do it for TTY frame CACHE->f. Value is a
7362 pointer to the newly created realized face. */
7363
7364 static struct face *
7365 realize_tty_face (cache, attrs, c)
7366 struct face_cache *cache;
7367 Lisp_Object *attrs;
7368 int c;
7369 {
7370 struct face *face;
7371 int weight, slant;
7372 int face_colors_defaulted = 0;
7373 struct frame *f = cache->f;
7374
7375 /* Frame must be a termcap frame. */
7376 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
7377
7378 /* Allocate a new realized face. */
7379 face = make_realized_face (attrs);
7380 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
7381
7382 /* Map face attributes to TTY appearances. We map slant to
7383 dimmed text because we want italic text to appear differently
7384 and because dimmed text is probably used infrequently. */
7385 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
7386 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
7387
7388 if (weight > XLFD_WEIGHT_MEDIUM)
7389 face->tty_bold_p = 1;
7390 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
7391 face->tty_dim_p = 1;
7392 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
7393 face->tty_underline_p = 1;
7394 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
7395 face->tty_reverse_p = 1;
7396
7397 /* Map color names to color indices. */
7398 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
7399 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
7400
7401 /* Swap colors if face is inverse-video. If the colors are taken
7402 from the frame colors, they are already inverted, since the
7403 frame-creation function calls x-handle-reverse-video. */
7404 if (face->tty_reverse_p && !face_colors_defaulted)
7405 {
7406 unsigned long tem = face->foreground;
7407 face->foreground = face->background;
7408 face->background = tem;
7409 }
7410
7411 if (tty_suppress_bold_inverse_default_colors_p
7412 && face->tty_bold_p
7413 && face->background == FACE_TTY_DEFAULT_FG_COLOR
7414 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
7415 face->tty_bold_p = 0;
7416
7417 return face;
7418 }
7419
7420
7421 DEFUN ("tty-suppress-bold-inverse-default-colors",
7422 Ftty_suppress_bold_inverse_default_colors,
7423 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
7424 doc: /* Suppress/allow boldness of faces with inverse default colors.
7425 SUPPRESS non-nil means suppress it.
7426 This affects bold faces on TTYs whose foreground is the default background
7427 color of the display and whose background is the default foreground color.
7428 For such faces, the bold face attribute is ignored if this variable
7429 is non-nil. */)
7430 (suppress)
7431 Lisp_Object suppress;
7432 {
7433 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
7434 ++face_change_count;
7435 return suppress;
7436 }
7437
7438
7439 \f
7440 /***********************************************************************
7441 Computing Faces
7442 ***********************************************************************/
7443
7444 /* Return the ID of the face to use to display character CH with face
7445 property PROP on frame F in current_buffer. */
7446
7447 int
7448 compute_char_face (f, ch, prop)
7449 struct frame *f;
7450 int ch;
7451 Lisp_Object prop;
7452 {
7453 int face_id;
7454
7455 if (NILP (current_buffer->enable_multibyte_characters))
7456 ch = 0;
7457
7458 if (NILP (prop))
7459 {
7460 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7461 face_id = FACE_FOR_CHAR (f, face, ch);
7462 }
7463 else
7464 {
7465 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7466 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7467 bcopy (default_face->lface, attrs, sizeof attrs);
7468 merge_face_ref (f, prop, attrs, 1, 0);
7469 face_id = lookup_face (f, attrs, ch, NULL);
7470 }
7471
7472 return face_id;
7473 }
7474
7475 /* Return the face ID associated with buffer position POS for
7476 displaying ASCII characters. Return in *ENDPTR the position at
7477 which a different face is needed, as far as text properties and
7478 overlays are concerned. W is a window displaying current_buffer.
7479
7480 REGION_BEG, REGION_END delimit the region, so it can be
7481 highlighted.
7482
7483 LIMIT is a position not to scan beyond. That is to limit the time
7484 this function can take.
7485
7486 If MOUSE is non-zero, use the character's mouse-face, not its face.
7487
7488 The face returned is suitable for displaying ASCII characters. */
7489
7490 int
7491 face_at_buffer_position (w, pos, region_beg, region_end,
7492 endptr, limit, mouse)
7493 struct window *w;
7494 int pos;
7495 int region_beg, region_end;
7496 int *endptr;
7497 int limit;
7498 int mouse;
7499 {
7500 struct frame *f = XFRAME (w->frame);
7501 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7502 Lisp_Object prop, position;
7503 int i, noverlays;
7504 Lisp_Object *overlay_vec;
7505 Lisp_Object frame;
7506 int endpos;
7507 Lisp_Object propname = mouse ? Qmouse_face : Qface;
7508 Lisp_Object limit1, end;
7509 struct face *default_face;
7510
7511 /* W must display the current buffer. We could write this function
7512 to use the frame and buffer of W, but right now it doesn't. */
7513 /* xassert (XBUFFER (w->buffer) == current_buffer); */
7514
7515 XSETFRAME (frame, f);
7516 XSETFASTINT (position, pos);
7517
7518 endpos = ZV;
7519 if (pos < region_beg && region_beg < endpos)
7520 endpos = region_beg;
7521
7522 /* Get the `face' or `mouse_face' text property at POS, and
7523 determine the next position at which the property changes. */
7524 prop = Fget_text_property (position, propname, w->buffer);
7525 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
7526 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
7527 if (INTEGERP (end))
7528 endpos = XINT (end);
7529
7530 /* Look at properties from overlays. */
7531 {
7532 int next_overlay;
7533
7534 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
7535 if (next_overlay < endpos)
7536 endpos = next_overlay;
7537 }
7538
7539 *endptr = endpos;
7540
7541 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
7542
7543 /* Optimize common cases where we can use the default face. */
7544 if (noverlays == 0
7545 && NILP (prop)
7546 && !(pos >= region_beg && pos < region_end))
7547 return DEFAULT_FACE_ID;
7548
7549 /* Begin with attributes from the default face. */
7550 bcopy (default_face->lface, attrs, sizeof attrs);
7551
7552 /* Merge in attributes specified via text properties. */
7553 if (!NILP (prop))
7554 merge_face_ref (f, prop, attrs, 1, 0);
7555
7556 /* Now merge the overlay data. */
7557 noverlays = sort_overlays (overlay_vec, noverlays, w);
7558 for (i = 0; i < noverlays; i++)
7559 {
7560 Lisp_Object oend;
7561 int oendpos;
7562
7563 prop = Foverlay_get (overlay_vec[i], propname);
7564 if (!NILP (prop))
7565 merge_face_ref (f, prop, attrs, 1, 0);
7566
7567 oend = OVERLAY_END (overlay_vec[i]);
7568 oendpos = OVERLAY_POSITION (oend);
7569 if (oendpos < endpos)
7570 endpos = oendpos;
7571 }
7572
7573 /* If in the region, merge in the region face. */
7574 if (pos >= region_beg && pos < region_end)
7575 {
7576 merge_named_face (f, Qregion, attrs, 0);
7577
7578 if (region_end < endpos)
7579 endpos = region_end;
7580 }
7581
7582 *endptr = endpos;
7583
7584 /* Look up a realized face with the given face attributes,
7585 or realize a new one for ASCII characters. */
7586 return lookup_face (f, attrs, 0, NULL);
7587 }
7588
7589
7590 /* Compute the face at character position POS in Lisp string STRING on
7591 window W, for ASCII characters.
7592
7593 If STRING is an overlay string, it comes from position BUFPOS in
7594 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
7595 not an overlay string. W must display the current buffer.
7596 REGION_BEG and REGION_END give the start and end positions of the
7597 region; both are -1 if no region is visible.
7598
7599 BASE_FACE_ID is the id of a face to merge with. For strings coming
7600 from overlays or the `display' property it is the face at BUFPOS.
7601
7602 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
7603
7604 Set *ENDPTR to the next position where to check for faces in
7605 STRING; -1 if the face is constant from POS to the end of the
7606 string.
7607
7608 Value is the id of the face to use. The face returned is suitable
7609 for displaying ASCII characters. */
7610
7611 int
7612 face_at_string_position (w, string, pos, bufpos, region_beg,
7613 region_end, endptr, base_face_id, mouse_p)
7614 struct window *w;
7615 Lisp_Object string;
7616 int pos, bufpos;
7617 int region_beg, region_end;
7618 int *endptr;
7619 enum face_id base_face_id;
7620 int mouse_p;
7621 {
7622 Lisp_Object prop, position, end, limit;
7623 struct frame *f = XFRAME (WINDOW_FRAME (w));
7624 Lisp_Object attrs[LFACE_VECTOR_SIZE];
7625 struct face *base_face;
7626 int multibyte_p = STRING_MULTIBYTE (string);
7627 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
7628
7629 /* Get the value of the face property at the current position within
7630 STRING. Value is nil if there is no face property. */
7631 XSETFASTINT (position, pos);
7632 prop = Fget_text_property (position, prop_name, string);
7633
7634 /* Get the next position at which to check for faces. Value of end
7635 is nil if face is constant all the way to the end of the string.
7636 Otherwise it is a string position where to check faces next.
7637 Limit is the maximum position up to which to check for property
7638 changes in Fnext_single_property_change. Strings are usually
7639 short, so set the limit to the end of the string. */
7640 XSETFASTINT (limit, SCHARS (string));
7641 end = Fnext_single_property_change (position, prop_name, string, limit);
7642 if (INTEGERP (end))
7643 *endptr = XFASTINT (end);
7644 else
7645 *endptr = -1;
7646
7647 base_face = FACE_FROM_ID (f, base_face_id);
7648 xassert (base_face);
7649
7650 /* Optimize the default case that there is no face property and we
7651 are not in the region. */
7652 if (NILP (prop)
7653 && (base_face_id != DEFAULT_FACE_ID
7654 /* BUFPOS <= 0 means STRING is not an overlay string, so
7655 that the region doesn't have to be taken into account. */
7656 || bufpos <= 0
7657 || bufpos < region_beg
7658 || bufpos >= region_end)
7659 && (multibyte_p
7660 /* We can't realize faces for different charsets differently
7661 if we don't have fonts, so we can stop here if not working
7662 on a window-system frame. */
7663 || !FRAME_WINDOW_P (f)
7664 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
7665 return base_face->id;
7666
7667 /* Begin with attributes from the base face. */
7668 bcopy (base_face->lface, attrs, sizeof attrs);
7669
7670 /* Merge in attributes specified via text properties. */
7671 if (!NILP (prop))
7672 merge_face_ref (f, prop, attrs, 1, 0);
7673
7674 /* If in the region, merge in the region face. */
7675 if (bufpos
7676 && bufpos >= region_beg
7677 && bufpos < region_end)
7678 merge_named_face (f, Qregion, attrs, 0);
7679
7680 /* Look up a realized face with the given face attributes,
7681 or realize a new one for ASCII characters. */
7682 return lookup_face (f, attrs, 0, NULL);
7683 }
7684
7685
7686 \f
7687 /***********************************************************************
7688 Tests
7689 ***********************************************************************/
7690
7691 #if GLYPH_DEBUG
7692
7693 /* Print the contents of the realized face FACE to stderr. */
7694
7695 static void
7696 dump_realized_face (face)
7697 struct face *face;
7698 {
7699 fprintf (stderr, "ID: %d\n", face->id);
7700 #ifdef HAVE_X_WINDOWS
7701 fprintf (stderr, "gc: %d\n", (int) face->gc);
7702 #endif
7703 fprintf (stderr, "foreground: 0x%lx (%s)\n",
7704 face->foreground,
7705 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
7706 fprintf (stderr, "background: 0x%lx (%s)\n",
7707 face->background,
7708 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
7709 fprintf (stderr, "font_name: %s (%s)\n",
7710 face->font_name,
7711 SDATA (face->lface[LFACE_FAMILY_INDEX]));
7712 #ifdef HAVE_X_WINDOWS
7713 fprintf (stderr, "font = %p\n", face->font);
7714 #endif
7715 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
7716 fprintf (stderr, "fontset: %d\n", face->fontset);
7717 fprintf (stderr, "underline: %d (%s)\n",
7718 face->underline_p,
7719 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
7720 fprintf (stderr, "hash: %d\n", face->hash);
7721 fprintf (stderr, "charset: %d\n", face->charset);
7722 }
7723
7724
7725 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
7726 (n)
7727 Lisp_Object n;
7728 {
7729 if (NILP (n))
7730 {
7731 int i;
7732
7733 fprintf (stderr, "font selection order: ");
7734 for (i = 0; i < DIM (font_sort_order); ++i)
7735 fprintf (stderr, "%d ", font_sort_order[i]);
7736 fprintf (stderr, "\n");
7737
7738 fprintf (stderr, "alternative fonts: ");
7739 debug_print (Vface_alternative_font_family_alist);
7740 fprintf (stderr, "\n");
7741
7742 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
7743 Fdump_face (make_number (i));
7744 }
7745 else
7746 {
7747 struct face *face;
7748 CHECK_NUMBER (n);
7749 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
7750 if (face == NULL)
7751 error ("Not a valid face");
7752 dump_realized_face (face);
7753 }
7754
7755 return Qnil;
7756 }
7757
7758
7759 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
7760 0, 0, 0, doc: /* */)
7761 ()
7762 {
7763 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
7764 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
7765 fprintf (stderr, "number of GCs = %d\n", ngcs);
7766 return Qnil;
7767 }
7768
7769 #endif /* GLYPH_DEBUG != 0 */
7770
7771
7772 \f
7773 /***********************************************************************
7774 Initialization
7775 ***********************************************************************/
7776
7777 void
7778 syms_of_xfaces ()
7779 {
7780 Qface = intern ("face");
7781 staticpro (&Qface);
7782 Qface_no_inherit = intern ("face-no-inherit");
7783 staticpro (&Qface_no_inherit);
7784 Qbitmap_spec_p = intern ("bitmap-spec-p");
7785 staticpro (&Qbitmap_spec_p);
7786 Qframe_update_face_colors = intern ("frame-update-face-colors");
7787 staticpro (&Qframe_update_face_colors);
7788
7789 /* Lisp face attribute keywords. */
7790 QCfamily = intern (":family");
7791 staticpro (&QCfamily);
7792 QCheight = intern (":height");
7793 staticpro (&QCheight);
7794 QCweight = intern (":weight");
7795 staticpro (&QCweight);
7796 QCslant = intern (":slant");
7797 staticpro (&QCslant);
7798 QCunderline = intern (":underline");
7799 staticpro (&QCunderline);
7800 QCinverse_video = intern (":inverse-video");
7801 staticpro (&QCinverse_video);
7802 QCreverse_video = intern (":reverse-video");
7803 staticpro (&QCreverse_video);
7804 QCforeground = intern (":foreground");
7805 staticpro (&QCforeground);
7806 QCbackground = intern (":background");
7807 staticpro (&QCbackground);
7808 QCstipple = intern (":stipple");;
7809 staticpro (&QCstipple);
7810 QCwidth = intern (":width");
7811 staticpro (&QCwidth);
7812 QCfont = intern (":font");
7813 staticpro (&QCfont);
7814 QCbold = intern (":bold");
7815 staticpro (&QCbold);
7816 QCitalic = intern (":italic");
7817 staticpro (&QCitalic);
7818 QCoverline = intern (":overline");
7819 staticpro (&QCoverline);
7820 QCstrike_through = intern (":strike-through");
7821 staticpro (&QCstrike_through);
7822 QCbox = intern (":box");
7823 staticpro (&QCbox);
7824 QCinherit = intern (":inherit");
7825 staticpro (&QCinherit);
7826
7827 /* Symbols used for Lisp face attribute values. */
7828 QCcolor = intern (":color");
7829 staticpro (&QCcolor);
7830 QCline_width = intern (":line-width");
7831 staticpro (&QCline_width);
7832 QCstyle = intern (":style");
7833 staticpro (&QCstyle);
7834 Qreleased_button = intern ("released-button");
7835 staticpro (&Qreleased_button);
7836 Qpressed_button = intern ("pressed-button");
7837 staticpro (&Qpressed_button);
7838 Qnormal = intern ("normal");
7839 staticpro (&Qnormal);
7840 Qultra_light = intern ("ultra-light");
7841 staticpro (&Qultra_light);
7842 Qextra_light = intern ("extra-light");
7843 staticpro (&Qextra_light);
7844 Qlight = intern ("light");
7845 staticpro (&Qlight);
7846 Qsemi_light = intern ("semi-light");
7847 staticpro (&Qsemi_light);
7848 Qsemi_bold = intern ("semi-bold");
7849 staticpro (&Qsemi_bold);
7850 Qbold = intern ("bold");
7851 staticpro (&Qbold);
7852 Qextra_bold = intern ("extra-bold");
7853 staticpro (&Qextra_bold);
7854 Qultra_bold = intern ("ultra-bold");
7855 staticpro (&Qultra_bold);
7856 Qoblique = intern ("oblique");
7857 staticpro (&Qoblique);
7858 Qitalic = intern ("italic");
7859 staticpro (&Qitalic);
7860 Qreverse_oblique = intern ("reverse-oblique");
7861 staticpro (&Qreverse_oblique);
7862 Qreverse_italic = intern ("reverse-italic");
7863 staticpro (&Qreverse_italic);
7864 Qultra_condensed = intern ("ultra-condensed");
7865 staticpro (&Qultra_condensed);
7866 Qextra_condensed = intern ("extra-condensed");
7867 staticpro (&Qextra_condensed);
7868 Qcondensed = intern ("condensed");
7869 staticpro (&Qcondensed);
7870 Qsemi_condensed = intern ("semi-condensed");
7871 staticpro (&Qsemi_condensed);
7872 Qsemi_expanded = intern ("semi-expanded");
7873 staticpro (&Qsemi_expanded);
7874 Qexpanded = intern ("expanded");
7875 staticpro (&Qexpanded);
7876 Qextra_expanded = intern ("extra-expanded");
7877 staticpro (&Qextra_expanded);
7878 Qultra_expanded = intern ("ultra-expanded");
7879 staticpro (&Qultra_expanded);
7880 Qbackground_color = intern ("background-color");
7881 staticpro (&Qbackground_color);
7882 Qforeground_color = intern ("foreground-color");
7883 staticpro (&Qforeground_color);
7884 Qunspecified = intern ("unspecified");
7885 staticpro (&Qunspecified);
7886
7887 Qface_alias = intern ("face-alias");
7888 staticpro (&Qface_alias);
7889 Qdefault = intern ("default");
7890 staticpro (&Qdefault);
7891 Qtool_bar = intern ("tool-bar");
7892 staticpro (&Qtool_bar);
7893 Qregion = intern ("region");
7894 staticpro (&Qregion);
7895 Qfringe = intern ("fringe");
7896 staticpro (&Qfringe);
7897 Qheader_line = intern ("header-line");
7898 staticpro (&Qheader_line);
7899 Qscroll_bar = intern ("scroll-bar");
7900 staticpro (&Qscroll_bar);
7901 Qmenu = intern ("menu");
7902 staticpro (&Qmenu);
7903 Qcursor = intern ("cursor");
7904 staticpro (&Qcursor);
7905 Qborder = intern ("border");
7906 staticpro (&Qborder);
7907 Qmouse = intern ("mouse");
7908 staticpro (&Qmouse);
7909 Qmode_line_inactive = intern ("mode-line-inactive");
7910 staticpro (&Qmode_line_inactive);
7911 Qtty_color_desc = intern ("tty-color-desc");
7912 staticpro (&Qtty_color_desc);
7913 Qtty_color_standard_values = intern ("tty-color-standard-values");
7914 staticpro (&Qtty_color_standard_values);
7915 Qtty_color_by_index = intern ("tty-color-by-index");
7916 staticpro (&Qtty_color_by_index);
7917 Qtty_color_alist = intern ("tty-color-alist");
7918 staticpro (&Qtty_color_alist);
7919 Qscalable_fonts_allowed = intern ("scalable-fonts-allowed");
7920 staticpro (&Qscalable_fonts_allowed);
7921
7922 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
7923 staticpro (&Vparam_value_alist);
7924 Vface_alternative_font_family_alist = Qnil;
7925 staticpro (&Vface_alternative_font_family_alist);
7926 Vface_alternative_font_registry_alist = Qnil;
7927 staticpro (&Vface_alternative_font_registry_alist);
7928
7929 defsubr (&Sinternal_make_lisp_face);
7930 defsubr (&Sinternal_lisp_face_p);
7931 defsubr (&Sinternal_set_lisp_face_attribute);
7932 #ifdef HAVE_WINDOW_SYSTEM
7933 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
7934 #endif
7935 defsubr (&Scolor_gray_p);
7936 defsubr (&Scolor_supported_p);
7937 defsubr (&Sface_attribute_relative_p);
7938 defsubr (&Smerge_face_attribute);
7939 defsubr (&Sinternal_get_lisp_face_attribute);
7940 defsubr (&Sinternal_lisp_face_attribute_values);
7941 defsubr (&Sinternal_lisp_face_equal_p);
7942 defsubr (&Sinternal_lisp_face_empty_p);
7943 defsubr (&Sinternal_copy_lisp_face);
7944 defsubr (&Sinternal_merge_in_global_face);
7945 defsubr (&Sface_font);
7946 defsubr (&Sframe_face_alist);
7947 defsubr (&Sdisplay_supports_face_attributes_p);
7948 defsubr (&Scolor_distance);
7949 defsubr (&Sinternal_set_font_selection_order);
7950 defsubr (&Sinternal_set_alternative_font_family_alist);
7951 defsubr (&Sinternal_set_alternative_font_registry_alist);
7952 defsubr (&Sface_attributes_as_vector);
7953 #if GLYPH_DEBUG
7954 defsubr (&Sdump_face);
7955 defsubr (&Sshow_face_resources);
7956 #endif /* GLYPH_DEBUG */
7957 defsubr (&Sclear_face_cache);
7958 defsubr (&Stty_suppress_bold_inverse_default_colors);
7959
7960 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
7961 defsubr (&Sdump_colors);
7962 #endif
7963
7964 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
7965 doc: /* *Limit for font matching.
7966 If an integer > 0, font matching functions won't load more than
7967 that number of fonts when searching for a matching font. */);
7968 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
7969
7970 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
7971 doc: /* List of global face definitions (for internal use only.) */);
7972 Vface_new_frame_defaults = Qnil;
7973
7974 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
7975 doc: /* *Default stipple pattern used on monochrome displays.
7976 This stipple pattern is used on monochrome displays
7977 instead of shades of gray for a face background color.
7978 See `set-face-stipple' for possible values for this variable. */);
7979 Vface_default_stipple = build_string ("gray3");
7980
7981 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
7982 doc: /* An alist of defined terminal colors and their RGB values. */);
7983 Vtty_defined_color_alist = Qnil;
7984
7985 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
7986 doc: /* Allowed scalable fonts.
7987 A value of nil means don't allow any scalable fonts.
7988 A value of t means allow any scalable font.
7989 Otherwise, value must be a list of regular expressions. A font may be
7990 scaled if its name matches a regular expression in the list.
7991 Note that if value is nil, a scalable font might still be used, if no
7992 other font of the appropriate family and registry is available. */);
7993 Vscalable_fonts_allowed = Qnil;
7994
7995 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
7996 doc: /* List of ignored fonts.
7997 Each element is a regular expression that matches names of fonts to
7998 ignore. */);
7999 Vface_ignored_fonts = Qnil;
8000
8001 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
8002 doc: /* Alist of fonts vs the rescaling factors.
8003 Each element is a cons (FONT-NAME-PATTERN . RESCALE-RATIO), where
8004 FONT-NAME-PATTERN is a regular expression matching a font name, and
8005 RESCALE-RATIO is a floating point number to specify how much larger
8006 \(or smaller) font we should use. For instance, if a face requests
8007 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
8008 Vface_font_rescale_alist = Qnil;
8009
8010 #ifdef HAVE_WINDOW_SYSTEM
8011 defsubr (&Sbitmap_spec_p);
8012 defsubr (&Sx_list_fonts);
8013 defsubr (&Sinternal_face_x_get_resource);
8014 defsubr (&Sx_family_fonts);
8015 defsubr (&Sx_font_family_list);
8016 #endif /* HAVE_WINDOW_SYSTEM */
8017 }
8018
8019 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
8020 (do not change this comment) */