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