]> code.delx.au - gnu-emacs/blob - src/xfaces.c
(load_face_colors): Load background color if setting
[gnu-emacs] / src / xfaces.c
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999 Free Software Foundation.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22
23 /* Faces.
24
25 When using Emacs with X, the display style of characters can be
26 changed by defining `faces'. Each face can specify the following
27 display attributes:
28
29 1. Font family or fontset alias name.
30
31 2. Relative proportionate width, aka character set width or set
32 width (swidth), e.g. `semi-compressed'.
33
34 3. Font height in 1/10pt
35
36 4. Font weight, e.g. `bold'.
37
38 5. Font slant, e.g. `italic'.
39
40 6. Foreground color.
41
42 7. Background color.
43
44 8. Whether or not characters should be underlined, and in what color.
45
46 9. Whether or not characters should be displayed in inverse video.
47
48 10. A background stipple, a bitmap.
49
50 11. Whether or not characters should be overlined, and in what color.
51
52 12. Whether or not characters should be strike-through, and in what
53 color.
54
55 13. Whether or not a box should be drawn around characters, the box
56 type, and, for simple boxes, in what color.
57
58 Faces are frame-local by nature because Emacs allows to define the
59 same named face (face names are symbols) differently for different
60 frames. Each frame has an alist of face definitions for all named
61 faces. The value of a named face in such an alist is a Lisp vector
62 with the symbol `face' in slot 0, and a slot for each each of the
63 face attributes mentioned above.
64
65 There is also a global face alist `Vface_new_frame_defaults'. Face
66 definitions from this list are used to initialize faces of newly
67 created frames.
68
69 A face doesn't have to specify all attributes. Those not specified
70 have a value of `unspecified'. Faces specifying all attributes are
71 called `fully-specified'.
72
73
74 Face merging.
75
76 The display style of a given character in the text is determined by
77 combining several faces. This process is called `face merging'.
78 Any aspect of the display style that isn't specified by overlays or
79 text properties is taken from the `default' face. Since it is made
80 sure that the default face is always fully-specified, face merging
81 always results in a fully-specified face.
82
83
84 Face realization.
85
86 After all face attributes for a character have been determined by
87 merging faces of that character, that face is `realized'. The
88 realization process maps face attributes to what is physically
89 available on the system where Emacs runs. The result is a
90 `realized face' in form of a struct face which is stored in the
91 face cache of the frame on which it was realized.
92
93 Face realization is done in the context of the charset of the
94 character to display because different fonts and encodings are used
95 for different charsets. In other words, for characters of
96 different charsets, different realized faces are needed to display
97 them.
98
99 Except for composite characters (CHARSET_COMPOSITION), faces are
100 always realized for a specific character set and contain a specific
101 font, even if the face being realized specifies a fontset (see
102 `font selection' below). The reason is that the result of the new
103 font selection stage is better than what can be done with
104 statically defined font name patterns in fontsets.
105
106
107 Unibyte text.
108
109 In unibyte text, Emacs' charsets aren't applicable; function
110 `char-charset' reports CHARSET_ASCII for all characters, including
111 those > 0x7f. The X registry and encoding of fonts to use is
112 determined from the variable `x-unibyte-registry-and-encoding' in
113 this case. The variable is initialized at Emacs startup time from
114 the font the user specified for Emacs.
115
116 Currently all unibyte text, i.e. all buffers with
117 enable_multibyte_characters nil are displayed with fonts of the
118 same registry and encoding `x-unibyte-registry-and-encoding'. This
119 is consistent with the fact that languages can also be set
120 globally, only.
121
122
123 Font selection.
124
125 Font selection tries to find the best available matching font for a
126 given (charset, face) combination. This is done slightly
127 differently for faces specifying a fontset, or a font family name.
128
129 If the face specifies a fontset alias name, that fontset determines
130 a pattern for fonts of the given charset. If the face specifies a
131 font family, a font pattern is constructed. Charset symbols have a
132 property `x-charset-registry' for that purpose that maps a charset
133 to an XLFD registry and encoding in the font pattern constructed.
134
135 Available fonts on the system on which Emacs runs are then matched
136 against the font pattern. The result of font selection is the best
137 match for the given face attributes in this font list.
138
139 Font selection can be influenced by the user.
140
141 1. The user can specify the relative importance he gives the face
142 attributes width, height, weight, and slant by setting
143 face-font-selection-order (faces.el) to a list of face attribute
144 names. The default is '(:width :height :weight :slant), and means
145 that font selection first tries to find a good match for the font
146 width specified by a face, then---within fonts with that
147 width---tries to find a best match for the specified font height,
148 etc.
149
150 2. Setting face-alternative-font-family-alist allows the user to
151 specify alternative font families to try if a family specified by a
152 face doesn't exist.
153
154
155 Composite characters.
156
157 Realized faces for composite characters are the only ones having a
158 fontset id >= 0. When a composite character is encoded into a
159 sequence of non-composite characters (in xterm.c), a suitable font
160 for the non-composite characters is then selected and realized,
161 i.e. the realization process is delayed but in principle the same.
162
163
164 Initialization of basic faces.
165
166 The faces `default', `modeline' are considered `basic faces'.
167 When redisplay happens the first time for a newly created frame,
168 basic faces are realized for CHARSET_ASCII. Frame parameters are
169 used to fill in unspecified attributes of the default face. */
170
171 /* Define SCALABLE_FONTS to a non-zero value to enable scalable
172 font use. Define it to zero to disable scalable font use.
173
174 Use of too many or too large scalable fonts can crash XFree86
175 servers. That's why I've put the code dealing with scalable fonts
176 in #if's. */
177
178 #define SCALABLE_FONTS 1
179
180 #include <sys/types.h>
181 #include <sys/stat.h>
182 #include <config.h>
183 #include "lisp.h"
184 #include "charset.h"
185 #include "frame.h"
186
187 #ifdef HAVE_X_WINDOWS
188 #include "xterm.h"
189 #include "fontset.h"
190 #endif
191
192 #ifdef MSDOS
193 #include "dosfns.h"
194 #endif
195
196 #include "buffer.h"
197 #include "dispextern.h"
198 #include "blockinput.h"
199 #include "window.h"
200 #include "intervals.h"
201
202 #ifdef HAVE_X_WINDOWS
203
204 /* Compensate for a bug in Xos.h on some systems, on which it requires
205 time.h. On some such systems, Xos.h tries to redefine struct
206 timeval and struct timezone if USG is #defined while it is
207 #included. */
208
209 #ifdef XOS_NEEDS_TIME_H
210 #include <time.h>
211 #undef USG
212 #include <X11/Xos.h>
213 #define USG
214 #define __TIMEVAL__
215 #else /* not XOS_NEEDS_TIME_H */
216 #include <X11/Xos.h>
217 #endif /* not XOS_NEEDS_TIME_H */
218
219 #endif /* HAVE_X_WINDOWS */
220
221 #include <stdio.h>
222 #include <stdlib.h>
223 #include <ctype.h>
224 #include "keyboard.h"
225
226 #ifndef max
227 #define max(A, B) ((A) > (B) ? (A) : (B))
228 #define min(A, B) ((A) < (B) ? (A) : (B))
229 #define abs(X) ((X) < 0 ? -(X) : (X))
230 #endif
231
232 /* Non-zero if face attribute ATTR is unspecified. */
233
234 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
235
236 /* Value is the number of elements of VECTOR. */
237
238 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
239
240 /* Make a copy of string S on the stack using alloca. Value is a pointer
241 to the copy. */
242
243 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
244
245 /* Make a copy of the contents of Lisp string S on the stack using
246 alloca. Value is a pointer to the copy. */
247
248 #define LSTRDUPA(S) STRDUPA (XSTRING ((S))->data)
249
250 /* Size of hash table of realized faces in face caches (should be a
251 prime number). */
252
253 #define FACE_CACHE_BUCKETS_SIZE 1001
254
255 /* Keyword symbols used for face attribute names. */
256
257 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
258 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
259 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
260 Lisp_Object QCreverse_video;
261 Lisp_Object QCoverline, QCstrike_through, QCbox;
262
263 /* Symbols used for attribute values. */
264
265 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
266 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
267 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
268 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
269 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
270 Lisp_Object Qultra_expanded;
271 Lisp_Object Qreleased_button, Qpressed_button;
272 Lisp_Object QCstyle, QCcolor, QCline_width;
273 Lisp_Object Qunspecified;
274
275 /* The symbol `x-charset-registry'. This property of charsets defines
276 the X registry and encoding that fonts should have that are used to
277 display characters of that charset. */
278
279 Lisp_Object Qx_charset_registry;
280
281 /* Names of basic faces. */
282
283 Lisp_Object Qdefault, Qmodeline, Qtoolbar, Qregion, Qbitmap_area;
284 Lisp_Object Qtop_line;
285
286 /* Default stipple pattern used on monochrome displays. This stipple
287 pattern is used on monochrome displays instead of shades of gray
288 for a face background color. See `set-face-stipple' for possible
289 values for this variable. */
290
291 Lisp_Object Vface_default_stipple;
292
293 /* Default registry and encoding to use for charsets whose charset
294 symbols don't specify one. */
295
296 Lisp_Object Vface_default_registry;
297
298 /* Alist of alternative font families. Each element is of the form
299 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
300 try FAMILY1, then FAMILY2, ... */
301
302 Lisp_Object Vface_alternative_font_family_alist;
303
304 /* Allowed scalable fonts. A value of nil means don't allow any
305 scalable fonts. A value of t means allow the use of any scalable
306 font. Otherwise, value must be a list of regular expressions. A
307 font may be scaled if its name matches a regular expression in the
308 list. */
309
310 #if SCALABLE_FONTS
311 Lisp_Object Vscalable_fonts_allowed;
312 #endif
313
314 /* The symbols `foreground-color' and `background-color' which can be
315 used as part of a `face' property. This is for compatibility with
316 Emacs 20.2. */
317
318 Lisp_Object Qforeground_color, Qbackground_color;
319
320 /* The symbols `face' and `mouse-face' used as text properties. */
321
322 Lisp_Object Qface;
323 extern Lisp_Object Qmouse_face;
324
325 /* Error symbol for wrong_type_argument in load_pixmap. */
326
327 Lisp_Object Qpixmap_spec_p;
328
329 /* Alist of global face definitions. Each element is of the form
330 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
331 is a Lisp vector of face attributes. These faces are used
332 to initialize faces for new frames. */
333
334 Lisp_Object Vface_new_frame_defaults;
335
336 /* The next ID to assign to Lisp faces. */
337
338 static int next_lface_id;
339
340 /* A vector mapping Lisp face Id's to face names. */
341
342 static Lisp_Object *lface_id_to_name;
343 static int lface_id_to_name_size;
344
345 /* An alist of elements (COLOR-NAME . INDEX) mapping color names
346 to color indices for tty frames. */
347
348 Lisp_Object Vface_tty_color_alist;
349
350 /* Counter for calls to clear_face_cache. If this counter reaches
351 CLEAR_FONT_TABLE_COUNT, and a frame has more than
352 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
353
354 static int clear_font_table_count;
355 #define CLEAR_FONT_TABLE_COUNT 100
356 #define CLEAR_FONT_TABLE_NFONTS 10
357
358 /* Non-zero means face attributes have been changed since the last
359 redisplay. Used in redisplay_internal. */
360
361 int face_change_count;
362
363 /* The total number of colors currently allocated. */
364
365 #if GLYPH_DEBUG
366 static int ncolors_allocated;
367 static int npixmaps_allocated;
368 static int ngcs;
369 #endif
370
371
372 \f
373 /* Function prototypes. */
374
375 struct font_name;
376 struct table_entry;
377
378 static int may_use_scalable_font_p P_ ((struct font_name *, char *));
379 static void set_font_frame_param P_ ((Lisp_Object, Lisp_Object));
380 static int better_font_p P_ ((int *, struct font_name *, struct font_name *,
381 int));
382 static int first_font_matching P_ ((struct frame *f, char *,
383 struct font_name *));
384 static int x_face_list_fonts P_ ((struct frame *, char *,
385 struct font_name *, int, int, int));
386 static int font_scalable_p P_ ((struct font_name *));
387 static Lisp_Object deduce_unibyte_registry P_ ((struct frame *, char *));
388 static int get_lface_attributes P_ ((struct frame *, Lisp_Object, Lisp_Object *, int));
389 static int load_pixmap P_ ((struct frame *, Lisp_Object, unsigned *, unsigned *));
390 static char *xstrdup P_ ((char *));
391 static unsigned char *xstrlwr P_ ((unsigned char *));
392 static void signal_error P_ ((char *, Lisp_Object));
393 static void display_message P_ ((struct frame *, char *, Lisp_Object, Lisp_Object));
394 static struct frame *frame_or_selected_frame P_ ((Lisp_Object, int));
395 static void load_face_font_or_fontset P_ ((struct frame *, struct face *, char *, int));
396 static unsigned long load_color P_ ((struct frame *,
397 struct face *,
398 Lisp_Object,
399 enum lface_attribute_index));
400 static void load_face_colors P_ ((struct frame *, struct face *, Lisp_Object *));
401 static void free_face_colors P_ ((struct frame *, struct face *));
402 static int face_color_gray_p P_ ((struct frame *, char *));
403 static char *build_font_name P_ ((struct font_name *));
404 static void free_font_names P_ ((struct font_name *, int));
405 static int sorted_font_list P_ ((struct frame *, char *,
406 int (*cmpfn) P_ ((const void *, const void *)),
407 struct font_name **));
408 static int font_list P_ ((struct frame *, char *, char *, char *, struct font_name **));
409 static int try_font_list P_ ((struct frame *, Lisp_Object *, char *, char *, char *,
410 struct font_name **));
411 static int cmp_font_names P_ ((const void *, const void *));
412 static struct face *realize_face P_ ((struct face_cache *,
413 Lisp_Object *, int));
414 static struct face *realize_x_face P_ ((struct face_cache *,
415 Lisp_Object *, int));
416 static struct face *realize_tty_face P_ ((struct face_cache *,
417 Lisp_Object *, int));
418 static int realize_basic_faces P_ ((struct frame *));
419 static int realize_default_face P_ ((struct frame *));
420 static void realize_named_face P_ ((struct frame *, Lisp_Object, int));
421 static int lface_fully_specified_p P_ ((Lisp_Object *));
422 static int lface_equal_p P_ ((Lisp_Object *, Lisp_Object *));
423 static unsigned hash_string_case_insensitive P_ ((Lisp_Object));
424 static unsigned lface_hash P_ ((Lisp_Object *));
425 static int lface_same_font_attributes_p P_ ((Lisp_Object *, Lisp_Object *));
426 static struct face_cache *make_face_cache P_ ((struct frame *));
427 static void free_realized_face P_ ((struct frame *, struct face *));
428 static void clear_face_gcs P_ ((struct face_cache *));
429 static void free_face_cache P_ ((struct face_cache *));
430 static int face_numeric_weight P_ ((Lisp_Object));
431 static int face_numeric_slant P_ ((Lisp_Object));
432 static int face_numeric_swidth P_ ((Lisp_Object));
433 static int face_fontset P_ ((struct frame *, Lisp_Object *));
434 static char *choose_face_font P_ ((struct frame *, Lisp_Object *, int,
435 Lisp_Object));
436 static char *choose_face_fontset_font P_ ((struct frame *, Lisp_Object *,
437 int, int));
438 static void merge_face_vectors P_ ((Lisp_Object *from, Lisp_Object *));
439 static void merge_face_vector_with_property P_ ((struct frame *, Lisp_Object *,
440 Lisp_Object));
441 static int set_lface_from_font_name P_ ((struct frame *, Lisp_Object, char *,
442 int));
443 static Lisp_Object lface_from_face_name P_ ((struct frame *, Lisp_Object, int));
444 static struct face *make_realized_face P_ ((Lisp_Object *, int, Lisp_Object));
445 static void free_realized_faces P_ ((struct face_cache *));
446 static char *best_matching_font P_ ((struct frame *, Lisp_Object *,
447 struct font_name *, int));
448 static void cache_face P_ ((struct face_cache *, struct face *, unsigned));
449 static void uncache_face P_ ((struct face_cache *, struct face *));
450 static int xlfd_numeric_slant P_ ((struct font_name *));
451 static int xlfd_numeric_weight P_ ((struct font_name *));
452 static int xlfd_numeric_swidth P_ ((struct font_name *));
453 static Lisp_Object xlfd_symbolic_slant P_ ((struct font_name *));
454 static Lisp_Object xlfd_symbolic_weight P_ ((struct font_name *));
455 static Lisp_Object xlfd_symbolic_swidth P_ ((struct font_name *));
456 static int xlfd_fixed_p P_ ((struct font_name *));
457 static int xlfd_numeric_value P_ ((struct table_entry *, int, struct font_name *,
458 int, int));
459 static Lisp_Object xlfd_symbolic_value P_ ((struct table_entry *, int,
460 struct font_name *, int, int));
461 static struct table_entry *xlfd_lookup_field_contents P_ ((struct table_entry *, int,
462 struct font_name *, int));
463
464 #ifdef HAVE_X_WINDOWS
465
466 static int split_font_name P_ ((struct frame *, struct font_name *, int));
467 static int xlfd_point_size P_ ((struct frame *, struct font_name *));
468 static void sort_fonts P_ ((struct frame *, struct font_name *, int,
469 int (*cmpfn) P_ ((const void *, const void *))));
470 static GC x_create_gc P_ ((struct frame *, unsigned long, XGCValues *));
471 static void x_free_gc P_ ((struct frame *, GC));
472 static void clear_font_table P_ ((struct frame *));
473
474 #endif /* HAVE_X_WINDOWS */
475
476 \f
477 /***********************************************************************
478 Utilities
479 ***********************************************************************/
480
481 #ifdef HAVE_X_WINDOWS
482
483 /* Create and return a GC for use on frame F. GC values and mask
484 are given by XGCV and MASK. */
485
486 static INLINE GC
487 x_create_gc (f, mask, xgcv)
488 struct frame *f;
489 unsigned long mask;
490 XGCValues *xgcv;
491 {
492 GC gc;
493 BLOCK_INPUT;
494 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
495 UNBLOCK_INPUT;
496 IF_DEBUG (++ngcs);
497 return gc;
498 }
499
500
501 /* Free GC which was used on frame F. */
502
503 static INLINE void
504 x_free_gc (f, gc)
505 struct frame *f;
506 GC gc;
507 {
508 BLOCK_INPUT;
509 xassert (--ngcs >= 0);
510 XFreeGC (FRAME_X_DISPLAY (f), gc);
511 UNBLOCK_INPUT;
512 }
513
514 #endif /* HAVE_X_WINDOWS */
515
516
517 /* Like strdup, but uses xmalloc. */
518
519 static char *
520 xstrdup (s)
521 char *s;
522 {
523 int len = strlen (s) + 1;
524 char *p = (char *) xmalloc (len);
525 bcopy (s, p, len);
526 return p;
527 }
528
529
530 /* Like stricmp. Used to compare parts of font names which are in
531 ISO8859-1. */
532
533 int
534 xstricmp (s1, s2)
535 unsigned char *s1, *s2;
536 {
537 while (*s1 && *s2)
538 {
539 unsigned char c1 = tolower (*s1);
540 unsigned char c2 = tolower (*s2);
541 if (c1 != c2)
542 return c1 < c2 ? -1 : 1;
543 ++s1, ++s2;
544 }
545
546 if (*s1 == 0)
547 return *s2 == 0 ? 0 : -1;
548 return 1;
549 }
550
551
552 /* Like strlwr, which might not always be available. */
553
554 static unsigned char *
555 xstrlwr (s)
556 unsigned char *s;
557 {
558 unsigned char *p = s;
559
560 for (p = s; *p; ++p)
561 *p = tolower (*p);
562
563 return s;
564 }
565
566
567 /* Signal `error' with message S, and additional argument ARG. */
568
569 static void
570 signal_error (s, arg)
571 char *s;
572 Lisp_Object arg;
573 {
574 Fsignal (Qerror, Fcons (build_string (s), Fcons (arg, Qnil)));
575 }
576
577
578 /* Display a message with format string FORMAT and arguments ARG1 and
579 ARG2 on frame F. Used to display errors if fonts, bitmaps, colors
580 etc. for a realized face on frame F cannot be loaded. (If we would
581 signal an error in these cases, we would end up in an infinite
582 recursion because this would stop realization, and the redisplay
583 triggered by the signal would try to realize that same face again.)
584
585 If basic faces of F are not realized, just add the message to the
586 messages buffer "*Messages*". Because Fmessage calls
587 echo_area_display which tries to realize basic faces again, we would
588 otherwise also end in an infinite recursion. */
589
590 static void
591 display_message (f, format, arg1, arg2)
592 struct frame *f;
593 char *format;
594 Lisp_Object arg1, arg2;
595 {
596 Lisp_Object args[3];
597 Lisp_Object nargs;
598 extern int waiting_for_input;
599
600 /* Function note_mouse_highlight calls face_at_buffer_position which
601 may realize a face. If some attribute of that face is invalid,
602 say an invalid color, don't display an error to avoid calling
603 Lisp from XTread_socket. */
604 if (waiting_for_input)
605 return;
606
607 nargs = make_number (DIM (args));
608 args[0] = build_string (format);
609 args[1] = arg1;
610 args[2] = arg2;
611
612 if (f->face_cache->used >= BASIC_FACE_ID_SENTINEL)
613 Fmessage (nargs, args);
614 else
615 {
616 Lisp_Object msg = Fformat (nargs, args);
617 char *buffer = LSTRDUPA (msg);
618 message_dolog (buffer, strlen (buffer), 1, 0);
619 }
620 }
621
622
623 /* If FRAME is nil, return selected_frame. Otherwise, check that
624 FRAME is a live frame, and return a pointer to it. NPARAM
625 is the parameter number of FRAME, for CHECK_LIVE_FRAME. This is
626 here because it's a frequent pattern in Lisp function definitions. */
627
628 static INLINE struct frame *
629 frame_or_selected_frame (frame, nparam)
630 Lisp_Object frame;
631 int nparam;
632 {
633 struct frame *f;
634
635 if (NILP (frame))
636 f = selected_frame;
637 else
638 {
639 CHECK_LIVE_FRAME (frame, nparam);
640 f = XFRAME (frame);
641 }
642
643 return f;
644 }
645
646 \f
647 /***********************************************************************
648 Frames and faces
649 ***********************************************************************/
650
651 /* Initialize face cache and basic faces for frame F. */
652
653 void
654 init_frame_faces (f)
655 struct frame *f;
656 {
657 /* Make a face cache, if F doesn't have one. */
658 if (FRAME_FACE_CACHE (f) == NULL)
659 FRAME_FACE_CACHE (f) = make_face_cache (f);
660
661 #ifdef HAVE_X_WINDOWS
662 /* Make the image cache. */
663 if (FRAME_X_P (f))
664 {
665 if (FRAME_X_IMAGE_CACHE (f) == NULL)
666 FRAME_X_IMAGE_CACHE (f) = make_image_cache ();
667 ++FRAME_X_IMAGE_CACHE (f)->refcount;
668 }
669 #endif /* HAVE_X_WINDOWS */
670
671 /* Realize basic faces. Must have enough information in frame
672 parameters to realize basic faces at this point. */
673 #ifdef HAVE_X_WINDOWS
674 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
675 #endif
676 if (!realize_basic_faces (f))
677 abort ();
678 }
679
680
681 /* Free face cache of frame F. Called from Fdelete_frame. */
682
683 void
684 free_frame_faces (f)
685 struct frame *f;
686 {
687 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
688
689 if (face_cache)
690 {
691 free_face_cache (face_cache);
692 FRAME_FACE_CACHE (f) = NULL;
693 }
694
695 #ifdef HAVE_X_WINDOWS
696 if (FRAME_X_P (f))
697 {
698 struct image_cache *image_cache = FRAME_X_IMAGE_CACHE (f);
699 if (image_cache)
700 {
701 --image_cache->refcount;
702 if (image_cache->refcount == 0)
703 free_image_cache (f);
704 }
705 }
706 #endif /* HAVE_X_WINDOWS */
707 }
708
709
710 /* Recompute basic faces for frame F. Call this after changing frame
711 parameters on which those faces depend, or when realized faces have
712 been freed due to changing attributes of named faces. */
713
714 void
715 recompute_basic_faces (f)
716 struct frame *f;
717 {
718 if (FRAME_FACE_CACHE (f))
719 {
720 int realized_p = realize_basic_faces (f);
721 xassert (realized_p);
722 }
723 }
724
725
726 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
727 try to free unused fonts, too. */
728
729 void
730 clear_face_cache (clear_fonts_p)
731 int clear_fonts_p;
732 {
733 #ifdef HAVE_X_WINDOWS
734 Lisp_Object tail, frame;
735 struct frame *f;
736
737 if (clear_fonts_p
738 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
739 {
740 /* From time to time see if we can unload some fonts. This also
741 frees all realized faces on all frames. Fonts needed by
742 faces will be loaded again when faces are realized again. */
743 clear_font_table_count = 0;
744
745 FOR_EACH_FRAME (tail, frame)
746 {
747 f = XFRAME (frame);
748 if (FRAME_X_P (f)
749 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
750 {
751 free_all_realized_faces (frame);
752 clear_font_table (f);
753 }
754 }
755 }
756 else
757 {
758 /* Clear GCs of realized faces. */
759 FOR_EACH_FRAME (tail, frame)
760 {
761 f = XFRAME (frame);
762 if (FRAME_X_P (f))
763 {
764 clear_face_gcs (FRAME_FACE_CACHE (f));
765 clear_image_cache (f, 0);
766 }
767 }
768 }
769 #endif /* HAVE_X_WINDOWS */
770 }
771
772
773 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
774 "Clear face caches on all frames.\n\
775 Optional THOROUGHLY non-nil means try to free unused fonts, too.")
776 (thorougly)
777 Lisp_Object thorougly;
778 {
779 clear_face_cache (!NILP (thorougly));
780 return Qnil;
781 }
782
783
784
785 #ifdef HAVE_X_WINDOWS
786
787
788 /* Remove those fonts from the font table of frame F that are not used
789 by fontsets. Called from clear_face_cache from time to time. */
790
791 static void
792 clear_font_table (f)
793 struct frame *f;
794 {
795 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
796 char *used;
797 Lisp_Object rest, frame;
798 int i;
799
800 xassert (FRAME_X_P (f));
801
802 used = (char *) alloca (dpyinfo->n_fonts * sizeof *used);
803 bzero (used, dpyinfo->n_fonts * sizeof *used);
804
805 /* For all frames with the same x_display_info as F, record
806 in `used' those fonts that are in use by fontsets. */
807 FOR_EACH_FRAME (rest, frame)
808 if (FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
809 {
810 struct frame *f = XFRAME (frame);
811 struct fontset_data *fontset_data = FRAME_FONTSET_DATA (f);
812
813 for (i = 0; i < fontset_data->n_fontsets; ++i)
814 {
815 struct fontset_info *info = fontset_data->fontset_table[i];
816 int j;
817
818 for (j = 0; j <= MAX_CHARSET; ++j)
819 {
820 int idx = info->font_indexes[j];
821 if (idx >= 0)
822 used[idx] = 1;
823 }
824 }
825 }
826
827 /* Free those fonts that are not used by fontsets. */
828 for (i = 0; i < dpyinfo->n_fonts; ++i)
829 if (used[i] == 0 && dpyinfo->font_table[i].name)
830 {
831 struct font_info *font_info = dpyinfo->font_table + i;
832
833 /* Free names. In xfns.c there is a comment that full_name
834 should never be freed because it is always shared with
835 something else. I don't think this is true anymore---see
836 x_load_font. It's either equal to font_info->name or
837 allocated via xmalloc, and there seems to be no place in
838 the source files where full_name is transferred to another
839 data structure. */
840 if (font_info->full_name != font_info->name)
841 xfree (font_info->full_name);
842 xfree (font_info->name);
843
844 /* Free the font. */
845 BLOCK_INPUT;
846 XFreeFont (dpyinfo->display, font_info->font);
847 UNBLOCK_INPUT;
848
849 /* Mark font table slot free. */
850 font_info->font = NULL;
851 font_info->name = font_info->full_name = NULL;
852 }
853 }
854
855
856 #endif /* HAVE_X_WINDOWS */
857
858
859 \f
860 /***********************************************************************
861 X Pixmaps
862 ***********************************************************************/
863
864 #ifdef HAVE_X_WINDOWS
865
866 DEFUN ("pixmap-spec-p", Fpixmap_spec_p, Spixmap_spec_p, 1, 1, 0,
867 "Non-nil if OBJECT is a valid pixmap specification.\n\
868 A pixmap specification is either a string, or a list (WIDTH HEIGHT DATA)\n\
869 where WIDTH is the pixel width of the pixmap, HEIGHT is its height,\n\
870 and DATA contains the bits of the pixmap.")
871 (object)
872 Lisp_Object object;
873 {
874 Lisp_Object height, width;
875
876 return ((STRINGP (object)
877 || (CONSP (object)
878 && CONSP (XCONS (object)->cdr)
879 && CONSP (XCONS (XCONS (object)->cdr)->cdr)
880 && NILP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->cdr)
881 && (width = XCONS (object)->car, INTEGERP (width))
882 && (height = XCONS (XCONS (object)->cdr)->car,
883 INTEGERP (height))
884 && STRINGP (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)
885 && XINT (width) > 0
886 && XINT (height) > 0
887 /* The string must have enough bits for width * height. */
888 && ((XSTRING (XCONS (XCONS (XCONS (object)->cdr)->cdr)->car)->size
889 * (BITS_PER_INT / sizeof (int)))
890 >= XFASTINT (width) * XFASTINT (height))))
891 ? Qt : Qnil);
892 }
893
894
895 /* Load a bitmap according to NAME (which is either a file name or a
896 pixmap spec) for use on frame F. Value is the bitmap_id (see
897 xfns.c). If NAME is nil, return with a bitmap id of zero. If
898 bitmap cannot be loaded, display a message saying so, and return
899 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
900 if these pointers are not null. */
901
902 static int
903 load_pixmap (f, name, w_ptr, h_ptr)
904 FRAME_PTR f;
905 Lisp_Object name;
906 unsigned int *w_ptr, *h_ptr;
907 {
908 int bitmap_id;
909 Lisp_Object tem;
910
911 if (NILP (name))
912 return 0;
913
914 tem = Fpixmap_spec_p (name);
915 if (NILP (tem))
916 wrong_type_argument (Qpixmap_spec_p, name);
917
918 BLOCK_INPUT;
919 if (CONSP (name))
920 {
921 /* Decode a bitmap spec into a bitmap. */
922
923 int h, w;
924 Lisp_Object bits;
925
926 w = XINT (Fcar (name));
927 h = XINT (Fcar (Fcdr (name)));
928 bits = Fcar (Fcdr (Fcdr (name)));
929
930 bitmap_id = x_create_bitmap_from_data (f, XSTRING (bits)->data,
931 w, h);
932 }
933 else
934 {
935 /* It must be a string -- a file name. */
936 bitmap_id = x_create_bitmap_from_file (f, name);
937 }
938 UNBLOCK_INPUT;
939
940 if (bitmap_id < 0)
941 {
942 display_message (f, "Invalid or undefined bitmap %s", name, Qnil);
943 bitmap_id = 0;
944
945 if (w_ptr)
946 *w_ptr = 0;
947 if (h_ptr)
948 *h_ptr = 0;
949 }
950 else
951 {
952 #if GLYPH_DEBUG
953 ++npixmaps_allocated;
954 #endif
955 if (w_ptr)
956 *w_ptr = x_bitmap_width (f, bitmap_id);
957
958 if (h_ptr)
959 *h_ptr = x_bitmap_height (f, bitmap_id);
960 }
961
962 return bitmap_id;
963 }
964
965 #endif /* HAVE_X_WINDOWS */
966
967
968 \f
969 /***********************************************************************
970 Minimum font bounds
971 ***********************************************************************/
972
973 #ifdef HAVE_X_WINDOWS
974
975 /* Update the line_height of frame F. Return non-zero if line height
976 changes. */
977
978 int
979 frame_update_line_height (f)
980 struct frame *f;
981 {
982 int fontset, line_height, changed_p;
983
984 fontset = f->output_data.x->fontset;
985 if (fontset > 0)
986 line_height = FRAME_FONTSET_DATA (f)->fontset_table[fontset]->height;
987 else
988 line_height = FONT_HEIGHT (f->output_data.x->font);
989
990 changed_p = line_height != f->output_data.x->line_height;
991 f->output_data.x->line_height = line_height;
992 return changed_p;
993 }
994
995 #endif /* HAVE_X_WINDOWS */
996
997 \f
998 /***********************************************************************
999 Fonts
1000 ***********************************************************************/
1001
1002 #ifdef HAVE_X_WINDOWS
1003
1004 /* Load font or fontset of face FACE which is used on frame F.
1005 FONTSET is the fontset FACE should use or -1, if FACE doesn't use a
1006 fontset. FONT_NAME is the name of the font to load, if no fontset
1007 is used. It is null if no suitable font name could be determined
1008 for the face. */
1009
1010 static void
1011 load_face_font_or_fontset (f, face, font_name, fontset)
1012 struct frame *f;
1013 struct face *face;
1014 char *font_name;
1015 int fontset;
1016 {
1017 struct font_info *font_info = NULL;
1018
1019 face->font_info_id = -1;
1020 face->fontset = fontset;
1021 face->font = NULL;
1022
1023 BLOCK_INPUT;
1024 if (fontset >= 0)
1025 font_info = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII,
1026 NULL, fontset);
1027 else if (font_name)
1028 font_info = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f), face->charset,
1029 font_name, -1);
1030 UNBLOCK_INPUT;
1031
1032 if (font_info)
1033 {
1034 char *s;
1035 int i;
1036
1037 face->font_info_id = FONT_INFO_ID (f, font_info);
1038 face->font = font_info->font;
1039 face->font_name = font_info->full_name;
1040
1041 /* Make the registry part of the font name readily accessible.
1042 The registry is used to find suitable faces for unibyte text. */
1043 s = font_info->full_name + strlen (font_info->full_name);
1044 i = 0;
1045 while (i < 2 && --s >= font_info->full_name)
1046 if (*s == '-')
1047 ++i;
1048
1049 if (!STRINGP (face->registry)
1050 || xstricmp (XSTRING (face->registry)->data, s + 1) != 0)
1051 {
1052 if (STRINGP (Vface_default_registry)
1053 && !xstricmp (XSTRING (Vface_default_registry)->data, s + 1))
1054 face->registry = Vface_default_registry;
1055 else
1056 face->registry = build_string (s + 1);
1057 }
1058 }
1059 else if (fontset >= 0)
1060 display_message (f, "Unable to load ASCII font of fontset %d",
1061 make_number (fontset), Qnil);
1062 else if (font_name)
1063 display_message (f, "Unable to load font %s",
1064 build_string (font_name), Qnil);
1065 }
1066
1067 #endif /* HAVE_X_WINDOWS */
1068
1069
1070 \f
1071 /***********************************************************************
1072 X Colors
1073 ***********************************************************************/
1074
1075 #ifdef HAVE_X_WINDOWS
1076
1077 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1078 black) on frame F. The algorithm is taken from 20.2 faces.el. */
1079
1080 static int
1081 face_color_gray_p (f, color_name)
1082 struct frame *f;
1083 char *color_name;
1084 {
1085 XColor color;
1086 int gray_p;
1087
1088 if (defined_color (f, color_name, &color, 0))
1089 gray_p = ((abs (color.red - color.green)
1090 < max (color.red, color.green) / 20)
1091 && (abs (color.green - color.blue)
1092 < max (color.green, color.blue) / 20)
1093 && (abs (color.blue - color.red)
1094 < max (color.blue, color.red) / 20));
1095 else
1096 gray_p = 0;
1097
1098 return gray_p;
1099 }
1100
1101
1102 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1103 BACKGROUND_P non-zero means the color will be used as background
1104 color. */
1105
1106 static int
1107 face_color_supported_p (f, color_name, background_p)
1108 struct frame *f;
1109 char *color_name;
1110 int background_p;
1111 {
1112 Lisp_Object frame;
1113
1114 XSETFRAME (frame, f);
1115 return (!NILP (Vwindow_system)
1116 && (!NILP (Fx_display_color_p (frame))
1117 || xstricmp (color_name, "black") == 0
1118 || xstricmp (color_name, "white") == 0
1119 || (background_p
1120 && face_color_gray_p (f, color_name))
1121 || (!NILP (Fx_display_grayscale_p (frame))
1122 && face_color_gray_p (f, color_name))));
1123 }
1124
1125
1126 DEFUN ("face-color-gray-p", Fface_color_gray_p, Sface_color_gray_p, 1, 2, 0,
1127 "Return non-nil if COLOR is a shade of gray (or white or black).\n\
1128 FRAME specifies the frame and thus the display for interpreting COLOR.\n\
1129 If FRAME is nil or omitted, use the selected frame.")
1130 (color, frame)
1131 Lisp_Object color, frame;
1132 {
1133 struct frame *f = check_x_frame (frame);
1134 CHECK_STRING (color, 0);
1135 return face_color_gray_p (f, XSTRING (color)->data) ? Qt : Qnil;
1136 }
1137
1138
1139 DEFUN ("face-color-supported-p", Fface_color_supported_p,
1140 Sface_color_supported_p, 2, 3, 0,
1141 "Return non-nil if COLOR can be displayed on FRAME.\n\
1142 BACKGROUND-P non-nil means COLOR is used as a background.\n\
1143 If FRAME is nil or omitted, use the selected frame.\n\
1144 COLOR must be a valid color name.")
1145 (frame, color, background_p)
1146 Lisp_Object frame, color, background_p;
1147 {
1148 struct frame *f = check_x_frame (frame);
1149 CHECK_STRING (color, 0);
1150 if (face_color_supported_p (f, XSTRING (color)->data, !NILP (background_p)))
1151 return Qt;
1152 return Qnil;
1153 }
1154
1155 /* Load color with name NAME for use by face FACE on frame F.
1156 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1157 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1158 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1159 pixel color. If color cannot be loaded, display a message, and
1160 return the foreground, background or underline color of F, but
1161 record that fact in flags of the face so that we don't try to free
1162 these colors. */
1163
1164 static unsigned long
1165 load_color (f, face, name, target_index)
1166 struct frame *f;
1167 struct face *face;
1168 Lisp_Object name;
1169 enum lface_attribute_index target_index;
1170 {
1171 XColor color;
1172
1173 xassert (STRINGP (name));
1174 xassert (target_index == LFACE_FOREGROUND_INDEX
1175 || target_index == LFACE_BACKGROUND_INDEX
1176 || target_index == LFACE_UNDERLINE_INDEX
1177 || target_index == LFACE_OVERLINE_INDEX
1178 || target_index == LFACE_STRIKE_THROUGH_INDEX
1179 || target_index == LFACE_BOX_INDEX);
1180
1181 /* if the color map is full, defined_color will return a best match
1182 to the values in an existing cell. */
1183 if (!defined_color (f, XSTRING (name)->data, &color, 1))
1184 {
1185 display_message (f, "Unable to load color %s", name, Qnil);
1186
1187 switch (target_index)
1188 {
1189 case LFACE_FOREGROUND_INDEX:
1190 face->foreground_defaulted_p = 1;
1191 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1192 break;
1193
1194 case LFACE_BACKGROUND_INDEX:
1195 face->background_defaulted_p = 1;
1196 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1197 break;
1198
1199 case LFACE_UNDERLINE_INDEX:
1200 face->underline_defaulted_p = 1;
1201 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1202 break;
1203
1204 case LFACE_OVERLINE_INDEX:
1205 face->overline_color_defaulted_p = 1;
1206 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1207 break;
1208
1209 case LFACE_STRIKE_THROUGH_INDEX:
1210 face->strike_through_color_defaulted_p = 1;
1211 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1212 break;
1213
1214 case LFACE_BOX_INDEX:
1215 face->box_color_defaulted_p = 1;
1216 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1217 break;
1218
1219 default:
1220 abort ();
1221 }
1222 }
1223 #if GLYPH_DEBUG
1224 else
1225 ++ncolors_allocated;
1226 #endif
1227
1228 return color.pixel;
1229 }
1230
1231
1232 /* Load colors for face FACE which is used on frame F. Colors are
1233 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1234 of ATTRS. If the background color specified is not supported on F,
1235 try to emulate gray colors with a stipple from Vface_default_stipple. */
1236
1237 static void
1238 load_face_colors (f, face, attrs)
1239 struct frame *f;
1240 struct face *face;
1241 Lisp_Object *attrs;
1242 {
1243 Lisp_Object fg, bg;
1244
1245 bg = attrs[LFACE_BACKGROUND_INDEX];
1246 fg = attrs[LFACE_FOREGROUND_INDEX];
1247
1248 /* Swap colors if face is inverse-video. */
1249 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1250 {
1251 Lisp_Object tmp;
1252 tmp = fg;
1253 fg = bg;
1254 bg = tmp;
1255 }
1256
1257 /* Check for support for foreground, not for background because
1258 face_color_supported_p is smart enough to know that grays are
1259 "supported" as background because we are supposed to use stipple
1260 for them. */
1261 if (!face_color_supported_p (f, XSTRING (bg)->data, 0)
1262 && !NILP (Fpixmap_spec_p (Vface_default_stipple)))
1263 {
1264 x_destroy_bitmap (f, face->stipple);
1265 face->stipple = load_pixmap (f, Vface_default_stipple,
1266 &face->pixmap_w, &face->pixmap_h);
1267 }
1268 else
1269 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1270
1271 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1272 }
1273
1274
1275 /* Free color PIXEL on frame F. */
1276
1277 void
1278 unload_color (f, pixel)
1279 struct frame *f;
1280 unsigned long pixel;
1281 {
1282 Display *dpy = FRAME_X_DISPLAY (f);
1283 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
1284
1285 if (pixel == BLACK_PIX_DEFAULT (f)
1286 || pixel == WHITE_PIX_DEFAULT (f))
1287 return;
1288
1289 BLOCK_INPUT;
1290
1291 /* If display has an immutable color map, freeing colors is not
1292 necessary and some servers don't allow it. So don't do it. */
1293 if (! (class == StaticColor || class == StaticGray || class == TrueColor))
1294 {
1295 Colormap cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
1296 XFreeColors (dpy, cmap, &pixel, 1, 0);
1297 }
1298
1299 UNBLOCK_INPUT;
1300 }
1301
1302
1303 /* Free colors allocated for FACE. */
1304
1305 static void
1306 free_face_colors (f, face)
1307 struct frame *f;
1308 struct face *face;
1309 {
1310 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
1311
1312 /* If display has an immutable color map, freeing colors is not
1313 necessary and some servers don't allow it. So don't do it. */
1314 if (class != StaticColor
1315 && class != StaticGray
1316 && class != TrueColor)
1317 {
1318 Display *dpy;
1319 Colormap cmap;
1320
1321 BLOCK_INPUT;
1322 dpy = FRAME_X_DISPLAY (f);
1323 cmap = DefaultColormapOfScreen (FRAME_X_SCREEN (f));
1324
1325 if (face->foreground != BLACK_PIX_DEFAULT (f)
1326 && face->foreground != WHITE_PIX_DEFAULT (f)
1327 && !face->foreground_defaulted_p)
1328 {
1329 XFreeColors (dpy, cmap, &face->foreground, 1, 0);
1330 IF_DEBUG (--ncolors_allocated);
1331 }
1332
1333 if (face->background != BLACK_PIX_DEFAULT (f)
1334 && face->background != WHITE_PIX_DEFAULT (f)
1335 && !face->background_defaulted_p)
1336 {
1337 XFreeColors (dpy, cmap, &face->background, 1, 0);
1338 IF_DEBUG (--ncolors_allocated);
1339 }
1340
1341 if (face->underline_p
1342 && !face->underline_defaulted_p
1343 && face->underline_color != BLACK_PIX_DEFAULT (f)
1344 && face->underline_color != WHITE_PIX_DEFAULT (f))
1345 {
1346 XFreeColors (dpy, cmap, &face->underline_color, 1, 0);
1347 IF_DEBUG (--ncolors_allocated);
1348 }
1349
1350 if (face->overline_p
1351 && !face->overline_color_defaulted_p
1352 && face->overline_color != BLACK_PIX_DEFAULT (f)
1353 && face->overline_color != WHITE_PIX_DEFAULT (f))
1354 {
1355 XFreeColors (dpy, cmap, &face->overline_color, 1, 0);
1356 IF_DEBUG (--ncolors_allocated);
1357 }
1358
1359 if (face->strike_through_p
1360 && !face->strike_through_color_defaulted_p
1361 && face->strike_through_color != BLACK_PIX_DEFAULT (f)
1362 && face->strike_through_color != WHITE_PIX_DEFAULT (f))
1363 {
1364 XFreeColors (dpy, cmap, &face->strike_through_color, 1, 0);
1365 IF_DEBUG (--ncolors_allocated);
1366 }
1367
1368 if (face->box != FACE_NO_BOX
1369 && !face->box_color_defaulted_p
1370 && face->box_color != BLACK_PIX_DEFAULT (f)
1371 && face->box_color != WHITE_PIX_DEFAULT (f))
1372 {
1373 XFreeColors (dpy, cmap, &face->box_color, 1, 0);
1374 IF_DEBUG (--ncolors_allocated);
1375 }
1376
1377 UNBLOCK_INPUT;
1378 }
1379 }
1380
1381 #endif /* HAVE_X_WINDOWS */
1382
1383
1384 \f
1385 /***********************************************************************
1386 XLFD Font Names
1387 ***********************************************************************/
1388
1389 /* An enumerator for each field of an XLFD font name. */
1390
1391 enum xlfd_field
1392 {
1393 XLFD_FOUNDRY,
1394 XLFD_FAMILY,
1395 XLFD_WEIGHT,
1396 XLFD_SLANT,
1397 XLFD_SWIDTH,
1398 XLFD_ADSTYLE,
1399 XLFD_PIXEL_SIZE,
1400 XLFD_POINT_SIZE,
1401 XLFD_RESX,
1402 XLFD_RESY,
1403 XLFD_SPACING,
1404 XLFD_AVGWIDTH,
1405 XLFD_REGISTRY,
1406 XLFD_ENCODING,
1407 XLFD_LAST
1408 };
1409
1410 /* An enumerator for each possible slant value of a font. Taken from
1411 the XLFD specification. */
1412
1413 enum xlfd_slant
1414 {
1415 XLFD_SLANT_UNKNOWN,
1416 XLFD_SLANT_ROMAN,
1417 XLFD_SLANT_ITALIC,
1418 XLFD_SLANT_OBLIQUE,
1419 XLFD_SLANT_REVERSE_ITALIC,
1420 XLFD_SLANT_REVERSE_OBLIQUE,
1421 XLFD_SLANT_OTHER
1422 };
1423
1424 /* Relative font weight according to XLFD documentation. */
1425
1426 enum xlfd_weight
1427 {
1428 XLFD_WEIGHT_UNKNOWN,
1429 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1430 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1431 XLFD_WEIGHT_LIGHT, /* 30 */
1432 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1433 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1434 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1435 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1436 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1437 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1438 };
1439
1440 /* Relative proportionate width. */
1441
1442 enum xlfd_swidth
1443 {
1444 XLFD_SWIDTH_UNKNOWN,
1445 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1446 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1447 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1448 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1449 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1450 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1451 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1452 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1453 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1454 };
1455
1456 /* Structure used for tables mapping XLFD weight, slant, and width
1457 names to numeric and symbolic values. */
1458
1459 struct table_entry
1460 {
1461 char *name;
1462 int numeric;
1463 Lisp_Object *symbol;
1464 };
1465
1466 /* Table of XLFD slant names and their numeric and symbolic
1467 representations. This table must be sorted by slant names in
1468 ascending order. */
1469
1470 static struct table_entry slant_table[] =
1471 {
1472 {"i", XLFD_SLANT_ITALIC, &Qitalic},
1473 {"o", XLFD_SLANT_OBLIQUE, &Qoblique},
1474 {"ot", XLFD_SLANT_OTHER, &Qitalic},
1475 {"r", XLFD_SLANT_ROMAN, &Qnormal},
1476 {"ri", XLFD_SLANT_REVERSE_ITALIC, &Qreverse_italic},
1477 {"ro", XLFD_SLANT_REVERSE_OBLIQUE, &Qreverse_oblique}
1478 };
1479
1480 /* Table of XLFD weight names. This table must be sorted by weight
1481 names in ascending order. */
1482
1483 static struct table_entry weight_table[] =
1484 {
1485 {"black", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold},
1486 {"bold", XLFD_WEIGHT_BOLD, &Qbold},
1487 {"book", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1488 {"demibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1489 {"extralight", XLFD_WEIGHT_EXTRA_LIGHT, &Qextra_light},
1490 {"extrabold", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1491 {"heavy", XLFD_WEIGHT_EXTRA_BOLD, &Qextra_bold},
1492 {"light", XLFD_WEIGHT_LIGHT, &Qlight},
1493 {"medium", XLFD_WEIGHT_MEDIUM, &Qnormal},
1494 {"normal", XLFD_WEIGHT_MEDIUM, &Qnormal},
1495 {"regular", XLFD_WEIGHT_MEDIUM, &Qnormal},
1496 {"semibold", XLFD_WEIGHT_SEMI_BOLD, &Qsemi_bold},
1497 {"semilight", XLFD_WEIGHT_SEMI_LIGHT, &Qsemi_light},
1498 {"ultralight", XLFD_WEIGHT_ULTRA_LIGHT, &Qultra_light},
1499 {"ultrabold", XLFD_WEIGHT_ULTRA_BOLD, &Qultra_bold}
1500 };
1501
1502 /* Table of XLFD width names. This table must be sorted by width
1503 names in ascending order. */
1504
1505 static struct table_entry swidth_table[] =
1506 {
1507 {"compressed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1508 {"condensed", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1509 {"demiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1510 {"expanded", XLFD_SWIDTH_EXPANDED, &Qexpanded},
1511 {"extracondensed", XLFD_SWIDTH_EXTRA_CONDENSED, &Qextra_condensed},
1512 {"extraexpanded", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded},
1513 {"medium", XLFD_SWIDTH_MEDIUM, &Qnormal},
1514 {"narrow", XLFD_SWIDTH_CONDENSED, &Qcondensed},
1515 {"normal", XLFD_SWIDTH_MEDIUM, &Qnormal},
1516 {"regular", XLFD_SWIDTH_MEDIUM, &Qnormal},
1517 {"semicondensed", XLFD_SWIDTH_SEMI_CONDENSED, &Qsemi_condensed},
1518 {"semiexpanded", XLFD_SWIDTH_SEMI_EXPANDED, &Qsemi_expanded},
1519 {"ultracondensed", XLFD_SWIDTH_ULTRA_CONDENSED, &Qultra_condensed},
1520 {"ultraexpanded", XLFD_SWIDTH_ULTRA_EXPANDED, &Qultra_expanded},
1521 {"wide", XLFD_SWIDTH_EXTRA_EXPANDED, &Qextra_expanded}
1522 };
1523
1524 /* Structure used to hold the result of splitting font names in XLFD
1525 format into their fields. */
1526
1527 struct font_name
1528 {
1529 /* The original name which is modified destructively by
1530 split_font_name. The pointer is kept here to be able to free it
1531 if it was allocated from the heap. */
1532 char *name;
1533
1534 /* Font name fields. Each vector element points into `name' above.
1535 Fields are NUL-terminated. */
1536 char *fields[XLFD_LAST];
1537
1538 /* Numeric values for those fields that interest us. See
1539 split_font_name for which these are. */
1540 int numeric[XLFD_LAST];
1541 };
1542
1543 /* The frame in effect when sorting font names. Set temporarily in
1544 sort_fonts so that it is available in font comparison functions. */
1545
1546 static struct frame *font_frame;
1547
1548 /* Order by which font selection chooses fonts. The default values
1549 mean `first, find a best match for the font width, then for the
1550 font height, then for weight, then for slant.' This variable can be
1551 set via set-face-font-sort-order. */
1552
1553 static int font_sort_order[4];
1554
1555
1556 /* Look up FONT.fields[FIELD_INDEX] in TABLE which has DIM entries.
1557 TABLE must be sorted by TABLE[i]->name in ascending order. Value
1558 is a pointer to the matching table entry or null if no table entry
1559 matches. */
1560
1561 static struct table_entry *
1562 xlfd_lookup_field_contents (table, dim, font, field_index)
1563 struct table_entry *table;
1564 int dim;
1565 struct font_name *font;
1566 int field_index;
1567 {
1568 /* Function split_font_name converts fields to lower-case, so there
1569 is no need to use xstrlwr or xstricmp here. */
1570 char *s = font->fields[field_index];
1571 int low, mid, high, cmp;
1572
1573 low = 0;
1574 high = dim - 1;
1575
1576 while (low <= high)
1577 {
1578 mid = (low + high) / 2;
1579 cmp = strcmp (table[mid].name, s);
1580
1581 if (cmp < 0)
1582 low = mid + 1;
1583 else if (cmp > 0)
1584 high = mid - 1;
1585 else
1586 return table + mid;
1587 }
1588
1589 return NULL;
1590 }
1591
1592
1593 /* Return a numeric representation for font name field
1594 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
1595 has DIM entries. Value is the numeric value found or DFLT if no
1596 table entry matches. This function is used to translate weight,
1597 slant, and swidth names of XLFD font names to numeric values. */
1598
1599 static INLINE int
1600 xlfd_numeric_value (table, dim, font, field_index, dflt)
1601 struct table_entry *table;
1602 int dim;
1603 struct font_name *font;
1604 int field_index;
1605 int dflt;
1606 {
1607 struct table_entry *p;
1608 p = xlfd_lookup_field_contents (table, dim, font, field_index);
1609 return p ? p->numeric : dflt;
1610 }
1611
1612
1613 /* Return a symbolic representation for font name field
1614 FONT.fields[FIELD_INDEX]. The field is looked up in TABLE which
1615 has DIM entries. Value is the symbolic value found or DFLT if no
1616 table entry matches. This function is used to translate weight,
1617 slant, and swidth names of XLFD font names to symbols. */
1618
1619 static INLINE Lisp_Object
1620 xlfd_symbolic_value (table, dim, font, field_index, dflt)
1621 struct table_entry *table;
1622 int dim;
1623 struct font_name *font;
1624 int field_index;
1625 int dflt;
1626 {
1627 struct table_entry *p;
1628 p = xlfd_lookup_field_contents (table, dim, font, field_index);
1629 return p ? *p->symbol : dflt;
1630 }
1631
1632
1633 /* Return a numeric value for the slant of the font given by FONT. */
1634
1635 static INLINE int
1636 xlfd_numeric_slant (font)
1637 struct font_name *font;
1638 {
1639 return xlfd_numeric_value (slant_table, DIM (slant_table),
1640 font, XLFD_SLANT, XLFD_SLANT_ROMAN);
1641 }
1642
1643
1644 /* Return a symbol representing the weight of the font given by FONT. */
1645
1646 static INLINE Lisp_Object
1647 xlfd_symbolic_slant (font)
1648 struct font_name *font;
1649 {
1650 return xlfd_symbolic_value (slant_table, DIM (slant_table),
1651 font, XLFD_SLANT, Qnormal);
1652 }
1653
1654
1655 /* Return a numeric value for the weight of the font given by FONT. */
1656
1657 static INLINE int
1658 xlfd_numeric_weight (font)
1659 struct font_name *font;
1660 {
1661 return xlfd_numeric_value (weight_table, DIM (weight_table),
1662 font, XLFD_WEIGHT, XLFD_WEIGHT_MEDIUM);
1663 }
1664
1665
1666 /* Return a symbol representing the slant of the font given by FONT. */
1667
1668 static INLINE Lisp_Object
1669 xlfd_symbolic_weight (font)
1670 struct font_name *font;
1671 {
1672 return xlfd_symbolic_value (weight_table, DIM (weight_table),
1673 font, XLFD_WEIGHT, Qnormal);
1674 }
1675
1676
1677 /* Return a numeric value for the swidth of the font whose XLFD font
1678 name fields are found in FONT. */
1679
1680 static INLINE int
1681 xlfd_numeric_swidth (font)
1682 struct font_name *font;
1683 {
1684 return xlfd_numeric_value (swidth_table, DIM (swidth_table),
1685 font, XLFD_SWIDTH, XLFD_SWIDTH_MEDIUM);
1686 }
1687
1688
1689 /* Return a symbolic value for the swidth of FONT. */
1690
1691 static INLINE Lisp_Object
1692 xlfd_symbolic_swidth (font)
1693 struct font_name *font;
1694 {
1695 return xlfd_symbolic_value (swidth_table, DIM (swidth_table),
1696 font, XLFD_SWIDTH, Qnormal);
1697 }
1698
1699
1700 /* Look up the entry of SYMBOL in the vector TABLE which has DIM
1701 entries. Value is a pointer to the matching table entry or null if
1702 no element of TABLE contains SYMBOL. */
1703
1704 static struct table_entry *
1705 face_value (table, dim, symbol)
1706 struct table_entry *table;
1707 int dim;
1708 Lisp_Object symbol;
1709 {
1710 int i;
1711
1712 xassert (SYMBOLP (symbol));
1713
1714 for (i = 0; i < dim; ++i)
1715 if (EQ (*table[i].symbol, symbol))
1716 break;
1717
1718 return i < dim ? table + i : NULL;
1719 }
1720
1721
1722 /* Return a numeric value for SYMBOL in the vector TABLE which has DIM
1723 entries. Value is -1 if SYMBOL is not found in TABLE. */
1724
1725 static INLINE int
1726 face_numeric_value (table, dim, symbol)
1727 struct table_entry *table;
1728 int dim;
1729 Lisp_Object symbol;
1730 {
1731 struct table_entry *p = face_value (table, dim, symbol);
1732 return p ? p->numeric : -1;
1733 }
1734
1735
1736 /* Return a numeric value representing the weight specified by Lisp
1737 symbol WEIGHT. Value is one of the enumerators of enum
1738 xlfd_weight. */
1739
1740 static INLINE int
1741 face_numeric_weight (weight)
1742 Lisp_Object weight;
1743 {
1744 return face_numeric_value (weight_table, DIM (weight_table), weight);
1745 }
1746
1747
1748 /* Return a numeric value representing the slant specified by Lisp
1749 symbol SLANT. Value is one of the enumerators of enum xlfd_slant. */
1750
1751 static INLINE int
1752 face_numeric_slant (slant)
1753 Lisp_Object slant;
1754 {
1755 return face_numeric_value (slant_table, DIM (slant_table), slant);
1756 }
1757
1758
1759 /* Return a numeric value representing the swidth specified by Lisp
1760 symbol WIDTH. Value is one of the enumerators of enum xlfd_swidth. */
1761
1762 static int
1763 face_numeric_swidth (width)
1764 Lisp_Object width;
1765 {
1766 return face_numeric_value (swidth_table, DIM (swidth_table), width);
1767 }
1768
1769
1770 #ifdef HAVE_X_WINDOWS
1771
1772 /* Return non-zero if FONT is the name of a fixed-pitch font. */
1773
1774 static INLINE int
1775 xlfd_fixed_p (font)
1776 struct font_name *font;
1777 {
1778 /* Function split_font_name converts fields to lower-case, so there
1779 is no need to use tolower here. */
1780 return *font->fields[XLFD_SPACING] != 'p';
1781 }
1782
1783
1784 /* Return the point size of FONT on frame F, measured in 1/10 pt.
1785
1786 The actual height of the font when displayed on F depends on the
1787 resolution of both the font and frame. For example, a 10pt font
1788 designed for a 100dpi display will display larger than 10pt on a
1789 75dpi display. (It's not unusual to use fonts not designed for the
1790 display one is using. For example, some intlfonts are available in
1791 72dpi versions, only.)
1792
1793 Value is the real point size of FONT on frame F, or 0 if it cannot
1794 be determined. */
1795
1796 static INLINE int
1797 xlfd_point_size (f, font)
1798 struct frame *f;
1799 struct font_name *font;
1800 {
1801 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
1802 double font_resy = atoi (font->fields[XLFD_RESY]);
1803 double font_pt = atoi (font->fields[XLFD_POINT_SIZE]);
1804 int real_pt;
1805
1806 if (font_resy == 0 || font_pt == 0)
1807 real_pt = 0;
1808 else
1809 real_pt = (font_resy / resy) * font_pt + 0.5;
1810
1811 return real_pt;
1812 }
1813
1814
1815 /* Split XLFD font name FONT->name destructively into NUL-terminated,
1816 lower-case fields in FONT->fields. NUMERIC_P non-zero means
1817 compute numeric values for fields XLFD_POINT_SIZE, XLFD_SWIDTH,
1818 XLFD_RESY, XLFD_SLANT, and XLFD_WEIGHT in FONT->numeric. Value is
1819 zero if the font name doesn't have the format we expect. The
1820 expected format is a font name that starts with a `-' and has
1821 XLFD_LAST fields separated by `-'. (The XLFD specification allows
1822 forms of font names where certain field contents are enclosed in
1823 square brackets. We don't support that, for now. */
1824
1825 static int
1826 split_font_name (f, font, numeric_p)
1827 struct frame *f;
1828 struct font_name *font;
1829 int numeric_p;
1830 {
1831 int i = 0;
1832 int success_p;
1833
1834 if (*font->name == '-')
1835 {
1836 char *p = xstrlwr (font->name) + 1;
1837
1838 while (i < XLFD_LAST)
1839 {
1840 font->fields[i] = p;
1841 ++i;
1842
1843 while (*p && *p != '-')
1844 ++p;
1845
1846 if (*p != '-')
1847 break;
1848
1849 *p++ = 0;
1850 }
1851 }
1852
1853 success_p = i == XLFD_LAST;
1854
1855 /* If requested, and font name was in the expected format,
1856 compute numeric values for some fields. */
1857 if (numeric_p && success_p)
1858 {
1859 font->numeric[XLFD_POINT_SIZE] = xlfd_point_size (f, font);
1860 font->numeric[XLFD_RESY] = atoi (font->fields[XLFD_RESY]);
1861 font->numeric[XLFD_SLANT] = xlfd_numeric_slant (font);
1862 font->numeric[XLFD_WEIGHT] = xlfd_numeric_weight (font);
1863 font->numeric[XLFD_SWIDTH] = xlfd_numeric_swidth (font);
1864 }
1865
1866 return success_p;
1867 }
1868
1869
1870 /* Build an XLFD font name from font name fields in FONT. Value is a
1871 pointer to the font name, which is allocated via xmalloc. */
1872
1873 static char *
1874 build_font_name (font)
1875 struct font_name *font;
1876 {
1877 int i;
1878 int size = 100;
1879 char *font_name = (char *) xmalloc (size);
1880 int total_length = 0;
1881
1882 for (i = 0; i < XLFD_LAST; ++i)
1883 {
1884 /* Add 1 because of the leading `-'. */
1885 int len = strlen (font->fields[i]) + 1;
1886
1887 /* Reallocate font_name if necessary. Add 1 for the final
1888 NUL-byte. */
1889 if (total_length + len + 1 >= size)
1890 {
1891 int new_size = max (2 * size, size + len + 1);
1892 int sz = new_size * sizeof *font_name;
1893 font_name = (char *) xrealloc (font_name, sz);
1894 size = new_size;
1895 }
1896
1897 font_name[total_length] = '-';
1898 bcopy (font->fields[i], font_name + total_length + 1, len - 1);
1899 total_length += len;
1900 }
1901
1902 font_name[total_length] = 0;
1903 return font_name;
1904 }
1905
1906
1907 /* Free an array FONTS of N font_name structures. This frees FONTS
1908 itself and all `name' fields in its elements. */
1909
1910 static INLINE void
1911 free_font_names (fonts, n)
1912 struct font_name *fonts;
1913 int n;
1914 {
1915 while (n)
1916 xfree (fonts[--n].name);
1917 xfree (fonts);
1918 }
1919
1920
1921 /* Sort vector FONTS of font_name structures which contains NFONTS
1922 elements using qsort and comparison function CMPFN. F is the frame
1923 on which the fonts will be used. The global variable font_frame
1924 is temporarily set to F to make it available in CMPFN. */
1925
1926 static INLINE void
1927 sort_fonts (f, fonts, nfonts, cmpfn)
1928 struct frame *f;
1929 struct font_name *fonts;
1930 int nfonts;
1931 int (*cmpfn) P_ ((const void *, const void *));
1932 {
1933 font_frame = f;
1934 qsort (fonts, nfonts, sizeof *fonts, cmpfn);
1935 font_frame = NULL;
1936 }
1937
1938
1939 /* Get fonts matching PATTERN on frame F. If F is null, use the first
1940 display in x_display_list. FONTS is a pointer to a vector of
1941 NFONTS font_name structures. TRY_ALTERNATIVES_P non-zero means try
1942 alternative patterns from Valternate_fontname_alist if no fonts are
1943 found matching PATTERN. SCALABLE_FONTS_P non-zero means include
1944 scalable fonts.
1945
1946 For all fonts found, set FONTS[i].name to the name of the font,
1947 allocated via xmalloc, and split font names into fields. Ignore
1948 fonts that we can't parse. Value is the number of fonts found.
1949
1950 This is similar to x_list_fonts. The differences are:
1951
1952 1. It avoids consing.
1953 2. It never calls XLoadQueryFont. */
1954
1955 static int
1956 x_face_list_fonts (f, pattern, fonts, nfonts, try_alternatives_p,
1957 scalable_fonts_p)
1958 struct frame *f;
1959 char *pattern;
1960 struct font_name *fonts;
1961 int nfonts, try_alternatives_p;
1962 int scalable_fonts_p;
1963 {
1964 Display *dpy = f ? FRAME_X_DISPLAY (f) : x_display_list->display;
1965 int n, i, j;
1966 char **names;
1967
1968 /* Get the list of fonts matching PATTERN from the X server. */
1969 BLOCK_INPUT;
1970 names = XListFonts (dpy, pattern, nfonts, &n);
1971 UNBLOCK_INPUT;
1972
1973 if (names)
1974 {
1975 /* Make a copy of the font names we got from X, and
1976 split them into fields. */
1977 for (i = j = 0; i < n; ++i)
1978 {
1979 /* Make a copy of the font name. */
1980 fonts[j].name = xstrdup (names[i]);
1981
1982 /* Ignore fonts having a name that we can't parse. */
1983 if (!split_font_name (f, fonts + j, 1))
1984 xfree (fonts[j].name);
1985 else if (font_scalable_p (fonts + j))
1986 {
1987 #if SCALABLE_FONTS
1988 if (!scalable_fonts_p
1989 || !may_use_scalable_font_p (fonts + j, names[i]))
1990 xfree (fonts[j].name);
1991 else
1992 ++j;
1993 #else /* !SCALABLE_FONTS */
1994 /* Always ignore scalable fonts. */
1995 xfree (fonts[j].name);
1996 #endif /* !SCALABLE_FONTS */
1997 }
1998 else
1999 ++j;
2000 }
2001
2002 n = j;
2003
2004 /* Free font names. */
2005 BLOCK_INPUT;
2006 XFreeFontNames (names);
2007 UNBLOCK_INPUT;
2008 }
2009
2010
2011 /* If no fonts found, try patterns from Valternate_fontname_alist. */
2012 if (n == 0 && try_alternatives_p)
2013 {
2014 Lisp_Object list = Valternate_fontname_alist;
2015
2016 while (CONSP (list))
2017 {
2018 Lisp_Object entry = XCAR (list);
2019 if (CONSP (entry)
2020 && STRINGP (XCAR (entry))
2021 && strcmp (XSTRING (XCAR (entry))->data, pattern) == 0)
2022 break;
2023 list = XCDR (list);
2024 }
2025
2026 if (CONSP (list))
2027 {
2028 Lisp_Object patterns = XCAR (list);
2029 Lisp_Object name;
2030
2031 while (CONSP (patterns)
2032 /* If list is screwed up, give up. */
2033 && (name = XCAR (patterns),
2034 STRINGP (name))
2035 /* Ignore patterns equal to PATTERN because we tried that
2036 already with no success. */
2037 && (strcmp (XSTRING (name)->data, pattern) == 0
2038 || (n = x_face_list_fonts (f, XSTRING (name)->data,
2039 fonts, nfonts, 0,
2040 scalable_fonts_p),
2041 n == 0)))
2042 patterns = XCDR (patterns);
2043 }
2044 }
2045
2046 return n;
2047 }
2048
2049
2050 /* Determine the first font matching PATTERN on frame F. Return in
2051 *FONT the matching font name, split into fields. Value is non-zero
2052 if a match was found. */
2053
2054 static int
2055 first_font_matching (f, pattern, font)
2056 struct frame *f;
2057 char *pattern;
2058 struct font_name *font;
2059 {
2060 int nfonts = 100;
2061 struct font_name *fonts;
2062
2063 fonts = (struct font_name *) xmalloc (nfonts * sizeof *fonts);
2064 nfonts = x_face_list_fonts (f, pattern, fonts, nfonts, 1, 0);
2065
2066 if (nfonts > 0)
2067 {
2068 bcopy (&fonts[0], font, sizeof *font);
2069
2070 fonts[0].name = NULL;
2071 free_font_names (fonts, nfonts);
2072 }
2073
2074 return nfonts > 0;
2075 }
2076
2077
2078 /* Determine fonts matching PATTERN on frame F. Sort resulting fonts
2079 using comparison function CMPFN. Value is the number of fonts
2080 found. If value is non-zero, *FONTS is set to a vector of
2081 font_name structures allocated from the heap containing matching
2082 fonts. Each element of *FONTS contains a name member that is also
2083 allocated from the heap. Font names in these structures are split
2084 into fields. Use free_font_names to free such an array. */
2085
2086 static int
2087 sorted_font_list (f, pattern, cmpfn, fonts)
2088 struct frame *f;
2089 char *pattern;
2090 int (*cmpfn) P_ ((const void *, const void *));
2091 struct font_name **fonts;
2092 {
2093 int nfonts;
2094
2095 /* Get the list of fonts matching pattern. 100 should suffice. */
2096 nfonts = 100;
2097 *fonts = (struct font_name *) xmalloc (nfonts * sizeof **fonts);
2098 #if SCALABLE_FONTS
2099 nfonts = x_face_list_fonts (f, pattern, *fonts, nfonts, 1, 1);
2100 #else
2101 nfonts = x_face_list_fonts (f, pattern, *fonts, nfonts, 1, 0);
2102 #endif
2103
2104 /* Sort the resulting array and return it in *FONTS. If no
2105 fonts were found, make sure to set *FONTS to null. */
2106 if (nfonts)
2107 sort_fonts (f, *fonts, nfonts, cmpfn);
2108 else
2109 {
2110 xfree (*fonts);
2111 *fonts = NULL;
2112 }
2113
2114 return nfonts;
2115 }
2116
2117
2118 /* Compare two font_name structures *A and *B. Value is analogous to
2119 strcmp. Sort order is given by the global variable
2120 font_sort_order. Font names are sorted so that, everything else
2121 being equal, fonts with a resolution closer to that of the frame on
2122 which they are used are listed first. The global variable
2123 font_frame is the frame on which we operate. */
2124
2125 static int
2126 cmp_font_names (a, b)
2127 const void *a, *b;
2128 {
2129 struct font_name *x = (struct font_name *) a;
2130 struct font_name *y = (struct font_name *) b;
2131 int cmp;
2132
2133 /* All strings have been converted to lower-case by split_font_name,
2134 so we can use strcmp here. */
2135 cmp = strcmp (x->fields[XLFD_FAMILY], y->fields[XLFD_FAMILY]);
2136 if (cmp == 0)
2137 {
2138 int i;
2139
2140 for (i = 0; i < DIM (font_sort_order) && cmp == 0; ++i)
2141 {
2142 int j = font_sort_order[i];
2143 cmp = x->numeric[j] - y->numeric[j];
2144 }
2145
2146 if (cmp == 0)
2147 {
2148 /* Everything else being equal, we prefer fonts with an
2149 y-resolution closer to that of the frame. */
2150 int resy = FRAME_X_DISPLAY_INFO (font_frame)->resy;
2151 int x_resy = x->numeric[XLFD_RESY];
2152 int y_resy = y->numeric[XLFD_RESY];
2153 cmp = abs (resy - x_resy) - abs (resy - y_resy);
2154 }
2155 }
2156
2157 return cmp;
2158 }
2159
2160
2161 /* Get a sorted list of fonts of family FAMILY on frame F. If PATTERN
2162 is non-null list fonts matching that pattern. Otherwise, if
2163 REGISTRY_AND_ENCODING is non-null return only fonts with that
2164 registry and encoding, otherwise return fonts of any registry and
2165 encoding. Set *FONTS to a vector of font_name structures allocated
2166 from the heap containing the fonts found. Value is the number of
2167 fonts found. */
2168
2169 static int
2170 font_list (f, pattern, family, registry_and_encoding, fonts)
2171 struct frame *f;
2172 char *pattern;
2173 char *family;
2174 char *registry_and_encoding;
2175 struct font_name **fonts;
2176 {
2177 if (pattern == NULL)
2178 {
2179 if (family == NULL)
2180 family = "*";
2181
2182 if (registry_and_encoding == NULL)
2183 registry_and_encoding = "*";
2184
2185 pattern = (char *) alloca (strlen (family)
2186 + strlen (registry_and_encoding)
2187 + 10);
2188 if (index (family, '-'))
2189 sprintf (pattern, "-%s-*-%s", family, registry_and_encoding);
2190 else
2191 sprintf (pattern, "-*-%s-*-%s", family, registry_and_encoding);
2192 }
2193
2194 return sorted_font_list (f, pattern, cmp_font_names, fonts);
2195 }
2196
2197
2198 /* Remove elements from LIST whose cars are `equal'. Called from
2199 x-font-list and x-font-family-list to remove duplicate font
2200 entries. */
2201
2202 static void
2203 remove_duplicates (list)
2204 Lisp_Object list;
2205 {
2206 Lisp_Object tail = list;
2207
2208 while (!NILP (tail) && !NILP (XCDR (tail)))
2209 {
2210 Lisp_Object next = XCDR (tail);
2211 if (!NILP (Fequal (XCAR (next), XCAR (tail))))
2212 XCDR (tail) = XCDR (next);
2213 else
2214 tail = XCDR (tail);
2215 }
2216 }
2217
2218
2219 DEFUN ("x-font-list", Fxfont_list, Sx_font_list, 0, 2, 0,
2220 "Return a list of available fonts of family FAMILY on FRAME.\n\
2221 If FAMILY is omitted or nil, list all families.\n\
2222 Otherwise, FAMILY must be a string, possibly containing wildcards\n\
2223 `?' and `*'.\n\
2224 If FRAME is omitted or nil, use the selected frame.\n\
2225 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT\n\
2226 SLANT FIXED-P].\n\
2227 FAMILY is the font family name. POINT-SIZE is the size of the\n\
2228 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the\n\
2229 width, weight and slant of the font. These symbols are the same as for\n\
2230 face attributes. FIXED-P is non-nil if the font is fixed-pitch.\n\
2231 The result list is sorted according to the current setting of\n\
2232 the face font sort order.")
2233 (family, frame)
2234 Lisp_Object family, frame;
2235 {
2236 struct frame *f = check_x_frame (frame);
2237 struct font_name *fonts;
2238 int i, nfonts;
2239 Lisp_Object result;
2240 struct gcpro gcpro1;
2241 char *family_pattern;
2242
2243 if (NILP (family))
2244 family_pattern = "*";
2245 else
2246 {
2247 CHECK_STRING (family, 1);
2248 family_pattern = LSTRDUPA (family);
2249 }
2250
2251 result = Qnil;
2252 GCPRO1 (result);
2253 nfonts = font_list (f, NULL, family_pattern, NULL, &fonts);
2254 for (i = nfonts - 1; i >= 0; --i)
2255 {
2256 Lisp_Object v = Fmake_vector (make_number (6), Qnil);
2257
2258 #define ASET(VECTOR, IDX, VAL) (XVECTOR (VECTOR)->contents[IDX] = (VAL))
2259
2260 ASET (v, 0, build_string (fonts[i].fields[XLFD_FAMILY]));
2261 ASET (v, 1, xlfd_symbolic_swidth (fonts + i));
2262 ASET (v, 2, make_number (xlfd_point_size (f, fonts + i)));
2263 ASET (v, 3, xlfd_symbolic_weight (fonts + i));
2264 ASET (v, 4, xlfd_symbolic_slant (fonts + i));
2265 ASET (v, 5, xlfd_fixed_p (fonts + i) ? Qt : Qnil);
2266 result = Fcons (v, result);
2267
2268 #undef ASET
2269 }
2270
2271 remove_duplicates (result);
2272 free_font_names (fonts, nfonts);
2273 UNGCPRO;
2274 return result;
2275 }
2276
2277
2278 DEFUN ("x-font-family-list", Fx_font_family_list, Sx_font_family_list,
2279 0, 1, 0,
2280 "Return a list of available font families on FRAME.\n\
2281 If FRAME is omitted or nil, use the selected frame.\n\
2282 Value is a list of conses (FAMILY . FIXED-P) where FAMILY\n\
2283 is a font family, and FIXED-P is non-nil if fonts of that family\n\
2284 are fixed-pitch.")
2285 (frame)
2286 Lisp_Object frame;
2287 {
2288 struct frame *f = check_x_frame (frame);
2289 int nfonts, i;
2290 struct font_name *fonts;
2291 Lisp_Object result;
2292 struct gcpro gcpro1;
2293
2294 nfonts = font_list (f, NULL, "*", NULL, &fonts);
2295 result = Qnil;
2296 GCPRO1 (result);
2297 for (i = nfonts - 1; i >= 0; --i)
2298 result = Fcons (Fcons (build_string (fonts[i].fields[XLFD_FAMILY]),
2299 xlfd_fixed_p (fonts + i) ? Qt : Qnil),
2300 result);
2301
2302 remove_duplicates (result);
2303 free_font_names (fonts, nfonts);
2304 UNGCPRO;
2305 return result;
2306 }
2307
2308
2309 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
2310 "Return a list of the names of available fonts matching PATTERN.\n\
2311 If optional arguments FACE and FRAME are specified, return only fonts\n\
2312 the same size as FACE on FRAME.\n\
2313 PATTERN is a string, perhaps with wildcard characters;\n\
2314 the * character matches any substring, and\n\
2315 the ? character matches any single character.\n\
2316 PATTERN is case-insensitive.\n\
2317 FACE is a face name--a symbol.\n\
2318 \n\
2319 The return value is a list of strings, suitable as arguments to\n\
2320 set-face-font.\n\
2321 \n\
2322 Fonts Emacs can't use may or may not be excluded\n\
2323 even if they match PATTERN and FACE.\n\
2324 The optional fourth argument MAXIMUM sets a limit on how many\n\
2325 fonts to match. The first MAXIMUM fonts are reported.\n\
2326 The optional fifth argument WIDTH, if specified, is a number of columns\n\
2327 occupied by a character of a font. In that case, return only fonts\n\
2328 the WIDTH times as wide as FACE on FRAME.")
2329 (pattern, face, frame, maximum, width)
2330 Lisp_Object pattern, face, frame, maximum, width;
2331 {
2332 struct frame *f;
2333 int size;
2334 int maxnames;
2335
2336 check_x ();
2337 CHECK_STRING (pattern, 0);
2338
2339 if (NILP (maximum))
2340 maxnames = 2000;
2341 else
2342 {
2343 CHECK_NATNUM (maximum, 0);
2344 maxnames = XINT (maximum);
2345 }
2346
2347 if (!NILP (width))
2348 CHECK_NUMBER (width, 4);
2349
2350 /* We can't simply call check_x_frame because this function may be
2351 called before any frame is created. */
2352 f = frame_or_selected_frame (frame, 2);
2353 if (!FRAME_X_P (f))
2354 {
2355 /* Perhaps we have not yet created any frame. */
2356 f = NULL;
2357 face = Qnil;
2358 }
2359
2360 /* Determine the width standard for comparison with the fonts we find. */
2361
2362 if (NILP (face))
2363 size = 0;
2364 else
2365 {
2366 /* This is of limited utility since it works with character
2367 widths. Keep it for compatibility. --gerd. */
2368 int face_id = lookup_named_face (f, face, CHARSET_ASCII);
2369 struct face *face = FACE_FROM_ID (f, face_id);
2370
2371 if (face->font)
2372 size = face->font->max_bounds.width;
2373 else
2374 size = FRAME_FONT (f)->max_bounds.width;
2375
2376 if (!NILP (width))
2377 size *= XINT (width);
2378 }
2379
2380 {
2381 Lisp_Object args[2];
2382
2383 args[0] = x_list_fonts (f, pattern, size, maxnames);
2384 if (f == NULL)
2385 /* We don't have to check fontsets. */
2386 return args[0];
2387 args[1] = list_fontsets (f, pattern, size);
2388 return Fnconc (2, args);
2389 }
2390 }
2391
2392 #endif /* HAVE_X_WINDOWS */
2393
2394
2395 \f
2396 /***********************************************************************
2397 Lisp Faces
2398 ***********************************************************************/
2399
2400 /* Access face attributes of face FACE, a Lisp vector. */
2401
2402 #define LFACE_FAMILY(LFACE) \
2403 XVECTOR (LFACE)->contents[LFACE_FAMILY_INDEX]
2404 #define LFACE_HEIGHT(LFACE) \
2405 XVECTOR (LFACE)->contents[LFACE_HEIGHT_INDEX]
2406 #define LFACE_WEIGHT(LFACE) \
2407 XVECTOR (LFACE)->contents[LFACE_WEIGHT_INDEX]
2408 #define LFACE_SLANT(LFACE) \
2409 XVECTOR (LFACE)->contents[LFACE_SLANT_INDEX]
2410 #define LFACE_UNDERLINE(LFACE) \
2411 XVECTOR (LFACE)->contents[LFACE_UNDERLINE_INDEX]
2412 #define LFACE_INVERSE(LFACE) \
2413 XVECTOR (LFACE)->contents[LFACE_INVERSE_INDEX]
2414 #define LFACE_FOREGROUND(LFACE) \
2415 XVECTOR (LFACE)->contents[LFACE_FOREGROUND_INDEX]
2416 #define LFACE_BACKGROUND(LFACE) \
2417 XVECTOR (LFACE)->contents[LFACE_BACKGROUND_INDEX]
2418 #define LFACE_STIPPLE(LFACE) \
2419 XVECTOR (LFACE)->contents[LFACE_STIPPLE_INDEX]
2420 #define LFACE_SWIDTH(LFACE) \
2421 XVECTOR (LFACE)->contents[LFACE_SWIDTH_INDEX]
2422 #define LFACE_OVERLINE(LFACE) \
2423 XVECTOR (LFACE)->contents[LFACE_OVERLINE_INDEX]
2424 #define LFACE_STRIKE_THROUGH(LFACE) \
2425 XVECTOR (LFACE)->contents[LFACE_STRIKE_THROUGH_INDEX]
2426 #define LFACE_BOX(LFACE) \
2427 XVECTOR (LFACE)->contents[LFACE_BOX_INDEX]
2428
2429 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
2430 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
2431
2432 #define LFACEP(LFACE) \
2433 (VECTORP (LFACE) \
2434 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
2435 && EQ (XVECTOR (LFACE)->contents[0], Qface))
2436
2437
2438 #if GLYPH_DEBUG
2439
2440 /* Check consistency of Lisp face attribute vector ATTRS. */
2441
2442 static void
2443 check_lface_attrs (attrs)
2444 Lisp_Object *attrs;
2445 {
2446 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
2447 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
2448 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
2449 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
2450 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
2451 || INTEGERP (attrs[LFACE_HEIGHT_INDEX]));
2452 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
2453 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
2454 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
2455 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
2456 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
2457 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
2458 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
2459 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
2460 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
2461 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
2462 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2463 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2464 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
2465 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
2466 || SYMBOLP (attrs[LFACE_BOX_INDEX])
2467 || STRINGP (attrs[LFACE_BOX_INDEX])
2468 || INTEGERP (attrs[LFACE_BOX_INDEX])
2469 || CONSP (attrs[LFACE_BOX_INDEX]));
2470 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
2471 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
2472 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
2473 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
2474 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
2475 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
2476 #ifdef HAVE_WINDOW_SYSTEM
2477 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
2478 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
2479 || !NILP (Fpixmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
2480 #endif
2481 }
2482
2483
2484 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
2485
2486 static void
2487 check_lface (lface)
2488 Lisp_Object lface;
2489 {
2490 if (!NILP (lface))
2491 {
2492 xassert (LFACEP (lface));
2493 check_lface_attrs (XVECTOR (lface)->contents);
2494 }
2495 }
2496
2497 #else /* GLYPH_DEBUG == 0 */
2498
2499 #define check_lface_attrs(attrs) (void) 0
2500 #define check_lface(lface) (void) 0
2501
2502 #endif /* GLYPH_DEBUG == 0 */
2503
2504
2505 /* Return the face definition of FACE_NAME on frame F. F null means
2506 return the global definition. FACE_NAME may be a string or a
2507 symbol (apparently Emacs 20.2 allows strings as face names in face
2508 text properties; ediff uses that). If SIGNAL_P is non-zero, signal
2509 an error if FACE_NAME is not a valid face name. If SIGNAL_P is
2510 zero, value is nil if FACE_NAME is not a valid face name. */
2511
2512 static INLINE Lisp_Object
2513 lface_from_face_name (f, face_name, signal_p)
2514 struct frame *f;
2515 Lisp_Object face_name;
2516 int signal_p;
2517 {
2518 Lisp_Object lface;
2519
2520 if (STRINGP (face_name))
2521 face_name = intern (XSTRING (face_name)->data);
2522
2523 if (f)
2524 lface = assq_no_quit (face_name, f->face_alist);
2525 else
2526 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2527
2528 if (CONSP (lface))
2529 lface = XCDR (lface);
2530 else if (signal_p)
2531 signal_error ("Invalid face", face_name);
2532
2533 check_lface (lface);
2534 return lface;
2535 }
2536
2537
2538 /* Get face attributes of face FACE_NAME from frame-local faces on
2539 frame F. Store the resulting attributes in ATTRS which must point
2540 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2541 is non-zero, signal an error if FACE_NAME does not name a face.
2542 Otherwise, value is zero if FACE_NAME is not a face. */
2543
2544 static INLINE int
2545 get_lface_attributes (f, face_name, attrs, signal_p)
2546 struct frame *f;
2547 Lisp_Object face_name;
2548 Lisp_Object *attrs;
2549 int signal_p;
2550 {
2551 Lisp_Object lface;
2552 int success_p;
2553
2554 lface = lface_from_face_name (f, face_name, signal_p);
2555 if (!NILP (lface))
2556 {
2557 bcopy (XVECTOR (lface)->contents, attrs,
2558 LFACE_VECTOR_SIZE * sizeof *attrs);
2559 success_p = 1;
2560 }
2561 else
2562 success_p = 0;
2563
2564 return success_p;
2565 }
2566
2567
2568 /* Non-zero if all attributes in face attribute vector ATTRS are
2569 specified, i.e. are non-nil. */
2570
2571 static int
2572 lface_fully_specified_p (attrs)
2573 Lisp_Object *attrs;
2574 {
2575 int i;
2576
2577 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2578 if (UNSPECIFIEDP (attrs[i]))
2579 break;
2580
2581 return i == LFACE_VECTOR_SIZE;
2582 }
2583
2584
2585 #ifdef HAVE_X_WINDOWS
2586
2587 /* Set font-related attributes of Lisp face LFACE from XLFD font name
2588 FONT_NAME. If FORCE_P is zero, set only unspecified attributes of
2589 LFACE. Ignore fields of FONT_NAME containing wildcards. Value is
2590 zero if not successful because FONT_NAME was not in a valid format.
2591 A valid format is one that is suitable for split_font_name, see the
2592 comment there. */
2593
2594 static int
2595 set_lface_from_font_name (f, lface, font_name, force_p)
2596 struct frame *f;
2597 Lisp_Object lface;
2598 char *font_name;
2599 int force_p;
2600 {
2601 struct font_name font;
2602 char *buffer;
2603 int pt;
2604 int free_font_name_p = 0;
2605
2606 /* If FONT_NAME contains wildcards, use the first matching font. */
2607 if (index (font_name, '*') || index (font_name, '?'))
2608 {
2609 if (!first_font_matching (f, font_name, &font))
2610 return 0;
2611 free_font_name_p = 1;
2612 }
2613 else
2614 {
2615 font.name = STRDUPA (font_name);
2616 if (!split_font_name (f, &font, 1))
2617 {
2618 /* The font name may be something like `6x13'. Make
2619 sure we use the full name. */
2620 struct font_info *font_info;
2621
2622 BLOCK_INPUT;
2623 font_info = fs_load_font (f, FRAME_X_FONT_TABLE (f),
2624 CHARSET_ASCII, font_name, -1);
2625 UNBLOCK_INPUT;
2626
2627 if (!font_info)
2628 return 0;
2629
2630 font.name = STRDUPA (font_info->full_name);
2631 split_font_name (f, &font, 1);
2632 }
2633
2634 /* FONT_NAME should not be a fontset name, here. */
2635 xassert (xstricmp (font.fields[XLFD_REGISTRY], "fontset") != 0);
2636 }
2637
2638 /* Set attributes only if unspecified, otherwise face defaults for
2639 new frames would never take effect. */
2640
2641 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2642 {
2643 buffer = (char *) alloca (strlen (font.fields[XLFD_FAMILY])
2644 + strlen (font.fields[XLFD_FOUNDRY])
2645 + 2);
2646 sprintf (buffer, "%s-%s", font.fields[XLFD_FOUNDRY],
2647 font.fields[XLFD_FAMILY]);
2648 LFACE_FAMILY (lface) = build_string (buffer);
2649 }
2650
2651 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2652 {
2653 pt = xlfd_point_size (f, &font);
2654 xassert (pt > 0);
2655 LFACE_HEIGHT (lface) = make_number (pt);
2656 }
2657
2658 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2659 LFACE_SWIDTH (lface) = xlfd_symbolic_swidth (&font);
2660
2661 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2662 LFACE_WEIGHT (lface) = xlfd_symbolic_weight (&font);
2663
2664 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2665 LFACE_SLANT (lface) = xlfd_symbolic_slant (&font);
2666
2667 if (free_font_name_p)
2668 xfree (font.name);
2669
2670 return 1;
2671 }
2672
2673 #endif /* HAVE_X_WINDOWS */
2674
2675
2676 /* Merge two Lisp face attribute vectors FROM and TO and store the
2677 resulting attributes in TO. Every non-nil attribute of FROM
2678 overrides the corresponding attribute of TO. */
2679
2680 static INLINE void
2681 merge_face_vectors (from, to)
2682 Lisp_Object *from, *to;
2683 {
2684 int i;
2685 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2686 if (!UNSPECIFIEDP (from[i]))
2687 to[i] = from[i];
2688 }
2689
2690
2691 /* Given a Lisp face attribute vector TO and a Lisp object PROP that
2692 is a face property, determine the resulting face attributes on
2693 frame F, and store them in TO. PROP may be a single face
2694 specification or a list of such specifications. Each face
2695 specification can be
2696
2697 1. A symbol or string naming a Lisp face.
2698
2699 2. A property list of the form (KEYWORD VALUE ...) where each
2700 KEYWORD is a face attribute name, and value is an appropriate value
2701 for that attribute.
2702
2703 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2704 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2705 for compatibility with 20.2.
2706
2707 Face specifications earlier in lists take precedence over later
2708 specifications. */
2709
2710 static void
2711 merge_face_vector_with_property (f, to, prop)
2712 struct frame *f;
2713 Lisp_Object *to;
2714 Lisp_Object prop;
2715 {
2716 if (CONSP (prop))
2717 {
2718 Lisp_Object first = XCAR (prop);
2719
2720 if (EQ (first, Qforeground_color)
2721 || EQ (first, Qbackground_color))
2722 {
2723 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2724 . COLOR). COLOR must be a string. */
2725 Lisp_Object color_name = XCDR (prop);
2726 Lisp_Object color = first;
2727
2728 if (STRINGP (color_name))
2729 {
2730 if (EQ (color, Qforeground_color))
2731 to[LFACE_FOREGROUND_INDEX] = color_name;
2732 else
2733 to[LFACE_BACKGROUND_INDEX] = color_name;
2734 }
2735 else
2736 display_message (f, "Invalid face color", color_name, Qnil);
2737 }
2738 else if (SYMBOLP (first)
2739 && *XSYMBOL (first)->name->data == ':')
2740 {
2741 /* Assume this is the property list form. */
2742 while (CONSP (prop) && CONSP (XCDR (prop)))
2743 {
2744 Lisp_Object keyword = XCAR (prop);
2745 Lisp_Object value = XCAR (XCDR (prop));
2746
2747 if (EQ (keyword, QCfamily))
2748 {
2749 if (STRINGP (value))
2750 to[LFACE_FAMILY_INDEX] = value;
2751 else
2752 display_message (f, "Illegal face font family",
2753 value, Qnil);
2754 }
2755 else if (EQ (keyword, QCheight))
2756 {
2757 if (INTEGERP (value))
2758 to[LFACE_HEIGHT_INDEX] = value;
2759 else
2760 display_message (f, "Illegal face font height",
2761 value, Qnil);
2762 }
2763 else if (EQ (keyword, QCweight))
2764 {
2765 if (SYMBOLP (value)
2766 && face_numeric_weight (value) >= 0)
2767 to[LFACE_WEIGHT_INDEX] = value;
2768 else
2769 display_message (f, "Illegal face weight", value, Qnil);
2770 }
2771 else if (EQ (keyword, QCslant))
2772 {
2773 if (SYMBOLP (value)
2774 && face_numeric_slant (value) >= 0)
2775 to[LFACE_SLANT_INDEX] = value;
2776 else
2777 display_message (f, "Illegal face slant", value, Qnil);
2778 }
2779 else if (EQ (keyword, QCunderline))
2780 {
2781 if (EQ (value, Qt)
2782 || NILP (value)
2783 || STRINGP (value))
2784 to[LFACE_UNDERLINE_INDEX] = value;
2785 else
2786 display_message (f, "Illegal face underline", value, Qnil);
2787 }
2788 else if (EQ (keyword, QCoverline))
2789 {
2790 if (EQ (value, Qt)
2791 || NILP (value)
2792 || STRINGP (value))
2793 to[LFACE_OVERLINE_INDEX] = value;
2794 else
2795 display_message (f, "Illegal face overline", value, Qnil);
2796 }
2797 else if (EQ (keyword, QCstrike_through))
2798 {
2799 if (EQ (value, Qt)
2800 || NILP (value)
2801 || STRINGP (value))
2802 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2803 else
2804 display_message (f, "Illegal face strike-through",
2805 value, Qnil);
2806 }
2807 else if (EQ (keyword, QCbox))
2808 {
2809 if (EQ (value, Qt))
2810 value = make_number (1);
2811 if (INTEGERP (value)
2812 || STRINGP (value)
2813 || CONSP (value)
2814 || NILP (value))
2815 to[LFACE_BOX_INDEX] = value;
2816 else
2817 display_message (f, "Illegal face box", value, Qnil);
2818 }
2819 else if (EQ (keyword, QCinverse_video)
2820 || EQ (keyword, QCreverse_video))
2821 {
2822 if (EQ (value, Qt) || NILP (value))
2823 to[LFACE_INVERSE_INDEX] = value;
2824 else
2825 display_message (f, "Illegal face inverse-video",
2826 value, Qnil);
2827 }
2828 else if (EQ (keyword, QCforeground))
2829 {
2830 if (STRINGP (value))
2831 to[LFACE_FOREGROUND_INDEX] = value;
2832 else
2833 display_message (f, "Illegal face foreground",
2834 value, Qnil);
2835 }
2836 else if (EQ (keyword, QCbackground))
2837 {
2838 if (STRINGP (value))
2839 to[LFACE_BACKGROUND_INDEX] = value;
2840 else
2841 display_message (f, "Illegal face background",
2842 value, Qnil);
2843 }
2844 else if (EQ (keyword, QCstipple))
2845 {
2846 #ifdef HAVE_X_WINDOWS
2847 Lisp_Object pixmap_p = Fpixmap_spec_p (value);
2848 if (!NILP (pixmap_p))
2849 to[LFACE_STIPPLE_INDEX] = value;
2850 else
2851 display_message (f, "Illegal face stipple", value, Qnil);
2852 #endif
2853 }
2854 else if (EQ (keyword, QCwidth))
2855 {
2856 if (SYMBOLP (value)
2857 && face_numeric_swidth (value) >= 0)
2858 to[LFACE_SWIDTH_INDEX] = value;
2859 else
2860 display_message (f, "Illegal face width", value, Qnil);
2861 }
2862 else
2863 display_message (f, "Invalid attribute %s in face property",
2864 keyword, Qnil);
2865
2866 prop = XCDR (XCDR (prop));
2867 }
2868 }
2869 else
2870 {
2871 /* This is a list of face specs. Specifications at the
2872 beginning of the list take precedence over later
2873 specifications, so we have to merge starting with the
2874 last specification. */
2875 Lisp_Object next = XCDR (prop);
2876 if (!NILP (next))
2877 merge_face_vector_with_property (f, to, next);
2878 merge_face_vector_with_property (f, to, first);
2879 }
2880 }
2881 else
2882 {
2883 /* PROP ought to be a face name. */
2884 Lisp_Object lface = lface_from_face_name (f, prop, 0);
2885 if (NILP (lface))
2886 display_message (f, "Invalid face text property value: %s",
2887 prop, Qnil);
2888 else
2889 merge_face_vectors (XVECTOR (lface)->contents, to);
2890 }
2891 }
2892
2893
2894 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2895 Sinternal_make_lisp_face, 1, 2, 0,
2896 "Make FACE, a symbol, a Lisp face with all attributes nil.\n\
2897 If FACE was not known as a face before, create a new one.\n\
2898 If optional argument FRAME is specified, make a frame-local face\n\
2899 for that frame. Otherwise operate on the global face definition.\n\
2900 Value is a vector of face attributes.")
2901 (face, frame)
2902 Lisp_Object face, frame;
2903 {
2904 Lisp_Object global_lface, lface;
2905 struct frame *f;
2906 int i;
2907
2908 CHECK_SYMBOL (face, 0);
2909 global_lface = lface_from_face_name (NULL, face, 0);
2910
2911 if (!NILP (frame))
2912 {
2913 CHECK_LIVE_FRAME (frame, 1);
2914 f = XFRAME (frame);
2915 lface = lface_from_face_name (f, face, 0);
2916 }
2917 else
2918 f = NULL, lface = Qnil;
2919
2920 /* Add a global definition if there is none. */
2921 if (NILP (global_lface))
2922 {
2923 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2924 Qunspecified);
2925 XVECTOR (global_lface)->contents[0] = Qface;
2926 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2927 Vface_new_frame_defaults);
2928
2929 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2930 face id to Lisp face is given by the vector lface_id_to_name.
2931 The mapping from Lisp face to Lisp face id is given by the
2932 property `face' of the Lisp face name. */
2933 if (next_lface_id == lface_id_to_name_size)
2934 {
2935 int new_size = max (50, 2 * lface_id_to_name_size);
2936 int sz = new_size * sizeof *lface_id_to_name;
2937 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
2938 lface_id_to_name_size = new_size;
2939 }
2940
2941 lface_id_to_name[next_lface_id] = face;
2942 Fput (face, Qface, make_number (next_lface_id));
2943 ++next_lface_id;
2944 }
2945 else if (f == NULL)
2946 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2947 XVECTOR (global_lface)->contents[i] = Qunspecified;
2948
2949 /* Add a frame-local definition. */
2950 if (f)
2951 {
2952 if (NILP (lface))
2953 {
2954 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2955 Qunspecified);
2956 XVECTOR (lface)->contents[0] = Qface;
2957 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
2958 }
2959 else
2960 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2961 XVECTOR (lface)->contents[i] = Qunspecified;
2962 }
2963 else
2964 lface = global_lface;
2965
2966 xassert (LFACEP (lface));
2967 check_lface (lface);
2968 return lface;
2969 }
2970
2971
2972 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2973 Sinternal_lisp_face_p, 1, 2, 0,
2974 "Return non-nil if FACE names a face.\n\
2975 If optional second parameter FRAME is non-nil, check for the\n\
2976 existence of a frame-local face with name FACE on that frame.\n\
2977 Otherwise check for the existence of a global face.")
2978 (face, frame)
2979 Lisp_Object face, frame;
2980 {
2981 Lisp_Object lface;
2982
2983 if (!NILP (frame))
2984 {
2985 CHECK_LIVE_FRAME (frame, 1);
2986 lface = lface_from_face_name (XFRAME (frame), face, 0);
2987 }
2988 else
2989 lface = lface_from_face_name (NULL, face, 0);
2990
2991 return lface;
2992 }
2993
2994
2995 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2996 Sinternal_copy_lisp_face, 4, 4, 0,
2997 "Copy face FROM to TO.\n\
2998 If FRAME it t, copy the global face definition of FROM to the\n\
2999 global face definition of TO. Otherwise, copy the frame-local\n\
3000 definition of FROM on FRAME to the frame-local definition of TO\n\
3001 on NEW-FRAME, or FRAME if NEW-FRAME is nil.\n\
3002 \n\
3003 Value is TO.")
3004 (from, to, frame, new_frame)
3005 Lisp_Object from, to, frame, new_frame;
3006 {
3007 Lisp_Object lface, copy;
3008
3009 CHECK_SYMBOL (from, 0);
3010 CHECK_SYMBOL (to, 1);
3011 if (NILP (new_frame))
3012 new_frame = frame;
3013
3014 if (EQ (frame, Qt))
3015 {
3016 /* Copy global definition of FROM. We don't make copies of
3017 strings etc. because 20.2 didn't do it either. */
3018 lface = lface_from_face_name (NULL, from, 1);
3019 copy = Finternal_make_lisp_face (to, Qnil);
3020 }
3021 else
3022 {
3023 /* Copy frame-local definition of FROM. */
3024 CHECK_LIVE_FRAME (frame, 2);
3025 CHECK_LIVE_FRAME (new_frame, 3);
3026 lface = lface_from_face_name (XFRAME (frame), from, 1);
3027 copy = Finternal_make_lisp_face (to, new_frame);
3028 }
3029
3030 bcopy (XVECTOR (lface)->contents, XVECTOR (copy)->contents,
3031 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
3032
3033 return to;
3034 }
3035
3036
3037 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
3038 Sinternal_set_lisp_face_attribute, 3, 4, 0,
3039 "Set attribute ATTR of FACE to VALUE.\n\
3040 If optional argument FRAME is given, set the face attribute of face FACE\n\
3041 on that frame. If FRAME is t, set the attribute of the default for face\n\
3042 FACE (for new frames). If FRAME is omitted or nil, use the selected\n\
3043 frame.")
3044 (face, attr, value, frame)
3045 Lisp_Object face, attr, value, frame;
3046 {
3047 Lisp_Object lface;
3048 Lisp_Object old_value = Qnil;
3049 int font_related_attr_p = 0;
3050
3051 CHECK_SYMBOL (face, 0);
3052 CHECK_SYMBOL (attr, 1);
3053
3054 /* Set lface to the Lisp attribute vector of FACE. */
3055 if (EQ (frame, Qt))
3056 lface = lface_from_face_name (NULL, face, 1);
3057 else
3058 {
3059 if (NILP (frame))
3060 XSETFRAME (frame, selected_frame);
3061
3062 CHECK_LIVE_FRAME (frame, 3);
3063 lface = lface_from_face_name (XFRAME (frame), face, 0);
3064
3065 /* If a frame-local face doesn't exist yet, create one. */
3066 if (NILP (lface))
3067 lface = Finternal_make_lisp_face (face, frame);
3068 }
3069
3070 if (EQ (attr, QCfamily))
3071 {
3072 if (!UNSPECIFIEDP (value))
3073 {
3074 CHECK_STRING (value, 3);
3075 if (XSTRING (value)->size == 0)
3076 signal_error ("Invalid face family", value);
3077 }
3078 old_value = LFACE_FAMILY (lface);
3079 LFACE_FAMILY (lface) = value;
3080 font_related_attr_p = 1;
3081 }
3082 else if (EQ (attr, QCheight))
3083 {
3084 if (!UNSPECIFIEDP (value))
3085 {
3086 CHECK_NUMBER (value, 3);
3087 if (XINT (value) <= 0)
3088 signal_error ("Invalid face height", value);
3089 }
3090 old_value = LFACE_HEIGHT (lface);
3091 LFACE_HEIGHT (lface) = value;
3092 font_related_attr_p = 1;
3093 }
3094 else if (EQ (attr, QCweight))
3095 {
3096 if (!UNSPECIFIEDP (value))
3097 {
3098 CHECK_SYMBOL (value, 3);
3099 if (face_numeric_weight (value) < 0)
3100 signal_error ("Invalid face weight", value);
3101 }
3102 old_value = LFACE_WEIGHT (lface);
3103 LFACE_WEIGHT (lface) = value;
3104 font_related_attr_p = 1;
3105 }
3106 else if (EQ (attr, QCslant))
3107 {
3108 if (!UNSPECIFIEDP (value))
3109 {
3110 CHECK_SYMBOL (value, 3);
3111 if (face_numeric_slant (value) < 0)
3112 signal_error ("Invalid face slant", value);
3113 }
3114 old_value = LFACE_SLANT (lface);
3115 LFACE_SLANT (lface) = value;
3116 font_related_attr_p = 1;
3117 }
3118 else if (EQ (attr, QCunderline))
3119 {
3120 if (!UNSPECIFIEDP (value))
3121 if ((SYMBOLP (value)
3122 && !EQ (value, Qt)
3123 && !EQ (value, Qnil))
3124 /* Underline color. */
3125 || (STRINGP (value)
3126 && XSTRING (value)->size == 0))
3127 signal_error ("Invalid face underline", value);
3128
3129 old_value = LFACE_UNDERLINE (lface);
3130 LFACE_UNDERLINE (lface) = value;
3131 }
3132 else if (EQ (attr, QCoverline))
3133 {
3134 if (!UNSPECIFIEDP (value))
3135 if ((SYMBOLP (value)
3136 && !EQ (value, Qt)
3137 && !EQ (value, Qnil))
3138 /* Overline color. */
3139 || (STRINGP (value)
3140 && XSTRING (value)->size == 0))
3141 signal_error ("Invalid face overline", value);
3142
3143 old_value = LFACE_OVERLINE (lface);
3144 LFACE_OVERLINE (lface) = value;
3145 }
3146 else if (EQ (attr, QCstrike_through))
3147 {
3148 if (!UNSPECIFIEDP (value))
3149 if ((SYMBOLP (value)
3150 && !EQ (value, Qt)
3151 && !EQ (value, Qnil))
3152 /* Strike-through color. */
3153 || (STRINGP (value)
3154 && XSTRING (value)->size == 0))
3155 signal_error ("Invalid face strike-through", value);
3156
3157 old_value = LFACE_STRIKE_THROUGH (lface);
3158 LFACE_STRIKE_THROUGH (lface) = value;
3159 }
3160 else if (EQ (attr, QCbox))
3161 {
3162 int valid_p;
3163
3164 /* Allow t meaning a simple box of width 1 in foreground color
3165 of the face. */
3166 if (EQ (value, Qt))
3167 value = make_number (1);
3168
3169 if (UNSPECIFIEDP (value))
3170 valid_p = 1;
3171 else if (NILP (value))
3172 valid_p = 1;
3173 else if (INTEGERP (value))
3174 valid_p = XINT (value) > 0;
3175 else if (STRINGP (value))
3176 valid_p = XSTRING (value)->size > 0;
3177 else if (CONSP (value))
3178 {
3179 Lisp_Object tem;
3180
3181 tem = value;
3182 while (CONSP (tem))
3183 {
3184 Lisp_Object k, v;
3185
3186 k = XCAR (tem);
3187 tem = XCDR (tem);
3188 if (!CONSP (tem))
3189 break;
3190 v = XCAR (tem);
3191 tem = XCDR (tem);
3192
3193 if (EQ (k, QCline_width))
3194 {
3195 if (!INTEGERP (v) || XINT (v) <= 0)
3196 break;
3197 }
3198 else if (EQ (k, QCcolor))
3199 {
3200 if (!STRINGP (v) || XSTRING (v)->size == 0)
3201 break;
3202 }
3203 else if (EQ (k, QCstyle))
3204 {
3205 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3206 break;
3207 }
3208 else
3209 break;
3210 }
3211
3212 valid_p = NILP (tem);
3213 }
3214 else
3215 valid_p = 0;
3216
3217 if (!valid_p)
3218 signal_error ("Invalid face box", value);
3219
3220 old_value = LFACE_BOX (lface);
3221 LFACE_BOX (lface) = value;
3222 }
3223 else if (EQ (attr, QCinverse_video)
3224 || EQ (attr, QCreverse_video))
3225 {
3226 if (!UNSPECIFIEDP (value))
3227 {
3228 CHECK_SYMBOL (value, 3);
3229 if (!EQ (value, Qt) && !NILP (value))
3230 signal_error ("Invalid inverse-video face attribute value", value);
3231 }
3232 old_value = LFACE_INVERSE (lface);
3233 LFACE_INVERSE (lface) = value;
3234 }
3235 else if (EQ (attr, QCforeground))
3236 {
3237 if (!UNSPECIFIEDP (value))
3238 {
3239 /* Don't check for valid color names here because it depends
3240 on the frame (display) whether the color will be valid
3241 when the face is realized. */
3242 CHECK_STRING (value, 3);
3243 if (XSTRING (value)->size == 0)
3244 signal_error ("Empty foreground color value", value);
3245 }
3246 old_value = LFACE_FOREGROUND (lface);
3247 LFACE_FOREGROUND (lface) = value;
3248 }
3249 else if (EQ (attr, QCbackground))
3250 {
3251 if (!UNSPECIFIEDP (value))
3252 {
3253 /* Don't check for valid color names here because it depends
3254 on the frame (display) whether the color will be valid
3255 when the face is realized. */
3256 CHECK_STRING (value, 3);
3257 if (XSTRING (value)->size == 0)
3258 signal_error ("Empty background color value", value);
3259 }
3260 old_value = LFACE_BACKGROUND (lface);
3261 LFACE_BACKGROUND (lface) = value;
3262 }
3263 else if (EQ (attr, QCstipple))
3264 {
3265 #ifdef HAVE_X_WINDOWS
3266 if (!UNSPECIFIEDP (value)
3267 && !NILP (value)
3268 && NILP (Fpixmap_spec_p (value)))
3269 signal_error ("Invalid stipple attribute", value);
3270 old_value = LFACE_STIPPLE (lface);
3271 LFACE_STIPPLE (lface) = value;
3272 #endif /* HAVE_X_WINDOWS */
3273 }
3274 else if (EQ (attr, QCwidth))
3275 {
3276 if (!UNSPECIFIEDP (value))
3277 {
3278 CHECK_SYMBOL (value, 3);
3279 if (face_numeric_swidth (value) < 0)
3280 signal_error ("Invalid face width", value);
3281 }
3282 old_value = LFACE_SWIDTH (lface);
3283 LFACE_SWIDTH (lface) = value;
3284 font_related_attr_p = 1;
3285 }
3286 else if (EQ (attr, QCfont))
3287 {
3288 #ifdef HAVE_X_WINDOWS
3289 /* Set font-related attributes of the Lisp face from an
3290 XLFD font name. */
3291 struct frame *f;
3292
3293 CHECK_STRING (value, 3);
3294 if (EQ (frame, Qt))
3295 f = selected_frame;
3296 else
3297 f = check_x_frame (frame);
3298
3299 if (!set_lface_from_font_name (f, lface, XSTRING (value)->data, 1))
3300 signal_error ("Invalid font name", value);
3301
3302 font_related_attr_p = 1;
3303 #endif /* HAVE_X_WINDOWS */
3304 }
3305 else if (EQ (attr, QCbold))
3306 {
3307 old_value = LFACE_WEIGHT (lface);
3308 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
3309 font_related_attr_p = 1;
3310 }
3311 else if (EQ (attr, QCitalic))
3312 {
3313 old_value = LFACE_SLANT (lface);
3314 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
3315 font_related_attr_p = 1;
3316 }
3317 else
3318 signal_error ("Invalid face attribute name", attr);
3319
3320 /* Changing a named face means that all realized faces depending on
3321 that face are invalid. Since we cannot tell which realized faces
3322 depend on the face, make sure they are all removed. This is done
3323 by incrementing face_change_count. The next call to
3324 init_iterator will then free realized faces. */
3325 if (!EQ (frame, Qt)
3326 && (EQ (attr, QCfont)
3327 || NILP (Fequal (old_value, value))))
3328 {
3329 ++face_change_count;
3330 ++windows_or_buffers_changed;
3331 }
3332
3333 #ifdef HAVE_X_WINDOWS
3334 /* Changed font-related attributes of the `default' face are
3335 reflected in changed `font' frame parameters. */
3336 if (EQ (face, Qdefault)
3337 && !EQ (frame, Qt)
3338 && font_related_attr_p
3339 && lface_fully_specified_p (XVECTOR (lface)->contents)
3340 && NILP (Fequal (old_value, value)))
3341 set_font_frame_param (frame, lface);
3342
3343 #endif /* HAVE_X_WINDOWS */
3344
3345 return face;
3346 }
3347
3348
3349 #ifdef HAVE_X_WINDOWS
3350
3351 /* Set the `font' frame parameter of FRAME according to `default' face
3352 attributes LFACE. */
3353
3354 static void
3355 set_font_frame_param (frame, lface)
3356 Lisp_Object frame, lface;
3357 {
3358 struct frame *f = XFRAME (frame);
3359 Lisp_Object frame_font;
3360 int fontset;
3361 char *font;
3362
3363 /* Get FRAME's font parameter. */
3364 frame_font = Fassq (Qfont, f->param_alist);
3365 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
3366 frame_font = XCDR (frame_font);
3367
3368 fontset = fs_query_fontset (f, XSTRING (frame_font)->data);
3369 if (fontset >= 0)
3370 {
3371 /* Frame parameter is a fontset name. Modify the fontset so
3372 that all its fonts reflect face attributes LFACE. */
3373 int charset;
3374 struct fontset_info *fontset_info;
3375
3376 fontset_info = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
3377
3378 for (charset = 0; charset < MAX_CHARSET; ++charset)
3379 if (fontset_info->fontname[charset])
3380 {
3381 font = choose_face_fontset_font (f, XVECTOR (lface)->contents,
3382 fontset, charset);
3383 Fset_fontset_font (frame_font, CHARSET_SYMBOL (charset),
3384 build_string (font), frame);
3385 xfree (font);
3386 }
3387 }
3388 else
3389 {
3390 /* Frame parameter is an X font name. I believe this can
3391 only happen in unibyte mode. */
3392 font = choose_face_font (f, XVECTOR (lface)->contents,
3393 -1, Vface_default_registry);
3394 if (font)
3395 {
3396 store_frame_param (f, Qfont, build_string (font));
3397 xfree (font);
3398 }
3399 }
3400 }
3401
3402
3403 /* Get the value of X resource RESOURCE, class CLASS for the display
3404 of frame FRAME. This is here because ordinary `x-get-resource'
3405 doesn't take a frame argument. */
3406
3407 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3408 Sinternal_face_x_get_resource, 3, 3, 0, "")
3409 (resource, class, frame)
3410 Lisp_Object resource, class, frame;
3411 {
3412 Lisp_Object value;
3413 CHECK_STRING (resource, 0);
3414 CHECK_STRING (class, 1);
3415 CHECK_LIVE_FRAME (frame, 2);
3416 BLOCK_INPUT;
3417 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3418 resource, class, Qnil, Qnil);
3419 UNBLOCK_INPUT;
3420 return value;
3421 }
3422
3423
3424 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3425 If VALUE is "on" or "true", return t. If VALUE is "off" or
3426 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3427 error; if SIGNAL_P is zero, return 0. */
3428
3429 static Lisp_Object
3430 face_boolean_x_resource_value (value, signal_p)
3431 Lisp_Object value;
3432 int signal_p;
3433 {
3434 Lisp_Object result = make_number (0);
3435
3436 xassert (STRINGP (value));
3437
3438 if (xstricmp (XSTRING (value)->data, "on") == 0
3439 || xstricmp (XSTRING (value)->data, "true") == 0)
3440 result = Qt;
3441 else if (xstricmp (XSTRING (value)->data, "off") == 0
3442 || xstricmp (XSTRING (value)->data, "false") == 0)
3443 result = Qnil;
3444 else if (xstricmp (XSTRING (value)->data, "unspecified") == 0)
3445 result = Qunspecified;
3446 else if (signal_p)
3447 signal_error ("Invalid face attribute value from X resource", value);
3448
3449 return result;
3450 }
3451
3452
3453 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3454 Finternal_set_lisp_face_attribute_from_resource,
3455 Sinternal_set_lisp_face_attribute_from_resource,
3456 3, 4, 0, "")
3457 (face, attr, value, frame)
3458 Lisp_Object face, attr, value, frame;
3459 {
3460 CHECK_SYMBOL (face, 0);
3461 CHECK_SYMBOL (attr, 1);
3462 CHECK_STRING (value, 2);
3463
3464 if (xstricmp (XSTRING (value)->data, "unspecified") == 0)
3465 value = Qunspecified;
3466 else if (EQ (attr, QCheight))
3467 {
3468 value = Fstring_to_number (value, make_number (10));
3469 if (XINT (value) <= 0)
3470 signal_error ("Invalid face height from X resource", value);
3471 }
3472 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3473 value = face_boolean_x_resource_value (value, 1);
3474 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3475 value = intern (XSTRING (value)->data);
3476 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3477 value = face_boolean_x_resource_value (value, 1);
3478 else if (EQ (attr, QCunderline)
3479 || EQ (attr, QCoverline)
3480 || EQ (attr, QCstrike_through)
3481 || EQ (attr, QCbox))
3482 {
3483 Lisp_Object boolean_value;
3484
3485 /* If the result of face_boolean_x_resource_value is t or nil,
3486 VALUE does NOT specify a color. */
3487 boolean_value = face_boolean_x_resource_value (value, 0);
3488 if (SYMBOLP (boolean_value))
3489 value = boolean_value;
3490 }
3491
3492 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3493 }
3494
3495
3496 #endif /* HAVE_X_WINDOWS */
3497
3498
3499
3500 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3501 Sinternal_get_lisp_face_attribute,
3502 2, 3, 0,
3503 "Return face attribute KEYWORD of face SYMBOL.\n\
3504 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid\n\
3505 face attribute name, signal an error.\n\
3506 If the optional argument FRAME is given, report on face FACE in that\n\
3507 frame. If FRAME is t, report on the defaults for face FACE (for new\n\
3508 frames). If FRAME is omitted or nil, use the selected frame.")
3509 (symbol, keyword, frame)
3510 Lisp_Object symbol, keyword, frame;
3511 {
3512 Lisp_Object lface, value = Qnil;
3513
3514 CHECK_SYMBOL (symbol, 0);
3515 CHECK_SYMBOL (keyword, 1);
3516
3517 if (EQ (frame, Qt))
3518 lface = lface_from_face_name (NULL, symbol, 1);
3519 else
3520 {
3521 if (NILP (frame))
3522 XSETFRAME (frame, selected_frame);
3523 CHECK_LIVE_FRAME (frame, 2);
3524 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3525 }
3526
3527 if (EQ (keyword, QCfamily))
3528 value = LFACE_FAMILY (lface);
3529 else if (EQ (keyword, QCheight))
3530 value = LFACE_HEIGHT (lface);
3531 else if (EQ (keyword, QCweight))
3532 value = LFACE_WEIGHT (lface);
3533 else if (EQ (keyword, QCslant))
3534 value = LFACE_SLANT (lface);
3535 else if (EQ (keyword, QCunderline))
3536 value = LFACE_UNDERLINE (lface);
3537 else if (EQ (keyword, QCoverline))
3538 value = LFACE_OVERLINE (lface);
3539 else if (EQ (keyword, QCstrike_through))
3540 value = LFACE_STRIKE_THROUGH (lface);
3541 else if (EQ (keyword, QCbox))
3542 value = LFACE_BOX (lface);
3543 else if (EQ (keyword, QCinverse_video)
3544 || EQ (keyword, QCreverse_video))
3545 value = LFACE_INVERSE (lface);
3546 else if (EQ (keyword, QCforeground))
3547 value = LFACE_FOREGROUND (lface);
3548 else if (EQ (keyword, QCbackground))
3549 value = LFACE_BACKGROUND (lface);
3550 else if (EQ (keyword, QCstipple))
3551 value = LFACE_STIPPLE (lface);
3552 else if (EQ (keyword, QCwidth))
3553 value = LFACE_SWIDTH (lface);
3554 else
3555 signal_error ("Invalid face attribute name", keyword);
3556
3557 return value;
3558 }
3559
3560
3561 DEFUN ("internal-lisp-face-attribute-values",
3562 Finternal_lisp_face_attribute_values,
3563 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3564 "Return a list of valid discrete values for face attribute ATTR.\n\
3565 Value is nil if ATTR doesn't have a discrete set of valid values.")
3566 (attr)
3567 Lisp_Object attr;
3568 {
3569 Lisp_Object result = Qnil;
3570
3571 CHECK_SYMBOL (attr, 0);
3572
3573 if (EQ (attr, QCweight)
3574 || EQ (attr, QCslant)
3575 || EQ (attr, QCwidth))
3576 {
3577 /* Extract permissible symbols from tables. */
3578 struct table_entry *table;
3579 int i, dim;
3580
3581 if (EQ (attr, QCweight))
3582 table = weight_table, dim = DIM (weight_table);
3583 else if (EQ (attr, QCslant))
3584 table = slant_table, dim = DIM (slant_table);
3585 else
3586 table = swidth_table, dim = DIM (swidth_table);
3587
3588 for (i = 0; i < dim; ++i)
3589 {
3590 Lisp_Object symbol = *table[i].symbol;
3591 Lisp_Object tail = result;
3592
3593 while (!NILP (tail)
3594 && !EQ (XCAR (tail), symbol))
3595 tail = XCDR (tail);
3596
3597 if (NILP (tail))
3598 result = Fcons (symbol, result);
3599 }
3600 }
3601 else if (EQ (attr, QCunderline))
3602 result = Fcons (Qt, Fcons (Qnil, Qnil));
3603 else if (EQ (attr, QCoverline))
3604 result = Fcons (Qt, Fcons (Qnil, Qnil));
3605 else if (EQ (attr, QCstrike_through))
3606 result = Fcons (Qt, Fcons (Qnil, Qnil));
3607 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3608 result = Fcons (Qt, Fcons (Qnil, Qnil));
3609
3610 return result;
3611 }
3612
3613
3614 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3615 Sinternal_merge_in_global_face, 2, 2, 0,
3616 "Add attributes from frame-default definition of FACE to FACE on FRAME.")
3617 (face, frame)
3618 Lisp_Object face, frame;
3619 {
3620 Lisp_Object global_lface, local_lface;
3621 CHECK_LIVE_FRAME (frame, 1);
3622 global_lface = lface_from_face_name (NULL, face, 1);
3623 local_lface = lface_from_face_name (XFRAME (frame), face, 0);
3624 if (NILP (local_lface))
3625 local_lface = Finternal_make_lisp_face (face, frame);
3626 merge_face_vectors (XVECTOR (global_lface)->contents,
3627 XVECTOR (local_lface)->contents);
3628 return face;
3629 }
3630
3631
3632 /* The following function is implemented for compatibility with 20.2.
3633 The function is used in x-resolve-fonts when it is asked to
3634 return fonts with the same size as the font of a face. This is
3635 done in fontset.el. */
3636
3637 DEFUN ("face-font", Fface_font, Sface_font, 1, 2, 0,
3638 "Return the font name of face FACE, or nil if it is unspecified.\n\
3639 If the optional argument FRAME is given, report on face FACE in that frame.\n\
3640 If FRAME is t, report on the defaults for face FACE (for new frames).\n\
3641 The font default for a face is either nil, or a list\n\
3642 of the form (bold), (italic) or (bold italic).\n\
3643 If FRAME is omitted or nil, use the selected frame.")
3644 (face, frame)
3645 Lisp_Object face, frame;
3646 {
3647 if (EQ (frame, Qt))
3648 {
3649 Lisp_Object result = Qnil;
3650 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3651
3652 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3653 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3654 result = Fcons (Qbold, result);
3655
3656 if (!NILP (LFACE_SLANT (lface))
3657 && !EQ (LFACE_SLANT (lface), Qnormal))
3658 result = Fcons (Qitalic, result);
3659
3660 return result;
3661 }
3662 else
3663 {
3664 struct frame *f = frame_or_selected_frame (frame, 1);
3665 int face_id = lookup_named_face (f, face, CHARSET_ASCII);
3666 struct face *face = FACE_FROM_ID (f, face_id);
3667 return build_string (face->font_name);
3668 }
3669 }
3670
3671
3672 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3673 all attributes are `equal'. Tries to be fast because this function
3674 is called quite often. */
3675
3676 static INLINE int
3677 lface_equal_p (v1, v2)
3678 Lisp_Object *v1, *v2;
3679 {
3680 int i, equal_p = 1;
3681
3682 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3683 {
3684 Lisp_Object a = v1[i];
3685 Lisp_Object b = v2[i];
3686
3687 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3688 and the other is specified. */
3689 equal_p = XTYPE (a) == XTYPE (b);
3690 if (!equal_p)
3691 break;
3692
3693 if (!EQ (a, b))
3694 {
3695 switch (XTYPE (a))
3696 {
3697 case Lisp_String:
3698 equal_p = (XSTRING (a)->size == XSTRING (b)->size
3699 && bcmp (XSTRING (a)->data, XSTRING (b)->data,
3700 XSTRING (a)->size) == 0);
3701 break;
3702
3703 case Lisp_Int:
3704 case Lisp_Symbol:
3705 equal_p = 0;
3706 break;
3707
3708 default:
3709 equal_p = !NILP (Fequal (a, b));
3710 break;
3711 }
3712 }
3713 }
3714
3715 return equal_p;
3716 }
3717
3718
3719 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3720 Sinternal_lisp_face_equal_p, 2, 3, 0,
3721 "True if FACE1 and FACE2 are equal.\n\
3722 If the optional argument FRAME is given, report on face FACE in that frame.\n\
3723 If FRAME is t, report on the defaults for face FACE (for new frames).\n\
3724 If FRAME is omitted or nil, use the selected frame.")
3725 (face1, face2, frame)
3726 Lisp_Object face1, face2, frame;
3727 {
3728 int equal_p;
3729 struct frame *f;
3730 Lisp_Object lface1, lface2;
3731
3732 if (EQ (frame, Qt))
3733 f = NULL;
3734 else
3735 /* Don't use check_x_frame here because this function is called
3736 before X frames exist. At that time, if FRAME is nil,
3737 selected_frame will be used which is the frame dumped with
3738 Emacs. That frame is not an X frame. */
3739 f = frame_or_selected_frame (frame, 2);
3740
3741 lface1 = lface_from_face_name (NULL, face1, 1);
3742 lface2 = lface_from_face_name (NULL, face2, 1);
3743 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3744 XVECTOR (lface2)->contents);
3745 return equal_p ? Qt : Qnil;
3746 }
3747
3748
3749 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3750 Sinternal_lisp_face_empty_p, 1, 2, 0,
3751 "True if FACE has no attribute specified.\n\
3752 If the optional argument FRAME is given, report on face FACE in that frame.\n\
3753 If FRAME is t, report on the defaults for face FACE (for new frames).\n\
3754 If FRAME is omitted or nil, use the selected frame.")
3755 (face, frame)
3756 Lisp_Object face, frame;
3757 {
3758 struct frame *f;
3759 Lisp_Object lface;
3760 int i;
3761
3762 if (NILP (frame))
3763 f = selected_frame;
3764 else
3765 {
3766 CHECK_LIVE_FRAME (frame, 0);
3767 f = XFRAME (frame);
3768 }
3769
3770 if (EQ (frame, Qt))
3771 lface = lface_from_face_name (NULL, face, 1);
3772 else
3773 lface = lface_from_face_name (f, face, 1);
3774
3775 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3776 if (!UNSPECIFIEDP (XVECTOR (lface)->contents[i]))
3777 break;
3778
3779 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3780 }
3781
3782
3783 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3784 0, 1, 0,
3785 "Return an alist of frame-local faces defined on FRAME.\n\
3786 For internal use only.")
3787 (frame)
3788 Lisp_Object frame;
3789 {
3790 struct frame *f = frame_or_selected_frame (frame, 0);
3791 return f->face_alist;
3792 }
3793
3794
3795 /* Return a hash code for Lisp string STRING with case ignored. Used
3796 below in computing a hash value for a Lisp face. */
3797
3798 static INLINE unsigned
3799 hash_string_case_insensitive (string)
3800 Lisp_Object string;
3801 {
3802 unsigned char *s;
3803 unsigned hash = 0;
3804 xassert (STRINGP (string));
3805 for (s = XSTRING (string)->data; *s; ++s)
3806 hash = (hash << 1) ^ tolower (*s);
3807 return hash;
3808 }
3809
3810
3811 /* Return a hash code for face attribute vector V. */
3812
3813 static INLINE unsigned
3814 lface_hash (v)
3815 Lisp_Object *v;
3816 {
3817 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3818 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3819 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3820 ^ (unsigned) v[LFACE_WEIGHT_INDEX]
3821 ^ (unsigned) v[LFACE_SLANT_INDEX]
3822 ^ (unsigned) v[LFACE_SWIDTH_INDEX]
3823 ^ XFASTINT (v[LFACE_HEIGHT_INDEX]));
3824 }
3825
3826
3827 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
3828 considering charsets/registries). They do if they specify the same
3829 family, point size, weight, width and slant. Both LFACE1 and
3830 LFACE2 must be fully-specified. */
3831
3832 static INLINE int
3833 lface_same_font_attributes_p (lface1, lface2)
3834 Lisp_Object *lface1, *lface2;
3835 {
3836 xassert (lface_fully_specified_p (lface1)
3837 && lface_fully_specified_p (lface2));
3838 return (xstricmp (XSTRING (lface1[LFACE_FAMILY_INDEX])->data,
3839 XSTRING (lface2[LFACE_FAMILY_INDEX])->data) == 0
3840 && (XFASTINT (lface1[LFACE_HEIGHT_INDEX])
3841 == XFASTINT (lface2[LFACE_HEIGHT_INDEX]))
3842 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3843 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3844 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX]));
3845 }
3846
3847
3848 \f
3849 /***********************************************************************
3850 Realized Faces
3851 ***********************************************************************/
3852
3853 /* Allocate and return a new realized face for Lisp face attribute
3854 vector ATTR, charset CHARSET, and registry REGISTRY. */
3855
3856 static struct face *
3857 make_realized_face (attr, charset, registry)
3858 Lisp_Object *attr;
3859 int charset;
3860 Lisp_Object registry;
3861 {
3862 struct face *face = (struct face *) xmalloc (sizeof *face);
3863 bzero (face, sizeof *face);
3864 face->charset = charset;
3865 face->registry = registry;
3866 bcopy (attr, face->lface, sizeof face->lface);
3867 return face;
3868 }
3869
3870
3871 /* Free realized face FACE, including its X resources. FACE may
3872 be null. */
3873
3874 static void
3875 free_realized_face (f, face)
3876 struct frame *f;
3877 struct face *face;
3878 {
3879 if (face)
3880 {
3881 #ifdef HAVE_X_WINDOWS
3882 if (FRAME_X_P (f))
3883 {
3884 if (face->gc)
3885 {
3886 x_free_gc (f, face->gc);
3887 face->gc = 0;
3888 }
3889
3890 free_face_colors (f, face);
3891 x_destroy_bitmap (f, face->stipple);
3892 }
3893 #endif /* HAVE_X_WINDOWS */
3894
3895 xfree (face);
3896 }
3897 }
3898
3899
3900 /* Prepare face FACE for subsequent display on frame F. This
3901 allocated GCs if they haven't been allocated yet or have been freed
3902 by clearing the face cache. */
3903
3904 void
3905 prepare_face_for_display (f, face)
3906 struct frame *f;
3907 struct face *face;
3908 {
3909 #ifdef HAVE_X_WINDOWS
3910 xassert (FRAME_X_P (f));
3911
3912 if (face->gc == 0)
3913 {
3914 XGCValues xgcv;
3915 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
3916
3917 xgcv.foreground = face->foreground;
3918 xgcv.background = face->background;
3919 xgcv.graphics_exposures = False;
3920
3921 /* The font of FACE may be null if we couldn't load it. */
3922 if (face->font)
3923 {
3924 xgcv.font = face->font->fid;
3925 mask |= GCFont;
3926 }
3927
3928 BLOCK_INPUT;
3929 if (face->stipple)
3930 {
3931 xgcv.fill_style = FillStippled;
3932 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
3933 mask |= GCFillStyle | GCStipple;
3934 }
3935
3936 face->gc = x_create_gc (f, mask, &xgcv);
3937 UNBLOCK_INPUT;
3938 }
3939 #endif
3940 }
3941
3942
3943 /* Non-zero if FACE is suitable for displaying ISO8859-1. Used in
3944 macro FACE_SUITABLE_FOR_CHARSET_P to avoid realizing a new face for
3945 ISO8859-1 if the ASCII face suffices. */
3946
3947 int
3948 face_suitable_for_iso8859_1_p (face)
3949 struct face *face;
3950 {
3951 int len = strlen (face->font_name);
3952 return len >= 9 && xstricmp (face->font_name + len - 9, "iso8859-1") == 0;
3953 }
3954
3955
3956 /* Value is non-zero if FACE is suitable for displaying characters
3957 of CHARSET. CHARSET < 0 means unibyte text. */
3958
3959 INLINE int
3960 face_suitable_for_charset_p (face, charset)
3961 struct face *face;
3962 int charset;
3963 {
3964 int suitable_p = 0;
3965
3966 if (charset < 0)
3967 {
3968 if (EQ (face->registry, Vface_default_registry)
3969 || !NILP (Fequal (face->registry, Vface_default_registry)))
3970 suitable_p = 1;
3971 }
3972 else if (face->charset == charset)
3973 suitable_p = 1;
3974 else if (face->charset == CHARSET_ASCII
3975 && charset == charset_latin_iso8859_1)
3976 suitable_p = face_suitable_for_iso8859_1_p (face);
3977 else if (face->charset == charset_latin_iso8859_1
3978 && charset == CHARSET_ASCII)
3979 suitable_p = 1;
3980
3981 return suitable_p;
3982 }
3983
3984
3985 \f
3986 /***********************************************************************
3987 Face Cache
3988 ***********************************************************************/
3989
3990 /* Return a new face cache for frame F. */
3991
3992 static struct face_cache *
3993 make_face_cache (f)
3994 struct frame *f;
3995 {
3996 struct face_cache *c;
3997 int size;
3998
3999 c = (struct face_cache *) xmalloc (sizeof *c);
4000 bzero (c, sizeof *c);
4001 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4002 c->buckets = (struct face **) xmalloc (size);
4003 bzero (c->buckets, size);
4004 c->size = 50;
4005 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4006 c->f = f;
4007 return c;
4008 }
4009
4010
4011 /* Clear out all graphics contexts for all realized faces, except for
4012 the basic faces. This should be done from time to time just to avoid
4013 keeping too many graphics contexts that are no longer needed. */
4014
4015 static void
4016 clear_face_gcs (c)
4017 struct face_cache *c;
4018 {
4019 if (c && FRAME_X_P (c->f))
4020 {
4021 #ifdef HAVE_X_WINDOWS
4022 int i;
4023 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4024 {
4025 struct face *face = c->faces_by_id[i];
4026 if (face && face->gc)
4027 {
4028 x_free_gc (c->f, face->gc);
4029 face->gc = 0;
4030 }
4031 }
4032 #endif /* HAVE_X_WINDOWS */
4033 }
4034 }
4035
4036
4037 /* Free all realized faces in face cache C, including basic faces. C
4038 may be null. If faces are freed, make sure the frame's current
4039 matrix is marked invalid, so that a display caused by an expose
4040 event doesn't try to use faces we destroyed. */
4041
4042 static void
4043 free_realized_faces (c)
4044 struct face_cache *c;
4045 {
4046 if (c && c->used)
4047 {
4048 int i, size;
4049 struct frame *f = c->f;
4050
4051 for (i = 0; i < c->used; ++i)
4052 {
4053 free_realized_face (f, c->faces_by_id[i]);
4054 c->faces_by_id[i] = NULL;
4055 }
4056
4057 c->used = 0;
4058 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4059 bzero (c->buckets, size);
4060
4061 /* Must do a thorough redisplay the next time. Mark current
4062 matrices as invalid because they will reference faces freed
4063 above. This function is also called when a frame is
4064 destroyed. In this case, the root window of F is nil. */
4065 if (WINDOWP (f->root_window))
4066 {
4067 clear_current_matrices (f);
4068 ++windows_or_buffers_changed;
4069 }
4070 }
4071 }
4072
4073
4074 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4075 This is done after attributes of a named face have been changed,
4076 because we can't tell which realized faces depend on that face. */
4077
4078 void
4079 free_all_realized_faces (frame)
4080 Lisp_Object frame;
4081 {
4082 if (NILP (frame))
4083 {
4084 Lisp_Object rest;
4085 FOR_EACH_FRAME (rest, frame)
4086 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4087 }
4088 else
4089 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4090 }
4091
4092
4093 /* Free face cache C and faces in it, including their X resources. */
4094
4095 static void
4096 free_face_cache (c)
4097 struct face_cache *c;
4098 {
4099 if (c)
4100 {
4101 free_realized_faces (c);
4102 xfree (c->buckets);
4103 xfree (c->faces_by_id);
4104 xfree (c);
4105 }
4106 }
4107
4108
4109 /* Cache realized face FACE in face cache C. HASH is the hash value
4110 of FACE. If FACE->fontset >= 0, add the new face to the end of the
4111 collision list of the face hash table of C. This is done because
4112 otherwise lookup_face would find FACE for every charset, even if
4113 faces with the same attributes but for specific charsets exist. */
4114
4115 static void
4116 cache_face (c, face, hash)
4117 struct face_cache *c;
4118 struct face *face;
4119 unsigned hash;
4120 {
4121 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4122
4123 face->hash = hash;
4124
4125 if (face->fontset >= 0)
4126 {
4127 struct face *last = c->buckets[i];
4128 if (last)
4129 {
4130 while (last->next)
4131 last = last->next;
4132 last->next = face;
4133 face->prev = last;
4134 face->next = NULL;
4135 }
4136 else
4137 {
4138 c->buckets[i] = face;
4139 face->prev = face->next = NULL;
4140 }
4141 }
4142 else
4143 {
4144 face->prev = NULL;
4145 face->next = c->buckets[i];
4146 if (face->next)
4147 face->next->prev = face;
4148 c->buckets[i] = face;
4149 }
4150
4151 /* Find a free slot in C->faces_by_id and use the index of the free
4152 slot as FACE->id. */
4153 for (i = 0; i < c->used; ++i)
4154 if (c->faces_by_id[i] == NULL)
4155 break;
4156 face->id = i;
4157
4158 /* Maybe enlarge C->faces_by_id. */
4159 if (i == c->used && c->used == c->size)
4160 {
4161 int new_size = 2 * c->size;
4162 int sz = new_size * sizeof *c->faces_by_id;
4163 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4164 c->size = new_size;
4165 }
4166
4167 #if GLYPH_DEBUG
4168 /* Check that FACE got a unique id. */
4169 {
4170 int j, n;
4171 struct face *face;
4172
4173 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4174 for (face = c->buckets[j]; face; face = face->next)
4175 if (face->id == i)
4176 ++n;
4177
4178 xassert (n == 1);
4179 }
4180 #endif /* GLYPH_DEBUG */
4181
4182 c->faces_by_id[i] = face;
4183 if (i == c->used)
4184 ++c->used;
4185 }
4186
4187
4188 /* Remove face FACE from cache C. */
4189
4190 static void
4191 uncache_face (c, face)
4192 struct face_cache *c;
4193 struct face *face;
4194 {
4195 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4196
4197 if (face->prev)
4198 face->prev->next = face->next;
4199 else
4200 c->buckets[i] = face->next;
4201
4202 if (face->next)
4203 face->next->prev = face->prev;
4204
4205 c->faces_by_id[face->id] = NULL;
4206 if (face->id == c->used)
4207 --c->used;
4208 }
4209
4210
4211 /* Look up a realized face with face attributes ATTR in the face cache
4212 of frame F. The face will be used to display characters of
4213 CHARSET. CHARSET < 0 means the face will be used to display
4214 unibyte text. The value of face-default-registry is used to choose
4215 a font for the face in that case. Value is the ID of the face
4216 found. If no suitable face is found, realize a new one. */
4217
4218 INLINE int
4219 lookup_face (f, attr, charset)
4220 struct frame *f;
4221 Lisp_Object *attr;
4222 int charset;
4223 {
4224 struct face_cache *c = FRAME_FACE_CACHE (f);
4225 unsigned hash;
4226 int i;
4227 struct face *face;
4228
4229 xassert (c != NULL);
4230 check_lface_attrs (attr);
4231
4232 /* Look up ATTR in the face cache. */
4233 hash = lface_hash (attr);
4234 i = hash % FACE_CACHE_BUCKETS_SIZE;
4235
4236 for (face = c->buckets[i]; face; face = face->next)
4237 if (face->hash == hash
4238 && (FRAME_TERMCAP_P (f)
4239 || FACE_SUITABLE_FOR_CHARSET_P (face, charset))
4240 && lface_equal_p (face->lface, attr))
4241 break;
4242
4243 /* If not found, realize a new face. */
4244 if (face == NULL)
4245 {
4246 face = realize_face (c, attr, charset);
4247 cache_face (c, face, hash);
4248 }
4249
4250 #if GLYPH_DEBUG
4251 xassert (face == FACE_FROM_ID (f, face->id));
4252 if (FRAME_X_P (f))
4253 xassert (charset < 0 || FACE_SUITABLE_FOR_CHARSET_P (face, charset));
4254 #endif /* GLYPH_DEBUG */
4255
4256 return face->id;
4257 }
4258
4259
4260 /* Return the face id of the realized face for named face SYMBOL on
4261 frame F suitable for displaying characters from CHARSET. CHARSET <
4262 0 means unibyte text. */
4263
4264 int
4265 lookup_named_face (f, symbol, charset)
4266 struct frame *f;
4267 Lisp_Object symbol;
4268 int charset;
4269 {
4270 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4271 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4272 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4273
4274 get_lface_attributes (f, symbol, symbol_attrs, 1);
4275 bcopy (default_face->lface, attrs, sizeof attrs);
4276 merge_face_vectors (symbol_attrs, attrs);
4277 return lookup_face (f, attrs, charset);
4278 }
4279
4280
4281 /* Return the ID of the realized ASCII face of Lisp face with ID
4282 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
4283
4284 int
4285 ascii_face_of_lisp_face (f, lface_id)
4286 struct frame *f;
4287 int lface_id;
4288 {
4289 int face_id;
4290
4291 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
4292 {
4293 Lisp_Object face_name = lface_id_to_name[lface_id];
4294 face_id = lookup_named_face (f, face_name, CHARSET_ASCII);
4295 }
4296 else
4297 face_id = -1;
4298
4299 return face_id;
4300 }
4301
4302
4303 /* Return a face for charset ASCII that is like the face with id
4304 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4305 STEPS < 0 means larger. Value is the id of the face. */
4306
4307 int
4308 smaller_face (f, face_id, steps)
4309 struct frame *f;
4310 int face_id, steps;
4311 {
4312 #ifdef HAVE_X_WINDOWS
4313 struct face *face;
4314 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4315 int pt, last_pt, last_height;
4316 int delta;
4317 int new_face_id;
4318 struct face *new_face;
4319
4320 /* If not called for an X frame, just return the original face. */
4321 if (FRAME_TERMCAP_P (f))
4322 return face_id;
4323
4324 /* Try in increments of 1/2 pt. */
4325 delta = steps < 0 ? 5 : -5;
4326 steps = abs (steps);
4327
4328 face = FACE_FROM_ID (f, face_id);
4329 bcopy (face->lface, attrs, sizeof attrs);
4330 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4331 new_face_id = face_id;
4332 last_height = FONT_HEIGHT (face->font);
4333
4334 while (steps
4335 && pt + delta > 0
4336 /* Give up if we cannot find a font within 10pt. */
4337 && abs (last_pt - pt) < 100)
4338 {
4339 /* Look up a face for a slightly smaller/larger font. */
4340 pt += delta;
4341 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4342 new_face_id = lookup_face (f, attrs, CHARSET_ASCII);
4343 new_face = FACE_FROM_ID (f, new_face_id);
4344
4345 /* If height changes, count that as one step. */
4346 if (FONT_HEIGHT (new_face->font) != last_height)
4347 {
4348 --steps;
4349 last_height = FONT_HEIGHT (new_face->font);
4350 last_pt = pt;
4351 }
4352 }
4353
4354 return new_face_id;
4355
4356 #else /* not HAVE_X_WINDOWS */
4357
4358 return face_id;
4359
4360 #endif /* not HAVE_X_WINDOWS */
4361 }
4362
4363
4364 /* Return a face for charset ASCII that is like the face with id
4365 FACE_ID on frame F, but has height HEIGHT. */
4366
4367 int
4368 face_with_height (f, face_id, height)
4369 struct frame *f;
4370 int face_id;
4371 int height;
4372 {
4373 #ifdef HAVE_X_WINDOWS
4374 struct face *face;
4375 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4376
4377 if (FRAME_TERMCAP_P (f)
4378 || height <= 0)
4379 return face_id;
4380
4381 face = FACE_FROM_ID (f, face_id);
4382 bcopy (face->lface, attrs, sizeof attrs);
4383 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4384 face_id = lookup_face (f, attrs, CHARSET_ASCII);
4385 #endif /* HAVE_X_WINDOWS */
4386
4387 return face_id;
4388 }
4389
4390
4391 \f
4392 /***********************************************************************
4393 Font selection
4394 ***********************************************************************/
4395
4396 DEFUN ("internal-set-font-selection-order",
4397 Finternal_set_font_selection_order,
4398 Sinternal_set_font_selection_order, 1, 1, 0,
4399 "Set font selection order for face font selection to ORDER.\n\
4400 ORDER must be a list of length 4 containing the symbols `:width',\n\
4401 `:height', `:weight', and `:slant'. Face attributes appearing\n\
4402 first in ORDER are matched first, e.g. if `:height' appears before\n\
4403 `:weight' in ORDER, font selection first tries to find a font with\n\
4404 a suitable height, and then tries to match the font weight.\n\
4405 Value is ORDER.")
4406 (order)
4407 Lisp_Object order;
4408 {
4409 Lisp_Object list;
4410 int i;
4411 int indices[4];
4412
4413 CHECK_LIST (order, 0);
4414 bzero (indices, sizeof indices);
4415 i = 0;
4416
4417 for (list = order;
4418 CONSP (list) && i < DIM (indices);
4419 list = XCDR (list), ++i)
4420 {
4421 Lisp_Object attr = XCAR (list);
4422 int xlfd;
4423
4424 if (EQ (attr, QCwidth))
4425 xlfd = XLFD_SWIDTH;
4426 else if (EQ (attr, QCheight))
4427 xlfd = XLFD_POINT_SIZE;
4428 else if (EQ (attr, QCweight))
4429 xlfd = XLFD_WEIGHT;
4430 else if (EQ (attr, QCslant))
4431 xlfd = XLFD_SLANT;
4432 else
4433 break;
4434
4435 if (indices[i] != 0)
4436 break;
4437 indices[i] = xlfd;
4438 }
4439
4440 if (!NILP (list)
4441 || i != DIM (indices)
4442 || indices[0] == 0
4443 || indices[1] == 0
4444 || indices[2] == 0
4445 || indices[3] == 0)
4446 signal_error ("Invalid font sort order", order);
4447
4448 if (bcmp (indices, font_sort_order, sizeof indices) != 0)
4449 {
4450 bcopy (indices, font_sort_order, sizeof font_sort_order);
4451 free_all_realized_faces (Qnil);
4452 }
4453
4454 return Qnil;
4455 }
4456
4457
4458 DEFUN ("internal-set-alternative-font-family-alist",
4459 Finternal_set_alternative_font_family_alist,
4460 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
4461 "Define alternative font families to try in face font selection.\n\
4462 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.\n\
4463 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can\n\
4464 be found. Value is ALIST.")
4465 (alist)
4466 Lisp_Object alist;
4467 {
4468 CHECK_LIST (alist, 0);
4469 Vface_alternative_font_family_alist = alist;
4470 free_all_realized_faces (Qnil);
4471 return alist;
4472 }
4473
4474
4475 #ifdef HAVE_X_WINDOWS
4476
4477 /* Return the X registry and encoding of font name FONT_NAME on frame F.
4478 Value is nil if not successful. */
4479
4480 static Lisp_Object
4481 deduce_unibyte_registry (f, font_name)
4482 struct frame *f;
4483 char *font_name;
4484 {
4485 struct font_name font;
4486 Lisp_Object registry = Qnil;
4487
4488 font.name = STRDUPA (font_name);
4489 if (split_font_name (f, &font, 0))
4490 {
4491 char *buffer;
4492
4493 /* Extract registry and encoding. */
4494 buffer = (char *) alloca (strlen (font.fields[XLFD_REGISTRY])
4495 + strlen (font.fields[XLFD_ENCODING])
4496 + 10);
4497 strcpy (buffer, font.fields[XLFD_REGISTRY]);
4498 strcat (buffer, "-");
4499 strcat (buffer, font.fields[XLFD_ENCODING]);
4500 registry = build_string (buffer);
4501 }
4502
4503 return registry;
4504 }
4505
4506
4507 /* Value is non-zero if FONT is the name of a scalable font. The
4508 X11R6 XLFD spec says that point size, pixel size, and average width
4509 are zero for scalable fonts. Intlfonts contain at least one
4510 scalable font ("*-muleindian-1") for which this isn't true, so we
4511 just test average width. */
4512
4513 static int
4514 font_scalable_p (font)
4515 struct font_name *font;
4516 {
4517 char *s = font->fields[XLFD_AVGWIDTH];
4518 return *s == '0' && *(s + 1) == '\0';
4519 }
4520
4521
4522 /* Value is non-zero if FONT1 is a better match for font attributes
4523 VALUES than FONT2. VALUES is an array of face attribute values in
4524 font sort order. COMPARE_PT_P zero means don't compare point
4525 sizes. */
4526
4527 static int
4528 better_font_p (values, font1, font2, compare_pt_p)
4529 int *values;
4530 struct font_name *font1, *font2;
4531 int compare_pt_p;
4532 {
4533 int i;
4534
4535 for (i = 0; i < 4; ++i)
4536 {
4537 int xlfd_idx = font_sort_order[i];
4538
4539 if (compare_pt_p || xlfd_idx != XLFD_POINT_SIZE)
4540 {
4541 int delta1 = abs (values[i] - font1->numeric[xlfd_idx]);
4542 int delta2 = abs (values[i] - font2->numeric[xlfd_idx]);
4543
4544 if (delta1 > delta2)
4545 return 0;
4546 else if (delta1 < delta2)
4547 return 1;
4548 else
4549 {
4550 /* The difference may be equal because, e.g., the face
4551 specifies `italic' but we have only `regular' and
4552 `oblique'. Prefer `oblique' in this case. */
4553 if ((xlfd_idx == XLFD_WEIGHT || xlfd_idx == XLFD_SLANT)
4554 && font1->numeric[xlfd_idx] > values[i]
4555 && font2->numeric[xlfd_idx] < values[i])
4556 return 1;
4557 }
4558 }
4559 }
4560
4561 return 0;
4562 }
4563
4564
4565 #if SCALABLE_FONTS
4566
4567 /* Value is non-zero if FONT is an exact match for face attributes in
4568 SPECIFIED. SPECIFIED is an array of face attribute values in font
4569 sort order. */
4570
4571 static int
4572 exact_face_match_p (specified, font)
4573 int *specified;
4574 struct font_name *font;
4575 {
4576 int i;
4577
4578 for (i = 0; i < 4; ++i)
4579 if (specified[i] != font->numeric[font_sort_order[i]])
4580 break;
4581
4582 return i == 4;
4583 }
4584
4585
4586 /* Value is the name of a scaled font, generated from scalable font
4587 FONT on frame F. SPECIFIED_PT is the point-size to scale FONT to.
4588 Value is allocated from heap. */
4589
4590 static char *
4591 build_scalable_font_name (f, font, specified_pt)
4592 struct frame *f;
4593 struct font_name *font;
4594 int specified_pt;
4595 {
4596 char point_size[20], pixel_size[20];
4597 int pixel_value;
4598 double resy = FRAME_X_DISPLAY_INFO (f)->resy;
4599 double pt;
4600
4601 /* If scalable font is for a specific resolution, compute
4602 the point size we must specify from the resolution of
4603 the display and the specified resolution of the font. */
4604 if (font->numeric[XLFD_RESY] != 0)
4605 {
4606 pt = resy / font->numeric[XLFD_RESY] * specified_pt + 0.5;
4607 pixel_value = font->numeric[XLFD_RESY] / 720.0 * pt;
4608 }
4609 else
4610 {
4611 pt = specified_pt;
4612 pixel_value = resy / 720.0 * pt;
4613 }
4614
4615 /* Set point size of the font. */
4616 sprintf (point_size, "%d", (int) pt);
4617 font->fields[XLFD_POINT_SIZE] = point_size;
4618 font->numeric[XLFD_POINT_SIZE] = pt;
4619
4620 /* Set pixel size. */
4621 sprintf (pixel_size, "%d", pixel_value);
4622 font->fields[XLFD_PIXEL_SIZE] = pixel_size;
4623 font->numeric[XLFD_PIXEL_SIZE] = pixel_value;
4624
4625 /* If font doesn't specify its resolution, use the
4626 resolution of the display. */
4627 if (font->numeric[XLFD_RESY] == 0)
4628 {
4629 char buffer[20];
4630 sprintf (buffer, "%d", (int) resy);
4631 font->fields[XLFD_RESY] = buffer;
4632 font->numeric[XLFD_RESY] = resy;
4633 }
4634
4635 if (strcmp (font->fields[XLFD_RESX], "0") == 0)
4636 {
4637 char buffer[20];
4638 int resx = FRAME_X_DISPLAY_INFO (f)->resx;
4639 sprintf (buffer, "%d", resx);
4640 font->fields[XLFD_RESX] = buffer;
4641 font->numeric[XLFD_RESX] = resx;
4642 }
4643
4644 return build_font_name (font);
4645 }
4646
4647
4648 /* Value is non-zero if we are allowed to use scalable font FONT. We
4649 can't run a Lisp function here since this function may be called
4650 with input blocked. */
4651
4652 static int
4653 may_use_scalable_font_p (font, name)
4654 struct font_name *font;
4655 char *name;
4656 {
4657 if (EQ (Vscalable_fonts_allowed, Qt))
4658 return 1;
4659 else if (CONSP (Vscalable_fonts_allowed))
4660 {
4661 Lisp_Object tail, regexp;
4662
4663 for (tail = Vscalable_fonts_allowed; CONSP (tail); tail = XCDR (tail))
4664 {
4665 regexp = XCAR (tail);
4666 if (STRINGP (regexp)
4667 && fast_c_string_match_ignore_case (regexp, name) >= 0)
4668 return 1;
4669 }
4670 }
4671
4672 return 0;
4673 }
4674
4675 #endif /* SCALABLE_FONTS != 0 */
4676
4677
4678 /* Return the name of the best matching font for face attributes
4679 ATTRS in the array of font_name structures FONTS which contains
4680 NFONTS elements. Value is a font name which is allocated from
4681 the heap. FONTS is freed by this function. */
4682
4683 static char *
4684 best_matching_font (f, attrs, fonts, nfonts)
4685 struct frame *f;
4686 Lisp_Object *attrs;
4687 struct font_name *fonts;
4688 int nfonts;
4689 {
4690 char *font_name;
4691 struct font_name *best;
4692 int i, pt;
4693 int specified[4];
4694 int exact_p;
4695
4696 if (nfonts == 0)
4697 return NULL;
4698
4699 /* Make specified font attributes available in `specified',
4700 indexed by sort order. */
4701 for (i = 0; i < DIM (font_sort_order); ++i)
4702 {
4703 int xlfd_idx = font_sort_order[i];
4704
4705 if (xlfd_idx == XLFD_SWIDTH)
4706 specified[i] = face_numeric_swidth (attrs[LFACE_SWIDTH_INDEX]);
4707 else if (xlfd_idx == XLFD_POINT_SIZE)
4708 specified[i] = pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4709 else if (xlfd_idx == XLFD_WEIGHT)
4710 specified[i] = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
4711 else if (xlfd_idx == XLFD_SLANT)
4712 specified[i] = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
4713 else
4714 abort ();
4715 }
4716
4717 #if SCALABLE_FONTS
4718
4719 /* Set to 1 */
4720 exact_p = 0;
4721
4722 /* Start with the first non-scalable font in the list. */
4723 for (i = 0; i < nfonts; ++i)
4724 if (!font_scalable_p (fonts + i))
4725 break;
4726
4727 /* Find the best match among the non-scalable fonts. */
4728 if (i < nfonts)
4729 {
4730 best = fonts + i;
4731
4732 for (i = 1; i < nfonts; ++i)
4733 if (!font_scalable_p (fonts + i)
4734 && better_font_p (specified, fonts + i, best, 1))
4735 {
4736 best = fonts + i;
4737
4738 exact_p = exact_face_match_p (specified, best);
4739 if (exact_p)
4740 break;
4741 }
4742
4743 }
4744 else
4745 best = NULL;
4746
4747 /* Unless we found an exact match among non-scalable fonts, see if
4748 we can find a better match among scalable fonts. */
4749 if (!exact_p)
4750 {
4751 /* A scalable font is better if
4752
4753 1. its weight, slant, swidth attributes are better, or.
4754
4755 2. the best non-scalable font doesn't have the required
4756 point size, and the scalable fonts weight, slant, swidth
4757 isn't worse. */
4758
4759 int non_scalable_has_exact_height_p;
4760
4761 if (best && best->numeric[XLFD_POINT_SIZE] == pt)
4762 non_scalable_has_exact_height_p = 1;
4763 else
4764 non_scalable_has_exact_height_p = 0;
4765
4766 for (i = 0; i < nfonts; ++i)
4767 if (font_scalable_p (fonts + i))
4768 {
4769 if (best == NULL
4770 || better_font_p (specified, fonts + i, best, 0)
4771 || (!non_scalable_has_exact_height_p
4772 && !better_font_p (specified, best, fonts + i, 0)))
4773 best = fonts + i;
4774 }
4775 }
4776
4777 if (font_scalable_p (best))
4778 font_name = build_scalable_font_name (f, best, pt);
4779 else
4780 font_name = build_font_name (best);
4781
4782 #else /* !SCALABLE_FONTS */
4783
4784 /* Find the best non-scalable font. */
4785 best = fonts;
4786
4787 for (i = 1; i < nfonts; ++i)
4788 {
4789 xassert (!font_scalable_p (fonts + i));
4790 if (better_font_p (specified, fonts + i, best, 1))
4791 best = fonts + i;
4792 }
4793
4794 font_name = build_font_name (best);
4795
4796 #endif /* !SCALABLE_FONTS */
4797
4798 /* Free font_name structures. */
4799 free_font_names (fonts, nfonts);
4800
4801 return font_name;
4802 }
4803
4804
4805 /* Try to get a list of fonts on frame F with font family FAMILY and
4806 registry/encoding REGISTRY. Return in *FONTS a pointer to a vector
4807 of font_name structures for the fonts matched. Value is the number
4808 of fonts found. */
4809
4810 static int
4811 try_font_list (f, attrs, pattern, family, registry, fonts)
4812 struct frame *f;
4813 Lisp_Object *attrs;
4814 char *pattern, *family, *registry;
4815 struct font_name **fonts;
4816 {
4817 int nfonts;
4818
4819 if (family == NULL)
4820 family = LSTRDUPA (attrs[LFACE_FAMILY_INDEX]);
4821
4822 nfonts = font_list (f, pattern, family, registry, fonts);
4823
4824 if (nfonts == 0)
4825 {
4826 Lisp_Object alter;
4827
4828 /* Try alternative font families from
4829 Vface_alternative_font_family_alist. */
4830 alter = Fassoc (build_string (family),
4831 Vface_alternative_font_family_alist);
4832 if (CONSP (alter))
4833 for (alter = XCDR (alter);
4834 CONSP (alter) && nfonts == 0;
4835 alter = XCDR (alter))
4836 {
4837 if (STRINGP (XCAR (alter)))
4838 {
4839 family = LSTRDUPA (XCAR (alter));
4840 nfonts = font_list (f, NULL, family, registry, fonts);
4841 }
4842 }
4843
4844 /* Try font family of the default face or "fixed". */
4845 if (nfonts == 0)
4846 {
4847 struct face *dflt = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4848 if (dflt)
4849 family = LSTRDUPA (dflt->lface[LFACE_FAMILY_INDEX]);
4850 else
4851 family = "fixed";
4852 nfonts = font_list (f, NULL, family, registry, fonts);
4853 }
4854
4855 /* Try any family with the given registry. */
4856 if (nfonts == 0)
4857 nfonts = font_list (f, NULL, "*", registry, fonts);
4858 }
4859
4860 return nfonts;
4861 }
4862
4863
4864 /* Return the registry and encoding pattern that fonts for CHARSET
4865 should match. Value is allocated from the heap. */
4866
4867 char *
4868 x_charset_registry (charset)
4869 int charset;
4870 {
4871 Lisp_Object prop, charset_plist;
4872 char *registry;
4873
4874 /* Get registry and encoding from the charset's plist. */
4875 charset_plist = CHARSET_TABLE_INFO (charset, CHARSET_PLIST_IDX);
4876 prop = Fplist_get (charset_plist, Qx_charset_registry);
4877
4878 if (STRINGP (prop))
4879 {
4880 if (index (XSTRING (prop)->data, '-'))
4881 registry = xstrdup (XSTRING (prop)->data);
4882 else
4883 {
4884 /* If registry doesn't contain a `-', make it a pattern. */
4885 registry = (char *) xmalloc (STRING_BYTES (XSTRING (prop)) + 5);
4886 strcpy (registry, XSTRING (prop)->data);
4887 strcat (registry, "*-*");
4888 }
4889 }
4890 else if (STRINGP (Vface_default_registry))
4891 registry = xstrdup (XSTRING (Vface_default_registry)->data);
4892 else
4893 registry = xstrdup ("iso8859-1");
4894
4895 return registry;
4896 }
4897
4898
4899 /* Return the fontset id of the fontset name or alias name given by
4900 the family attribute of ATTRS on frame F. Value is -1 if the
4901 family attribute of ATTRS doesn't name a fontset. */
4902
4903 static int
4904 face_fontset (f, attrs)
4905 struct frame *f;
4906 Lisp_Object *attrs;
4907 {
4908 Lisp_Object name = attrs[LFACE_FAMILY_INDEX];
4909 int fontset;
4910
4911 name = Fquery_fontset (name, Qnil);
4912 if (NILP (name))
4913 fontset = -1;
4914 else
4915 fontset = fs_query_fontset (f, XSTRING (name)->data);
4916
4917 return fontset;
4918 }
4919
4920
4921 /* Get the font to use for the face realizing the fully-specified Lisp
4922 face ATTRS for charset CHARSET on frame F. CHARSET < 0 means
4923 unibyte text; UNIBYTE_REGISTRY is the registry and encoding to use
4924 in this case. Value is the font name which is allocated from the
4925 heap (which means that it must be freed eventually). */
4926
4927 static char *
4928 choose_face_font (f, attrs, charset, unibyte_registry)
4929 struct frame *f;
4930 Lisp_Object *attrs;
4931 int charset;
4932 Lisp_Object unibyte_registry;
4933 {
4934 struct font_name *fonts;
4935 int nfonts;
4936 char *registry;
4937
4938 /* ATTRS must be fully-specified. */
4939 xassert (lface_fully_specified_p (attrs));
4940
4941 if (STRINGP (unibyte_registry))
4942 registry = xstrdup (XSTRING (unibyte_registry)->data);
4943 else
4944 registry = x_charset_registry (charset);
4945
4946 nfonts = try_font_list (f, attrs, NULL, NULL, registry, &fonts);
4947 xfree (registry);
4948 return best_matching_font (f, attrs, fonts, nfonts);
4949 }
4950
4951
4952 /* Choose a font to use on frame F to display CHARSET using FONTSET
4953 with Lisp face attributes specified by ATTRS. CHARSET may be any
4954 valid charset except CHARSET_COMPOSITION. CHARSET < 0 means
4955 unibyte text. If the fontset doesn't contain a font pattern for
4956 charset, use the pattern for CHARSET_ASCII. Value is the font name
4957 which is allocated from the heap and must be freed by the caller. */
4958
4959 static char *
4960 choose_face_fontset_font (f, attrs, fontset, charset)
4961 struct frame *f;
4962 Lisp_Object *attrs;
4963 int fontset, charset;
4964 {
4965 char *pattern;
4966 char *font_name = NULL;
4967 struct fontset_info *fontset_info;
4968 struct font_name *fonts;
4969 int nfonts;
4970
4971 xassert (charset != CHARSET_COMPOSITION);
4972 xassert (fontset >= 0 && fontset < FRAME_FONTSET_DATA (f)->n_fontsets);
4973
4974 /* For unibyte text, use the ASCII font of the fontset. Using the
4975 ASCII font seems to be the most reasonable thing we can do in
4976 this case. */
4977 if (charset < 0)
4978 charset = CHARSET_ASCII;
4979
4980 /* Get the font name pattern to use for CHARSET from the fontset. */
4981 fontset_info = FRAME_FONTSET_DATA (f)->fontset_table[fontset];
4982 pattern = fontset_info->fontname[charset];
4983 if (!pattern)
4984 pattern = fontset_info->fontname[CHARSET_ASCII];
4985 xassert (pattern);
4986
4987 /* Get a list of fonts matching that pattern and choose the
4988 best match for the specified face attributes from it. */
4989 nfonts = try_font_list (f, attrs, pattern, NULL, NULL, &fonts);
4990 font_name = best_matching_font (f, attrs, fonts, nfonts);
4991 return font_name;
4992 }
4993
4994 #endif /* HAVE_X_WINDOWS */
4995
4996
4997 \f
4998 /***********************************************************************
4999 Face Realization
5000 ***********************************************************************/
5001
5002 /* Realize basic faces on frame F. Value is zero if frame parameters
5003 of F don't contain enough information needed to realize the default
5004 face. */
5005
5006 static int
5007 realize_basic_faces (f)
5008 struct frame *f;
5009 {
5010 int success_p = 0;
5011
5012 if (realize_default_face (f))
5013 {
5014 realize_named_face (f, Qmodeline, MODE_LINE_FACE_ID);
5015 realize_named_face (f, Qtoolbar, TOOLBAR_FACE_ID);
5016 realize_named_face (f, Qbitmap_area, BITMAP_AREA_FACE_ID);
5017 realize_named_face (f, Qtop_line, TOP_LINE_FACE_ID);
5018 success_p = 1;
5019 }
5020
5021 return success_p;
5022 }
5023
5024
5025 /* Realize the default face on frame F. If the face is not fully
5026 specified, make it fully-specified. Attributes of the default face
5027 that are not explicitly specified are taken from frame parameters. */
5028
5029 static int
5030 realize_default_face (f)
5031 struct frame *f;
5032 {
5033 struct face_cache *c = FRAME_FACE_CACHE (f);
5034 Lisp_Object lface;
5035 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5036 Lisp_Object unibyte_registry;
5037 Lisp_Object frame_font;
5038 struct face *face;
5039 int fontset;
5040
5041 /* If the `default' face is not yet known, create it. */
5042 lface = lface_from_face_name (f, Qdefault, 0);
5043 if (NILP (lface))
5044 {
5045 Lisp_Object frame;
5046 XSETFRAME (frame, f);
5047 lface = Finternal_make_lisp_face (Qdefault, frame);
5048 }
5049
5050 #ifdef HAVE_X_WINDOWS
5051 if (FRAME_X_P (f))
5052 {
5053 /* Set frame_font to the value of the `font' frame parameter. */
5054 frame_font = Fassq (Qfont, f->param_alist);
5055 xassert (CONSP (frame_font) && STRINGP (XCDR (frame_font)));
5056 frame_font = XCDR (frame_font);
5057
5058 fontset = fs_query_fontset (f, XSTRING (frame_font)->data);
5059 if (fontset >= 0)
5060 {
5061 /* If frame_font is a fontset name, don't use that for
5062 determining font-related attributes of the default face
5063 because it is just an artificial name. Use the ASCII font of
5064 the fontset, instead. */
5065 struct font_info *font_info;
5066 struct font_name font;
5067
5068 BLOCK_INPUT;
5069 font_info = FS_LOAD_FONT (f, FRAME_X_FONT_TABLE (f), CHARSET_ASCII,
5070 NULL, fontset);
5071 UNBLOCK_INPUT;
5072
5073 /* Set weight etc. from the ASCII font. */
5074 if (!set_lface_from_font_name (f, lface, font_info->full_name, 0))
5075 return 0;
5076
5077 /* Remember registry and encoding of the frame font. */
5078 unibyte_registry = deduce_unibyte_registry (f, font_info->full_name);
5079 if (STRINGP (unibyte_registry))
5080 Vface_default_registry = unibyte_registry;
5081 else
5082 Vface_default_registry = build_string ("iso8859-1");
5083
5084 /* But set the family to the fontset alias name. Implementation
5085 note: When a font is passed to Emacs via `-fn FONT', a
5086 fontset is created in `x-win.el' whose name ends in
5087 `fontset-startup'. This fontset has an alias name that is
5088 equal to frame_font. */
5089 xassert (STRINGP (frame_font));
5090 font.name = LSTRDUPA (frame_font);
5091
5092 if (!split_font_name (f, &font, 1)
5093 || xstricmp (font.fields[XLFD_REGISTRY], "fontset") != 0
5094 || xstricmp (font.fields[XLFD_ENCODING], "startup") != 0)
5095 LFACE_FAMILY (lface) = frame_font;
5096 }
5097 else
5098 {
5099 /* Frame parameters contain a real font. Fill default face
5100 attributes from that font. */
5101 if (!set_lface_from_font_name (f, lface,
5102 XSTRING (frame_font)->data, 0))
5103 return 0;
5104
5105 /* Remember registry and encoding of the frame font. */
5106 unibyte_registry
5107 = deduce_unibyte_registry (f, XSTRING (frame_font)->data);
5108 if (STRINGP (unibyte_registry))
5109 Vface_default_registry = unibyte_registry;
5110 else
5111 Vface_default_registry = build_string ("iso8859-1");
5112 }
5113 }
5114 #endif /* HAVE_X_WINDOWS */
5115
5116 if (FRAME_TERMCAP_P (f))
5117 {
5118 LFACE_FAMILY (lface) = build_string ("default");
5119 LFACE_SWIDTH (lface) = Qnormal;
5120 LFACE_HEIGHT (lface) = make_number (1);
5121 LFACE_WEIGHT (lface) = Qnormal;
5122 LFACE_SLANT (lface) = Qnormal;
5123 }
5124
5125 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5126 LFACE_UNDERLINE (lface) = Qnil;
5127
5128 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5129 LFACE_OVERLINE (lface) = Qnil;
5130
5131 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5132 LFACE_STRIKE_THROUGH (lface) = Qnil;
5133
5134 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5135 LFACE_BOX (lface) = Qnil;
5136
5137 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5138 LFACE_INVERSE (lface) = Qnil;
5139
5140 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5141 {
5142 /* This function is called so early that colors are not yet
5143 set in the frame parameter list. */
5144 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5145
5146 if (CONSP (color) && STRINGP (XCDR (color)))
5147 LFACE_FOREGROUND (lface) = XCDR (color);
5148 else if (FRAME_X_P (f))
5149 return 0;
5150 else if (FRAME_TERMCAP_P (f))
5151 /* Frame parameters for terminal frames usually don't contain
5152 a color. Use an empty string to indicate that the face
5153 should use the (unknown) default color of the terminal. */
5154 LFACE_FOREGROUND (lface) = build_string ("");
5155 else
5156 abort ();
5157 }
5158
5159 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5160 {
5161 /* This function is called so early that colors are not yet
5162 set in the frame parameter list. */
5163 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5164 if (CONSP (color) && STRINGP (XCDR (color)))
5165 LFACE_BACKGROUND (lface) = XCDR (color);
5166 else if (FRAME_X_P (f))
5167 return 0;
5168 else if (FRAME_TERMCAP_P (f))
5169 /* Frame parameters for terminal frames usually don't contain
5170 a color. Use an empty string to indicate that the face
5171 should use the (unknown) default color of the terminal. */
5172 LFACE_BACKGROUND (lface) = build_string ("");
5173 else
5174 abort ();
5175 }
5176
5177 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5178 LFACE_STIPPLE (lface) = Qnil;
5179
5180 /* Realize the face; it must be fully-specified now. */
5181 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5182 check_lface (lface);
5183 bcopy (XVECTOR (lface)->contents, attrs, sizeof attrs);
5184 face = realize_face (c, attrs, CHARSET_ASCII);
5185
5186 /* Remove the former default face. */
5187 if (c->used > DEFAULT_FACE_ID)
5188 {
5189 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5190 uncache_face (c, default_face);
5191 free_realized_face (f, default_face);
5192 }
5193
5194 /* Insert the new default face. */
5195 cache_face (c, face, lface_hash (attrs));
5196 xassert (face->id == DEFAULT_FACE_ID);
5197 return 1;
5198 }
5199
5200
5201 /* Realize basic faces other than the default face in face cache C.
5202 SYMBOL is the face name, ID is the face id the realized face must
5203 have. The default face must have been realized already. */
5204
5205 static void
5206 realize_named_face (f, symbol, id)
5207 struct frame *f;
5208 Lisp_Object symbol;
5209 int id;
5210 {
5211 struct face_cache *c = FRAME_FACE_CACHE (f);
5212 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5213 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5214 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5215 struct face *new_face;
5216
5217 /* The default face must exist and be fully specified. */
5218 get_lface_attributes (f, Qdefault, attrs, 1);
5219 check_lface_attrs (attrs);
5220 xassert (lface_fully_specified_p (attrs));
5221
5222 /* If SYMBOL isn't know as a face, create it. */
5223 if (NILP (lface))
5224 {
5225 Lisp_Object frame;
5226 XSETFRAME (frame, f);
5227 lface = Finternal_make_lisp_face (symbol, frame);
5228 }
5229
5230 /* Merge SYMBOL's face with the default face. */
5231 get_lface_attributes (f, symbol, symbol_attrs, 1);
5232 merge_face_vectors (symbol_attrs, attrs);
5233
5234 /* Realize the face. */
5235 new_face = realize_face (c, attrs, CHARSET_ASCII);
5236
5237 /* Remove the former face. */
5238 if (c->used > id)
5239 {
5240 struct face *old_face = c->faces_by_id[id];
5241 uncache_face (c, old_face);
5242 free_realized_face (f, old_face);
5243 }
5244
5245 /* Insert the new face. */
5246 cache_face (c, new_face, lface_hash (attrs));
5247 xassert (new_face->id == id);
5248 }
5249
5250
5251 /* Realize the fully-specified face with attributes ATTRS in face
5252 cache C for character set CHARSET or for unibyte text if CHARSET <
5253 0. Value is a pointer to the newly created realized face. */
5254
5255 static struct face *
5256 realize_face (c, attrs, charset)
5257 struct face_cache *c;
5258 Lisp_Object *attrs;
5259 int charset;
5260 {
5261 struct face *face;
5262
5263 /* LFACE must be fully specified. */
5264 xassert (c != NULL);
5265 check_lface_attrs (attrs);
5266
5267 if (FRAME_X_P (c->f))
5268 face = realize_x_face (c, attrs, charset);
5269 else if (FRAME_TERMCAP_P (c->f))
5270 face = realize_tty_face (c, attrs, charset);
5271 else
5272 abort ();
5273
5274 return face;
5275 }
5276
5277
5278 /* Realize the fully-specified face with attributes ATTRS in face
5279 cache C for character set CHARSET or for unibyte text if CHARSET <
5280 0. Do it for X frame C->f. Value is a pointer to the newly
5281 created realized face. */
5282
5283 static struct face *
5284 realize_x_face (c, attrs, charset)
5285 struct face_cache *c;
5286 Lisp_Object *attrs;
5287 int charset;
5288 {
5289 #ifdef HAVE_X_WINDOWS
5290 struct face *face, *default_face;
5291 struct frame *f = c->f;
5292 Lisp_Object stipple, overline, strike_through, box;
5293 Lisp_Object unibyte_registry;
5294 struct gcpro gcpro1;
5295
5296 xassert (FRAME_X_P (f));
5297
5298 /* If realizing a face for use in unibyte text, get the X registry
5299 and encoding to use from Vface_default_registry. */
5300 if (charset < 0)
5301 unibyte_registry = (STRINGP (Vface_default_registry)
5302 ? Vface_default_registry
5303 : build_string ("iso8859-1"));
5304 else
5305 unibyte_registry = Qnil;
5306 GCPRO1 (unibyte_registry);
5307
5308 /* Allocate a new realized face. */
5309 face = make_realized_face (attrs, charset, unibyte_registry);
5310
5311 /* Determine the font to use. Most of the time, the font will be
5312 the same as the font of the default face, so try that first. */
5313 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5314 if (default_face
5315 && FACE_SUITABLE_FOR_CHARSET_P (default_face, charset)
5316 && lface_same_font_attributes_p (default_face->lface, attrs))
5317 {
5318 face->font = default_face->font;
5319 face->fontset = default_face->fontset;
5320 face->font_info_id = default_face->font_info_id;
5321 face->font_name = default_face->font_name;
5322 face->registry = default_face->registry;
5323 }
5324 else if (charset >= 0)
5325 {
5326 /* For all charsets except CHARSET_COMPOSITION, we use our own
5327 font selection functions to choose a best matching font for
5328 the specified face attributes. If the face specifies a
5329 fontset alias name, the fontset determines the font name
5330 pattern, otherwise we construct a font pattern from face
5331 attributes and charset.
5332
5333 If charset is CHARSET_COMPOSITION, we always construct a face
5334 with a fontset, even if the face doesn't specify a fontset alias
5335 (we use fontset-standard in that case). When the composite
5336 character is displayed in xterm.c, a suitable concrete font is
5337 loaded in x_get_char_font_and_encoding. */
5338
5339 char *font_name = NULL;
5340 int fontset = face_fontset (f, attrs);
5341
5342 if (charset == CHARSET_COMPOSITION)
5343 fontset = max (0, fontset);
5344 else if (fontset < 0)
5345 font_name = choose_face_font (f, attrs, charset, Qnil);
5346 else
5347 {
5348 font_name = choose_face_fontset_font (f, attrs, fontset, charset);
5349 fontset = -1;
5350 }
5351
5352 load_face_font_or_fontset (f, face, font_name, fontset);
5353 xfree (font_name);
5354 }
5355 else
5356 {
5357 /* Unibyte case, and font is not equal to that of the default
5358 face. UNIBYTE_REGISTRY is the X registry and encoding the
5359 font should have. What is a reasonable thing to do if the
5360 user specified a fontset alias name for the face in this
5361 case? We choose a font by taking the ASCII font of the
5362 fontset, but using UNIBYTE_REGISTRY for its registry and
5363 encoding. */
5364
5365 char *font_name = NULL;
5366 int fontset = face_fontset (f, attrs);
5367
5368 if (fontset < 0)
5369 font_name = choose_face_font (f, attrs, charset, unibyte_registry);
5370 else
5371 font_name = choose_face_fontset_font (f, attrs, fontset, charset);
5372
5373 load_face_font_or_fontset (f, face, font_name, -1);
5374 xfree (font_name);
5375 }
5376
5377 /* Load colors, and set remaining attributes. */
5378
5379 load_face_colors (f, face, attrs);
5380
5381 /* Set up box. */
5382 box = attrs[LFACE_BOX_INDEX];
5383 if (STRINGP (box))
5384 {
5385 /* A simple box of line width 1 drawn in color given by
5386 the string. */
5387 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5388 LFACE_BOX_INDEX);
5389 face->box = FACE_SIMPLE_BOX;
5390 face->box_line_width = 1;
5391 }
5392 else if (INTEGERP (box))
5393 {
5394 /* Simple box of specified line width in foreground color of the
5395 face. */
5396 xassert (XINT (box) > 0);
5397 face->box = FACE_SIMPLE_BOX;
5398 face->box_line_width = XFASTINT (box);
5399 face->box_color = face->foreground;
5400 face->box_color_defaulted_p = 1;
5401 }
5402 else if (CONSP (box))
5403 {
5404 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5405 being one of `raised' or `sunken'. */
5406 face->box = FACE_SIMPLE_BOX;
5407 face->box_color = face->foreground;
5408 face->box_color_defaulted_p = 1;
5409 face->box_line_width = 1;
5410
5411 while (CONSP (box))
5412 {
5413 Lisp_Object keyword, value;
5414
5415 keyword = XCAR (box);
5416 box = XCDR (box);
5417
5418 if (!CONSP (box))
5419 break;
5420 value = XCAR (box);
5421 box = XCDR (box);
5422
5423 if (EQ (keyword, QCline_width))
5424 {
5425 if (INTEGERP (value) && XINT (value) > 0)
5426 face->box_line_width = XFASTINT (value);
5427 }
5428 else if (EQ (keyword, QCcolor))
5429 {
5430 if (STRINGP (value))
5431 {
5432 face->box_color = load_color (f, face, value,
5433 LFACE_BOX_INDEX);
5434 face->use_box_color_for_shadows_p = 1;
5435 }
5436 }
5437 else if (EQ (keyword, QCstyle))
5438 {
5439 if (EQ (value, Qreleased_button))
5440 face->box = FACE_RAISED_BOX;
5441 else if (EQ (value, Qpressed_button))
5442 face->box = FACE_SUNKEN_BOX;
5443 }
5444 }
5445 }
5446
5447 /* Text underline, overline, strike-through. */
5448
5449 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5450 {
5451 /* Use default color (same as foreground color). */
5452 face->underline_p = 1;
5453 face->underline_defaulted_p = 1;
5454 face->underline_color = 0;
5455 }
5456 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
5457 {
5458 /* Use specified color. */
5459 face->underline_p = 1;
5460 face->underline_defaulted_p = 0;
5461 face->underline_color
5462 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5463 LFACE_UNDERLINE_INDEX);
5464 }
5465 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
5466 {
5467 face->underline_p = 0;
5468 face->underline_defaulted_p = 0;
5469 face->underline_color = 0;
5470 }
5471
5472 overline = attrs[LFACE_OVERLINE_INDEX];
5473 if (STRINGP (overline))
5474 {
5475 face->overline_color
5476 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5477 LFACE_OVERLINE_INDEX);
5478 face->overline_p = 1;
5479 }
5480 else if (EQ (overline, Qt))
5481 {
5482 face->overline_color = face->foreground;
5483 face->overline_color_defaulted_p = 1;
5484 face->overline_p = 1;
5485 }
5486
5487 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5488 if (STRINGP (strike_through))
5489 {
5490 face->strike_through_color
5491 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5492 LFACE_STRIKE_THROUGH_INDEX);
5493 face->strike_through_p = 1;
5494 }
5495 else if (EQ (strike_through, Qt))
5496 {
5497 face->strike_through_color = face->foreground;
5498 face->strike_through_color_defaulted_p = 1;
5499 face->strike_through_p = 1;
5500 }
5501
5502 stipple = attrs[LFACE_STIPPLE_INDEX];
5503 if (!NILP (stipple))
5504 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5505
5506 UNGCPRO;
5507 xassert (face->fontset < 0 || face->charset == CHARSET_COMPOSITION);
5508 xassert (FACE_SUITABLE_FOR_CHARSET_P (face, charset));
5509 return face;
5510 #endif /* HAVE_X_WINDOWS */
5511 }
5512
5513
5514 /* Realize the fully-specified face with attributes ATTRS in face
5515 cache C for character set CHARSET or for unibyte text if CHARSET <
5516 0. Do it for TTY frame C->f. Value is a pointer to the newly
5517 created realized face. */
5518
5519 static struct face *
5520 realize_tty_face (c, attrs, charset)
5521 struct face_cache *c;
5522 Lisp_Object *attrs;
5523 int charset;
5524 {
5525 struct face *face;
5526 int weight, slant;
5527 Lisp_Object color;
5528
5529 /* Frame must be a termcap frame. */
5530 xassert (FRAME_TERMCAP_P (c->f));
5531
5532 /* Allocate a new realized face. */
5533 face = make_realized_face (attrs, charset, Qnil);
5534 face->font_name = "tty";
5535
5536 /* Map face attributes to TTY appearances. We map slant to
5537 dimmed text because we want italic text to appear differently
5538 and because dimmed text is probably used infrequently. */
5539 weight = face_numeric_weight (attrs[LFACE_WEIGHT_INDEX]);
5540 slant = face_numeric_slant (attrs[LFACE_SLANT_INDEX]);
5541
5542 if (weight > XLFD_WEIGHT_MEDIUM)
5543 face->tty_bold_p = 1;
5544 if (weight < XLFD_WEIGHT_MEDIUM || slant != XLFD_SLANT_ROMAN)
5545 face->tty_dim_p = 1;
5546 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5547 face->tty_underline_p = 1;
5548 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5549 face->tty_reverse_p = 1;
5550
5551 /* Map color names to color indices. */
5552 face->foreground = face->background = FACE_TTY_DEFAULT_COLOR;
5553
5554 color = attrs[LFACE_FOREGROUND_INDEX];
5555 if (XSTRING (color)->size
5556 && (color = Fassoc (color, Vface_tty_color_alist),
5557 CONSP (color)))
5558 face->foreground = XINT (XCDR (color));
5559
5560 color = attrs[LFACE_BACKGROUND_INDEX];
5561 if (XSTRING (color)->size
5562 && (color = Fassoc (color, Vface_tty_color_alist),
5563 CONSP (color)))
5564 face->background = XINT (XCDR (color));
5565
5566 return face;
5567 }
5568
5569
5570 DEFUN ("face-register-tty-color", Fface_register_tty_color,
5571 Sface_register_tty_color, 2, 2, 0,
5572 "Say that COLOR is color number NUMBER on the terminal.\n\
5573 COLOR is a string, the color name. Value is COLOR.")
5574 (color, number)
5575 Lisp_Object color, number;
5576 {
5577 Lisp_Object entry;
5578
5579 CHECK_STRING (color, 0);
5580 CHECK_NUMBER (number, 1);
5581 entry = Fassoc (color, Vface_tty_color_alist);
5582 if (NILP (entry))
5583 Vface_tty_color_alist = Fcons (Fcons (color, number),
5584 Vface_tty_color_alist);
5585 else
5586 Fsetcdr (entry, number);
5587 return color;
5588 }
5589
5590
5591 DEFUN ("face-clear-tty-colors", Fface_clear_tty_colors,
5592 Sface_clear_tty_colors, 0, 0, 0,
5593 "Unregister all registered tty colors.")
5594 ()
5595 {
5596 return Vface_tty_color_alist = Qnil;
5597 }
5598
5599
5600 DEFUN ("tty-defined-colors", Ftty_defined_colors,
5601 Stty_defined_colors, 0, 0, 0,
5602 "Return a list of registered tty colors.")
5603 ()
5604 {
5605 Lisp_Object list, colors;
5606
5607 colors = Qnil;
5608 for (list = Vface_tty_color_alist; CONSP (list); list = XCDR (list))
5609 colors = Fcons (XCAR (XCAR (list)), colors);
5610
5611 return colors;
5612 }
5613
5614
5615 \f
5616 /***********************************************************************
5617 Computing Faces
5618 ***********************************************************************/
5619
5620 /* Return the ID of the face to use to display character CH with face
5621 property PROP on frame F in current_buffer. */
5622
5623 int
5624 compute_char_face (f, ch, prop)
5625 struct frame *f;
5626 int ch;
5627 Lisp_Object prop;
5628 {
5629 int face_id;
5630 int charset = (NILP (current_buffer->enable_multibyte_characters)
5631 ? -1
5632 : CHAR_CHARSET (ch));
5633
5634 if (NILP (prop))
5635 face_id = FACE_FOR_CHARSET (f, DEFAULT_FACE_ID, charset);
5636 else
5637 {
5638 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5639 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5640 bcopy (default_face->lface, attrs, sizeof attrs);
5641 merge_face_vector_with_property (f, attrs, prop);
5642 face_id = lookup_face (f, attrs, charset);
5643 }
5644
5645 return face_id;
5646 }
5647
5648
5649 /* Return the face ID associated with buffer position POS for
5650 displaying ASCII characters. Return in *ENDPTR the position at
5651 which a different face is needed, as far as text properties and
5652 overlays are concerned. W is a window displaying current_buffer.
5653
5654 REGION_BEG, REGION_END delimit the region, so it can be
5655 highlighted.
5656
5657 LIMIT is a position not to scan beyond. That is to limit the time
5658 this function can take.
5659
5660 If MOUSE is non-zero, use the character's mouse-face, not its face.
5661
5662 The face returned is suitable for displaying CHARSET_ASCII if
5663 current_buffer->enable_multibyte_characters is non-nil. Otherwise,
5664 the face is suitable for displaying unibyte text. */
5665
5666 int
5667 face_at_buffer_position (w, pos, region_beg, region_end,
5668 endptr, limit, mouse)
5669 struct window *w;
5670 int pos;
5671 int region_beg, region_end;
5672 int *endptr;
5673 int limit;
5674 int mouse;
5675 {
5676 struct frame *f = XFRAME (w->frame);
5677 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5678 Lisp_Object prop, position;
5679 int i, noverlays;
5680 Lisp_Object *overlay_vec;
5681 Lisp_Object frame;
5682 int endpos;
5683 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5684 Lisp_Object limit1, end;
5685 struct face *default_face;
5686 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5687
5688 /* W must display the current buffer. We could write this function
5689 to use the frame and buffer of W, but right now it doesn't. */
5690 xassert (XBUFFER (w->buffer) == current_buffer);
5691
5692 XSETFRAME (frame, f);
5693 XSETFASTINT (position, pos);
5694
5695 endpos = ZV;
5696 if (pos < region_beg && region_beg < endpos)
5697 endpos = region_beg;
5698
5699 /* Get the `face' or `mouse_face' text property at POS, and
5700 determine the next position at which the property changes. */
5701 prop = Fget_text_property (position, propname, w->buffer);
5702 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5703 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
5704 if (INTEGERP (end))
5705 endpos = XINT (end);
5706
5707 /* Look at properties from overlays. */
5708 {
5709 int next_overlay;
5710 int len;
5711
5712 /* First try with room for 40 overlays. */
5713 len = 40;
5714 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
5715 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
5716 &next_overlay, NULL);
5717
5718 /* If there are more than 40, make enough space for all, and try
5719 again. */
5720 if (noverlays > len)
5721 {
5722 len = noverlays;
5723 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
5724 noverlays = overlays_at (pos, 0, &overlay_vec, &len,
5725 &next_overlay, NULL);
5726 }
5727
5728 if (next_overlay < endpos)
5729 endpos = next_overlay;
5730 }
5731
5732 *endptr = endpos;
5733
5734 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5735
5736 /* Optimize common cases where we can use the default face. */
5737 if (noverlays == 0
5738 && NILP (prop)
5739 && !(pos >= region_beg && pos < region_end)
5740 && (multibyte_p
5741 || !FRAME_WINDOW_P (f)
5742 || FACE_SUITABLE_FOR_CHARSET_P (default_face, -1)))
5743 return DEFAULT_FACE_ID;
5744
5745 /* Begin with attributes from the default face. */
5746 bcopy (default_face->lface, attrs, sizeof attrs);
5747
5748 /* Merge in attributes specified via text properties. */
5749 if (!NILP (prop))
5750 merge_face_vector_with_property (f, attrs, prop);
5751
5752 /* Now merge the overlay data. */
5753 noverlays = sort_overlays (overlay_vec, noverlays, w);
5754 for (i = 0; i < noverlays; i++)
5755 {
5756 Lisp_Object oend;
5757 int oendpos;
5758
5759 prop = Foverlay_get (overlay_vec[i], propname);
5760 if (!NILP (prop))
5761 merge_face_vector_with_property (f, attrs, prop);
5762
5763 oend = OVERLAY_END (overlay_vec[i]);
5764 oendpos = OVERLAY_POSITION (oend);
5765 if (oendpos < endpos)
5766 endpos = oendpos;
5767 }
5768
5769 /* If in the region, merge in the region face. */
5770 if (pos >= region_beg && pos < region_end)
5771 {
5772 Lisp_Object region_face = lface_from_face_name (f, Qregion, 0);
5773 merge_face_vectors (XVECTOR (region_face)->contents, attrs);
5774
5775 if (region_end < endpos)
5776 endpos = region_end;
5777 }
5778
5779 *endptr = endpos;
5780
5781 /* Look up a realized face with the given face attributes,
5782 or realize a new one. Charset is ignored for tty frames. */
5783 return lookup_face (f, attrs, multibyte_p ? CHARSET_ASCII : -1);
5784 }
5785
5786
5787 /* Compute the face at character position POS in Lisp string STRING on
5788 window W, for charset CHARSET_ASCII.
5789
5790 If STRING is an overlay string, it comes from position BUFPOS in
5791 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
5792 not an overlay string. W must display the current buffer.
5793 REGION_BEG and REGION_END give the start and end positions of the
5794 region; both are -1 if no region is visible. BASE_FACE_ID is the
5795 id of the basic face to merge with. It is usually equal to
5796 DEFAULT_FACE_ID but can be MODE_LINE_FACE_ID or TOP_LINE_FACE_ID
5797 for strings displayed in the mode or top line.
5798
5799 Set *ENDPTR to the next position where to check for faces in
5800 STRING; -1 if the face is constant from POS to the end of the
5801 string.
5802
5803 Value is the id of the face to use. The face returned is suitable
5804 for displaying CHARSET_ASCII if STRING is multibyte. Otherwise,
5805 the face is suitable for displaying unibyte text. */
5806
5807 int
5808 face_at_string_position (w, string, pos, bufpos, region_beg,
5809 region_end, endptr, base_face_id)
5810 struct window *w;
5811 Lisp_Object string;
5812 int pos, bufpos;
5813 int region_beg, region_end;
5814 int *endptr;
5815 enum face_id base_face_id;
5816 {
5817 Lisp_Object prop, position, end, limit;
5818 struct frame *f = XFRAME (WINDOW_FRAME (w));
5819 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5820 struct face *base_face;
5821 int multibyte_p = STRING_MULTIBYTE (string);
5822
5823 /* Get the value of the face property at the current position within
5824 STRING. Value is nil if there is no face property. */
5825 XSETFASTINT (position, pos);
5826 prop = Fget_text_property (position, Qface, string);
5827
5828 /* Get the next position at which to check for faces. Value of end
5829 is nil if face is constant all the way to the end of the string.
5830 Otherwise it is a string position where to check faces next.
5831 Limit is the maximum position up to which to check for property
5832 changes in Fnext_single_property_change. Strings are usually
5833 short, so set the limit to the end of the string. */
5834 XSETFASTINT (limit, XSTRING (string)->size);
5835 end = Fnext_single_property_change (position, Qface, string, limit);
5836 if (INTEGERP (end))
5837 *endptr = XFASTINT (end);
5838 else
5839 *endptr = -1;
5840
5841 base_face = FACE_FROM_ID (f, base_face_id);
5842 xassert (base_face);
5843
5844 /* Optimize the default case that there is no face property and we
5845 are not in the region. */
5846 if (NILP (prop)
5847 && (base_face_id != DEFAULT_FACE_ID
5848 /* BUFPOS <= 0 means STRING is not an overlay string, so
5849 that the region doesn't have to be taken into account. */
5850 || bufpos <= 0
5851 || bufpos < region_beg
5852 || bufpos >= region_end)
5853 && (multibyte_p
5854 /* We can't realize faces for different charsets differently
5855 if we don't have fonts, so we can stop here if not working
5856 on a window-system frame. */
5857 || !FRAME_WINDOW_P (f)
5858 || FACE_SUITABLE_FOR_CHARSET_P (base_face, -1)))
5859 return base_face->id;
5860
5861 /* Begin with attributes from the base face. */
5862 bcopy (base_face->lface, attrs, sizeof attrs);
5863
5864 /* Merge in attributes specified via text properties. */
5865 if (!NILP (prop))
5866 merge_face_vector_with_property (f, attrs, prop);
5867
5868 /* If in the region, merge in the region face. */
5869 if (bufpos
5870 && bufpos >= region_beg
5871 && bufpos < region_end)
5872 {
5873 Lisp_Object region_face = lface_from_face_name (f, Qregion, 0);
5874 merge_face_vectors (XVECTOR (region_face)->contents, attrs);
5875 }
5876
5877 /* Look up a realized face with the given face attributes,
5878 or realize a new one. */
5879 return lookup_face (f, attrs, multibyte_p ? CHARSET_ASCII : -1);
5880 }
5881
5882
5883 \f
5884 /***********************************************************************
5885 Tests
5886 ***********************************************************************/
5887
5888 #if GLYPH_DEBUG
5889
5890 /* Print the contents of the realized face FACE to stderr. */
5891
5892 static void
5893 dump_realized_face (face)
5894 struct face *face;
5895 {
5896 fprintf (stderr, "ID: %d\n", face->id);
5897 #ifdef HAVE_X_WINDOWS
5898 fprintf (stderr, "gc: %d\n", (int) face->gc);
5899 #endif
5900 fprintf (stderr, "foreground: 0x%lx (%s)\n",
5901 face->foreground,
5902 XSTRING (face->lface[LFACE_FOREGROUND_INDEX])->data);
5903 fprintf (stderr, "background: 0x%lx (%s)\n",
5904 face->background,
5905 XSTRING (face->lface[LFACE_BACKGROUND_INDEX])->data);
5906 fprintf (stderr, "font_name: %s (%s)\n",
5907 face->font_name,
5908 XSTRING (face->lface[LFACE_FAMILY_INDEX])->data);
5909 #ifdef HAVE_X_WINDOWS
5910 fprintf (stderr, "font = %p\n", face->font);
5911 #endif
5912 fprintf (stderr, "font_info_id = %d\n", face->font_info_id);
5913 fprintf (stderr, "fontset: %d\n", face->fontset);
5914 fprintf (stderr, "underline: %d (%s)\n",
5915 face->underline_p,
5916 XSTRING (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX]))->data);
5917 fprintf (stderr, "hash: %d\n", face->hash);
5918 fprintf (stderr, "charset: %d\n", face->charset);
5919 }
5920
5921
5922 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, "")
5923 (n)
5924 Lisp_Object n;
5925 {
5926 if (NILP (n))
5927 {
5928 int i;
5929
5930 fprintf (stderr, "font selection order: ");
5931 for (i = 0; i < DIM (font_sort_order); ++i)
5932 fprintf (stderr, "%d ", font_sort_order[i]);
5933 fprintf (stderr, "\n");
5934
5935 fprintf (stderr, "alternative fonts: ");
5936 debug_print (Vface_alternative_font_family_alist);
5937 fprintf (stderr, "\n");
5938
5939 for (i = 0; i < FRAME_FACE_CACHE (selected_frame)->used; ++i)
5940 Fdump_face (make_number (i));
5941 }
5942 else
5943 {
5944 struct face *face;
5945 CHECK_NUMBER (n, 0);
5946 face = FACE_FROM_ID (selected_frame, XINT (n));
5947 if (face == NULL)
5948 error ("Not a valid face");
5949 dump_realized_face (face);
5950 }
5951
5952 return Qnil;
5953 }
5954
5955
5956 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
5957 0, 0, 0, "")
5958 ()
5959 {
5960 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
5961 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
5962 fprintf (stderr, "number of GCs = %d\n", ngcs);
5963 return Qnil;
5964 }
5965
5966 #endif /* GLYPH_DEBUG != 0 */
5967
5968
5969 \f
5970 /***********************************************************************
5971 Initialization
5972 ***********************************************************************/
5973
5974 void
5975 syms_of_xfaces ()
5976 {
5977 Qface = intern ("face");
5978 staticpro (&Qface);
5979 Qpixmap_spec_p = intern ("pixmap-spec-p");
5980 staticpro (&Qpixmap_spec_p);
5981
5982 /* Lisp face attribute keywords. */
5983 QCfamily = intern (":family");
5984 staticpro (&QCfamily);
5985 QCheight = intern (":height");
5986 staticpro (&QCheight);
5987 QCweight = intern (":weight");
5988 staticpro (&QCweight);
5989 QCslant = intern (":slant");
5990 staticpro (&QCslant);
5991 QCunderline = intern (":underline");
5992 staticpro (&QCunderline);
5993 QCinverse_video = intern (":inverse-video");
5994 staticpro (&QCinverse_video);
5995 QCreverse_video = intern (":reverse-video");
5996 staticpro (&QCreverse_video);
5997 QCforeground = intern (":foreground");
5998 staticpro (&QCforeground);
5999 QCbackground = intern (":background");
6000 staticpro (&QCbackground);
6001 QCstipple = intern (":stipple");;
6002 staticpro (&QCstipple);
6003 QCwidth = intern (":width");
6004 staticpro (&QCwidth);
6005 QCfont = intern (":font");
6006 staticpro (&QCfont);
6007 QCbold = intern (":bold");
6008 staticpro (&QCbold);
6009 QCitalic = intern (":italic");
6010 staticpro (&QCitalic);
6011 QCoverline = intern (":overline");
6012 staticpro (&QCoverline);
6013 QCstrike_through = intern (":strike-through");
6014 staticpro (&QCstrike_through);
6015 QCbox = intern (":box");
6016 staticpro (&QCbox);
6017
6018 /* Symbols used for Lisp face attribute values. */
6019 QCcolor = intern (":color");
6020 staticpro (&QCcolor);
6021 QCline_width = intern (":line-width");
6022 staticpro (&QCline_width);
6023 QCstyle = intern (":style");
6024 staticpro (&QCstyle);
6025 Qreleased_button = intern ("released-button");
6026 staticpro (&Qreleased_button);
6027 Qpressed_button = intern ("pressed-button");
6028 staticpro (&Qpressed_button);
6029 Qnormal = intern ("normal");
6030 staticpro (&Qnormal);
6031 Qultra_light = intern ("ultra-light");
6032 staticpro (&Qultra_light);
6033 Qextra_light = intern ("extra-light");
6034 staticpro (&Qextra_light);
6035 Qlight = intern ("light");
6036 staticpro (&Qlight);
6037 Qsemi_light = intern ("semi-light");
6038 staticpro (&Qsemi_light);
6039 Qsemi_bold = intern ("semi-bold");
6040 staticpro (&Qsemi_bold);
6041 Qbold = intern ("bold");
6042 staticpro (&Qbold);
6043 Qextra_bold = intern ("extra-bold");
6044 staticpro (&Qextra_bold);
6045 Qultra_bold = intern ("ultra-bold");
6046 staticpro (&Qultra_bold);
6047 Qoblique = intern ("oblique");
6048 staticpro (&Qoblique);
6049 Qitalic = intern ("italic");
6050 staticpro (&Qitalic);
6051 Qreverse_oblique = intern ("reverse-oblique");
6052 staticpro (&Qreverse_oblique);
6053 Qreverse_italic = intern ("reverse-italic");
6054 staticpro (&Qreverse_italic);
6055 Qultra_condensed = intern ("ultra-condensed");
6056 staticpro (&Qultra_condensed);
6057 Qextra_condensed = intern ("extra-condensed");
6058 staticpro (&Qextra_condensed);
6059 Qcondensed = intern ("condensed");
6060 staticpro (&Qcondensed);
6061 Qsemi_condensed = intern ("semi-condensed");
6062 staticpro (&Qsemi_condensed);
6063 Qsemi_expanded = intern ("semi-expanded");
6064 staticpro (&Qsemi_expanded);
6065 Qexpanded = intern ("expanded");
6066 staticpro (&Qexpanded);
6067 Qextra_expanded = intern ("extra-expanded");
6068 staticpro (&Qextra_expanded);
6069 Qultra_expanded = intern ("ultra-expanded");
6070 staticpro (&Qultra_expanded);
6071 Qbackground_color = intern ("background-color");
6072 staticpro (&Qbackground_color);
6073 Qforeground_color = intern ("foreground-color");
6074 staticpro (&Qforeground_color);
6075 Qunspecified = intern ("unspecified");
6076 staticpro (&Qunspecified);
6077
6078 Qx_charset_registry = intern ("x-charset-registry");
6079 staticpro (&Qx_charset_registry);
6080 Qdefault = intern ("default");
6081 staticpro (&Qdefault);
6082 Qmodeline = intern ("modeline");
6083 staticpro (&Qmodeline);
6084 Qtoolbar = intern ("toolbar");
6085 staticpro (&Qtoolbar);
6086 Qregion = intern ("region");
6087 staticpro (&Qregion);
6088 Qbitmap_area = intern ("bitmap-area");
6089 staticpro (&Qbitmap_area);
6090 Qtop_line = intern ("top-line");
6091 staticpro (&Qtop_line);
6092
6093 defsubr (&Sinternal_make_lisp_face);
6094 defsubr (&Sinternal_lisp_face_p);
6095 defsubr (&Sinternal_set_lisp_face_attribute);
6096 #ifdef HAVE_X_WINDOWS
6097 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6098 defsubr (&Sface_color_gray_p);
6099 defsubr (&Sface_color_supported_p);
6100 #endif
6101 defsubr (&Sinternal_get_lisp_face_attribute);
6102 defsubr (&Sinternal_lisp_face_attribute_values);
6103 defsubr (&Sinternal_lisp_face_equal_p);
6104 defsubr (&Sinternal_lisp_face_empty_p);
6105 defsubr (&Sinternal_copy_lisp_face);
6106 defsubr (&Sinternal_merge_in_global_face);
6107 defsubr (&Sface_font);
6108 defsubr (&Sframe_face_alist);
6109 defsubr (&Sinternal_set_font_selection_order);
6110 defsubr (&Sinternal_set_alternative_font_family_alist);
6111 #if GLYPH_DEBUG
6112 defsubr (&Sdump_face);
6113 defsubr (&Sshow_face_resources);
6114 #endif /* GLYPH_DEBUG */
6115 defsubr (&Sclear_face_cache);
6116
6117 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
6118 "List of global face definitions (for internal use only.)");
6119 Vface_new_frame_defaults = Qnil;
6120
6121 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
6122 "*Default stipple pattern used on monochrome displays.\n\
6123 This stipple pattern is used on monochrome displays\n\
6124 instead of shades of gray for a face background color.\n\
6125 See `set-face-stipple' for possible values for this variable.");
6126 Vface_default_stipple = build_string ("gray3");
6127
6128 DEFVAR_LISP ("face-default-registry", &Vface_default_registry,
6129 "Default registry and encoding to use.\n\
6130 This registry and encoding is used for unibyte text. It is set up\n\
6131 from the specified frame font when Emacs starts. (For internal use only.)");
6132 Vface_default_registry = Qnil;
6133
6134 DEFVAR_LISP ("face-alternative-font-family-alist",
6135 &Vface_alternative_font_family_alist, "");
6136 Vface_alternative_font_family_alist = Qnil;
6137
6138 #if SCALABLE_FONTS
6139
6140 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
6141 "Allowed scalable fonts.\n\
6142 A value of nil means don't allow any scalable fonts.\n\
6143 A value of t means allow any scalable font.\n\
6144 Otherwise, value must be a list of regular expressions. A font may be\n\
6145 scaled if its name matches a regular expression in the list.");
6146 Vscalable_fonts_allowed = Qnil;
6147
6148 #endif /* SCALABLE_FONTS */
6149
6150 #ifdef HAVE_X_WINDOWS
6151 defsubr (&Spixmap_spec_p);
6152 defsubr (&Sx_list_fonts);
6153 defsubr (&Sinternal_face_x_get_resource);
6154 defsubr (&Sx_font_list);
6155 defsubr (&Sx_font_family_list);
6156 #endif /* HAVE_X_WINDOWS */
6157
6158 /* TTY face support. */
6159 defsubr (&Sface_register_tty_color);
6160 defsubr (&Sface_clear_tty_colors);
6161 defsubr (&Stty_defined_colors);
6162 Vface_tty_color_alist = Qnil;
6163 staticpro (&Vface_tty_color_alist);
6164 }