* lisp/vc/vc-filewise.el: Comment fixes.
[emacs.git] / src / fontset.c
blobd08d68fa28d0a936d16a49be12ea9fe1ffec6feb
1 /* Fontset handler.
3 Copyright (C) 2001-2014 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
17 (at 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 <http://www.gnu.org/licenses/>. */
27 #include <config.h>
28 #include <stdio.h>
30 #include "lisp.h"
31 #include "blockinput.h"
32 #include "character.h"
33 #include "buffer.h"
34 #include "charset.h"
35 #include "ccl.h"
36 #include "keyboard.h"
37 #include "frame.h"
38 #include "dispextern.h"
39 #include "intervals.h"
40 #include "fontset.h"
41 #include "window.h"
42 #ifdef HAVE_WINDOW_SYSTEM
43 #include TERM_HEADER
44 #endif /* HAVE_WINDOW_SYSTEM */
45 #include "termhooks.h"
46 #include "font.h"
48 /* FONTSET
50 A fontset is a collection of font related information to give
51 similar appearance (style, etc) of characters. A fontset has two
52 roles. One is to use for the frame parameter `font' as if it is an
53 ASCII font. In that case, Emacs uses the font specified for
54 `ascii' script for the frame's default font.
56 Another role, the more important one, is to provide information
57 about which font to use for each non-ASCII character.
59 There are two kinds of fontsets; base and realized. A base fontset
60 is created by `new-fontset' from Emacs Lisp explicitly. A realized
61 fontset is created implicitly when a face is realized for ASCII
62 characters. A face is also realized for non-ASCII characters based
63 on an ASCII face. All of non-ASCII faces based on the same ASCII
64 face share the same realized fontset.
66 A fontset object is implemented by a char-table whose default value
67 and parent are always nil.
69 An element of a base fontset is a vector of FONT-DEFs which itself
70 is a vector [ FONT-SPEC ENCODING REPERTORY ].
72 An element of a realized fontset is nil, t, 0, or a vector of this
73 form:
75 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
76 RFONT-DEF0 RFONT-DEF1 ... ]
78 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 by the current charset
83 priority list.
85 The value nil means that we have not yet generated the above vector
86 from the base of the fontset.
88 The value t means that no font is available for the corresponding
89 range of characters.
91 The value 0 means that no font is available for the corresponding
92 range of characters in this fontset, but may be available in the
93 default fontset.
95 A fontset has 8 extra slots.
97 The 1st slot:
98 base: the ID number of the fontset
99 realized: Likewise
101 The 2nd slot:
102 base: the name of the fontset
103 realized: nil
105 The 3rd slot:
106 base: the font name for ASCII characters
107 realized: nil
109 The 4th slot:
110 base: nil
111 realized: the base fontset
113 The 5th slot:
114 base: nil
115 realized: the frame that the fontset belongs to
117 The 6th slot:
118 base: nil
119 realized: the ID number of a face to use for characters that
120 has no font in a realized fontset.
122 The 7th slot:
123 base: nil
124 realized: If the base is not the default fontset, a fontset
125 realized from the default fontset, else nil.
127 The 8th slot:
128 base: Same as element value (but for fallback fonts).
129 realized: Likewise.
131 All fontsets are recorded in the vector Vfontset_table.
134 DEFAULT FONTSET
136 There's a special base fontset named `default fontset' which
137 defines the default font specifications. When a base fontset
138 doesn't specify a font for a specific character, the corresponding
139 value in the default fontset is used.
141 The parent of a realized fontset created for such a face that has
142 no fontset is the default fontset.
145 These structures are hidden from the other codes than this file.
146 The other codes handle fontsets only by their ID numbers. They
147 usually use the variable name `fontset' for IDs. But, in this
148 file, we always use variable name `id' for IDs, and name `fontset'
149 for an actual fontset object, i.e., char-table.
153 /********** VARIABLES and FUNCTION PROTOTYPES **********/
155 static Lisp_Object Qfontset;
156 static Lisp_Object Qfontset_info;
157 static Lisp_Object Qprepend, Qappend;
158 Lisp_Object Qlatin;
160 /* Vector containing all fontsets. */
161 static Lisp_Object Vfontset_table;
163 /* Next possibly free fontset ID. Usually this keeps the minimum
164 fontset ID not yet used. */
165 static int next_fontset_id;
167 /* The default fontset. This gives default FAMILY and REGISTRY of
168 font for each character. */
169 static Lisp_Object Vdefault_fontset;
171 /* Prototype declarations for static functions. */
172 static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
174 /* Return true if ID is a valid fontset id.
175 Optimized away if ENABLE_CHECKING is not defined. */
177 static bool
178 fontset_id_valid_p (int id)
180 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
185 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
187 /* Return the fontset with ID. No check of ID's validness. */
188 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
190 /* Access special values of FONTSET. */
192 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
193 static void
194 set_fontset_id (Lisp_Object fontset, Lisp_Object id)
196 set_char_table_extras (fontset, 0, id);
199 /* Access special values of (base) FONTSET. */
201 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
202 static void
203 set_fontset_name (Lisp_Object fontset, Lisp_Object name)
205 set_char_table_extras (fontset, 1, name);
208 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
209 static void
210 set_fontset_ascii (Lisp_Object fontset, Lisp_Object ascii)
212 set_char_table_extras (fontset, 2, ascii);
215 /* Access special values of (realized) FONTSET. */
217 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
218 static void
219 set_fontset_base (Lisp_Object fontset, Lisp_Object base)
221 set_char_table_extras (fontset, 3, base);
224 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
225 static void
226 set_fontset_frame (Lisp_Object fontset, Lisp_Object frame)
228 set_char_table_extras (fontset, 4, frame);
231 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
232 static void
233 set_fontset_nofont_face (Lisp_Object fontset, Lisp_Object face)
235 set_char_table_extras (fontset, 5, face);
238 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
239 static void
240 set_fontset_default (Lisp_Object fontset, Lisp_Object def)
242 set_char_table_extras (fontset, 6, def);
245 /* For both base and realized fontset. */
247 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
248 static void
249 set_fontset_fallback (Lisp_Object fontset, Lisp_Object fallback)
251 set_char_table_extras (fontset, 7, fallback);
254 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
256 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
257 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
258 do { \
259 (font_def) = make_uninit_vector (3); \
260 ASET ((font_def), 0, font_spec); \
261 ASET ((font_def), 1, encoding); \
262 ASET ((font_def), 2, repertory); \
263 } while (0)
265 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
266 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
267 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
269 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
270 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
271 ASET ((rfont_def), 0, make_number (face_id))
272 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
273 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
274 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
275 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
276 ASET ((rfont_def), 2, (object))
277 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
278 the order of listing by font backends, the higher bits represents
279 the order given by charset priority list. The smaller value is
280 preferable. */
281 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
282 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
283 ASET ((rfont_def), 3, make_number (score))
284 #define RFONT_DEF_NEW(rfont_def, font_def) \
285 do { \
286 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
287 ASET ((rfont_def), 1, (font_def)); \
288 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
289 } while (0)
292 /* Return the element of FONTSET for the character C. If FONTSET is a
293 base fontset other then the default fontset and FONTSET doesn't
294 contain information for C, return the information in the default
295 fontset. */
297 #define FONTSET_REF(fontset, c) \
298 (EQ (fontset, Vdefault_fontset) \
299 ? CHAR_TABLE_REF (fontset, c) \
300 : fontset_ref ((fontset), (c)))
302 static Lisp_Object
303 fontset_ref (Lisp_Object fontset, int c)
305 Lisp_Object elt;
307 elt = CHAR_TABLE_REF (fontset, c);
308 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
309 /* Don't check Vdefault_fontset for a realized fontset. */
310 && NILP (FONTSET_BASE (fontset)))
311 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
312 return elt;
315 /* Set elements of FONTSET for characters in RANGE to the value ELT.
316 RANGE is a cons (FROM . TO), where FROM and TO are character codes
317 specifying a range. */
319 #define FONTSET_SET(fontset, range, elt) \
320 Fset_char_table_range ((fontset), (range), (elt))
323 /* Modify the elements of FONTSET for characters in RANGE by replacing
324 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
325 and TO are character codes specifying a range. If ADD is nil,
326 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
327 append ELT. */
329 #define FONTSET_ADD(fontset, range, elt, add) \
330 (NILP (add) \
331 ? (NILP (range) \
332 ? (set_fontset_fallback \
333 (fontset, Fmake_vector (make_number (1), (elt)))) \
334 : ((void) \
335 Fset_char_table_range (fontset, range, \
336 Fmake_vector (make_number (1), elt)))) \
337 : fontset_add ((fontset), (range), (elt), (add)))
339 static void
340 fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
342 Lisp_Object args[2];
343 int idx = (EQ (add, Qappend) ? 0 : 1);
345 args[1 - idx] = Fmake_vector (make_number (1), elt);
347 if (CONSP (range))
349 int from = XINT (XCAR (range));
350 int to = XINT (XCDR (range));
351 int from1, to1;
353 do {
354 from1 = from, to1 = to;
355 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
356 char_table_set_range (fontset, from, to1,
357 NILP (args[idx]) ? args[1 - idx]
358 : Fvconcat (2, args));
359 from = to1 + 1;
360 } while (from < to);
362 else
364 args[idx] = FONTSET_FALLBACK (fontset);
365 set_fontset_fallback
366 (fontset, NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args));
370 static int
371 fontset_compare_rfontdef (const void *val1, const void *val2)
373 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
374 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
377 /* Update FONT-GROUP which has this form:
378 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
379 RFONT-DEF0 RFONT-DEF1 ... ]
380 Reorder RFONT-DEFs according to the current language, and update
381 CHARSET-ORDERED-LIST-TICK.
383 If PREFERRED_FAMILY is not nil, that family has the higher priority
384 if the encoding charsets or languages in font-specs are the same. */
386 static void
387 reorder_font_vector (Lisp_Object font_group, struct font *font)
389 Lisp_Object vec, font_object;
390 int size;
391 int i;
392 bool score_changed = 0;
394 if (font)
395 XSETFONT (font_object, font);
396 else
397 font_object = Qnil;
399 vec = XCDR (font_group);
400 size = ASIZE (vec);
401 /* Exclude the tailing nil element from the reordering. */
402 if (NILP (AREF (vec, size - 1)))
403 size--;
405 for (i = 0; i < size; i++)
407 Lisp_Object rfont_def = AREF (vec, i);
408 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
409 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
410 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
411 Lisp_Object otf_spec = Ffont_get (font_spec, QCotf);
413 if (! NILP (otf_spec))
414 /* A font-spec with :otf is preferable regardless of encoding
415 and language.. */
417 else if (! font_match_p (font_spec, font_object))
419 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
421 if (! NILP (encoding))
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 Lisp_Object lang = Ffont_get (font_spec, QClang);
437 if (! NILP (lang)
438 && ! EQ (lang, Vcurrent_iso639_language)
439 && (! CONSP (Vcurrent_iso639_language)
440 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
441 score |= 0x100;
444 if (RFONT_DEF_SCORE (rfont_def) != score)
446 RFONT_DEF_SET_SCORE (rfont_def, score);
447 score_changed = 1;
451 if (score_changed)
452 qsort (XVECTOR (vec)->contents, size, word_size,
453 fontset_compare_rfontdef);
454 XSETCAR (font_group, make_number (charset_ordered_list_tick));
457 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
458 character C in FONTSET. If C is -1, return a fallback font-group.
459 If C is not -1, the value may be Qt (FONTSET doesn't have a font
460 for C even in the fallback group), or 0 (a font for C may be found
461 only in the fallback group). */
463 static Lisp_Object
464 fontset_get_font_group (Lisp_Object fontset, int c)
466 Lisp_Object font_group;
467 Lisp_Object base_fontset;
468 int from = 0, to = MAX_CHAR, i;
470 eassert (! BASE_FONTSET_P (fontset));
471 if (c >= 0)
472 font_group = CHAR_TABLE_REF (fontset, c);
473 else
474 font_group = FONTSET_FALLBACK (fontset);
475 if (! NILP (font_group))
476 return font_group;
477 base_fontset = FONTSET_BASE (fontset);
478 if (NILP (base_fontset))
479 font_group = Qnil;
480 else if (c >= 0)
481 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
482 else
483 font_group = FONTSET_FALLBACK (base_fontset);
484 if (NILP (font_group))
486 font_group = make_number (0);
487 if (c >= 0)
488 char_table_set_range (fontset, from, to, font_group);
489 return font_group;
491 if (!VECTORP (font_group))
492 return font_group;
493 font_group = Fcopy_sequence (font_group);
494 for (i = 0; i < ASIZE (font_group); i++)
495 if (! NILP (AREF (font_group, i)))
497 Lisp_Object rfont_def;
499 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
500 /* Remember the original order. */
501 RFONT_DEF_SET_SCORE (rfont_def, i);
502 ASET (font_group, i, rfont_def);
504 font_group = Fcons (make_number (-1), font_group);
505 if (c >= 0)
506 char_table_set_range (fontset, from, to, font_group);
507 else
508 set_fontset_fallback (fontset, font_group);
509 return font_group;
512 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
513 character C. If no font is found, return Qnil if there's a
514 possibility that the default fontset or the fallback font groups
515 have a proper font, and return Qt if not.
517 If a font is found but is not yet opened, open it (if FACE is not
518 NULL) or return Qnil (if FACE is NULL).
520 ID is a charset-id that must be preferred, or -1 meaning no
521 preference.
523 If FALLBACK, search only fallback fonts. */
525 static Lisp_Object
526 fontset_find_font (Lisp_Object fontset, int c, struct face *face, int id,
527 bool fallback)
529 Lisp_Object vec, font_group;
530 int i, charset_matched = 0, found_index;
531 struct frame *f = (FRAMEP (FONTSET_FRAME (fontset))
532 ? XFRAME (FONTSET_FRAME (fontset))
533 : XFRAME (selected_frame));
534 Lisp_Object rfont_def;
536 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
537 if (! CONSP (font_group))
538 return font_group;
539 vec = XCDR (font_group);
540 if (ASIZE (vec) == 0)
541 return Qnil;
543 if (ASIZE (vec) > 1)
545 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
546 /* We have just created the font-group,
547 or the charset priorities were changed. */
548 reorder_font_vector (font_group, face->ascii_face->font);
549 if (id >= 0)
550 /* Find a spec matching with the charset ID to try at
551 first. */
552 for (i = 0; i < ASIZE (vec); i++)
554 Lisp_Object repertory;
556 rfont_def = AREF (vec, i);
557 if (NILP (rfont_def))
558 break;
559 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
561 if (XINT (repertory) == id)
563 charset_matched = i;
564 break;
569 /* Find the first available font in the vector of RFONT-DEF. */
570 for (i = 0; i < ASIZE (vec); i++)
572 Lisp_Object font_def;
573 Lisp_Object font_entity, font_object;
575 found_index = i;
576 if (i == 0)
578 if (charset_matched > 0)
580 /* Try the element matching with the charset ID at first. */
581 found_index = charset_matched;
582 /* Make this negative so that we don't come here in the
583 next loop. */
584 charset_matched = - charset_matched;
585 /* We must try the first element in the next loop. */
586 i--;
589 else if (i == - charset_matched)
591 /* We have already tried this element and the followings
592 that have the same font specifications in the first
593 iteration. So, skip them all. */
594 rfont_def = AREF (vec, i);
595 font_def = RFONT_DEF_FONT_DEF (rfont_def);
596 for (; i + 1 < ASIZE (vec); i++)
598 rfont_def = AREF (vec, i + 1);
599 if (NILP (rfont_def))
600 break;
601 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
602 break;
604 continue;
607 rfont_def = AREF (vec, found_index);
608 if (NILP (rfont_def))
610 if (i < 0)
611 continue;
612 /* This is a sign of not to try the other fonts. */
613 return Qt;
615 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
616 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
617 /* We couldn't open this font last time. */
618 continue;
620 font_object = RFONT_DEF_OBJECT (rfont_def);
621 if (NILP (font_object))
623 font_def = RFONT_DEF_FONT_DEF (rfont_def);
625 if (! face)
626 /* We have not yet opened the font. */
627 return Qnil;
628 /* Find a font best-matching with the spec without checking
629 the support of the character C. That checking is costly,
630 and even without the checking, the found font supports C
631 in high possibility. */
632 font_entity = font_find_for_lface (f, face->lface,
633 FONT_DEF_SPEC (font_def), -1);
634 if (NILP (font_entity))
636 /* Record that no font matches the spec. */
637 RFONT_DEF_SET_FACE (rfont_def, -1);
638 continue;
640 font_object = font_open_for_lface (f, font_entity, face->lface,
641 FONT_DEF_SPEC (font_def));
642 if (NILP (font_object))
644 /* Something strange happened, perhaps because of a
645 Font-backend problem. Too avoid crashing, record
646 that this spec is unusable. It may be better to find
647 another font of the same spec, but currently we don't
648 have such an API. */
649 RFONT_DEF_SET_FACE (rfont_def, -1);
650 continue;
652 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
655 if (font_has_char (f, font_object, c))
656 goto found;
658 /* Find a font already opened, matching with the current spec,
659 and supporting C. */
660 font_def = RFONT_DEF_FONT_DEF (rfont_def);
661 for (; found_index + 1 < ASIZE (vec); found_index++)
663 rfont_def = AREF (vec, found_index + 1);
664 if (NILP (rfont_def))
665 break;
666 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
667 break;
668 font_object = RFONT_DEF_OBJECT (rfont_def);
669 if (! NILP (font_object) && font_has_char (f, font_object, c))
671 found_index++;
672 goto found;
676 /* Find a font-entity with the current spec and supporting C. */
677 font_entity = font_find_for_lface (f, face->lface,
678 FONT_DEF_SPEC (font_def), c);
679 if (! NILP (font_entity))
681 /* We found a font. Open it and insert a new element for
682 that font in VEC. */
683 Lisp_Object new_vec;
684 int j;
686 font_object = font_open_for_lface (f, font_entity, face->lface,
687 Qnil);
688 if (NILP (font_object))
689 continue;
690 RFONT_DEF_NEW (rfont_def, font_def);
691 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
692 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
693 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
694 found_index++;
695 for (j = 0; j < found_index; j++)
696 ASET (new_vec, j, AREF (vec, j));
697 ASET (new_vec, j, rfont_def);
698 for (j++; j < ASIZE (new_vec); j++)
699 ASET (new_vec, j, AREF (vec, j - 1));
700 XSETCDR (font_group, new_vec);
701 vec = new_vec;
702 goto found;
704 if (i >= 0)
705 i = found_index;
708 FONTSET_SET (fontset, make_number (c), make_number (0));
709 return Qnil;
711 found:
712 if (fallback && found_index > 0)
714 /* The order of fonts in the fallback font-group is not that
715 important, and it is better to move the found font to the
716 first of the group so that the next try will find it
717 quickly. */
718 for (i = found_index; i > 0; i--)
719 ASET (vec, i, AREF (vec, i - 1));
720 ASET (vec, 0, rfont_def);
722 return rfont_def;
726 static Lisp_Object
727 fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
729 Lisp_Object rfont_def, default_rfont_def IF_LINT (= Qnil);
730 Lisp_Object base_fontset;
732 /* Try a font-group of FONTSET. */
733 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
734 rfont_def = fontset_find_font (fontset, c, face, id, 0);
735 if (VECTORP (rfont_def))
736 return rfont_def;
737 if (NILP (rfont_def))
738 FONTSET_SET (fontset, make_number (c), make_number (0));
740 /* Try a font-group of the default fontset. */
741 base_fontset = FONTSET_BASE (fontset);
742 if (! EQ (base_fontset, Vdefault_fontset))
744 if (NILP (FONTSET_DEFAULT (fontset)))
745 set_fontset_default
746 (fontset,
747 make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset));
748 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
749 default_rfont_def
750 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
751 if (VECTORP (default_rfont_def))
752 return default_rfont_def;
753 if (NILP (default_rfont_def))
754 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
755 make_number (0));
758 /* Try a fallback font-group of FONTSET. */
759 if (! EQ (rfont_def, Qt))
761 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
762 rfont_def = fontset_find_font (fontset, c, face, id, 1);
763 if (VECTORP (rfont_def))
764 return rfont_def;
765 /* Remember that FONTSET has no font for C. */
766 FONTSET_SET (fontset, make_number (c), Qt);
769 /* Try a fallback font-group of the default fontset. */
770 if (! EQ (base_fontset, Vdefault_fontset)
771 && ! EQ (default_rfont_def, Qt))
773 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
774 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
775 if (VECTORP (rfont_def))
776 return rfont_def;
777 /* Remember that the default fontset has no font for C. */
778 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
781 return Qnil;
784 /* Return a newly created fontset with NAME. If BASE is nil, make a
785 base fontset. Otherwise make a realized fontset whose base is
786 BASE. */
788 static Lisp_Object
789 make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
791 Lisp_Object fontset;
792 int size = ASIZE (Vfontset_table);
793 int id = next_fontset_id;
795 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
796 the next available fontset ID. So it is expected that this loop
797 terminates quickly. In addition, as the last element of
798 Vfontset_table is always nil, we don't have to check the range of
799 id. */
800 while (!NILP (AREF (Vfontset_table, id))) id++;
802 if (id + 1 == size)
803 Vfontset_table = larger_vector (Vfontset_table, 1, -1);
805 fontset = Fmake_char_table (Qfontset, Qnil);
807 set_fontset_id (fontset, make_number (id));
808 if (NILP (base))
809 set_fontset_name (fontset, name);
810 else
812 set_fontset_name (fontset, Qnil);
813 set_fontset_frame (fontset, frame);
814 set_fontset_base (fontset, base);
817 ASET (Vfontset_table, id, fontset);
818 next_fontset_id = id + 1;
819 return fontset;
823 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
825 /* Return the name of the fontset who has ID. */
827 Lisp_Object
828 fontset_name (int id)
830 Lisp_Object fontset;
832 fontset = FONTSET_FROM_ID (id);
833 return FONTSET_NAME (fontset);
837 /* Return the ASCII font name of the fontset who has ID. */
839 Lisp_Object
840 fontset_ascii (int id)
842 Lisp_Object fontset, elt;
844 fontset= FONTSET_FROM_ID (id);
845 elt = FONTSET_ASCII (fontset);
846 if (CONSP (elt))
847 elt = XCAR (elt);
848 return elt;
851 /* Free fontset of FACE defined on frame F. Called from
852 free_realized_face. */
854 void
855 free_face_fontset (struct frame *f, struct face *face)
857 Lisp_Object fontset;
859 fontset = FONTSET_FROM_ID (face->fontset);
860 if (NILP (fontset))
861 return;
862 eassert (! BASE_FONTSET_P (fontset));
863 eassert (f == XFRAME (FONTSET_FRAME (fontset)));
864 ASET (Vfontset_table, face->fontset, Qnil);
865 if (face->fontset < next_fontset_id)
866 next_fontset_id = face->fontset;
867 if (! NILP (FONTSET_DEFAULT (fontset)))
869 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
871 fontset = AREF (Vfontset_table, id);
872 eassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
873 eassert (f == XFRAME (FONTSET_FRAME (fontset)));
874 ASET (Vfontset_table, id, Qnil);
875 if (id < next_fontset_id)
876 next_fontset_id = face->fontset;
878 face->fontset = -1;
881 /* Return ID of face suitable for displaying character C at buffer position
882 POS on frame F. FACE must be realized for ASCII characters in advance.
883 Called from the macro FACE_FOR_CHAR. */
886 face_for_char (struct frame *f, struct face *face, int c,
887 ptrdiff_t pos, Lisp_Object object)
889 Lisp_Object fontset, rfont_def, charset;
890 int face_id;
891 int id;
893 eassert (fontset_id_valid_p (face->fontset));
895 if (ASCII_CHAR_P (c) || CHAR_BYTE8_P (c))
896 return face->ascii_face->id;
898 #ifdef HAVE_NS
899 if (face->font)
901 /* Fonts often have characters in other scripts, like symbol, even if they
902 don't match script: symbol. So check if the character is present
903 in the current face first. Only enable for NS for now, but should
904 perhaps be general? */
905 Lisp_Object font_object;
906 XSETFONT (font_object, face->font);
907 if (font_has_char (f, font_object, c)) return face->id;
909 #endif
911 fontset = FONTSET_FROM_ID (face->fontset);
912 eassert (!BASE_FONTSET_P (fontset));
914 if (pos < 0)
916 id = -1;
917 charset = Qnil;
919 else
921 charset = Fget_char_property (make_number (pos), Qcharset, object);
922 if (CHARSETP (charset))
924 Lisp_Object val;
926 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
927 if (CONSP (val) && CHARSETP (XCDR (val)))
928 charset = XCDR (val);
929 id = XINT (CHARSET_SYMBOL_ID (charset));
931 else
932 id = -1;
935 rfont_def = fontset_font (fontset, c, face, id);
936 if (VECTORP (rfont_def))
938 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
939 face_id = XINT (RFONT_DEF_FACE (rfont_def));
940 else
942 Lisp_Object font_object;
944 font_object = RFONT_DEF_OBJECT (rfont_def);
945 face_id = face_for_font (f, font_object, face);
946 RFONT_DEF_SET_FACE (rfont_def, face_id);
949 else
951 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
952 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
953 else
955 face_id = face_for_font (f, Qnil, face);
956 set_fontset_nofont_face (fontset, make_number (face_id));
959 eassert (face_id >= 0);
960 return face_id;
964 Lisp_Object
965 font_for_char (struct face *face, int c, ptrdiff_t pos, Lisp_Object object)
967 Lisp_Object fontset, rfont_def, charset;
968 int id;
970 if (ASCII_CHAR_P (c))
972 Lisp_Object font_object;
974 XSETFONT (font_object, face->ascii_face->font);
975 return font_object;
978 eassert (fontset_id_valid_p (face->fontset));
979 fontset = FONTSET_FROM_ID (face->fontset);
980 eassert (!BASE_FONTSET_P (fontset));
981 if (pos < 0)
983 id = -1;
984 charset = Qnil;
986 else
988 charset = Fget_char_property (make_number (pos), Qcharset, object);
989 if (CHARSETP (charset))
991 Lisp_Object val;
993 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
994 if (CONSP (val) && CHARSETP (XCDR (val)))
995 charset = XCDR (val);
996 id = XINT (CHARSET_SYMBOL_ID (charset));
998 else
999 id = -1;
1002 rfont_def = fontset_font (fontset, c, face, id);
1003 return (VECTORP (rfont_def)
1004 ? RFONT_DEF_OBJECT (rfont_def)
1005 : Qnil);
1009 /* Make a realized fontset for ASCII face FACE on frame F from the
1010 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1011 default fontset as the base. Value is the id of the new fontset.
1012 Called from realize_x_face. */
1015 make_fontset_for_ascii_face (struct frame *f, int base_fontset_id, struct face *face)
1017 Lisp_Object base_fontset, fontset, frame;
1019 XSETFRAME (frame, f);
1020 if (base_fontset_id >= 0)
1022 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1023 if (!BASE_FONTSET_P (base_fontset))
1024 base_fontset = FONTSET_BASE (base_fontset);
1025 eassert (BASE_FONTSET_P (base_fontset));
1027 else
1028 base_fontset = Vdefault_fontset;
1030 fontset = make_fontset (frame, Qnil, base_fontset);
1031 return XINT (FONTSET_ID (fontset));
1036 /* Cache data used by fontset_pattern_regexp. The car part is a
1037 pattern string containing at least one wild card, the cdr part is
1038 the corresponding regular expression. */
1039 static Lisp_Object Vcached_fontset_data;
1041 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1042 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1044 /* If fontset name PATTERN contains any wild card, return regular
1045 expression corresponding to PATTERN. */
1047 static Lisp_Object
1048 fontset_pattern_regexp (Lisp_Object pattern)
1050 if (!strchr (SSDATA (pattern), '*')
1051 && !strchr (SSDATA (pattern), '?'))
1052 /* PATTERN does not contain any wild cards. */
1053 return Qnil;
1055 if (!CONSP (Vcached_fontset_data)
1056 || strcmp (SSDATA (pattern), CACHED_FONTSET_NAME))
1058 /* We must at first update the cached data. */
1059 unsigned char *regex, *p0, *p1;
1060 int ndashes = 0, nstars = 0, nescs = 0;
1062 for (p0 = SDATA (pattern); *p0; p0++)
1064 if (*p0 == '-')
1065 ndashes++;
1066 else if (*p0 == '*')
1067 nstars++;
1068 else if (*p0 == '['
1069 || *p0 == '.' || *p0 == '\\'
1070 || *p0 == '+' || *p0 == '^'
1071 || *p0 == '$')
1072 nescs++;
1075 /* If PATTERN is not full XLFD we convert "*" to ".*". Otherwise
1076 we convert "*" to "[^-]*" which is much faster in regular
1077 expression matching. */
1078 ptrdiff_t regexsize = (SBYTES (pattern)
1079 + (ndashes < 14 ? 2 : 5) * nstars
1080 + 2 * nescs + 1);
1081 USE_SAFE_ALLOCA;
1082 p1 = regex = SAFE_ALLOCA (regexsize);
1084 *p1++ = '^';
1085 for (p0 = SDATA (pattern); *p0; p0++)
1087 if (*p0 == '*')
1089 if (ndashes < 14)
1090 *p1++ = '.';
1091 else
1092 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1093 *p1++ = '*';
1095 else if (*p0 == '?')
1096 *p1++ = '.';
1097 else if (*p0 == '['
1098 || *p0 == '.' || *p0 == '\\'
1099 || *p0 == '+' || *p0 == '^'
1100 || *p0 == '$')
1101 *p1++ = '\\', *p1++ = *p0;
1102 else
1103 *p1++ = *p0;
1105 *p1++ = '$';
1106 *p1++ = 0;
1108 Vcached_fontset_data = Fcons (build_string (SSDATA (pattern)),
1109 build_string ((char *) regex));
1110 SAFE_FREE ();
1113 return CACHED_FONTSET_REGEX;
1116 /* Return ID of the base fontset named NAME. If there's no such
1117 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1118 0: pattern containing '*' and '?' as wildcards
1119 1: regular expression
1120 2: literal fontset name
1124 fs_query_fontset (Lisp_Object name, int name_pattern)
1126 Lisp_Object tem;
1127 int i;
1129 name = Fdowncase (name);
1130 if (name_pattern != 1)
1132 tem = Frassoc (name, Vfontset_alias_alist);
1133 if (NILP (tem))
1134 tem = Fassoc (name, Vfontset_alias_alist);
1135 if (CONSP (tem) && STRINGP (XCAR (tem)))
1136 name = XCAR (tem);
1137 else if (name_pattern == 0)
1139 tem = fontset_pattern_regexp (name);
1140 if (STRINGP (tem))
1142 name = tem;
1143 name_pattern = 1;
1148 for (i = 0; i < ASIZE (Vfontset_table); i++)
1150 Lisp_Object fontset, this_name;
1152 fontset = FONTSET_FROM_ID (i);
1153 if (NILP (fontset)
1154 || !BASE_FONTSET_P (fontset))
1155 continue;
1157 this_name = FONTSET_NAME (fontset);
1158 if (name_pattern == 1
1159 ? fast_string_match_ignore_case (name, this_name) >= 0
1160 : !xstrcasecmp (SSDATA (name), SSDATA (this_name)))
1161 return i;
1163 return -1;
1167 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1168 doc: /* Return the name of a fontset that matches PATTERN.
1169 The value is nil if there is no matching fontset.
1170 PATTERN can contain `*' or `?' as a wildcard
1171 just as X font name matching algorithm allows.
1172 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1173 (Lisp_Object pattern, Lisp_Object regexpp)
1175 Lisp_Object fontset;
1176 int id;
1178 check_window_system (NULL);
1180 CHECK_STRING (pattern);
1182 if (SCHARS (pattern) == 0)
1183 return Qnil;
1185 id = fs_query_fontset (pattern, !NILP (regexpp));
1186 if (id < 0)
1187 return Qnil;
1189 fontset = FONTSET_FROM_ID (id);
1190 return FONTSET_NAME (fontset);
1193 /* Return a list of base fontset names matching PATTERN on frame F. */
1195 Lisp_Object
1196 list_fontsets (struct frame *f, Lisp_Object pattern, int size)
1198 Lisp_Object frame, regexp, val;
1199 int id;
1201 XSETFRAME (frame, f);
1203 regexp = fontset_pattern_regexp (pattern);
1204 val = Qnil;
1206 for (id = 0; id < ASIZE (Vfontset_table); id++)
1208 Lisp_Object fontset, name;
1210 fontset = FONTSET_FROM_ID (id);
1211 if (NILP (fontset)
1212 || !BASE_FONTSET_P (fontset)
1213 || !EQ (frame, FONTSET_FRAME (fontset)))
1214 continue;
1215 name = FONTSET_NAME (fontset);
1217 if (STRINGP (regexp)
1218 ? (fast_string_match (regexp, name) < 0)
1219 : strcmp (SSDATA (pattern), SSDATA (name)))
1220 continue;
1222 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1225 return val;
1229 /* Free all realized fontsets whose base fontset is BASE. */
1231 static void
1232 free_realized_fontsets (Lisp_Object base)
1234 int id;
1236 #if 0
1237 /* For the moment, this doesn't work because free_realized_face
1238 doesn't remove FACE from a cache. Until we find a solution, we
1239 suppress this code, and simply use Fclear_face_cache even though
1240 that is not efficient. */
1241 block_input ();
1242 for (id = 0; id < ASIZE (Vfontset_table); id++)
1244 Lisp_Object this = AREF (Vfontset_table, id);
1246 if (EQ (FONTSET_BASE (this), base))
1248 Lisp_Object tail;
1250 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1251 tail = XCDR (tail))
1253 struct frame *f = XFRAME (FONTSET_FRAME (this));
1254 int face_id = XINT (XCDR (XCAR (tail)));
1255 struct face *face = FACE_FROM_ID (f, face_id);
1257 /* Face THIS itself is also freed by the following call. */
1258 free_realized_face (f, face);
1262 unblock_input ();
1263 #else /* not 0 */
1264 /* But, we don't have to call Fclear_face_cache if no fontset has
1265 been realized from BASE. */
1266 for (id = 0; id < ASIZE (Vfontset_table); id++)
1268 Lisp_Object this = AREF (Vfontset_table, id);
1270 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1272 Fclear_face_cache (Qt);
1273 break;
1276 #endif /* not 0 */
1280 /* Check validity of NAME as a fontset name and return the
1281 corresponding fontset. If not valid, signal an error.
1283 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1284 fontset of *FRAME.
1286 Set *FRAME to the actual frame. */
1288 static Lisp_Object
1289 check_fontset_name (Lisp_Object name, Lisp_Object *frame)
1291 int id;
1292 struct frame *f = decode_live_frame (*frame);
1294 XSETFRAME (*frame, f);
1296 if (EQ (name, Qt))
1297 return Vdefault_fontset;
1298 if (NILP (name))
1299 id = FRAME_FONTSET (f);
1300 else
1302 CHECK_STRING (name);
1303 /* First try NAME as literal. */
1304 id = fs_query_fontset (name, 2);
1305 if (id < 0)
1306 /* For backward compatibility, try again NAME as pattern. */
1307 id = fs_query_fontset (name, 0);
1308 if (id < 0)
1309 error ("Fontset `%s' does not exist", SDATA (name));
1311 return FONTSET_FROM_ID (id);
1314 static void
1315 accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
1317 if (EQ (XCAR (arg), val))
1319 if (CONSP (range))
1320 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1321 else
1322 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1327 /* Callback function for map_charset_chars in Fset_fontset_font.
1328 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1330 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1331 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1332 The consumed ranges are popped up from SCRIPT_RANGE_LIST, and the
1333 new SCRIPT_RANGE_LIST is stored in ARG.
1335 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1336 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1337 case. */
1339 static void
1340 set_fontset_font (Lisp_Object arg, Lisp_Object range)
1342 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1343 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1345 fontset = AREF (arg, 0);
1346 font_def = AREF (arg, 1);
1347 add = AREF (arg, 2);
1348 ascii = AREF (arg, 3);
1349 script_range_list = AREF (arg, 4);
1351 if (NILP (ascii) && from < 0x80)
1353 if (to < 0x80)
1354 return;
1355 from = 0x80;
1356 range = Fcons (make_number (0x80), XCDR (range));
1359 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1360 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1361 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1363 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1364 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1365 if (CONSP (script_range_list))
1367 if (SCRIPT_FROM < from)
1368 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1369 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1370 POP_SCRIPT_RANGE ();
1371 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1372 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1375 FONTSET_ADD (fontset, range, font_def, add);
1376 ASET (arg, 4, script_range_list);
1379 static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
1382 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1383 doc: /*
1384 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1386 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1387 the default fontset.
1389 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1390 In that case, use FONT-SPEC for all characters in the range FROM and
1391 TO (inclusive).
1393 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1394 all characters that belong to the script.
1396 TARGET may be a charset. In that case, use FONT-SPEC for all
1397 characters in the charset.
1399 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1400 that no FONT-SPEC is specified.
1402 FONT-SPEC may one of these:
1403 * A font-spec object made by the function `font-spec' (which see).
1404 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1405 REGISTRY is a font registry name. FAMILY may contain foundry
1406 name, and REGISTRY may contain encoding name.
1407 * A font name string.
1408 * nil, which explicitly specifies that there's no font for TARGET.
1410 Optional 4th argument FRAME is a frame or nil for the selected frame
1411 that is concerned in the case that NAME is nil.
1413 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1414 to the font specifications for TARGET previously set. If it is
1415 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1416 appended. By default, FONT-SPEC overrides the previous settings. */)
1417 (Lisp_Object name, Lisp_Object target, Lisp_Object font_spec, Lisp_Object frame, Lisp_Object add)
1419 Lisp_Object fontset;
1420 Lisp_Object font_def, registry, family;
1421 Lisp_Object range_list;
1422 struct charset *charset = NULL;
1423 Lisp_Object fontname;
1424 bool ascii_changed = 0;
1426 fontset = check_fontset_name (name, &frame);
1428 fontname = Qnil;
1429 if (CONSP (font_spec))
1431 Lisp_Object spec = Ffont_spec (0, NULL);
1433 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1434 font_spec = spec;
1435 fontname = Ffont_xlfd_name (font_spec, Qnil);
1437 else if (STRINGP (font_spec))
1439 Lisp_Object args[2];
1441 fontname = font_spec;
1442 args[0] = QCname;
1443 args[1] = font_spec;
1444 font_spec = Ffont_spec (2, args);
1446 else if (FONT_SPEC_P (font_spec))
1447 fontname = Ffont_xlfd_name (font_spec, Qnil);
1448 else if (! NILP (font_spec))
1449 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1451 if (! NILP (font_spec))
1453 Lisp_Object encoding, repertory;
1455 family = AREF (font_spec, FONT_FAMILY_INDEX);
1456 if (! NILP (family) )
1457 family = SYMBOL_NAME (family);
1458 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1459 if (! NILP (registry))
1460 registry = Fdowncase (SYMBOL_NAME (registry));
1461 AUTO_STRING (dash, "-");
1462 encoding = find_font_encoding (concat3 (family, dash, registry));
1463 if (NILP (encoding))
1464 encoding = Qascii;
1466 if (SYMBOLP (encoding))
1468 CHECK_CHARSET (encoding);
1469 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1471 else
1473 repertory = XCDR (encoding);
1474 encoding = XCAR (encoding);
1475 CHECK_CHARSET (encoding);
1476 encoding = CHARSET_SYMBOL_ID (encoding);
1477 if (! NILP (repertory) && SYMBOLP (repertory))
1479 CHECK_CHARSET (repertory);
1480 repertory = CHARSET_SYMBOL_ID (repertory);
1483 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1485 else
1486 font_def = Qnil;
1488 if (CHARACTERP (target))
1490 if (XFASTINT (target) < 0x80)
1491 error ("Can't set a font for partial ASCII range");
1492 range_list = list1 (Fcons (target, target));
1494 else if (CONSP (target))
1496 Lisp_Object from, to;
1498 from = Fcar (target);
1499 to = Fcdr (target);
1500 CHECK_CHARACTER (from);
1501 CHECK_CHARACTER (to);
1502 if (XFASTINT (from) < 0x80)
1504 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1505 error ("Can't set a font for partial ASCII range");
1506 ascii_changed = 1;
1508 range_list = list1 (target);
1510 else if (SYMBOLP (target) && !NILP (target))
1512 Lisp_Object script_list;
1513 Lisp_Object val;
1515 range_list = Qnil;
1516 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1517 if (! NILP (Fmemq (target, script_list)))
1519 if (EQ (target, Qlatin))
1520 ascii_changed = 1;
1521 val = list1 (target);
1522 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1523 val);
1524 range_list = Fnreverse (XCDR (val));
1526 if (CHARSETP (target))
1528 CHECK_CHARSET_GET_CHARSET (target, charset);
1529 if (charset->ascii_compatible_p)
1530 ascii_changed = 1;
1532 else if (NILP (range_list))
1533 error ("Invalid script or charset name: %s",
1534 SDATA (SYMBOL_NAME (target)));
1536 else if (NILP (target))
1537 range_list = list1 (Qnil);
1538 else
1539 error ("Invalid target for setting a font");
1541 if (ascii_changed)
1543 Lisp_Object val;
1545 if (NILP (font_spec))
1546 error ("Can't set ASCII font to nil");
1547 val = CHAR_TABLE_REF (fontset, 0);
1548 if (! NILP (val) && EQ (add, Qappend))
1549 /* We are going to change just an additional font for ASCII. */
1550 ascii_changed = 0;
1553 if (charset)
1555 Lisp_Object arg;
1557 arg = make_uninit_vector (5);
1558 ASET (arg, 0, fontset);
1559 ASET (arg, 1, font_def);
1560 ASET (arg, 2, add);
1561 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1562 ASET (arg, 4, range_list);
1564 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1565 CHARSET_MIN_CODE (charset),
1566 CHARSET_MAX_CODE (charset));
1567 range_list = AREF (arg, 4);
1569 for (; CONSP (range_list); range_list = XCDR (range_list))
1570 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1572 if (ascii_changed)
1574 Lisp_Object tail, fr;
1575 int fontset_id = XINT (FONTSET_ID (fontset));
1577 set_fontset_ascii (fontset, fontname);
1578 name = FONTSET_NAME (fontset);
1579 FOR_EACH_FRAME (tail, fr)
1581 struct frame *f = XFRAME (fr);
1582 Lisp_Object font_object;
1583 struct face *face;
1585 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1586 continue;
1587 if (fontset_id != FRAME_FONTSET (f))
1588 continue;
1589 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1590 if (face)
1591 font_object = font_load_for_lface (f, face->lface, font_spec);
1592 else
1593 font_object = font_open_by_spec (f, font_spec);
1594 if (! NILP (font_object))
1596 update_auto_fontset_alist (font_object, fontset);
1597 AUTO_FRAME_ARG (arg, Qfont, Fcons (name, font_object));
1598 Fmodify_frame_parameters (fr, arg);
1603 /* Free all realized fontsets whose base is FONTSET. This way, the
1604 specified character(s) are surely redisplayed by a correct
1605 font. */
1606 free_realized_fontsets (fontset);
1608 return Qnil;
1612 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1613 doc: /* Create a new fontset NAME from font information in FONTLIST.
1615 FONTLIST is an alist of scripts vs the corresponding font specification list.
1616 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1617 character of SCRIPT is displayed by a font that matches one of
1618 FONT-SPEC.
1620 SCRIPT is a symbol that appears in the first extra slot of the
1621 char-table `char-script-table'.
1623 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1624 `set-fontset-font' for the meaning. */)
1625 (Lisp_Object name, Lisp_Object fontlist)
1627 Lisp_Object fontset;
1628 int id;
1630 CHECK_STRING (name);
1631 CHECK_LIST (fontlist);
1633 name = Fdowncase (name);
1634 id = fs_query_fontset (name, 0);
1635 if (id < 0)
1637 Lisp_Object font_spec = Ffont_spec (0, NULL);
1638 Lisp_Object short_name;
1639 char xlfd[256];
1640 int len;
1642 if (font_parse_xlfd (SSDATA (name), SBYTES (name), font_spec) < 0)
1643 error ("Fontset name must be in XLFD format");
1644 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1645 if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1646 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1647 error ("Registry field of fontset name must be \"fontset-*\"");
1648 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1649 Vfontset_alias_alist);
1650 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1651 fontset = make_fontset (Qnil, name, Qnil);
1652 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1653 if (len < 0)
1654 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1655 set_fontset_ascii (fontset, make_unibyte_string (xlfd, len));
1657 else
1659 fontset = FONTSET_FROM_ID (id);
1660 free_realized_fontsets (fontset);
1661 Fset_char_table_range (fontset, Qt, Qnil);
1664 for (; CONSP (fontlist); fontlist = XCDR (fontlist))
1666 Lisp_Object elt, script;
1668 elt = XCAR (fontlist);
1669 script = Fcar (elt);
1670 elt = Fcdr (elt);
1671 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1672 for (; CONSP (elt); elt = XCDR (elt))
1673 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1674 else
1675 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1677 return name;
1681 /* Alist of automatically created fontsets. Each element is a cons
1682 (FONT-SPEC . FONTSET-ID). */
1683 static Lisp_Object auto_fontset_alist;
1685 /* Number of automatically created fontsets. */
1686 static ptrdiff_t num_auto_fontsets;
1688 /* Return a fontset synthesized from FONT-OBJECT. This is called from
1689 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1690 frame, and the returned fontset is used for the default fontset of
1691 that frame. The fontset specifies a font of the same registry as
1692 FONT-OBJECT for all characters in the repertory of the registry
1693 (see Vfont_encoding_alist). If the repertory is not known, the
1694 fontset specifies the font for all Latin characters assuming that a
1695 user intends to use FONT-OBJECT for Latin characters. */
1698 fontset_from_font (Lisp_Object font_object)
1700 Lisp_Object font_name = font_get_name (font_object);
1701 Lisp_Object font_spec = copy_font_spec (font_object);
1702 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1703 Lisp_Object fontset_spec, alias, name, fontset;
1704 Lisp_Object val;
1706 val = assoc_no_quit (font_spec, auto_fontset_alist);
1707 if (CONSP (val))
1708 return XINT (FONTSET_ID (XCDR (val)));
1709 if (num_auto_fontsets++ == 0)
1710 alias = intern ("fontset-startup");
1711 else
1713 char temp[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1715 sprintf (temp, "fontset-auto%"pD"d", num_auto_fontsets - 1);
1716 alias = intern (temp);
1718 fontset_spec = copy_font_spec (font_spec);
1719 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1720 name = Ffont_xlfd_name (fontset_spec, Qnil);
1721 eassert (!NILP (name));
1722 fontset = make_fontset (Qnil, name, Qnil);
1723 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1724 Vfontset_alias_alist);
1725 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1726 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1727 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1728 font_spec = Ffont_spec (0, NULL);
1729 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1731 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1733 if (CONSP (target))
1734 target = XCDR (target);
1735 if (! CHARSETP (target))
1736 target = Qlatin;
1737 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1738 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1741 set_fontset_ascii (fontset, font_name);
1743 return XINT (FONTSET_ID (fontset));
1747 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1748 FONTSET is changed, we delete an entry of FONTSET if any from
1749 auto_fontset_alist so that FONTSET is not re-used by
1750 fontset_from_font. */
1752 static void
1753 update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
1755 Lisp_Object prev, tail;
1757 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1758 prev = tail, tail = XCDR (tail))
1759 if (EQ (fontset, XCDR (XCAR (tail))))
1761 if (NILP (prev))
1762 auto_fontset_alist = XCDR (tail);
1763 else
1764 XSETCDR (prev, XCDR (tail));
1765 break;
1770 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1771 FONT-OBJECT is the font for the character at POSITION in the current
1772 buffer. This is computed from all the text properties and overlays
1773 that apply to POSITION. POSITION may be nil, in which case,
1774 FONT-SPEC is the font for displaying the character CH with the
1775 default face.
1777 GLYPH-CODE is the glyph code in the font to use for the character.
1779 If the 2nd optional arg CH is non-nil, it is a character to check
1780 the font instead of the character at POSITION.
1782 It returns nil in the following cases:
1784 (1) The window system doesn't have a font for the character (thus
1785 it is displayed by an empty box).
1787 (2) The character code is invalid.
1789 (3) If POSITION is not nil, and the current buffer is not displayed
1790 in any window.
1792 In addition, the returned font name may not take into account of
1793 such redisplay engine hooks as what used in jit-lock-mode if
1794 POSITION is currently not visible. */
1797 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1798 doc: /* For internal use only. */)
1799 (Lisp_Object position, Lisp_Object ch)
1801 ptrdiff_t pos, pos_byte, dummy;
1802 int face_id;
1803 int c;
1804 struct frame *f;
1805 struct face *face;
1807 if (NILP (position))
1809 CHECK_CHARACTER (ch);
1810 c = XINT (ch);
1811 f = XFRAME (selected_frame);
1812 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1813 pos = -1;
1815 else
1817 Lisp_Object window;
1818 struct window *w;
1820 CHECK_NUMBER_COERCE_MARKER (position);
1821 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
1822 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1823 pos = XINT (position);
1824 pos_byte = CHAR_TO_BYTE (pos);
1825 if (NILP (ch))
1826 c = FETCH_CHAR (pos_byte);
1827 else
1829 CHECK_NATNUM (ch);
1830 c = XINT (ch);
1832 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1833 if (NILP (window))
1834 return Qnil;
1835 w = XWINDOW (window);
1836 f = XFRAME (w->frame);
1837 face_id = face_at_buffer_position (w, pos, &dummy,
1838 pos + 100, 0, -1);
1840 if (! CHAR_VALID_P (c))
1841 return Qnil;
1842 if (!FRAME_WINDOW_P (f))
1843 return Qnil;
1844 /* We need the basic faces to be valid below, so recompute them if
1845 some code just happened to clear the face cache. */
1846 if (FRAME_FACE_CACHE (f)->used == 0)
1847 recompute_basic_faces (f);
1848 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1849 face = FACE_FROM_ID (f, face_id);
1850 if (face->font)
1852 unsigned code = face->font->driver->encode_char (face->font, c);
1853 Lisp_Object font_object;
1855 if (code == FONT_INVALID_CODE)
1856 return Qnil;
1857 XSETFONT (font_object, face->font);
1858 return Fcons (font_object, INTEGER_TO_CONS (code));
1860 return Qnil;
1864 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1865 doc: /* Return information about a fontset FONTSET on frame FRAME.
1867 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1868 for the default fontset. FRAME nil means the selected frame.
1870 The value is a char-table whose elements have this form:
1872 ((FONT OPENED-FONT ...) ...)
1874 FONT is a name of font specified for a range of characters.
1876 OPENED-FONT is a name of a font actually opened.
1878 The char-table has one extra slot. If FONTSET is not the default
1879 fontset, the value the extra slot is a char-table containing the
1880 information about the derived fonts from the default fontset. The
1881 format is the same as above. */)
1882 (Lisp_Object fontset, Lisp_Object frame)
1884 Lisp_Object *realized[2], fontsets[2], tables[2];
1885 Lisp_Object val, elt;
1886 int c, i, j, k;
1888 check_window_system (NULL);
1889 fontset = check_fontset_name (fontset, &frame);
1891 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1892 in the table `realized'. */
1893 USE_SAFE_ALLOCA;
1894 SAFE_ALLOCA_LISP (realized[0], 2 * ASIZE (Vfontset_table));
1895 realized[1] = realized[0] + ASIZE (Vfontset_table);
1896 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1898 elt = FONTSET_FROM_ID (i);
1899 if (!NILP (elt)
1900 && EQ (FONTSET_BASE (elt), fontset)
1901 && EQ (FONTSET_FRAME (elt), frame))
1902 realized[0][j++] = elt;
1904 realized[0][j] = Qnil;
1906 for (i = j = 0; ! NILP (realized[0][i]); i++)
1908 elt = FONTSET_DEFAULT (realized[0][i]);
1909 if (! NILP (elt))
1910 realized[1][j++] = elt;
1912 realized[1][j] = Qnil;
1914 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1915 fontsets[0] = fontset;
1916 if (!EQ (fontset, Vdefault_fontset))
1918 tables[1] = Fmake_char_table (Qnil, Qnil);
1919 set_char_table_extras (tables[0], 0, tables[1]);
1920 fontsets[1] = Vdefault_fontset;
1923 /* Accumulate information of the fontset in TABLE. The format of
1924 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1925 for (k = 0; k <= 1; k++)
1927 for (c = 0; c <= MAX_CHAR; )
1929 int from = c, to = MAX_5_BYTE_CHAR;
1931 if (c <= MAX_5_BYTE_CHAR)
1933 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1935 else
1937 val = FONTSET_FALLBACK (fontsets[k]);
1938 to = MAX_CHAR;
1940 if (VECTORP (val))
1942 Lisp_Object alist;
1944 /* At first, set ALIST to ((FONT-SPEC) ...). */
1945 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
1946 if (! NILP (AREF (val, i)))
1947 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1948 alist);
1949 alist = Fnreverse (alist);
1951 /* Then store opened font names to cdr of each elements. */
1952 for (i = 0; ! NILP (realized[k][i]); i++)
1954 if (c <= MAX_5_BYTE_CHAR)
1955 val = FONTSET_REF (realized[k][i], c);
1956 else
1957 val = FONTSET_FALLBACK (realized[k][i]);
1958 if (! CONSP (val) || ! VECTORP (XCDR (val)))
1959 continue;
1960 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1961 val = XCDR (val);
1962 for (j = 0; j < ASIZE (val); j++)
1964 elt = AREF (val, j);
1965 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1967 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1968 Lisp_Object slot, name;
1970 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1971 name = AREF (font_object, FONT_NAME_INDEX);
1972 if (NILP (Fmember (name, XCDR (slot))))
1973 nconc2 (slot, list1 (name));
1978 /* Store ALIST in TBL for characters C..TO. */
1979 if (c <= MAX_5_BYTE_CHAR)
1980 char_table_set_range (tables[k], c, to, alist);
1981 else
1982 set_char_table_defalt (tables[k], alist);
1984 /* At last, change each elements to font names. */
1985 for (; CONSP (alist); alist = XCDR (alist))
1987 elt = XCAR (alist);
1988 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
1991 c = to + 1;
1993 if (EQ (fontset, Vdefault_fontset))
1994 break;
1997 SAFE_FREE ();
1998 return tables[0];
2002 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2003 doc: /* Return a font name pattern for character CH in fontset NAME.
2004 If NAME is t, find a pattern in the default fontset.
2005 If NAME is nil, find a pattern in the fontset of the selected frame.
2007 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2008 family name and REGISTRY is a font registry name. This is actually
2009 the first font name pattern for CH in the fontset or in the default
2010 fontset.
2012 If the 2nd optional arg ALL is non-nil, return a list of all font name
2013 patterns. */)
2014 (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
2016 int c;
2017 Lisp_Object fontset, elt, list, repertory, val;
2018 int i, j;
2019 Lisp_Object frame;
2021 frame = Qnil;
2022 fontset = check_fontset_name (name, &frame);
2024 CHECK_CHARACTER (ch);
2025 c = XINT (ch);
2026 list = Qnil;
2027 while (1)
2029 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2030 i++, elt = FONTSET_FALLBACK (fontset))
2031 if (VECTORP (elt))
2032 for (j = 0; j < ASIZE (elt); j++)
2034 Lisp_Object family, registry;
2036 val = AREF (elt, j);
2037 if (NILP (val))
2038 return Qnil;
2039 repertory = AREF (val, 1);
2040 if (INTEGERP (repertory))
2042 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2044 if (! CHAR_CHARSET_P (c, charset))
2045 continue;
2047 else if (CHAR_TABLE_P (repertory))
2049 if (NILP (CHAR_TABLE_REF (repertory, c)))
2050 continue;
2052 val = AREF (val, 0);
2053 /* VAL is a FONT-SPEC */
2054 family = AREF (val, FONT_FAMILY_INDEX);
2055 if (! NILP (family))
2056 family = SYMBOL_NAME (family);
2057 registry = AREF (val, FONT_REGISTRY_INDEX);
2058 if (! NILP (registry))
2059 registry = SYMBOL_NAME (registry);
2060 val = Fcons (family, registry);
2061 if (NILP (all))
2062 return val;
2063 list = Fcons (val, list);
2065 if (EQ (fontset, Vdefault_fontset))
2066 break;
2067 fontset = Vdefault_fontset;
2069 return (Fnreverse (list));
2072 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2073 doc: /* Return a list of all defined fontset names. */)
2074 (void)
2076 Lisp_Object fontset, list;
2077 int i;
2079 list = Qnil;
2080 for (i = 0; i < ASIZE (Vfontset_table); i++)
2082 fontset = FONTSET_FROM_ID (i);
2083 if (!NILP (fontset)
2084 && BASE_FONTSET_P (fontset))
2085 list = Fcons (FONTSET_NAME (fontset), list);
2088 return list;
2092 #ifdef ENABLE_CHECKING
2094 Lisp_Object dump_fontset (Lisp_Object) EXTERNALLY_VISIBLE;
2096 Lisp_Object
2097 dump_fontset (Lisp_Object fontset)
2099 Lisp_Object vec;
2101 vec = Fmake_vector (make_number (3), Qnil);
2102 ASET (vec, 0, FONTSET_ID (fontset));
2104 if (BASE_FONTSET_P (fontset))
2106 ASET (vec, 1, FONTSET_NAME (fontset));
2108 else
2110 Lisp_Object frame;
2112 frame = FONTSET_FRAME (fontset);
2113 if (FRAMEP (frame))
2115 struct frame *f = XFRAME (frame);
2117 if (FRAME_LIVE_P (f))
2118 ASET (vec, 1,
2119 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)),
2120 f->name));
2121 else
2122 ASET (vec, 1,
2123 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2125 if (!NILP (FONTSET_DEFAULT (fontset)))
2126 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2128 return vec;
2131 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2132 doc: /* Return a brief summary of all fontsets for debug use. */)
2133 (void)
2135 Lisp_Object val;
2136 int i;
2138 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2139 if (! NILP (AREF (Vfontset_table, i)))
2140 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2141 return (Fnreverse (val));
2143 #endif /* ENABLE_CHECKING */
2145 void
2146 syms_of_fontset (void)
2148 DEFSYM (Qfontset, "fontset");
2149 Fput (Qfontset, Qchar_table_extra_slots, make_number (8));
2150 DEFSYM (Qfontset_info, "fontset-info");
2151 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2153 DEFSYM (Qprepend, "prepend");
2154 DEFSYM (Qappend, "append");
2155 DEFSYM (Qlatin, "latin");
2157 Vcached_fontset_data = Qnil;
2158 staticpro (&Vcached_fontset_data);
2160 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2161 staticpro (&Vfontset_table);
2163 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2164 staticpro (&Vdefault_fontset);
2165 set_fontset_id (Vdefault_fontset, make_number (0));
2166 set_fontset_name
2167 (Vdefault_fontset,
2168 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2169 ASET (Vfontset_table, 0, Vdefault_fontset);
2170 next_fontset_id = 1;
2172 auto_fontset_alist = Qnil;
2173 staticpro (&auto_fontset_alist);
2175 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist,
2176 doc: /*
2177 Alist of charsets vs the charsets to determine the preferred font encoding.
2178 Each element looks like (CHARSET . ENCODING-CHARSET),
2179 where ENCODING-CHARSET is a charset registered in the variable
2180 `font-encoding-alist' as ENCODING.
2182 When a text has a property `charset' and the value is CHARSET, a font
2183 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2184 Vfont_encoding_charset_alist = Qnil;
2186 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent,
2187 doc: /*
2188 Char table of characters whose ascent values should be ignored.
2189 If an entry for a character is non-nil, the ascent value of the glyph
2190 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2192 This affects how a composite character which contains
2193 such a character is displayed on screen. */);
2194 Vuse_default_ascent = Qnil;
2196 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition,
2197 doc: /*
2198 Char table of characters which are not composed relatively.
2199 If an entry for a character is non-nil, a composition sequence
2200 which contains that character is displayed so that
2201 the glyph of that character is put without considering
2202 an ascent and descent value of a previous character. */);
2203 Vignore_relative_composition = Qnil;
2205 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist,
2206 doc: /* Alist of fontname vs list of the alternate fontnames.
2207 When a specified font name is not found, the corresponding
2208 alternate fontnames (if any) are tried instead. */);
2209 Valternate_fontname_alist = Qnil;
2211 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist,
2212 doc: /* Alist of fontset names vs the aliases. */);
2213 Vfontset_alias_alist
2214 = list1 (Fcons (FONTSET_NAME (Vdefault_fontset),
2215 build_pure_c_string ("fontset-default")));
2217 DEFVAR_LISP ("vertical-centering-font-regexp",
2218 Vvertical_centering_font_regexp,
2219 doc: /* Regexp matching font names that require vertical centering on display.
2220 When a character is displayed with such fonts, the character is displayed
2221 at the vertical center of lines. */);
2222 Vvertical_centering_font_regexp = Qnil;
2224 DEFVAR_LISP ("otf-script-alist", Votf_script_alist,
2225 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2226 Votf_script_alist = Qnil;
2228 defsubr (&Squery_fontset);
2229 defsubr (&Snew_fontset);
2230 defsubr (&Sset_fontset_font);
2231 defsubr (&Sinternal_char_font);
2232 defsubr (&Sfontset_info);
2233 defsubr (&Sfontset_font);
2234 defsubr (&Sfontset_list);
2235 #ifdef ENABLE_CHECKING
2236 defsubr (&Sfontset_list_all);
2237 #endif