Merge from emacs-23; up to 2010-06-10T12:56:11Z!michael.albinus@gmx.de.
[bpt/emacs.git] / src / fontset.c
1 /* Fontset handler.
2
3 Copyright (C) 2001-2011 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
8 Copyright (C) 2003, 2006
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
11
12 This file is part of GNU Emacs.
13
14 GNU Emacs is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* #define FONTSET_DEBUG */
28
29 #include <config.h>
30 #include <stdio.h>
31 #include <setjmp.h>
32
33 #include "lisp.h"
34 #include "blockinput.h"
35 #include "buffer.h"
36 #include "character.h"
37 #include "charset.h"
38 #include "ccl.h"
39 #include "keyboard.h"
40 #include "frame.h"
41 #include "dispextern.h"
42 #include "intervals.h"
43 #include "fontset.h"
44 #include "window.h"
45 #ifdef HAVE_X_WINDOWS
46 #include "xterm.h"
47 #endif
48 #ifdef WINDOWSNT
49 #include "w32term.h"
50 #endif
51 #ifdef HAVE_NS
52 #include "nsterm.h"
53 #endif
54 #include "termhooks.h"
55
56 #include "font.h"
57
58 #undef xassert
59 #ifdef FONTSET_DEBUG
60 #define xassert(X) do {if (!(X)) abort ();} while (0)
61 #undef INLINE
62 #define INLINE
63 #else /* not FONTSET_DEBUG */
64 #define xassert(X) (void) 0
65 #endif /* not FONTSET_DEBUG */
66
67 /* FONTSET
68
69 A fontset is a collection of font related information to give
70 similar appearance (style, etc) of characters. A fontset has two
71 roles. One is to use for the frame parameter `font' as if it is an
72 ASCII font. In that case, Emacs uses the font specified for
73 `ascii' script for the frame's default font.
74
75 Another role, the more important one, is to provide information
76 about which font to use for each non-ASCII character.
77
78 There are two kinds of fontsets; base and realized. A base fontset
79 is created by `new-fontset' from Emacs Lisp explicitly. A realized
80 fontset is created implicitly when a face is realized for ASCII
81 characters. A face is also realized for non-ASCII characters based
82 on an ASCII face. All of non-ASCII faces based on the same ASCII
83 face share the same realized fontset.
84
85 A fontset object is implemented by a char-table whose default value
86 and parent are always nil.
87
88 An element of a base fontset is a vector of FONT-DEFs which itself
89 is a vector [ FONT-SPEC ENCODING REPERTORY ].
90
91 An element of a realized fontset is nil, t, 0, or a vector of this
92 form:
93
94 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
95 RFONT-DEF0 RFONT-DEF1 ... ]
96
97 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
98
99 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
100
101 RFONT-DEFn are automatically reordered by the current charset
102 priority list.
103
104 The value nil means that we have not yet generated the above vector
105 from the base of the fontset.
106
107 The value t means that no font is available for the corresponding
108 range of characters.
109
110 The value 0 means that no font is available for the corresponding
111 range of characters in this fontset, but may be available in the
112 default fontset.
113
114
115 A fontset has 9 extra slots.
116
117 The 1st slot: the ID number of the fontset
118
119 The 2nd slot:
120 base: the name of the fontset
121 realized: nil
122
123 The 3rd slot:
124 base: nil
125 realized: the base fontset
126
127 The 4th slot:
128 base: nil
129 realized: the frame that the fontset belongs to
130
131 The 5th slot:
132 base: the font name for ASCII characters
133 realized: nil
134
135 The 6th slot:
136 base: nil
137 realized: the ID number of a face to use for characters that
138 has no font in a realized fontset.
139
140 The 7th slot:
141 base: nil
142 realized: Alist of font index vs the corresponding repertory
143 char-table.
144
145 The 8th slot:
146 base: nil
147 realized: If the base is not the default fontset, a fontset
148 realized from the default fontset, else nil.
149
150 The 9th slot:
151 base: Same as element value (but for fallback fonts).
152 realized: Likewise.
153
154 All fontsets are recorded in the vector Vfontset_table.
155
156
157 DEFAULT FONTSET
158
159 There's a special base fontset named `default fontset' which
160 defines the default font specifications. When a base fontset
161 doesn't specify a font for a specific character, the corresponding
162 value in the default fontset is used.
163
164 The parent of a realized fontset created for such a face that has
165 no fontset is the default fontset.
166
167
168 These structures are hidden from the other codes than this file.
169 The other codes handle fontsets only by their ID numbers. They
170 usually use the variable name `fontset' for IDs. But, in this
171 file, we always use varialbe name `id' for IDs, and name `fontset'
172 for an actual fontset object, i.e., char-table.
173
174 */
175
176 /********** VARIABLES and FUNCTION PROTOTYPES **********/
177
178 static Lisp_Object Qfontset;
179 static Lisp_Object Qfontset_info;
180 static Lisp_Object Qprepend, Qappend;
181 Lisp_Object Qlatin;
182
183 /* Vector containing all fontsets. */
184 static Lisp_Object Vfontset_table;
185
186 /* Next possibly free fontset ID. Usually this keeps the minimum
187 fontset ID not yet used. */
188 static int next_fontset_id;
189
190 /* The default fontset. This gives default FAMILY and REGISTRY of
191 font for each character. */
192 static Lisp_Object Vdefault_fontset;
193
194 /* Check if any window system is used now. */
195 void (*check_window_system_func) (void);
196
197
198 /* Prototype declarations for static functions. */
199 static Lisp_Object fontset_add (Lisp_Object, Lisp_Object, Lisp_Object,
200 Lisp_Object);
201 static Lisp_Object fontset_find_font (Lisp_Object, int, struct face *,
202 int, int);
203 static void reorder_font_vector (Lisp_Object, struct font *);
204 static Lisp_Object fontset_font (Lisp_Object, int, struct face *, int);
205 static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
206 static Lisp_Object fontset_pattern_regexp (Lisp_Object);
207 static void accumulate_script_ranges (Lisp_Object, Lisp_Object,
208 Lisp_Object);
209 static void set_fontset_font (Lisp_Object, Lisp_Object);
210
211 #ifdef FONTSET_DEBUG
212
213 /* Return 1 if ID is a valid fontset id, else return 0. */
214
215 static int
216 fontset_id_valid_p (int id)
217 {
218 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
219 }
220
221 #endif
222
223
224 \f
225 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
226
227 /* Return the fontset with ID. No check of ID's validness. */
228 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
229
230 /* Macros to access special values of FONTSET. */
231 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
232
233 /* Macros to access special values of (base) FONTSET. */
234 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
235 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
236 /* #define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5] */
237
238 /* Macros to access special values of (realized) FONTSET. */
239 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
240 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
241 /* #define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4] */
242 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
243 /* #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6] */
244 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
245
246 /* For both base and realized fontset. */
247 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
248
249 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
250
251
252 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
253 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
254 do { \
255 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
256 ASET ((font_def), 1, encoding); \
257 ASET ((font_def), 2, repertory); \
258 } while (0)
259
260 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
261 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
262 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
263
264 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
265 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
266 ASET ((rfont_def), 0, make_number (face_id))
267 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
268 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
269 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
270 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
271 ASET ((rfont_def), 2, (object))
272 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
273 the order of listing by font backends, the higher bits represents
274 the order given by charset priority list. The smaller value is
275 preferable. */
276 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
277 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
278 ASET ((rfont_def), 3, make_number (score))
279 #define RFONT_DEF_NEW(rfont_def, font_def) \
280 do { \
281 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
282 ASET ((rfont_def), 1, (font_def)); \
283 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
284 } while (0)
285
286
287 /* Return the element of FONTSET for the character C. If FONTSET is a
288 base fontset other then the default fontset and FONTSET doesn't
289 contain information for C, return the information in the default
290 fontset. */
291
292 #define FONTSET_REF(fontset, c) \
293 (EQ (fontset, Vdefault_fontset) \
294 ? CHAR_TABLE_REF (fontset, c) \
295 : fontset_ref ((fontset), (c)))
296
297 static Lisp_Object
298 fontset_ref (Lisp_Object fontset, int c)
299 {
300 Lisp_Object elt;
301
302 elt = CHAR_TABLE_REF (fontset, c);
303 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
304 /* Don't check Vdefault_fontset for a realized fontset. */
305 && NILP (FONTSET_BASE (fontset)))
306 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
307 return elt;
308 }
309
310 /* Set elements of FONTSET for characters in RANGE to the value ELT.
311 RANGE is a cons (FROM . TO), where FROM and TO are character codes
312 specifying a range. */
313
314 #define FONTSET_SET(fontset, range, elt) \
315 Fset_char_table_range ((fontset), (range), (elt))
316
317
318 /* Modify the elements of FONTSET for characters in RANGE by replacing
319 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
320 and TO are character codes specifying a range. If ADD is nil,
321 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
322 append ELT. */
323
324 #define FONTSET_ADD(fontset, range, elt, add) \
325 (NILP (add) \
326 ? (NILP (range) \
327 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
328 : Fset_char_table_range ((fontset), (range), \
329 Fmake_vector (make_number (1), (elt)))) \
330 : fontset_add ((fontset), (range), (elt), (add)))
331
332 static Lisp_Object
333 fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
334 {
335 Lisp_Object args[2];
336 int idx = (EQ (add, Qappend) ? 0 : 1);
337
338 args[1 - idx] = Fmake_vector (make_number (1), elt);
339
340 if (CONSP (range))
341 {
342 int from = XINT (XCAR (range));
343 int to = XINT (XCDR (range));
344 int from1, to1;
345
346 do {
347 from1 = from, to1 = to;
348 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
349 char_table_set_range (fontset, from, to1,
350 NILP (args[idx]) ? args[1 - idx]
351 : Fvconcat (2, args));
352 from = to1 + 1;
353 } while (from < to);
354 }
355 else
356 {
357 args[idx] = FONTSET_FALLBACK (fontset);
358 FONTSET_FALLBACK (fontset)
359 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
360 }
361 return Qnil;
362 }
363
364 static int
365 fontset_compare_rfontdef (const void *val1, const void *val2)
366 {
367 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
368 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
369 }
370
371 /* Update FONT-GROUP which has this form:
372 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
373 RFONT-DEF0 RFONT-DEF1 ... ]
374 Reorder RFONT-DEFs according to the current language, and update
375 CHARSET-ORDERED-LIST-TICK.
376
377 If PREFERRED_FAMILY is not nil, that family has the higher priority
378 if the encoding charsets or languages in font-specs are the same. */
379
380 static void
381 reorder_font_vector (Lisp_Object font_group, struct font *font)
382 {
383 Lisp_Object vec, font_object;
384 int size;
385 int i;
386 int score_changed = 0;
387
388 if (font)
389 XSETFONT (font_object, font);
390 else
391 font_object = Qnil;
392
393 vec = XCDR (font_group);
394 size = ASIZE (vec);
395 /* Exclude the tailing nil element from the reordering. */
396 if (NILP (AREF (vec, size - 1)))
397 size--;
398
399 for (i = 0; i < size; i++)
400 {
401 Lisp_Object rfont_def = AREF (vec, i);
402 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
403 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
404 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
405 Lisp_Object otf_spec = Ffont_get (font_spec, QCotf);
406
407 if (! NILP (otf_spec))
408 /* A font-spec with :otf is preferable regardless of encoding
409 and language.. */
410 ;
411 else if (! font_match_p (font_spec, font_object))
412 {
413 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
414
415 if (! NILP (encoding))
416 {
417 Lisp_Object tail;
418
419 for (tail = Vcharset_ordered_list;
420 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
421 score += 0x100, tail = XCDR (tail))
422 if (EQ (encoding, XCAR (tail)))
423 break;
424 }
425 else
426 {
427 Lisp_Object lang = Ffont_get (font_spec, QClang);
428
429 if (! NILP (lang)
430 && ! EQ (lang, Vcurrent_iso639_language)
431 && (! CONSP (Vcurrent_iso639_language)
432 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
433 score |= 0x100;
434 }
435 }
436 if (RFONT_DEF_SCORE (rfont_def) != score)
437 {
438 RFONT_DEF_SET_SCORE (rfont_def, score);
439 score_changed = 1;
440 }
441 }
442
443 if (score_changed)
444 qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
445 fontset_compare_rfontdef);
446 XSETCAR (font_group, make_number (charset_ordered_list_tick));
447 }
448
449 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
450 character C in FONTSET. If C is -1, return a fallback font-group.
451 If C is not -1, the value may be Qt (FONTSET doesn't have a font
452 for C even in the fallback group, or 0 (a font for C may be found
453 only in the fallback group). */
454
455 static Lisp_Object
456 fontset_get_font_group (Lisp_Object fontset, int c)
457 {
458 Lisp_Object font_group;
459 Lisp_Object base_fontset;
460 int from = 0, to = MAX_CHAR, i;
461
462 xassert (! BASE_FONTSET_P (fontset));
463 if (c >= 0)
464 font_group = CHAR_TABLE_REF (fontset, c);
465 else
466 font_group = FONTSET_FALLBACK (fontset);
467 if (! NILP (font_group))
468 return font_group;
469 base_fontset = FONTSET_BASE (fontset);
470 if (c >= 0)
471 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
472 else
473 font_group = FONTSET_FALLBACK (base_fontset);
474 if (NILP (font_group))
475 {
476 font_group = make_number (0);
477 if (c >= 0)
478 char_table_set_range (fontset, from, to, font_group);
479 return font_group;
480 }
481 font_group = Fcopy_sequence (font_group);
482 for (i = 0; i < ASIZE (font_group); i++)
483 if (! NILP (AREF (font_group, i)))
484 {
485 Lisp_Object rfont_def;
486
487 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
488 /* Remember the original order. */
489 RFONT_DEF_SET_SCORE (rfont_def, i);
490 ASET (font_group, i, rfont_def);
491 }
492 font_group = Fcons (make_number (-1), font_group);
493 if (c >= 0)
494 char_table_set_range (fontset, from, to, font_group);
495 else
496 FONTSET_FALLBACK (fontset) = font_group;
497 return font_group;
498 }
499
500 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
501 character C. If no font is found, return Qnil if there's a
502 possibility that the default fontset or the fallback font groups
503 have a proper font, and return Qt if not.
504
505 If a font is found but is not yet opened, open it (if FACE is not
506 NULL) or return Qnil (if FACE is NULL).
507
508 ID is a charset-id that must be preferred, or -1 meaning no
509 preference.
510
511 If FALLBACK is nonzero, search only fallback fonts. */
512
513 static Lisp_Object
514 fontset_find_font (Lisp_Object fontset, int c, struct face *face, int id, int fallback)
515 {
516 Lisp_Object vec, font_group;
517 int i, charset_matched = 0, found_index;
518 FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset))
519 ? XFRAME (FONTSET_FRAME (fontset)) : XFRAME (selected_frame));
520 Lisp_Object rfont_def;
521
522 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
523 if (! CONSP (font_group))
524 return font_group;
525 vec = XCDR (font_group);
526 if (ASIZE (vec) == 0)
527 return Qnil;
528
529 if (ASIZE (vec) > 1)
530 {
531 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
532 /* We have just created the font-group,
533 or the charset priorities were changed. */
534 reorder_font_vector (font_group, face->ascii_face->font);
535 if (id >= 0)
536 /* Find a spec matching with the charset ID to try at
537 first. */
538 for (i = 0; i < ASIZE (vec); i++)
539 {
540 Lisp_Object repertory;
541
542 rfont_def = AREF (vec, i);
543 if (NILP (rfont_def))
544 break;
545 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
546
547 if (XINT (repertory) == id)
548 {
549 charset_matched = i;
550 break;
551 }
552 }
553 }
554
555 /* Find the first available font in the vector of RFONT-DEF. */
556 for (i = 0; i < ASIZE (vec); i++)
557 {
558 Lisp_Object font_def;
559 Lisp_Object font_entity, font_object;
560
561 found_index = i;
562 if (i == 0)
563 {
564 if (charset_matched > 0)
565 {
566 /* Try the element matching with the charset ID at first. */
567 found_index = charset_matched;
568 /* Make this negative so that we don't come here in the
569 next loop. */
570 charset_matched = - charset_matched;
571 /* We must try the first element in the next loop. */
572 i--;
573 }
574 }
575 else if (i == - charset_matched)
576 {
577 /* We have already tried this element and the followings
578 that have the same font specifications in the first
579 iteration. So, skip them all. */
580 rfont_def = AREF (vec, i);
581 font_def = RFONT_DEF_FONT_DEF (rfont_def);
582 for (; i + 1 < ASIZE (vec); i++)
583 {
584 rfont_def = AREF (vec, i + 1);
585 if (NILP (rfont_def))
586 break;
587 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
588 break;
589 }
590 continue;
591 }
592
593 rfont_def = AREF (vec, found_index);
594 if (NILP (rfont_def))
595 {
596 if (i < 0)
597 continue;
598 /* This is a sign of not to try the other fonts. */
599 return Qt;
600 }
601 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
602 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
603 /* We couldn't open this font last time. */
604 continue;
605
606 font_object = RFONT_DEF_OBJECT (rfont_def);
607 if (NILP (font_object))
608 {
609 font_def = RFONT_DEF_FONT_DEF (rfont_def);
610
611 if (! face)
612 /* We have not yet opened the font. */
613 return Qnil;
614 /* Find a font best-matching with the spec without checking
615 the support of the character C. That checking is costly,
616 and even without the checking, the found font supports C
617 in high possibility. */
618 font_entity = font_find_for_lface (f, face->lface,
619 FONT_DEF_SPEC (font_def), -1);
620 if (NILP (font_entity))
621 {
622 /* Record that no font matches the spec. */
623 RFONT_DEF_SET_FACE (rfont_def, -1);
624 continue;
625 }
626 font_object = font_open_for_lface (f, font_entity, face->lface,
627 FONT_DEF_SPEC (font_def));
628 if (NILP (font_object))
629 {
630 /* Something strange happened, perhaps because of a
631 Font-backend problem. Too avoid crashing, record
632 that this spec is unsable. It may be better to find
633 another font of the same spec, but currently we don't
634 have such an API. */
635 RFONT_DEF_SET_FACE (rfont_def, -1);
636 continue;
637 }
638 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
639 }
640
641 if (font_has_char (f, font_object, c))
642 goto found;
643
644 /* Find a font already opened, maching with the current spec,
645 and supporting C. */
646 font_def = RFONT_DEF_FONT_DEF (rfont_def);
647 for (; found_index + 1 < ASIZE (vec); found_index++)
648 {
649 rfont_def = AREF (vec, found_index + 1);
650 if (NILP (rfont_def))
651 break;
652 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
653 break;
654 font_object = RFONT_DEF_OBJECT (rfont_def);
655 if (! NILP (font_object) && font_has_char (f, font_object, c))
656 {
657 found_index++;
658 goto found;
659 }
660 }
661
662 /* Find a font-entity with the current spec and supporting C. */
663 font_entity = font_find_for_lface (f, face->lface,
664 FONT_DEF_SPEC (font_def), c);
665 if (! NILP (font_entity))
666 {
667 /* We found a font. Open it and insert a new element for
668 that font in VEC. */
669 Lisp_Object new_vec;
670 int j;
671
672 font_object = font_open_for_lface (f, font_entity, face->lface,
673 Qnil);
674 if (NILP (font_object))
675 continue;
676 RFONT_DEF_NEW (rfont_def, font_def);
677 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
678 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
679 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
680 found_index++;
681 for (j = 0; j < found_index; j++)
682 ASET (new_vec, j, AREF (vec, j));
683 ASET (new_vec, j, rfont_def);
684 for (j++; j < ASIZE (new_vec); j++)
685 ASET (new_vec, j, AREF (vec, j - 1));
686 XSETCDR (font_group, new_vec);
687 vec = new_vec;
688 goto found;
689 }
690 if (i >= 0)
691 i = found_index;
692 }
693
694 FONTSET_SET (fontset, make_number (c), make_number (0));
695 return Qnil;
696
697 found:
698 if (fallback && found_index > 0)
699 {
700 /* The order of fonts in the fallback font-group is not that
701 important, and it is better to move the found font to the
702 first of the group so that the next try will find it
703 quickly. */
704 for (i = found_index; i > 0; i--)
705 ASET (vec, i, AREF (vec, i - 1));
706 ASET (vec, 0, rfont_def);
707 }
708 return rfont_def;
709 }
710
711
712 static Lisp_Object
713 fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
714 {
715 Lisp_Object rfont_def, default_rfont_def IF_LINT (= Qnil);
716 Lisp_Object base_fontset;
717
718 /* Try a font-group of FONTSET. */
719 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
720 rfont_def = fontset_find_font (fontset, c, face, id, 0);
721 if (VECTORP (rfont_def))
722 return rfont_def;
723 if (NILP (rfont_def))
724 FONTSET_SET (fontset, make_number (c), make_number (0));
725
726 /* Try a font-group of the default fontset. */
727 base_fontset = FONTSET_BASE (fontset);
728 if (! EQ (base_fontset, Vdefault_fontset))
729 {
730 if (NILP (FONTSET_DEFAULT (fontset)))
731 FONTSET_DEFAULT (fontset)
732 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
733 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
734 default_rfont_def
735 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
736 if (VECTORP (default_rfont_def))
737 return default_rfont_def;
738 if (NILP (default_rfont_def))
739 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
740 make_number (0));
741 }
742
743 /* Try a fallback font-group of FONTSET. */
744 if (! EQ (rfont_def, Qt))
745 {
746 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
747 rfont_def = fontset_find_font (fontset, c, face, id, 1);
748 if (VECTORP (rfont_def))
749 return rfont_def;
750 /* Remember that FONTSET has no font for C. */
751 FONTSET_SET (fontset, make_number (c), Qt);
752 }
753
754 /* Try a fallback font-group of the default fontset. */
755 if (! EQ (base_fontset, Vdefault_fontset)
756 && ! EQ (default_rfont_def, Qt))
757 {
758 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
759 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
760 if (VECTORP (rfont_def))
761 return rfont_def;
762 /* Remember that the default fontset has no font for C. */
763 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
764 }
765
766 return Qnil;
767 }
768
769 /* Return a newly created fontset with NAME. If BASE is nil, make a
770 base fontset. Otherwise make a realized fontset whose base is
771 BASE. */
772
773 static Lisp_Object
774 make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
775 {
776 Lisp_Object fontset;
777 int size = ASIZE (Vfontset_table);
778 int id = next_fontset_id;
779
780 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
781 the next available fontset ID. So it is expected that this loop
782 terminates quickly. In addition, as the last element of
783 Vfontset_table is always nil, we don't have to check the range of
784 id. */
785 while (!NILP (AREF (Vfontset_table, id))) id++;
786
787 if (id + 1 == size)
788 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
789
790 fontset = Fmake_char_table (Qfontset, Qnil);
791
792 FONTSET_ID (fontset) = make_number (id);
793 if (NILP (base))
794 {
795 FONTSET_NAME (fontset) = name;
796 }
797 else
798 {
799 FONTSET_NAME (fontset) = Qnil;
800 FONTSET_FRAME (fontset) = frame;
801 FONTSET_BASE (fontset) = base;
802 }
803
804 ASET (Vfontset_table, id, fontset);
805 next_fontset_id = id + 1;
806 return fontset;
807 }
808
809 \f
810 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
811
812 /* Return the name of the fontset who has ID. */
813
814 Lisp_Object
815 fontset_name (int id)
816 {
817 Lisp_Object fontset;
818
819 fontset = FONTSET_FROM_ID (id);
820 return FONTSET_NAME (fontset);
821 }
822
823
824 /* Return the ASCII font name of the fontset who has ID. */
825
826 Lisp_Object
827 fontset_ascii (int id)
828 {
829 Lisp_Object fontset, elt;
830
831 fontset= FONTSET_FROM_ID (id);
832 elt = FONTSET_ASCII (fontset);
833 if (CONSP (elt))
834 elt = XCAR (elt);
835 return elt;
836 }
837
838 static void
839 free_realized_fontset (FRAME_PTR f, Lisp_Object fontset)
840 {
841 #if 0
842 Lisp_Object tail;
843
844 if (0)
845 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
846 {
847 xassert (FONT_OBJECT_P (XCAR (tail)));
848 font_close_object (f, XCAR (tail));
849 }
850 #endif
851 }
852
853 /* Free fontset of FACE defined on frame F. Called from
854 free_realized_face. */
855
856 void
857 free_face_fontset (FRAME_PTR f, struct face *face)
858 {
859 Lisp_Object fontset;
860
861 fontset = FONTSET_FROM_ID (face->fontset);
862 if (NILP (fontset))
863 return;
864 xassert (! BASE_FONTSET_P (fontset));
865 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
866 free_realized_fontset (f, fontset);
867 ASET (Vfontset_table, face->fontset, Qnil);
868 if (face->fontset < next_fontset_id)
869 next_fontset_id = face->fontset;
870 if (! NILP (FONTSET_DEFAULT (fontset)))
871 {
872 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
873
874 fontset = AREF (Vfontset_table, id);
875 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
876 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
877 free_realized_fontset (f, fontset);
878 ASET (Vfontset_table, id, Qnil);
879 if (id < next_fontset_id)
880 next_fontset_id = face->fontset;
881 }
882 face->fontset = -1;
883 }
884
885
886 #if 0
887 /* Return 1 if FACE is suitable for displaying character C.
888 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
889 when C is not an ASCII character. */
890
891 int
892 face_suitable_for_char_p (struct face *face, int c)
893 {
894 Lisp_Object fontset, rfont_def;
895
896 fontset = FONTSET_FROM_ID (face->fontset);
897 rfont_def = fontset_font (fontset, c, NULL, -1);
898 return (VECTORP (rfont_def)
899 && INTEGERP (RFONT_DEF_FACE (rfont_def))
900 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
901 }
902 #endif
903
904
905 /* Return ID of face suitable for displaying character C on frame F.
906 FACE must be reazlied for ASCII characters in advance. Called from
907 the macro FACE_FOR_CHAR. */
908
909 int
910 face_for_char (FRAME_PTR f, struct face *face, int c, int pos, Lisp_Object object)
911 {
912 Lisp_Object fontset, rfont_def, charset;
913 int face_id;
914 int id;
915
916 /* If face->fontset is negative (that happens when no font is found
917 for face), just return face->ascii_face because we can't do
918 anything. Perhaps, we should fix the callers to assure
919 that face->fontset is always valid. */
920 if (ASCII_CHAR_P (c) || face->fontset < 0)
921 return face->ascii_face->id;
922
923 xassert (fontset_id_valid_p (face->fontset));
924 fontset = FONTSET_FROM_ID (face->fontset);
925 xassert (!BASE_FONTSET_P (fontset));
926
927 if (pos < 0)
928 {
929 id = -1;
930 charset = Qnil;
931 }
932 else
933 {
934 charset = Fget_char_property (make_number (pos), Qcharset, object);
935 if (CHARSETP (charset))
936 {
937 Lisp_Object val;
938
939 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
940 if (CONSP (val) && CHARSETP (XCDR (val)))
941 charset = XCDR (val);
942 id = XINT (CHARSET_SYMBOL_ID (charset));
943 }
944 else
945 id = -1;
946 }
947
948 rfont_def = fontset_font (fontset, c, face, id);
949 if (VECTORP (rfont_def))
950 {
951 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
952 face_id = XINT (RFONT_DEF_FACE (rfont_def));
953 else
954 {
955 Lisp_Object font_object;
956
957 font_object = RFONT_DEF_OBJECT (rfont_def);
958 face_id = face_for_font (f, font_object, face);
959 RFONT_DEF_SET_FACE (rfont_def, face_id);
960 }
961 }
962 else
963 {
964 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
965 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
966 else
967 {
968 face_id = face_for_font (f, Qnil, face);
969 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
970 }
971 }
972 xassert (face_id >= 0);
973 return face_id;
974 }
975
976
977 Lisp_Object
978 font_for_char (struct face *face, int c, int pos, Lisp_Object object)
979 {
980 Lisp_Object fontset, rfont_def, charset;
981 int id;
982
983 if (ASCII_CHAR_P (c))
984 {
985 Lisp_Object font_object;
986
987 XSETFONT (font_object, face->ascii_face->font);
988 return font_object;
989 }
990
991 xassert (fontset_id_valid_p (face->fontset));
992 fontset = FONTSET_FROM_ID (face->fontset);
993 xassert (!BASE_FONTSET_P (fontset));
994 if (pos < 0)
995 {
996 id = -1;
997 charset = Qnil;
998 }
999 else
1000 {
1001 charset = Fget_char_property (make_number (pos), Qcharset, object);
1002 if (CHARSETP (charset))
1003 {
1004 Lisp_Object val;
1005
1006 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
1007 if (CONSP (val) && CHARSETP (XCDR (val)))
1008 charset = XCDR (val);
1009 id = XINT (CHARSET_SYMBOL_ID (charset));
1010 }
1011 else
1012 id = -1;
1013 }
1014
1015 rfont_def = fontset_font (fontset, c, face, id);
1016 return (VECTORP (rfont_def)
1017 ? RFONT_DEF_OBJECT (rfont_def)
1018 : Qnil);
1019 }
1020
1021
1022 /* Make a realized fontset for ASCII face FACE on frame F from the
1023 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1024 default fontset as the base. Value is the id of the new fontset.
1025 Called from realize_x_face. */
1026
1027 int
1028 make_fontset_for_ascii_face (FRAME_PTR f, int base_fontset_id, struct face *face)
1029 {
1030 Lisp_Object base_fontset, fontset, frame;
1031
1032 XSETFRAME (frame, f);
1033 if (base_fontset_id >= 0)
1034 {
1035 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1036 if (!BASE_FONTSET_P (base_fontset))
1037 base_fontset = FONTSET_BASE (base_fontset);
1038 if (! BASE_FONTSET_P (base_fontset))
1039 abort ();
1040 }
1041 else
1042 base_fontset = Vdefault_fontset;
1043
1044 fontset = make_fontset (frame, Qnil, base_fontset);
1045 return XINT (FONTSET_ID (fontset));
1046 }
1047
1048 \f
1049
1050 /* Cache data used by fontset_pattern_regexp. The car part is a
1051 pattern string containing at least one wild card, the cdr part is
1052 the corresponding regular expression. */
1053 static Lisp_Object Vcached_fontset_data;
1054
1055 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1056 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1057
1058 /* If fontset name PATTERN contains any wild card, return regular
1059 expression corresponding to PATTERN. */
1060
1061 static Lisp_Object
1062 fontset_pattern_regexp (Lisp_Object pattern)
1063 {
1064 if (!strchr (SSDATA (pattern), '*')
1065 && !strchr (SSDATA (pattern), '?'))
1066 /* PATTERN does not contain any wild cards. */
1067 return Qnil;
1068
1069 if (!CONSP (Vcached_fontset_data)
1070 || strcmp (SSDATA (pattern), CACHED_FONTSET_NAME))
1071 {
1072 /* We must at first update the cached data. */
1073 unsigned char *regex, *p0, *p1;
1074 int ndashes = 0, nstars = 0, nescs = 0;
1075
1076 for (p0 = SDATA (pattern); *p0; p0++)
1077 {
1078 if (*p0 == '-')
1079 ndashes++;
1080 else if (*p0 == '*')
1081 nstars++;
1082 else if (*p0 == '['
1083 || *p0 == '.' || *p0 == '\\'
1084 || *p0 == '+' || *p0 == '^'
1085 || *p0 == '$')
1086 nescs++;
1087 }
1088
1089 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1090 we convert "*" to "[^-]*" which is much faster in regular
1091 expression matching. */
1092 if (ndashes < 14)
1093 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
1094 else
1095 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
1096
1097 *p1++ = '^';
1098 for (p0 = SDATA (pattern); *p0; p0++)
1099 {
1100 if (*p0 == '*')
1101 {
1102 if (ndashes < 14)
1103 *p1++ = '.';
1104 else
1105 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1106 *p1++ = '*';
1107 }
1108 else if (*p0 == '?')
1109 *p1++ = '.';
1110 else if (*p0 == '['
1111 || *p0 == '.' || *p0 == '\\'
1112 || *p0 == '+' || *p0 == '^'
1113 || *p0 == '$')
1114 *p1++ = '\\', *p1++ = *p0;
1115 else
1116 *p1++ = *p0;
1117 }
1118 *p1++ = '$';
1119 *p1++ = 0;
1120
1121 Vcached_fontset_data = Fcons (build_string (SSDATA (pattern)),
1122 build_string ((char *) regex));
1123 }
1124
1125 return CACHED_FONTSET_REGEX;
1126 }
1127
1128 /* Return ID of the base fontset named NAME. If there's no such
1129 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1130 0: pattern containing '*' and '?' as wildcards
1131 1: regular expression
1132 2: literal fontset name
1133 */
1134
1135 int
1136 fs_query_fontset (Lisp_Object name, int name_pattern)
1137 {
1138 Lisp_Object tem;
1139 int i;
1140
1141 name = Fdowncase (name);
1142 if (name_pattern != 1)
1143 {
1144 tem = Frassoc (name, Vfontset_alias_alist);
1145 if (NILP (tem))
1146 tem = Fassoc (name, Vfontset_alias_alist);
1147 if (CONSP (tem) && STRINGP (XCAR (tem)))
1148 name = XCAR (tem);
1149 else if (name_pattern == 0)
1150 {
1151 tem = fontset_pattern_regexp (name);
1152 if (STRINGP (tem))
1153 {
1154 name = tem;
1155 name_pattern = 1;
1156 }
1157 }
1158 }
1159
1160 for (i = 0; i < ASIZE (Vfontset_table); i++)
1161 {
1162 Lisp_Object fontset, this_name;
1163
1164 fontset = FONTSET_FROM_ID (i);
1165 if (NILP (fontset)
1166 || !BASE_FONTSET_P (fontset))
1167 continue;
1168
1169 this_name = FONTSET_NAME (fontset);
1170 if (name_pattern == 1
1171 ? fast_string_match_ignore_case (name, this_name) >= 0
1172 : !xstrcasecmp (SSDATA (name), SSDATA (this_name)))
1173 return i;
1174 }
1175 return -1;
1176 }
1177
1178
1179 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1180 doc: /* Return the name of a fontset that matches PATTERN.
1181 The value is nil if there is no matching fontset.
1182 PATTERN can contain `*' or `?' as a wildcard
1183 just as X font name matching algorithm allows.
1184 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1185 (Lisp_Object pattern, Lisp_Object regexpp)
1186 {
1187 Lisp_Object fontset;
1188 int id;
1189
1190 (*check_window_system_func) ();
1191
1192 CHECK_STRING (pattern);
1193
1194 if (SCHARS (pattern) == 0)
1195 return Qnil;
1196
1197 id = fs_query_fontset (pattern, !NILP (regexpp));
1198 if (id < 0)
1199 return Qnil;
1200
1201 fontset = FONTSET_FROM_ID (id);
1202 return FONTSET_NAME (fontset);
1203 }
1204
1205 /* Return a list of base fontset names matching PATTERN on frame F. */
1206
1207 Lisp_Object
1208 list_fontsets (FRAME_PTR f, Lisp_Object pattern, int size)
1209 {
1210 Lisp_Object frame, regexp, val;
1211 int id;
1212
1213 XSETFRAME (frame, f);
1214
1215 regexp = fontset_pattern_regexp (pattern);
1216 val = Qnil;
1217
1218 for (id = 0; id < ASIZE (Vfontset_table); id++)
1219 {
1220 Lisp_Object fontset, name;
1221
1222 fontset = FONTSET_FROM_ID (id);
1223 if (NILP (fontset)
1224 || !BASE_FONTSET_P (fontset)
1225 || !EQ (frame, FONTSET_FRAME (fontset)))
1226 continue;
1227 name = FONTSET_NAME (fontset);
1228
1229 if (STRINGP (regexp)
1230 ? (fast_string_match (regexp, name) < 0)
1231 : strcmp (SSDATA (pattern), SSDATA (name)))
1232 continue;
1233
1234 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1235 }
1236
1237 return val;
1238 }
1239
1240
1241 /* Free all realized fontsets whose base fontset is BASE. */
1242
1243 static void
1244 free_realized_fontsets (Lisp_Object base)
1245 {
1246 int id;
1247
1248 #if 0
1249 /* For the moment, this doesn't work because free_realized_face
1250 doesn't remove FACE from a cache. Until we find a solution, we
1251 suppress this code, and simply use Fclear_face_cache even though
1252 that is not efficient. */
1253 BLOCK_INPUT;
1254 for (id = 0; id < ASIZE (Vfontset_table); id++)
1255 {
1256 Lisp_Object this = AREF (Vfontset_table, id);
1257
1258 if (EQ (FONTSET_BASE (this), base))
1259 {
1260 Lisp_Object tail;
1261
1262 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1263 tail = XCDR (tail))
1264 {
1265 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1266 int face_id = XINT (XCDR (XCAR (tail)));
1267 struct face *face = FACE_FROM_ID (f, face_id);
1268
1269 /* Face THIS itself is also freed by the following call. */
1270 free_realized_face (f, face);
1271 }
1272 }
1273 }
1274 UNBLOCK_INPUT;
1275 #else /* not 0 */
1276 /* But, we don't have to call Fclear_face_cache if no fontset has
1277 been realized from BASE. */
1278 for (id = 0; id < ASIZE (Vfontset_table); id++)
1279 {
1280 Lisp_Object this = AREF (Vfontset_table, id);
1281
1282 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1283 {
1284 Fclear_face_cache (Qt);
1285 break;
1286 }
1287 }
1288 #endif /* not 0 */
1289 }
1290
1291
1292 /* Check validity of NAME as a fontset name and return the
1293 corresponding fontset. If not valid, signal an error.
1294
1295 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1296 fontset of *FRAME.
1297
1298 Set *FRAME to the actual frame. */
1299
1300 static Lisp_Object
1301 check_fontset_name (Lisp_Object name, Lisp_Object *frame)
1302 {
1303 int id;
1304
1305 if (NILP (*frame))
1306 *frame = selected_frame;
1307 CHECK_LIVE_FRAME (*frame);
1308
1309 if (EQ (name, Qt))
1310 return Vdefault_fontset;
1311 if (NILP (name))
1312 {
1313 id = FRAME_FONTSET (XFRAME (*frame));
1314 }
1315 else
1316 {
1317 CHECK_STRING (name);
1318 /* First try NAME as literal. */
1319 id = fs_query_fontset (name, 2);
1320 if (id < 0)
1321 /* For backward compatibility, try again NAME as pattern. */
1322 id = fs_query_fontset (name, 0);
1323 if (id < 0)
1324 error ("Fontset `%s' does not exist", SDATA (name));
1325 }
1326 return FONTSET_FROM_ID (id);
1327 }
1328
1329 static void
1330 accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
1331 {
1332 if (EQ (XCAR (arg), val))
1333 {
1334 if (CONSP (range))
1335 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1336 else
1337 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1338 }
1339 }
1340
1341
1342 /* Callback function for map_charset_chars in Fset_fontset_font.
1343 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1344
1345 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1346 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1347 The consumed ranges are poped up from SCRIPT_RANGE_LIST, and the
1348 new SCRIPT_RANGE_LIST is stored in ARG.
1349
1350 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1351 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1352 case. */
1353
1354 static void
1355 set_fontset_font (Lisp_Object arg, Lisp_Object range)
1356 {
1357 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1358 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1359
1360 fontset = AREF (arg, 0);
1361 font_def = AREF (arg, 1);
1362 add = AREF (arg, 2);
1363 ascii = AREF (arg, 3);
1364 script_range_list = AREF (arg, 4);
1365
1366 if (NILP (ascii) && from < 0x80)
1367 {
1368 if (to < 0x80)
1369 return;
1370 from = 0x80;
1371 range = Fcons (make_number (0x80), XCDR (range));
1372 }
1373
1374 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1375 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1376 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1377
1378 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1379 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1380 if (CONSP (script_range_list))
1381 {
1382 if (SCRIPT_FROM < from)
1383 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1384 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1385 POP_SCRIPT_RANGE ();
1386 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1387 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1388 }
1389
1390 FONTSET_ADD (fontset, range, font_def, add);
1391 ASET (arg, 4, script_range_list);
1392 }
1393
1394 static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
1395
1396
1397 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1398 doc: /*
1399 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1400
1401 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1402 the default fontset.
1403
1404 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1405 In that case, use FONT-SPEC for all characters in the range FROM and
1406 TO (inclusive).
1407
1408 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1409 all characters that belong to the script.
1410
1411 TARGET may be a charset. In that case, use FONT-SPEC for all
1412 characters in the charset.
1413
1414 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1415 that no FONT-SPEC is specified.
1416
1417 FONT-SPEC may one of these:
1418 * A font-spec object made by the function `font-spec' (which see).
1419 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1420 REGISTRY is a font registry name. FAMILY may contain foundry
1421 name, and REGISTRY may contain encoding name.
1422 * A font name string.
1423 * nil, which explicitly specifies that there's no font for TARGET.
1424
1425 Optional 4th argument FRAME is a frame or nil for the selected frame
1426 that is concerned in the case that NAME is nil.
1427
1428 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1429 to the font specifications for TARGET previously set. If it is
1430 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1431 appended. By default, FONT-SPEC overrides the previous settings. */)
1432 (Lisp_Object name, Lisp_Object target, Lisp_Object font_spec, Lisp_Object frame, Lisp_Object add)
1433 {
1434 Lisp_Object fontset;
1435 Lisp_Object font_def, registry, family;
1436 Lisp_Object range_list;
1437 struct charset *charset = NULL;
1438 Lisp_Object fontname;
1439 int ascii_changed = 0;
1440
1441 fontset = check_fontset_name (name, &frame);
1442
1443 fontname = Qnil;
1444 if (CONSP (font_spec))
1445 {
1446 Lisp_Object spec = Ffont_spec (0, NULL);
1447
1448 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1449 font_spec = spec;
1450 fontname = Ffont_xlfd_name (font_spec, Qnil);
1451 }
1452 else if (STRINGP (font_spec))
1453 {
1454 Lisp_Object args[2];
1455
1456 fontname = font_spec;
1457 args[0] = QCname;
1458 args[1] = font_spec;
1459 font_spec = Ffont_spec (2, args);
1460 }
1461 else if (FONT_SPEC_P (font_spec))
1462 fontname = Ffont_xlfd_name (font_spec, Qnil);
1463 else if (! NILP (font_spec))
1464 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1465
1466 if (! NILP (font_spec))
1467 {
1468 Lisp_Object encoding, repertory;
1469
1470 family = AREF (font_spec, FONT_FAMILY_INDEX);
1471 if (! NILP (family) )
1472 family = SYMBOL_NAME (family);
1473 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1474 if (! NILP (registry))
1475 registry = Fdowncase (SYMBOL_NAME (registry));
1476 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1477 registry));
1478 if (NILP (encoding))
1479 encoding = Qascii;
1480
1481 if (SYMBOLP (encoding))
1482 {
1483 CHECK_CHARSET (encoding);
1484 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1485 }
1486 else
1487 {
1488 repertory = XCDR (encoding);
1489 encoding = XCAR (encoding);
1490 CHECK_CHARSET (encoding);
1491 encoding = CHARSET_SYMBOL_ID (encoding);
1492 if (! NILP (repertory) && SYMBOLP (repertory))
1493 {
1494 CHECK_CHARSET (repertory);
1495 repertory = CHARSET_SYMBOL_ID (repertory);
1496 }
1497 }
1498 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1499 }
1500 else
1501 font_def = Qnil;
1502
1503 if (CHARACTERP (target))
1504 {
1505 if (XFASTINT (target) < 0x80)
1506 error ("Can't set a font for partial ASCII range");
1507 range_list = Fcons (Fcons (target, target), Qnil);
1508 }
1509 else if (CONSP (target))
1510 {
1511 Lisp_Object from, to;
1512
1513 from = Fcar (target);
1514 to = Fcdr (target);
1515 CHECK_CHARACTER (from);
1516 CHECK_CHARACTER (to);
1517 if (XFASTINT (from) < 0x80)
1518 {
1519 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1520 error ("Can't set a font for partial ASCII range");
1521 ascii_changed = 1;
1522 }
1523 range_list = Fcons (target, Qnil);
1524 }
1525 else if (SYMBOLP (target) && !NILP (target))
1526 {
1527 Lisp_Object script_list;
1528 Lisp_Object val;
1529
1530 range_list = Qnil;
1531 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1532 if (! NILP (Fmemq (target, script_list)))
1533 {
1534 if (EQ (target, Qlatin))
1535 ascii_changed = 1;
1536 val = Fcons (target, Qnil);
1537 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1538 val);
1539 range_list = Fnreverse (XCDR (val));
1540 }
1541 if (CHARSETP (target))
1542 {
1543 CHECK_CHARSET_GET_CHARSET (target, charset);
1544 if (charset->ascii_compatible_p)
1545 ascii_changed = 1;
1546 }
1547 else if (NILP (range_list))
1548 error ("Invalid script or charset name: %s",
1549 SDATA (SYMBOL_NAME (target)));
1550 }
1551 else if (NILP (target))
1552 range_list = Fcons (Qnil, Qnil);
1553 else
1554 error ("Invalid target for setting a font");
1555
1556 if (ascii_changed)
1557 {
1558 Lisp_Object val;
1559
1560 if (NILP (font_spec))
1561 error ("Can't set ASCII font to nil");
1562 val = CHAR_TABLE_REF (fontset, 0);
1563 if (! NILP (val) && EQ (add, Qappend))
1564 /* We are going to change just an additional font for ASCII. */
1565 ascii_changed = 0;
1566 }
1567
1568 if (charset)
1569 {
1570 Lisp_Object arg;
1571
1572 arg = Fmake_vector (make_number (5), Qnil);
1573 ASET (arg, 0, fontset);
1574 ASET (arg, 1, font_def);
1575 ASET (arg, 2, add);
1576 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1577 ASET (arg, 4, range_list);
1578
1579 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1580 CHARSET_MIN_CODE (charset),
1581 CHARSET_MAX_CODE (charset));
1582 range_list = AREF (arg, 4);
1583 }
1584 for (; CONSP (range_list); range_list = XCDR (range_list))
1585 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1586
1587 if (ascii_changed)
1588 {
1589 Lisp_Object tail, fr, alist;
1590 int fontset_id = XINT (FONTSET_ID (fontset));
1591
1592 FONTSET_ASCII (fontset) = fontname;
1593 name = FONTSET_NAME (fontset);
1594 FOR_EACH_FRAME (tail, fr)
1595 {
1596 FRAME_PTR f = XFRAME (fr);
1597 Lisp_Object font_object;
1598 struct face *face;
1599
1600 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1601 continue;
1602 if (fontset_id != FRAME_FONTSET (f))
1603 continue;
1604 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1605 if (face)
1606 font_object = font_load_for_lface (f, face->lface, font_spec);
1607 else
1608 font_object = font_open_by_spec (f, font_spec);
1609 if (! NILP (font_object))
1610 {
1611 update_auto_fontset_alist (font_object, fontset);
1612 alist = Fcons (Fcons (Qfont, Fcons (name, font_object)), Qnil);
1613 Fmodify_frame_parameters (fr, alist);
1614 }
1615 }
1616 }
1617
1618 /* Free all realized fontsets whose base is FONTSET. This way, the
1619 specified character(s) are surely redisplayed by a correct
1620 font. */
1621 free_realized_fontsets (fontset);
1622
1623 return Qnil;
1624 }
1625
1626
1627 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1628 doc: /* Create a new fontset NAME from font information in FONTLIST.
1629
1630 FONTLIST is an alist of scripts vs the corresponding font specification list.
1631 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1632 character of SCRIPT is displayed by a font that matches one of
1633 FONT-SPEC.
1634
1635 SCRIPT is a symbol that appears in the first extra slot of the
1636 char-table `char-script-table'.
1637
1638 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1639 `set-fontset-font' for the meaning. */)
1640 (Lisp_Object name, Lisp_Object fontlist)
1641 {
1642 Lisp_Object fontset;
1643 int id;
1644
1645 CHECK_STRING (name);
1646 CHECK_LIST (fontlist);
1647
1648 name = Fdowncase (name);
1649 id = fs_query_fontset (name, 0);
1650 if (id < 0)
1651 {
1652 Lisp_Object font_spec = Ffont_spec (0, NULL);
1653 Lisp_Object short_name;
1654 char xlfd[256];
1655 int len;
1656
1657 if (font_parse_xlfd (SSDATA (name), font_spec) < 0)
1658 error ("Fontset name must be in XLFD format");
1659 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1660 if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1661 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1662 error ("Registry field of fontset name must be \"fontset-*\"");
1663 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1664 Vfontset_alias_alist);
1665 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1666 fontset = make_fontset (Qnil, name, Qnil);
1667 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1668 if (len < 0)
1669 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1670 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
1671 }
1672 else
1673 {
1674 fontset = FONTSET_FROM_ID (id);
1675 free_realized_fontsets (fontset);
1676 Fset_char_table_range (fontset, Qt, Qnil);
1677 }
1678
1679 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1680 {
1681 Lisp_Object elt, script;
1682
1683 elt = Fcar (fontlist);
1684 script = Fcar (elt);
1685 elt = Fcdr (elt);
1686 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1687 for (; CONSP (elt); elt = XCDR (elt))
1688 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1689 else
1690 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1691 }
1692 return name;
1693 }
1694
1695
1696 /* Alist of automatically created fontsets. Each element is a cons
1697 (FONT-SPEC . FONTSET-ID). */
1698 static Lisp_Object auto_fontset_alist;
1699
1700 /* Number of automatically created fontsets. */
1701 static int num_auto_fontsets;
1702
1703 /* Retun a fontset synthesized from FONT-OBJECT. This is called from
1704 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1705 frame, and the returned fontset is used for the default fontset of
1706 that frame. The fontset specifies a font of the same registry as
1707 FONT-OBJECT for all characters in the repertory of the registry
1708 (see Vfont_encoding_alist). If the repertory is not known, the
1709 fontset specifies the font for all Latin characters assuming that a
1710 user intends to use FONT-OBJECT for Latin characters. */
1711
1712 int
1713 fontset_from_font (Lisp_Object font_object)
1714 {
1715 Lisp_Object font_name = font_get_name (font_object);
1716 Lisp_Object font_spec = copy_font_spec (font_object);
1717 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1718 Lisp_Object fontset_spec, alias, name, fontset;
1719 Lisp_Object val;
1720
1721 val = assoc_no_quit (font_spec, auto_fontset_alist);
1722 if (CONSP (val))
1723 return XINT (FONTSET_ID (XCDR (val)));
1724 if (num_auto_fontsets++ == 0)
1725 alias = intern ("fontset-startup");
1726 else
1727 {
1728 char temp[32];
1729
1730 sprintf (temp, "fontset-auto%d", num_auto_fontsets - 1);
1731 alias = intern (temp);
1732 }
1733 fontset_spec = copy_font_spec (font_spec);
1734 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1735 name = Ffont_xlfd_name (fontset_spec, Qnil);
1736 if (NILP (name))
1737 abort ();
1738 fontset = make_fontset (Qnil, name, Qnil);
1739 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1740 Vfontset_alias_alist);
1741 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1742 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1743 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1744 font_spec = Ffont_spec (0, NULL);
1745 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1746 {
1747 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1748
1749 if (CONSP (target))
1750 target = XCDR (target);
1751 if (! CHARSETP (target))
1752 target = Qlatin;
1753 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1754 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1755 }
1756
1757 FONTSET_ASCII (fontset) = font_name;
1758
1759 return XINT (FONTSET_ID (fontset));
1760 }
1761
1762
1763 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1764 FONTSET is changed, we delete an entry of FONTSET if any from
1765 auto_fontset_alist so that FONTSET is not re-used by
1766 fontset_from_font. */
1767
1768 static void
1769 update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
1770 {
1771 Lisp_Object prev, tail;
1772
1773 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1774 prev = tail, tail = XCDR (tail))
1775 if (EQ (fontset, XCDR (XCAR (tail))))
1776 {
1777 if (NILP (prev))
1778 auto_fontset_alist = XCDR (tail);
1779 else
1780 XSETCDR (prev, XCDR (tail));
1781 break;
1782 }
1783 }
1784
1785
1786 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1787 FONT-OBJECT is the font for the character at POSITION in the current
1788 buffer. This is computed from all the text properties and overlays
1789 that apply to POSITION. POSTION may be nil, in which case,
1790 FONT-SPEC is the font for displaying the character CH with the
1791 default face.
1792
1793 GLYPH-CODE is the glyph code in the font to use for the character.
1794
1795 If the 2nd optional arg CH is non-nil, it is a character to check
1796 the font instead of the character at POSITION.
1797
1798 It returns nil in the following cases:
1799
1800 (1) The window system doesn't have a font for the character (thus
1801 it is displayed by an empty box).
1802
1803 (2) The character code is invalid.
1804
1805 (3) If POSITION is not nil, and the current buffer is not displayed
1806 in any window.
1807
1808 In addition, the returned font name may not take into account of
1809 such redisplay engine hooks as what used in jit-lock-mode if
1810 POSITION is currently not visible. */
1811
1812
1813 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1814 doc: /* For internal use only. */)
1815 (Lisp_Object position, Lisp_Object ch)
1816 {
1817 EMACS_INT pos, pos_byte, dummy;
1818 int face_id;
1819 int c;
1820 struct frame *f;
1821 struct face *face;
1822
1823 if (NILP (position))
1824 {
1825 CHECK_CHARACTER (ch);
1826 c = XINT (ch);
1827 f = XFRAME (selected_frame);
1828 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1829 pos = -1;
1830 }
1831 else
1832 {
1833 Lisp_Object window;
1834 struct window *w;
1835
1836 CHECK_NUMBER_COERCE_MARKER (position);
1837 pos = XINT (position);
1838 if (pos < BEGV || pos >= ZV)
1839 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1840 pos_byte = CHAR_TO_BYTE (pos);
1841 if (NILP (ch))
1842 c = FETCH_CHAR (pos_byte);
1843 else
1844 {
1845 CHECK_NATNUM (ch);
1846 c = XINT (ch);
1847 }
1848 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1849 if (NILP (window))
1850 return Qnil;
1851 w = XWINDOW (window);
1852 f = XFRAME (w->frame);
1853 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy,
1854 pos + 100, 0, -1);
1855 }
1856 if (! CHAR_VALID_P (c, 0))
1857 return Qnil;
1858 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1859 face = FACE_FROM_ID (f, face_id);
1860 if (face->font)
1861 {
1862 unsigned code = face->font->driver->encode_char (face->font, c);
1863 Lisp_Object font_object;
1864 /* Assignment to EMACS_INT stops GCC whining about limited range
1865 of data type. */
1866 EMACS_INT cod = code;
1867
1868 if (code == FONT_INVALID_CODE)
1869 return Qnil;
1870 XSETFONT (font_object, face->font);
1871 if (cod <= MOST_POSITIVE_FIXNUM)
1872 return Fcons (font_object, make_number (code));
1873 return Fcons (font_object, Fcons (make_number (code >> 16),
1874 make_number (code & 0xFFFF)));
1875 }
1876 return Qnil;
1877 }
1878
1879
1880 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1881 doc: /* Return information about a fontset FONTSET on frame FRAME.
1882
1883 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1884 for the default fontset. FRAME nil means the selected frame.
1885
1886 The value is a char-table whose elements have this form:
1887
1888 ((FONT OPENED-FONT ...) ...)
1889
1890 FONT is a name of font specified for a range of characters.
1891
1892 OPENED-FONT is a name of a font actually opened.
1893
1894 The char-table has one extra slot. If FONTSET is not the default
1895 fontset, the value the extra slot is a char-table containing the
1896 information about the derived fonts from the default fontset. The
1897 format is the same as above. */)
1898 (Lisp_Object fontset, Lisp_Object frame)
1899 {
1900 Lisp_Object *realized[2], fontsets[2], tables[2];
1901 Lisp_Object val, elt;
1902 int c, i, j, k;
1903
1904 (*check_window_system_func) ();
1905
1906 fontset = check_fontset_name (fontset, &frame);
1907
1908 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1909 in the table `realized'. */
1910 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1911 * ASIZE (Vfontset_table));
1912 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1913 {
1914 elt = FONTSET_FROM_ID (i);
1915 if (!NILP (elt)
1916 && EQ (FONTSET_BASE (elt), fontset)
1917 && EQ (FONTSET_FRAME (elt), frame))
1918 realized[0][j++] = elt;
1919 }
1920 realized[0][j] = Qnil;
1921
1922 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1923 * ASIZE (Vfontset_table));
1924 for (i = j = 0; ! NILP (realized[0][i]); i++)
1925 {
1926 elt = FONTSET_DEFAULT (realized[0][i]);
1927 if (! NILP (elt))
1928 realized[1][j++] = elt;
1929 }
1930 realized[1][j] = Qnil;
1931
1932 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1933 fontsets[0] = fontset;
1934 if (!EQ (fontset, Vdefault_fontset))
1935 {
1936 tables[1] = Fmake_char_table (Qnil, Qnil);
1937 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
1938 fontsets[1] = Vdefault_fontset;
1939 }
1940
1941 /* Accumulate information of the fontset in TABLE. The format of
1942 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1943 for (k = 0; k <= 1; k++)
1944 {
1945 for (c = 0; c <= MAX_CHAR; )
1946 {
1947 int from = c, to = MAX_5_BYTE_CHAR;
1948
1949 if (c <= MAX_5_BYTE_CHAR)
1950 {
1951 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1952 }
1953 else
1954 {
1955 val = FONTSET_FALLBACK (fontsets[k]);
1956 to = MAX_CHAR;
1957 }
1958 if (VECTORP (val))
1959 {
1960 Lisp_Object alist;
1961
1962 /* At first, set ALIST to ((FONT-SPEC) ...). */
1963 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
1964 if (! NILP (AREF (val, i)))
1965 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1966 alist);
1967 alist = Fnreverse (alist);
1968
1969 /* Then store opened font names to cdr of each elements. */
1970 for (i = 0; ! NILP (realized[k][i]); i++)
1971 {
1972 if (c <= MAX_5_BYTE_CHAR)
1973 val = FONTSET_REF (realized[k][i], c);
1974 else
1975 val = FONTSET_FALLBACK (realized[k][i]);
1976 if (! CONSP (val) || ! VECTORP (XCDR (val)))
1977 continue;
1978 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1979 val = XCDR (val);
1980 for (j = 0; j < ASIZE (val); j++)
1981 {
1982 elt = AREF (val, j);
1983 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1984 {
1985 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1986 Lisp_Object slot, name;
1987
1988 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1989 name = AREF (font_object, FONT_NAME_INDEX);
1990 if (NILP (Fmember (name, XCDR (slot))))
1991 nconc2 (slot, Fcons (name, Qnil));
1992 }
1993 }
1994 }
1995
1996 /* Store ALIST in TBL for characters C..TO. */
1997 if (c <= MAX_5_BYTE_CHAR)
1998 char_table_set_range (tables[k], c, to, alist);
1999 else
2000 XCHAR_TABLE (tables[k])->defalt = alist;
2001
2002 /* At last, change each elements to font names. */
2003 for (; CONSP (alist); alist = XCDR (alist))
2004 {
2005 elt = XCAR (alist);
2006 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
2007 }
2008 }
2009 c = to + 1;
2010 }
2011 if (EQ (fontset, Vdefault_fontset))
2012 break;
2013 }
2014
2015 return tables[0];
2016 }
2017
2018
2019 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2020 doc: /* Return a font name pattern for character CH in fontset NAME.
2021 If NAME is t, find a pattern in the default fontset.
2022 If NAME is nil, find a pattern in the fontset of the selected frame.
2023
2024 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2025 family name and REGISTRY is a font registry name. This is actually
2026 the first font name pattern for CH in the fontset or in the default
2027 fontset.
2028
2029 If the 2nd optional arg ALL is non-nil, return a list of all font name
2030 patterns. */)
2031 (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
2032 {
2033 int c;
2034 Lisp_Object fontset, elt, list, repertory, val;
2035 int i, j;
2036 Lisp_Object frame;
2037
2038 frame = Qnil;
2039 fontset = check_fontset_name (name, &frame);
2040
2041 CHECK_CHARACTER (ch);
2042 c = XINT (ch);
2043 list = Qnil;
2044 while (1)
2045 {
2046 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2047 i++, elt = FONTSET_FALLBACK (fontset))
2048 if (VECTORP (elt))
2049 for (j = 0; j < ASIZE (elt); j++)
2050 {
2051 Lisp_Object family, registry;
2052
2053 val = AREF (elt, j);
2054 if (NILP (val))
2055 return Qnil;
2056 repertory = AREF (val, 1);
2057 if (INTEGERP (repertory))
2058 {
2059 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2060
2061 if (! CHAR_CHARSET_P (c, charset))
2062 continue;
2063 }
2064 else if (CHAR_TABLE_P (repertory))
2065 {
2066 if (NILP (CHAR_TABLE_REF (repertory, c)))
2067 continue;
2068 }
2069 val = AREF (val, 0);
2070 /* VAL is a FONT-SPEC */
2071 family = AREF (val, FONT_FAMILY_INDEX);
2072 if (! NILP (family))
2073 family = SYMBOL_NAME (family);
2074 registry = AREF (val, FONT_REGISTRY_INDEX);
2075 if (! NILP (registry))
2076 registry = SYMBOL_NAME (registry);
2077 val = Fcons (family, registry);
2078 if (NILP (all))
2079 return val;
2080 list = Fcons (val, list);
2081 }
2082 if (EQ (fontset, Vdefault_fontset))
2083 break;
2084 fontset = Vdefault_fontset;
2085 }
2086 return (Fnreverse (list));
2087 }
2088
2089 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2090 doc: /* Return a list of all defined fontset names. */)
2091 (void)
2092 {
2093 Lisp_Object fontset, list;
2094 int i;
2095
2096 list = Qnil;
2097 for (i = 0; i < ASIZE (Vfontset_table); i++)
2098 {
2099 fontset = FONTSET_FROM_ID (i);
2100 if (!NILP (fontset)
2101 && BASE_FONTSET_P (fontset))
2102 list = Fcons (FONTSET_NAME (fontset), list);
2103 }
2104
2105 return list;
2106 }
2107
2108
2109 #ifdef FONTSET_DEBUG
2110
2111 Lisp_Object
2112 dump_fontset (Lisp_Object fontset)
2113 {
2114 Lisp_Object vec;
2115
2116 vec = Fmake_vector (make_number (3), Qnil);
2117 ASET (vec, 0, FONTSET_ID (fontset));
2118
2119 if (BASE_FONTSET_P (fontset))
2120 {
2121 ASET (vec, 1, FONTSET_NAME (fontset));
2122 }
2123 else
2124 {
2125 Lisp_Object frame;
2126
2127 frame = FONTSET_FRAME (fontset);
2128 if (FRAMEP (frame))
2129 {
2130 FRAME_PTR f = XFRAME (frame);
2131
2132 if (FRAME_LIVE_P (f))
2133 ASET (vec, 1,
2134 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2135 else
2136 ASET (vec, 1,
2137 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2138 }
2139 if (!NILP (FONTSET_DEFAULT (fontset)))
2140 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2141 }
2142 return vec;
2143 }
2144
2145 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2146 doc: /* Return a brief summary of all fontsets for debug use. */)
2147 (void)
2148 {
2149 Lisp_Object val;
2150 int i;
2151
2152 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2153 if (! NILP (AREF (Vfontset_table, i)))
2154 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2155 return (Fnreverse (val));
2156 }
2157 #endif /* FONTSET_DEBUG */
2158
2159 void
2160 syms_of_fontset (void)
2161 {
2162 DEFSYM (Qfontset, "fontset");
2163 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2164 DEFSYM (Qfontset_info, "fontset-info");
2165 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2166
2167 DEFSYM (Qprepend, "prepend");
2168 DEFSYM (Qappend, "append");
2169 DEFSYM (Qlatin, "latin");
2170
2171 Vcached_fontset_data = Qnil;
2172 staticpro (&Vcached_fontset_data);
2173
2174 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2175 staticpro (&Vfontset_table);
2176
2177 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2178 staticpro (&Vdefault_fontset);
2179 FONTSET_ID (Vdefault_fontset) = make_number (0);
2180 FONTSET_NAME (Vdefault_fontset)
2181 = make_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2182 ASET (Vfontset_table, 0, Vdefault_fontset);
2183 next_fontset_id = 1;
2184
2185 auto_fontset_alist = Qnil;
2186 staticpro (&auto_fontset_alist);
2187
2188 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist,
2189 doc: /*
2190 Alist of charsets vs the charsets to determine the preferred font encoding.
2191 Each element looks like (CHARSET . ENCODING-CHARSET),
2192 where ENCODING-CHARSET is a charset registered in the variable
2193 `font-encoding-alist' as ENCODING.
2194
2195 When a text has a property `charset' and the value is CHARSET, a font
2196 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2197 Vfont_encoding_charset_alist = Qnil;
2198
2199 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent,
2200 doc: /*
2201 Char table of characters whose ascent values should be ignored.
2202 If an entry for a character is non-nil, the ascent value of the glyph
2203 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2204
2205 This affects how a composite character which contains
2206 such a character is displayed on screen. */);
2207 Vuse_default_ascent = Qnil;
2208
2209 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition,
2210 doc: /*
2211 Char table of characters which are not composed relatively.
2212 If an entry for a character is non-nil, a composition sequence
2213 which contains that character is displayed so that
2214 the glyph of that character is put without considering
2215 an ascent and descent value of a previous character. */);
2216 Vignore_relative_composition = Qnil;
2217
2218 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist,
2219 doc: /* Alist of fontname vs list of the alternate fontnames.
2220 When a specified font name is not found, the corresponding
2221 alternate fontnames (if any) are tried instead. */);
2222 Valternate_fontname_alist = Qnil;
2223
2224 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist,
2225 doc: /* Alist of fontset names vs the aliases. */);
2226 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2227 make_pure_c_string ("fontset-default")),
2228 Qnil);
2229
2230 DEFVAR_LISP ("vertical-centering-font-regexp",
2231 Vvertical_centering_font_regexp,
2232 doc: /* *Regexp matching font names that require vertical centering on display.
2233 When a character is displayed with such fonts, the character is displayed
2234 at the vertical center of lines. */);
2235 Vvertical_centering_font_regexp = Qnil;
2236
2237 DEFVAR_LISP ("otf-script-alist", Votf_script_alist,
2238 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2239 Votf_script_alist = Qnil;
2240
2241 defsubr (&Squery_fontset);
2242 defsubr (&Snew_fontset);
2243 defsubr (&Sset_fontset_font);
2244 defsubr (&Sinternal_char_font);
2245 defsubr (&Sfontset_info);
2246 defsubr (&Sfontset_font);
2247 defsubr (&Sfontset_list);
2248 #ifdef FONTSET_DEBUG
2249 defsubr (&Sfontset_list_all);
2250 #endif
2251 }