* keyboard.c (Fset_input_meta_mode): Doc fix.
[emacs.git] / src / fontset.c
blob6e2e34dac77407978f9d803af0170b52de3cfed6
1 /* Fontset handler.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009
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 /* #define FONTSET_DEBUG */
29 #include <config.h>
30 #include <stdio.h>
32 #include "lisp.h"
33 #include "blockinput.h"
34 #include "buffer.h"
35 #include "character.h"
36 #include "charset.h"
37 #include "ccl.h"
38 #include "keyboard.h"
39 #include "frame.h"
40 #include "dispextern.h"
41 #include "intervals.h"
42 #include "fontset.h"
43 #include "window.h"
44 #ifdef HAVE_X_WINDOWS
45 #include "xterm.h"
46 #endif
47 #ifdef WINDOWSNT
48 #include "w32term.h"
49 #endif
50 #ifdef HAVE_NS
51 #include "nsterm.h"
52 #endif
53 #include "termhooks.h"
55 #include "font.h"
57 #undef xassert
58 #ifdef FONTSET_DEBUG
59 #define xassert(X) do {if (!(X)) abort ();} while (0)
60 #undef INLINE
61 #define INLINE
62 #else /* not FONTSET_DEBUG */
63 #define xassert(X) (void) 0
64 #endif /* not FONTSET_DEBUG */
66 EXFUN (Fclear_face_cache, 1);
68 /* FONTSET
70 A fontset is a collection of font related information to give
71 similar appearance (style, etc) of characters. A fontset has two
72 roles. One is to use for the frame parameter `font' as if it is an
73 ASCII font. In that case, Emacs uses the font specified for
74 `ascii' script for the frame's default font.
76 Another role, the more important one, is to provide information
77 about which font to use for each non-ASCII character.
79 There are two kinds of fontsets; base and realized. A base fontset
80 is created by `new-fontset' from Emacs Lisp explicitly. A realized
81 fontset is created implicitly when a face is realized for ASCII
82 characters. A face is also realized for non-ASCII characters based
83 on an ASCII face. All of non-ASCII faces based on the same ASCII
84 face share the same realized fontset.
86 A fontset object is implemented by a char-table whose default value
87 and parent are always nil.
89 An element of a base fontset is a vector of FONT-DEFs which itself
90 is a vector [ FONT-SPEC ENCODING REPERTORY ].
92 An element of a realized fontset is nil, t, 0, or a vector of this
93 form:
95 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
96 RFONT-DEF0 RFONT-DEF1 ... ]
98 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
100 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
102 RFONT-DEFn are automatically reordered by the current charset
103 priority list.
105 The value nil means that we have not yet generated the above vector
106 from the base of the fontset.
108 The value t means that no font is available for the corresponding
109 range of characters.
111 The value 0 means that no font is available for the corresponding
112 range of characters in this fontset, but may be available in the
113 default fontset.
116 A fontset has 9 extra slots.
118 The 1st slot: the ID number of the fontset
120 The 2nd slot:
121 base: the name of the fontset
122 realized: nil
124 The 3rd slot:
125 base: nil
126 realized: the base fontset
128 The 4th slot:
129 base: nil
130 realized: the frame that the fontset belongs to
132 The 5th slot:
133 base: the font name for ASCII characters
134 realized: nil
136 The 6th slot:
137 base: nil
138 realized: the ID number of a face to use for characters that
139 has no font in a realized fontset.
141 The 7th slot:
142 base: nil
143 realized: Alist of font index vs the corresponding repertory
144 char-table.
146 The 8th slot:
147 base: nil
148 realized: If the base is not the default fontset, a fontset
149 realized from the default fontset, else nil.
151 The 9th slot:
152 base: Same as element value (but for fallback fonts).
153 realized: Likewise.
155 All fontsets are recorded in the vector Vfontset_table.
158 DEFAULT FONTSET
160 There's a special base fontset named `default fontset' which
161 defines the default font specifications. When a base fontset
162 doesn't specify a font for a specific character, the corresponding
163 value in the default fontset is used.
165 The parent of a realized fontset created for such a face that has
166 no fontset is the default fontset.
169 These structures are hidden from the other codes than this file.
170 The other codes handle fontsets only by their ID numbers. They
171 usually use the variable name `fontset' for IDs. But, in this
172 file, we always use varialbe name `id' for IDs, and name `fontset'
173 for an actual fontset object, i.e., char-table.
177 /********** VARIABLES and FUNCTION PROTOTYPES **********/
179 extern Lisp_Object Qfont;
180 static Lisp_Object Qfontset;
181 static Lisp_Object Qfontset_info;
182 static Lisp_Object Qprepend, Qappend;
183 static Lisp_Object Qlatin;
185 /* Vector containing all fontsets. */
186 static Lisp_Object Vfontset_table;
188 /* Next possibly free fontset ID. Usually this keeps the minimum
189 fontset ID not yet used. */
190 static int next_fontset_id;
192 /* The default fontset. This gives default FAMILY and REGISTRY of
193 font for each character. */
194 static Lisp_Object Vdefault_fontset;
196 Lisp_Object Vfont_encoding_charset_alist;
197 Lisp_Object Vuse_default_ascent;
198 Lisp_Object Vignore_relative_composition;
199 Lisp_Object Valternate_fontname_alist;
200 Lisp_Object Vfontset_alias_alist;
201 Lisp_Object Vvertical_centering_font_regexp;
202 Lisp_Object Votf_script_alist;
204 /* Check if any window system is used now. */
205 void (*check_window_system_func) P_ ((void));
208 /* Prototype declarations for static functions. */
209 static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
210 Lisp_Object));
211 static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
212 int, int));
213 static void reorder_font_vector P_ ((Lisp_Object, struct font *));
214 static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
215 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
216 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
217 static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
218 Lisp_Object));
219 Lisp_Object find_font_encoding P_ ((Lisp_Object));
221 static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
223 #ifdef FONTSET_DEBUG
225 /* Return 1 if ID is a valid fontset id, else return 0. */
227 static int
228 fontset_id_valid_p (id)
229 int id;
231 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
234 #endif
238 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
240 /* Return the fontset with ID. No check of ID's validness. */
241 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
243 /* Macros to access special values of FONTSET. */
244 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
246 /* Macros to access special values of (base) FONTSET. */
247 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
248 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
249 #define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5]
251 /* Macros to access special values of (realized) FONTSET. */
252 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
253 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
254 #define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4]
255 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
256 #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
257 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
259 /* For both base and realized fontset. */
260 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
262 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
265 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
266 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
267 do { \
268 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
269 ASET ((font_def), 1, encoding); \
270 ASET ((font_def), 2, repertory); \
271 } while (0)
273 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
274 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
275 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
277 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
278 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
279 ASET ((rfont_def), 0, make_number (face_id))
280 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
281 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
282 #define RFONT_DEF_REPERTORY(rfont_def) FONT_DEF_REPERTORY (AREF (rfont_def, 1))
283 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
284 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
285 ASET ((rfont_def), 2, (object))
286 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
287 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
288 ASET ((rfont_def), 3, make_number (score))
289 #define RFONT_DEF_NEW(rfont_def, font_def) \
290 do { \
291 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
292 ASET ((rfont_def), 1, (font_def)); \
293 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
294 } while (0)
297 /* Return the element of FONTSET for the character C. If FONTSET is a
298 base fontset other then the default fontset and FONTSET doesn't
299 contain information for C, return the information in the default
300 fontset. */
302 #define FONTSET_REF(fontset, c) \
303 (EQ (fontset, Vdefault_fontset) \
304 ? CHAR_TABLE_REF (fontset, c) \
305 : fontset_ref ((fontset), (c)))
307 static Lisp_Object
308 fontset_ref (fontset, c)
309 Lisp_Object fontset;
310 int c;
312 Lisp_Object elt;
314 elt = CHAR_TABLE_REF (fontset, c);
315 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
316 /* Don't check Vdefault_fontset for a realized fontset. */
317 && NILP (FONTSET_BASE (fontset)))
318 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
319 return elt;
322 /* Set elements of FONTSET for characters in RANGE to the value ELT.
323 RANGE is a cons (FROM . TO), where FROM and TO are character codes
324 specifying a range. */
326 #define FONTSET_SET(fontset, range, elt) \
327 Fset_char_table_range ((fontset), (range), (elt))
330 /* Modify the elements of FONTSET for characters in RANGE by replacing
331 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
332 and TO are character codes specifying a range. If ADD is nil,
333 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
334 append ELT. */
336 #define FONTSET_ADD(fontset, range, elt, add) \
337 (NILP (add) \
338 ? (NILP (range) \
339 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
340 : Fset_char_table_range ((fontset), (range), \
341 Fmake_vector (make_number (1), (elt)))) \
342 : fontset_add ((fontset), (range), (elt), (add)))
344 static Lisp_Object
345 fontset_add (fontset, range, elt, add)
346 Lisp_Object fontset, range, elt, add;
348 Lisp_Object args[2];
349 int idx = (EQ (add, Qappend) ? 0 : 1);
351 args[1 - idx] = Fmake_vector (make_number (1), elt);
353 if (CONSP (range))
355 int from = XINT (XCAR (range));
356 int to = XINT (XCDR (range));
357 int from1, to1;
359 do {
360 from1 = from, to1 = to;
361 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
362 char_table_set_range (fontset, from, to1,
363 NILP (args[idx]) ? args[1 - idx]
364 : Fvconcat (2, args));
365 from = to1 + 1;
366 } while (from < to);
368 else
370 args[idx] = FONTSET_FALLBACK (fontset);
371 FONTSET_FALLBACK (fontset)
372 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
374 return Qnil;
377 static int
378 fontset_compare_rfontdef (val1, val2)
379 const void *val1, *val2;
381 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
382 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
385 /* Update FONT-GROUP which has this form:
386 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
387 RFONT-DEF0 RFONT-DEF1 ... ]
388 Reorder RFONT-DEFs according to the current language, and update
389 CHARSET-ORDERED-LIST-TICK.
391 If PREFERRED_FAMILY is not nil, that family has the higher priority
392 if the encoding charsets or languages in font-specs are the same. */
394 extern Lisp_Object Fassoc_string ();
396 static void
397 reorder_font_vector (font_group, font)
398 Lisp_Object font_group;
399 struct font *font;
401 Lisp_Object vec, font_object;
402 int size;
403 int i;
404 int score_changed = 0;
406 if (font)
407 XSETFONT (font_object, font);
408 else
409 font_object = Qnil;
411 vec = XCDR (font_group);
412 size = ASIZE (vec);
413 /* Exclude the tailing nil element from the reordering. */
414 if (NILP (AREF (vec, size - 1)))
415 size--;
417 for (i = 0; i < size; i++)
419 Lisp_Object rfont_def = AREF (vec, i);
420 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
421 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
422 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
424 if (! font_match_p (font_spec, font_object))
426 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
428 if (! NILP (encoding))
430 Lisp_Object tail;
432 for (tail = Vcharset_ordered_list;
433 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
434 score += 0x100, tail = XCDR (tail))
435 if (EQ (encoding, XCAR (tail)))
436 break;
438 else
440 Lisp_Object lang = Ffont_get (font_spec, QClang);
442 if (! NILP (lang)
443 && ! EQ (lang, Vcurrent_iso639_language)
444 && (! CONSP (Vcurrent_iso639_language)
445 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
446 score |= 0x100;
449 if (RFONT_DEF_SCORE (rfont_def) != score)
451 RFONT_DEF_SET_SCORE (rfont_def, score);
452 score_changed = 1;
456 if (score_changed)
457 qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
458 fontset_compare_rfontdef);
459 XSETCAR (font_group, make_number (charset_ordered_list_tick));
462 static Lisp_Object
463 fontset_get_font_group (Lisp_Object fontset, int c)
465 Lisp_Object font_group;
466 Lisp_Object base_fontset;
467 int from = 0, to = MAX_CHAR, i;
469 xassert (! BASE_FONTSET_P (fontset));
470 if (c >= 0)
471 font_group = CHAR_TABLE_REF (fontset, c);
472 else
473 font_group = FONTSET_FALLBACK (fontset);
474 if (! NILP (font_group))
475 return font_group;
476 base_fontset = FONTSET_BASE (fontset);
477 if (c >= 0)
478 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
479 else
480 font_group = FONTSET_FALLBACK (base_fontset);
481 if (NILP (font_group))
483 if (c >= 0)
484 char_table_set_range (fontset, from, to, make_number (0));
485 return Qnil;
487 font_group = Fcopy_sequence (font_group);
488 for (i = 0; i < ASIZE (font_group); i++)
489 if (! NILP (AREF (font_group, i)))
491 Lisp_Object rfont_def;
493 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
494 /* Remember the original order. */
495 RFONT_DEF_SET_SCORE (rfont_def, i);
496 ASET (font_group, i, rfont_def);
498 font_group = Fcons (make_number (-1), font_group);
499 if (c >= 0)
500 char_table_set_range (fontset, from, to, font_group);
501 else
502 FONTSET_FALLBACK (fontset) = font_group;
503 return font_group;
506 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
507 character C. If no font is found, return Qnil if there's a
508 possibility that the default fontset or the fallback font groups
509 have a proper font, and return Qt if not.
511 If a font is found but is not yet opened, open it (if FACE is not
512 NULL) or return Qnil (if FACE is NULL).
514 ID is a charset-id that must be preferred, or -1 meaning no
515 preference.
517 If FALLBACK is nonzero, search only fallback fonts. */
519 static Lisp_Object
520 fontset_find_font (fontset, c, face, id, fallback)
521 Lisp_Object fontset;
522 int c;
523 struct face *face;
524 int id, fallback;
526 Lisp_Object vec, font_group;
527 int i, charset_matched = -1;
528 FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset)))
529 ? XFRAME (selected_frame) : XFRAME (FONTSET_FRAME (fontset));
531 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
532 if (! CONSP (font_group))
533 return font_group;
534 vec = XCDR (font_group);
535 if (ASIZE (vec) == 0)
536 return Qnil;
538 if (ASIZE (vec) > 1)
540 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
541 /* We have just created the font-group,
542 or the charset priorities were changed. */
543 reorder_font_vector (font_group, face->ascii_face->font);
544 if (id >= 0)
545 /* Find a spec matching with the charset ID to try at
546 first. */
547 for (i = 0; i < ASIZE (vec); i++)
549 Lisp_Object rfont_def = AREF (vec, i);
550 Lisp_Object repertory
551 = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
553 if (XINT (repertory) == id)
555 charset_matched = i;
556 break;
561 /* Find the first available font in the vector of RFONT-DEF. */
562 for (i = 0; i < ASIZE (vec); i++)
564 Lisp_Object rfont_def, font_def;
565 Lisp_Object font_entity, font_object;
567 if (i == 0 && charset_matched >= 0)
569 /* Try the element matching with the charset ID at first. */
570 rfont_def = AREF (vec, charset_matched);
571 charset_matched = -1;
572 i--;
574 else if (i != charset_matched)
575 rfont_def = AREF (vec, i);
576 else
577 continue;
579 if (NILP (rfont_def))
580 /* This is a sign of not to try the other fonts. */
581 return Qt;
582 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
583 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
584 /* We couldn't open this font last time. */
585 continue;
587 font_object = RFONT_DEF_OBJECT (rfont_def);
588 if (NILP (font_object))
590 font_def = RFONT_DEF_FONT_DEF (rfont_def);
592 if (! face)
593 /* We have not yet opened the font. */
594 return Qnil;
595 /* Find a font best-matching with the spec without checking
596 the support of the character C. That checking is costly,
597 and even without the checking, the found font supports C
598 in high possibility. */
599 font_entity = font_find_for_lface (f, face->lface,
600 FONT_DEF_SPEC (font_def), -1);
601 if (NILP (font_entity))
603 /* Record that no font matches the spec. */
604 RFONT_DEF_SET_FACE (rfont_def, -1);
605 continue;
607 font_object = font_open_for_lface (f, font_entity, face->lface,
608 FONT_DEF_SPEC (font_def));
609 if (NILP (font_object))
611 /* Something strange happened, perhaps because of a
612 Font-backend problem. Too avoid crashing, record
613 that this spec is unsable. It may be better to find
614 another font of the same spec, but currently we don't
615 have such an API. */
616 RFONT_DEF_SET_FACE (rfont_def, -1);
617 continue;
619 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
622 if (font_has_char (f, font_object, c))
623 return rfont_def;
625 /* Find a font already opened, maching with the current spec,
626 and supporting C. */
627 font_def = RFONT_DEF_FONT_DEF (rfont_def);
628 for (i++; i < ASIZE (vec); i++)
630 rfont_def = AREF (vec, i);
631 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
632 break;
633 font_object = RFONT_DEF_OBJECT (AREF (vec, i));
634 if (! NILP (font_object) && font_has_char (f, font_object, c))
635 return rfont_def;
638 /* Find a font-entity with the current spec and supporting C. */
639 font_entity = font_find_for_lface (f, face->lface,
640 FONT_DEF_SPEC (font_def), c);
641 if (! NILP (font_entity))
643 /* We found a font. Open it and insert a new element for
644 that font in VEC. */
645 Lisp_Object new_vec;
646 int j;
648 font_object = font_open_for_lface (f, font_entity, face->lface,
649 Qnil);
650 if (NILP (font_object))
651 continue;
652 RFONT_DEF_NEW (rfont_def, font_def);
653 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
654 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
655 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
656 for (j = 0; j < i; j++)
657 ASET (new_vec, j, AREF (vec, j));
658 ASET (new_vec, j, rfont_def);
659 for (j++; j < ASIZE (new_vec); j++)
660 ASET (new_vec, j, AREF (vec, j - 1));
661 XSETCDR (font_group, new_vec);
662 return rfont_def;
665 /* No font of the current spec for C. Try the next spec. */
666 i--;
669 FONTSET_SET (fontset, make_number (c), make_number (0));
670 return Qnil;
674 static Lisp_Object
675 fontset_font (fontset, c, face, id)
676 Lisp_Object fontset;
677 int c;
678 struct face *face;
679 int id;
681 Lisp_Object rfont_def;
682 Lisp_Object base_fontset;
684 /* Try a font-group of FONTSET. */
685 rfont_def = fontset_find_font (fontset, c, face, id, 0);
686 if (VECTORP (rfont_def))
687 return rfont_def;
688 if (EQ (rfont_def, Qt))
689 goto no_font;
691 /* Try a font-group of the default fontset. */
692 base_fontset = FONTSET_BASE (fontset);
693 if (! EQ (base_fontset, Vdefault_fontset))
695 if (NILP (FONTSET_DEFAULT (fontset)))
696 FONTSET_DEFAULT (fontset)
697 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
698 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
699 if (VECTORP (rfont_def))
700 return rfont_def;
701 if (EQ (rfont_def, Qt))
702 goto no_font;
705 /* Try a fallback font-group of FONTSET. */
706 rfont_def = fontset_find_font (fontset, c, face, id, 1);
707 if (VECTORP (rfont_def))
708 return rfont_def;
709 if (EQ (rfont_def, Qt))
710 goto no_font;
712 /* Try a fallback font-group of the default fontset . */
713 if (! EQ (base_fontset, Vdefault_fontset))
715 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
716 if (VECTORP (rfont_def))
717 return rfont_def;
720 no_font:
721 /* Remember that we have no font for C. */
722 FONTSET_SET (fontset, make_number (c), Qt);
724 return Qnil;
727 /* Return a newly created fontset with NAME. If BASE is nil, make a
728 base fontset. Otherwise make a realized fontset whose base is
729 BASE. */
731 static Lisp_Object
732 make_fontset (frame, name, base)
733 Lisp_Object frame, name, base;
735 Lisp_Object fontset;
736 int size = ASIZE (Vfontset_table);
737 int id = next_fontset_id;
739 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
740 the next available fontset ID. So it is expected that this loop
741 terminates quickly. In addition, as the last element of
742 Vfontset_table is always nil, we don't have to check the range of
743 id. */
744 while (!NILP (AREF (Vfontset_table, id))) id++;
746 if (id + 1 == size)
747 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
749 fontset = Fmake_char_table (Qfontset, Qnil);
751 FONTSET_ID (fontset) = make_number (id);
752 if (NILP (base))
754 FONTSET_NAME (fontset) = name;
756 else
758 FONTSET_NAME (fontset) = Qnil;
759 FONTSET_FRAME (fontset) = frame;
760 FONTSET_BASE (fontset) = base;
763 ASET (Vfontset_table, id, fontset);
764 next_fontset_id = id + 1;
765 return fontset;
769 /* Set the ASCII font of the default fontset to FONTNAME if that is
770 not yet set. */
771 void
772 set_default_ascii_font (fontname)
773 Lisp_Object fontname;
775 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
777 int id = fs_query_fontset (fontname, 2);
779 if (id >= 0)
780 fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
781 FONTSET_ASCII (Vdefault_fontset)= fontname;
786 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
788 /* Return the name of the fontset who has ID. */
790 Lisp_Object
791 fontset_name (id)
792 int id;
794 Lisp_Object fontset;
796 fontset = FONTSET_FROM_ID (id);
797 return FONTSET_NAME (fontset);
801 /* Return the ASCII font name of the fontset who has ID. */
803 Lisp_Object
804 fontset_ascii (id)
805 int id;
807 Lisp_Object fontset, elt;
809 fontset= FONTSET_FROM_ID (id);
810 elt = FONTSET_ASCII (fontset);
811 if (CONSP (elt))
812 elt = XCAR (elt);
813 return elt;
816 void
817 free_realized_fontset (f, fontset)
818 FRAME_PTR f;
819 Lisp_Object fontset;
821 Lisp_Object tail;
823 return;
824 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
826 xassert (FONT_OBJECT_P (XCAR (tail)));
827 font_close_object (f, XCAR (tail));
831 /* Free fontset of FACE defined on frame F. Called from
832 free_realized_face. */
834 void
835 free_face_fontset (f, face)
836 FRAME_PTR f;
837 struct face *face;
839 Lisp_Object fontset;
841 fontset = FONTSET_FROM_ID (face->fontset);
842 if (NILP (fontset))
843 return;
844 xassert (! BASE_FONTSET_P (fontset));
845 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
846 free_realized_fontset (f, fontset);
847 ASET (Vfontset_table, face->fontset, Qnil);
848 if (face->fontset < next_fontset_id)
849 next_fontset_id = face->fontset;
850 if (! NILP (FONTSET_DEFAULT (fontset)))
852 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
854 fontset = AREF (Vfontset_table, id);
855 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
856 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
857 free_realized_fontset (f, fontset);
858 ASET (Vfontset_table, id, Qnil);
859 if (id < next_fontset_id)
860 next_fontset_id = face->fontset;
862 face->fontset = -1;
866 /* Return 1 if FACE is suitable for displaying character C.
867 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
868 when C is not an ASCII character. */
871 face_suitable_for_char_p (face, c)
872 struct face *face;
873 int c;
875 Lisp_Object fontset, rfont_def;
877 fontset = FONTSET_FROM_ID (face->fontset);
878 rfont_def = fontset_font (fontset, c, NULL, -1);
879 return (VECTORP (rfont_def)
880 && INTEGERP (RFONT_DEF_FACE (rfont_def))
881 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
885 /* Return ID of face suitable for displaying character C on frame F.
886 FACE must be reazlied for ASCII characters in advance. Called from
887 the macro FACE_FOR_CHAR. */
890 face_for_char (f, face, c, pos, object)
891 FRAME_PTR f;
892 struct face *face;
893 int c, pos;
894 Lisp_Object object;
896 Lisp_Object fontset, rfont_def, charset;
897 int face_id;
898 int id;
900 /* If face->fontset is negative (that happens when no font is found
901 for face), just return face->ascii_face because we can't do
902 anything. Perhaps, we should fix the callers to assure
903 that face->fontset is always valid. */
904 if (ASCII_CHAR_P (c) || face->fontset < 0)
905 return face->ascii_face->id;
907 xassert (fontset_id_valid_p (face->fontset));
908 fontset = FONTSET_FROM_ID (face->fontset);
909 xassert (!BASE_FONTSET_P (fontset));
911 if (pos < 0)
913 id = -1;
914 charset = Qnil;
916 else
918 charset = Fget_char_property (make_number (pos), Qcharset, object);
919 if (CHARSETP (charset))
921 Lisp_Object val;
923 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
924 if (CONSP (val) && CHARSETP (XCDR (val)))
925 charset = XCDR (val);
926 id = XINT (CHARSET_SYMBOL_ID (charset));
928 else
929 id = -1;
932 font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
933 rfont_def = fontset_font (fontset, c, face, id);
934 if (VECTORP (rfont_def))
936 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
937 face_id = XINT (RFONT_DEF_FACE (rfont_def));
938 else
940 Lisp_Object font_object;
942 font_object = RFONT_DEF_OBJECT (rfont_def);
943 face_id = face_for_font (f, font_object, face);
944 RFONT_DEF_SET_FACE (rfont_def, face_id);
947 else
949 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
950 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
951 else
953 face_id = face_for_font (f, Qnil, face);
954 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
957 xassert (face_id >= 0);
958 return face_id;
962 Lisp_Object
963 font_for_char (face, c, pos, object)
964 struct face *face;
965 int c, pos;
966 Lisp_Object object;
968 Lisp_Object fontset, rfont_def, charset;
969 int face_id;
970 int id;
972 if (ASCII_CHAR_P (c))
974 Lisp_Object font_object;
976 XSETFONT (font_object, face->ascii_face->font);
977 return font_object;
980 xassert (fontset_id_valid_p (face->fontset));
981 fontset = FONTSET_FROM_ID (face->fontset);
982 xassert (!BASE_FONTSET_P (fontset));
983 if (pos < 0)
985 id = -1;
986 charset = Qnil;
988 else
990 charset = Fget_char_property (make_number (pos), Qcharset, object);
991 if (CHARSETP (charset))
993 Lisp_Object val;
995 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
996 if (CONSP (val) && CHARSETP (XCDR (val)))
997 charset = XCDR (val);
998 id = XINT (CHARSET_SYMBOL_ID (charset));
1000 else
1001 id = -1;
1004 font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
1005 rfont_def = fontset_font (fontset, c, face, id);
1006 return (VECTORP (rfont_def)
1007 ? RFONT_DEF_OBJECT (rfont_def)
1008 : Qnil);
1012 /* Make a realized fontset for ASCII face FACE on frame F from the
1013 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1014 default fontset as the base. Value is the id of the new fontset.
1015 Called from realize_x_face. */
1018 make_fontset_for_ascii_face (f, base_fontset_id, face)
1019 FRAME_PTR f;
1020 int base_fontset_id;
1021 struct face *face;
1023 Lisp_Object base_fontset, fontset, frame;
1025 XSETFRAME (frame, f);
1026 if (base_fontset_id >= 0)
1028 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1029 if (!BASE_FONTSET_P (base_fontset))
1030 base_fontset = FONTSET_BASE (base_fontset);
1031 if (! BASE_FONTSET_P (base_fontset))
1032 abort ();
1034 else
1035 base_fontset = Vdefault_fontset;
1037 fontset = make_fontset (frame, Qnil, base_fontset);
1038 return XINT (FONTSET_ID (fontset));
1043 /* Cache data used by fontset_pattern_regexp. The car part is a
1044 pattern string containing at least one wild card, the cdr part is
1045 the corresponding regular expression. */
1046 static Lisp_Object Vcached_fontset_data;
1048 #define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
1049 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1051 /* If fontset name PATTERN contains any wild card, return regular
1052 expression corresponding to PATTERN. */
1054 static Lisp_Object
1055 fontset_pattern_regexp (pattern)
1056 Lisp_Object pattern;
1058 if (!index ((char *) SDATA (pattern), '*')
1059 && !index ((char *) SDATA (pattern), '?'))
1060 /* PATTERN does not contain any wild cards. */
1061 return Qnil;
1063 if (!CONSP (Vcached_fontset_data)
1064 || strcmp ((char *) SDATA (pattern), CACHED_FONTSET_NAME))
1066 /* We must at first update the cached data. */
1067 unsigned char *regex, *p0, *p1;
1068 int ndashes = 0, nstars = 0, nescs = 0;
1070 for (p0 = SDATA (pattern); *p0; p0++)
1072 if (*p0 == '-')
1073 ndashes++;
1074 else if (*p0 == '*')
1075 nstars++;
1076 else if (*p0 == '['
1077 || *p0 == '.' || *p0 == '\\'
1078 || *p0 == '+' || *p0 == '^'
1079 || *p0 == '$')
1080 nescs++;
1083 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1084 we convert "*" to "[^-]*" which is much faster in regular
1085 expression matching. */
1086 if (ndashes < 14)
1087 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
1088 else
1089 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
1091 *p1++ = '^';
1092 for (p0 = SDATA (pattern); *p0; p0++)
1094 if (*p0 == '*')
1096 if (ndashes < 14)
1097 *p1++ = '.';
1098 else
1099 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1100 *p1++ = '*';
1102 else if (*p0 == '?')
1103 *p1++ = '.';
1104 else if (*p0 == '['
1105 || *p0 == '.' || *p0 == '\\'
1106 || *p0 == '+' || *p0 == '^'
1107 || *p0 == '$')
1108 *p1++ = '\\', *p1++ = *p0;
1109 else
1110 *p1++ = *p0;
1112 *p1++ = '$';
1113 *p1++ = 0;
1115 Vcached_fontset_data = Fcons (build_string ((char *) SDATA (pattern)),
1116 build_string ((char *) regex));
1119 return CACHED_FONTSET_REGEX;
1122 /* Return ID of the base fontset named NAME. If there's no such
1123 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1124 0: pattern containing '*' and '?' as wildcards
1125 1: regular expression
1126 2: literal fontset name
1130 fs_query_fontset (name, name_pattern)
1131 Lisp_Object name;
1132 int name_pattern;
1134 Lisp_Object tem;
1135 int i;
1137 name = Fdowncase (name);
1138 if (name_pattern != 1)
1140 tem = Frassoc (name, Vfontset_alias_alist);
1141 if (NILP (tem))
1142 tem = Fassoc (name, Vfontset_alias_alist);
1143 if (CONSP (tem) && STRINGP (XCAR (tem)))
1144 name = XCAR (tem);
1145 else if (name_pattern == 0)
1147 tem = fontset_pattern_regexp (name);
1148 if (STRINGP (tem))
1150 name = tem;
1151 name_pattern = 1;
1156 for (i = 0; i < ASIZE (Vfontset_table); i++)
1158 Lisp_Object fontset, this_name;
1160 fontset = FONTSET_FROM_ID (i);
1161 if (NILP (fontset)
1162 || !BASE_FONTSET_P (fontset))
1163 continue;
1165 this_name = FONTSET_NAME (fontset);
1166 if (name_pattern == 1
1167 ? fast_string_match_ignore_case (name, this_name) >= 0
1168 : !xstrcasecmp (SDATA (name), SDATA (this_name)))
1169 return i;
1171 return -1;
1175 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1176 doc: /* Return the name of a fontset that matches PATTERN.
1177 The value is nil if there is no matching fontset.
1178 PATTERN can contain `*' or `?' as a wildcard
1179 just as X font name matching algorithm allows.
1180 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1181 (pattern, regexpp)
1182 Lisp_Object pattern, regexpp;
1184 Lisp_Object fontset;
1185 int id;
1187 (*check_window_system_func) ();
1189 CHECK_STRING (pattern);
1191 if (SCHARS (pattern) == 0)
1192 return Qnil;
1194 id = fs_query_fontset (pattern, !NILP (regexpp));
1195 if (id < 0)
1196 return Qnil;
1198 fontset = FONTSET_FROM_ID (id);
1199 return FONTSET_NAME (fontset);
1202 /* Return a list of base fontset names matching PATTERN on frame F. */
1204 Lisp_Object
1205 list_fontsets (f, pattern, size)
1206 FRAME_PTR f;
1207 Lisp_Object pattern;
1208 int size;
1210 Lisp_Object frame, regexp, val;
1211 int id;
1213 XSETFRAME (frame, f);
1215 regexp = fontset_pattern_regexp (pattern);
1216 val = Qnil;
1218 for (id = 0; id < ASIZE (Vfontset_table); id++)
1220 Lisp_Object fontset, name;
1222 fontset = FONTSET_FROM_ID (id);
1223 if (NILP (fontset)
1224 || !BASE_FONTSET_P (fontset)
1225 || !EQ (frame, FONTSET_FRAME (fontset)))
1226 continue;
1227 name = FONTSET_NAME (fontset);
1229 if (STRINGP (regexp)
1230 ? (fast_string_match (regexp, name) < 0)
1231 : strcmp ((char *) SDATA (pattern), (char *) SDATA (name)))
1232 continue;
1234 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1237 return val;
1241 /* Free all realized fontsets whose base fontset is BASE. */
1243 static void
1244 free_realized_fontsets (base)
1245 Lisp_Object base;
1247 int id;
1249 #if 0
1250 /* For the moment, this doesn't work because free_realized_face
1251 doesn't remove FACE from a cache. Until we find a solution, we
1252 suppress this code, and simply use Fclear_face_cache even though
1253 that is not efficient. */
1254 BLOCK_INPUT;
1255 for (id = 0; id < ASIZE (Vfontset_table); id++)
1257 Lisp_Object this = AREF (Vfontset_table, id);
1259 if (EQ (FONTSET_BASE (this), base))
1261 Lisp_Object tail;
1263 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1264 tail = XCDR (tail))
1266 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1267 int face_id = XINT (XCDR (XCAR (tail)));
1268 struct face *face = FACE_FROM_ID (f, face_id);
1270 /* Face THIS itself is also freed by the following call. */
1271 free_realized_face (f, face);
1275 UNBLOCK_INPUT;
1276 #else /* not 0 */
1277 /* But, we don't have to call Fclear_face_cache if no fontset has
1278 been realized from BASE. */
1279 for (id = 0; id < ASIZE (Vfontset_table); id++)
1281 Lisp_Object this = AREF (Vfontset_table, id);
1283 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1285 Fclear_face_cache (Qt);
1286 break;
1289 #endif /* not 0 */
1293 /* Check validity of NAME as a fontset name and return the
1294 corresponding fontset. If not valid, signal an error.
1296 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1297 fontset of *FRAME.
1299 Set *FRAME to the actual frame. */
1301 static Lisp_Object
1302 check_fontset_name (name, frame)
1303 Lisp_Object name, *frame;
1305 int id;
1307 if (NILP (*frame))
1308 *frame = selected_frame;
1309 CHECK_LIVE_FRAME (*frame);
1311 if (EQ (name, Qt))
1312 return Vdefault_fontset;
1313 if (NILP (name))
1315 id = FRAME_FONTSET (XFRAME (*frame));
1317 else
1319 CHECK_STRING (name);
1320 /* First try NAME as literal. */
1321 id = fs_query_fontset (name, 2);
1322 if (id < 0)
1323 /* For backward compatibility, try again NAME as pattern. */
1324 id = fs_query_fontset (name, 0);
1325 if (id < 0)
1326 error ("Fontset `%s' does not exist", SDATA (name));
1328 return FONTSET_FROM_ID (id);
1331 static void
1332 accumulate_script_ranges (arg, range, val)
1333 Lisp_Object arg, range, val;
1335 if (EQ (XCAR (arg), val))
1337 if (CONSP (range))
1338 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1339 else
1340 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1345 /* Return an ASCII font name generated from fontset name NAME and
1346 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1348 static INLINE Lisp_Object
1349 generate_ascii_font_name (name, ascii_spec)
1350 Lisp_Object name, ascii_spec;
1352 Lisp_Object font_spec = Ffont_spec (0, NULL);
1353 int i;
1354 char xlfd[256];
1356 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
1357 error ("Not an XLFD font name: %s", SDATA (name));
1358 for (i = FONT_FOUNDRY_INDEX; i < FONT_EXTRA_INDEX; i++)
1359 if (! NILP (AREF (ascii_spec, i)))
1360 ASET (font_spec, i, AREF (ascii_spec, i));
1361 i = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1362 if (i < 0)
1363 error ("Not an XLFD font name: %s", SDATA (name));
1364 return make_unibyte_string (xlfd, i);
1367 /* Callback function for map_charset_chars in Fset_fontset_font.
1368 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1370 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1371 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1372 The consumed ranges are poped up from SCRIPT_RANGE_LIST, and the
1373 new SCRIPT_RANGE_LIST is stored in ARG.
1375 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1376 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1377 case. */
1379 static void
1380 set_fontset_font (arg, range)
1381 Lisp_Object arg, range;
1383 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1384 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1386 fontset = AREF (arg, 0);
1387 font_def = AREF (arg, 1);
1388 add = AREF (arg, 2);
1389 ascii = AREF (arg, 3);
1390 script_range_list = AREF (arg, 4);
1392 if (NILP (ascii) && from < 0x80)
1394 if (to < 0x80)
1395 return;
1396 from = 0x80;
1397 range = Fcons (make_number (0x80), XCDR (range));
1400 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1401 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1402 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1404 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1405 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1406 if (CONSP (script_range_list))
1408 if (SCRIPT_FROM < from)
1409 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1410 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1411 POP_SCRIPT_RANGE ();
1412 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1413 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1416 FONTSET_ADD (fontset, range, font_def, add);
1417 ASET (arg, 4, script_range_list);
1420 extern Lisp_Object QCfamily, QCregistry;
1421 static void update_auto_fontset_alist P_ ((Lisp_Object, Lisp_Object));
1424 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1425 doc: /*
1426 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1428 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1429 the default fontset.
1431 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1432 In that case, use FONT-SPEC for all characters in the range FROM and
1433 TO (inclusive).
1435 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1436 all characters that belong to the script.
1438 TARGET may be a charset. In that case, use FONT-SPEC for all
1439 characters in the charset.
1441 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1442 that no FONT-SPEC is specified.
1444 FONT-SPEC may one of these:
1445 * A font-spec object made by the function `font-spec' (which see).
1446 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1447 REGISTRY is a font registry name. FAMILY may contain foundry
1448 name, and REGISTRY may contain encoding name.
1449 * A font name string.
1450 * nil, which explicitly specifies that there's no font for TARGET.
1452 Optional 4th argument FRAME is a frame or nil for the selected frame
1453 that is concerned in the case that NAME is nil.
1455 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1456 to the font specifications for TARGET previously set. If it is
1457 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1458 appended. By default, FONT-SPEC overrides the previous settings. */)
1459 (name, target, font_spec, frame, add)
1460 Lisp_Object name, target, font_spec, frame, add;
1462 Lisp_Object fontset;
1463 Lisp_Object font_def, registry, family;
1464 Lisp_Object range_list;
1465 struct charset *charset = NULL;
1466 Lisp_Object fontname;
1467 int ascii_changed = 0;
1469 fontset = check_fontset_name (name, &frame);
1471 fontname = Qnil;
1472 if (CONSP (font_spec))
1474 Lisp_Object spec = Ffont_spec (0, NULL);
1476 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1477 font_spec = spec;
1478 fontname = Ffont_xlfd_name (font_spec, Qnil);
1480 else if (STRINGP (font_spec))
1482 Lisp_Object args[2];
1483 extern Lisp_Object QCname;
1485 fontname = font_spec;
1486 args[0] = QCname;
1487 args[1] = font_spec;
1488 font_spec = Ffont_spec (2, args);
1490 else if (FONT_SPEC_P (font_spec))
1491 fontname = Ffont_xlfd_name (font_spec, Qnil);
1492 else if (! NILP (font_spec))
1493 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1495 if (! NILP (font_spec))
1497 Lisp_Object encoding, repertory;
1499 family = AREF (font_spec, FONT_FAMILY_INDEX);
1500 if (! NILP (family) )
1501 family = SYMBOL_NAME (family);
1502 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1503 if (! NILP (registry))
1504 registry = Fdowncase (SYMBOL_NAME (registry));
1505 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1506 registry));
1507 if (NILP (encoding))
1508 encoding = Qascii;
1510 if (SYMBOLP (encoding))
1512 CHECK_CHARSET (encoding);
1513 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1515 else
1517 repertory = XCDR (encoding);
1518 encoding = XCAR (encoding);
1519 CHECK_CHARSET (encoding);
1520 encoding = CHARSET_SYMBOL_ID (encoding);
1521 if (! NILP (repertory) && SYMBOLP (repertory))
1523 CHECK_CHARSET (repertory);
1524 repertory = CHARSET_SYMBOL_ID (repertory);
1527 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1529 else
1530 font_def = Qnil;
1532 if (CHARACTERP (target))
1534 if (XFASTINT (target) < 0x80)
1535 error ("Can't set a font for partial ASCII range.");
1536 range_list = Fcons (Fcons (target, target), Qnil);
1538 else if (CONSP (target))
1540 Lisp_Object from, to;
1542 from = Fcar (target);
1543 to = Fcdr (target);
1544 CHECK_CHARACTER (from);
1545 CHECK_CHARACTER (to);
1546 if (XFASTINT (from) < 0x80)
1548 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1549 error ("Can't set a font for partial ASCII range.");
1550 ascii_changed = 1;
1552 range_list = Fcons (target, Qnil);
1554 else if (SYMBOLP (target) && !NILP (target))
1556 Lisp_Object script_list;
1557 Lisp_Object val;
1559 range_list = Qnil;
1560 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1561 if (! NILP (Fmemq (target, script_list)))
1563 if (EQ (target, Qlatin))
1564 ascii_changed = 1;
1565 val = Fcons (target, Qnil);
1566 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1567 val);
1568 range_list = Fnreverse (XCDR (val));
1570 if (CHARSETP (target))
1572 CHECK_CHARSET_GET_CHARSET (target, charset);
1573 if (charset->ascii_compatible_p)
1574 ascii_changed = 1;
1576 else if (NILP (range_list))
1577 error ("Invalid script or charset name: %s",
1578 SDATA (SYMBOL_NAME (target)));
1580 else if (NILP (target))
1581 range_list = Fcons (Qnil, Qnil);
1582 else
1583 error ("Invalid target for setting a font");
1585 if (ascii_changed)
1587 Lisp_Object val;
1589 if (NILP (font_spec))
1590 error ("Can't set ASCII font to nil");
1591 val = CHAR_TABLE_REF (fontset, 0);
1592 if (! NILP (val) && EQ (add, Qappend))
1593 /* We are going to change just an additional font for ASCII. */
1594 ascii_changed = 0;
1597 if (charset)
1599 Lisp_Object arg;
1601 arg = Fmake_vector (make_number (5), Qnil);
1602 ASET (arg, 0, fontset);
1603 ASET (arg, 1, font_def);
1604 ASET (arg, 2, add);
1605 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1606 ASET (arg, 4, range_list);
1608 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1609 CHARSET_MIN_CODE (charset),
1610 CHARSET_MAX_CODE (charset));
1611 range_list = AREF (arg, 4);
1613 for (; CONSP (range_list); range_list = XCDR (range_list))
1614 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1616 if (ascii_changed)
1618 Lisp_Object tail, frame, alist;
1619 int fontset_id = XINT (FONTSET_ID (fontset));
1621 FONTSET_ASCII (fontset) = fontname;
1622 name = FONTSET_NAME (fontset);
1623 FOR_EACH_FRAME (tail, frame)
1625 FRAME_PTR f = XFRAME (frame);
1626 Lisp_Object font_object;
1627 struct face *face;
1629 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1630 continue;
1631 if (fontset_id != FRAME_FONTSET (f))
1632 continue;
1633 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1634 if (face)
1635 font_object = font_load_for_lface (f, face->lface, font_spec);
1636 else
1637 font_object = font_open_by_spec (f, font_spec);
1638 if (! NILP (font_object))
1640 update_auto_fontset_alist (font_object, fontset);
1641 alist = Fcons (Fcons (Qfont, Fcons (name, font_object)), Qnil);
1642 Fmodify_frame_parameters (frame, alist);
1647 /* Free all realized fontsets whose base is FONTSET. This way, the
1648 specified character(s) are surely redisplayed by a correct
1649 font. */
1650 free_realized_fontsets (fontset);
1652 return Qnil;
1656 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1657 doc: /* Create a new fontset NAME from font information in FONTLIST.
1659 FONTLIST is an alist of scripts vs the corresponding font specification list.
1660 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1661 character of SCRIPT is displayed by a font that matches one of
1662 FONT-SPEC.
1664 SCRIPT is a symbol that appears in the first extra slot of the
1665 char-table `char-script-table'.
1667 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1668 `set-fontset-font' for the meaning. */)
1669 (name, fontlist)
1670 Lisp_Object name, fontlist;
1672 Lisp_Object fontset;
1673 int id;
1675 CHECK_STRING (name);
1676 CHECK_LIST (fontlist);
1678 name = Fdowncase (name);
1679 id = fs_query_fontset (name, 0);
1680 if (id < 0)
1682 Lisp_Object font_spec = Ffont_spec (0, NULL);
1683 Lisp_Object short_name;
1684 char xlfd[256];
1685 int len;
1687 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
1688 error ("Fontset name must be in XLFD format");
1689 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1690 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1691 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1692 error ("Registry field of fontset name must be \"fontset-*\"");
1693 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1694 Vfontset_alias_alist);
1695 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1696 fontset = make_fontset (Qnil, name, Qnil);
1697 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1698 if (len < 0)
1699 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1700 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
1702 else
1704 fontset = FONTSET_FROM_ID (id);
1705 free_realized_fontsets (fontset);
1706 Fset_char_table_range (fontset, Qt, Qnil);
1709 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1711 Lisp_Object elt, script;
1713 elt = Fcar (fontlist);
1714 script = Fcar (elt);
1715 elt = Fcdr (elt);
1716 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1717 for (; CONSP (elt); elt = XCDR (elt))
1718 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1719 else
1720 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1722 return name;
1726 /* Alist of automatically created fontsets. Each element is a cons
1727 (FONT-SPEC . FONTSET-ID). */
1728 static Lisp_Object auto_fontset_alist;
1730 /* Number of automatically created fontsets. */
1731 static int num_auto_fontsets;
1733 /* Retun a fontset synthesized from FONT-OBJECT. This is called from
1734 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1735 frame, and the returned fontset is used for the default fontset of
1736 that frame. The fontset specifies a font of the same registry as
1737 FONT-OBJECT for all characters in the repertory of the registry
1738 (see Vfont_encoding_alist). If the repertory is not known, the
1739 fontset specifies the font for all Latin characters assuming that a
1740 user intends to use FONT-OBJECT for Latin characters. */
1743 fontset_from_font (font_object)
1744 Lisp_Object font_object;
1746 Lisp_Object font_name = font_get_name (font_object);
1747 Lisp_Object font_spec = Fcopy_font_spec (font_object);
1748 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1749 Lisp_Object fontset_spec, alias, name, fontset;
1750 Lisp_Object val;
1751 int i;
1753 val = assoc_no_quit (font_spec, auto_fontset_alist);
1754 if (CONSP (val))
1755 return XINT (FONTSET_ID (XCDR (val)));
1756 if (num_auto_fontsets++ == 0)
1757 alias = intern ("fontset-startup");
1758 else
1760 char temp[32];
1762 sprintf (temp, "fontset-auto%d", num_auto_fontsets - 1);
1763 alias = intern (temp);
1765 fontset_spec = Fcopy_font_spec (font_spec);
1766 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1767 name = Ffont_xlfd_name (fontset_spec, Qnil);
1768 if (NILP (name))
1769 abort ();
1770 fontset = make_fontset (Qnil, name, Qnil);
1771 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1772 Vfontset_alias_alist);
1773 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1774 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1775 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1776 font_spec = Ffont_spec (0, NULL);
1777 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1779 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1781 if (CONSP (target))
1782 target = XCDR (target);
1783 if (! CHARSETP (target))
1784 target = Qlatin;
1785 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1786 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1789 FONTSET_ASCII (fontset) = font_name;
1791 #ifdef HAVE_NS
1792 nsfont_make_fontset_for_font(name, font_object);
1793 #endif
1795 return XINT (FONTSET_ID (fontset));
1799 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1800 FONTSET is changed, we delete an entry of FONTSET if any from
1801 auto_fontset_alist so that FONTSET is not re-used by
1802 fontset_from_font. */
1804 static void
1805 update_auto_fontset_alist (font_object, fontset)
1806 Lisp_Object font_object, fontset;
1808 Lisp_Object prev, tail;
1810 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1811 prev = tail, tail = XCDR (tail))
1812 if (EQ (fontset, XCDR (XCAR (tail))))
1814 if (NILP (prev))
1815 auto_fontset_alist = XCDR (tail);
1816 else
1817 XSETCDR (prev, XCDR (tail));
1818 break;
1823 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1824 FONT-OBJECT is the font for the character at POSITION in the current
1825 buffer. This is computed from all the text properties and overlays
1826 that apply to POSITION. POSTION may be nil, in which case,
1827 FONT-SPEC is the font for displaying the character CH with the
1828 default face.
1830 GLYPH-CODE is the glyph code in the font to use for the character.
1832 If the 2nd optional arg CH is non-nil, it is a character to check
1833 the font instead of the character at POSITION.
1835 It returns nil in the following cases:
1837 (1) The window system doesn't have a font for the character (thus
1838 it is displayed by an empty box).
1840 (2) The character code is invalid.
1842 (3) If POSITION is not nil, and the current buffer is not displayed
1843 in any window.
1845 In addition, the returned font name may not take into account of
1846 such redisplay engine hooks as what used in jit-lock-mode if
1847 POSITION is currently not visible. */
1850 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1851 doc: /* For internal use only. */)
1852 (position, ch)
1853 Lisp_Object position, ch;
1855 EMACS_INT pos, pos_byte, dummy;
1856 int face_id;
1857 int c;
1858 struct frame *f;
1859 struct face *face;
1860 int cs_id;
1862 if (NILP (position))
1864 CHECK_CHARACTER (ch);
1865 c = XINT (ch);
1866 f = XFRAME (selected_frame);
1867 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1868 pos = -1;
1869 cs_id = -1;
1871 else
1873 Lisp_Object window, charset;
1874 struct window *w;
1876 CHECK_NUMBER_COERCE_MARKER (position);
1877 pos = XINT (position);
1878 if (pos < BEGV || pos >= ZV)
1879 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1880 pos_byte = CHAR_TO_BYTE (pos);
1881 if (NILP (ch))
1882 c = FETCH_CHAR (pos_byte);
1883 else
1885 CHECK_NATNUM (ch);
1886 c = XINT (ch);
1888 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1889 if (NILP (window))
1890 return Qnil;
1891 w = XWINDOW (window);
1892 f = XFRAME (w->frame);
1893 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
1894 charset = Fget_char_property (position, Qcharset, Qnil);
1895 if (CHARSETP (charset))
1896 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
1897 else
1898 cs_id = -1;
1900 if (! CHAR_VALID_P (c, 0))
1901 return Qnil;
1902 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1903 face = FACE_FROM_ID (f, face_id);
1904 if (face->font)
1906 unsigned code = face->font->driver->encode_char (face->font, c);
1907 Lisp_Object font_object;
1908 /* Assignment to EMACS_INT stops GCC whining about limited range
1909 of data type. */
1910 EMACS_INT cod = code;
1912 if (code == FONT_INVALID_CODE)
1913 return Qnil;
1914 XSETFONT (font_object, face->font);
1915 if (cod <= MOST_POSITIVE_FIXNUM)
1916 return Fcons (font_object, make_number (code));
1917 return Fcons (font_object, Fcons (make_number (code >> 16),
1918 make_number (code & 0xFFFF)));
1920 return Qnil;
1924 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1925 doc: /* Return information about a fontset FONTSET on frame FRAME.
1927 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1928 for the default fontset. FRAME nil means the selected frame.
1930 The value is a char-table whose elements have this form:
1932 ((FONT OPENED-FONT ...) ...)
1934 FONT is a name of font specified for a range of characters.
1936 OPENED-FONT is a name of a font actually opened.
1938 The char-table has one extra slot. If FONTSET is not the default
1939 fontset, the value the extra slot is a char-table containing the
1940 information about the derived fonts from the default fontset. The
1941 format is the same as above. */)
1942 (fontset, frame)
1943 Lisp_Object fontset, frame;
1945 FRAME_PTR f;
1946 Lisp_Object *realized[2], fontsets[2], tables[2];
1947 Lisp_Object val, elt;
1948 int c, i, j, k;
1950 (*check_window_system_func) ();
1952 fontset = check_fontset_name (fontset, &frame);
1953 f = XFRAME (frame);
1955 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1956 in the table `realized'. */
1957 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1958 * ASIZE (Vfontset_table));
1959 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1961 elt = FONTSET_FROM_ID (i);
1962 if (!NILP (elt)
1963 && EQ (FONTSET_BASE (elt), fontset)
1964 && EQ (FONTSET_FRAME (elt), frame))
1965 realized[0][j++] = elt;
1967 realized[0][j] = Qnil;
1969 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1970 * ASIZE (Vfontset_table));
1971 for (i = j = 0; ! NILP (realized[0][i]); i++)
1973 elt = FONTSET_DEFAULT (realized[0][i]);
1974 if (! NILP (elt))
1975 realized[1][j++] = elt;
1977 realized[1][j] = Qnil;
1979 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1980 fontsets[0] = fontset;
1981 if (!EQ (fontset, Vdefault_fontset))
1983 tables[1] = Fmake_char_table (Qnil, Qnil);
1984 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
1985 fontsets[1] = Vdefault_fontset;
1988 /* Accumulate information of the fontset in TABLE. The format of
1989 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1990 for (k = 0; k <= 1; k++)
1992 for (c = 0; c <= MAX_CHAR; )
1994 int from = c, to = MAX_5_BYTE_CHAR;
1996 if (c <= MAX_5_BYTE_CHAR)
1998 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
2000 else
2002 val = FONTSET_FALLBACK (fontsets[k]);
2003 to = MAX_CHAR;
2005 if (VECTORP (val))
2007 Lisp_Object alist;
2009 /* At first, set ALIST to ((FONT-SPEC) ...). */
2010 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
2011 if (! NILP (AREF (val, i)))
2012 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
2013 alist);
2014 alist = Fnreverse (alist);
2016 /* Then store opened font names to cdr of each elements. */
2017 for (i = 0; ! NILP (realized[k][i]); i++)
2019 if (c <= MAX_5_BYTE_CHAR)
2020 val = FONTSET_REF (realized[k][i], c);
2021 else
2022 val = FONTSET_FALLBACK (realized[k][i]);
2023 if (! CONSP (val) || ! VECTORP (XCDR (val)))
2024 continue;
2025 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
2026 val = XCDR (val);
2027 for (j = 0; j < ASIZE (val); j++)
2029 elt = AREF (val, j);
2030 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
2032 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
2033 Lisp_Object slot, name;
2035 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
2036 name = AREF (font_object, FONT_NAME_INDEX);
2037 if (NILP (Fmember (name, XCDR (slot))))
2038 nconc2 (slot, Fcons (name, Qnil));
2043 /* Store ALIST in TBL for characters C..TO. */
2044 if (c <= MAX_5_BYTE_CHAR)
2045 char_table_set_range (tables[k], c, to, alist);
2046 else
2047 XCHAR_TABLE (tables[k])->defalt = alist;
2049 /* At last, change each elements to font names. */
2050 for (; CONSP (alist); alist = XCDR (alist))
2052 elt = XCAR (alist);
2053 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
2056 c = to + 1;
2058 if (EQ (fontset, Vdefault_fontset))
2059 break;
2062 return tables[0];
2066 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2067 doc: /* Return a font name pattern for character CH in fontset NAME.
2068 If NAME is t, find a pattern in the default fontset.
2070 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2071 family name and REGISTRY is a font registry name. This is actually
2072 the first font name pattern for CH in the fontset or in the default
2073 fontset.
2075 If the 2nd optional arg ALL is non-nil, return a list of all font name
2076 patterns. */)
2077 (name, ch, all)
2078 Lisp_Object name, ch, all;
2080 int c;
2081 Lisp_Object fontset, elt, list, repertory, val;
2082 int i, j;
2083 Lisp_Object frame;
2085 frame = Qnil;
2086 fontset = check_fontset_name (name, &frame);
2088 CHECK_CHARACTER (ch);
2089 c = XINT (ch);
2090 list = Qnil;
2091 while (1)
2093 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2094 i++, elt = FONTSET_FALLBACK (fontset))
2095 if (VECTORP (elt))
2096 for (j = 0; j < ASIZE (elt); j++)
2098 Lisp_Object family, registry;
2100 val = AREF (elt, j);
2101 repertory = AREF (val, 1);
2102 if (INTEGERP (repertory))
2104 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2106 if (! CHAR_CHARSET_P (c, charset))
2107 continue;
2109 else if (CHAR_TABLE_P (repertory))
2111 if (NILP (CHAR_TABLE_REF (repertory, c)))
2112 continue;
2114 val = AREF (val, 0);
2115 /* VAL is a FONT-SPEC */
2116 family = AREF (val, FONT_FAMILY_INDEX);
2117 if (! NILP (family))
2118 family = SYMBOL_NAME (family);
2119 registry = AREF (val, FONT_REGISTRY_INDEX);
2120 if (! NILP (registry))
2121 registry = SYMBOL_NAME (registry);
2122 val = Fcons (family, registry);
2123 if (NILP (all))
2124 return val;
2125 list = Fcons (val, list);
2127 if (EQ (fontset, Vdefault_fontset))
2128 break;
2129 fontset = Vdefault_fontset;
2131 return (Fnreverse (list));
2134 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2135 doc: /* Return a list of all defined fontset names. */)
2138 Lisp_Object fontset, list;
2139 int i;
2141 list = Qnil;
2142 for (i = 0; i < ASIZE (Vfontset_table); i++)
2144 fontset = FONTSET_FROM_ID (i);
2145 if (!NILP (fontset)
2146 && BASE_FONTSET_P (fontset))
2147 list = Fcons (FONTSET_NAME (fontset), list);
2150 return list;
2154 #ifdef FONTSET_DEBUG
2156 Lisp_Object
2157 dump_fontset (fontset)
2158 Lisp_Object fontset;
2160 Lisp_Object vec;
2162 vec = Fmake_vector (make_number (3), Qnil);
2163 ASET (vec, 0, FONTSET_ID (fontset));
2165 if (BASE_FONTSET_P (fontset))
2167 ASET (vec, 1, FONTSET_NAME (fontset));
2169 else
2171 Lisp_Object frame;
2173 frame = FONTSET_FRAME (fontset);
2174 if (FRAMEP (frame))
2176 FRAME_PTR f = XFRAME (frame);
2178 if (FRAME_LIVE_P (f))
2179 ASET (vec, 1,
2180 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2181 else
2182 ASET (vec, 1,
2183 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2185 if (!NILP (FONTSET_DEFAULT (fontset)))
2186 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2188 return vec;
2191 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2192 doc: /* Return a brief summary of all fontsets for debug use. */)
2195 Lisp_Object val;
2196 int i;
2198 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2199 if (! NILP (AREF (Vfontset_table, i)))
2200 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2201 return (Fnreverse (val));
2203 #endif /* FONTSET_DEBUG */
2205 void
2206 syms_of_fontset ()
2208 DEFSYM (Qfontset, "fontset");
2209 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2210 DEFSYM (Qfontset_info, "fontset-info");
2211 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2213 DEFSYM (Qprepend, "prepend");
2214 DEFSYM (Qappend, "append");
2215 DEFSYM (Qlatin, "latin");
2217 Vcached_fontset_data = Qnil;
2218 staticpro (&Vcached_fontset_data);
2220 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2221 staticpro (&Vfontset_table);
2223 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2224 staticpro (&Vdefault_fontset);
2225 FONTSET_ID (Vdefault_fontset) = make_number (0);
2226 FONTSET_NAME (Vdefault_fontset)
2227 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2228 ASET (Vfontset_table, 0, Vdefault_fontset);
2229 next_fontset_id = 1;
2231 auto_fontset_alist = Qnil;
2232 staticpro (&auto_fontset_alist);
2234 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2235 doc: /*
2236 Alist of charsets vs the charsets to determine the preferred font encoding.
2237 Each element looks like (CHARSET . ENCODING-CHARSET),
2238 where ENCODING-CHARSET is a charset registered in the variable
2239 `font-encoding-alist' as ENCODING.
2241 When a text has a property `charset' and the value is CHARSET, a font
2242 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2243 Vfont_encoding_charset_alist = Qnil;
2245 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
2246 doc: /*
2247 Char table of characters whose ascent values should be ignored.
2248 If an entry for a character is non-nil, the ascent value of the glyph
2249 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2251 This affects how a composite character which contains
2252 such a character is displayed on screen. */);
2253 Vuse_default_ascent = Qnil;
2255 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
2256 doc: /*
2257 Char table of characters which are not composed relatively.
2258 If an entry for a character is non-nil, a composition sequence
2259 which contains that character is displayed so that
2260 the glyph of that character is put without considering
2261 an ascent and descent value of a previous character. */);
2262 Vignore_relative_composition = Qnil;
2264 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
2265 doc: /* Alist of fontname vs list of the alternate fontnames.
2266 When a specified font name is not found, the corresponding
2267 alternate fontnames (if any) are tried instead. */);
2268 Valternate_fontname_alist = Qnil;
2270 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
2271 doc: /* Alist of fontset names vs the aliases. */);
2272 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2273 build_string ("fontset-default")),
2274 Qnil);
2276 DEFVAR_LISP ("vertical-centering-font-regexp",
2277 &Vvertical_centering_font_regexp,
2278 doc: /* *Regexp matching font names that require vertical centering on display.
2279 When a character is displayed with such fonts, the character is displayed
2280 at the vertical center of lines. */);
2281 Vvertical_centering_font_regexp = Qnil;
2283 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2284 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2285 Votf_script_alist = Qnil;
2287 defsubr (&Squery_fontset);
2288 defsubr (&Snew_fontset);
2289 defsubr (&Sset_fontset_font);
2290 defsubr (&Sinternal_char_font);
2291 defsubr (&Sfontset_info);
2292 defsubr (&Sfontset_font);
2293 defsubr (&Sfontset_list);
2294 #ifdef FONTSET_DEBUG
2295 defsubr (&Sfontset_list_all);
2296 #endif
2299 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2300 (do not change this comment) */