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