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