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