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