Add a few more unit tests for JSON
[emacs.git] / src / fontset.c
blob985800103d2c5e45612bb6c013b1ce9e5b26c97b
1 /* Fontset handler.
3 Copyright (C) 2001-2017 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
12 This file is part of GNU Emacs.
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 (at
17 your option) any later version.
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.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
27 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "lisp.h"
32 #include "blockinput.h"
33 #include "character.h"
34 #include "charset.h"
35 #include "frame.h"
36 #include "dispextern.h"
37 #include "fontset.h"
38 #ifdef HAVE_WINDOW_SYSTEM
39 #include TERM_HEADER
40 #endif /* HAVE_WINDOW_SYSTEM */
41 #include "font.h"
43 /* FONTSET
45 A fontset is a collection of font related information to give
46 similar appearance (style, etc) of characters. A fontset has two
47 roles. One is to use for the frame parameter `font' as if it is an
48 ASCII font. In that case, Emacs uses the font specified for
49 `ascii' script for the frame's default font.
51 Another role, the more important one, is to provide information
52 about which font to use for each non-ASCII character.
54 There are two kinds of fontsets; base and realized. A base fontset
55 is created by `new-fontset' from Emacs Lisp explicitly. A realized
56 fontset is created implicitly when a face is realized for ASCII
57 characters. A face is also realized for non-ASCII characters based
58 on an ASCII face. All of non-ASCII faces based on the same ASCII
59 face share the same realized fontset.
61 A fontset object is implemented by a char-table whose default value
62 and parent are always nil.
64 An element of a base fontset is a vector of FONT-DEFs which themselves
65 are vectors of the form [ FONT-SPEC ENCODING REPERTORY ].
67 An element of a realized fontset is nil, t, 0, or a cons that has
68 this from:
70 (CHARSET-ORDERED-LIST-TICK . FONT-GROUP)
72 CHARSET_ORDERED_LIST_TICK is the same as charset_ordered_list_tick or -1.
74 FONT-GROUP is a vector of elements that have this form:
76 [ RFONT-DEF0 RFONT-DEF1 ... ]
78 Each RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
80 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
82 RFONT-DEFn are automatically reordered considering the current
83 charset priority list, the current language environment, and
84 priorities determined by font-backends.
86 RFONT-DEFn may not be a vector in the following cases.
88 The value nil means that we have not yet generated the above vector
89 from the base of the fontset.
91 The value t means that no font is available for the corresponding
92 range of characters.
94 The value 0 means that no font is available for the corresponding
95 range of characters in this fontset, but may be available in the
96 fallback font-group or in the default fontset.
98 A fontset has 8 extra slots.
100 The 1st slot:
101 base: the ID number of the fontset
102 realized: Likewise
104 The 2nd slot:
105 base: the name of the fontset
106 realized: nil
108 The 3rd slot:
109 base: the font name for ASCII characters
110 realized: nil
112 The 4th slot:
113 base: nil
114 realized: the base fontset
116 The 5th slot:
117 base: nil
118 realized: the frame that the fontset belongs to
120 The 6th slot:
121 base: nil
122 realized: the ID number of a face to use for characters that
123 has no font in a realized fontset.
125 The 7th slot:
126 base: nil
127 realized: If the base is not the default fontset, a fontset
128 realized from the default fontset, else nil.
130 The 8th slot:
131 base: Same as element value (but for fallback fonts).
132 realized: Likewise.
134 All fontsets are recorded in the vector Vfontset_table.
137 DEFAULT FONTSET
139 There's a special base fontset named `default fontset' which
140 defines the default font specifications. When a base fontset
141 doesn't specify a font for a specific character, the corresponding
142 value in the default fontset is used.
144 The parent of a realized fontset created for such a face that has
145 no fontset is the default fontset.
148 These structures are hidden from the other codes than this file.
149 The other codes handle fontsets only by their ID numbers. They
150 usually use the variable name `fontset' for IDs. But, in this
151 file, we always use variable name `id' for IDs, and name `fontset'
152 for an actual fontset object, i.e., char-table.
156 /********** VARIABLES and FUNCTION PROTOTYPES **********/
158 /* Vector containing all fontsets. */
159 static Lisp_Object Vfontset_table;
161 /* Next possibly free fontset ID. Usually this keeps the minimum
162 fontset ID not yet used. */
163 static int next_fontset_id;
165 /* The default fontset. This gives default FAMILY and REGISTRY of
166 font for each character. */
167 static Lisp_Object Vdefault_fontset;
169 /* Prototype declarations for static functions. */
170 static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
172 /* Return true if ID is a valid fontset id.
173 Optimized away if ENABLE_CHECKING is not defined. */
175 static bool
176 fontset_id_valid_p (int id)
178 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
183 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
185 /* Return the fontset with ID. No check of ID's validness. */
186 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
188 /* Access special values of FONTSET. */
190 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
191 static void
192 set_fontset_id (Lisp_Object fontset, Lisp_Object id)
194 set_char_table_extras (fontset, 0, id);
197 /* Access special values of (base) FONTSET. */
199 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
200 static void
201 set_fontset_name (Lisp_Object fontset, Lisp_Object name)
203 set_char_table_extras (fontset, 1, name);
206 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
207 static void
208 set_fontset_ascii (Lisp_Object fontset, Lisp_Object ascii)
210 set_char_table_extras (fontset, 2, ascii);
213 /* Access special values of (realized) FONTSET. */
215 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
216 static void
217 set_fontset_base (Lisp_Object fontset, Lisp_Object base)
219 set_char_table_extras (fontset, 3, base);
222 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
223 static void
224 set_fontset_frame (Lisp_Object fontset, Lisp_Object frame)
226 set_char_table_extras (fontset, 4, frame);
229 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
230 static void
231 set_fontset_nofont_face (Lisp_Object fontset, Lisp_Object face)
233 set_char_table_extras (fontset, 5, face);
236 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
237 static void
238 set_fontset_default (Lisp_Object fontset, Lisp_Object def)
240 set_char_table_extras (fontset, 6, def);
243 /* For both base and realized fontset. */
245 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
246 static void
247 set_fontset_fallback (Lisp_Object fontset, Lisp_Object fallback)
249 set_char_table_extras (fontset, 7, fallback);
252 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
254 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
255 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
256 do { \
257 (font_def) = make_uninit_vector (3); \
258 ASET ((font_def), 0, font_spec); \
259 ASET ((font_def), 1, encoding); \
260 ASET ((font_def), 2, repertory); \
261 } while (0)
263 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
264 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
265 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
267 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
268 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
269 ASET ((rfont_def), 0, make_number (face_id))
270 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
271 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
272 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
273 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
274 ASET ((rfont_def), 2, (object))
275 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
276 the order of listing by font backends, the higher bits represents
277 the order given by charset priority list. The smaller value is
278 preferable. */
279 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
280 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
281 ASET ((rfont_def), 3, make_number (score))
282 #define RFONT_DEF_NEW(rfont_def, font_def) \
283 do { \
284 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
285 ASET ((rfont_def), 1, (font_def)); \
286 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
287 } while (0)
290 /* Return the element of FONTSET for the character C. If FONTSET is a
291 base fontset other then the default fontset and FONTSET doesn't
292 contain information for C, return the information in the default
293 fontset. */
295 #define FONTSET_REF(fontset, c) \
296 (EQ (fontset, Vdefault_fontset) \
297 ? CHAR_TABLE_REF (fontset, c) \
298 : fontset_ref ((fontset), (c)))
300 static Lisp_Object
301 fontset_ref (Lisp_Object fontset, int c)
303 Lisp_Object elt;
305 elt = CHAR_TABLE_REF (fontset, c);
306 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
307 /* Don't check Vdefault_fontset for a realized fontset. */
308 && NILP (FONTSET_BASE (fontset)))
309 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
310 return elt;
313 /* Set elements of FONTSET for characters in RANGE to the value ELT.
314 RANGE is a cons (FROM . TO), where FROM and TO are character codes
315 specifying a range. */
317 #define FONTSET_SET(fontset, range, elt) \
318 Fset_char_table_range ((fontset), (range), (elt))
321 /* Modify the elements of FONTSET for characters in RANGE by replacing
322 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
323 and TO are character codes specifying a range. If ADD is nil,
324 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
325 append ELT. */
327 #define FONTSET_ADD(fontset, range, elt, add) \
328 (NILP (add) \
329 ? (NILP (range) \
330 ? (set_fontset_fallback \
331 (fontset, Fmake_vector (make_number (1), (elt)))) \
332 : ((void) \
333 Fset_char_table_range (fontset, range, \
334 Fmake_vector (make_number (1), elt)))) \
335 : fontset_add ((fontset), (range), (elt), (add)))
337 static void
338 fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
340 Lisp_Object args[2];
341 int idx = (EQ (add, Qappend) ? 0 : 1);
343 args[1 - idx] = Fmake_vector (make_number (1), elt);
345 if (CONSP (range))
347 int from = XINT (XCAR (range));
348 int to = XINT (XCDR (range));
349 int from1, to1;
351 do {
352 from1 = from, to1 = to;
353 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
354 char_table_set_range (fontset, from, to1,
355 (NILP (args[idx]) ? args[1 - idx]
356 : CALLMANY (Fvconcat, args)));
357 from = to1 + 1;
358 } while (from < to);
360 else
362 args[idx] = FONTSET_FALLBACK (fontset);
363 set_fontset_fallback (fontset,
364 (NILP (args[idx]) ? args[1 - idx]
365 : CALLMANY (Fvconcat, args)));
369 static int
370 fontset_compare_rfontdef (const void *val1, const void *val2)
372 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
373 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
376 /* Update a cons cell which has this form:
377 (CHARSET-ORDERED-LIST-TICK . FONT-GROUP)
378 where FONT-GROUP is of the form
379 [ PREFERRED-RFONT-DEF RFONT-DEF0 RFONT-DEF1 ... ]
380 Reorder RFONT-DEFs according to the current language, and update
381 CHARSET-ORDERED-LIST-TICK. */
383 static void
384 reorder_font_vector (Lisp_Object font_group, struct font *font)
386 Lisp_Object vec, font_object;
387 int size;
388 int i;
389 bool score_changed = false;
391 if (font)
392 XSETFONT (font_object, font);
393 else
394 font_object = Qnil;
396 vec = XCDR (font_group);
397 size = ASIZE (vec);
398 /* Exclude the tailing nil element from the reordering. */
399 if (NILP (AREF (vec, size - 1)))
400 size--;
402 for (i = 0; i < size; i++)
404 Lisp_Object rfont_def = AREF (vec, i);
405 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
406 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
407 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
408 Lisp_Object otf_spec = Ffont_get (font_spec, QCotf);
410 if (! NILP (otf_spec))
411 /* A font-spec with :otf is preferable regardless of encoding
412 and language.. */
414 else if (! font_match_p (font_spec, font_object))
416 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
418 if (! NILP (encoding))
420 /* This spec specifies an encoding by a charset set
421 name. Reflect the preference order of that charset
422 in the upper bits of SCORE. */
423 Lisp_Object tail;
425 for (tail = Vcharset_ordered_list;
426 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
427 tail = XCDR (tail))
428 if (EQ (encoding, XCAR (tail)))
429 break;
430 else if (score <= min (INT_MAX, MOST_POSITIVE_FIXNUM) - 0x100)
431 score += 0x100;
433 else
435 /* This spec does not specify an encoding. If the spec
436 specifies a language, and the language is not for the
437 current language environment, make the score
438 larger. */
439 Lisp_Object lang = Ffont_get (font_spec, QClang);
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;
448 if (RFONT_DEF_SCORE (rfont_def) != score)
450 RFONT_DEF_SET_SCORE (rfont_def, score);
451 score_changed = true;
455 if (score_changed)
456 qsort (XVECTOR (vec)->contents, size, word_size,
457 fontset_compare_rfontdef);
458 EMACS_INT low_tick_bits = charset_ordered_list_tick & MOST_POSITIVE_FIXNUM;
459 XSETCAR (font_group, make_number (low_tick_bits));
462 /* Return a font-group (actually a cons (CHARSET_ORDERED_LIST_TICK
463 . FONT-GROUP)) for character C or a fallback font-group in the
464 realized fontset FONTSET. The elements of FONT-GROUP are
465 RFONT-DEFs. The value may not be a cons. See the comment at the
466 head of this file for the detail of the return value. */
468 static Lisp_Object
469 fontset_get_font_group (Lisp_Object fontset, int c)
471 Lisp_Object font_group;
472 Lisp_Object base_fontset;
473 int from = 0, to = MAX_CHAR, i;
475 eassert (! BASE_FONTSET_P (fontset));
476 if (c >= 0)
477 font_group = CHAR_TABLE_REF (fontset, c);
478 else
479 font_group = FONTSET_FALLBACK (fontset);
480 if (! NILP (font_group))
481 /* We have already realized FONT-DEFs of this font group for C or
482 for fallback (FONT_GROUP is a cons), or we have already found
483 that no appropriate font was found (FONT_GROUP is t or 0). */
484 return font_group;
485 base_fontset = FONTSET_BASE (fontset);
486 if (NILP (base_fontset))
487 /* Actually we never come here because FONTSET is a realized one,
488 and thus it should have a base. */
489 font_group = Qnil;
490 else if (c >= 0)
491 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
492 else
493 font_group = FONTSET_FALLBACK (base_fontset);
495 /* FONT_GROUP not being a vector means that no fonts are specified
496 for C, or the fontset does not have fallback fonts. */
497 if (NILP (font_group))
499 font_group = make_number (0);
500 if (c >= 0)
501 /* Record that FONTSET does not specify fonts for C. As
502 there's a possibility that a font is found in a fallback
503 font group, we set 0 at the moment. */
504 char_table_set_range (fontset, from, to, font_group);
505 return font_group;
507 if (!VECTORP (font_group))
508 return font_group;
510 /* Now realize FONT-DEFs of this font group, and update the realized
511 fontset FONTSET. */
512 font_group = Fcopy_sequence (font_group);
513 for (i = 0; i < ASIZE (font_group); i++)
514 if (! NILP (AREF (font_group, i)))
516 Lisp_Object rfont_def;
518 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
519 /* Remember the original order. */
520 RFONT_DEF_SET_SCORE (rfont_def, i);
521 ASET (font_group, i, rfont_def);
523 font_group = Fcons (make_number (-1), font_group);
524 if (c >= 0)
525 char_table_set_range (fontset, from, to, font_group);
526 else
527 set_fontset_fallback (fontset, font_group);
528 return font_group;
531 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
532 character C. If no font is found, return Qnil or 0 if there's a
533 possibility that the default fontset or the fallback font groups
534 have a proper font, and return Qt if not.
536 If a font is found but is not yet opened, open it (if FACE is not
537 NULL) or return Qnil (if FACE is NULL).
539 CHARSET_ID is a charset-id that must be preferred, or -1 meaning no
540 preference.
542 If FALLBACK, search only fallback fonts. */
544 static Lisp_Object
545 fontset_find_font (Lisp_Object fontset, int c, struct face *face,
546 int charset_id, bool fallback)
548 Lisp_Object vec, font_group;
549 int i, charset_matched = 0, found_index;
550 struct frame *f = (FRAMEP (FONTSET_FRAME (fontset))
551 ? XFRAME (FONTSET_FRAME (fontset))
552 : XFRAME (selected_frame));
553 Lisp_Object rfont_def;
555 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
556 if (! CONSP (font_group))
557 return font_group;
558 vec = XCDR (font_group);
559 if (ASIZE (vec) == 0)
560 return Qnil;
562 if (ASIZE (vec) > 1)
564 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
565 /* We have just created the font-group,
566 or the charset priorities were changed. */
567 reorder_font_vector (font_group, face->ascii_face->font);
568 if (charset_id >= 0)
569 /* Find a spec matching with CHARSET_ID to try it at
570 first. */
571 for (i = 0; i < ASIZE (vec); i++)
573 Lisp_Object repertory;
575 rfont_def = AREF (vec, i);
576 if (NILP (rfont_def))
577 break;
578 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
580 if (XINT (repertory) == charset_id)
582 charset_matched = i;
583 break;
588 /* Find the first available font in the vector of RFONT-DEF. If
589 CHARSET_MATCHED > 0, try the corresponding RFONT-DEF first, then
590 try the rest. */
591 for (i = 0; i < ASIZE (vec); i++)
593 Lisp_Object font_def;
594 Lisp_Object font_entity, font_object;
596 found_index = i;
597 if (i == 0)
599 if (charset_matched > 0)
601 /* Try the element matching with CHARSET_ID at first. */
602 found_index = charset_matched;
603 /* Make this negative so that we don't come here in the
604 next loop. */
605 charset_matched = - charset_matched;
606 /* We must try the first element in the next loop. */
607 i = -1;
610 else if (i == - charset_matched)
612 /* We have already tried this element and the followings
613 that have the same font specifications in the first
614 iteration. So, skip them all. */
615 rfont_def = AREF (vec, i);
616 font_def = RFONT_DEF_FONT_DEF (rfont_def);
617 for (; i + 1 < ASIZE (vec); i++)
619 rfont_def = AREF (vec, i + 1);
620 if (NILP (rfont_def))
621 break;
622 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
623 break;
625 continue;
628 rfont_def = AREF (vec, found_index);
629 if (NILP (rfont_def))
631 if (i < 0)
632 continue;
633 /* This is a sign of not to try the other fonts. */
634 return Qt;
636 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
637 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
638 /* We couldn't open this font last time. */
639 continue;
641 font_object = RFONT_DEF_OBJECT (rfont_def);
642 if (NILP (font_object))
644 font_def = RFONT_DEF_FONT_DEF (rfont_def);
646 if (! face)
647 /* We have not yet opened the font. */
648 return Qnil;
649 /* Find a font best-matching with the spec without checking
650 the support of the character C. That checking is costly,
651 and even without the checking, the found font supports C
652 in high possibility. */
653 font_entity = font_find_for_lface (f, face->lface,
654 FONT_DEF_SPEC (font_def), -1);
655 if (NILP (font_entity))
657 /* Record that no font matches the spec. */
658 RFONT_DEF_SET_FACE (rfont_def, -1);
659 continue;
661 font_object = font_open_for_lface (f, font_entity, face->lface,
662 FONT_DEF_SPEC (font_def));
663 if (NILP (font_object))
665 /* Something strange happened, perhaps because of a
666 Font-backend problem. To avoid crashing, record
667 that this spec is unusable. It may be better to find
668 another font of the same spec, but currently we don't
669 have such an API in font-backend. */
670 RFONT_DEF_SET_FACE (rfont_def, -1);
671 continue;
673 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
676 if (font_has_char (f, font_object, c))
677 goto found;
679 /* Find a font already opened, matching with the current spec,
680 and supporting C. */
681 font_def = RFONT_DEF_FONT_DEF (rfont_def);
682 for (; found_index + 1 < ASIZE (vec); found_index++)
684 rfont_def = AREF (vec, found_index + 1);
685 if (NILP (rfont_def))
686 break;
687 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
688 break;
689 font_object = RFONT_DEF_OBJECT (rfont_def);
690 if (! NILP (font_object) && font_has_char (f, font_object, c))
692 found_index++;
693 goto found;
697 /* Find a font-entity with the current spec and supporting C. */
698 font_entity = font_find_for_lface (f, face->lface,
699 FONT_DEF_SPEC (font_def), c);
700 if (! NILP (font_entity))
702 /* We found a font. Open it and insert a new element for
703 that font in VEC. */
704 Lisp_Object new_vec;
705 int j;
707 font_object = font_open_for_lface (f, font_entity, face->lface,
708 Qnil);
709 if (NILP (font_object))
710 continue;
711 RFONT_DEF_NEW (rfont_def, font_def);
712 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
713 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
714 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
715 found_index++;
716 for (j = 0; j < found_index; j++)
717 ASET (new_vec, j, AREF (vec, j));
718 ASET (new_vec, j, rfont_def);
719 for (j++; j < ASIZE (new_vec); j++)
720 ASET (new_vec, j, AREF (vec, j - 1));
721 XSETCDR (font_group, new_vec);
722 vec = new_vec;
723 goto found;
725 if (i >= 0)
726 i = found_index;
729 /* Record that no font in this font group supports C. */
730 FONTSET_SET (fontset, make_number (c), make_number (0));
731 return Qnil;
733 found:
734 if (fallback && found_index > 0)
736 /* The order of fonts in the fallback font-group is not that
737 important, and it is better to move the found font to the
738 first of the group so that the next try will find it
739 quickly. */
740 for (i = found_index; i > 0; i--)
741 ASET (vec, i, AREF (vec, i - 1));
742 ASET (vec, 0, rfont_def);
744 return rfont_def;
748 /* Return RFONT-DEF (vector) corresponding to the font for character
749 C. The value is not a vector if no font is found for C. */
751 static Lisp_Object
752 fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
754 Lisp_Object rfont_def;
755 Lisp_Object default_rfont_def UNINIT;
756 Lisp_Object base_fontset;
758 /* Try a font-group of FONTSET. */
759 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
760 rfont_def = fontset_find_font (fontset, c, face, id, 0);
761 if (VECTORP (rfont_def))
762 return rfont_def;
763 if (NILP (rfont_def))
764 FONTSET_SET (fontset, make_number (c), make_number (0));
766 /* Try a font-group of the default fontset. */
767 base_fontset = FONTSET_BASE (fontset);
768 if (! EQ (base_fontset, Vdefault_fontset))
770 if (NILP (FONTSET_DEFAULT (fontset)))
771 set_fontset_default
772 (fontset,
773 make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset));
774 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
775 default_rfont_def
776 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
777 if (VECTORP (default_rfont_def))
778 return default_rfont_def;
779 if (NILP (default_rfont_def))
780 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
781 make_number (0));
784 /* Try a fallback font-group of FONTSET. */
785 if (! EQ (rfont_def, Qt))
787 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
788 rfont_def = fontset_find_font (fontset, c, face, id, 1);
789 if (VECTORP (rfont_def))
790 return rfont_def;
791 /* Remember that FONTSET has no font for C. */
792 FONTSET_SET (fontset, make_number (c), Qt);
795 /* Try a fallback font-group of the default fontset. */
796 if (! EQ (base_fontset, Vdefault_fontset)
797 && ! EQ (default_rfont_def, Qt))
799 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
800 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
801 if (VECTORP (rfont_def))
802 return rfont_def;
803 /* Remember that the default fontset has no font for C. */
804 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
807 return Qnil;
810 /* Return a newly created fontset with NAME. If BASE is nil, make a
811 base fontset. Otherwise make a realized fontset whose base is
812 BASE. */
814 static Lisp_Object
815 make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
817 Lisp_Object fontset;
818 int size = ASIZE (Vfontset_table);
819 int id = next_fontset_id;
821 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
822 the next available fontset ID. So it is expected that this loop
823 terminates quickly. In addition, as the last element of
824 Vfontset_table is always nil, we don't have to check the range of
825 id. */
826 while (!NILP (AREF (Vfontset_table, id))) id++;
828 if (id + 1 == size)
829 Vfontset_table = larger_vector (Vfontset_table, 1, -1);
831 fontset = Fmake_char_table (Qfontset, Qnil);
833 set_fontset_id (fontset, make_number (id));
834 if (NILP (base))
835 set_fontset_name (fontset, name);
836 else
838 set_fontset_name (fontset, Qnil);
839 set_fontset_frame (fontset, frame);
840 set_fontset_base (fontset, base);
843 ASET (Vfontset_table, id, fontset);
844 next_fontset_id = id + 1;
845 return fontset;
849 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
851 /* Return the name of the fontset who has ID. */
853 Lisp_Object
854 fontset_name (int id)
856 Lisp_Object fontset;
858 fontset = FONTSET_FROM_ID (id);
859 return FONTSET_NAME (fontset);
863 /* Return the ASCII font name of the fontset who has ID. */
865 Lisp_Object
866 fontset_ascii (int id)
868 Lisp_Object fontset, elt;
870 fontset= FONTSET_FROM_ID (id);
871 elt = FONTSET_ASCII (fontset);
872 if (CONSP (elt))
873 elt = XCAR (elt);
874 return elt;
877 /* Free fontset of FACE defined on frame F. Called from
878 free_realized_face. */
880 void
881 free_face_fontset (struct frame *f, struct face *face)
883 Lisp_Object fontset;
885 fontset = FONTSET_FROM_ID (face->fontset);
886 if (NILP (fontset))
887 return;
888 eassert (! BASE_FONTSET_P (fontset));
889 eassert (f == XFRAME (FONTSET_FRAME (fontset)));
890 ASET (Vfontset_table, face->fontset, Qnil);
891 if (face->fontset < next_fontset_id)
892 next_fontset_id = face->fontset;
893 if (! NILP (FONTSET_DEFAULT (fontset)))
895 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
897 fontset = AREF (Vfontset_table, id);
898 eassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
899 eassert (f == XFRAME (FONTSET_FRAME (fontset)));
900 ASET (Vfontset_table, id, Qnil);
901 if (id < next_fontset_id)
902 next_fontset_id = face->fontset;
904 face->fontset = -1;
907 /* Return ID of face suitable for displaying character C at buffer position
908 POS on frame F. FACE must be realized for ASCII characters in advance.
909 Called from the macro FACE_FOR_CHAR. */
912 face_for_char (struct frame *f, struct face *face, int c,
913 ptrdiff_t pos, Lisp_Object object)
915 Lisp_Object fontset, rfont_def, charset;
916 int face_id;
917 int id;
919 eassert (fontset_id_valid_p (face->fontset));
921 if (ASCII_CHAR_P (c) || CHAR_BYTE8_P (c))
922 return face->ascii_face->id;
924 if (use_default_font_for_symbols /* let the user disable this feature */
925 && c > 0 && EQ (CHAR_TABLE_REF (Vchar_script_table, c), Qsymbol))
927 /* Fonts often have characters for punctuation and other
928 symbols, even if they don't match the 'symbol' script. So
929 check if the character is present in the current ASCII face
930 first, and if so, use the same font as used by that face.
931 This avoids unnecessarily switching to another font when the
932 frame's default font will do. We only do this for symbols so
933 that users could still setup fontsets to force Emacs to use
934 specific fonts for characters from other scripts, because
935 choice of fonts is frequently affected by cultural
936 preferences and font features, not by font coverage.
937 However, these considerations are unlikely to be relevant to
938 punctuation and other symbols, since the latter generally
939 aren't specific to any culture, and don't require
940 sophisticated OTF features. */
941 Lisp_Object font_object;
943 if (face->ascii_face->font)
945 XSETFONT (font_object, face->ascii_face->font);
946 if (font_has_char (f, font_object, c))
947 return face->ascii_face->id;
950 #if 0
951 /* Try the current face. Disabled because it can cause
952 counter-intuitive results, whereby the font used for some
953 character depends on the characters that precede it on
954 display. See the discussion of bug #15138. Note that the
955 original bug reported in #15138 was in a situation where face
956 == face->ascii_face, so the above code solves that situation
957 without risking the undesirable consequences. */
958 if (face->font)
960 XSETFONT (font_object, face->font);
961 if (font_has_char (f, font_object, c)) return face->id;
963 #endif
966 fontset = FONTSET_FROM_ID (face->fontset);
967 eassert (!BASE_FONTSET_P (fontset));
969 if (pos < 0)
971 id = -1;
972 charset = Qnil;
974 else
976 charset = Fget_char_property (make_number (pos), Qcharset, object);
977 if (CHARSETP (charset))
979 Lisp_Object val;
981 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
982 if (CONSP (val) && CHARSETP (XCDR (val)))
983 charset = XCDR (val);
984 id = XINT (CHARSET_SYMBOL_ID (charset));
986 else
987 id = -1;
990 rfont_def = fontset_font (fontset, c, face, id);
991 if (VECTORP (rfont_def))
993 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
994 face_id = XINT (RFONT_DEF_FACE (rfont_def));
995 else
997 Lisp_Object font_object;
999 font_object = RFONT_DEF_OBJECT (rfont_def);
1000 face_id = face_for_font (f, font_object, face);
1001 RFONT_DEF_SET_FACE (rfont_def, face_id);
1004 else
1006 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
1007 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
1008 else
1010 face_id = face_for_font (f, Qnil, face);
1011 set_fontset_nofont_face (fontset, make_number (face_id));
1014 eassert (face_id >= 0);
1015 return face_id;
1019 Lisp_Object
1020 font_for_char (struct face *face, int c, ptrdiff_t pos, Lisp_Object object)
1022 Lisp_Object fontset, rfont_def, charset;
1023 int id;
1025 if (ASCII_CHAR_P (c))
1027 Lisp_Object font_object;
1029 XSETFONT (font_object, face->ascii_face->font);
1030 return font_object;
1033 eassert (fontset_id_valid_p (face->fontset));
1034 fontset = FONTSET_FROM_ID (face->fontset);
1035 eassert (!BASE_FONTSET_P (fontset));
1036 if (pos < 0)
1038 id = -1;
1039 charset = Qnil;
1041 else
1043 charset = Fget_char_property (make_number (pos), Qcharset, object);
1044 if (CHARSETP (charset))
1046 Lisp_Object val;
1048 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
1049 if (CONSP (val) && CHARSETP (XCDR (val)))
1050 charset = XCDR (val);
1051 id = XINT (CHARSET_SYMBOL_ID (charset));
1053 else
1054 id = -1;
1057 rfont_def = fontset_font (fontset, c, face, id);
1058 return (VECTORP (rfont_def)
1059 ? RFONT_DEF_OBJECT (rfont_def)
1060 : Qnil);
1064 /* Make a realized fontset for ASCII face FACE on frame F from the
1065 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1066 default fontset as the base. Value is the id of the new fontset.
1067 Called from realize_x_face. */
1070 make_fontset_for_ascii_face (struct frame *f, int base_fontset_id, struct face *face)
1072 Lisp_Object base_fontset, fontset, frame;
1074 XSETFRAME (frame, f);
1075 if (base_fontset_id >= 0)
1077 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1078 if (!BASE_FONTSET_P (base_fontset))
1079 base_fontset = FONTSET_BASE (base_fontset);
1080 eassert (BASE_FONTSET_P (base_fontset));
1082 else
1083 base_fontset = Vdefault_fontset;
1085 fontset = make_fontset (frame, Qnil, base_fontset);
1086 return XINT (FONTSET_ID (fontset));
1091 /* Cache data used by fontset_pattern_regexp. The car part is a
1092 pattern string containing at least one wild card, the cdr part is
1093 the corresponding regular expression. */
1094 static Lisp_Object Vcached_fontset_data;
1096 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1097 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1099 /* If fontset name PATTERN contains any wild card, return regular
1100 expression corresponding to PATTERN. */
1102 static Lisp_Object
1103 fontset_pattern_regexp (Lisp_Object pattern)
1105 if (!strchr (SSDATA (pattern), '*')
1106 && !strchr (SSDATA (pattern), '?'))
1107 /* PATTERN does not contain any wild cards. */
1108 return Qnil;
1110 if (!CONSP (Vcached_fontset_data)
1111 || strcmp (SSDATA (pattern), CACHED_FONTSET_NAME))
1113 /* We must at first update the cached data. */
1114 unsigned char *regex, *p0, *p1;
1115 int ndashes = 0, nstars = 0, nescs = 0;
1117 for (p0 = SDATA (pattern); *p0; p0++)
1119 if (*p0 == '-')
1120 ndashes++;
1121 else if (*p0 == '*')
1122 nstars++;
1123 else if (*p0 == '['
1124 || *p0 == '.' || *p0 == '\\'
1125 || *p0 == '+' || *p0 == '^'
1126 || *p0 == '$')
1127 nescs++;
1130 /* If PATTERN is not full XLFD we convert "*" to ".*". Otherwise
1131 we convert "*" to "[^-]*" which is much faster in regular
1132 expression matching. */
1133 ptrdiff_t regexsize = (SBYTES (pattern)
1134 + (ndashes < 14 ? 2 : 5) * nstars
1135 + 2 * nescs + 3);
1136 USE_SAFE_ALLOCA;
1137 p1 = regex = SAFE_ALLOCA (regexsize);
1139 *p1++ = '^';
1140 for (p0 = SDATA (pattern); *p0; p0++)
1142 if (*p0 == '*')
1144 if (ndashes < 14)
1145 *p1++ = '.';
1146 else
1147 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1148 *p1++ = '*';
1150 else if (*p0 == '?')
1151 *p1++ = '.';
1152 else if (*p0 == '['
1153 || *p0 == '.' || *p0 == '\\'
1154 || *p0 == '+' || *p0 == '^'
1155 || *p0 == '$')
1156 *p1++ = '\\', *p1++ = *p0;
1157 else
1158 *p1++ = *p0;
1160 *p1++ = '$';
1161 *p1++ = 0;
1163 Vcached_fontset_data = Fcons (build_string (SSDATA (pattern)),
1164 build_string ((char *) regex));
1165 SAFE_FREE ();
1168 return CACHED_FONTSET_REGEX;
1171 /* Return ID of the base fontset named NAME. If there's no such
1172 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1173 0: pattern containing '*' and '?' as wildcards
1174 1: regular expression
1175 2: literal fontset name
1179 fs_query_fontset (Lisp_Object name, int name_pattern)
1181 Lisp_Object tem;
1182 int i;
1184 name = Fdowncase (name);
1185 if (name_pattern != 1)
1187 tem = Frassoc (name, Vfontset_alias_alist);
1188 if (NILP (tem))
1189 tem = Fassoc (name, Vfontset_alias_alist, Qnil);
1190 if (CONSP (tem) && STRINGP (XCAR (tem)))
1191 name = XCAR (tem);
1192 else if (name_pattern == 0)
1194 tem = fontset_pattern_regexp (name);
1195 if (STRINGP (tem))
1197 name = tem;
1198 name_pattern = 1;
1203 for (i = 0; i < ASIZE (Vfontset_table); i++)
1205 Lisp_Object fontset, this_name;
1207 fontset = FONTSET_FROM_ID (i);
1208 if (NILP (fontset)
1209 || !BASE_FONTSET_P (fontset))
1210 continue;
1212 this_name = FONTSET_NAME (fontset);
1213 if (name_pattern == 1
1214 ? fast_string_match_ignore_case (name, this_name) >= 0
1215 : !xstrcasecmp (SSDATA (name), SSDATA (this_name)))
1216 return i;
1218 return -1;
1222 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1223 doc: /* Return the name of a fontset that matches PATTERN.
1224 The value is nil if there is no matching fontset.
1225 PATTERN can contain `*' or `?' as a wildcard
1226 just as X font name matching algorithm allows.
1227 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1228 (Lisp_Object pattern, Lisp_Object regexpp)
1230 Lisp_Object fontset;
1231 int id;
1233 check_window_system (NULL);
1235 CHECK_STRING (pattern);
1237 if (SCHARS (pattern) == 0)
1238 return Qnil;
1240 id = fs_query_fontset (pattern, !NILP (regexpp));
1241 if (id < 0)
1242 return Qnil;
1244 fontset = FONTSET_FROM_ID (id);
1245 return FONTSET_NAME (fontset);
1248 /* Return a list of base fontset names matching PATTERN on frame F. */
1250 Lisp_Object
1251 list_fontsets (struct frame *f, Lisp_Object pattern, int size)
1253 Lisp_Object frame, regexp, val;
1254 int id;
1256 XSETFRAME (frame, f);
1258 regexp = fontset_pattern_regexp (pattern);
1259 val = Qnil;
1261 for (id = 0; id < ASIZE (Vfontset_table); id++)
1263 Lisp_Object fontset, name;
1265 fontset = FONTSET_FROM_ID (id);
1266 if (NILP (fontset)
1267 || !BASE_FONTSET_P (fontset)
1268 || !EQ (frame, FONTSET_FRAME (fontset)))
1269 continue;
1270 name = FONTSET_NAME (fontset);
1272 if (STRINGP (regexp)
1273 ? (fast_string_match (regexp, name) < 0)
1274 : strcmp (SSDATA (pattern), SSDATA (name)))
1275 continue;
1277 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1280 return val;
1284 /* Free all realized fontsets whose base fontset is BASE. */
1286 static void
1287 free_realized_fontsets (Lisp_Object base)
1289 int id;
1291 #if 0
1292 /* For the moment, this doesn't work because free_realized_face
1293 doesn't remove FACE from a cache. Until we find a solution, we
1294 suppress this code, and simply use Fclear_face_cache even though
1295 that is not efficient. */
1296 block_input ();
1297 for (id = 0; id < ASIZE (Vfontset_table); id++)
1299 Lisp_Object this = AREF (Vfontset_table, id);
1301 if (EQ (FONTSET_BASE (this), base))
1303 Lisp_Object tail;
1305 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1306 tail = XCDR (tail))
1308 struct frame *f = XFRAME (FONTSET_FRAME (this));
1309 int face_id = XINT (XCDR (XCAR (tail)));
1310 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1312 /* Face THIS itself is also freed by the following call. */
1313 free_realized_face (f, face);
1317 unblock_input ();
1318 #else /* not 0 */
1319 /* But, we don't have to call Fclear_face_cache if no fontset has
1320 been realized from BASE. */
1321 for (id = 0; id < ASIZE (Vfontset_table); id++)
1323 Lisp_Object this = AREF (Vfontset_table, id);
1325 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1327 Fclear_face_cache (Qt);
1328 /* This is in case some Lisp calls this function and then
1329 proceeds with calling some other function, like font-at,
1330 which needs the basic faces. */
1331 recompute_basic_faces (XFRAME (FONTSET_FRAME (this)));
1332 break;
1335 #endif /* not 0 */
1339 /* Check validity of NAME as a fontset name and return the
1340 corresponding fontset. If not valid, signal an error.
1342 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1343 fontset of *FRAME.
1345 Set *FRAME to the actual frame. */
1347 static Lisp_Object
1348 check_fontset_name (Lisp_Object name, Lisp_Object *frame)
1350 int id;
1351 struct frame *f = decode_live_frame (*frame);
1353 XSETFRAME (*frame, f);
1355 if (EQ (name, Qt))
1356 return Vdefault_fontset;
1357 if (NILP (name))
1358 id = FRAME_FONTSET (f);
1359 else
1361 CHECK_STRING (name);
1362 /* First try NAME as literal. */
1363 id = fs_query_fontset (name, 2);
1364 if (id < 0)
1365 /* For backward compatibility, try again NAME as pattern. */
1366 id = fs_query_fontset (name, 0);
1367 if (id < 0)
1368 error ("Fontset `%s' does not exist", SDATA (name));
1370 return FONTSET_FROM_ID (id);
1373 static void
1374 accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
1376 if (EQ (XCAR (arg), val))
1378 if (CONSP (range))
1379 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1380 else
1381 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1386 /* Callback function for map_charset_chars in Fset_fontset_font.
1387 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1389 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1390 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1391 The consumed ranges are popped up from SCRIPT_RANGE_LIST, and the
1392 new SCRIPT_RANGE_LIST is stored in ARG.
1394 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1395 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1396 case. */
1398 static void
1399 set_fontset_font (Lisp_Object arg, Lisp_Object range)
1401 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1402 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1404 fontset = AREF (arg, 0);
1405 font_def = AREF (arg, 1);
1406 add = AREF (arg, 2);
1407 ascii = AREF (arg, 3);
1408 script_range_list = AREF (arg, 4);
1410 if (NILP (ascii) && from < 0x80)
1412 if (to < 0x80)
1413 return;
1414 from = 0x80;
1415 range = Fcons (make_number (0x80), XCDR (range));
1418 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1419 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1420 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1422 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1423 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1424 if (CONSP (script_range_list))
1426 if (SCRIPT_FROM < from)
1427 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1428 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1429 POP_SCRIPT_RANGE ();
1430 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1431 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1434 FONTSET_ADD (fontset, range, font_def, add);
1435 ASET (arg, 4, script_range_list);
1438 static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
1441 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1442 doc: /*
1443 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1445 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1446 the default fontset.
1448 TARGET may be a single character to use FONT-SPEC for.
1450 Target may be a cons (FROM . TO), where FROM and TO are characters.
1451 In that case, use FONT-SPEC for all characters in the range FROM
1452 and TO (inclusive).
1454 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1455 all characters that belong to the script.
1457 TARGET may be a charset. In that case, use FONT-SPEC for all
1458 characters in the charset.
1460 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1461 that no FONT-SPEC is specified.
1463 FONT-SPEC may one of these:
1464 * A font-spec object made by the function `font-spec' (which see).
1465 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1466 REGISTRY is a font registry name. FAMILY may contain foundry
1467 name, and REGISTRY may contain encoding name.
1468 * A font name string.
1469 * nil, which explicitly specifies that there's no font for TARGET.
1471 Optional 4th argument FRAME is a frame or nil for the selected frame
1472 that is concerned in the case that NAME is nil.
1474 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1475 to the font specifications for TARGET previously set. If it is
1476 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1477 appended. By default, FONT-SPEC overrides the previous settings. */)
1478 (Lisp_Object name, Lisp_Object target, Lisp_Object font_spec, Lisp_Object frame, Lisp_Object add)
1480 Lisp_Object fontset;
1481 Lisp_Object font_def, registry, family;
1482 Lisp_Object range_list;
1483 struct charset *charset = NULL;
1484 Lisp_Object fontname;
1485 bool ascii_changed = 0;
1487 fontset = check_fontset_name (name, &frame);
1489 fontname = Qnil;
1490 if (CONSP (font_spec))
1492 Lisp_Object spec = Ffont_spec (0, NULL);
1494 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1495 font_spec = spec;
1496 fontname = Ffont_xlfd_name (font_spec, Qnil);
1498 else if (STRINGP (font_spec))
1500 fontname = font_spec;
1501 font_spec = CALLN (Ffont_spec, QCname, fontname);
1503 else if (FONT_SPEC_P (font_spec))
1504 fontname = Ffont_xlfd_name (font_spec, Qnil);
1505 else if (! NILP (font_spec))
1506 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1508 if (! NILP (font_spec))
1510 Lisp_Object encoding, repertory;
1512 family = AREF (font_spec, FONT_FAMILY_INDEX);
1513 if (! NILP (family) )
1514 family = SYMBOL_NAME (family);
1515 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1516 if (! NILP (registry))
1517 registry = Fdowncase (SYMBOL_NAME (registry));
1518 AUTO_STRING (dash, "-");
1519 encoding = find_font_encoding (concat3 (family, dash, registry));
1520 if (NILP (encoding))
1521 encoding = Qascii;
1523 if (SYMBOLP (encoding))
1525 CHECK_CHARSET (encoding);
1526 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1528 else
1530 repertory = XCDR (encoding);
1531 encoding = XCAR (encoding);
1532 CHECK_CHARSET (encoding);
1533 encoding = CHARSET_SYMBOL_ID (encoding);
1534 if (! NILP (repertory) && SYMBOLP (repertory))
1536 CHECK_CHARSET (repertory);
1537 repertory = CHARSET_SYMBOL_ID (repertory);
1540 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1542 else
1543 font_def = Qnil;
1545 if (CHARACTERP (target))
1547 if (XFASTINT (target) < 0x80)
1548 error ("Can't set a font for partial ASCII range");
1549 range_list = list1 (Fcons (target, target));
1551 else if (CONSP (target))
1553 Lisp_Object from, to;
1555 from = Fcar (target);
1556 to = Fcdr (target);
1557 CHECK_CHARACTER (from);
1558 CHECK_CHARACTER (to);
1559 if (XFASTINT (from) < 0x80)
1561 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1562 error ("Can't set a font for partial ASCII range");
1563 ascii_changed = 1;
1565 range_list = list1 (target);
1567 else if (SYMBOLP (target) && !NILP (target))
1569 Lisp_Object script_list;
1570 Lisp_Object val;
1572 range_list = Qnil;
1573 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1574 if (! NILP (Fmemq (target, script_list)))
1576 if (EQ (target, Qlatin))
1577 ascii_changed = 1;
1578 val = list1 (target);
1579 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1580 val);
1581 range_list = Fnreverse (XCDR (val));
1583 if (CHARSETP (target))
1585 CHECK_CHARSET_GET_CHARSET (target, charset);
1586 if (charset->ascii_compatible_p)
1587 ascii_changed = 1;
1589 else if (NILP (range_list))
1590 error ("Invalid script or charset name: %s",
1591 SDATA (SYMBOL_NAME (target)));
1593 else if (NILP (target))
1594 range_list = list1 (Qnil);
1595 else
1596 error ("Invalid target for setting a font");
1598 if (ascii_changed)
1600 Lisp_Object val;
1602 if (NILP (font_spec))
1603 error ("Can't set ASCII font to nil");
1604 val = CHAR_TABLE_REF (fontset, 0);
1605 if (! NILP (val) && EQ (add, Qappend))
1606 /* We are going to change just an additional font for ASCII. */
1607 ascii_changed = 0;
1610 if (charset)
1612 Lisp_Object arg;
1614 arg = make_uninit_vector (5);
1615 ASET (arg, 0, fontset);
1616 ASET (arg, 1, font_def);
1617 ASET (arg, 2, add);
1618 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1619 ASET (arg, 4, range_list);
1621 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1622 CHARSET_MIN_CODE (charset),
1623 CHARSET_MAX_CODE (charset));
1624 range_list = AREF (arg, 4);
1626 for (; CONSP (range_list); range_list = XCDR (range_list))
1627 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1629 if (ascii_changed)
1631 Lisp_Object tail, fr;
1632 int fontset_id = XINT (FONTSET_ID (fontset));
1634 set_fontset_ascii (fontset, fontname);
1635 name = FONTSET_NAME (fontset);
1636 FOR_EACH_FRAME (tail, fr)
1638 struct frame *f = XFRAME (fr);
1639 Lisp_Object font_object;
1640 struct face *face;
1642 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1643 continue;
1644 if (fontset_id != FRAME_FONTSET (f))
1645 continue;
1646 face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
1647 if (face)
1648 font_object = font_load_for_lface (f, face->lface, font_spec);
1649 else
1650 font_object = font_open_by_spec (f, font_spec);
1651 if (! NILP (font_object))
1653 update_auto_fontset_alist (font_object, fontset);
1654 AUTO_FRAME_ARG (arg, Qfont, Fcons (name, font_object));
1655 Fmodify_frame_parameters (fr, arg);
1660 /* Free all realized fontsets whose base is FONTSET. This way, the
1661 specified character(s) are surely redisplayed by a correct
1662 font. */
1663 free_realized_fontsets (fontset);
1665 return Qnil;
1669 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1670 doc: /* Create a new fontset NAME from font information in FONTLIST.
1672 FONTLIST is an alist of scripts vs the corresponding font specification list.
1673 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1674 character of SCRIPT is displayed by a font that matches one of
1675 FONT-SPEC.
1677 SCRIPT is a symbol that appears in the first extra slot of the
1678 char-table `char-script-table'.
1680 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1681 `set-fontset-font' for the meaning. */)
1682 (Lisp_Object name, Lisp_Object fontlist)
1684 Lisp_Object fontset, tail;
1685 int id;
1687 CHECK_STRING (name);
1689 name = Fdowncase (name);
1690 id = fs_query_fontset (name, 0);
1691 if (id < 0)
1693 Lisp_Object font_spec = Ffont_spec (0, NULL);
1694 Lisp_Object short_name;
1695 char xlfd[256];
1696 int len;
1698 if (font_parse_xlfd (SSDATA (name), SBYTES (name), font_spec) < 0)
1699 error ("Fontset name must be in XLFD format");
1700 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1701 if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1702 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1703 error ("Registry field of fontset name must be \"fontset-*\"");
1704 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1705 Vfontset_alias_alist);
1706 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1707 fontset = make_fontset (Qnil, name, Qnil);
1708 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1709 if (len < 0)
1710 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1711 set_fontset_ascii (fontset, make_unibyte_string (xlfd, len));
1713 else
1715 fontset = FONTSET_FROM_ID (id);
1716 free_realized_fontsets (fontset);
1717 Fset_char_table_range (fontset, Qt, Qnil);
1720 for (tail = fontlist; CONSP (tail); tail = XCDR (tail))
1722 Lisp_Object elt, script;
1724 elt = XCAR (tail);
1725 script = Fcar (elt);
1726 elt = Fcdr (elt);
1727 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1728 for (; CONSP (elt); elt = XCDR (elt))
1729 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1730 else
1731 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1733 CHECK_LIST_END (tail, fontlist);
1734 return name;
1738 /* Alist of automatically created fontsets. Each element is a cons
1739 (FONT-SPEC . FONTSET-ID). */
1740 static Lisp_Object auto_fontset_alist;
1742 /* Number of automatically created fontsets. */
1743 static ptrdiff_t num_auto_fontsets;
1745 /* Return a fontset synthesized from FONT-OBJECT. This is called from
1746 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1747 frame, and the returned fontset is used for the default fontset of
1748 that frame. The fontset specifies a font of the same registry as
1749 FONT-OBJECT for all characters in the repertory of the registry
1750 (see Vfont_encoding_alist). If the repertory is not known, the
1751 fontset specifies the font for all Latin characters assuming that a
1752 user intends to use FONT-OBJECT for Latin characters. */
1755 fontset_from_font (Lisp_Object font_object)
1757 Lisp_Object font_name = font_get_name (font_object);
1758 Lisp_Object font_spec = copy_font_spec (font_object);
1759 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1760 Lisp_Object fontset_spec, alias, name, fontset;
1761 Lisp_Object val;
1763 val = assoc_no_quit (font_spec, auto_fontset_alist);
1764 if (CONSP (val))
1765 return XINT (FONTSET_ID (XCDR (val)));
1766 if (num_auto_fontsets++ == 0)
1767 alias = intern ("fontset-startup");
1768 else
1770 char temp[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1772 sprintf (temp, "fontset-auto%"pD"d", num_auto_fontsets - 1);
1773 alias = intern (temp);
1775 fontset_spec = copy_font_spec (font_spec);
1776 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1777 name = Ffont_xlfd_name (fontset_spec, Qnil);
1778 eassert (!NILP (name));
1779 fontset = make_fontset (Qnil, name, Qnil);
1780 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1781 Vfontset_alias_alist);
1782 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1783 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1784 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1785 font_spec = Ffont_spec (0, NULL);
1786 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1788 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1790 if (CONSP (target))
1791 target = XCDR (target);
1792 if (! CHARSETP (target))
1793 target = Qlatin;
1794 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1795 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1798 set_fontset_ascii (fontset, font_name);
1800 return XINT (FONTSET_ID (fontset));
1804 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1805 FONTSET is changed, we delete an entry of FONTSET if any from
1806 auto_fontset_alist so that FONTSET is not re-used by
1807 fontset_from_font. */
1809 static void
1810 update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
1812 Lisp_Object prev, tail;
1814 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1815 prev = tail, tail = XCDR (tail))
1816 if (EQ (fontset, XCDR (XCAR (tail))))
1818 if (NILP (prev))
1819 auto_fontset_alist = XCDR (tail);
1820 else
1821 XSETCDR (prev, XCDR (tail));
1822 break;
1827 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1828 doc: /* Return information about a fontset FONTSET on frame FRAME.
1830 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1831 for the default fontset. FRAME nil means the selected frame.
1833 The value is a char-table whose elements have this form:
1835 ((FONT OPENED-FONT ...) ...)
1837 FONT is a name of font specified for a range of characters.
1839 OPENED-FONT is a name of a font actually opened.
1841 The char-table has one extra slot. If FONTSET is not the default
1842 fontset, the value the extra slot is a char-table containing the
1843 information about the derived fonts from the default fontset. The
1844 format is the same as above. */)
1845 (Lisp_Object fontset, Lisp_Object frame)
1847 Lisp_Object *realized[2], fontsets[2], tables[2];
1848 Lisp_Object val, elt;
1849 int c, i, j, k;
1851 check_window_system (NULL);
1852 fontset = check_fontset_name (fontset, &frame);
1854 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1855 in the table `realized'. */
1856 USE_SAFE_ALLOCA;
1857 SAFE_ALLOCA_LISP (realized[0], 2 * ASIZE (Vfontset_table));
1858 realized[1] = realized[0] + ASIZE (Vfontset_table);
1859 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1861 elt = FONTSET_FROM_ID (i);
1862 if (!NILP (elt)
1863 && EQ (FONTSET_BASE (elt), fontset)
1864 && EQ (FONTSET_FRAME (elt), frame))
1865 realized[0][j++] = elt;
1867 realized[0][j] = Qnil;
1869 for (i = j = 0; ! NILP (realized[0][i]); i++)
1871 elt = FONTSET_DEFAULT (realized[0][i]);
1872 if (! NILP (elt))
1873 realized[1][j++] = elt;
1875 realized[1][j] = Qnil;
1877 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1878 fontsets[0] = fontset;
1879 if (!EQ (fontset, Vdefault_fontset))
1881 tables[1] = Fmake_char_table (Qnil, Qnil);
1882 set_char_table_extras (tables[0], 0, tables[1]);
1883 fontsets[1] = Vdefault_fontset;
1886 /* Accumulate information of the fontset in TABLE. The format of
1887 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1888 for (k = 0; k <= 1; k++)
1890 for (c = 0; c <= MAX_CHAR; )
1892 int from = c, to = MAX_5_BYTE_CHAR;
1894 if (c <= MAX_5_BYTE_CHAR)
1896 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1898 else
1900 val = FONTSET_FALLBACK (fontsets[k]);
1901 to = MAX_CHAR;
1903 if (VECTORP (val))
1905 Lisp_Object alist;
1907 /* At first, set ALIST to ((FONT-SPEC) ...). */
1908 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
1909 if (! NILP (AREF (val, i)))
1910 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1911 alist);
1912 alist = Fnreverse (alist);
1914 /* Then store opened font names to cdr of each elements. */
1915 for (i = 0; ! NILP (realized[k][i]); i++)
1917 if (c <= MAX_5_BYTE_CHAR)
1918 val = FONTSET_REF (realized[k][i], c);
1919 else
1920 val = FONTSET_FALLBACK (realized[k][i]);
1921 if (! CONSP (val) || ! VECTORP (XCDR (val)))
1922 continue;
1923 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1924 val = XCDR (val);
1925 for (j = 0; j < ASIZE (val); j++)
1927 elt = AREF (val, j);
1928 if (!NILP (elt) && FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1930 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1931 Lisp_Object slot, name;
1933 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1934 name = AREF (font_object, FONT_NAME_INDEX);
1935 if (NILP (Fmember (name, XCDR (slot))))
1936 nconc2 (slot, list1 (name));
1941 /* Store ALIST in TBL for characters C..TO. */
1942 if (c <= MAX_5_BYTE_CHAR)
1943 char_table_set_range (tables[k], c, to, alist);
1944 else
1945 set_char_table_defalt (tables[k], alist);
1947 /* At last, change each elements to font names. */
1948 for (; CONSP (alist); alist = XCDR (alist))
1950 elt = XCAR (alist);
1951 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
1954 c = to + 1;
1956 if (EQ (fontset, Vdefault_fontset))
1957 break;
1960 SAFE_FREE ();
1961 return tables[0];
1965 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
1966 doc: /* Return a font name pattern for character CH in fontset NAME.
1967 If NAME is t, find a pattern in the default fontset.
1968 If NAME is nil, find a pattern in the fontset of the selected frame.
1970 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
1971 family name and REGISTRY is a font registry name. This is actually
1972 the first font name pattern for CH in the fontset or in the default
1973 fontset.
1975 If the 2nd optional arg ALL is non-nil, return a list of all font name
1976 patterns. */)
1977 (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
1979 int c;
1980 Lisp_Object fontset, elt, list, repertory, val;
1981 int i, j;
1982 Lisp_Object frame;
1984 frame = Qnil;
1985 fontset = check_fontset_name (name, &frame);
1987 CHECK_CHARACTER (ch);
1988 c = XINT (ch);
1989 list = Qnil;
1990 while (1)
1992 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
1993 i++, elt = FONTSET_FALLBACK (fontset))
1994 if (VECTORP (elt))
1995 for (j = 0; j < ASIZE (elt); j++)
1997 Lisp_Object family, registry;
1999 val = AREF (elt, j);
2000 if (NILP (val))
2001 return Qnil;
2002 repertory = AREF (val, 1);
2003 if (INTEGERP (repertory))
2005 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2007 if (! CHAR_CHARSET_P (c, charset))
2008 continue;
2010 else if (CHAR_TABLE_P (repertory))
2012 if (NILP (CHAR_TABLE_REF (repertory, c)))
2013 continue;
2015 val = AREF (val, 0);
2016 /* VAL is a FONT-SPEC */
2017 family = AREF (val, FONT_FAMILY_INDEX);
2018 if (! NILP (family))
2019 family = SYMBOL_NAME (family);
2020 registry = AREF (val, FONT_REGISTRY_INDEX);
2021 if (! NILP (registry))
2022 registry = SYMBOL_NAME (registry);
2023 val = Fcons (family, registry);
2024 if (NILP (all))
2025 return val;
2026 list = Fcons (val, list);
2028 if (EQ (fontset, Vdefault_fontset))
2029 break;
2030 fontset = Vdefault_fontset;
2032 return (Fnreverse (list));
2035 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2036 doc: /* Return a list of all defined fontset names. */)
2037 (void)
2039 Lisp_Object fontset, list;
2040 int i;
2042 list = Qnil;
2043 for (i = 0; i < ASIZE (Vfontset_table); i++)
2045 fontset = FONTSET_FROM_ID (i);
2046 if (!NILP (fontset)
2047 && BASE_FONTSET_P (fontset))
2048 list = Fcons (FONTSET_NAME (fontset), list);
2051 return list;
2055 #ifdef ENABLE_CHECKING
2057 Lisp_Object dump_fontset (Lisp_Object) EXTERNALLY_VISIBLE;
2059 Lisp_Object
2060 dump_fontset (Lisp_Object fontset)
2062 Lisp_Object vec;
2064 vec = Fmake_vector (make_number (3), Qnil);
2065 ASET (vec, 0, FONTSET_ID (fontset));
2067 if (BASE_FONTSET_P (fontset))
2069 ASET (vec, 1, FONTSET_NAME (fontset));
2071 else
2073 Lisp_Object frame;
2075 frame = FONTSET_FRAME (fontset);
2076 if (FRAMEP (frame))
2078 struct frame *f = XFRAME (frame);
2080 if (FRAME_LIVE_P (f))
2081 ASET (vec, 1,
2082 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)),
2083 f->name));
2084 else
2085 ASET (vec, 1,
2086 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2088 if (!NILP (FONTSET_DEFAULT (fontset)))
2089 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2091 return vec;
2094 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2095 doc: /* Return a brief summary of all fontsets for debug use. */)
2096 (void)
2098 Lisp_Object val;
2099 int i;
2101 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2102 if (! NILP (AREF (Vfontset_table, i)))
2103 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2104 return (Fnreverse (val));
2106 #endif /* ENABLE_CHECKING */
2108 void
2109 syms_of_fontset (void)
2111 DEFSYM (Qfontset, "fontset");
2112 Fput (Qfontset, Qchar_table_extra_slots, make_number (8));
2113 DEFSYM (Qfontset_info, "fontset-info");
2114 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2116 DEFSYM (Qappend, "append");
2117 DEFSYM (Qlatin, "latin");
2119 Vcached_fontset_data = Qnil;
2120 staticpro (&Vcached_fontset_data);
2122 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2123 staticpro (&Vfontset_table);
2125 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2126 staticpro (&Vdefault_fontset);
2127 set_fontset_id (Vdefault_fontset, make_number (0));
2128 set_fontset_name
2129 (Vdefault_fontset,
2130 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2131 ASET (Vfontset_table, 0, Vdefault_fontset);
2132 next_fontset_id = 1;
2134 auto_fontset_alist = Qnil;
2135 staticpro (&auto_fontset_alist);
2137 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist,
2138 doc: /*
2139 Alist of charsets vs the charsets to determine the preferred font encoding.
2140 Each element looks like (CHARSET . ENCODING-CHARSET),
2141 where ENCODING-CHARSET is a charset registered in the variable
2142 `font-encoding-alist' as ENCODING.
2144 When a text has a property `charset' and the value is CHARSET, a font
2145 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2146 Vfont_encoding_charset_alist = Qnil;
2148 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent,
2149 doc: /*
2150 Char table of characters whose ascent values should be ignored.
2151 If an entry for a character is non-nil, the ascent value of the glyph
2152 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2154 This affects how a composite character which contains
2155 such a character is displayed on screen. */);
2156 Vuse_default_ascent = Qnil;
2158 DEFVAR_BOOL ("use-default-font-for-symbols", use_default_font_for_symbols,
2159 doc: /*
2160 If non-nil, use the default face's font for symbols and punctuation.
2162 By default, Emacs will try to use the default face's font for
2163 displaying symbol and punctuation characters, disregarding the
2164 fontsets, if the default font can display the character.
2165 Set this to nil to make Emacs honor the fontsets instead. */);
2166 use_default_font_for_symbols = 1;
2168 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition,
2169 doc: /*
2170 Char table of characters which are not composed relatively.
2171 If an entry for a character is non-nil, a composition sequence
2172 which contains that character is displayed so that
2173 the glyph of that character is put without considering
2174 an ascent and descent value of a previous character. */);
2175 Vignore_relative_composition = Qnil;
2177 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist,
2178 doc: /* Alist of fontname vs list of the alternate fontnames.
2179 When a specified font name is not found, the corresponding
2180 alternate fontnames (if any) are tried instead. */);
2181 Valternate_fontname_alist = Qnil;
2183 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist,
2184 doc: /* Alist of fontset names vs the aliases. */);
2185 Vfontset_alias_alist
2186 = list1 (Fcons (FONTSET_NAME (Vdefault_fontset),
2187 build_pure_c_string ("fontset-default")));
2189 DEFVAR_LISP ("vertical-centering-font-regexp",
2190 Vvertical_centering_font_regexp,
2191 doc: /* Regexp matching font names that require vertical centering on display.
2192 When a character is displayed with such fonts, the character is displayed
2193 at the vertical center of lines. */);
2194 Vvertical_centering_font_regexp = Qnil;
2196 DEFVAR_LISP ("otf-script-alist", Votf_script_alist,
2197 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2198 Votf_script_alist = Qnil;
2200 defsubr (&Squery_fontset);
2201 defsubr (&Snew_fontset);
2202 defsubr (&Sset_fontset_font);
2203 defsubr (&Sfontset_info);
2204 defsubr (&Sfontset_font);
2205 defsubr (&Sfontset_list);
2206 #ifdef ENABLE_CHECKING
2207 defsubr (&Sfontset_list_all);
2208 #endif