src/xdisp.c (single_display_spec_string): Correct a FIXME comment.
[emacs.git] / src / fontset.c
bloba40a3dd5f9c0eb05b80ebfd3dcd48505f8545558
1 /* Fontset handler.
3 Copyright (C) 2001-2011 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
8 Copyright (C) 2003, 2006
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
12 This file is part of GNU Emacs.
14 GNU Emacs is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 /* #define FONTSET_DEBUG */
29 #include <config.h>
30 #include <stdio.h>
31 #include <setjmp.h>
33 #include "lisp.h"
34 #include "blockinput.h"
35 #include "buffer.h"
36 #include "character.h"
37 #include "charset.h"
38 #include "ccl.h"
39 #include "keyboard.h"
40 #include "frame.h"
41 #include "dispextern.h"
42 #include "intervals.h"
43 #include "fontset.h"
44 #include "window.h"
45 #ifdef HAVE_X_WINDOWS
46 #include "xterm.h"
47 #endif
48 #ifdef WINDOWSNT
49 #include "w32term.h"
50 #endif
51 #ifdef HAVE_NS
52 #include "nsterm.h"
53 #endif
54 #include "termhooks.h"
56 #include "font.h"
58 #undef xassert
59 #ifdef FONTSET_DEBUG
60 #define xassert(X) do {if (!(X)) abort ();} while (0)
61 #undef INLINE
62 #define INLINE
63 #else /* not FONTSET_DEBUG */
64 #define xassert(X) (void) 0
65 #endif /* not FONTSET_DEBUG */
67 /* FONTSET
69 A fontset is a collection of font related information to give
70 similar appearance (style, etc) of characters. A fontset has two
71 roles. One is to use for the frame parameter `font' as if it is an
72 ASCII font. In that case, Emacs uses the font specified for
73 `ascii' script for the frame's default font.
75 Another role, the more important one, is to provide information
76 about which font to use for each non-ASCII character.
78 There are two kinds of fontsets; base and realized. A base fontset
79 is created by `new-fontset' from Emacs Lisp explicitly. A realized
80 fontset is created implicitly when a face is realized for ASCII
81 characters. A face is also realized for non-ASCII characters based
82 on an ASCII face. All of non-ASCII faces based on the same ASCII
83 face share the same realized fontset.
85 A fontset object is implemented by a char-table whose default value
86 and parent are always nil.
88 An element of a base fontset is a vector of FONT-DEFs which itself
89 is a vector [ FONT-SPEC ENCODING REPERTORY ].
91 An element of a realized fontset is nil, t, 0, or a vector of this
92 form:
94 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
95 RFONT-DEF0 RFONT-DEF1 ... ]
97 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
99 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
101 RFONT-DEFn are automatically reordered by the current charset
102 priority list.
104 The value nil means that we have not yet generated the above vector
105 from the base of the fontset.
107 The value t means that no font is available for the corresponding
108 range of characters.
110 The value 0 means that no font is available for the corresponding
111 range of characters in this fontset, but may be available in the
112 default fontset.
115 A fontset has 9 extra slots.
117 The 1st slot: the ID number of the fontset
119 The 2nd slot:
120 base: the name of the fontset
121 realized: nil
123 The 3rd slot:
124 base: nil
125 realized: the base fontset
127 The 4th slot:
128 base: nil
129 realized: the frame that the fontset belongs to
131 The 5th slot:
132 base: the font name for ASCII characters
133 realized: nil
135 The 6th slot:
136 base: nil
137 realized: the ID number of a face to use for characters that
138 has no font in a realized fontset.
140 The 7th slot:
141 base: nil
142 realized: Alist of font index vs the corresponding repertory
143 char-table.
145 The 8th slot:
146 base: nil
147 realized: If the base is not the default fontset, a fontset
148 realized from the default fontset, else nil.
150 The 9th slot:
151 base: Same as element value (but for fallback fonts).
152 realized: Likewise.
154 All fontsets are recorded in the vector Vfontset_table.
157 DEFAULT FONTSET
159 There's a special base fontset named `default fontset' which
160 defines the default font specifications. When a base fontset
161 doesn't specify a font for a specific character, the corresponding
162 value in the default fontset is used.
164 The parent of a realized fontset created for such a face that has
165 no fontset is the default fontset.
168 These structures are hidden from the other codes than this file.
169 The other codes handle fontsets only by their ID numbers. They
170 usually use the variable name `fontset' for IDs. But, in this
171 file, we always use varialbe name `id' for IDs, and name `fontset'
172 for an actual fontset object, i.e., char-table.
176 /********** VARIABLES and FUNCTION PROTOTYPES **********/
178 static Lisp_Object Qfontset;
179 static Lisp_Object Qfontset_info;
180 static Lisp_Object Qprepend, Qappend;
181 Lisp_Object Qlatin;
183 /* Vector containing all fontsets. */
184 static Lisp_Object Vfontset_table;
186 /* Next possibly free fontset ID. Usually this keeps the minimum
187 fontset ID not yet used. */
188 static int next_fontset_id;
190 /* The default fontset. This gives default FAMILY and REGISTRY of
191 font for each character. */
192 static Lisp_Object Vdefault_fontset;
194 /* Check if any window system is used now. */
195 void (*check_window_system_func) (void);
198 /* Prototype declarations for static functions. */
199 static Lisp_Object fontset_add (Lisp_Object, Lisp_Object, Lisp_Object,
200 Lisp_Object);
201 static Lisp_Object fontset_find_font (Lisp_Object, int, struct face *,
202 int, int);
203 static void reorder_font_vector (Lisp_Object, struct font *);
204 static Lisp_Object fontset_font (Lisp_Object, int, struct face *, int);
205 static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
206 static Lisp_Object fontset_pattern_regexp (Lisp_Object);
207 static void accumulate_script_ranges (Lisp_Object, Lisp_Object,
208 Lisp_Object);
209 static void set_fontset_font (Lisp_Object, Lisp_Object);
211 #ifdef FONTSET_DEBUG
213 /* Return 1 if ID is a valid fontset id, else return 0. */
215 static int
216 fontset_id_valid_p (int id)
218 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
221 #endif
225 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
227 /* Return the fontset with ID. No check of ID's validness. */
228 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
230 /* Macros to access special values of FONTSET. */
231 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
233 /* Macros to access special values of (base) FONTSET. */
234 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
235 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
236 /* #define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5] */
238 /* Macros to access special values of (realized) FONTSET. */
239 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
240 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
241 /* #define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4] */
242 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
243 /* #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6] */
244 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
246 /* For both base and realized fontset. */
247 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
249 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
252 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
253 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
254 do { \
255 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
256 ASET ((font_def), 1, encoding); \
257 ASET ((font_def), 2, repertory); \
258 } while (0)
260 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
261 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
262 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
264 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
265 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
266 ASET ((rfont_def), 0, make_number (face_id))
267 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
268 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
269 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
270 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
271 ASET ((rfont_def), 2, (object))
272 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
273 the order of listing by font backends, the higher bits represents
274 the order given by charset priority list. The smaller value is
275 preferable. */
276 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
277 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
278 ASET ((rfont_def), 3, make_number (score))
279 #define RFONT_DEF_NEW(rfont_def, font_def) \
280 do { \
281 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
282 ASET ((rfont_def), 1, (font_def)); \
283 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
284 } while (0)
287 /* Return the element of FONTSET for the character C. If FONTSET is a
288 base fontset other then the default fontset and FONTSET doesn't
289 contain information for C, return the information in the default
290 fontset. */
292 #define FONTSET_REF(fontset, c) \
293 (EQ (fontset, Vdefault_fontset) \
294 ? CHAR_TABLE_REF (fontset, c) \
295 : fontset_ref ((fontset), (c)))
297 static Lisp_Object
298 fontset_ref (Lisp_Object fontset, int c)
300 Lisp_Object elt;
302 elt = CHAR_TABLE_REF (fontset, c);
303 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
304 /* Don't check Vdefault_fontset for a realized fontset. */
305 && NILP (FONTSET_BASE (fontset)))
306 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
307 return elt;
310 /* Set elements of FONTSET for characters in RANGE to the value ELT.
311 RANGE is a cons (FROM . TO), where FROM and TO are character codes
312 specifying a range. */
314 #define FONTSET_SET(fontset, range, elt) \
315 Fset_char_table_range ((fontset), (range), (elt))
318 /* Modify the elements of FONTSET for characters in RANGE by replacing
319 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
320 and TO are character codes specifying a range. If ADD is nil,
321 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
322 append ELT. */
324 #define FONTSET_ADD(fontset, range, elt, add) \
325 (NILP (add) \
326 ? (NILP (range) \
327 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
328 : Fset_char_table_range ((fontset), (range), \
329 Fmake_vector (make_number (1), (elt)))) \
330 : fontset_add ((fontset), (range), (elt), (add)))
332 static Lisp_Object
333 fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
335 Lisp_Object args[2];
336 int idx = (EQ (add, Qappend) ? 0 : 1);
338 args[1 - idx] = Fmake_vector (make_number (1), elt);
340 if (CONSP (range))
342 int from = XINT (XCAR (range));
343 int to = XINT (XCDR (range));
344 int from1, to1;
346 do {
347 from1 = from, to1 = to;
348 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
349 char_table_set_range (fontset, from, to1,
350 NILP (args[idx]) ? args[1 - idx]
351 : Fvconcat (2, args));
352 from = to1 + 1;
353 } while (from < to);
355 else
357 args[idx] = FONTSET_FALLBACK (fontset);
358 FONTSET_FALLBACK (fontset)
359 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
361 return Qnil;
364 static int
365 fontset_compare_rfontdef (const void *val1, const void *val2)
367 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
368 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
371 /* Update FONT-GROUP which has this form:
372 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
373 RFONT-DEF0 RFONT-DEF1 ... ]
374 Reorder RFONT-DEFs according to the current language, and update
375 CHARSET-ORDERED-LIST-TICK.
377 If PREFERRED_FAMILY is not nil, that family has the higher priority
378 if the encoding charsets or languages in font-specs are the same. */
380 static void
381 reorder_font_vector (Lisp_Object font_group, struct font *font)
383 Lisp_Object vec, font_object;
384 int size;
385 int i;
386 int score_changed = 0;
388 if (font)
389 XSETFONT (font_object, font);
390 else
391 font_object = Qnil;
393 vec = XCDR (font_group);
394 size = ASIZE (vec);
395 /* Exclude the tailing nil element from the reordering. */
396 if (NILP (AREF (vec, size - 1)))
397 size--;
399 for (i = 0; i < size; i++)
401 Lisp_Object rfont_def = AREF (vec, i);
402 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
403 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
404 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
405 Lisp_Object otf_spec = Ffont_get (font_spec, QCotf);
407 if (! NILP (otf_spec))
408 /* A font-spec with :otf is preferable regardless of encoding
409 and language.. */
411 else if (! font_match_p (font_spec, font_object))
413 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
415 if (! NILP (encoding))
417 Lisp_Object tail;
419 for (tail = Vcharset_ordered_list;
420 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
421 score += 0x100, tail = XCDR (tail))
422 if (EQ (encoding, XCAR (tail)))
423 break;
425 else
427 Lisp_Object lang = Ffont_get (font_spec, QClang);
429 if (! NILP (lang)
430 && ! EQ (lang, Vcurrent_iso639_language)
431 && (! CONSP (Vcurrent_iso639_language)
432 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
433 score |= 0x100;
436 if (RFONT_DEF_SCORE (rfont_def) != score)
438 RFONT_DEF_SET_SCORE (rfont_def, score);
439 score_changed = 1;
443 if (score_changed)
444 qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
445 fontset_compare_rfontdef);
446 XSETCAR (font_group, make_number (charset_ordered_list_tick));
449 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
450 character C in FONTSET. If C is -1, return a fallback font-group.
451 If C is not -1, the value may be Qt (FONTSET doesn't have a font
452 for C even in the fallback group, or 0 (a font for C may be found
453 only in the fallback group). */
455 static Lisp_Object
456 fontset_get_font_group (Lisp_Object fontset, int c)
458 Lisp_Object font_group;
459 Lisp_Object base_fontset;
460 int from = 0, to = MAX_CHAR, i;
462 xassert (! BASE_FONTSET_P (fontset));
463 if (c >= 0)
464 font_group = CHAR_TABLE_REF (fontset, c);
465 else
466 font_group = FONTSET_FALLBACK (fontset);
467 if (! NILP (font_group))
468 return font_group;
469 base_fontset = FONTSET_BASE (fontset);
470 if (c >= 0)
471 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
472 else
473 font_group = FONTSET_FALLBACK (base_fontset);
474 if (NILP (font_group))
476 font_group = make_number (0);
477 if (c >= 0)
478 char_table_set_range (fontset, from, to, font_group);
479 return font_group;
481 font_group = Fcopy_sequence (font_group);
482 for (i = 0; i < ASIZE (font_group); i++)
483 if (! NILP (AREF (font_group, i)))
485 Lisp_Object rfont_def;
487 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
488 /* Remember the original order. */
489 RFONT_DEF_SET_SCORE (rfont_def, i);
490 ASET (font_group, i, rfont_def);
492 font_group = Fcons (make_number (-1), font_group);
493 if (c >= 0)
494 char_table_set_range (fontset, from, to, font_group);
495 else
496 FONTSET_FALLBACK (fontset) = font_group;
497 return font_group;
500 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
501 character C. If no font is found, return Qnil if there's a
502 possibility that the default fontset or the fallback font groups
503 have a proper font, and return Qt if not.
505 If a font is found but is not yet opened, open it (if FACE is not
506 NULL) or return Qnil (if FACE is NULL).
508 ID is a charset-id that must be preferred, or -1 meaning no
509 preference.
511 If FALLBACK is nonzero, search only fallback fonts. */
513 static Lisp_Object
514 fontset_find_font (Lisp_Object fontset, int c, struct face *face, int id, int fallback)
516 Lisp_Object vec, font_group;
517 int i, charset_matched = 0, found_index;
518 FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset))
519 ? XFRAME (FONTSET_FRAME (fontset)) : XFRAME (selected_frame));
520 Lisp_Object rfont_def;
522 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
523 if (! CONSP (font_group))
524 return font_group;
525 vec = XCDR (font_group);
526 if (ASIZE (vec) == 0)
527 return Qnil;
529 if (ASIZE (vec) > 1)
531 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
532 /* We have just created the font-group,
533 or the charset priorities were changed. */
534 reorder_font_vector (font_group, face->ascii_face->font);
535 if (id >= 0)
536 /* Find a spec matching with the charset ID to try at
537 first. */
538 for (i = 0; i < ASIZE (vec); i++)
540 Lisp_Object repertory;
542 rfont_def = AREF (vec, i);
543 if (NILP (rfont_def))
544 break;
545 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
547 if (XINT (repertory) == id)
549 charset_matched = i;
550 break;
555 /* Find the first available font in the vector of RFONT-DEF. */
556 for (i = 0; i < ASIZE (vec); i++)
558 Lisp_Object font_def;
559 Lisp_Object font_entity, font_object;
561 found_index = i;
562 if (i == 0)
564 if (charset_matched > 0)
566 /* Try the element matching with the charset ID at first. */
567 found_index = charset_matched;
568 /* Make this negative so that we don't come here in the
569 next loop. */
570 charset_matched = - charset_matched;
571 /* We must try the first element in the next loop. */
572 i--;
575 else if (i == - charset_matched)
577 /* We have already tried this element and the followings
578 that have the same font specifications in the first
579 iteration. So, skip them all. */
580 rfont_def = AREF (vec, i);
581 font_def = RFONT_DEF_FONT_DEF (rfont_def);
582 for (; i + 1 < ASIZE (vec); i++)
584 rfont_def = AREF (vec, i + 1);
585 if (NILP (rfont_def))
586 break;
587 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
588 break;
590 continue;
593 rfont_def = AREF (vec, found_index);
594 if (NILP (rfont_def))
596 if (i < 0)
597 continue;
598 /* This is a sign of not to try the other fonts. */
599 return Qt;
601 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
602 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
603 /* We couldn't open this font last time. */
604 continue;
606 font_object = RFONT_DEF_OBJECT (rfont_def);
607 if (NILP (font_object))
609 font_def = RFONT_DEF_FONT_DEF (rfont_def);
611 if (! face)
612 /* We have not yet opened the font. */
613 return Qnil;
614 /* Find a font best-matching with the spec without checking
615 the support of the character C. That checking is costly,
616 and even without the checking, the found font supports C
617 in high possibility. */
618 font_entity = font_find_for_lface (f, face->lface,
619 FONT_DEF_SPEC (font_def), -1);
620 if (NILP (font_entity))
622 /* Record that no font matches the spec. */
623 RFONT_DEF_SET_FACE (rfont_def, -1);
624 continue;
626 font_object = font_open_for_lface (f, font_entity, face->lface,
627 FONT_DEF_SPEC (font_def));
628 if (NILP (font_object))
630 /* Something strange happened, perhaps because of a
631 Font-backend problem. Too avoid crashing, record
632 that this spec is unsable. It may be better to find
633 another font of the same spec, but currently we don't
634 have such an API. */
635 RFONT_DEF_SET_FACE (rfont_def, -1);
636 continue;
638 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
641 if (font_has_char (f, font_object, c))
642 goto found;
644 /* Find a font already opened, maching with the current spec,
645 and supporting C. */
646 font_def = RFONT_DEF_FONT_DEF (rfont_def);
647 for (; found_index + 1 < ASIZE (vec); found_index++)
649 rfont_def = AREF (vec, found_index + 1);
650 if (NILP (rfont_def))
651 break;
652 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
653 break;
654 font_object = RFONT_DEF_OBJECT (rfont_def);
655 if (! NILP (font_object) && font_has_char (f, font_object, c))
657 found_index++;
658 goto found;
662 /* Find a font-entity with the current spec and supporting C. */
663 font_entity = font_find_for_lface (f, face->lface,
664 FONT_DEF_SPEC (font_def), c);
665 if (! NILP (font_entity))
667 /* We found a font. Open it and insert a new element for
668 that font in VEC. */
669 Lisp_Object new_vec;
670 int j;
672 font_object = font_open_for_lface (f, font_entity, face->lface,
673 Qnil);
674 if (NILP (font_object))
675 continue;
676 RFONT_DEF_NEW (rfont_def, font_def);
677 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
678 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
679 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
680 found_index++;
681 for (j = 0; j < found_index; j++)
682 ASET (new_vec, j, AREF (vec, j));
683 ASET (new_vec, j, rfont_def);
684 for (j++; j < ASIZE (new_vec); j++)
685 ASET (new_vec, j, AREF (vec, j - 1));
686 XSETCDR (font_group, new_vec);
687 vec = new_vec;
688 goto found;
690 if (i >= 0)
691 i = found_index;
694 FONTSET_SET (fontset, make_number (c), make_number (0));
695 return Qnil;
697 found:
698 if (fallback && found_index > 0)
700 /* The order of fonts in the fallback font-group is not that
701 important, and it is better to move the found font to the
702 first of the group so that the next try will find it
703 quickly. */
704 for (i = found_index; i > 0; i--)
705 ASET (vec, i, AREF (vec, i - 1));
706 ASET (vec, 0, rfont_def);
708 return rfont_def;
712 static Lisp_Object
713 fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
715 Lisp_Object rfont_def, default_rfont_def IF_LINT (= Qnil);
716 Lisp_Object base_fontset;
718 /* Try a font-group of FONTSET. */
719 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
720 rfont_def = fontset_find_font (fontset, c, face, id, 0);
721 if (VECTORP (rfont_def))
722 return rfont_def;
723 if (NILP (rfont_def))
724 FONTSET_SET (fontset, make_number (c), make_number (0));
726 /* Try a font-group of the default fontset. */
727 base_fontset = FONTSET_BASE (fontset);
728 if (! EQ (base_fontset, Vdefault_fontset))
730 if (NILP (FONTSET_DEFAULT (fontset)))
731 FONTSET_DEFAULT (fontset)
732 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
733 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
734 default_rfont_def
735 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
736 if (VECTORP (default_rfont_def))
737 return default_rfont_def;
738 if (NILP (default_rfont_def))
739 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
740 make_number (0));
743 /* Try a fallback font-group of FONTSET. */
744 if (! EQ (rfont_def, Qt))
746 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
747 rfont_def = fontset_find_font (fontset, c, face, id, 1);
748 if (VECTORP (rfont_def))
749 return rfont_def;
750 /* Remember that FONTSET has no font for C. */
751 FONTSET_SET (fontset, make_number (c), Qt);
754 /* Try a fallback font-group of the default fontset. */
755 if (! EQ (base_fontset, Vdefault_fontset)
756 && ! EQ (default_rfont_def, Qt))
758 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
759 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
760 if (VECTORP (rfont_def))
761 return rfont_def;
762 /* Remember that the default fontset has no font for C. */
763 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
766 return Qnil;
769 /* Return a newly created fontset with NAME. If BASE is nil, make a
770 base fontset. Otherwise make a realized fontset whose base is
771 BASE. */
773 static Lisp_Object
774 make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
776 Lisp_Object fontset;
777 int size = ASIZE (Vfontset_table);
778 int id = next_fontset_id;
780 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
781 the next available fontset ID. So it is expected that this loop
782 terminates quickly. In addition, as the last element of
783 Vfontset_table is always nil, we don't have to check the range of
784 id. */
785 while (!NILP (AREF (Vfontset_table, id))) id++;
787 if (id + 1 == size)
788 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
790 fontset = Fmake_char_table (Qfontset, Qnil);
792 FONTSET_ID (fontset) = make_number (id);
793 if (NILP (base))
795 FONTSET_NAME (fontset) = name;
797 else
799 FONTSET_NAME (fontset) = Qnil;
800 FONTSET_FRAME (fontset) = frame;
801 FONTSET_BASE (fontset) = base;
804 ASET (Vfontset_table, id, fontset);
805 next_fontset_id = id + 1;
806 return fontset;
810 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
812 /* Return the name of the fontset who has ID. */
814 Lisp_Object
815 fontset_name (int id)
817 Lisp_Object fontset;
819 fontset = FONTSET_FROM_ID (id);
820 return FONTSET_NAME (fontset);
824 /* Return the ASCII font name of the fontset who has ID. */
826 Lisp_Object
827 fontset_ascii (int id)
829 Lisp_Object fontset, elt;
831 fontset= FONTSET_FROM_ID (id);
832 elt = FONTSET_ASCII (fontset);
833 if (CONSP (elt))
834 elt = XCAR (elt);
835 return elt;
838 static void
839 free_realized_fontset (FRAME_PTR f, Lisp_Object fontset)
841 #if 0
842 Lisp_Object tail;
844 if (0)
845 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
847 xassert (FONT_OBJECT_P (XCAR (tail)));
848 font_close_object (f, XCAR (tail));
850 #endif
853 /* Free fontset of FACE defined on frame F. Called from
854 free_realized_face. */
856 void
857 free_face_fontset (FRAME_PTR f, struct face *face)
859 Lisp_Object fontset;
861 fontset = FONTSET_FROM_ID (face->fontset);
862 if (NILP (fontset))
863 return;
864 xassert (! BASE_FONTSET_P (fontset));
865 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
866 free_realized_fontset (f, fontset);
867 ASET (Vfontset_table, face->fontset, Qnil);
868 if (face->fontset < next_fontset_id)
869 next_fontset_id = face->fontset;
870 if (! NILP (FONTSET_DEFAULT (fontset)))
872 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
874 fontset = AREF (Vfontset_table, id);
875 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
876 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
877 free_realized_fontset (f, fontset);
878 ASET (Vfontset_table, id, Qnil);
879 if (id < next_fontset_id)
880 next_fontset_id = face->fontset;
882 face->fontset = -1;
886 #if 0
887 /* Return 1 if FACE is suitable for displaying character C.
888 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
889 when C is not an ASCII character. */
892 face_suitable_for_char_p (struct face *face, int c)
894 Lisp_Object fontset, rfont_def;
896 fontset = FONTSET_FROM_ID (face->fontset);
897 rfont_def = fontset_font (fontset, c, NULL, -1);
898 return (VECTORP (rfont_def)
899 && INTEGERP (RFONT_DEF_FACE (rfont_def))
900 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
902 #endif
905 /* Return ID of face suitable for displaying character C on frame F.
906 FACE must be reazlied for ASCII characters in advance. Called from
907 the macro FACE_FOR_CHAR. */
910 face_for_char (FRAME_PTR f, struct face *face, int c, int pos, Lisp_Object object)
912 Lisp_Object fontset, rfont_def, charset;
913 int face_id;
914 int id;
916 /* If face->fontset is negative (that happens when no font is found
917 for face), just return face->ascii_face because we can't do
918 anything. Perhaps, we should fix the callers to assure
919 that face->fontset is always valid. */
920 if (ASCII_CHAR_P (c) || face->fontset < 0)
921 return face->ascii_face->id;
923 xassert (fontset_id_valid_p (face->fontset));
924 fontset = FONTSET_FROM_ID (face->fontset);
925 xassert (!BASE_FONTSET_P (fontset));
927 if (pos < 0)
929 id = -1;
930 charset = Qnil;
932 else
934 charset = Fget_char_property (make_number (pos), Qcharset, object);
935 if (CHARSETP (charset))
937 Lisp_Object val;
939 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
940 if (CONSP (val) && CHARSETP (XCDR (val)))
941 charset = XCDR (val);
942 id = XINT (CHARSET_SYMBOL_ID (charset));
944 else
945 id = -1;
948 rfont_def = fontset_font (fontset, c, face, id);
949 if (VECTORP (rfont_def))
951 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
952 face_id = XINT (RFONT_DEF_FACE (rfont_def));
953 else
955 Lisp_Object font_object;
957 font_object = RFONT_DEF_OBJECT (rfont_def);
958 face_id = face_for_font (f, font_object, face);
959 RFONT_DEF_SET_FACE (rfont_def, face_id);
962 else
964 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
965 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
966 else
968 face_id = face_for_font (f, Qnil, face);
969 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
972 xassert (face_id >= 0);
973 return face_id;
977 Lisp_Object
978 font_for_char (struct face *face, int c, int pos, Lisp_Object object)
980 Lisp_Object fontset, rfont_def, charset;
981 int id;
983 if (ASCII_CHAR_P (c))
985 Lisp_Object font_object;
987 XSETFONT (font_object, face->ascii_face->font);
988 return font_object;
991 xassert (fontset_id_valid_p (face->fontset));
992 fontset = FONTSET_FROM_ID (face->fontset);
993 xassert (!BASE_FONTSET_P (fontset));
994 if (pos < 0)
996 id = -1;
997 charset = Qnil;
999 else
1001 charset = Fget_char_property (make_number (pos), Qcharset, object);
1002 if (CHARSETP (charset))
1004 Lisp_Object val;
1006 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
1007 if (CONSP (val) && CHARSETP (XCDR (val)))
1008 charset = XCDR (val);
1009 id = XINT (CHARSET_SYMBOL_ID (charset));
1011 else
1012 id = -1;
1015 rfont_def = fontset_font (fontset, c, face, id);
1016 return (VECTORP (rfont_def)
1017 ? RFONT_DEF_OBJECT (rfont_def)
1018 : Qnil);
1022 /* Make a realized fontset for ASCII face FACE on frame F from the
1023 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1024 default fontset as the base. Value is the id of the new fontset.
1025 Called from realize_x_face. */
1028 make_fontset_for_ascii_face (FRAME_PTR f, int base_fontset_id, struct face *face)
1030 Lisp_Object base_fontset, fontset, frame;
1032 XSETFRAME (frame, f);
1033 if (base_fontset_id >= 0)
1035 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1036 if (!BASE_FONTSET_P (base_fontset))
1037 base_fontset = FONTSET_BASE (base_fontset);
1038 if (! BASE_FONTSET_P (base_fontset))
1039 abort ();
1041 else
1042 base_fontset = Vdefault_fontset;
1044 fontset = make_fontset (frame, Qnil, base_fontset);
1045 return XINT (FONTSET_ID (fontset));
1050 /* Cache data used by fontset_pattern_regexp. The car part is a
1051 pattern string containing at least one wild card, the cdr part is
1052 the corresponding regular expression. */
1053 static Lisp_Object Vcached_fontset_data;
1055 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1056 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1058 /* If fontset name PATTERN contains any wild card, return regular
1059 expression corresponding to PATTERN. */
1061 static Lisp_Object
1062 fontset_pattern_regexp (Lisp_Object pattern)
1064 if (!strchr (SSDATA (pattern), '*')
1065 && !strchr (SSDATA (pattern), '?'))
1066 /* PATTERN does not contain any wild cards. */
1067 return Qnil;
1069 if (!CONSP (Vcached_fontset_data)
1070 || strcmp (SSDATA (pattern), CACHED_FONTSET_NAME))
1072 /* We must at first update the cached data. */
1073 unsigned char *regex, *p0, *p1;
1074 int ndashes = 0, nstars = 0, nescs = 0;
1076 for (p0 = SDATA (pattern); *p0; p0++)
1078 if (*p0 == '-')
1079 ndashes++;
1080 else if (*p0 == '*')
1081 nstars++;
1082 else if (*p0 == '['
1083 || *p0 == '.' || *p0 == '\\'
1084 || *p0 == '+' || *p0 == '^'
1085 || *p0 == '$')
1086 nescs++;
1089 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1090 we convert "*" to "[^-]*" which is much faster in regular
1091 expression matching. */
1092 if (ndashes < 14)
1093 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
1094 else
1095 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
1097 *p1++ = '^';
1098 for (p0 = SDATA (pattern); *p0; p0++)
1100 if (*p0 == '*')
1102 if (ndashes < 14)
1103 *p1++ = '.';
1104 else
1105 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1106 *p1++ = '*';
1108 else if (*p0 == '?')
1109 *p1++ = '.';
1110 else if (*p0 == '['
1111 || *p0 == '.' || *p0 == '\\'
1112 || *p0 == '+' || *p0 == '^'
1113 || *p0 == '$')
1114 *p1++ = '\\', *p1++ = *p0;
1115 else
1116 *p1++ = *p0;
1118 *p1++ = '$';
1119 *p1++ = 0;
1121 Vcached_fontset_data = Fcons (build_string (SSDATA (pattern)),
1122 build_string ((char *) regex));
1125 return CACHED_FONTSET_REGEX;
1128 /* Return ID of the base fontset named NAME. If there's no such
1129 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1130 0: pattern containing '*' and '?' as wildcards
1131 1: regular expression
1132 2: literal fontset name
1136 fs_query_fontset (Lisp_Object name, int name_pattern)
1138 Lisp_Object tem;
1139 int i;
1141 name = Fdowncase (name);
1142 if (name_pattern != 1)
1144 tem = Frassoc (name, Vfontset_alias_alist);
1145 if (NILP (tem))
1146 tem = Fassoc (name, Vfontset_alias_alist);
1147 if (CONSP (tem) && STRINGP (XCAR (tem)))
1148 name = XCAR (tem);
1149 else if (name_pattern == 0)
1151 tem = fontset_pattern_regexp (name);
1152 if (STRINGP (tem))
1154 name = tem;
1155 name_pattern = 1;
1160 for (i = 0; i < ASIZE (Vfontset_table); i++)
1162 Lisp_Object fontset, this_name;
1164 fontset = FONTSET_FROM_ID (i);
1165 if (NILP (fontset)
1166 || !BASE_FONTSET_P (fontset))
1167 continue;
1169 this_name = FONTSET_NAME (fontset);
1170 if (name_pattern == 1
1171 ? fast_string_match_ignore_case (name, this_name) >= 0
1172 : !xstrcasecmp (SSDATA (name), SSDATA (this_name)))
1173 return i;
1175 return -1;
1179 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1180 doc: /* Return the name of a fontset that matches PATTERN.
1181 The value is nil if there is no matching fontset.
1182 PATTERN can contain `*' or `?' as a wildcard
1183 just as X font name matching algorithm allows.
1184 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1185 (Lisp_Object pattern, Lisp_Object regexpp)
1187 Lisp_Object fontset;
1188 int id;
1190 (*check_window_system_func) ();
1192 CHECK_STRING (pattern);
1194 if (SCHARS (pattern) == 0)
1195 return Qnil;
1197 id = fs_query_fontset (pattern, !NILP (regexpp));
1198 if (id < 0)
1199 return Qnil;
1201 fontset = FONTSET_FROM_ID (id);
1202 return FONTSET_NAME (fontset);
1205 /* Return a list of base fontset names matching PATTERN on frame F. */
1207 Lisp_Object
1208 list_fontsets (FRAME_PTR f, Lisp_Object pattern, 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 (SSDATA (pattern), SSDATA (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 (Lisp_Object base)
1246 int id;
1248 #if 0
1249 /* For the moment, this doesn't work because free_realized_face
1250 doesn't remove FACE from a cache. Until we find a solution, we
1251 suppress this code, and simply use Fclear_face_cache even though
1252 that is not efficient. */
1253 BLOCK_INPUT;
1254 for (id = 0; id < ASIZE (Vfontset_table); id++)
1256 Lisp_Object this = AREF (Vfontset_table, id);
1258 if (EQ (FONTSET_BASE (this), base))
1260 Lisp_Object tail;
1262 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1263 tail = XCDR (tail))
1265 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1266 int face_id = XINT (XCDR (XCAR (tail)));
1267 struct face *face = FACE_FROM_ID (f, face_id);
1269 /* Face THIS itself is also freed by the following call. */
1270 free_realized_face (f, face);
1274 UNBLOCK_INPUT;
1275 #else /* not 0 */
1276 /* But, we don't have to call Fclear_face_cache if no fontset has
1277 been realized from BASE. */
1278 for (id = 0; id < ASIZE (Vfontset_table); id++)
1280 Lisp_Object this = AREF (Vfontset_table, id);
1282 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1284 Fclear_face_cache (Qt);
1285 break;
1288 #endif /* not 0 */
1292 /* Check validity of NAME as a fontset name and return the
1293 corresponding fontset. If not valid, signal an error.
1295 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1296 fontset of *FRAME.
1298 Set *FRAME to the actual frame. */
1300 static Lisp_Object
1301 check_fontset_name (Lisp_Object name, Lisp_Object *frame)
1303 int id;
1305 if (NILP (*frame))
1306 *frame = selected_frame;
1307 CHECK_LIVE_FRAME (*frame);
1309 if (EQ (name, Qt))
1310 return Vdefault_fontset;
1311 if (NILP (name))
1313 id = FRAME_FONTSET (XFRAME (*frame));
1315 else
1317 CHECK_STRING (name);
1318 /* First try NAME as literal. */
1319 id = fs_query_fontset (name, 2);
1320 if (id < 0)
1321 /* For backward compatibility, try again NAME as pattern. */
1322 id = fs_query_fontset (name, 0);
1323 if (id < 0)
1324 error ("Fontset `%s' does not exist", SDATA (name));
1326 return FONTSET_FROM_ID (id);
1329 static void
1330 accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
1332 if (EQ (XCAR (arg), val))
1334 if (CONSP (range))
1335 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1336 else
1337 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1342 /* Callback function for map_charset_chars in Fset_fontset_font.
1343 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1345 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1346 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1347 The consumed ranges are poped up from SCRIPT_RANGE_LIST, and the
1348 new SCRIPT_RANGE_LIST is stored in ARG.
1350 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1351 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1352 case. */
1354 static void
1355 set_fontset_font (Lisp_Object arg, Lisp_Object range)
1357 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1358 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1360 fontset = AREF (arg, 0);
1361 font_def = AREF (arg, 1);
1362 add = AREF (arg, 2);
1363 ascii = AREF (arg, 3);
1364 script_range_list = AREF (arg, 4);
1366 if (NILP (ascii) && from < 0x80)
1368 if (to < 0x80)
1369 return;
1370 from = 0x80;
1371 range = Fcons (make_number (0x80), XCDR (range));
1374 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1375 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1376 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1378 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1379 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1380 if (CONSP (script_range_list))
1382 if (SCRIPT_FROM < from)
1383 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1384 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1385 POP_SCRIPT_RANGE ();
1386 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1387 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1390 FONTSET_ADD (fontset, range, font_def, add);
1391 ASET (arg, 4, script_range_list);
1394 static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
1397 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1398 doc: /*
1399 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1401 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1402 the default fontset.
1404 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1405 In that case, use FONT-SPEC for all characters in the range FROM and
1406 TO (inclusive).
1408 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1409 all characters that belong to the script.
1411 TARGET may be a charset. In that case, use FONT-SPEC for all
1412 characters in the charset.
1414 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1415 that no FONT-SPEC is specified.
1417 FONT-SPEC may one of these:
1418 * A font-spec object made by the function `font-spec' (which see).
1419 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1420 REGISTRY is a font registry name. FAMILY may contain foundry
1421 name, and REGISTRY may contain encoding name.
1422 * A font name string.
1423 * nil, which explicitly specifies that there's no font for TARGET.
1425 Optional 4th argument FRAME is a frame or nil for the selected frame
1426 that is concerned in the case that NAME is nil.
1428 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1429 to the font specifications for TARGET previously set. If it is
1430 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1431 appended. By default, FONT-SPEC overrides the previous settings. */)
1432 (Lisp_Object name, Lisp_Object target, Lisp_Object font_spec, Lisp_Object frame, Lisp_Object add)
1434 Lisp_Object fontset;
1435 Lisp_Object font_def, registry, family;
1436 Lisp_Object range_list;
1437 struct charset *charset = NULL;
1438 Lisp_Object fontname;
1439 int ascii_changed = 0;
1441 fontset = check_fontset_name (name, &frame);
1443 fontname = Qnil;
1444 if (CONSP (font_spec))
1446 Lisp_Object spec = Ffont_spec (0, NULL);
1448 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1449 font_spec = spec;
1450 fontname = Ffont_xlfd_name (font_spec, Qnil);
1452 else if (STRINGP (font_spec))
1454 Lisp_Object args[2];
1456 fontname = font_spec;
1457 args[0] = QCname;
1458 args[1] = font_spec;
1459 font_spec = Ffont_spec (2, args);
1461 else if (FONT_SPEC_P (font_spec))
1462 fontname = Ffont_xlfd_name (font_spec, Qnil);
1463 else if (! NILP (font_spec))
1464 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1466 if (! NILP (font_spec))
1468 Lisp_Object encoding, repertory;
1470 family = AREF (font_spec, FONT_FAMILY_INDEX);
1471 if (! NILP (family) )
1472 family = SYMBOL_NAME (family);
1473 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1474 if (! NILP (registry))
1475 registry = Fdowncase (SYMBOL_NAME (registry));
1476 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1477 registry));
1478 if (NILP (encoding))
1479 encoding = Qascii;
1481 if (SYMBOLP (encoding))
1483 CHECK_CHARSET (encoding);
1484 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1486 else
1488 repertory = XCDR (encoding);
1489 encoding = XCAR (encoding);
1490 CHECK_CHARSET (encoding);
1491 encoding = CHARSET_SYMBOL_ID (encoding);
1492 if (! NILP (repertory) && SYMBOLP (repertory))
1494 CHECK_CHARSET (repertory);
1495 repertory = CHARSET_SYMBOL_ID (repertory);
1498 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1500 else
1501 font_def = Qnil;
1503 if (CHARACTERP (target))
1505 if (XFASTINT (target) < 0x80)
1506 error ("Can't set a font for partial ASCII range");
1507 range_list = Fcons (Fcons (target, target), Qnil);
1509 else if (CONSP (target))
1511 Lisp_Object from, to;
1513 from = Fcar (target);
1514 to = Fcdr (target);
1515 CHECK_CHARACTER (from);
1516 CHECK_CHARACTER (to);
1517 if (XFASTINT (from) < 0x80)
1519 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1520 error ("Can't set a font for partial ASCII range");
1521 ascii_changed = 1;
1523 range_list = Fcons (target, Qnil);
1525 else if (SYMBOLP (target) && !NILP (target))
1527 Lisp_Object script_list;
1528 Lisp_Object val;
1530 range_list = Qnil;
1531 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1532 if (! NILP (Fmemq (target, script_list)))
1534 if (EQ (target, Qlatin))
1535 ascii_changed = 1;
1536 val = Fcons (target, Qnil);
1537 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1538 val);
1539 range_list = Fnreverse (XCDR (val));
1541 if (CHARSETP (target))
1543 CHECK_CHARSET_GET_CHARSET (target, charset);
1544 if (charset->ascii_compatible_p)
1545 ascii_changed = 1;
1547 else if (NILP (range_list))
1548 error ("Invalid script or charset name: %s",
1549 SDATA (SYMBOL_NAME (target)));
1551 else if (NILP (target))
1552 range_list = Fcons (Qnil, Qnil);
1553 else
1554 error ("Invalid target for setting a font");
1556 if (ascii_changed)
1558 Lisp_Object val;
1560 if (NILP (font_spec))
1561 error ("Can't set ASCII font to nil");
1562 val = CHAR_TABLE_REF (fontset, 0);
1563 if (! NILP (val) && EQ (add, Qappend))
1564 /* We are going to change just an additional font for ASCII. */
1565 ascii_changed = 0;
1568 if (charset)
1570 Lisp_Object arg;
1572 arg = Fmake_vector (make_number (5), Qnil);
1573 ASET (arg, 0, fontset);
1574 ASET (arg, 1, font_def);
1575 ASET (arg, 2, add);
1576 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1577 ASET (arg, 4, range_list);
1579 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1580 CHARSET_MIN_CODE (charset),
1581 CHARSET_MAX_CODE (charset));
1582 range_list = AREF (arg, 4);
1584 for (; CONSP (range_list); range_list = XCDR (range_list))
1585 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1587 if (ascii_changed)
1589 Lisp_Object tail, fr, alist;
1590 int fontset_id = XINT (FONTSET_ID (fontset));
1592 FONTSET_ASCII (fontset) = fontname;
1593 name = FONTSET_NAME (fontset);
1594 FOR_EACH_FRAME (tail, fr)
1596 FRAME_PTR f = XFRAME (fr);
1597 Lisp_Object font_object;
1598 struct face *face;
1600 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1601 continue;
1602 if (fontset_id != FRAME_FONTSET (f))
1603 continue;
1604 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1605 if (face)
1606 font_object = font_load_for_lface (f, face->lface, font_spec);
1607 else
1608 font_object = font_open_by_spec (f, font_spec);
1609 if (! NILP (font_object))
1611 update_auto_fontset_alist (font_object, fontset);
1612 alist = Fcons (Fcons (Qfont, Fcons (name, font_object)), Qnil);
1613 Fmodify_frame_parameters (fr, alist);
1618 /* Free all realized fontsets whose base is FONTSET. This way, the
1619 specified character(s) are surely redisplayed by a correct
1620 font. */
1621 free_realized_fontsets (fontset);
1623 return Qnil;
1627 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1628 doc: /* Create a new fontset NAME from font information in FONTLIST.
1630 FONTLIST is an alist of scripts vs the corresponding font specification list.
1631 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1632 character of SCRIPT is displayed by a font that matches one of
1633 FONT-SPEC.
1635 SCRIPT is a symbol that appears in the first extra slot of the
1636 char-table `char-script-table'.
1638 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1639 `set-fontset-font' for the meaning. */)
1640 (Lisp_Object name, Lisp_Object fontlist)
1642 Lisp_Object fontset;
1643 int id;
1645 CHECK_STRING (name);
1646 CHECK_LIST (fontlist);
1648 name = Fdowncase (name);
1649 id = fs_query_fontset (name, 0);
1650 if (id < 0)
1652 Lisp_Object font_spec = Ffont_spec (0, NULL);
1653 Lisp_Object short_name;
1654 char xlfd[256];
1655 int len;
1657 if (font_parse_xlfd (SSDATA (name), font_spec) < 0)
1658 error ("Fontset name must be in XLFD format");
1659 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1660 if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1661 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1662 error ("Registry field of fontset name must be \"fontset-*\"");
1663 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1664 Vfontset_alias_alist);
1665 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1666 fontset = make_fontset (Qnil, name, Qnil);
1667 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1668 if (len < 0)
1669 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1670 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
1672 else
1674 fontset = FONTSET_FROM_ID (id);
1675 free_realized_fontsets (fontset);
1676 Fset_char_table_range (fontset, Qt, Qnil);
1679 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1681 Lisp_Object elt, script;
1683 elt = Fcar (fontlist);
1684 script = Fcar (elt);
1685 elt = Fcdr (elt);
1686 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1687 for (; CONSP (elt); elt = XCDR (elt))
1688 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1689 else
1690 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1692 return name;
1696 /* Alist of automatically created fontsets. Each element is a cons
1697 (FONT-SPEC . FONTSET-ID). */
1698 static Lisp_Object auto_fontset_alist;
1700 /* Number of automatically created fontsets. */
1701 static int num_auto_fontsets;
1703 /* Retun a fontset synthesized from FONT-OBJECT. This is called from
1704 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1705 frame, and the returned fontset is used for the default fontset of
1706 that frame. The fontset specifies a font of the same registry as
1707 FONT-OBJECT for all characters in the repertory of the registry
1708 (see Vfont_encoding_alist). If the repertory is not known, the
1709 fontset specifies the font for all Latin characters assuming that a
1710 user intends to use FONT-OBJECT for Latin characters. */
1713 fontset_from_font (Lisp_Object font_object)
1715 Lisp_Object font_name = font_get_name (font_object);
1716 Lisp_Object font_spec = copy_font_spec (font_object);
1717 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1718 Lisp_Object fontset_spec, alias, name, fontset;
1719 Lisp_Object val;
1721 val = assoc_no_quit (font_spec, auto_fontset_alist);
1722 if (CONSP (val))
1723 return XINT (FONTSET_ID (XCDR (val)));
1724 if (num_auto_fontsets++ == 0)
1725 alias = intern ("fontset-startup");
1726 else
1728 char temp[32];
1730 sprintf (temp, "fontset-auto%d", num_auto_fontsets - 1);
1731 alias = intern (temp);
1733 fontset_spec = copy_font_spec (font_spec);
1734 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1735 name = Ffont_xlfd_name (fontset_spec, Qnil);
1736 if (NILP (name))
1737 abort ();
1738 fontset = make_fontset (Qnil, name, Qnil);
1739 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1740 Vfontset_alias_alist);
1741 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1742 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1743 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1744 font_spec = Ffont_spec (0, NULL);
1745 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1747 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1749 if (CONSP (target))
1750 target = XCDR (target);
1751 if (! CHARSETP (target))
1752 target = Qlatin;
1753 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1754 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1757 FONTSET_ASCII (fontset) = font_name;
1759 return XINT (FONTSET_ID (fontset));
1763 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1764 FONTSET is changed, we delete an entry of FONTSET if any from
1765 auto_fontset_alist so that FONTSET is not re-used by
1766 fontset_from_font. */
1768 static void
1769 update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
1771 Lisp_Object prev, tail;
1773 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1774 prev = tail, tail = XCDR (tail))
1775 if (EQ (fontset, XCDR (XCAR (tail))))
1777 if (NILP (prev))
1778 auto_fontset_alist = XCDR (tail);
1779 else
1780 XSETCDR (prev, XCDR (tail));
1781 break;
1786 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1787 FONT-OBJECT is the font for the character at POSITION in the current
1788 buffer. This is computed from all the text properties and overlays
1789 that apply to POSITION. POSTION may be nil, in which case,
1790 FONT-SPEC is the font for displaying the character CH with the
1791 default face.
1793 GLYPH-CODE is the glyph code in the font to use for the character.
1795 If the 2nd optional arg CH is non-nil, it is a character to check
1796 the font instead of the character at POSITION.
1798 It returns nil in the following cases:
1800 (1) The window system doesn't have a font for the character (thus
1801 it is displayed by an empty box).
1803 (2) The character code is invalid.
1805 (3) If POSITION is not nil, and the current buffer is not displayed
1806 in any window.
1808 In addition, the returned font name may not take into account of
1809 such redisplay engine hooks as what used in jit-lock-mode if
1810 POSITION is currently not visible. */
1813 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1814 doc: /* For internal use only. */)
1815 (Lisp_Object position, Lisp_Object ch)
1817 EMACS_INT pos, pos_byte, dummy;
1818 int face_id;
1819 int c;
1820 struct frame *f;
1821 struct face *face;
1823 if (NILP (position))
1825 CHECK_CHARACTER (ch);
1826 c = XINT (ch);
1827 f = XFRAME (selected_frame);
1828 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1829 pos = -1;
1831 else
1833 Lisp_Object window;
1834 struct window *w;
1836 CHECK_NUMBER_COERCE_MARKER (position);
1837 pos = XINT (position);
1838 if (pos < BEGV || pos >= ZV)
1839 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1840 pos_byte = CHAR_TO_BYTE (pos);
1841 if (NILP (ch))
1842 c = FETCH_CHAR (pos_byte);
1843 else
1845 CHECK_NATNUM (ch);
1846 c = XINT (ch);
1848 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1849 if (NILP (window))
1850 return Qnil;
1851 w = XWINDOW (window);
1852 f = XFRAME (w->frame);
1853 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy,
1854 pos + 100, 0, -1);
1856 if (! CHAR_VALID_P (c, 0))
1857 return Qnil;
1858 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1859 face = FACE_FROM_ID (f, face_id);
1860 if (face->font)
1862 unsigned code = face->font->driver->encode_char (face->font, c);
1863 Lisp_Object font_object;
1864 /* Assignment to EMACS_INT stops GCC whining about limited range
1865 of data type. */
1866 EMACS_INT cod = code;
1868 if (code == FONT_INVALID_CODE)
1869 return Qnil;
1870 XSETFONT (font_object, face->font);
1871 if (cod <= MOST_POSITIVE_FIXNUM)
1872 return Fcons (font_object, make_number (code));
1873 return Fcons (font_object, Fcons (make_number (code >> 16),
1874 make_number (code & 0xFFFF)));
1876 return Qnil;
1880 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1881 doc: /* Return information about a fontset FONTSET on frame FRAME.
1883 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1884 for the default fontset. FRAME nil means the selected frame.
1886 The value is a char-table whose elements have this form:
1888 ((FONT OPENED-FONT ...) ...)
1890 FONT is a name of font specified for a range of characters.
1892 OPENED-FONT is a name of a font actually opened.
1894 The char-table has one extra slot. If FONTSET is not the default
1895 fontset, the value the extra slot is a char-table containing the
1896 information about the derived fonts from the default fontset. The
1897 format is the same as above. */)
1898 (Lisp_Object fontset, Lisp_Object frame)
1900 Lisp_Object *realized[2], fontsets[2], tables[2];
1901 Lisp_Object val, elt;
1902 int c, i, j, k;
1904 (*check_window_system_func) ();
1906 fontset = check_fontset_name (fontset, &frame);
1908 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1909 in the table `realized'. */
1910 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1911 * ASIZE (Vfontset_table));
1912 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1914 elt = FONTSET_FROM_ID (i);
1915 if (!NILP (elt)
1916 && EQ (FONTSET_BASE (elt), fontset)
1917 && EQ (FONTSET_FRAME (elt), frame))
1918 realized[0][j++] = elt;
1920 realized[0][j] = Qnil;
1922 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1923 * ASIZE (Vfontset_table));
1924 for (i = j = 0; ! NILP (realized[0][i]); i++)
1926 elt = FONTSET_DEFAULT (realized[0][i]);
1927 if (! NILP (elt))
1928 realized[1][j++] = elt;
1930 realized[1][j] = Qnil;
1932 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1933 fontsets[0] = fontset;
1934 if (!EQ (fontset, Vdefault_fontset))
1936 tables[1] = Fmake_char_table (Qnil, Qnil);
1937 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
1938 fontsets[1] = Vdefault_fontset;
1941 /* Accumulate information of the fontset in TABLE. The format of
1942 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1943 for (k = 0; k <= 1; k++)
1945 for (c = 0; c <= MAX_CHAR; )
1947 int from = c, to = MAX_5_BYTE_CHAR;
1949 if (c <= MAX_5_BYTE_CHAR)
1951 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1953 else
1955 val = FONTSET_FALLBACK (fontsets[k]);
1956 to = MAX_CHAR;
1958 if (VECTORP (val))
1960 Lisp_Object alist;
1962 /* At first, set ALIST to ((FONT-SPEC) ...). */
1963 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
1964 if (! NILP (AREF (val, i)))
1965 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1966 alist);
1967 alist = Fnreverse (alist);
1969 /* Then store opened font names to cdr of each elements. */
1970 for (i = 0; ! NILP (realized[k][i]); i++)
1972 if (c <= MAX_5_BYTE_CHAR)
1973 val = FONTSET_REF (realized[k][i], c);
1974 else
1975 val = FONTSET_FALLBACK (realized[k][i]);
1976 if (! CONSP (val) || ! VECTORP (XCDR (val)))
1977 continue;
1978 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1979 val = XCDR (val);
1980 for (j = 0; j < ASIZE (val); j++)
1982 elt = AREF (val, j);
1983 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1985 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1986 Lisp_Object slot, name;
1988 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1989 name = AREF (font_object, FONT_NAME_INDEX);
1990 if (NILP (Fmember (name, XCDR (slot))))
1991 nconc2 (slot, Fcons (name, Qnil));
1996 /* Store ALIST in TBL for characters C..TO. */
1997 if (c <= MAX_5_BYTE_CHAR)
1998 char_table_set_range (tables[k], c, to, alist);
1999 else
2000 XCHAR_TABLE (tables[k])->defalt = alist;
2002 /* At last, change each elements to font names. */
2003 for (; CONSP (alist); alist = XCDR (alist))
2005 elt = XCAR (alist);
2006 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
2009 c = to + 1;
2011 if (EQ (fontset, Vdefault_fontset))
2012 break;
2015 return tables[0];
2019 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2020 doc: /* Return a font name pattern for character CH in fontset NAME.
2021 If NAME is t, find a pattern in the default fontset.
2022 If NAME is nil, find a pattern in the fontset of the selected frame.
2024 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2025 family name and REGISTRY is a font registry name. This is actually
2026 the first font name pattern for CH in the fontset or in the default
2027 fontset.
2029 If the 2nd optional arg ALL is non-nil, return a list of all font name
2030 patterns. */)
2031 (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
2033 int c;
2034 Lisp_Object fontset, elt, list, repertory, val;
2035 int i, j;
2036 Lisp_Object frame;
2038 frame = Qnil;
2039 fontset = check_fontset_name (name, &frame);
2041 CHECK_CHARACTER (ch);
2042 c = XINT (ch);
2043 list = Qnil;
2044 while (1)
2046 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2047 i++, elt = FONTSET_FALLBACK (fontset))
2048 if (VECTORP (elt))
2049 for (j = 0; j < ASIZE (elt); j++)
2051 Lisp_Object family, registry;
2053 val = AREF (elt, j);
2054 if (NILP (val))
2055 return Qnil;
2056 repertory = AREF (val, 1);
2057 if (INTEGERP (repertory))
2059 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2061 if (! CHAR_CHARSET_P (c, charset))
2062 continue;
2064 else if (CHAR_TABLE_P (repertory))
2066 if (NILP (CHAR_TABLE_REF (repertory, c)))
2067 continue;
2069 val = AREF (val, 0);
2070 /* VAL is a FONT-SPEC */
2071 family = AREF (val, FONT_FAMILY_INDEX);
2072 if (! NILP (family))
2073 family = SYMBOL_NAME (family);
2074 registry = AREF (val, FONT_REGISTRY_INDEX);
2075 if (! NILP (registry))
2076 registry = SYMBOL_NAME (registry);
2077 val = Fcons (family, registry);
2078 if (NILP (all))
2079 return val;
2080 list = Fcons (val, list);
2082 if (EQ (fontset, Vdefault_fontset))
2083 break;
2084 fontset = Vdefault_fontset;
2086 return (Fnreverse (list));
2089 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2090 doc: /* Return a list of all defined fontset names. */)
2091 (void)
2093 Lisp_Object fontset, list;
2094 int i;
2096 list = Qnil;
2097 for (i = 0; i < ASIZE (Vfontset_table); i++)
2099 fontset = FONTSET_FROM_ID (i);
2100 if (!NILP (fontset)
2101 && BASE_FONTSET_P (fontset))
2102 list = Fcons (FONTSET_NAME (fontset), list);
2105 return list;
2109 #ifdef FONTSET_DEBUG
2111 Lisp_Object
2112 dump_fontset (Lisp_Object fontset)
2114 Lisp_Object vec;
2116 vec = Fmake_vector (make_number (3), Qnil);
2117 ASET (vec, 0, FONTSET_ID (fontset));
2119 if (BASE_FONTSET_P (fontset))
2121 ASET (vec, 1, FONTSET_NAME (fontset));
2123 else
2125 Lisp_Object frame;
2127 frame = FONTSET_FRAME (fontset);
2128 if (FRAMEP (frame))
2130 FRAME_PTR f = XFRAME (frame);
2132 if (FRAME_LIVE_P (f))
2133 ASET (vec, 1,
2134 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2135 else
2136 ASET (vec, 1,
2137 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2139 if (!NILP (FONTSET_DEFAULT (fontset)))
2140 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2142 return vec;
2145 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2146 doc: /* Return a brief summary of all fontsets for debug use. */)
2147 (void)
2149 Lisp_Object val;
2150 int i;
2152 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2153 if (! NILP (AREF (Vfontset_table, i)))
2154 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2155 return (Fnreverse (val));
2157 #endif /* FONTSET_DEBUG */
2159 void
2160 syms_of_fontset (void)
2162 DEFSYM (Qfontset, "fontset");
2163 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2164 DEFSYM (Qfontset_info, "fontset-info");
2165 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2167 DEFSYM (Qprepend, "prepend");
2168 DEFSYM (Qappend, "append");
2169 DEFSYM (Qlatin, "latin");
2171 Vcached_fontset_data = Qnil;
2172 staticpro (&Vcached_fontset_data);
2174 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2175 staticpro (&Vfontset_table);
2177 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2178 staticpro (&Vdefault_fontset);
2179 FONTSET_ID (Vdefault_fontset) = make_number (0);
2180 FONTSET_NAME (Vdefault_fontset)
2181 = make_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2182 ASET (Vfontset_table, 0, Vdefault_fontset);
2183 next_fontset_id = 1;
2185 auto_fontset_alist = Qnil;
2186 staticpro (&auto_fontset_alist);
2188 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist,
2189 doc: /*
2190 Alist of charsets vs the charsets to determine the preferred font encoding.
2191 Each element looks like (CHARSET . ENCODING-CHARSET),
2192 where ENCODING-CHARSET is a charset registered in the variable
2193 `font-encoding-alist' as ENCODING.
2195 When a text has a property `charset' and the value is CHARSET, a font
2196 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2197 Vfont_encoding_charset_alist = Qnil;
2199 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent,
2200 doc: /*
2201 Char table of characters whose ascent values should be ignored.
2202 If an entry for a character is non-nil, the ascent value of the glyph
2203 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2205 This affects how a composite character which contains
2206 such a character is displayed on screen. */);
2207 Vuse_default_ascent = Qnil;
2209 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition,
2210 doc: /*
2211 Char table of characters which are not composed relatively.
2212 If an entry for a character is non-nil, a composition sequence
2213 which contains that character is displayed so that
2214 the glyph of that character is put without considering
2215 an ascent and descent value of a previous character. */);
2216 Vignore_relative_composition = Qnil;
2218 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist,
2219 doc: /* Alist of fontname vs list of the alternate fontnames.
2220 When a specified font name is not found, the corresponding
2221 alternate fontnames (if any) are tried instead. */);
2222 Valternate_fontname_alist = Qnil;
2224 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist,
2225 doc: /* Alist of fontset names vs the aliases. */);
2226 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2227 make_pure_c_string ("fontset-default")),
2228 Qnil);
2230 DEFVAR_LISP ("vertical-centering-font-regexp",
2231 Vvertical_centering_font_regexp,
2232 doc: /* *Regexp matching font names that require vertical centering on display.
2233 When a character is displayed with such fonts, the character is displayed
2234 at the vertical center of lines. */);
2235 Vvertical_centering_font_regexp = Qnil;
2237 DEFVAR_LISP ("otf-script-alist", Votf_script_alist,
2238 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2239 Votf_script_alist = Qnil;
2241 defsubr (&Squery_fontset);
2242 defsubr (&Snew_fontset);
2243 defsubr (&Sset_fontset_font);
2244 defsubr (&Sinternal_char_font);
2245 defsubr (&Sfontset_info);
2246 defsubr (&Sfontset_font);
2247 defsubr (&Sfontset_list);
2248 #ifdef FONTSET_DEBUG
2249 defsubr (&Sfontset_list_all);
2250 #endif