(vc-bzr-diff): Obey vc-disable-async-diff.
[bpt/emacs.git] / src / fontset.c
CommitLineData
4ed46869 1/* Fontset handler.
76b6f707 2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
429ab54e 3 Free Software Foundation, Inc.
7976eda0 4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
76b6f707 5 2005, 2006, 2007, 2008, 2009
ce03bf76
KH
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
8aa07a8d 8 Copyright (C) 2003, 2006
06f76f0d
KH
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
eb152aa9 11
369314dc
KH
12This file is part of GNU Emacs.
13
9ec0b715 14GNU Emacs is free software: you can redistribute it and/or modify
369314dc 15it under the terms of the GNU General Public License as published by
9ec0b715
GM
16the Free Software Foundation, either version 3 of the License, or
17(at your option) any later version.
4ed46869 18
369314dc
KH
19GNU Emacs is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
4ed46869 23
369314dc 24You should have received a copy of the GNU General Public License
9ec0b715 25along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
4ed46869 26
0d407d77
KH
27/* #define FONTSET_DEBUG */
28
4ed46869 29#include <config.h>
0d407d77 30#include <stdio.h>
d7306fe6 31#include <setjmp.h>
0d407d77 32
4ed46869 33#include "lisp.h"
06f76f0d 34#include "blockinput.h"
1ff005e1 35#include "buffer.h"
06f76f0d 36#include "character.h"
4ed46869
KH
37#include "charset.h"
38#include "ccl.h"
2538fae4 39#include "keyboard.h"
4ed46869 40#include "frame.h"
0d407d77 41#include "dispextern.h"
e1a14cdc 42#include "intervals.h"
3541bb8f 43#include "fontset.h"
0d407d77 44#include "window.h"
92c15c34
KH
45#ifdef HAVE_X_WINDOWS
46#include "xterm.h"
47#endif
48#ifdef WINDOWSNT
49#include "w32term.h"
50#endif
edfda783
AR
51#ifdef HAVE_NS
52#include "nsterm.h"
53#endif
fa971ac3 54#include "termhooks.h"
0d407d77 55
8aa07a8d 56#include "font.h"
8aa07a8d 57
0d407d77 58#undef xassert
8f924df7 59#ifdef FONTSET_DEBUG
0d407d77
KH
60#define xassert(X) do {if (!(X)) abort ();} while (0)
61#undef INLINE
62#define INLINE
8f924df7
KH
63#else /* not FONTSET_DEBUG */
64#define xassert(X) (void) 0
65#endif /* not FONTSET_DEBUG */
0d407d77 66
a980c932 67EXFUN (Fclear_face_cache, 1);
0d407d77
KH
68
69/* FONTSET
70
71 A fontset is a collection of font related information to give
1d5d7200
KH
72 similar appearance (style, etc) of characters. A fontset has two
73 roles. One is to use for the frame parameter `font' as if it is an
74 ASCII font. In that case, Emacs uses the font specified for
75 `ascii' script for the frame's default font.
76
77 Another role, the more important one, is to provide information
78 about which font to use for each non-ASCII character.
79
80 There are two kinds of fontsets; base and realized. A base fontset
81 is created by `new-fontset' from Emacs Lisp explicitly. A realized
82 fontset is created implicitly when a face is realized for ASCII
83 characters. A face is also realized for non-ASCII characters based
84 on an ASCII face. All of non-ASCII faces based on the same ASCII
85 face share the same realized fontset.
8f924df7 86
06f76f0d
KH
87 A fontset object is implemented by a char-table whose default value
88 and parent are always nil.
fc8865fc 89
1d5d7200
KH
90 An element of a base fontset is a vector of FONT-DEFs which itself
91 is a vector [ FONT-SPEC ENCODING REPERTORY ].
92
f088b054
KH
93 An element of a realized fontset is nil, t, 0, or a vector of this
94 form:
7e1a1cd9 95
581f7ac7
KH
96 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
97 RFONT-DEF0 RFONT-DEF1 ... ]
0d407d77 98
7e1a1cd9 99 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
0d407d77 100
581f7ac7 101 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
0d407d77 102
581f7ac7 103 RFONT-DEFn are automatically reordered by the current charset
1d5d7200 104 priority list.
0d407d77 105
7e1a1cd9
KH
106 The value nil means that we have not yet generated the above vector
107 from the base of the fontset.
0d407d77 108
1d5d7200
KH
109 The value t means that no font is available for the corresponding
110 range of characters.
0d407d77 111
f088b054
KH
112 The value 0 means that no font is available for the corresponding
113 range of characters in this fontset, but may be available in the
114 default fontset.
115
0d407d77 116
eb36588a 117 A fontset has 9 extra slots.
0d407d77 118
1d5d7200 119 The 1st slot: the ID number of the fontset
0d407d77 120
1d5d7200
KH
121 The 2nd slot:
122 base: the name of the fontset
123 realized: nil
0d407d77 124
1d5d7200 125 The 3rd slot:
d6aaac9e 126 base: nil
1d5d7200 127 realized: the base fontset
06f76f0d 128
1d5d7200
KH
129 The 4th slot:
130 base: nil
131 realized: the frame that the fontset belongs to
0d407d77 132
1d5d7200
KH
133 The 5th slot:
134 base: the font name for ASCII characters
135 realized: nil
0d407d77 136
1d5d7200
KH
137 The 6th slot:
138 base: nil
139 realized: the ID number of a face to use for characters that
140 has no font in a realized fontset.
0d407d77 141
1d5d7200
KH
142 The 7th slot:
143 base: nil
144 realized: Alist of font index vs the corresponding repertory
145 char-table.
eb152aa9 146
d6aaac9e
KH
147 The 8th slot:
148 base: nil
149 realized: If the base is not the default fontset, a fontset
150 realized from the default fontset, else nil.
1d5d7200 151
eb36588a 152 The 9th slot:
cc7b6145
KH
153 base: Same as element value (but for fallback fonts).
154 realized: Likewise.
eb36588a 155
1d5d7200 156 All fontsets are recorded in the vector Vfontset_table.
0d407d77
KH
157
158
159 DEFAULT FONTSET
160
1d5d7200
KH
161 There's a special base fontset named `default fontset' which
162 defines the default font specifications. When a base fontset
163 doesn't specify a font for a specific character, the corresponding
164 value in the default fontset is used.
0d407d77 165
afe93d01
KH
166 The parent of a realized fontset created for such a face that has
167 no fontset is the default fontset.
0d407d77
KH
168
169
170 These structures are hidden from the other codes than this file.
171 The other codes handle fontsets only by their ID numbers. They
06f76f0d
KH
172 usually use the variable name `fontset' for IDs. But, in this
173 file, we always use varialbe name `id' for IDs, and name `fontset'
1d5d7200 174 for an actual fontset object, i.e., char-table.
0d407d77
KH
175
176*/
177
178/********** VARIABLES and FUNCTION PROTOTYPES **********/
179
180extern Lisp_Object Qfont;
d6aaac9e
KH
181static Lisp_Object Qfontset;
182static Lisp_Object Qfontset_info;
1d5d7200 183static Lisp_Object Qprepend, Qappend;
ef6e0694 184Lisp_Object Qlatin;
0d407d77
KH
185
186/* Vector containing all fontsets. */
187static Lisp_Object Vfontset_table;
188
fc8865fc 189/* Next possibly free fontset ID. Usually this keeps the minimum
0d407d77
KH
190 fontset ID not yet used. */
191static int next_fontset_id;
192
193/* The default fontset. This gives default FAMILY and REGISTRY of
06f76f0d 194 font for each character. */
0d407d77 195static Lisp_Object Vdefault_fontset;
4ed46869 196
28e2436a 197Lisp_Object Vfont_encoding_charset_alist;
6a7e6d80 198Lisp_Object Vuse_default_ascent;
2aeafb78 199Lisp_Object Vignore_relative_composition;
01d4b817 200Lisp_Object Valternate_fontname_alist;
1c283e35 201Lisp_Object Vfontset_alias_alist;
810abb87 202Lisp_Object Vvertical_centering_font_regexp;
21ff5ed6 203Lisp_Object Votf_script_alist;
4ed46869 204
4ed46869 205/* Check if any window system is used now. */
5771dcf4 206void (*check_window_system_func) P_ ((void));
4ed46869 207
0d407d77
KH
208
209/* Prototype declarations for static functions. */
b11a4ed7
DL
210static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
211 Lisp_Object));
581f7ac7
KH
212static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
213 int, int));
41ad6003 214static void reorder_font_vector P_ ((Lisp_Object, struct font *));
7e1a1cd9 215static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
0d407d77 216static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
0d407d77 217static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
1d5d7200
KH
218static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
219 Lisp_Object));
8aa07a8d 220Lisp_Object find_font_encoding P_ ((Lisp_Object));
0d407d77 221
2449d4d0
KH
222static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
223
556383ac
KH
224#ifdef FONTSET_DEBUG
225
226/* Return 1 if ID is a valid fontset id, else return 0. */
227
228static int
229fontset_id_valid_p (id)
230 int id;
231{
232 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
233}
234
235#endif
236
0d407d77
KH
237
238\f
239/********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
240
0d407d77
KH
241/* Return the fontset with ID. No check of ID's validness. */
242#define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
243
afe93d01 244/* Macros to access special values of FONTSET. */
0d407d77 245#define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
06f76f0d
KH
246
247/* Macros to access special values of (base) FONTSET. */
0d407d77 248#define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
06f76f0d 249#define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
581f7ac7 250#define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5]
06f76f0d 251
06f76f0d
KH
252/* Macros to access special values of (realized) FONTSET. */
253#define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
254#define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
9459b1e9 255#define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4]
1d5d7200
KH
256#define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
257#define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
eb36588a
KH
258#define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
259
cc7b6145 260/* For both base and realized fontset. */
eb36588a 261#define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
0d407d77 262
e3400864 263#define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
0d407d77
KH
264
265
581f7ac7
KH
266/* Macros for FONT-DEF and RFONT-DEF of fontset. */
267#define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
268 do { \
269 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
270 ASET ((font_def), 1, encoding); \
271 ASET ((font_def), 2, repertory); \
272 } while (0)
273
274#define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
275#define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
276#define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
277
278#define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
279#define RFONT_DEF_SET_FACE(rfont_def, face_id) \
280 ASET ((rfont_def), 0, make_number (face_id))
281#define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
282#define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
283#define RFONT_DEF_REPERTORY(rfont_def) FONT_DEF_REPERTORY (AREF (rfont_def, 1))
284#define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
285#define RFONT_DEF_SET_OBJECT(rfont_def, object) \
286 ASET ((rfont_def), 2, (object))
287#define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
288#define RFONT_DEF_SET_SCORE(rfont_def, score) \
289 ASET ((rfont_def), 3, make_number (score))
290#define RFONT_DEF_NEW(rfont_def, font_def) \
291 do { \
292 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
293 ASET ((rfont_def), 1, (font_def)); \
294 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
295 } while (0)
296
297
1d5d7200
KH
298/* Return the element of FONTSET for the character C. If FONTSET is a
299 base fontset other then the default fontset and FONTSET doesn't
300 contain information for C, return the information in the default
301 fontset. */
0d407d77 302
1d5d7200
KH
303#define FONTSET_REF(fontset, c) \
304 (EQ (fontset, Vdefault_fontset) \
305 ? CHAR_TABLE_REF (fontset, c) \
306 : fontset_ref ((fontset), (c)))
0d407d77 307
afe93d01 308static Lisp_Object
0d407d77
KH
309fontset_ref (fontset, c)
310 Lisp_Object fontset;
311 int c;
312{
06f76f0d
KH
313 Lisp_Object elt;
314
1d5d7200
KH
315 elt = CHAR_TABLE_REF (fontset, c);
316 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
317 /* Don't check Vdefault_fontset for a realized fontset. */
318 && NILP (FONTSET_BASE (fontset)))
319 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
0d407d77
KH
320 return elt;
321}
322
1d5d7200
KH
323/* Set elements of FONTSET for characters in RANGE to the value ELT.
324 RANGE is a cons (FROM . TO), where FROM and TO are character codes
325 specifying a range. */
326
327#define FONTSET_SET(fontset, range, elt) \
328 Fset_char_table_range ((fontset), (range), (elt))
329
0d407d77 330
1d5d7200 331/* Modify the elements of FONTSET for characters in RANGE by replacing
6bad8007 332 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
1d5d7200
KH
333 and TO are character codes specifying a range. If ADD is nil,
334 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
335 append ELT. */
336
cc7b6145
KH
337#define FONTSET_ADD(fontset, range, elt, add) \
338 (NILP (add) \
339 ? (NILP (range) \
340 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
341 : Fset_char_table_range ((fontset), (range), \
342 Fmake_vector (make_number (1), (elt)))) \
1d5d7200 343 : fontset_add ((fontset), (range), (elt), (add)))
0d407d77 344
b11a4ed7 345static Lisp_Object
1d5d7200 346fontset_add (fontset, range, elt, add)
00c4da0f 347 Lisp_Object fontset, range, elt, add;
06f76f0d 348{
eb36588a
KH
349 Lisp_Object args[2];
350 int idx = (EQ (add, Qappend) ? 0 : 1);
351
352 args[1 - idx] = Fmake_vector (make_number (1), elt);
353
354 if (CONSP (range))
355 {
356 int from = XINT (XCAR (range));
357 int to = XINT (XCDR (range));
358 int from1, to1;
359
360 do {
5b30be1a 361 from1 = from, to1 = to;
eb36588a 362 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
eb36588a
KH
363 char_table_set_range (fontset, from, to1,
364 NILP (args[idx]) ? args[1 - idx]
365 : Fvconcat (2, args));
366 from = to1 + 1;
367 } while (from < to);
368 }
369 else
370 {
371 args[idx] = FONTSET_FALLBACK (fontset);
372 FONTSET_FALLBACK (fontset)
373 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
374 }
b11a4ed7 375 return Qnil;
1d5d7200
KH
376}
377
581f7ac7
KH
378static int
379fontset_compare_rfontdef (val1, val2)
380 const void *val1, *val2;
381{
41ad6003
KH
382 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
383 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
581f7ac7 384}
1d5d7200 385
ea0162a6 386/* Update FONT-GROUP which has this form:
581f7ac7
KH
387 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
388 RFONT-DEF0 RFONT-DEF1 ... ]
20746fe4 389 Reorder RFONT-DEFs according to the current language, and update
581f7ac7
KH
390 CHARSET-ORDERED-LIST-TICK.
391
392 If PREFERRED_FAMILY is not nil, that family has the higher priority
393 if the encoding charsets or languages in font-specs are the same. */
394
395extern Lisp_Object Fassoc_string ();
0d407d77
KH
396
397static void
41ad6003 398reorder_font_vector (font_group, font)
ea0162a6 399 Lisp_Object font_group;
41ad6003 400 struct font *font;
1d5d7200 401{
41ad6003 402 Lisp_Object vec, font_object;
1d5d7200 403 int size;
581f7ac7
KH
404 int i;
405 int score_changed = 0;
1d5d7200 406
41ad6003
KH
407 if (font)
408 XSETFONT (font_object, font);
409 else
410 font_object = Qnil;
411
412 vec = XCDR (font_group);
413 size = ASIZE (vec);
414 /* Exclude the tailing nil element from the reordering. */
415 if (NILP (AREF (vec, size - 1)))
416 size--;
581f7ac7
KH
417
418 for (i = 0; i < size; i++)
6bad8007 419 {
41ad6003 420 Lisp_Object rfont_def = AREF (vec, i);
581f7ac7
KH
421 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
422 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
41ad6003 423 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
581f7ac7 424
41ad6003 425 if (! font_match_p (font_spec, font_object))
eb152aa9 426 {
cd872f08 427 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
581f7ac7 428
cd872f08 429 if (! NILP (encoding))
41ad6003
KH
430 {
431 Lisp_Object tail;
581f7ac7 432
41ad6003
KH
433 for (tail = Vcharset_ordered_list;
434 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
435 score += 0x100, tail = XCDR (tail))
cd872f08 436 if (EQ (encoding, XCAR (tail)))
41ad6003
KH
437 break;
438 }
439 else
440 {
441 Lisp_Object lang = Ffont_get (font_spec, QClang);
442
443 if (! NILP (lang)
444 && ! EQ (lang, Vcurrent_iso639_language)
445 && (! CONSP (Vcurrent_iso639_language)
446 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
447 score |= 0x100;
448 }
ea0162a6 449 }
581f7ac7 450 if (RFONT_DEF_SCORE (rfont_def) != score)
ea0162a6 451 {
581f7ac7
KH
452 RFONT_DEF_SET_SCORE (rfont_def, score);
453 score_changed = 1;
ea0162a6 454 }
6bad8007 455 }
1d5d7200 456
581f7ac7 457 if (score_changed)
41ad6003 458 qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
581f7ac7 459 fontset_compare_rfontdef);
41ad6003
KH
460 XSETCAR (font_group, make_number (charset_ordered_list_tick));
461}
462
f084f942
KH
463/* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
464 character C in FONTSET. If C is -1, return a fallback font-group.
465 If C is not -1, the value may be Qt (FONTSET doesn't have a font
466 for C even in the fallback group, or 0 (a font for C may be found
467 only in the fallback group). */
468
41ad6003 469static Lisp_Object
3cba9369 470fontset_get_font_group (Lisp_Object fontset, int c)
41ad6003
KH
471{
472 Lisp_Object font_group;
473 Lisp_Object base_fontset;
5b30be1a 474 int from = 0, to = MAX_CHAR, i;
28e2436a 475
41ad6003
KH
476 xassert (! BASE_FONTSET_P (fontset));
477 if (c >= 0)
478 font_group = CHAR_TABLE_REF (fontset, c);
479 else
480 font_group = FONTSET_FALLBACK (fontset);
481 if (! NILP (font_group))
482 return font_group;
483 base_fontset = FONTSET_BASE (fontset);
484 if (c >= 0)
485 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
486 else
487 font_group = FONTSET_FALLBACK (base_fontset);
488 if (NILP (font_group))
f088b054 489 {
f084f942 490 font_group = make_number (0);
f088b054 491 if (c >= 0)
f084f942
KH
492 char_table_set_range (fontset, from, to, font_group);
493 return font_group;
f088b054 494 }
41ad6003
KH
495 font_group = Fcopy_sequence (font_group);
496 for (i = 0; i < ASIZE (font_group); i++)
497 if (! NILP (AREF (font_group, i)))
498 {
499 Lisp_Object rfont_def;
500
501 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
502 /* Remember the original order. */
503 RFONT_DEF_SET_SCORE (rfont_def, i);
504 ASET (font_group, i, rfont_def);
505 }
506 font_group = Fcons (make_number (-1), font_group);
507 if (c >= 0)
508 char_table_set_range (fontset, from, to, font_group);
509 else
510 FONTSET_FALLBACK (fontset) = font_group;
511 return font_group;
0d407d77
KH
512}
513
7e1a1cd9 514/* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
d67f7f1a
KH
515 character C. If no font is found, return Qnil if there's a
516 possibility that the default fontset or the fallback font groups
517 have a proper font, and return Qt if not.
518
519 If a font is found but is not yet opened, open it (if FACE is not
520 NULL) or return Qnil (if FACE is NULL).
521
1c82b4b3
KH
522 ID is a charset-id that must be preferred, or -1 meaning no
523 preference.
d67f7f1a 524
ea0162a6 525 If FALLBACK is nonzero, search only fallback fonts. */
0d407d77 526
7e1a1cd9 527static Lisp_Object
1c82b4b3 528fontset_find_font (fontset, c, face, id, fallback)
0d407d77
KH
529 Lisp_Object fontset;
530 int c;
1d5d7200 531 struct face *face;
1c82b4b3 532 int id, fallback;
0d407d77 533{
162f5458 534 Lisp_Object vec, font_group;
374bf7e4 535 int i, charset_matched = 0, found_index;
f3d29551
CY
536 FRAME_PTR f = (FRAMEP (FONTSET_FRAME (fontset)))
537 ? XFRAME (selected_frame) : XFRAME (FONTSET_FRAME (fontset));
a71ccf3a 538 Lisp_Object rfont_def;
0d407d77 539
41ad6003
KH
540 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
541 if (! CONSP (font_group))
bf86d09c 542 return font_group;
41ad6003
KH
543 vec = XCDR (font_group);
544 if (ASIZE (vec) == 0)
545 return Qnil;
eaa0402c 546
41ad6003 547 if (ASIZE (vec) > 1)
1d5d7200 548 {
41ad6003
KH
549 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
550 /* We have just created the font-group,
551 or the charset priorities were changed. */
552 reorder_font_vector (font_group, face->ascii_face->font);
553 if (id >= 0)
554 /* Find a spec matching with the charset ID to try at
555 first. */
556 for (i = 0; i < ASIZE (vec); i++)
557 {
0ff75233
KH
558 Lisp_Object repertory;
559
a71ccf3a 560 rfont_def = AREF (vec, i);
0ff75233
KH
561 if (NILP (rfont_def))
562 break;
563 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
41ad6003
KH
564
565 if (XINT (repertory) == id)
eaa0402c 566 {
41ad6003 567 charset_matched = i;
581f7ac7 568 break;
41ad6003
KH
569 }
570 }
581f7ac7 571 }
1d5d7200 572
7e1a1cd9 573 /* Find the first available font in the vector of RFONT-DEF. */
41ad6003 574 for (i = 0; i < ASIZE (vec); i++)
0d407d77 575 {
a71ccf3a 576 Lisp_Object font_def;
dbf46ba6 577 Lisp_Object font_entity, font_object;
581f7ac7 578
635c75b1 579 found_index = i;
374bf7e4 580 if (i == 0)
41ad6003 581 {
374bf7e4
KH
582 if (charset_matched > 0)
583 {
635c75b1
KH
584 /* Try the element matching with the charset ID at first. */
585 found_index = charset_matched;
586 /* Make this negative so that we don't come here in the
587 next loop. */
374bf7e4 588 charset_matched = - charset_matched;
635c75b1 589 /* We must try the first element in the next loop. */
374bf7e4
KH
590 i--;
591 }
41ad6003 592 }
635c75b1 593 else if (i == - charset_matched)
374bf7e4
KH
594 {
595 /* We have already tried this element and the followings
635c75b1
KH
596 that have the same font specifications in the first
597 iteration. So, skip them all. */
374bf7e4
KH
598 rfont_def = AREF (vec, i);
599 font_def = RFONT_DEF_FONT_DEF (rfont_def);
600 for (; i + 1 < ASIZE (vec); i++)
601 {
602 rfont_def = AREF (vec, i + 1);
603 if (NILP (rfont_def))
604 break;
605 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
606 break;
607 }
608 continue;
609 }
41ad6003 610
374bf7e4 611 rfont_def = AREF (vec, found_index);
162f5458 612 if (NILP (rfont_def))
374bf7e4 613 {
635c75b1 614 if (i < 0)
374bf7e4
KH
615 continue;
616 /* This is a sign of not to try the other fonts. */
617 return Qt;
618 }
162f5458
KH
619 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
620 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
1d5d7200
KH
621 /* We couldn't open this font last time. */
622 continue;
623
162f5458 624 font_object = RFONT_DEF_OBJECT (rfont_def);
581f7ac7 625 if (NILP (font_object))
8aa07a8d 626 {
162f5458 627 font_def = RFONT_DEF_FONT_DEF (rfont_def);
d67f7f1a 628
581f7ac7
KH
629 if (! face)
630 /* We have not yet opened the font. */
631 return Qnil;
162f5458
KH
632 /* Find a font best-matching with the spec without checking
633 the support of the character C. That checking is costly,
634 and even without the checking, the found font supports C
635 in high possibility. */
581f7ac7
KH
636 font_entity = font_find_for_lface (f, face->lface,
637 FONT_DEF_SPEC (font_def), -1);
d67f7f1a 638 if (NILP (font_entity))
8b67c40a 639 {
581f7ac7 640 /* Record that no font matches the spec. */
162f5458 641 RFONT_DEF_SET_FACE (rfont_def, -1);
581f7ac7 642 continue;
d67f7f1a 643 }
ca2b2cde
KH
644 font_object = font_open_for_lface (f, font_entity, face->lface,
645 FONT_DEF_SPEC (font_def));
581f7ac7 646 if (NILP (font_object))
d67f7f1a 647 {
162f5458
KH
648 /* Something strange happened, perhaps because of a
649 Font-backend problem. Too avoid crashing, record
650 that this spec is unsable. It may be better to find
651 another font of the same spec, but currently we don't
652 have such an API. */
653 RFONT_DEF_SET_FACE (rfont_def, -1);
581f7ac7 654 continue;
8aa07a8d 655 }
162f5458 656 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
8aa07a8d 657 }
eb152aa9 658
581f7ac7 659 if (font_has_char (f, font_object, c))
a71ccf3a 660 goto found;
581f7ac7 661
162f5458
KH
662 /* Find a font already opened, maching with the current spec,
663 and supporting C. */
664 font_def = RFONT_DEF_FONT_DEF (rfont_def);
374bf7e4 665 for (; found_index + 1 < ASIZE (vec); found_index++)
57e13af9 666 {
374bf7e4 667 rfont_def = AREF (vec, found_index + 1);
7d72bb82 668 if (NILP (rfont_def))
374bf7e4 669 break;
162f5458 670 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
581f7ac7 671 break;
374bf7e4 672 font_object = RFONT_DEF_OBJECT (rfont_def);
69a8e484 673 if (! NILP (font_object) && font_has_char (f, font_object, c))
374bf7e4
KH
674 {
675 found_index++;
676 goto found;
677 }
57e13af9 678 }
162f5458
KH
679
680 /* Find a font-entity with the current spec and supporting C. */
581f7ac7
KH
681 font_entity = font_find_for_lface (f, face->lface,
682 FONT_DEF_SPEC (font_def), c);
683 if (! NILP (font_entity))
1d5d7200 684 {
162f5458
KH
685 /* We found a font. Open it and insert a new element for
686 that font in VEC. */
687 Lisp_Object new_vec;
581f7ac7 688 int j;
1d5d7200 689
581f7ac7
KH
690 font_object = font_open_for_lface (f, font_entity, face->lface,
691 Qnil);
162f5458
KH
692 if (NILP (font_object))
693 continue;
581f7ac7
KH
694 RFONT_DEF_NEW (rfont_def, font_def);
695 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
162f5458 696 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
581f7ac7 697 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
374bf7e4
KH
698 found_index++;
699 for (j = 0; j < found_index; j++)
581f7ac7
KH
700 ASET (new_vec, j, AREF (vec, j));
701 ASET (new_vec, j, rfont_def);
702 for (j++; j < ASIZE (new_vec); j++)
703 ASET (new_vec, j, AREF (vec, j - 1));
162f5458 704 XSETCDR (font_group, new_vec);
a71ccf3a
KH
705 vec = new_vec;
706 goto found;
1d5d7200 707 }
374bf7e4
KH
708 if (i >= 0)
709 i = found_index;
0d407d77 710 }
d67f7f1a 711
581f7ac7 712 FONTSET_SET (fontset, make_number (c), make_number (0));
eaa0402c 713 return Qnil;
a71ccf3a
KH
714
715 found:
716 if (fallback && found_index > 0)
717 {
718 /* The order of fonts in the fallback font-group is not that
719 important, and it is better to move the found font to the
720 first of the group so that the next try will find it
721 quickly. */
722 for (i = found_index; i > 0; i--)
723 ASET (vec, i, AREF (vec, i - 1));
724 ASET (vec, 0, rfont_def);
725 }
726 return rfont_def;
1c82b4b3 727}
0d407d77 728
cc7b6145 729
1c82b4b3
KH
730static Lisp_Object
731fontset_font (fontset, c, face, id)
732 Lisp_Object fontset;
733 int c;
734 struct face *face;
735 int id;
736{
f084f942 737 Lisp_Object rfont_def, default_rfont_def;
1c82b4b3 738 Lisp_Object base_fontset;
cc7b6145 739
41ad6003 740 /* Try a font-group of FONTSET. */
b23bf85a 741 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
1c82b4b3 742 rfont_def = fontset_find_font (fontset, c, face, id, 0);
eaa0402c
KH
743 if (VECTORP (rfont_def))
744 return rfont_def;
f084f942
KH
745 if (NILP (rfont_def))
746 FONTSET_SET (fontset, make_number (c), make_number (0));
41ad6003
KH
747
748 /* Try a font-group of the default fontset. */
749 base_fontset = FONTSET_BASE (fontset);
750 if (! EQ (base_fontset, Vdefault_fontset))
751 {
752 if (NILP (FONTSET_DEFAULT (fontset)))
753 FONTSET_DEFAULT (fontset)
754 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
b23bf85a 755 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
f084f942
KH
756 default_rfont_def
757 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
758 if (VECTORP (default_rfont_def))
759 return default_rfont_def;
760 if (NILP (default_rfont_def))
761 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
762 make_number (0));
41ad6003
KH
763 }
764
765 /* Try a fallback font-group of FONTSET. */
f084f942
KH
766 if (! EQ (rfont_def, Qt))
767 {
768 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
769 rfont_def = fontset_find_font (fontset, c, face, id, 1);
770 if (VECTORP (rfont_def))
771 return rfont_def;
772 /* Remember that FONTSET has no font for C. */
773 FONTSET_SET (fontset, make_number (c), Qt);
774 }
d67f7f1a 775
f084f942
KH
776 /* Try a fallback font-group of the default fontset. */
777 if (! EQ (base_fontset, Vdefault_fontset)
778 && ! EQ (default_rfont_def, Qt))
41ad6003 779 {
b23bf85a 780 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
41ad6003
KH
781 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
782 if (VECTORP (rfont_def))
783 return rfont_def;
f084f942
KH
784 /* Remember that the default fontset has no font for C. */
785 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
41ad6003
KH
786 }
787
41ad6003 788 return Qnil;
0d407d77
KH
789}
790
0d407d77 791/* Return a newly created fontset with NAME. If BASE is nil, make a
06f76f0d 792 base fontset. Otherwise make a realized fontset whose base is
0d407d77
KH
793 BASE. */
794
795static Lisp_Object
796make_fontset (frame, name, base)
797 Lisp_Object frame, name, base;
4ed46869 798{
1337ac77 799 Lisp_Object fontset;
0d407d77
KH
800 int size = ASIZE (Vfontset_table);
801 int id = next_fontset_id;
0d407d77
KH
802
803 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
804 the next available fontset ID. So it is expected that this loop
805 terminates quickly. In addition, as the last element of
fc8865fc 806 Vfontset_table is always nil, we don't have to check the range of
0d407d77
KH
807 id. */
808 while (!NILP (AREF (Vfontset_table, id))) id++;
809
810 if (id + 1 == size)
63655c83 811 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
4ed46869 812
11d9bd93 813 fontset = Fmake_char_table (Qfontset, Qnil);
0d407d77
KH
814
815 FONTSET_ID (fontset) = make_number (id);
06f76f0d
KH
816 if (NILP (base))
817 {
818 FONTSET_NAME (fontset) = name;
819 }
820 else
821 {
822 FONTSET_NAME (fontset) = Qnil;
823 FONTSET_FRAME (fontset) = frame;
824 FONTSET_BASE (fontset) = base;
825 }
0d407d77 826
06f76f0d 827 ASET (Vfontset_table, id, fontset);
0d407d77
KH
828 next_fontset_id = id + 1;
829 return fontset;
4ed46869
KH
830}
831
0d407d77 832\f
1d5d7200 833/********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
0d407d77 834
1d5d7200 835/* Return the name of the fontset who has ID. */
0d407d77
KH
836
837Lisp_Object
838fontset_name (id)
839 int id;
840{
841 Lisp_Object fontset;
06f76f0d 842
0d407d77
KH
843 fontset = FONTSET_FROM_ID (id);
844 return FONTSET_NAME (fontset);
845}
846
847
1d5d7200 848/* Return the ASCII font name of the fontset who has ID. */
0d407d77
KH
849
850Lisp_Object
851fontset_ascii (id)
852 int id;
853{
854 Lisp_Object fontset, elt;
06f76f0d 855
0d407d77
KH
856 fontset= FONTSET_FROM_ID (id);
857 elt = FONTSET_ASCII (fontset);
8aa07a8d
KH
858 if (CONSP (elt))
859 elt = XCAR (elt);
1d5d7200 860 return elt;
0d407d77
KH
861}
862
9459b1e9
KH
863void
864free_realized_fontset (f, fontset)
865 FRAME_PTR f;
866 Lisp_Object fontset;
867{
9459b1e9
KH
868 Lisp_Object tail;
869
870 return;
871 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
872 {
873 xassert (FONT_OBJECT_P (XCAR (tail)));
874 font_close_object (f, XCAR (tail));
875 }
876}
0d407d77 877
06f76f0d
KH
878/* Free fontset of FACE defined on frame F. Called from
879 free_realized_face. */
0d407d77 880
4ed46869 881void
0d407d77
KH
882free_face_fontset (f, face)
883 FRAME_PTR f;
884 struct face *face;
4ed46869 885{
452a78e0
KH
886 Lisp_Object fontset;
887
ff7a2223 888 fontset = FONTSET_FROM_ID (face->fontset);
581f7ac7
KH
889 if (NILP (fontset))
890 return;
891 xassert (! BASE_FONTSET_P (fontset));
452a78e0 892 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
9459b1e9 893 free_realized_fontset (f, fontset);
1d5d7200 894 ASET (Vfontset_table, face->fontset, Qnil);
06f76f0d
KH
895 if (face->fontset < next_fontset_id)
896 next_fontset_id = face->fontset;
eb36588a 897 if (! NILP (FONTSET_DEFAULT (fontset)))
452a78e0 898 {
e1a14cdc 899 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
eb152aa9 900
452a78e0
KH
901 fontset = AREF (Vfontset_table, id);
902 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
903 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
9459b1e9 904 free_realized_fontset (f, fontset);
452a78e0
KH
905 ASET (Vfontset_table, id, Qnil);
906 if (id < next_fontset_id)
907 next_fontset_id = face->fontset;
908 }
581f7ac7 909 face->fontset = -1;
0d407d77 910}
18998710 911
0d407d77 912
e0f24100 913/* Return 1 if FACE is suitable for displaying character C.
0d407d77 914 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
06f76f0d 915 when C is not an ASCII character. */
0d407d77
KH
916
917int
918face_suitable_for_char_p (face, c)
919 struct face *face;
920 int c;
921{
7e1a1cd9 922 Lisp_Object fontset, rfont_def;
0d407d77 923
0d407d77 924 fontset = FONTSET_FROM_ID (face->fontset);
7e1a1cd9
KH
925 rfont_def = fontset_font (fontset, c, NULL, -1);
926 return (VECTORP (rfont_def)
581f7ac7
KH
927 && INTEGERP (RFONT_DEF_FACE (rfont_def))
928 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
0d407d77
KH
929}
930
931
932/* Return ID of face suitable for displaying character C on frame F.
1d5d7200
KH
933 FACE must be reazlied for ASCII characters in advance. Called from
934 the macro FACE_FOR_CHAR. */
0d407d77
KH
935
936int
6bad8007 937face_for_char (f, face, c, pos, object)
0d407d77
KH
938 FRAME_PTR f;
939 struct face *face;
6bad8007
KH
940 int c, pos;
941 Lisp_Object object;
0d407d77 942{
303d5190 943 Lisp_Object fontset, rfont_def, charset;
7e1a1cd9 944 int face_id;
6bad8007 945 int id;
1d5d7200 946
ea217c11
KH
947 /* If face->fontset is negative (that happens when no font is found
948 for face), just return face->ascii_face because we can't do
949 anything. Perhaps, we should fix the callers to assure
950 that face->fontset is always valid. */
951 if (ASCII_CHAR_P (c) || face->fontset < 0)
1d5d7200 952 return face->ascii_face->id;
0d407d77
KH
953
954 xassert (fontset_id_valid_p (face->fontset));
955 fontset = FONTSET_FROM_ID (face->fontset);
956 xassert (!BASE_FONTSET_P (fontset));
41ad6003 957
6bad8007 958 if (pos < 0)
303d5190
KH
959 {
960 id = -1;
961 charset = Qnil;
962 }
6bad8007
KH
963 else
964 {
965 charset = Fget_char_property (make_number (pos), Qcharset, object);
b1bde622 966 if (CHARSETP (charset))
28e2436a
KH
967 {
968 Lisp_Object val;
969
a124bbb6 970 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
28e2436a
KH
971 if (CONSP (val) && CHARSETP (XCDR (val)))
972 charset = XCDR (val);
973 id = XINT (CHARSET_SYMBOL_ID (charset));
974 }
b1bde622
KH
975 else
976 id = -1;
6bad8007 977 }
b59e66b3 978
7e1a1cd9
KH
979 rfont_def = fontset_font (fontset, c, face, id);
980 if (VECTORP (rfont_def))
981 {
581f7ac7
KH
982 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
983 face_id = XINT (RFONT_DEF_FACE (rfont_def));
8aa07a8d 984 else
7e1a1cd9 985 {
581f7ac7 986 Lisp_Object font_object;
7e1a1cd9 987
581f7ac7
KH
988 font_object = RFONT_DEF_OBJECT (rfont_def);
989 face_id = face_for_font (f, font_object, face);
990 RFONT_DEF_SET_FACE (rfont_def, face_id);
7e1a1cd9 991 }
7e1a1cd9 992 }
581f7ac7 993 else
7e1a1cd9 994 {
581f7ac7
KH
995 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
996 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
997 else
998 {
999 face_id = face_for_font (f, Qnil, face);
1000 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
1001 }
7e1a1cd9 1002 }
581f7ac7
KH
1003 xassert (face_id >= 0);
1004 return face_id;
0d407d77
KH
1005}
1006
1007
514cf218
KH
1008Lisp_Object
1009font_for_char (face, c, pos, object)
1010 struct face *face;
1011 int c, pos;
1012 Lisp_Object object;
1013{
1014 Lisp_Object fontset, rfont_def, charset;
514cf218
KH
1015 int id;
1016
1017 if (ASCII_CHAR_P (c))
1018 {
1019 Lisp_Object font_object;
1020
1021 XSETFONT (font_object, face->ascii_face->font);
1022 return font_object;
1023 }
1024
1025 xassert (fontset_id_valid_p (face->fontset));
1026 fontset = FONTSET_FROM_ID (face->fontset);
1027 xassert (!BASE_FONTSET_P (fontset));
1028 if (pos < 0)
1029 {
1030 id = -1;
1031 charset = Qnil;
1032 }
1033 else
1034 {
1035 charset = Fget_char_property (make_number (pos), Qcharset, object);
b1bde622 1036 if (CHARSETP (charset))
514cf218
KH
1037 {
1038 Lisp_Object val;
1039
9fe78804 1040 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
514cf218
KH
1041 if (CONSP (val) && CHARSETP (XCDR (val)))
1042 charset = XCDR (val);
1043 id = XINT (CHARSET_SYMBOL_ID (charset));
1044 }
b1bde622
KH
1045 else
1046 id = -1;
514cf218
KH
1047 }
1048
514cf218
KH
1049 rfont_def = fontset_font (fontset, c, face, id);
1050 return (VECTORP (rfont_def)
1051 ? RFONT_DEF_OBJECT (rfont_def)
1052 : Qnil);
1053}
1054
1055
0d407d77
KH
1056/* Make a realized fontset for ASCII face FACE on frame F from the
1057 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1058 default fontset as the base. Value is the id of the new fontset.
1059 Called from realize_x_face. */
1060
1061int
1d5d7200 1062make_fontset_for_ascii_face (f, base_fontset_id, face)
0d407d77
KH
1063 FRAME_PTR f;
1064 int base_fontset_id;
1d5d7200 1065 struct face *face;
0d407d77 1066{
1337ac77 1067 Lisp_Object base_fontset, fontset, frame;
0d407d77
KH
1068
1069 XSETFRAME (frame, f);
1070 if (base_fontset_id >= 0)
1071 {
1072 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1073 if (!BASE_FONTSET_P (base_fontset))
1074 base_fontset = FONTSET_BASE (base_fontset);
1d5d7200
KH
1075 if (! BASE_FONTSET_P (base_fontset))
1076 abort ();
4ed46869 1077 }
0d407d77
KH
1078 else
1079 base_fontset = Vdefault_fontset;
1080
1081 fontset = make_fontset (frame, Qnil, base_fontset);
f3231837 1082 return XINT (FONTSET_ID (fontset));
0d407d77
KH
1083}
1084
0d407d77 1085\f
1d5d7200 1086
4ed46869
KH
1087/* Cache data used by fontset_pattern_regexp. The car part is a
1088 pattern string containing at least one wild card, the cdr part is
1089 the corresponding regular expression. */
1090static Lisp_Object Vcached_fontset_data;
1091
dbf46ba6 1092#define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
7539e11f 1093#define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
4ed46869
KH
1094
1095/* If fontset name PATTERN contains any wild card, return regular
1096 expression corresponding to PATTERN. */
1097
0d407d77 1098static Lisp_Object
4ed46869
KH
1099fontset_pattern_regexp (pattern)
1100 Lisp_Object pattern;
1101{
dbf46ba6
KH
1102 if (!index ((char *) SDATA (pattern), '*')
1103 && !index ((char *) SDATA (pattern), '?'))
4ed46869 1104 /* PATTERN does not contain any wild cards. */
1c283e35 1105 return Qnil;
4ed46869
KH
1106
1107 if (!CONSP (Vcached_fontset_data)
dbf46ba6 1108 || strcmp ((char *) SDATA (pattern), CACHED_FONTSET_NAME))
4ed46869
KH
1109 {
1110 /* We must at first update the cached data. */
6cc06608 1111 unsigned char *regex, *p0, *p1;
821bc4db 1112 int ndashes = 0, nstars = 0, nescs = 0;
6cc06608 1113
fc1062f5
KH
1114 for (p0 = SDATA (pattern); *p0; p0++)
1115 {
1116 if (*p0 == '-')
1117 ndashes++;
a653f812 1118 else if (*p0 == '*')
fc1062f5 1119 nstars++;
821bc4db
KH
1120 else if (*p0 == '['
1121 || *p0 == '.' || *p0 == '\\'
8510724d 1122 || *p0 == '+' || *p0 == '^'
821bc4db
KH
1123 || *p0 == '$')
1124 nescs++;
fc1062f5
KH
1125 }
1126
1127 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1128 we convert "*" to "[^-]*" which is much faster in regular
1129 expression matching. */
1130 if (ndashes < 14)
821bc4db 1131 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
fc1062f5 1132 else
821bc4db 1133 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
4ed46869 1134
1c283e35 1135 *p1++ = '^';
6cc06608 1136 for (p0 = SDATA (pattern); *p0; p0++)
4ed46869 1137 {
a653f812 1138 if (*p0 == '*')
4ed46869 1139 {
fc1062f5
KH
1140 if (ndashes < 14)
1141 *p1++ = '.';
1142 else
1143 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1c283e35 1144 *p1++ = '*';
4ed46869 1145 }
1c283e35 1146 else if (*p0 == '?')
d96d677d 1147 *p1++ = '.';
821bc4db
KH
1148 else if (*p0 == '['
1149 || *p0 == '.' || *p0 == '\\'
8510724d 1150 || *p0 == '+' || *p0 == '^'
821bc4db
KH
1151 || *p0 == '$')
1152 *p1++ = '\\', *p1++ = *p0;
1c283e35
KH
1153 else
1154 *p1++ = *p0;
4ed46869
KH
1155 }
1156 *p1++ = '$';
1157 *p1++ = 0;
1158
dbf46ba6
KH
1159 Vcached_fontset_data = Fcons (build_string ((char *) SDATA (pattern)),
1160 build_string ((char *) regex));
4ed46869
KH
1161 }
1162
1163 return CACHED_FONTSET_REGEX;
1164}
1165
0d407d77 1166/* Return ID of the base fontset named NAME. If there's no such
a653f812
KH
1167 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1168 0: pattern containing '*' and '?' as wildcards
1169 1: regular expression
1170 2: literal fontset name
1171*/
0d407d77
KH
1172
1173int
a653f812 1174fs_query_fontset (name, name_pattern)
0d407d77 1175 Lisp_Object name;
a653f812 1176 int name_pattern;
0d407d77 1177{
1337ac77 1178 Lisp_Object tem;
0d407d77
KH
1179 int i;
1180
1181 name = Fdowncase (name);
a653f812 1182 if (name_pattern != 1)
0d407d77
KH
1183 {
1184 tem = Frassoc (name, Vfontset_alias_alist);
6bad8007
KH
1185 if (NILP (tem))
1186 tem = Fassoc (name, Vfontset_alias_alist);
0d407d77
KH
1187 if (CONSP (tem) && STRINGP (XCAR (tem)))
1188 name = XCAR (tem);
a653f812 1189 else if (name_pattern == 0)
0d407d77
KH
1190 {
1191 tem = fontset_pattern_regexp (name);
1192 if (STRINGP (tem))
1193 {
1194 name = tem;
a653f812 1195 name_pattern = 1;
0d407d77
KH
1196 }
1197 }
1198 }
1199
1200 for (i = 0; i < ASIZE (Vfontset_table); i++)
1201 {
6e1b0d8c 1202 Lisp_Object fontset, this_name;
0d407d77
KH
1203
1204 fontset = FONTSET_FROM_ID (i);
1205 if (NILP (fontset)
1206 || !BASE_FONTSET_P (fontset))
1207 continue;
1208
6e1b0d8c 1209 this_name = FONTSET_NAME (fontset);
a653f812 1210 if (name_pattern == 1
581f7ac7 1211 ? fast_string_match_ignore_case (name, this_name) >= 0
05131107 1212 : !xstrcasecmp (SDATA (name), SDATA (this_name)))
0d407d77
KH
1213 return i;
1214 }
1215 return -1;
1216}
1217
1218
727fb790 1219DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
335c5470
PJ
1220 doc: /* Return the name of a fontset that matches PATTERN.
1221The value is nil if there is no matching fontset.
1222PATTERN can contain `*' or `?' as a wildcard
1223just as X font name matching algorithm allows.
1224If REGEXPP is non-nil, PATTERN is a regular expression. */)
1225 (pattern, regexpp)
727fb790 1226 Lisp_Object pattern, regexpp;
4ed46869 1227{
0d407d77
KH
1228 Lisp_Object fontset;
1229 int id;
4ed46869
KH
1230
1231 (*check_window_system_func) ();
1232
b7826503 1233 CHECK_STRING (pattern);
4ed46869 1234
d5db4077 1235 if (SCHARS (pattern) == 0)
4ed46869
KH
1236 return Qnil;
1237
0d407d77
KH
1238 id = fs_query_fontset (pattern, !NILP (regexpp));
1239 if (id < 0)
1240 return Qnil;
4ed46869 1241
0d407d77
KH
1242 fontset = FONTSET_FROM_ID (id);
1243 return FONTSET_NAME (fontset);
4ed46869
KH
1244}
1245
06f76f0d 1246/* Return a list of base fontset names matching PATTERN on frame F. */
4ed46869
KH
1247
1248Lisp_Object
1249list_fontsets (f, pattern, size)
1250 FRAME_PTR f;
1251 Lisp_Object pattern;
1252 int size;
1253{
1337ac77 1254 Lisp_Object frame, regexp, val;
0d407d77 1255 int id;
4ed46869 1256
0d407d77 1257 XSETFRAME (frame, f);
4ed46869 1258
0d407d77 1259 regexp = fontset_pattern_regexp (pattern);
4ed46869 1260 val = Qnil;
4ed46869 1261
0d407d77
KH
1262 for (id = 0; id < ASIZE (Vfontset_table); id++)
1263 {
6e1b0d8c 1264 Lisp_Object fontset, name;
0d407d77
KH
1265
1266 fontset = FONTSET_FROM_ID (id);
1267 if (NILP (fontset)
1268 || !BASE_FONTSET_P (fontset)
1269 || !EQ (frame, FONTSET_FRAME (fontset)))
1270 continue;
6e1b0d8c 1271 name = FONTSET_NAME (fontset);
0d407d77 1272
1d5d7200 1273 if (STRINGP (regexp)
6e1b0d8c 1274 ? (fast_string_match (regexp, name) < 0)
dbf46ba6 1275 : strcmp ((char *) SDATA (pattern), (char *) SDATA (name)))
0d407d77
KH
1276 continue;
1277
0d407d77 1278 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
4ed46869
KH
1279 }
1280
1281 return val;
1282}
1283
4ed46869 1284
8f924df7 1285/* Free all realized fontsets whose base fontset is BASE. */
4ed46869 1286
06f76f0d
KH
1287static void
1288free_realized_fontsets (base)
1289 Lisp_Object base;
1290{
1291 int id;
4ed46869 1292
2f034212 1293#if 0
27e20b2f
KH
1294 /* For the moment, this doesn't work because free_realized_face
1295 doesn't remove FACE from a cache. Until we find a solution, we
1296 suppress this code, and simply use Fclear_face_cache even though
1297 that is not efficient. */
06f76f0d
KH
1298 BLOCK_INPUT;
1299 for (id = 0; id < ASIZE (Vfontset_table); id++)
4ed46869 1300 {
06f76f0d 1301 Lisp_Object this = AREF (Vfontset_table, id);
0d407d77 1302
06f76f0d 1303 if (EQ (FONTSET_BASE (this), base))
0d407d77 1304 {
06f76f0d 1305 Lisp_Object tail;
4ed46869 1306
06f76f0d
KH
1307 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1308 tail = XCDR (tail))
1309 {
1310 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1311 int face_id = XINT (XCDR (XCAR (tail)));
1312 struct face *face = FACE_FROM_ID (f, face_id);
4ed46869 1313
06f76f0d
KH
1314 /* Face THIS itself is also freed by the following call. */
1315 free_realized_face (f, face);
1316 }
1317 }
0d407d77 1318 }
06f76f0d 1319 UNBLOCK_INPUT;
27e20b2f 1320#else /* not 0 */
2f034212
KH
1321 /* But, we don't have to call Fclear_face_cache if no fontset has
1322 been realized from BASE. */
1323 for (id = 0; id < ASIZE (Vfontset_table); id++)
1324 {
1325 Lisp_Object this = AREF (Vfontset_table, id);
1326
985773c9 1327 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
2f034212
KH
1328 {
1329 Fclear_face_cache (Qt);
1330 break;
1331 }
1332 }
27e20b2f 1333#endif /* not 0 */
0d407d77 1334}
4ed46869 1335
4ed46869 1336
0d407d77
KH
1337/* Check validity of NAME as a fontset name and return the
1338 corresponding fontset. If not valid, signal an error.
a3d16f39
KH
1339
1340 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1341 fontset of *FRAME.
1342
1343 Set *FRAME to the actual frame. */
0d407d77
KH
1344
1345static Lisp_Object
a3d16f39
KH
1346check_fontset_name (name, frame)
1347 Lisp_Object name, *frame;
0d407d77
KH
1348{
1349 int id;
1350
a3d16f39
KH
1351 if (NILP (*frame))
1352 *frame = selected_frame;
1353 CHECK_LIVE_FRAME (*frame);
1354
0d407d77
KH
1355 if (EQ (name, Qt))
1356 return Vdefault_fontset;
a3d16f39
KH
1357 if (NILP (name))
1358 {
1359 id = FRAME_FONTSET (XFRAME (*frame));
1360 }
1361 else
1362 {
1363 CHECK_STRING (name);
1364 /* First try NAME as literal. */
1365 id = fs_query_fontset (name, 2);
1366 if (id < 0)
1367 /* For backward compatibility, try again NAME as pattern. */
1368 id = fs_query_fontset (name, 0);
1369 if (id < 0)
1370 error ("Fontset `%s' does not exist", SDATA (name));
1371 }
0d407d77
KH
1372 return FONTSET_FROM_ID (id);
1373}
4ed46869 1374
1d5d7200
KH
1375static void
1376accumulate_script_ranges (arg, range, val)
1377 Lisp_Object arg, range, val;
1378{
1379 if (EQ (XCAR (arg), val))
1380 {
1381 if (CONSP (range))
1382 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1383 else
1384 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1385 }
1386}
1387
1388
a3d16f39
KH
1389/* Callback function for map_charset_chars in Fset_fontset_font.
1390 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
fb78e2ed 1391
a3d16f39
KH
1392 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1393 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1394 The consumed ranges are poped up from SCRIPT_RANGE_LIST, and the
1395 new SCRIPT_RANGE_LIST is stored in ARG.
1396
1397 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1398 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1399 case. */
fb78e2ed 1400
2449d4d0 1401static void
a3d16f39
KH
1402set_fontset_font (arg, range)
1403 Lisp_Object arg, range;
2449d4d0 1404{
a3d16f39
KH
1405 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1406 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
2449d4d0 1407
a3d16f39
KH
1408 fontset = AREF (arg, 0);
1409 font_def = AREF (arg, 1);
1410 add = AREF (arg, 2);
1411 ascii = AREF (arg, 3);
1412 script_range_list = AREF (arg, 4);
fb78e2ed 1413
a3d16f39
KH
1414 if (NILP (ascii) && from < 0x80)
1415 {
1416 if (to < 0x80)
fb78e2ed 1417 return;
a3d16f39
KH
1418 from = 0x80;
1419 range = Fcons (make_number (0x80), XCDR (range));
1420 }
1421
1422#define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1423#define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1424#define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1425
1426 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1427 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1428 if (CONSP (script_range_list))
1429 {
1430 if (SCRIPT_FROM < from)
1431 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1432 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1433 POP_SCRIPT_RANGE ();
1434 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1435 XSETCAR (XCAR (script_range_list), make_number (to + 1));
fb78e2ed 1436 }
a3d16f39
KH
1437
1438 FONTSET_ADD (fontset, range, font_def, add);
1439 ASET (arg, 4, script_range_list);
2449d4d0
KH
1440}
1441
8b67c40a 1442extern Lisp_Object QCfamily, QCregistry;
d6490542
KH
1443static void update_auto_fontset_alist P_ ((Lisp_Object, Lisp_Object));
1444
d6aaac9e 1445
1d5d7200 1446DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
8f924df7 1447 doc: /*
eb36588a 1448Modify fontset NAME to use FONT-SPEC for TARGET characters.
335c5470 1449
a3d16f39
KH
1450NAME is a fontset name string, nil for the fontset of FRAME, or t for
1451the default fontset.
1452
eb36588a
KH
1453TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1454In that case, use FONT-SPEC for all characters in the range FROM and
1455TO (inclusive).
06f76f0d 1456
eb36588a
KH
1457TARGET may be a script name symbol. In that case, use FONT-SPEC for
1458all characters that belong to the script.
06f76f0d 1459
eb36588a 1460TARGET may be a charset. In that case, use FONT-SPEC for all
95318a38 1461characters in the charset.
1d5d7200 1462
eb36588a
KH
1463TARGET may be nil. In that case, use FONT-SPEC for any characters for
1464that no FONT-SPEC is specified.
1465
0fdf26e6 1466FONT-SPEC may one of these:
581f7ac7 1467 * A font-spec object made by the function `font-spec' (which see).
3dcd48dd 1468 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
581f7ac7
KH
1469 REGISTRY is a font registry name. FAMILY may contain foundry
1470 name, and REGISTRY may contain encoding name.
00c4da0f 1471 * A font name string.
eaa0402c 1472 * nil, which explicitly specifies that there's no font for TARGET.
1d5d7200 1473
a3d16f39
KH
1474Optional 4th argument FRAME is a frame or nil for the selected frame
1475that is concerned in the case that NAME is nil.
1d5d7200
KH
1476
1477Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
eb36588a 1478to the font specifications for TARGET previously set. If it is
1d5d7200
KH
1479`prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1480appended. By default, FONT-SPEC overrides the previous settings. */)
eb36588a
KH
1481 (name, target, font_spec, frame, add)
1482 Lisp_Object name, target, font_spec, frame, add;
0d407d77 1483{
06f76f0d 1484 Lisp_Object fontset;
9683749a 1485 Lisp_Object font_def, registry, family;
1d5d7200 1486 Lisp_Object range_list;
fb78e2ed 1487 struct charset *charset = NULL;
d6490542
KH
1488 Lisp_Object fontname;
1489 int ascii_changed = 0;
0d407d77 1490
a3d16f39 1491 fontset = check_fontset_name (name, &frame);
1d5d7200 1492
d6490542 1493 fontname = Qnil;
581f7ac7 1494 if (CONSP (font_spec))
0d407d77 1495 {
581f7ac7 1496 Lisp_Object spec = Ffont_spec (0, NULL);
1d5d7200 1497
581f7ac7
KH
1498 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1499 font_spec = spec;
d6490542 1500 fontname = Ffont_xlfd_name (font_spec, Qnil);
0890801b 1501 }
d67f7f1a 1502 else if (STRINGP (font_spec))
4ed46869 1503 {
ea0162a6 1504 Lisp_Object args[2];
581f7ac7 1505 extern Lisp_Object QCname;
ea0162a6 1506
d6490542 1507 fontname = font_spec;
ea0162a6
KH
1508 args[0] = QCname;
1509 args[1] = font_spec;
1510 font_spec = Ffont_spec (2, args);
0d407d77 1511 }
d6490542
KH
1512 else if (FONT_SPEC_P (font_spec))
1513 fontname = Ffont_xlfd_name (font_spec, Qnil);
1514 else if (! NILP (font_spec))
581f7ac7 1515 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1d5d7200 1516
d67f7f1a 1517 if (! NILP (font_spec))
57e13af9 1518 {
581f7ac7
KH
1519 Lisp_Object encoding, repertory;
1520
d67f7f1a 1521 family = AREF (font_spec, FONT_FAMILY_INDEX);
581f7ac7 1522 if (! NILP (family) )
d67f7f1a
KH
1523 family = SYMBOL_NAME (family);
1524 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
581f7ac7
KH
1525 if (! NILP (registry))
1526 registry = Fdowncase (SYMBOL_NAME (registry));
1527 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1528 registry));
d67f7f1a
KH
1529 if (NILP (encoding))
1530 encoding = Qascii;
1531
1532 if (SYMBOLP (encoding))
1533 {
1534 CHECK_CHARSET (encoding);
1535 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1536 }
1537 else
57e13af9 1538 {
d67f7f1a
KH
1539 repertory = XCDR (encoding);
1540 encoding = XCAR (encoding);
1541 CHECK_CHARSET (encoding);
1542 encoding = CHARSET_SYMBOL_ID (encoding);
1543 if (! NILP (repertory) && SYMBOLP (repertory))
1544 {
1545 CHECK_CHARSET (repertory);
1546 repertory = CHARSET_SYMBOL_ID (repertory);
1547 }
57e13af9 1548 }
581f7ac7 1549 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
0d407d77 1550 }
d67f7f1a
KH
1551 else
1552 font_def = Qnil;
4ed46869 1553
eb36588a 1554 if (CHARACTERP (target))
d6490542
KH
1555 {
1556 if (XFASTINT (target) < 0x80)
d7a39b51 1557 error ("Can't set a font for partial ASCII range");
d6490542
KH
1558 range_list = Fcons (Fcons (target, target), Qnil);
1559 }
eb36588a 1560 else if (CONSP (target))
0d407d77 1561 {
06f76f0d
KH
1562 Lisp_Object from, to;
1563
eb36588a
KH
1564 from = Fcar (target);
1565 to = Fcdr (target);
06f76f0d
KH
1566 CHECK_CHARACTER (from);
1567 CHECK_CHARACTER (to);
d6490542
KH
1568 if (XFASTINT (from) < 0x80)
1569 {
1570 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
d7a39b51 1571 error ("Can't set a font for partial ASCII range");
d6490542
KH
1572 ascii_changed = 1;
1573 }
eb36588a 1574 range_list = Fcons (target, Qnil);
4ed46869 1575 }
eb36588a 1576 else if (SYMBOLP (target) && !NILP (target))
8a9be3ac 1577 {
1d5d7200
KH
1578 Lisp_Object script_list;
1579 Lisp_Object val;
0d407d77 1580
1d5d7200
KH
1581 range_list = Qnil;
1582 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
eb36588a 1583 if (! NILP (Fmemq (target, script_list)))
afe93d01 1584 {
d6490542
KH
1585 if (EQ (target, Qlatin))
1586 ascii_changed = 1;
eb36588a 1587 val = Fcons (target, Qnil);
1d5d7200 1588 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
8f924df7 1589 val);
a3d16f39 1590 range_list = Fnreverse (XCDR (val));
afe93d01 1591 }
fb78e2ed 1592 if (CHARSETP (target))
afe93d01 1593 {
d6490542
KH
1594 CHECK_CHARSET_GET_CHARSET (target, charset);
1595 if (charset->ascii_compatible_p)
1596 ascii_changed = 1;
afe93d01 1597 }
fb78e2ed 1598 else if (NILP (range_list))
1d5d7200 1599 error ("Invalid script or charset name: %s",
eb36588a 1600 SDATA (SYMBOL_NAME (target)));
8a9be3ac 1601 }
eb36588a
KH
1602 else if (NILP (target))
1603 range_list = Fcons (Qnil, Qnil);
1604 else
1605 error ("Invalid target for setting a font");
0d407d77 1606
a3d16f39
KH
1607 if (ascii_changed)
1608 {
1609 Lisp_Object val;
1610
1611 if (NILP (font_spec))
1612 error ("Can't set ASCII font to nil");
1613 val = CHAR_TABLE_REF (fontset, 0);
1614 if (! NILP (val) && EQ (add, Qappend))
1615 /* We are going to change just an additional font for ASCII. */
1616 ascii_changed = 0;
1617 }
fb78e2ed
KH
1618
1619 if (charset)
1620 {
a3d16f39
KH
1621 Lisp_Object arg;
1622
1623 arg = Fmake_vector (make_number (5), Qnil);
1624 ASET (arg, 0, fontset);
1625 ASET (arg, 1, font_def);
1626 ASET (arg, 2, add);
1627 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1628 ASET (arg, 4, range_list);
fb78e2ed 1629
a3d16f39 1630 map_charset_chars (set_fontset_font, Qnil, arg, charset,
fb78e2ed
KH
1631 CHARSET_MIN_CODE (charset),
1632 CHARSET_MAX_CODE (charset));
a3d16f39 1633 range_list = AREF (arg, 4);
fb78e2ed 1634 }
1d5d7200
KH
1635 for (; CONSP (range_list); range_list = XCDR (range_list))
1636 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
4ed46869 1637
d6490542
KH
1638 if (ascii_changed)
1639 {
1640 Lisp_Object tail, frame, alist;
1641 int fontset_id = XINT (FONTSET_ID (fontset));
1642
d6490542
KH
1643 FONTSET_ASCII (fontset) = fontname;
1644 name = FONTSET_NAME (fontset);
1645 FOR_EACH_FRAME (tail, frame)
1646 {
1647 FRAME_PTR f = XFRAME (frame);
1648 Lisp_Object font_object;
0875632e 1649 struct face *face;
d6490542 1650
0875632e 1651 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
d6490542
KH
1652 continue;
1653 if (fontset_id != FRAME_FONTSET (f))
1654 continue;
0875632e
KH
1655 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1656 if (face)
1657 font_object = font_load_for_lface (f, face->lface, font_spec);
1658 else
1659 font_object = font_open_by_spec (f, font_spec);
d6490542
KH
1660 if (! NILP (font_object))
1661 {
1662 update_auto_fontset_alist (font_object, fontset);
0875632e 1663 alist = Fcons (Fcons (Qfont, Fcons (name, font_object)), Qnil);
d6490542
KH
1664 Fmodify_frame_parameters (frame, alist);
1665 }
1666 }
1667 }
1668
0875632e
KH
1669 /* Free all realized fontsets whose base is FONTSET. This way, the
1670 specified character(s) are surely redisplayed by a correct
1671 font. */
1672 free_realized_fontsets (fontset);
1673
4ed46869
KH
1674 return Qnil;
1675}
1676
06f76f0d
KH
1677
1678DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1679 doc: /* Create a new fontset NAME from font information in FONTLIST.
1680
1d5d7200 1681FONTLIST is an alist of scripts vs the corresponding font specification list.
d6aaac9e
KH
1682Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1683character of SCRIPT is displayed by a font that matches one of
1684FONT-SPEC.
06f76f0d 1685
d6aaac9e
KH
1686SCRIPT is a symbol that appears in the first extra slot of the
1687char-table `char-script-table'.
06f76f0d 1688
1d5d7200
KH
1689FONT-SPEC is a vector, a cons, or a string. See the documentation of
1690`set-fontset-font' for the meaning. */)
06f76f0d
KH
1691 (name, fontlist)
1692 Lisp_Object name, fontlist;
1693{
1d5d7200 1694 Lisp_Object fontset;
1d5d7200 1695 int id;
06f76f0d
KH
1696
1697 CHECK_STRING (name);
1698 CHECK_LIST (fontlist);
1699
581f7ac7 1700 name = Fdowncase (name);
1d5d7200
KH
1701 id = fs_query_fontset (name, 0);
1702 if (id < 0)
0d407d77 1703 {
581f7ac7
KH
1704 Lisp_Object font_spec = Ffont_spec (0, NULL);
1705 Lisp_Object short_name;
5f2d79e0 1706 char xlfd[256];
581f7ac7
KH
1707 int len;
1708
dbf46ba6 1709 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
d6aaac9e 1710 error ("Fontset name must be in XLFD format");
581f7ac7 1711 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
dbf46ba6 1712 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
581f7ac7
KH
1713 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1714 error ("Registry field of fontset name must be \"fontset-*\"");
1715 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1716 Vfontset_alias_alist);
1717 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
d6aaac9e 1718 fontset = make_fontset (Qnil, name, Qnil);
5f2d79e0
KH
1719 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1720 if (len < 0)
1721 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
581f7ac7 1722 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
d6aaac9e 1723 }
1d5d7200
KH
1724 else
1725 {
8510724d 1726 fontset = FONTSET_FROM_ID (id);
1d5d7200
KH
1727 free_realized_fontsets (fontset);
1728 Fset_char_table_range (fontset, Qt, Qnil);
0d407d77 1729 }
4ed46869 1730
1d5d7200
KH
1731 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1732 {
1733 Lisp_Object elt, script;
1734
1735 elt = Fcar (fontlist);
1736 script = Fcar (elt);
cc36ddbf
KH
1737 elt = Fcdr (elt);
1738 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1739 for (; CONSP (elt); elt = XCDR (elt))
1740 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1741 else
1742 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1d5d7200 1743 }
06f76f0d
KH
1744 return name;
1745}
1746
1747
452a78e0 1748/* Alist of automatically created fontsets. Each element is a cons
581f7ac7 1749 (FONT-SPEC . FONTSET-ID). */
452a78e0 1750static Lisp_Object auto_fontset_alist;
d6aaac9e 1751
d6490542
KH
1752/* Number of automatically created fontsets. */
1753static int num_auto_fontsets;
1754
d8d2f142
KH
1755/* Retun a fontset synthesized from FONT-OBJECT. This is called from
1756 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1757 frame, and the returned fontset is used for the default fontset of
1758 that frame. The fontset specifies a font of the same registry as
1759 FONT-OBJECT for all characters in the repertory of the registry
1760 (see Vfont_encoding_alist). If the repertory is not known, the
1761 fontset specifies the font for all Latin characters assuming that a
1762 user intends to use FONT-OBJECT for Latin characters. */
d6490542 1763
d6aaac9e 1764int
581f7ac7 1765fontset_from_font (font_object)
8aa07a8d
KH
1766 Lisp_Object font_object;
1767{
03ef560c 1768 Lisp_Object font_name = font_get_name (font_object);
581f7ac7 1769 Lisp_Object font_spec = Fcopy_font_spec (font_object);
d6490542 1770 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
581f7ac7
KH
1771 Lisp_Object fontset_spec, alias, name, fontset;
1772 Lisp_Object val;
8aa07a8d 1773
581f7ac7
KH
1774 val = assoc_no_quit (font_spec, auto_fontset_alist);
1775 if (CONSP (val))
1776 return XINT (FONTSET_ID (XCDR (val)));
d6490542 1777 if (num_auto_fontsets++ == 0)
581f7ac7 1778 alias = intern ("fontset-startup");
03ef560c
KH
1779 else
1780 {
1781 char temp[32];
8aa07a8d 1782
d6490542 1783 sprintf (temp, "fontset-auto%d", num_auto_fontsets - 1);
581f7ac7 1784 alias = intern (temp);
03ef560c 1785 }
581f7ac7
KH
1786 fontset_spec = Fcopy_font_spec (font_spec);
1787 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
7ae2e7f0 1788 name = Ffont_xlfd_name (fontset_spec, Qnil);
03ef560c 1789 if (NILP (name))
581f7ac7 1790 abort ();
03ef560c 1791 fontset = make_fontset (Qnil, name, Qnil);
581f7ac7
KH
1792 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1793 Vfontset_alias_alist);
1794 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1795 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1796 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
d8d2f142
KH
1797 font_spec = Ffont_spec (0, NULL);
1798 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1799 {
1800 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1801
1802 if (CONSP (target))
1803 target = XCDR (target);
1804 if (! CHARSETP (target))
1805 target = Qlatin;
1806 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1807 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1808 }
1809
d6490542 1810 FONTSET_ASCII (fontset) = font_name;
edfda783 1811
03ef560c 1812 return XINT (FONTSET_ID (fontset));
8aa07a8d
KH
1813}
1814
d6490542
KH
1815
1816/* Update auto_fontset_alist for FONTSET. When an ASCII font of
1817 FONTSET is changed, we delete an entry of FONTSET if any from
1818 auto_fontset_alist so that FONTSET is not re-used by
1819 fontset_from_font. */
1820
1821static void
1822update_auto_fontset_alist (font_object, fontset)
1823 Lisp_Object font_object, fontset;
1824{
1825 Lisp_Object prev, tail;
1826
1827 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1828 prev = tail, tail = XCDR (tail))
1829 if (EQ (fontset, XCDR (XCAR (tail))))
1830 {
1831 if (NILP (prev))
1832 auto_fontset_alist = XCDR (tail);
1833 else
1834 XSETCDR (prev, XCDR (tail));
1835 break;
1836 }
1837}
1838
1839
fe44f009
KH
1840/* Return a cons (FONT-OBJECT . GLYPH-CODE).
1841 FONT-OBJECT is the font for the character at POSITION in the current
1ff005e1 1842 buffer. This is computed from all the text properties and overlays
cf14fd6e 1843 that apply to POSITION. POSTION may be nil, in which case,
fe44f009 1844 FONT-SPEC is the font for displaying the character CH with the
cf14fd6e
KH
1845 default face.
1846
92c15c34
KH
1847 GLYPH-CODE is the glyph code in the font to use for the character.
1848
1849 If the 2nd optional arg CH is non-nil, it is a character to check
1850 the font instead of the character at POSITION.
1851
1852 It returns nil in the following cases:
1ff005e1
KH
1853
1854 (1) The window system doesn't have a font for the character (thus
1855 it is displayed by an empty box).
1856
1857 (2) The character code is invalid.
1858
cf14fd6e
KH
1859 (3) If POSITION is not nil, and the current buffer is not displayed
1860 in any window.
1ff005e1
KH
1861
1862 In addition, the returned font name may not take into account of
1863 such redisplay engine hooks as what used in jit-lock-mode if
1864 POSITION is currently not visible. */
1865
1866
92c15c34 1867DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
335c5470 1868 doc: /* For internal use only. */)
92c15c34
KH
1869 (position, ch)
1870 Lisp_Object position, ch;
1ff005e1 1871{
0f8b27ea 1872 EMACS_INT pos, pos_byte, dummy;
1ff005e1
KH
1873 int face_id;
1874 int c;
1ff005e1
KH
1875 struct frame *f;
1876 struct face *face;
f0be8ea0 1877 int cs_id;
1ff005e1 1878
cf14fd6e 1879 if (NILP (position))
92c15c34
KH
1880 {
1881 CHECK_CHARACTER (ch);
1882 c = XINT (ch);
cf14fd6e 1883 f = XFRAME (selected_frame);
f2cec7a9 1884 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
327719ee 1885 pos = -1;
f0be8ea0 1886 cs_id = -1;
cf14fd6e
KH
1887 }
1888 else
1889 {
2f80c0a2 1890 Lisp_Object window, charset;
cf14fd6e
KH
1891 struct window *w;
1892
1893 CHECK_NUMBER_COERCE_MARKER (position);
1894 pos = XINT (position);
1895 if (pos < BEGV || pos >= ZV)
1896 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1897 pos_byte = CHAR_TO_BYTE (pos);
1898 if (NILP (ch))
1899 c = FETCH_CHAR (pos_byte);
1900 else
1901 {
1902 CHECK_NATNUM (ch);
1903 c = XINT (ch);
1904 }
1905 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1906 if (NILP (window))
1907 return Qnil;
1908 w = XWINDOW (window);
1909 f = XFRAME (w->frame);
6970f632
CY
1910 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy,
1911 pos + 100, 0, -1);
2f80c0a2
KH
1912 charset = Fget_char_property (position, Qcharset, Qnil);
1913 if (CHARSETP (charset))
f0be8ea0 1914 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
2f80c0a2 1915 else
f0be8ea0 1916 cs_id = -1;
92c15c34 1917 }
1ff005e1 1918 if (! CHAR_VALID_P (c, 0))
1ff005e1 1919 return Qnil;
327719ee 1920 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1ff005e1 1921 face = FACE_FROM_ID (f, face_id);
581f7ac7 1922 if (face->font)
8aa07a8d 1923 {
fe44f009
KH
1924 unsigned code = face->font->driver->encode_char (face->font, c);
1925 Lisp_Object font_object;
581f7ac7
KH
1926 /* Assignment to EMACS_INT stops GCC whining about limited range
1927 of data type. */
1928 EMACS_INT cod = code;
1929
1930 if (code == FONT_INVALID_CODE)
1931 return Qnil;
fe44f009 1932 XSETFONT (font_object, face->font);
581f7ac7 1933 if (cod <= MOST_POSITIVE_FIXNUM)
fe44f009
KH
1934 return Fcons (font_object, make_number (code));
1935 return Fcons (font_object, Fcons (make_number (code >> 16),
581f7ac7 1936 make_number (code & 0xFFFF)));
92c15c34
KH
1937 }
1938 return Qnil;
1ff005e1
KH
1939}
1940
1941
1d5d7200
KH
1942DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1943 doc: /* Return information about a fontset FONTSET on frame FRAME.
e2b45cf9 1944
a3d16f39
KH
1945FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1946for the default fontset. FRAME nil means the selected frame.
1ff005e1 1947
a3d16f39 1948The value is a char-table whose elements have this form:
1ff005e1 1949
a3d16f39 1950 ((FONT OPENED-FONT ...) ...)
1ff005e1 1951
a3d16f39 1952FONT is a name of font specified for a range of characters.
1ff005e1 1953
d6aaac9e 1954OPENED-FONT is a name of a font actually opened.
1ff005e1 1955
a3d16f39
KH
1956The char-table has one extra slot. If FONTSET is not the default
1957fontset, the value the extra slot is a char-table containing the
1958information about the derived fonts from the default fontset. The
1959format is the same as above. */)
1d5d7200
KH
1960 (fontset, frame)
1961 Lisp_Object fontset, frame;
4ed46869
KH
1962{
1963 FRAME_PTR f;
cc7b6145
KH
1964 Lisp_Object *realized[2], fontsets[2], tables[2];
1965 Lisp_Object val, elt;
1966 int c, i, j, k;
fc8865fc 1967
4ed46869
KH
1968 (*check_window_system_func) ();
1969
a3d16f39 1970 fontset = check_fontset_name (fontset, &frame);
18f39d0e 1971 f = XFRAME (frame);
4ed46869 1972
1d5d7200
KH
1973 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1974 in the table `realized'. */
cc7b6145
KH
1975 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1976 * ASIZE (Vfontset_table));
1977 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
0d407d77 1978 {
1ff005e1
KH
1979 elt = FONTSET_FROM_ID (i);
1980 if (!NILP (elt)
1d5d7200
KH
1981 && EQ (FONTSET_BASE (elt), fontset)
1982 && EQ (FONTSET_FRAME (elt), frame))
cc7b6145 1983 realized[0][j++] = elt;
0d407d77 1984 }
cc7b6145 1985 realized[0][j] = Qnil;
4ed46869 1986
cc7b6145
KH
1987 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1988 * ASIZE (Vfontset_table));
1989 for (i = j = 0; ! NILP (realized[0][i]); i++)
1990 {
1991 elt = FONTSET_DEFAULT (realized[0][i]);
1992 if (! NILP (elt))
1993 realized[1][j++] = elt;
1994 }
1995 realized[1][j] = Qnil;
1996
1997 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
cc7b6145 1998 fontsets[0] = fontset;
348db3dd 1999 if (!EQ (fontset, Vdefault_fontset))
a3d16f39
KH
2000 {
2001 tables[1] = Fmake_char_table (Qnil, Qnil);
2002 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
2003 fontsets[1] = Vdefault_fontset;
2004 }
e2b45cf9 2005
1d5d7200
KH
2006 /* Accumulate information of the fontset in TABLE. The format of
2007 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
cc7b6145 2008 for (k = 0; k <= 1; k++)
0d407d77 2009 {
cc7b6145 2010 for (c = 0; c <= MAX_CHAR; )
d6aaac9e 2011 {
5b30be1a 2012 int from = c, to = MAX_5_BYTE_CHAR;
0d407d77 2013
cc7b6145
KH
2014 if (c <= MAX_5_BYTE_CHAR)
2015 {
2016 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
cc7b6145
KH
2017 }
2018 else
2019 {
2020 val = FONTSET_FALLBACK (fontsets[k]);
2021 to = MAX_CHAR;
2022 }
d6aaac9e 2023 if (VECTORP (val))
0d407d77 2024 {
d6aaac9e
KH
2025 Lisp_Object alist;
2026
2027 /* At first, set ALIST to ((FONT-SPEC) ...). */
2028 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
581f7ac7
KH
2029 if (! NILP (AREF (val, i)))
2030 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
2031 alist);
d6aaac9e
KH
2032 alist = Fnreverse (alist);
2033
660100f3 2034 /* Then store opened font names to cdr of each elements. */
cc7b6145 2035 for (i = 0; ! NILP (realized[k][i]); i++)
1ff005e1 2036 {
cc7b6145
KH
2037 if (c <= MAX_5_BYTE_CHAR)
2038 val = FONTSET_REF (realized[k][i], c);
2039 else
2040 val = FONTSET_FALLBACK (realized[k][i]);
a3d16f39 2041 if (! CONSP (val) || ! VECTORP (XCDR (val)))
d6aaac9e 2042 continue;
a3d16f39
KH
2043 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
2044 val = XCDR (val);
2045 for (j = 0; j < ASIZE (val); j++)
3d4448a8 2046 {
581f7ac7
KH
2047 elt = AREF (val, j);
2048 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
2049 {
2050 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
2051 Lisp_Object slot, name;
2052
2053 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
2054 name = AREF (font_object, FONT_NAME_INDEX);
2055 if (NILP (Fmember (name, XCDR (slot))))
2056 nconc2 (slot, Fcons (name, Qnil));
2057 }
3d4448a8 2058 }
1ff005e1 2059 }
d6aaac9e
KH
2060
2061 /* Store ALIST in TBL for characters C..TO. */
cc7b6145
KH
2062 if (c <= MAX_5_BYTE_CHAR)
2063 char_table_set_range (tables[k], c, to, alist);
2064 else
2065 XCHAR_TABLE (tables[k])->defalt = alist;
581f7ac7
KH
2066
2067 /* At last, change each elements to font names. */
2068 for (; CONSP (alist); alist = XCDR (alist))
2069 {
2070 elt = XCAR (alist);
7ae2e7f0 2071 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
581f7ac7 2072 }
0d407d77 2073 }
d6aaac9e 2074 c = to + 1;
0d407d77 2075 }
348db3dd 2076 if (EQ (fontset, Vdefault_fontset))
a3d16f39 2077 break;
0d407d77 2078 }
a921395d 2079
cc7b6145 2080 return tables[0];
4ed46869
KH
2081}
2082
1d5d7200 2083
0fdf26e6 2084DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
335c5470 2085 doc: /* Return a font name pattern for character CH in fontset NAME.
0fdf26e6 2086If NAME is t, find a pattern in the default fontset.
5fc05db0 2087If NAME is nil, find a pattern in the fontset of the selected frame.
0fdf26e6
KH
2088
2089The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2090family name and REGISTRY is a font registry name. This is actually
2091the first font name pattern for CH in the fontset or in the default
2092fontset.
2093
2094If the 2nd optional arg ALL is non-nil, return a list of all font name
2095patterns. */)
2096 (name, ch, all)
2097 Lisp_Object name, ch, all;
0d407d77 2098{
1337ac77 2099 int c;
0fdf26e6
KH
2100 Lisp_Object fontset, elt, list, repertory, val;
2101 int i, j;
a3d16f39 2102 Lisp_Object frame;
0d407d77 2103
a3d16f39
KH
2104 frame = Qnil;
2105 fontset = check_fontset_name (name, &frame);
0d407d77 2106
06f76f0d 2107 CHECK_CHARACTER (ch);
0d407d77 2108 c = XINT (ch);
0fdf26e6
KH
2109 list = Qnil;
2110 while (1)
2111 {
2112 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2113 i++, elt = FONTSET_FALLBACK (fontset))
2114 if (VECTORP (elt))
2115 for (j = 0; j < ASIZE (elt); j++)
2116 {
a124bbb6
KH
2117 Lisp_Object family, registry;
2118
0fdf26e6 2119 val = AREF (elt, j);
5fc05db0 2120 if (NILP (val))
563a866e 2121 return Qnil;
0fdf26e6
KH
2122 repertory = AREF (val, 1);
2123 if (INTEGERP (repertory))
2124 {
2125 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2126
2127 if (! CHAR_CHARSET_P (c, charset))
2128 continue;
2129 }
2130 else if (CHAR_TABLE_P (repertory))
2131 {
2132 if (NILP (CHAR_TABLE_REF (repertory, c)))
2133 continue;
2134 }
2135 val = AREF (val, 0);
a124bbb6
KH
2136 /* VAL is a FONT-SPEC */
2137 family = AREF (val, FONT_FAMILY_INDEX);
2138 if (! NILP (family))
2139 family = SYMBOL_NAME (family);
2140 registry = AREF (val, FONT_REGISTRY_INDEX);
2141 if (! NILP (registry))
2142 registry = SYMBOL_NAME (registry);
2143 val = Fcons (family, registry);
0fdf26e6
KH
2144 if (NILP (all))
2145 return val;
2146 list = Fcons (val, list);
2147 }
2148 if (EQ (fontset, Vdefault_fontset))
2149 break;
2150 fontset = Vdefault_fontset;
2151 }
2152 return (Fnreverse (list));
0d407d77 2153}
0d407d77
KH
2154
2155DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
335c5470
PJ
2156 doc: /* Return a list of all defined fontset names. */)
2157 ()
0d407d77
KH
2158{
2159 Lisp_Object fontset, list;
2160 int i;
2161
2162 list = Qnil;
2163 for (i = 0; i < ASIZE (Vfontset_table); i++)
2164 {
2165 fontset = FONTSET_FROM_ID (i);
2166 if (!NILP (fontset)
2167 && BASE_FONTSET_P (fontset))
2168 list = Fcons (FONTSET_NAME (fontset), list);
2169 }
1ff005e1 2170
0d407d77
KH
2171 return list;
2172}
2173
452a78e0
KH
2174
2175#ifdef FONTSET_DEBUG
2176
2177Lisp_Object
2178dump_fontset (fontset)
2179 Lisp_Object fontset;
2180{
2181 Lisp_Object vec;
2182
2183 vec = Fmake_vector (make_number (3), Qnil);
2184 ASET (vec, 0, FONTSET_ID (fontset));
2185
2186 if (BASE_FONTSET_P (fontset))
2187 {
2188 ASET (vec, 1, FONTSET_NAME (fontset));
2189 }
2190 else
2191 {
2192 Lisp_Object frame;
2193
2194 frame = FONTSET_FRAME (fontset);
2195 if (FRAMEP (frame))
2196 {
2197 FRAME_PTR f = XFRAME (frame);
2198
2199 if (FRAME_LIVE_P (f))
1c82b4b3
KH
2200 ASET (vec, 1,
2201 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
452a78e0 2202 else
1c82b4b3
KH
2203 ASET (vec, 1,
2204 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
452a78e0 2205 }
eb36588a
KH
2206 if (!NILP (FONTSET_DEFAULT (fontset)))
2207 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
452a78e0
KH
2208 }
2209 return vec;
2210}
2211
2212DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2213 doc: /* Return a brief summary of all fontsets for debug use. */)
2214 ()
2215{
2216 Lisp_Object val;
2217 int i;
2218
2219 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2220 if (! NILP (AREF (Vfontset_table, i)))
2221 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2222 return (Fnreverse (val));
2223}
2224#endif /* FONTSET_DEBUG */
2225
dfcf069d 2226void
4ed46869
KH
2227syms_of_fontset ()
2228{
1d5d7200 2229 DEFSYM (Qfontset, "fontset");
eb36588a 2230 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
d6aaac9e
KH
2231 DEFSYM (Qfontset_info, "fontset-info");
2232 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
4ed46869 2233
1d5d7200
KH
2234 DEFSYM (Qprepend, "prepend");
2235 DEFSYM (Qappend, "append");
c3fb88cc 2236 DEFSYM (Qlatin, "latin");
4ed46869
KH
2237
2238 Vcached_fontset_data = Qnil;
2239 staticpro (&Vcached_fontset_data);
2240
0d407d77
KH
2241 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2242 staticpro (&Vfontset_table);
0d407d77
KH
2243
2244 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2245 staticpro (&Vdefault_fontset);
1ff005e1
KH
2246 FONTSET_ID (Vdefault_fontset) = make_number (0);
2247 FONTSET_NAME (Vdefault_fontset)
d67b4f80 2248 = make_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
ff7a2223 2249 ASET (Vfontset_table, 0, Vdefault_fontset);
1ff005e1 2250 next_fontset_id = 1;
4ed46869 2251
452a78e0
KH
2252 auto_fontset_alist = Qnil;
2253 staticpro (&auto_fontset_alist);
2254
28e2436a
KH
2255 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2256 doc: /*
2257Alist of charsets vs the charsets to determine the preferred font encoding.
eb152aa9 2258Each element looks like (CHARSET . ENCODING-CHARSET),
28e2436a
KH
2259where ENCODING-CHARSET is a charset registered in the variable
2260`font-encoding-alist' as ENCODING.
2261
2262When a text has a property `charset' and the value is CHARSET, a font
2263whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2264 Vfont_encoding_charset_alist = Qnil;
2265
6a7e6d80 2266 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
1d5d7200
KH
2267 doc: /*
2268Char table of characters whose ascent values should be ignored.
335c5470 2269If an entry for a character is non-nil, the ascent value of the glyph
eb152aa9 2270is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
335c5470
PJ
2271
2272This affects how a composite character which contains
2273such a character is displayed on screen. */);
2aeafb78
KH
2274 Vuse_default_ascent = Qnil;
2275
2276 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
1d5d7200 2277 doc: /*
eb152aa9 2278Char table of characters which are not composed relatively.
335c5470
PJ
2279If an entry for a character is non-nil, a composition sequence
2280which contains that character is displayed so that
2281the glyph of that character is put without considering
2282an ascent and descent value of a previous character. */);
810abb87 2283 Vignore_relative_composition = Qnil;
6a7e6d80 2284
01d4b817 2285 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
335c5470
PJ
2286 doc: /* Alist of fontname vs list of the alternate fontnames.
2287When a specified font name is not found, the corresponding
2288alternate fontnames (if any) are tried instead. */);
01d4b817 2289 Valternate_fontname_alist = Qnil;
8c83e4f9 2290
1c283e35 2291 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
335c5470 2292 doc: /* Alist of fontset names vs the aliases. */);
1ff005e1 2293 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
d67b4f80 2294 make_pure_c_string ("fontset-default")),
1ff005e1 2295 Qnil);
1c283e35 2296
810abb87
KH
2297 DEFVAR_LISP ("vertical-centering-font-regexp",
2298 &Vvertical_centering_font_regexp,
335c5470
PJ
2299 doc: /* *Regexp matching font names that require vertical centering on display.
2300When a character is displayed with such fonts, the character is displayed
fc8865fc 2301at the vertical center of lines. */);
810abb87
KH
2302 Vvertical_centering_font_regexp = Qnil;
2303
21ff5ed6
KH
2304 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2305 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2306 Votf_script_alist = Qnil;
2307
4ed46869
KH
2308 defsubr (&Squery_fontset);
2309 defsubr (&Snew_fontset);
2310 defsubr (&Sset_fontset_font);
1ff005e1 2311 defsubr (&Sinternal_char_font);
4ed46869 2312 defsubr (&Sfontset_info);
0d407d77
KH
2313 defsubr (&Sfontset_font);
2314 defsubr (&Sfontset_list);
452a78e0
KH
2315#ifdef FONTSET_DEBUG
2316 defsubr (&Sfontset_list_all);
2317#endif
e3400864 2318}
92c15c34
KH
2319
2320/* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2321 (do not change this comment) */