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