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