(erc-button-add-button): Only call `widget-convert-button' in XEmacs.
[emacs.git] / src / fontset.c
blobf8eb0c86eb89c5fda2c069d40bdf4de9c5e3ae31
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 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 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
463 character C in FONTSET. If C is -1, return a fallback font-group.
464 If C is not -1, the value may be Qt (FONTSET doesn't have a font
465 for C even in the fallback group, or 0 (a font for C may be found
466 only in the fallback group). */
468 static Lisp_Object
469 fontset_get_font_group (Lisp_Object fontset, int c)
471 Lisp_Object font_group;
472 Lisp_Object base_fontset;
473 int from = 0, to = MAX_CHAR, i;
475 xassert (! BASE_FONTSET_P (fontset));
476 if (c >= 0)
477 font_group = CHAR_TABLE_REF (fontset, c);
478 else
479 font_group = FONTSET_FALLBACK (fontset);
480 if (! NILP (font_group))
481 return font_group;
482 base_fontset = FONTSET_BASE (fontset);
483 if (c >= 0)
484 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
485 else
486 font_group = FONTSET_FALLBACK (base_fontset);
487 if (NILP (font_group))
489 font_group = make_number (0);
490 if (c >= 0)
491 char_table_set_range (fontset, from, to, font_group);
492 return font_group;
494 font_group = Fcopy_sequence (font_group);
495 for (i = 0; i < ASIZE (font_group); i++)
496 if (! NILP (AREF (font_group, i)))
498 Lisp_Object rfont_def;
500 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
501 /* Remember the original order. */
502 RFONT_DEF_SET_SCORE (rfont_def, i);
503 ASET (font_group, i, rfont_def);
505 font_group = Fcons (make_number (-1), font_group);
506 if (c >= 0)
507 char_table_set_range (fontset, from, to, font_group);
508 else
509 FONTSET_FALLBACK (fontset) = font_group;
510 return font_group;
513 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
514 character C. If no font is found, return Qnil if there's a
515 possibility that the default fontset or the fallback font groups
516 have a proper font, and return Qt if not.
518 If a font is found but is not yet opened, open it (if FACE is not
519 NULL) or return Qnil (if FACE is NULL).
521 ID is a charset-id that must be preferred, or -1 meaning no
522 preference.
524 If FALLBACK is nonzero, search only fallback fonts. */
526 static Lisp_Object
527 fontset_find_font (fontset, c, face, id, fallback)
528 Lisp_Object fontset;
529 int c;
530 struct face *face;
531 int id, fallback;
533 Lisp_Object vec, font_group;
534 int i, charset_matched = 0, found_index;
535 FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset)))
536 ? XFRAME (selected_frame) : XFRAME (FONTSET_FRAME (fontset));
537 Lisp_Object rfont_def;
539 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
540 if (! CONSP (font_group))
541 return font_group;
542 vec = XCDR (font_group);
543 if (ASIZE (vec) == 0)
544 return Qnil;
546 if (ASIZE (vec) > 1)
548 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
549 /* We have just created the font-group,
550 or the charset priorities were changed. */
551 reorder_font_vector (font_group, face->ascii_face->font);
552 if (id >= 0)
553 /* Find a spec matching with the charset ID to try at
554 first. */
555 for (i = 0; i < ASIZE (vec); i++)
557 Lisp_Object repertory;
559 rfont_def = AREF (vec, i);
560 if (NILP (rfont_def))
561 break;
562 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
564 if (XINT (repertory) == id)
566 charset_matched = i;
567 break;
572 /* Find the first available font in the vector of RFONT-DEF. */
573 for (i = 0; i < ASIZE (vec); i++)
575 Lisp_Object font_def;
576 Lisp_Object font_entity, font_object;
578 found_index = i;
579 if (i == 0)
581 if (charset_matched > 0)
583 /* Try the element matching with the charset ID at first. */
584 found_index = charset_matched;
585 /* Make this negative so that we don't come here in the
586 next loop. */
587 charset_matched = - charset_matched;
588 /* We must try the first element in the next loop. */
589 i--;
592 else if (i == - charset_matched)
594 /* We have already tried this element and the followings
595 that have the same font specifications in the first
596 iteration. So, skip them all. */
597 rfont_def = AREF (vec, i);
598 font_def = RFONT_DEF_FONT_DEF (rfont_def);
599 for (; i + 1 < ASIZE (vec); i++)
601 rfont_def = AREF (vec, i + 1);
602 if (NILP (rfont_def))
603 break;
604 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
605 break;
607 continue;
610 rfont_def = AREF (vec, found_index);
611 if (NILP (rfont_def))
613 if (i < 0)
614 continue;
615 /* This is a sign of not to try the other fonts. */
616 return Qt;
618 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
619 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
620 /* We couldn't open this font last time. */
621 continue;
623 font_object = RFONT_DEF_OBJECT (rfont_def);
624 if (NILP (font_object))
626 font_def = RFONT_DEF_FONT_DEF (rfont_def);
628 if (! face)
629 /* We have not yet opened the font. */
630 return Qnil;
631 /* Find a font best-matching with the spec without checking
632 the support of the character C. That checking is costly,
633 and even without the checking, the found font supports C
634 in high possibility. */
635 font_entity = font_find_for_lface (f, face->lface,
636 FONT_DEF_SPEC (font_def), -1);
637 if (NILP (font_entity))
639 /* Record that no font matches the spec. */
640 RFONT_DEF_SET_FACE (rfont_def, -1);
641 continue;
643 font_object = font_open_for_lface (f, font_entity, face->lface,
644 FONT_DEF_SPEC (font_def));
645 if (NILP (font_object))
647 /* Something strange happened, perhaps because of a
648 Font-backend problem. Too avoid crashing, record
649 that this spec is unsable. It may be better to find
650 another font of the same spec, but currently we don't
651 have such an API. */
652 RFONT_DEF_SET_FACE (rfont_def, -1);
653 continue;
655 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
658 if (font_has_char (f, font_object, c))
659 goto found;
661 /* Find a font already opened, maching with the current spec,
662 and supporting C. */
663 font_def = RFONT_DEF_FONT_DEF (rfont_def);
664 for (; found_index + 1 < ASIZE (vec); found_index++)
666 rfont_def = AREF (vec, found_index + 1);
667 if (NILP (rfont_def))
668 break;
669 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
670 break;
671 font_object = RFONT_DEF_OBJECT (rfont_def);
672 if (! NILP (font_object) && font_has_char (f, font_object, c))
674 found_index++;
675 goto found;
679 /* Find a font-entity with the current spec and supporting C. */
680 font_entity = font_find_for_lface (f, face->lface,
681 FONT_DEF_SPEC (font_def), c);
682 if (! NILP (font_entity))
684 /* We found a font. Open it and insert a new element for
685 that font in VEC. */
686 Lisp_Object new_vec;
687 int j;
689 font_object = font_open_for_lface (f, font_entity, face->lface,
690 Qnil);
691 if (NILP (font_object))
692 continue;
693 RFONT_DEF_NEW (rfont_def, font_def);
694 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
695 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
696 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
697 found_index++;
698 for (j = 0; j < found_index; j++)
699 ASET (new_vec, j, AREF (vec, j));
700 ASET (new_vec, j, rfont_def);
701 for (j++; j < ASIZE (new_vec); j++)
702 ASET (new_vec, j, AREF (vec, j - 1));
703 XSETCDR (font_group, new_vec);
704 vec = new_vec;
705 goto found;
707 if (i >= 0)
708 i = found_index;
711 FONTSET_SET (fontset, make_number (c), make_number (0));
712 return Qnil;
714 found:
715 if (fallback && found_index > 0)
717 /* The order of fonts in the fallback font-group is not that
718 important, and it is better to move the found font to the
719 first of the group so that the next try will find it
720 quickly. */
721 for (i = found_index; i > 0; i--)
722 ASET (vec, i, AREF (vec, i - 1));
723 ASET (vec, 0, rfont_def);
725 return rfont_def;
729 static Lisp_Object
730 fontset_font (fontset, c, face, id)
731 Lisp_Object fontset;
732 int c;
733 struct face *face;
734 int id;
736 Lisp_Object rfont_def, default_rfont_def;
737 Lisp_Object base_fontset;
739 /* Try a font-group of FONTSET. */
740 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
741 rfont_def = fontset_find_font (fontset, c, face, id, 0);
742 if (VECTORP (rfont_def))
743 return rfont_def;
744 if (NILP (rfont_def))
745 FONTSET_SET (fontset, make_number (c), make_number (0));
747 /* Try a font-group of the default fontset. */
748 base_fontset = FONTSET_BASE (fontset);
749 if (! EQ (base_fontset, Vdefault_fontset))
751 if (NILP (FONTSET_DEFAULT (fontset)))
752 FONTSET_DEFAULT (fontset)
753 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
754 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
755 default_rfont_def
756 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
757 if (VECTORP (default_rfont_def))
758 return default_rfont_def;
759 if (NILP (default_rfont_def))
760 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
761 make_number (0));
764 /* Try a fallback font-group of FONTSET. */
765 if (! EQ (rfont_def, Qt))
767 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
768 rfont_def = fontset_find_font (fontset, c, face, id, 1);
769 if (VECTORP (rfont_def))
770 return rfont_def;
771 /* Remember that FONTSET has no font for C. */
772 FONTSET_SET (fontset, make_number (c), Qt);
775 /* Try a fallback font-group of the default fontset. */
776 if (! EQ (base_fontset, Vdefault_fontset)
777 && ! EQ (default_rfont_def, Qt))
779 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
780 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
781 if (VECTORP (rfont_def))
782 return rfont_def;
783 /* Remember that the default fontset has no font for C. */
784 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
787 return Qnil;
790 /* Return a newly created fontset with NAME. If BASE is nil, make a
791 base fontset. Otherwise make a realized fontset whose base is
792 BASE. */
794 static Lisp_Object
795 make_fontset (frame, name, base)
796 Lisp_Object frame, name, base;
798 Lisp_Object fontset;
799 int size = ASIZE (Vfontset_table);
800 int id = next_fontset_id;
802 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
803 the next available fontset ID. So it is expected that this loop
804 terminates quickly. In addition, as the last element of
805 Vfontset_table is always nil, we don't have to check the range of
806 id. */
807 while (!NILP (AREF (Vfontset_table, id))) id++;
809 if (id + 1 == size)
810 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
812 fontset = Fmake_char_table (Qfontset, Qnil);
814 FONTSET_ID (fontset) = make_number (id);
815 if (NILP (base))
817 FONTSET_NAME (fontset) = name;
819 else
821 FONTSET_NAME (fontset) = Qnil;
822 FONTSET_FRAME (fontset) = frame;
823 FONTSET_BASE (fontset) = base;
826 ASET (Vfontset_table, id, fontset);
827 next_fontset_id = id + 1;
828 return fontset;
832 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
834 /* Return the name of the fontset who has ID. */
836 Lisp_Object
837 fontset_name (id)
838 int id;
840 Lisp_Object fontset;
842 fontset = FONTSET_FROM_ID (id);
843 return FONTSET_NAME (fontset);
847 /* Return the ASCII font name of the fontset who has ID. */
849 Lisp_Object
850 fontset_ascii (id)
851 int id;
853 Lisp_Object fontset, elt;
855 fontset= FONTSET_FROM_ID (id);
856 elt = FONTSET_ASCII (fontset);
857 if (CONSP (elt))
858 elt = XCAR (elt);
859 return elt;
862 void
863 free_realized_fontset (f, fontset)
864 FRAME_PTR f;
865 Lisp_Object fontset;
867 Lisp_Object tail;
869 return;
870 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
872 xassert (FONT_OBJECT_P (XCAR (tail)));
873 font_close_object (f, XCAR (tail));
877 /* Free fontset of FACE defined on frame F. Called from
878 free_realized_face. */
880 void
881 free_face_fontset (f, face)
882 FRAME_PTR f;
883 struct face *face;
885 Lisp_Object fontset;
887 fontset = FONTSET_FROM_ID (face->fontset);
888 if (NILP (fontset))
889 return;
890 xassert (! BASE_FONTSET_P (fontset));
891 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
892 free_realized_fontset (f, fontset);
893 ASET (Vfontset_table, face->fontset, Qnil);
894 if (face->fontset < next_fontset_id)
895 next_fontset_id = face->fontset;
896 if (! NILP (FONTSET_DEFAULT (fontset)))
898 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
900 fontset = AREF (Vfontset_table, id);
901 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
902 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
903 free_realized_fontset (f, fontset);
904 ASET (Vfontset_table, id, Qnil);
905 if (id < next_fontset_id)
906 next_fontset_id = face->fontset;
908 face->fontset = -1;
912 /* Return 1 if FACE is suitable for displaying character C.
913 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
914 when C is not an ASCII character. */
917 face_suitable_for_char_p (face, c)
918 struct face *face;
919 int c;
921 Lisp_Object fontset, rfont_def;
923 fontset = FONTSET_FROM_ID (face->fontset);
924 rfont_def = fontset_font (fontset, c, NULL, -1);
925 return (VECTORP (rfont_def)
926 && INTEGERP (RFONT_DEF_FACE (rfont_def))
927 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
931 /* Return ID of face suitable for displaying character C on frame F.
932 FACE must be reazlied for ASCII characters in advance. Called from
933 the macro FACE_FOR_CHAR. */
936 face_for_char (f, face, c, pos, object)
937 FRAME_PTR f;
938 struct face *face;
939 int c, pos;
940 Lisp_Object object;
942 Lisp_Object fontset, rfont_def, charset;
943 int face_id;
944 int id;
946 /* If face->fontset is negative (that happens when no font is found
947 for face), just return face->ascii_face because we can't do
948 anything. Perhaps, we should fix the callers to assure
949 that face->fontset is always valid. */
950 if (ASCII_CHAR_P (c) || face->fontset < 0)
951 return face->ascii_face->id;
953 xassert (fontset_id_valid_p (face->fontset));
954 fontset = FONTSET_FROM_ID (face->fontset);
955 xassert (!BASE_FONTSET_P (fontset));
957 if (pos < 0)
959 id = -1;
960 charset = Qnil;
962 else
964 charset = Fget_char_property (make_number (pos), Qcharset, object);
965 if (CHARSETP (charset))
967 Lisp_Object val;
969 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
970 if (CONSP (val) && CHARSETP (XCDR (val)))
971 charset = XCDR (val);
972 id = XINT (CHARSET_SYMBOL_ID (charset));
974 else
975 id = -1;
978 rfont_def = fontset_font (fontset, c, face, id);
979 if (VECTORP (rfont_def))
981 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
982 face_id = XINT (RFONT_DEF_FACE (rfont_def));
983 else
985 Lisp_Object font_object;
987 font_object = RFONT_DEF_OBJECT (rfont_def);
988 face_id = face_for_font (f, font_object, face);
989 RFONT_DEF_SET_FACE (rfont_def, face_id);
992 else
994 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
995 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
996 else
998 face_id = face_for_font (f, Qnil, face);
999 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
1002 xassert (face_id >= 0);
1003 return face_id;
1007 Lisp_Object
1008 font_for_char (face, c, pos, object)
1009 struct face *face;
1010 int c, pos;
1011 Lisp_Object object;
1013 Lisp_Object fontset, rfont_def, charset;
1014 int id;
1016 if (ASCII_CHAR_P (c))
1018 Lisp_Object font_object;
1020 XSETFONT (font_object, face->ascii_face->font);
1021 return font_object;
1024 xassert (fontset_id_valid_p (face->fontset));
1025 fontset = FONTSET_FROM_ID (face->fontset);
1026 xassert (!BASE_FONTSET_P (fontset));
1027 if (pos < 0)
1029 id = -1;
1030 charset = Qnil;
1032 else
1034 charset = Fget_char_property (make_number (pos), Qcharset, object);
1035 if (CHARSETP (charset))
1037 Lisp_Object val;
1039 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
1040 if (CONSP (val) && CHARSETP (XCDR (val)))
1041 charset = XCDR (val);
1042 id = XINT (CHARSET_SYMBOL_ID (charset));
1044 else
1045 id = -1;
1048 rfont_def = fontset_font (fontset, c, face, id);
1049 return (VECTORP (rfont_def)
1050 ? RFONT_DEF_OBJECT (rfont_def)
1051 : Qnil);
1055 /* Make a realized fontset for ASCII face FACE on frame F from the
1056 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1057 default fontset as the base. Value is the id of the new fontset.
1058 Called from realize_x_face. */
1061 make_fontset_for_ascii_face (f, base_fontset_id, face)
1062 FRAME_PTR f;
1063 int base_fontset_id;
1064 struct face *face;
1066 Lisp_Object base_fontset, fontset, frame;
1068 XSETFRAME (frame, f);
1069 if (base_fontset_id >= 0)
1071 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1072 if (!BASE_FONTSET_P (base_fontset))
1073 base_fontset = FONTSET_BASE (base_fontset);
1074 if (! BASE_FONTSET_P (base_fontset))
1075 abort ();
1077 else
1078 base_fontset = Vdefault_fontset;
1080 fontset = make_fontset (frame, Qnil, base_fontset);
1081 return XINT (FONTSET_ID (fontset));
1086 /* Cache data used by fontset_pattern_regexp. The car part is a
1087 pattern string containing at least one wild card, the cdr part is
1088 the corresponding regular expression. */
1089 static Lisp_Object Vcached_fontset_data;
1091 #define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
1092 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1094 /* If fontset name PATTERN contains any wild card, return regular
1095 expression corresponding to PATTERN. */
1097 static Lisp_Object
1098 fontset_pattern_regexp (pattern)
1099 Lisp_Object pattern;
1101 if (!index ((char *) SDATA (pattern), '*')
1102 && !index ((char *) SDATA (pattern), '?'))
1103 /* PATTERN does not contain any wild cards. */
1104 return Qnil;
1106 if (!CONSP (Vcached_fontset_data)
1107 || strcmp ((char *) SDATA (pattern), CACHED_FONTSET_NAME))
1109 /* We must at first update the cached data. */
1110 unsigned char *regex, *p0, *p1;
1111 int ndashes = 0, nstars = 0, nescs = 0;
1113 for (p0 = SDATA (pattern); *p0; p0++)
1115 if (*p0 == '-')
1116 ndashes++;
1117 else if (*p0 == '*')
1118 nstars++;
1119 else if (*p0 == '['
1120 || *p0 == '.' || *p0 == '\\'
1121 || *p0 == '+' || *p0 == '^'
1122 || *p0 == '$')
1123 nescs++;
1126 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1127 we convert "*" to "[^-]*" which is much faster in regular
1128 expression matching. */
1129 if (ndashes < 14)
1130 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
1131 else
1132 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
1134 *p1++ = '^';
1135 for (p0 = SDATA (pattern); *p0; p0++)
1137 if (*p0 == '*')
1139 if (ndashes < 14)
1140 *p1++ = '.';
1141 else
1142 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1143 *p1++ = '*';
1145 else if (*p0 == '?')
1146 *p1++ = '.';
1147 else if (*p0 == '['
1148 || *p0 == '.' || *p0 == '\\'
1149 || *p0 == '+' || *p0 == '^'
1150 || *p0 == '$')
1151 *p1++ = '\\', *p1++ = *p0;
1152 else
1153 *p1++ = *p0;
1155 *p1++ = '$';
1156 *p1++ = 0;
1158 Vcached_fontset_data = Fcons (build_string ((char *) SDATA (pattern)),
1159 build_string ((char *) regex));
1162 return CACHED_FONTSET_REGEX;
1165 /* Return ID of the base fontset named NAME. If there's no such
1166 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1167 0: pattern containing '*' and '?' as wildcards
1168 1: regular expression
1169 2: literal fontset name
1173 fs_query_fontset (name, name_pattern)
1174 Lisp_Object name;
1175 int name_pattern;
1177 Lisp_Object tem;
1178 int i;
1180 name = Fdowncase (name);
1181 if (name_pattern != 1)
1183 tem = Frassoc (name, Vfontset_alias_alist);
1184 if (NILP (tem))
1185 tem = Fassoc (name, Vfontset_alias_alist);
1186 if (CONSP (tem) && STRINGP (XCAR (tem)))
1187 name = XCAR (tem);
1188 else if (name_pattern == 0)
1190 tem = fontset_pattern_regexp (name);
1191 if (STRINGP (tem))
1193 name = tem;
1194 name_pattern = 1;
1199 for (i = 0; i < ASIZE (Vfontset_table); i++)
1201 Lisp_Object fontset, this_name;
1203 fontset = FONTSET_FROM_ID (i);
1204 if (NILP (fontset)
1205 || !BASE_FONTSET_P (fontset))
1206 continue;
1208 this_name = FONTSET_NAME (fontset);
1209 if (name_pattern == 1
1210 ? fast_string_match_ignore_case (name, this_name) >= 0
1211 : !xstrcasecmp (SDATA (name), SDATA (this_name)))
1212 return i;
1214 return -1;
1218 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1219 doc: /* Return the name of a fontset that matches PATTERN.
1220 The value is nil if there is no matching fontset.
1221 PATTERN can contain `*' or `?' as a wildcard
1222 just as X font name matching algorithm allows.
1223 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1224 (pattern, regexpp)
1225 Lisp_Object pattern, regexpp;
1227 Lisp_Object fontset;
1228 int id;
1230 (*check_window_system_func) ();
1232 CHECK_STRING (pattern);
1234 if (SCHARS (pattern) == 0)
1235 return Qnil;
1237 id = fs_query_fontset (pattern, !NILP (regexpp));
1238 if (id < 0)
1239 return Qnil;
1241 fontset = FONTSET_FROM_ID (id);
1242 return FONTSET_NAME (fontset);
1245 /* Return a list of base fontset names matching PATTERN on frame F. */
1247 Lisp_Object
1248 list_fontsets (f, pattern, size)
1249 FRAME_PTR f;
1250 Lisp_Object pattern;
1251 int size;
1253 Lisp_Object frame, regexp, val;
1254 int id;
1256 XSETFRAME (frame, f);
1258 regexp = fontset_pattern_regexp (pattern);
1259 val = Qnil;
1261 for (id = 0; id < ASIZE (Vfontset_table); id++)
1263 Lisp_Object fontset, name;
1265 fontset = FONTSET_FROM_ID (id);
1266 if (NILP (fontset)
1267 || !BASE_FONTSET_P (fontset)
1268 || !EQ (frame, FONTSET_FRAME (fontset)))
1269 continue;
1270 name = FONTSET_NAME (fontset);
1272 if (STRINGP (regexp)
1273 ? (fast_string_match (regexp, name) < 0)
1274 : strcmp ((char *) SDATA (pattern), (char *) SDATA (name)))
1275 continue;
1277 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1280 return val;
1284 /* Free all realized fontsets whose base fontset is BASE. */
1286 static void
1287 free_realized_fontsets (base)
1288 Lisp_Object base;
1290 int id;
1292 #if 0
1293 /* For the moment, this doesn't work because free_realized_face
1294 doesn't remove FACE from a cache. Until we find a solution, we
1295 suppress this code, and simply use Fclear_face_cache even though
1296 that is not efficient. */
1297 BLOCK_INPUT;
1298 for (id = 0; id < ASIZE (Vfontset_table); id++)
1300 Lisp_Object this = AREF (Vfontset_table, id);
1302 if (EQ (FONTSET_BASE (this), base))
1304 Lisp_Object tail;
1306 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1307 tail = XCDR (tail))
1309 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1310 int face_id = XINT (XCDR (XCAR (tail)));
1311 struct face *face = FACE_FROM_ID (f, face_id);
1313 /* Face THIS itself is also freed by the following call. */
1314 free_realized_face (f, face);
1318 UNBLOCK_INPUT;
1319 #else /* not 0 */
1320 /* But, we don't have to call Fclear_face_cache if no fontset has
1321 been realized from BASE. */
1322 for (id = 0; id < ASIZE (Vfontset_table); id++)
1324 Lisp_Object this = AREF (Vfontset_table, id);
1326 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1328 Fclear_face_cache (Qt);
1329 break;
1332 #endif /* not 0 */
1336 /* Check validity of NAME as a fontset name and return the
1337 corresponding fontset. If not valid, signal an error.
1339 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1340 fontset of *FRAME.
1342 Set *FRAME to the actual frame. */
1344 static Lisp_Object
1345 check_fontset_name (name, frame)
1346 Lisp_Object name, *frame;
1348 int id;
1350 if (NILP (*frame))
1351 *frame = selected_frame;
1352 CHECK_LIVE_FRAME (*frame);
1354 if (EQ (name, Qt))
1355 return Vdefault_fontset;
1356 if (NILP (name))
1358 id = FRAME_FONTSET (XFRAME (*frame));
1360 else
1362 CHECK_STRING (name);
1363 /* First try NAME as literal. */
1364 id = fs_query_fontset (name, 2);
1365 if (id < 0)
1366 /* For backward compatibility, try again NAME as pattern. */
1367 id = fs_query_fontset (name, 0);
1368 if (id < 0)
1369 error ("Fontset `%s' does not exist", SDATA (name));
1371 return FONTSET_FROM_ID (id);
1374 static void
1375 accumulate_script_ranges (arg, range, val)
1376 Lisp_Object arg, range, val;
1378 if (EQ (XCAR (arg), val))
1380 if (CONSP (range))
1381 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1382 else
1383 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1388 /* Callback function for map_charset_chars in Fset_fontset_font.
1389 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1391 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1392 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1393 The consumed ranges are poped up from SCRIPT_RANGE_LIST, and the
1394 new SCRIPT_RANGE_LIST is stored in ARG.
1396 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1397 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1398 case. */
1400 static void
1401 set_fontset_font (arg, range)
1402 Lisp_Object arg, range;
1404 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1405 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1407 fontset = AREF (arg, 0);
1408 font_def = AREF (arg, 1);
1409 add = AREF (arg, 2);
1410 ascii = AREF (arg, 3);
1411 script_range_list = AREF (arg, 4);
1413 if (NILP (ascii) && from < 0x80)
1415 if (to < 0x80)
1416 return;
1417 from = 0x80;
1418 range = Fcons (make_number (0x80), XCDR (range));
1421 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1422 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1423 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1425 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1426 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1427 if (CONSP (script_range_list))
1429 if (SCRIPT_FROM < from)
1430 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1431 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1432 POP_SCRIPT_RANGE ();
1433 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1434 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1437 FONTSET_ADD (fontset, range, font_def, add);
1438 ASET (arg, 4, script_range_list);
1441 extern Lisp_Object QCfamily, QCregistry;
1442 static void update_auto_fontset_alist P_ ((Lisp_Object, Lisp_Object));
1445 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1446 doc: /*
1447 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1449 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1450 the default fontset.
1452 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1453 In that case, use FONT-SPEC for all characters in the range FROM and
1454 TO (inclusive).
1456 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1457 all characters that belong to the script.
1459 TARGET may be a charset. In that case, use FONT-SPEC for all
1460 characters in the charset.
1462 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1463 that no FONT-SPEC is specified.
1465 FONT-SPEC may one of these:
1466 * A font-spec object made by the function `font-spec' (which see).
1467 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1468 REGISTRY is a font registry name. FAMILY may contain foundry
1469 name, and REGISTRY may contain encoding name.
1470 * A font name string.
1471 * nil, which explicitly specifies that there's no font for TARGET.
1473 Optional 4th argument FRAME is a frame or nil for the selected frame
1474 that is concerned in the case that NAME is nil.
1476 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1477 to the font specifications for TARGET previously set. If it is
1478 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1479 appended. By default, FONT-SPEC overrides the previous settings. */)
1480 (name, target, font_spec, frame, add)
1481 Lisp_Object name, target, font_spec, frame, add;
1483 Lisp_Object fontset;
1484 Lisp_Object font_def, registry, family;
1485 Lisp_Object range_list;
1486 struct charset *charset = NULL;
1487 Lisp_Object fontname;
1488 int ascii_changed = 0;
1490 fontset = check_fontset_name (name, &frame);
1492 fontname = Qnil;
1493 if (CONSP (font_spec))
1495 Lisp_Object spec = Ffont_spec (0, NULL);
1497 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1498 font_spec = spec;
1499 fontname = Ffont_xlfd_name (font_spec, Qnil);
1501 else if (STRINGP (font_spec))
1503 Lisp_Object args[2];
1504 extern Lisp_Object QCname;
1506 fontname = font_spec;
1507 args[0] = QCname;
1508 args[1] = font_spec;
1509 font_spec = Ffont_spec (2, args);
1511 else if (FONT_SPEC_P (font_spec))
1512 fontname = Ffont_xlfd_name (font_spec, Qnil);
1513 else if (! NILP (font_spec))
1514 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1516 if (! NILP (font_spec))
1518 Lisp_Object encoding, repertory;
1520 family = AREF (font_spec, FONT_FAMILY_INDEX);
1521 if (! NILP (family) )
1522 family = SYMBOL_NAME (family);
1523 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1524 if (! NILP (registry))
1525 registry = Fdowncase (SYMBOL_NAME (registry));
1526 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1527 registry));
1528 if (NILP (encoding))
1529 encoding = Qascii;
1531 if (SYMBOLP (encoding))
1533 CHECK_CHARSET (encoding);
1534 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1536 else
1538 repertory = XCDR (encoding);
1539 encoding = XCAR (encoding);
1540 CHECK_CHARSET (encoding);
1541 encoding = CHARSET_SYMBOL_ID (encoding);
1542 if (! NILP (repertory) && SYMBOLP (repertory))
1544 CHECK_CHARSET (repertory);
1545 repertory = CHARSET_SYMBOL_ID (repertory);
1548 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1550 else
1551 font_def = Qnil;
1553 if (CHARACTERP (target))
1555 if (XFASTINT (target) < 0x80)
1556 error ("Can't set a font for partial ASCII range.");
1557 range_list = Fcons (Fcons (target, target), Qnil);
1559 else if (CONSP (target))
1561 Lisp_Object from, to;
1563 from = Fcar (target);
1564 to = Fcdr (target);
1565 CHECK_CHARACTER (from);
1566 CHECK_CHARACTER (to);
1567 if (XFASTINT (from) < 0x80)
1569 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1570 error ("Can't set a font for partial ASCII range.");
1571 ascii_changed = 1;
1573 range_list = Fcons (target, Qnil);
1575 else if (SYMBOLP (target) && !NILP (target))
1577 Lisp_Object script_list;
1578 Lisp_Object val;
1580 range_list = Qnil;
1581 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1582 if (! NILP (Fmemq (target, script_list)))
1584 if (EQ (target, Qlatin))
1585 ascii_changed = 1;
1586 val = Fcons (target, Qnil);
1587 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1588 val);
1589 range_list = Fnreverse (XCDR (val));
1591 if (CHARSETP (target))
1593 CHECK_CHARSET_GET_CHARSET (target, charset);
1594 if (charset->ascii_compatible_p)
1595 ascii_changed = 1;
1597 else if (NILP (range_list))
1598 error ("Invalid script or charset name: %s",
1599 SDATA (SYMBOL_NAME (target)));
1601 else if (NILP (target))
1602 range_list = Fcons (Qnil, Qnil);
1603 else
1604 error ("Invalid target for setting a font");
1606 if (ascii_changed)
1608 Lisp_Object val;
1610 if (NILP (font_spec))
1611 error ("Can't set ASCII font to nil");
1612 val = CHAR_TABLE_REF (fontset, 0);
1613 if (! NILP (val) && EQ (add, Qappend))
1614 /* We are going to change just an additional font for ASCII. */
1615 ascii_changed = 0;
1618 if (charset)
1620 Lisp_Object arg;
1622 arg = Fmake_vector (make_number (5), Qnil);
1623 ASET (arg, 0, fontset);
1624 ASET (arg, 1, font_def);
1625 ASET (arg, 2, add);
1626 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1627 ASET (arg, 4, range_list);
1629 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1630 CHARSET_MIN_CODE (charset),
1631 CHARSET_MAX_CODE (charset));
1632 range_list = AREF (arg, 4);
1634 for (; CONSP (range_list); range_list = XCDR (range_list))
1635 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1637 if (ascii_changed)
1639 Lisp_Object tail, frame, alist;
1640 int fontset_id = XINT (FONTSET_ID (fontset));
1642 FONTSET_ASCII (fontset) = fontname;
1643 name = FONTSET_NAME (fontset);
1644 FOR_EACH_FRAME (tail, frame)
1646 FRAME_PTR f = XFRAME (frame);
1647 Lisp_Object font_object;
1648 struct face *face;
1650 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1651 continue;
1652 if (fontset_id != FRAME_FONTSET (f))
1653 continue;
1654 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1655 if (face)
1656 font_object = font_load_for_lface (f, face->lface, font_spec);
1657 else
1658 font_object = font_open_by_spec (f, font_spec);
1659 if (! NILP (font_object))
1661 update_auto_fontset_alist (font_object, fontset);
1662 alist = Fcons (Fcons (Qfont, Fcons (name, font_object)), Qnil);
1663 Fmodify_frame_parameters (frame, alist);
1668 /* Free all realized fontsets whose base is FONTSET. This way, the
1669 specified character(s) are surely redisplayed by a correct
1670 font. */
1671 free_realized_fontsets (fontset);
1673 return Qnil;
1677 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1678 doc: /* Create a new fontset NAME from font information in FONTLIST.
1680 FONTLIST is an alist of scripts vs the corresponding font specification list.
1681 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1682 character of SCRIPT is displayed by a font that matches one of
1683 FONT-SPEC.
1685 SCRIPT is a symbol that appears in the first extra slot of the
1686 char-table `char-script-table'.
1688 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1689 `set-fontset-font' for the meaning. */)
1690 (name, fontlist)
1691 Lisp_Object name, fontlist;
1693 Lisp_Object fontset;
1694 int id;
1696 CHECK_STRING (name);
1697 CHECK_LIST (fontlist);
1699 name = Fdowncase (name);
1700 id = fs_query_fontset (name, 0);
1701 if (id < 0)
1703 Lisp_Object font_spec = Ffont_spec (0, NULL);
1704 Lisp_Object short_name;
1705 char xlfd[256];
1706 int len;
1708 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
1709 error ("Fontset name must be in XLFD format");
1710 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1711 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1712 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1713 error ("Registry field of fontset name must be \"fontset-*\"");
1714 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1715 Vfontset_alias_alist);
1716 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1717 fontset = make_fontset (Qnil, name, Qnil);
1718 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1719 if (len < 0)
1720 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1721 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
1723 else
1725 fontset = FONTSET_FROM_ID (id);
1726 free_realized_fontsets (fontset);
1727 Fset_char_table_range (fontset, Qt, Qnil);
1730 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1732 Lisp_Object elt, script;
1734 elt = Fcar (fontlist);
1735 script = Fcar (elt);
1736 elt = Fcdr (elt);
1737 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1738 for (; CONSP (elt); elt = XCDR (elt))
1739 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1740 else
1741 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1743 return name;
1747 /* Alist of automatically created fontsets. Each element is a cons
1748 (FONT-SPEC . FONTSET-ID). */
1749 static Lisp_Object auto_fontset_alist;
1751 /* Number of automatically created fontsets. */
1752 static int num_auto_fontsets;
1754 /* Retun a fontset synthesized from FONT-OBJECT. This is called from
1755 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1756 frame, and the returned fontset is used for the default fontset of
1757 that frame. The fontset specifies a font of the same registry as
1758 FONT-OBJECT for all characters in the repertory of the registry
1759 (see Vfont_encoding_alist). If the repertory is not known, the
1760 fontset specifies the font for all Latin characters assuming that a
1761 user intends to use FONT-OBJECT for Latin characters. */
1764 fontset_from_font (font_object)
1765 Lisp_Object font_object;
1767 Lisp_Object font_name = font_get_name (font_object);
1768 Lisp_Object font_spec = Fcopy_font_spec (font_object);
1769 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1770 Lisp_Object fontset_spec, alias, name, fontset;
1771 Lisp_Object val;
1773 val = assoc_no_quit (font_spec, auto_fontset_alist);
1774 if (CONSP (val))
1775 return XINT (FONTSET_ID (XCDR (val)));
1776 if (num_auto_fontsets++ == 0)
1777 alias = intern ("fontset-startup");
1778 else
1780 char temp[32];
1782 sprintf (temp, "fontset-auto%d", num_auto_fontsets - 1);
1783 alias = intern (temp);
1785 fontset_spec = Fcopy_font_spec (font_spec);
1786 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1787 name = Ffont_xlfd_name (fontset_spec, Qnil);
1788 if (NILP (name))
1789 abort ();
1790 fontset = make_fontset (Qnil, name, Qnil);
1791 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1792 Vfontset_alias_alist);
1793 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1794 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1795 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1796 font_spec = Ffont_spec (0, NULL);
1797 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1799 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1801 if (CONSP (target))
1802 target = XCDR (target);
1803 if (! CHARSETP (target))
1804 target = Qlatin;
1805 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1806 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1809 FONTSET_ASCII (fontset) = font_name;
1811 return XINT (FONTSET_ID (fontset));
1815 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1816 FONTSET is changed, we delete an entry of FONTSET if any from
1817 auto_fontset_alist so that FONTSET is not re-used by
1818 fontset_from_font. */
1820 static void
1821 update_auto_fontset_alist (font_object, fontset)
1822 Lisp_Object font_object, fontset;
1824 Lisp_Object prev, tail;
1826 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1827 prev = tail, tail = XCDR (tail))
1828 if (EQ (fontset, XCDR (XCAR (tail))))
1830 if (NILP (prev))
1831 auto_fontset_alist = XCDR (tail);
1832 else
1833 XSETCDR (prev, XCDR (tail));
1834 break;
1839 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1840 FONT-OBJECT is the font for the character at POSITION in the current
1841 buffer. This is computed from all the text properties and overlays
1842 that apply to POSITION. POSTION may be nil, in which case,
1843 FONT-SPEC is the font for displaying the character CH with the
1844 default face.
1846 GLYPH-CODE is the glyph code in the font to use for the character.
1848 If the 2nd optional arg CH is non-nil, it is a character to check
1849 the font instead of the character at POSITION.
1851 It returns nil in the following cases:
1853 (1) The window system doesn't have a font for the character (thus
1854 it is displayed by an empty box).
1856 (2) The character code is invalid.
1858 (3) If POSITION is not nil, and the current buffer is not displayed
1859 in any window.
1861 In addition, the returned font name may not take into account of
1862 such redisplay engine hooks as what used in jit-lock-mode if
1863 POSITION is currently not visible. */
1866 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1867 doc: /* For internal use only. */)
1868 (position, ch)
1869 Lisp_Object position, ch;
1871 EMACS_INT pos, pos_byte, dummy;
1872 int face_id;
1873 int c;
1874 struct frame *f;
1875 struct face *face;
1876 int cs_id;
1878 if (NILP (position))
1880 CHECK_CHARACTER (ch);
1881 c = XINT (ch);
1882 f = XFRAME (selected_frame);
1883 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1884 pos = -1;
1885 cs_id = -1;
1887 else
1889 Lisp_Object window, charset;
1890 struct window *w;
1892 CHECK_NUMBER_COERCE_MARKER (position);
1893 pos = XINT (position);
1894 if (pos < BEGV || pos >= ZV)
1895 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1896 pos_byte = CHAR_TO_BYTE (pos);
1897 if (NILP (ch))
1898 c = FETCH_CHAR (pos_byte);
1899 else
1901 CHECK_NATNUM (ch);
1902 c = XINT (ch);
1904 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1905 if (NILP (window))
1906 return Qnil;
1907 w = XWINDOW (window);
1908 f = XFRAME (w->frame);
1909 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy,
1910 pos + 100, 0, -1);
1911 charset = Fget_char_property (position, Qcharset, Qnil);
1912 if (CHARSETP (charset))
1913 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
1914 else
1915 cs_id = -1;
1917 if (! CHAR_VALID_P (c, 0))
1918 return Qnil;
1919 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1920 face = FACE_FROM_ID (f, face_id);
1921 if (face->font)
1923 unsigned code = face->font->driver->encode_char (face->font, c);
1924 Lisp_Object font_object;
1925 /* Assignment to EMACS_INT stops GCC whining about limited range
1926 of data type. */
1927 EMACS_INT cod = code;
1929 if (code == FONT_INVALID_CODE)
1930 return Qnil;
1931 XSETFONT (font_object, face->font);
1932 if (cod <= MOST_POSITIVE_FIXNUM)
1933 return Fcons (font_object, make_number (code));
1934 return Fcons (font_object, Fcons (make_number (code >> 16),
1935 make_number (code & 0xFFFF)));
1937 return Qnil;
1941 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1942 doc: /* Return information about a fontset FONTSET on frame FRAME.
1944 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1945 for the default fontset. FRAME nil means the selected frame.
1947 The value is a char-table whose elements have this form:
1949 ((FONT OPENED-FONT ...) ...)
1951 FONT is a name of font specified for a range of characters.
1953 OPENED-FONT is a name of a font actually opened.
1955 The char-table has one extra slot. If FONTSET is not the default
1956 fontset, the value the extra slot is a char-table containing the
1957 information about the derived fonts from the default fontset. The
1958 format is the same as above. */)
1959 (fontset, frame)
1960 Lisp_Object fontset, frame;
1962 FRAME_PTR f;
1963 Lisp_Object *realized[2], fontsets[2], tables[2];
1964 Lisp_Object val, elt;
1965 int c, i, j, k;
1967 (*check_window_system_func) ();
1969 fontset = check_fontset_name (fontset, &frame);
1970 f = XFRAME (frame);
1972 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1973 in the table `realized'. */
1974 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1975 * ASIZE (Vfontset_table));
1976 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1978 elt = FONTSET_FROM_ID (i);
1979 if (!NILP (elt)
1980 && EQ (FONTSET_BASE (elt), fontset)
1981 && EQ (FONTSET_FRAME (elt), frame))
1982 realized[0][j++] = elt;
1984 realized[0][j] = Qnil;
1986 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1987 * ASIZE (Vfontset_table));
1988 for (i = j = 0; ! NILP (realized[0][i]); i++)
1990 elt = FONTSET_DEFAULT (realized[0][i]);
1991 if (! NILP (elt))
1992 realized[1][j++] = elt;
1994 realized[1][j] = Qnil;
1996 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1997 fontsets[0] = fontset;
1998 if (!EQ (fontset, Vdefault_fontset))
2000 tables[1] = Fmake_char_table (Qnil, Qnil);
2001 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
2002 fontsets[1] = Vdefault_fontset;
2005 /* Accumulate information of the fontset in TABLE. The format of
2006 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
2007 for (k = 0; k <= 1; k++)
2009 for (c = 0; c <= MAX_CHAR; )
2011 int from = c, to = MAX_5_BYTE_CHAR;
2013 if (c <= MAX_5_BYTE_CHAR)
2015 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
2017 else
2019 val = FONTSET_FALLBACK (fontsets[k]);
2020 to = MAX_CHAR;
2022 if (VECTORP (val))
2024 Lisp_Object alist;
2026 /* At first, set ALIST to ((FONT-SPEC) ...). */
2027 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
2028 if (! NILP (AREF (val, i)))
2029 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
2030 alist);
2031 alist = Fnreverse (alist);
2033 /* Then store opened font names to cdr of each elements. */
2034 for (i = 0; ! NILP (realized[k][i]); i++)
2036 if (c <= MAX_5_BYTE_CHAR)
2037 val = FONTSET_REF (realized[k][i], c);
2038 else
2039 val = FONTSET_FALLBACK (realized[k][i]);
2040 if (! CONSP (val) || ! VECTORP (XCDR (val)))
2041 continue;
2042 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
2043 val = XCDR (val);
2044 for (j = 0; j < ASIZE (val); j++)
2046 elt = AREF (val, j);
2047 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
2049 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
2050 Lisp_Object slot, name;
2052 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
2053 name = AREF (font_object, FONT_NAME_INDEX);
2054 if (NILP (Fmember (name, XCDR (slot))))
2055 nconc2 (slot, Fcons (name, Qnil));
2060 /* Store ALIST in TBL for characters C..TO. */
2061 if (c <= MAX_5_BYTE_CHAR)
2062 char_table_set_range (tables[k], c, to, alist);
2063 else
2064 XCHAR_TABLE (tables[k])->defalt = alist;
2066 /* At last, change each elements to font names. */
2067 for (; CONSP (alist); alist = XCDR (alist))
2069 elt = XCAR (alist);
2070 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
2073 c = to + 1;
2075 if (EQ (fontset, Vdefault_fontset))
2076 break;
2079 return tables[0];
2083 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2084 doc: /* Return a font name pattern for character CH in fontset NAME.
2085 If NAME is t, find a pattern in the default fontset.
2086 If NAME is nil, find a pattern in the fontset of the selected frame.
2088 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2089 family name and REGISTRY is a font registry name. This is actually
2090 the first font name pattern for CH in the fontset or in the default
2091 fontset.
2093 If the 2nd optional arg ALL is non-nil, return a list of all font name
2094 patterns. */)
2095 (name, ch, all)
2096 Lisp_Object name, ch, all;
2098 int c;
2099 Lisp_Object fontset, elt, list, repertory, val;
2100 int i, j;
2101 Lisp_Object frame;
2103 frame = Qnil;
2104 fontset = check_fontset_name (name, &frame);
2106 CHECK_CHARACTER (ch);
2107 c = XINT (ch);
2108 list = Qnil;
2109 while (1)
2111 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2112 i++, elt = FONTSET_FALLBACK (fontset))
2113 if (VECTORP (elt))
2114 for (j = 0; j < ASIZE (elt); j++)
2116 Lisp_Object family, registry;
2118 val = AREF (elt, j);
2119 if (NILP (val))
2120 return Qnil;
2121 repertory = AREF (val, 1);
2122 if (INTEGERP (repertory))
2124 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2126 if (! CHAR_CHARSET_P (c, charset))
2127 continue;
2129 else if (CHAR_TABLE_P (repertory))
2131 if (NILP (CHAR_TABLE_REF (repertory, c)))
2132 continue;
2134 val = AREF (val, 0);
2135 /* VAL is a FONT-SPEC */
2136 family = AREF (val, FONT_FAMILY_INDEX);
2137 if (! NILP (family))
2138 family = SYMBOL_NAME (family);
2139 registry = AREF (val, FONT_REGISTRY_INDEX);
2140 if (! NILP (registry))
2141 registry = SYMBOL_NAME (registry);
2142 val = Fcons (family, registry);
2143 if (NILP (all))
2144 return val;
2145 list = Fcons (val, list);
2147 if (EQ (fontset, Vdefault_fontset))
2148 break;
2149 fontset = Vdefault_fontset;
2151 return (Fnreverse (list));
2154 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2155 doc: /* Return a list of all defined fontset names. */)
2158 Lisp_Object fontset, list;
2159 int i;
2161 list = Qnil;
2162 for (i = 0; i < ASIZE (Vfontset_table); i++)
2164 fontset = FONTSET_FROM_ID (i);
2165 if (!NILP (fontset)
2166 && BASE_FONTSET_P (fontset))
2167 list = Fcons (FONTSET_NAME (fontset), list);
2170 return list;
2174 #ifdef FONTSET_DEBUG
2176 Lisp_Object
2177 dump_fontset (fontset)
2178 Lisp_Object fontset;
2180 Lisp_Object vec;
2182 vec = Fmake_vector (make_number (3), Qnil);
2183 ASET (vec, 0, FONTSET_ID (fontset));
2185 if (BASE_FONTSET_P (fontset))
2187 ASET (vec, 1, FONTSET_NAME (fontset));
2189 else
2191 Lisp_Object frame;
2193 frame = FONTSET_FRAME (fontset);
2194 if (FRAMEP (frame))
2196 FRAME_PTR f = XFRAME (frame);
2198 if (FRAME_LIVE_P (f))
2199 ASET (vec, 1,
2200 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2201 else
2202 ASET (vec, 1,
2203 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2205 if (!NILP (FONTSET_DEFAULT (fontset)))
2206 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2208 return vec;
2211 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2212 doc: /* Return a brief summary of all fontsets for debug use. */)
2215 Lisp_Object val;
2216 int i;
2218 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2219 if (! NILP (AREF (Vfontset_table, i)))
2220 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2221 return (Fnreverse (val));
2223 #endif /* FONTSET_DEBUG */
2225 void
2226 syms_of_fontset ()
2228 DEFSYM (Qfontset, "fontset");
2229 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2230 DEFSYM (Qfontset_info, "fontset-info");
2231 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2233 DEFSYM (Qprepend, "prepend");
2234 DEFSYM (Qappend, "append");
2235 DEFSYM (Qlatin, "latin");
2237 Vcached_fontset_data = Qnil;
2238 staticpro (&Vcached_fontset_data);
2240 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2241 staticpro (&Vfontset_table);
2243 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2244 staticpro (&Vdefault_fontset);
2245 FONTSET_ID (Vdefault_fontset) = make_number (0);
2246 FONTSET_NAME (Vdefault_fontset)
2247 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2248 ASET (Vfontset_table, 0, Vdefault_fontset);
2249 next_fontset_id = 1;
2251 auto_fontset_alist = Qnil;
2252 staticpro (&auto_fontset_alist);
2254 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2255 doc: /*
2256 Alist of charsets vs the charsets to determine the preferred font encoding.
2257 Each element looks like (CHARSET . ENCODING-CHARSET),
2258 where ENCODING-CHARSET is a charset registered in the variable
2259 `font-encoding-alist' as ENCODING.
2261 When a text has a property `charset' and the value is CHARSET, a font
2262 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2263 Vfont_encoding_charset_alist = Qnil;
2265 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
2266 doc: /*
2267 Char table of characters whose ascent values should be ignored.
2268 If an entry for a character is non-nil, the ascent value of the glyph
2269 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2271 This affects how a composite character which contains
2272 such a character is displayed on screen. */);
2273 Vuse_default_ascent = Qnil;
2275 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
2276 doc: /*
2277 Char table of characters which are not composed relatively.
2278 If an entry for a character is non-nil, a composition sequence
2279 which contains that character is displayed so that
2280 the glyph of that character is put without considering
2281 an ascent and descent value of a previous character. */);
2282 Vignore_relative_composition = Qnil;
2284 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
2285 doc: /* Alist of fontname vs list of the alternate fontnames.
2286 When a specified font name is not found, the corresponding
2287 alternate fontnames (if any) are tried instead. */);
2288 Valternate_fontname_alist = Qnil;
2290 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
2291 doc: /* Alist of fontset names vs the aliases. */);
2292 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2293 build_string ("fontset-default")),
2294 Qnil);
2296 DEFVAR_LISP ("vertical-centering-font-regexp",
2297 &Vvertical_centering_font_regexp,
2298 doc: /* *Regexp matching font names that require vertical centering on display.
2299 When a character is displayed with such fonts, the character is displayed
2300 at the vertical center of lines. */);
2301 Vvertical_centering_font_regexp = Qnil;
2303 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2304 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2305 Votf_script_alist = Qnil;
2307 defsubr (&Squery_fontset);
2308 defsubr (&Snew_fontset);
2309 defsubr (&Sset_fontset_font);
2310 defsubr (&Sinternal_char_font);
2311 defsubr (&Sfontset_info);
2312 defsubr (&Sfontset_font);
2313 defsubr (&Sfontset_list);
2314 #ifdef FONTSET_DEBUG
2315 defsubr (&Sfontset_list_all);
2316 #endif
2319 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2320 (do not change this comment) */