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