(doc-view-convert-current-doc): Save the current
[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{
41ad6003 526 Lisp_Object elt, 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);
550 Lisp_Object repertory
551 = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
552
553 if (XINT (repertory) == id)
eaa0402c 554 {
41ad6003 555 charset_matched = i;
581f7ac7 556 break;
41ad6003
KH
557 }
558 }
581f7ac7 559 }
1d5d7200 560
7e1a1cd9 561 /* Find the first available font in the vector of RFONT-DEF. */
41ad6003 562 for (i = 0; i < ASIZE (vec); i++)
0d407d77 563 {
dbf46ba6 564 Lisp_Object font_entity, font_object;
581f7ac7 565
41ad6003
KH
566 if (i == 0 && charset_matched >= 0)
567 {
568 /* Try the element matching with the charset ID at first. */
569 elt = AREF (vec, charset_matched);
570 charset_matched = -1;
571 i--;
572 }
573 else if (i != charset_matched)
574 elt = AREF (vec, i);
575 else
576 continue;
577
6bad8007 578 if (NILP (elt))
41ad6003 579 /* This is a sign of not to try the other fonts. */
eaa0402c 580 return Qt;
581f7ac7 581 if (INTEGERP (RFONT_DEF_FACE (elt))
cf064184 582 && XINT (RFONT_DEF_FACE (elt)) < 0)
1d5d7200
KH
583 /* We couldn't open this font last time. */
584 continue;
585
581f7ac7
KH
586 font_object = RFONT_DEF_OBJECT (elt);
587 if (NILP (font_object))
8aa07a8d 588 {
581f7ac7 589 Lisp_Object font_def = RFONT_DEF_FONT_DEF (elt);
d67f7f1a 590
581f7ac7
KH
591 if (! face)
592 /* We have not yet opened the font. */
593 return Qnil;
594 font_entity = font_find_for_lface (f, face->lface,
595 FONT_DEF_SPEC (font_def), -1);
d67f7f1a 596 if (NILP (font_entity))
8b67c40a 597 {
581f7ac7
KH
598 /* Record that no font matches the spec. */
599 RFONT_DEF_SET_FACE (elt, -1);
600 continue;
d67f7f1a 601 }
ca2b2cde
KH
602 font_object = font_open_for_lface (f, font_entity, face->lface,
603 FONT_DEF_SPEC (font_def));
581f7ac7 604 if (NILP (font_object))
d67f7f1a 605 {
581f7ac7
KH
606 /* Record that the font is unsable. */
607 RFONT_DEF_SET_FACE (elt, -1);
608 continue;
8aa07a8d 609 }
581f7ac7 610 RFONT_DEF_SET_OBJECT (elt, font_object);
8aa07a8d 611 }
eb152aa9 612
581f7ac7
KH
613 if (font_has_char (f, font_object, c))
614 return elt;
1d5d7200 615
581f7ac7
KH
616#if 0
617 /* The following code makes Emacs to find a font for C by fairly
618 exhausitive search. But, that takes long time especially for
619 X font backend. */
620
621 /* Try to find the different font maching with the current spec
622 and support C. */
623 font_def = RFONT_DEF_FONT_DEF (elt);
624 for (i++; i < ASIZE (vec); i++)
57e13af9 625 {
581f7ac7
KH
626 if (! EQ (RFONT_DEF_FONT_DEF (AREF (vec, i)), font_def))
627 break;
628 if (font_has_char (f, RFONT_DEF_OBJECT (AREF (vec, i)), c))
629 return AREF (vec, i);
57e13af9 630 }
581f7ac7
KH
631 /* Find an font-entity that support C. */
632 font_entity = font_find_for_lface (f, face->lface,
633 FONT_DEF_SPEC (font_def), c);
634 if (! NILP (font_entity))
1d5d7200 635 {
581f7ac7
KH
636 Lisp_Object rfont_def, new_vec;
637 int j;
1d5d7200 638
581f7ac7
KH
639 font_object = font_open_for_lface (f, font_entity, face->lface,
640 Qnil);
641 RFONT_DEF_NEW (rfont_def, font_def);
642 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
643 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (elt));
644 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
645 for (j = 0; j < i; j++)
646 ASET (new_vec, j, AREF (vec, j));
647 ASET (new_vec, j, rfont_def);
648 for (j++; j < ASIZE (new_vec); j++)
649 ASET (new_vec, j, AREF (vec, j - 1));
650 vec = new_vec;
651 return rfont_def;
1d5d7200 652 }
581f7ac7
KH
653 i--;
654#endif /* 0 */
0d407d77 655 }
d67f7f1a 656
581f7ac7 657 FONTSET_SET (fontset, make_number (c), make_number (0));
eaa0402c 658 return Qnil;
1c82b4b3 659}
0d407d77 660
cc7b6145 661
1c82b4b3
KH
662static Lisp_Object
663fontset_font (fontset, c, face, id)
664 Lisp_Object fontset;
665 int c;
666 struct face *face;
667 int id;
668{
669 Lisp_Object rfont_def;
670 Lisp_Object base_fontset;
cc7b6145 671
41ad6003 672 /* Try a font-group of FONTSET. */
1c82b4b3 673 rfont_def = fontset_find_font (fontset, c, face, id, 0);
eaa0402c
KH
674 if (VECTORP (rfont_def))
675 return rfont_def;
676 if (EQ (rfont_def, Qt))
677 return Qnil;
41ad6003
KH
678
679 /* Try a font-group of the default fontset. */
680 base_fontset = FONTSET_BASE (fontset);
681 if (! EQ (base_fontset, Vdefault_fontset))
682 {
683 if (NILP (FONTSET_DEFAULT (fontset)))
684 FONTSET_DEFAULT (fontset)
685 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
686 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
687 if (VECTORP (rfont_def))
688 return rfont_def;
689 if (EQ (rfont_def, Qt))
690 return Qnil;
691 }
692
693 /* Try a fallback font-group of FONTSET. */
1c82b4b3 694 rfont_def = fontset_find_font (fontset, c, face, id, 1);
dbf46ba6
KH
695 if (VECTORP (rfont_def))
696 return rfont_def;
697 if (EQ (rfont_def, Qt))
698 return Qnil;
d67f7f1a 699
dbf46ba6 700 /* Try a fallback font-group of the default fontset . */
41ad6003
KH
701 if (! EQ (base_fontset, Vdefault_fontset))
702 {
703 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
704 if (VECTORP (rfont_def))
705 return rfont_def;
706 }
707
8657206e 708 /* Remember that we have no font for C. */
41ad6003 709 FONTSET_SET (fontset, make_number (c), Qt);
d67f7f1a 710
41ad6003 711 return Qnil;
0d407d77
KH
712}
713
0d407d77 714/* Return a newly created fontset with NAME. If BASE is nil, make a
06f76f0d 715 base fontset. Otherwise make a realized fontset whose base is
0d407d77
KH
716 BASE. */
717
718static Lisp_Object
719make_fontset (frame, name, base)
720 Lisp_Object frame, name, base;
4ed46869 721{
1337ac77 722 Lisp_Object fontset;
0d407d77
KH
723 int size = ASIZE (Vfontset_table);
724 int id = next_fontset_id;
0d407d77
KH
725
726 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
727 the next available fontset ID. So it is expected that this loop
728 terminates quickly. In addition, as the last element of
fc8865fc 729 Vfontset_table is always nil, we don't have to check the range of
0d407d77
KH
730 id. */
731 while (!NILP (AREF (Vfontset_table, id))) id++;
732
733 if (id + 1 == size)
63655c83 734 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
4ed46869 735
11d9bd93 736 fontset = Fmake_char_table (Qfontset, Qnil);
0d407d77
KH
737
738 FONTSET_ID (fontset) = make_number (id);
06f76f0d
KH
739 if (NILP (base))
740 {
741 FONTSET_NAME (fontset) = name;
742 }
743 else
744 {
745 FONTSET_NAME (fontset) = Qnil;
746 FONTSET_FRAME (fontset) = frame;
747 FONTSET_BASE (fontset) = base;
748 }
0d407d77 749
06f76f0d 750 ASET (Vfontset_table, id, fontset);
0d407d77
KH
751 next_fontset_id = id + 1;
752 return fontset;
4ed46869
KH
753}
754
0d407d77 755
2cdd4f88
MB
756/* Set the ASCII font of the default fontset to FONTNAME if that is
757 not yet set. */
758void
759set_default_ascii_font (fontname)
0d407d77
KH
760 Lisp_Object fontname;
761{
d7fb7b91 762 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
2cdd4f88
MB
763 {
764 int id = fs_query_fontset (fontname, 2);
765
766 if (id >= 0)
d7fb7b91
KH
767 fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
768 FONTSET_ASCII (Vdefault_fontset)= fontname;
2cdd4f88 769 }
0d407d77
KH
770}
771
772\f
1d5d7200 773/********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
0d407d77 774
1d5d7200 775/* Return the name of the fontset who has ID. */
0d407d77
KH
776
777Lisp_Object
778fontset_name (id)
779 int id;
780{
781 Lisp_Object fontset;
06f76f0d 782
0d407d77
KH
783 fontset = FONTSET_FROM_ID (id);
784 return FONTSET_NAME (fontset);
785}
786
787
1d5d7200 788/* Return the ASCII font name of the fontset who has ID. */
0d407d77
KH
789
790Lisp_Object
791fontset_ascii (id)
792 int id;
793{
794 Lisp_Object fontset, elt;
06f76f0d 795
0d407d77
KH
796 fontset= FONTSET_FROM_ID (id);
797 elt = FONTSET_ASCII (fontset);
8aa07a8d
KH
798 if (CONSP (elt))
799 elt = XCAR (elt);
1d5d7200 800 return elt;
0d407d77
KH
801}
802
9459b1e9
KH
803void
804free_realized_fontset (f, fontset)
805 FRAME_PTR f;
806 Lisp_Object fontset;
807{
9459b1e9
KH
808 Lisp_Object tail;
809
810 return;
811 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
812 {
813 xassert (FONT_OBJECT_P (XCAR (tail)));
814 font_close_object (f, XCAR (tail));
815 }
816}
0d407d77 817
06f76f0d
KH
818/* Free fontset of FACE defined on frame F. Called from
819 free_realized_face. */
0d407d77 820
4ed46869 821void
0d407d77
KH
822free_face_fontset (f, face)
823 FRAME_PTR f;
824 struct face *face;
4ed46869 825{
452a78e0
KH
826 Lisp_Object fontset;
827
ff7a2223 828 fontset = FONTSET_FROM_ID (face->fontset);
581f7ac7
KH
829 if (NILP (fontset))
830 return;
831 xassert (! BASE_FONTSET_P (fontset));
452a78e0 832 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
9459b1e9 833 free_realized_fontset (f, fontset);
1d5d7200 834 ASET (Vfontset_table, face->fontset, Qnil);
06f76f0d
KH
835 if (face->fontset < next_fontset_id)
836 next_fontset_id = face->fontset;
eb36588a 837 if (! NILP (FONTSET_DEFAULT (fontset)))
452a78e0 838 {
e1a14cdc 839 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
eb152aa9 840
452a78e0
KH
841 fontset = AREF (Vfontset_table, id);
842 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
843 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
9459b1e9 844 free_realized_fontset (f, fontset);
452a78e0
KH
845 ASET (Vfontset_table, id, Qnil);
846 if (id < next_fontset_id)
847 next_fontset_id = face->fontset;
848 }
581f7ac7 849 face->fontset = -1;
0d407d77 850}
18998710 851
0d407d77 852
e0f24100 853/* Return 1 if FACE is suitable for displaying character C.
0d407d77 854 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
06f76f0d 855 when C is not an ASCII character. */
0d407d77
KH
856
857int
858face_suitable_for_char_p (face, c)
859 struct face *face;
860 int c;
861{
7e1a1cd9 862 Lisp_Object fontset, rfont_def;
0d407d77 863
0d407d77 864 fontset = FONTSET_FROM_ID (face->fontset);
7e1a1cd9
KH
865 rfont_def = fontset_font (fontset, c, NULL, -1);
866 return (VECTORP (rfont_def)
581f7ac7
KH
867 && INTEGERP (RFONT_DEF_FACE (rfont_def))
868 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
0d407d77
KH
869}
870
871
872/* Return ID of face suitable for displaying character C on frame F.
1d5d7200
KH
873 FACE must be reazlied for ASCII characters in advance. Called from
874 the macro FACE_FOR_CHAR. */
0d407d77
KH
875
876int
6bad8007 877face_for_char (f, face, c, pos, object)
0d407d77
KH
878 FRAME_PTR f;
879 struct face *face;
6bad8007
KH
880 int c, pos;
881 Lisp_Object object;
0d407d77 882{
303d5190 883 Lisp_Object fontset, rfont_def, charset;
7e1a1cd9 884 int face_id;
6bad8007 885 int id;
1d5d7200 886
ea217c11
KH
887 /* If face->fontset is negative (that happens when no font is found
888 for face), just return face->ascii_face because we can't do
889 anything. Perhaps, we should fix the callers to assure
890 that face->fontset is always valid. */
891 if (ASCII_CHAR_P (c) || face->fontset < 0)
1d5d7200 892 return face->ascii_face->id;
0d407d77
KH
893
894 xassert (fontset_id_valid_p (face->fontset));
895 fontset = FONTSET_FROM_ID (face->fontset);
896 xassert (!BASE_FONTSET_P (fontset));
41ad6003 897
6bad8007 898 if (pos < 0)
303d5190
KH
899 {
900 id = -1;
901 charset = Qnil;
902 }
6bad8007
KH
903 else
904 {
905 charset = Fget_char_property (make_number (pos), Qcharset, object);
b1bde622 906 if (CHARSETP (charset))
28e2436a
KH
907 {
908 Lisp_Object val;
909
a124bbb6 910 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
28e2436a
KH
911 if (CONSP (val) && CHARSETP (XCDR (val)))
912 charset = XCDR (val);
913 id = XINT (CHARSET_SYMBOL_ID (charset));
914 }
b1bde622
KH
915 else
916 id = -1;
6bad8007 917 }
b59e66b3 918
cd872f08 919 font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
7e1a1cd9
KH
920 rfont_def = fontset_font (fontset, c, face, id);
921 if (VECTORP (rfont_def))
922 {
581f7ac7
KH
923 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
924 face_id = XINT (RFONT_DEF_FACE (rfont_def));
8aa07a8d 925 else
7e1a1cd9 926 {
581f7ac7 927 Lisp_Object font_object;
7e1a1cd9 928
581f7ac7
KH
929 font_object = RFONT_DEF_OBJECT (rfont_def);
930 face_id = face_for_font (f, font_object, face);
931 RFONT_DEF_SET_FACE (rfont_def, face_id);
7e1a1cd9 932 }
7e1a1cd9 933 }
581f7ac7 934 else
7e1a1cd9 935 {
581f7ac7
KH
936 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
937 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
938 else
939 {
940 face_id = face_for_font (f, Qnil, face);
941 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
942 }
7e1a1cd9 943 }
581f7ac7
KH
944 xassert (face_id >= 0);
945 return face_id;
0d407d77
KH
946}
947
948
514cf218
KH
949Lisp_Object
950font_for_char (face, c, pos, object)
951 struct face *face;
952 int c, pos;
953 Lisp_Object object;
954{
955 Lisp_Object fontset, rfont_def, charset;
956 int face_id;
957 int id;
958
959 if (ASCII_CHAR_P (c))
960 {
961 Lisp_Object font_object;
962
963 XSETFONT (font_object, face->ascii_face->font);
964 return font_object;
965 }
966
967 xassert (fontset_id_valid_p (face->fontset));
968 fontset = FONTSET_FROM_ID (face->fontset);
969 xassert (!BASE_FONTSET_P (fontset));
970 if (pos < 0)
971 {
972 id = -1;
973 charset = Qnil;
974 }
975 else
976 {
977 charset = Fget_char_property (make_number (pos), Qcharset, object);
b1bde622 978 if (CHARSETP (charset))
514cf218
KH
979 {
980 Lisp_Object val;
981
9fe78804 982 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
514cf218
KH
983 if (CONSP (val) && CHARSETP (XCDR (val)))
984 charset = XCDR (val);
985 id = XINT (CHARSET_SYMBOL_ID (charset));
986 }
b1bde622
KH
987 else
988 id = -1;
514cf218
KH
989 }
990
991 font_deferred_log ("font for", Fcons (make_number (c), charset), Qnil);
992 rfont_def = fontset_font (fontset, c, face, id);
993 return (VECTORP (rfont_def)
994 ? RFONT_DEF_OBJECT (rfont_def)
995 : Qnil);
996}
997
998
0d407d77
KH
999/* Make a realized fontset for ASCII face FACE on frame F from the
1000 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1001 default fontset as the base. Value is the id of the new fontset.
1002 Called from realize_x_face. */
1003
1004int
1d5d7200 1005make_fontset_for_ascii_face (f, base_fontset_id, face)
0d407d77
KH
1006 FRAME_PTR f;
1007 int base_fontset_id;
1d5d7200 1008 struct face *face;
0d407d77 1009{
1337ac77 1010 Lisp_Object base_fontset, fontset, frame;
0d407d77
KH
1011
1012 XSETFRAME (frame, f);
1013 if (base_fontset_id >= 0)
1014 {
1015 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1016 if (!BASE_FONTSET_P (base_fontset))
1017 base_fontset = FONTSET_BASE (base_fontset);
1d5d7200
KH
1018 if (! BASE_FONTSET_P (base_fontset))
1019 abort ();
4ed46869 1020 }
0d407d77
KH
1021 else
1022 base_fontset = Vdefault_fontset;
1023
1024 fontset = make_fontset (frame, Qnil, base_fontset);
f3231837 1025 return XINT (FONTSET_ID (fontset));
0d407d77
KH
1026}
1027
0d407d77 1028\f
1d5d7200 1029
4ed46869
KH
1030/* Cache data used by fontset_pattern_regexp. The car part is a
1031 pattern string containing at least one wild card, the cdr part is
1032 the corresponding regular expression. */
1033static Lisp_Object Vcached_fontset_data;
1034
dbf46ba6 1035#define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
7539e11f 1036#define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
4ed46869
KH
1037
1038/* If fontset name PATTERN contains any wild card, return regular
1039 expression corresponding to PATTERN. */
1040
0d407d77 1041static Lisp_Object
4ed46869
KH
1042fontset_pattern_regexp (pattern)
1043 Lisp_Object pattern;
1044{
dbf46ba6
KH
1045 if (!index ((char *) SDATA (pattern), '*')
1046 && !index ((char *) SDATA (pattern), '?'))
4ed46869 1047 /* PATTERN does not contain any wild cards. */
1c283e35 1048 return Qnil;
4ed46869
KH
1049
1050 if (!CONSP (Vcached_fontset_data)
dbf46ba6 1051 || strcmp ((char *) SDATA (pattern), CACHED_FONTSET_NAME))
4ed46869
KH
1052 {
1053 /* We must at first update the cached data. */
6cc06608 1054 unsigned char *regex, *p0, *p1;
821bc4db 1055 int ndashes = 0, nstars = 0, nescs = 0;
6cc06608 1056
fc1062f5
KH
1057 for (p0 = SDATA (pattern); *p0; p0++)
1058 {
1059 if (*p0 == '-')
1060 ndashes++;
a653f812 1061 else if (*p0 == '*')
fc1062f5 1062 nstars++;
821bc4db
KH
1063 else if (*p0 == '['
1064 || *p0 == '.' || *p0 == '\\'
8510724d 1065 || *p0 == '+' || *p0 == '^'
821bc4db
KH
1066 || *p0 == '$')
1067 nescs++;
fc1062f5
KH
1068 }
1069
1070 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1071 we convert "*" to "[^-]*" which is much faster in regular
1072 expression matching. */
1073 if (ndashes < 14)
821bc4db 1074 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
fc1062f5 1075 else
821bc4db 1076 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
4ed46869 1077
1c283e35 1078 *p1++ = '^';
6cc06608 1079 for (p0 = SDATA (pattern); *p0; p0++)
4ed46869 1080 {
a653f812 1081 if (*p0 == '*')
4ed46869 1082 {
fc1062f5
KH
1083 if (ndashes < 14)
1084 *p1++ = '.';
1085 else
1086 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1c283e35 1087 *p1++ = '*';
4ed46869 1088 }
1c283e35 1089 else if (*p0 == '?')
d96d677d 1090 *p1++ = '.';
821bc4db
KH
1091 else if (*p0 == '['
1092 || *p0 == '.' || *p0 == '\\'
8510724d 1093 || *p0 == '+' || *p0 == '^'
821bc4db
KH
1094 || *p0 == '$')
1095 *p1++ = '\\', *p1++ = *p0;
1c283e35
KH
1096 else
1097 *p1++ = *p0;
4ed46869
KH
1098 }
1099 *p1++ = '$';
1100 *p1++ = 0;
1101
dbf46ba6
KH
1102 Vcached_fontset_data = Fcons (build_string ((char *) SDATA (pattern)),
1103 build_string ((char *) regex));
4ed46869
KH
1104 }
1105
1106 return CACHED_FONTSET_REGEX;
1107}
1108
0d407d77 1109/* Return ID of the base fontset named NAME. If there's no such
a653f812
KH
1110 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1111 0: pattern containing '*' and '?' as wildcards
1112 1: regular expression
1113 2: literal fontset name
1114*/
0d407d77
KH
1115
1116int
a653f812 1117fs_query_fontset (name, name_pattern)
0d407d77 1118 Lisp_Object name;
a653f812 1119 int name_pattern;
0d407d77 1120{
1337ac77 1121 Lisp_Object tem;
0d407d77
KH
1122 int i;
1123
1124 name = Fdowncase (name);
a653f812 1125 if (name_pattern != 1)
0d407d77
KH
1126 {
1127 tem = Frassoc (name, Vfontset_alias_alist);
6bad8007
KH
1128 if (NILP (tem))
1129 tem = Fassoc (name, Vfontset_alias_alist);
0d407d77
KH
1130 if (CONSP (tem) && STRINGP (XCAR (tem)))
1131 name = XCAR (tem);
a653f812 1132 else if (name_pattern == 0)
0d407d77
KH
1133 {
1134 tem = fontset_pattern_regexp (name);
1135 if (STRINGP (tem))
1136 {
1137 name = tem;
a653f812 1138 name_pattern = 1;
0d407d77
KH
1139 }
1140 }
1141 }
1142
1143 for (i = 0; i < ASIZE (Vfontset_table); i++)
1144 {
6e1b0d8c 1145 Lisp_Object fontset, this_name;
0d407d77
KH
1146
1147 fontset = FONTSET_FROM_ID (i);
1148 if (NILP (fontset)
1149 || !BASE_FONTSET_P (fontset))
1150 continue;
1151
6e1b0d8c 1152 this_name = FONTSET_NAME (fontset);
a653f812 1153 if (name_pattern == 1
581f7ac7 1154 ? fast_string_match_ignore_case (name, this_name) >= 0
05131107 1155 : !xstrcasecmp (SDATA (name), SDATA (this_name)))
0d407d77
KH
1156 return i;
1157 }
1158 return -1;
1159}
1160
1161
727fb790 1162DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
335c5470
PJ
1163 doc: /* Return the name of a fontset that matches PATTERN.
1164The value is nil if there is no matching fontset.
1165PATTERN can contain `*' or `?' as a wildcard
1166just as X font name matching algorithm allows.
1167If REGEXPP is non-nil, PATTERN is a regular expression. */)
1168 (pattern, regexpp)
727fb790 1169 Lisp_Object pattern, regexpp;
4ed46869 1170{
0d407d77
KH
1171 Lisp_Object fontset;
1172 int id;
4ed46869
KH
1173
1174 (*check_window_system_func) ();
1175
b7826503 1176 CHECK_STRING (pattern);
4ed46869 1177
d5db4077 1178 if (SCHARS (pattern) == 0)
4ed46869
KH
1179 return Qnil;
1180
0d407d77
KH
1181 id = fs_query_fontset (pattern, !NILP (regexpp));
1182 if (id < 0)
1183 return Qnil;
4ed46869 1184
0d407d77
KH
1185 fontset = FONTSET_FROM_ID (id);
1186 return FONTSET_NAME (fontset);
4ed46869
KH
1187}
1188
06f76f0d 1189/* Return a list of base fontset names matching PATTERN on frame F. */
4ed46869
KH
1190
1191Lisp_Object
1192list_fontsets (f, pattern, size)
1193 FRAME_PTR f;
1194 Lisp_Object pattern;
1195 int size;
1196{
1337ac77 1197 Lisp_Object frame, regexp, val;
0d407d77 1198 int id;
4ed46869 1199
0d407d77 1200 XSETFRAME (frame, f);
4ed46869 1201
0d407d77 1202 regexp = fontset_pattern_regexp (pattern);
4ed46869 1203 val = Qnil;
4ed46869 1204
0d407d77
KH
1205 for (id = 0; id < ASIZE (Vfontset_table); id++)
1206 {
6e1b0d8c 1207 Lisp_Object fontset, name;
0d407d77
KH
1208
1209 fontset = FONTSET_FROM_ID (id);
1210 if (NILP (fontset)
1211 || !BASE_FONTSET_P (fontset)
1212 || !EQ (frame, FONTSET_FRAME (fontset)))
1213 continue;
6e1b0d8c 1214 name = FONTSET_NAME (fontset);
0d407d77 1215
1d5d7200 1216 if (STRINGP (regexp)
6e1b0d8c 1217 ? (fast_string_match (regexp, name) < 0)
dbf46ba6 1218 : strcmp ((char *) SDATA (pattern), (char *) SDATA (name)))
0d407d77
KH
1219 continue;
1220
0d407d77 1221 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
4ed46869
KH
1222 }
1223
1224 return val;
1225}
1226
4ed46869 1227
8f924df7 1228/* Free all realized fontsets whose base fontset is BASE. */
4ed46869 1229
06f76f0d
KH
1230static void
1231free_realized_fontsets (base)
1232 Lisp_Object base;
1233{
1234 int id;
4ed46869 1235
2f034212 1236#if 0
27e20b2f
KH
1237 /* For the moment, this doesn't work because free_realized_face
1238 doesn't remove FACE from a cache. Until we find a solution, we
1239 suppress this code, and simply use Fclear_face_cache even though
1240 that is not efficient. */
06f76f0d
KH
1241 BLOCK_INPUT;
1242 for (id = 0; id < ASIZE (Vfontset_table); id++)
4ed46869 1243 {
06f76f0d 1244 Lisp_Object this = AREF (Vfontset_table, id);
0d407d77 1245
06f76f0d 1246 if (EQ (FONTSET_BASE (this), base))
0d407d77 1247 {
06f76f0d 1248 Lisp_Object tail;
4ed46869 1249
06f76f0d
KH
1250 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1251 tail = XCDR (tail))
1252 {
1253 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1254 int face_id = XINT (XCDR (XCAR (tail)));
1255 struct face *face = FACE_FROM_ID (f, face_id);
4ed46869 1256
06f76f0d
KH
1257 /* Face THIS itself is also freed by the following call. */
1258 free_realized_face (f, face);
1259 }
1260 }
0d407d77 1261 }
06f76f0d 1262 UNBLOCK_INPUT;
27e20b2f 1263#else /* not 0 */
2f034212
KH
1264 /* But, we don't have to call Fclear_face_cache if no fontset has
1265 been realized from BASE. */
1266 for (id = 0; id < ASIZE (Vfontset_table); id++)
1267 {
1268 Lisp_Object this = AREF (Vfontset_table, id);
1269
985773c9 1270 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
2f034212
KH
1271 {
1272 Fclear_face_cache (Qt);
1273 break;
1274 }
1275 }
27e20b2f 1276#endif /* not 0 */
0d407d77 1277}
4ed46869 1278
4ed46869 1279
0d407d77
KH
1280/* Check validity of NAME as a fontset name and return the
1281 corresponding fontset. If not valid, signal an error.
1282 If NAME is t, return Vdefault_fontset. */
1283
1284static Lisp_Object
1285check_fontset_name (name)
1286 Lisp_Object name;
1287{
1288 int id;
1289
1290 if (EQ (name, Qt))
1291 return Vdefault_fontset;
4ed46869 1292
b7826503 1293 CHECK_STRING (name);
a653f812
KH
1294 /* First try NAME as literal. */
1295 id = fs_query_fontset (name, 2);
1296 if (id < 0)
1297 /* For backward compatibility, try again NAME as pattern. */
1298 id = fs_query_fontset (name, 0);
0d407d77 1299 if (id < 0)
d5db4077 1300 error ("Fontset `%s' does not exist", SDATA (name));
0d407d77
KH
1301 return FONTSET_FROM_ID (id);
1302}
4ed46869 1303
1d5d7200
KH
1304static void
1305accumulate_script_ranges (arg, range, val)
1306 Lisp_Object arg, range, val;
1307{
1308 if (EQ (XCAR (arg), val))
1309 {
1310 if (CONSP (range))
1311 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1312 else
1313 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1314 }
1315}
1316
1317
d6aaac9e 1318/* Return an ASCII font name generated from fontset name NAME and
7ff614ed 1319 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
d6aaac9e
KH
1320
1321static INLINE Lisp_Object
1322generate_ascii_font_name (name, ascii_spec)
1323 Lisp_Object name, ascii_spec;
1324{
7ff614ed 1325 Lisp_Object font_spec = Ffont_spec (0, NULL);
d6aaac9e 1326 int i;
7ff614ed 1327 char xlfd[256];
d6aaac9e 1328
dbf46ba6 1329 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
7ff614ed 1330 error ("Not an XLFD font name: %s", SDATA (name));
581f7ac7 1331 for (i = FONT_FOUNDRY_INDEX; i < FONT_EXTRA_INDEX; i++)
d6aaac9e 1332 if (! NILP (AREF (ascii_spec, i)))
7ff614ed
KH
1333 ASET (font_spec, i, AREF (ascii_spec, i));
1334 i = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1335 if (i < 0)
1336 error ("Not an XLFD font name: %s", SDATA (name));
1337 return make_unibyte_string (xlfd, i);
d6aaac9e
KH
1338}
1339
fb78e2ed
KH
1340/* Variables referred in set_fontset_font. They are set before
1341 map_charset_chars is called in Fset_fontset_font. */
1342static Lisp_Object font_def_arg, add_arg;
1343static int from_arg, to_arg;
1344
1345/* Callback function for map_charset_chars in Fset_fontset_font. In
1346 FONTSET, set font_def_arg in a fashion specified by add_arg for
1347 characters in RANGE while ignoring the range between from_arg and
1348 to_arg. */
1349
2449d4d0 1350static void
fb78e2ed
KH
1351set_fontset_font (fontset, range)
1352 Lisp_Object fontset, range;
2449d4d0 1353{
fb78e2ed
KH
1354 if (from_arg < to_arg)
1355 {
1356 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
2449d4d0 1357
fb78e2ed
KH
1358 if (from < from_arg)
1359 {
1360 if (to > to_arg)
1361 {
1362 Lisp_Object range2;
1363
1364 range2 = Fcons (make_number (to_arg), XCDR (range));
1365 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1366 to = to_arg;
1367 }
1368 if (to > from_arg)
1369 range = Fcons (XCAR (range), make_number (from_arg));
1370 }
1371 else if (to <= to_arg)
1372 return;
1373 else
1374 {
1375 if (from < to_arg)
1376 range = Fcons (make_number (to_arg), XCDR (range));
1377 }
1378 }
1379 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
2449d4d0
KH
1380}
1381
8b67c40a 1382extern Lisp_Object QCfamily, QCregistry;
d6aaac9e 1383
1d5d7200 1384DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
8f924df7 1385 doc: /*
eb36588a 1386Modify fontset NAME to use FONT-SPEC for TARGET characters.
335c5470 1387
eb36588a
KH
1388TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1389In that case, use FONT-SPEC for all characters in the range FROM and
1390TO (inclusive).
06f76f0d 1391
eb36588a
KH
1392TARGET may be a script name symbol. In that case, use FONT-SPEC for
1393all characters that belong to the script.
06f76f0d 1394
eb36588a 1395TARGET may be a charset. In that case, use FONT-SPEC for all
95318a38 1396characters in the charset.
1d5d7200 1397
eb36588a
KH
1398TARGET may be nil. In that case, use FONT-SPEC for any characters for
1399that no FONT-SPEC is specified.
1400
0fdf26e6 1401FONT-SPEC may one of these:
581f7ac7 1402 * A font-spec object made by the function `font-spec' (which see).
3dcd48dd 1403 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
581f7ac7
KH
1404 REGISTRY is a font registry name. FAMILY may contain foundry
1405 name, and REGISTRY may contain encoding name.
00c4da0f 1406 * A font name string.
eaa0402c 1407 * nil, which explicitly specifies that there's no font for TARGET.
1d5d7200
KH
1408
1409Optional 4th argument FRAME, if non-nil, is a frame. This argument is
1410kept for backward compatibility and has no meaning.
1411
1412Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
eb36588a 1413to the font specifications for TARGET previously set. If it is
1d5d7200
KH
1414`prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1415appended. By default, FONT-SPEC overrides the previous settings. */)
eb36588a
KH
1416 (name, target, font_spec, frame, add)
1417 Lisp_Object name, target, font_spec, frame, add;
0d407d77 1418{
06f76f0d 1419 Lisp_Object fontset;
9683749a 1420 Lisp_Object font_def, registry, family;
1d5d7200 1421 Lisp_Object range_list;
fb78e2ed 1422 struct charset *charset = NULL;
0d407d77
KH
1423
1424 fontset = check_fontset_name (name);
1425
1d5d7200
KH
1426 /* The arg FRAME is kept for backward compatibility. We only check
1427 the validity. */
1428 if (!NILP (frame))
1429 CHECK_LIVE_FRAME (frame);
1430
581f7ac7 1431 if (CONSP (font_spec))
0d407d77 1432 {
581f7ac7 1433 Lisp_Object spec = Ffont_spec (0, NULL);
1d5d7200 1434
581f7ac7
KH
1435 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1436 font_spec = spec;
0890801b 1437 }
d67f7f1a 1438 else if (STRINGP (font_spec))
4ed46869 1439 {
ea0162a6 1440 Lisp_Object args[2];
581f7ac7 1441 extern Lisp_Object QCname;
ea0162a6 1442
ea0162a6
KH
1443 args[0] = QCname;
1444 args[1] = font_spec;
1445 font_spec = Ffont_spec (2, args);
0d407d77 1446 }
581f7ac7
KH
1447 else if (! NILP (font_spec) && ! FONT_SPEC_P (font_spec))
1448 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1d5d7200 1449
d67f7f1a 1450 if (! NILP (font_spec))
57e13af9 1451 {
581f7ac7
KH
1452 Lisp_Object encoding, repertory;
1453
d67f7f1a 1454 family = AREF (font_spec, FONT_FAMILY_INDEX);
581f7ac7 1455 if (! NILP (family) )
d67f7f1a
KH
1456 family = SYMBOL_NAME (family);
1457 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
581f7ac7
KH
1458 if (! NILP (registry))
1459 registry = Fdowncase (SYMBOL_NAME (registry));
1460 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1461 registry));
d67f7f1a
KH
1462 if (NILP (encoding))
1463 encoding = Qascii;
1464
1465 if (SYMBOLP (encoding))
1466 {
1467 CHECK_CHARSET (encoding);
1468 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1469 }
1470 else
57e13af9 1471 {
d67f7f1a
KH
1472 repertory = XCDR (encoding);
1473 encoding = XCAR (encoding);
1474 CHECK_CHARSET (encoding);
1475 encoding = CHARSET_SYMBOL_ID (encoding);
1476 if (! NILP (repertory) && SYMBOLP (repertory))
1477 {
1478 CHECK_CHARSET (repertory);
1479 repertory = CHARSET_SYMBOL_ID (repertory);
1480 }
57e13af9 1481 }
581f7ac7 1482 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
0d407d77 1483 }
d67f7f1a
KH
1484 else
1485 font_def = Qnil;
4ed46869 1486
eb36588a
KH
1487 if (CHARACTERP (target))
1488 range_list = Fcons (Fcons (target, target), Qnil);
1489 else if (CONSP (target))
0d407d77 1490 {
06f76f0d
KH
1491 Lisp_Object from, to;
1492
eb36588a
KH
1493 from = Fcar (target);
1494 to = Fcdr (target);
06f76f0d
KH
1495 CHECK_CHARACTER (from);
1496 CHECK_CHARACTER (to);
eb36588a 1497 range_list = Fcons (target, Qnil);
4ed46869 1498 }
eb36588a 1499 else if (SYMBOLP (target) && !NILP (target))
8a9be3ac 1500 {
1d5d7200
KH
1501 Lisp_Object script_list;
1502 Lisp_Object val;
0d407d77 1503
1d5d7200
KH
1504 range_list = Qnil;
1505 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
eb36588a 1506 if (! NILP (Fmemq (target, script_list)))
afe93d01 1507 {
eb36588a 1508 val = Fcons (target, Qnil);
1d5d7200 1509 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
8f924df7
KH
1510 val);
1511 range_list = XCDR (val);
432cfa54 1512 if (EQ (target, Qlatin) && NILP (FONTSET_ASCII (fontset)))
c3fb88cc
KH
1513 {
1514 if (VECTORP (font_spec))
1515 val = generate_ascii_font_name (FONTSET_NAME (fontset),
1516 font_spec);
1517 else
1518 val = font_spec;
1519 FONTSET_ASCII (fontset) = val;
1520 }
afe93d01 1521 }
fb78e2ed 1522 if (CHARSETP (target))
afe93d01 1523 {
432cfa54 1524 if (EQ (target, Qascii) && NILP (FONTSET_ASCII (fontset)))
862aa7f9 1525 {
d6aaac9e 1526 if (VECTORP (font_spec))
862aa7f9
KH
1527 font_spec = generate_ascii_font_name (FONTSET_NAME (fontset),
1528 font_spec);
1529 FONTSET_ASCII (fontset) = font_spec;
2449d4d0
KH
1530 range_list = Fcons (Fcons (make_number (0), make_number (127)),
1531 Qnil);
1532 }
1533 else
1534 {
fb78e2ed 1535 CHECK_CHARSET_GET_CHARSET (target, charset);
862aa7f9 1536 }
afe93d01 1537 }
fb78e2ed 1538 else if (NILP (range_list))
1d5d7200 1539 error ("Invalid script or charset name: %s",
eb36588a 1540 SDATA (SYMBOL_NAME (target)));
8a9be3ac 1541 }
eb36588a
KH
1542 else if (NILP (target))
1543 range_list = Fcons (Qnil, Qnil);
1544 else
1545 error ("Invalid target for setting a font");
0d407d77 1546
fb78e2ed
KH
1547
1548 if (charset)
1549 {
1550 font_def_arg = font_def;
1551 add_arg = add;
1552 if (NILP (range_list))
1553 from_arg = to_arg = 0;
1554 else
1555 from_arg = XINT (XCAR (XCAR (range_list))),
1556 to_arg = XINT (XCDR (XCAR (range_list)));
1557
1558 map_charset_chars (set_fontset_font, Qnil, fontset, charset,
1559 CHARSET_MIN_CODE (charset),
1560 CHARSET_MAX_CODE (charset));
1561 }
1d5d7200
KH
1562 for (; CONSP (range_list); range_list = XCDR (range_list))
1563 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
4ed46869 1564
06f76f0d
KH
1565 /* Free all realized fontsets whose base is FONTSET. This way, the
1566 specified character(s) are surely redisplayed by a correct
1567 font. */
1568 free_realized_fontsets (fontset);
4ed46869 1569
4ed46869
KH
1570 return Qnil;
1571}
1572
06f76f0d
KH
1573
1574DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1575 doc: /* Create a new fontset NAME from font information in FONTLIST.
1576
1d5d7200 1577FONTLIST is an alist of scripts vs the corresponding font specification list.
d6aaac9e
KH
1578Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1579character of SCRIPT is displayed by a font that matches one of
1580FONT-SPEC.
06f76f0d 1581
d6aaac9e
KH
1582SCRIPT is a symbol that appears in the first extra slot of the
1583char-table `char-script-table'.
06f76f0d 1584
1d5d7200
KH
1585FONT-SPEC is a vector, a cons, or a string. See the documentation of
1586`set-fontset-font' for the meaning. */)
06f76f0d
KH
1587 (name, fontlist)
1588 Lisp_Object name, fontlist;
1589{
1d5d7200 1590 Lisp_Object fontset;
1d5d7200 1591 int id;
06f76f0d
KH
1592
1593 CHECK_STRING (name);
1594 CHECK_LIST (fontlist);
1595
581f7ac7 1596 name = Fdowncase (name);
1d5d7200
KH
1597 id = fs_query_fontset (name, 0);
1598 if (id < 0)
0d407d77 1599 {
581f7ac7
KH
1600 Lisp_Object font_spec = Ffont_spec (0, NULL);
1601 Lisp_Object short_name;
5f2d79e0 1602 char xlfd[256];
581f7ac7
KH
1603 int len;
1604
dbf46ba6 1605 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
d6aaac9e 1606 error ("Fontset name must be in XLFD format");
581f7ac7 1607 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
dbf46ba6 1608 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
581f7ac7
KH
1609 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1610 error ("Registry field of fontset name must be \"fontset-*\"");
1611 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1612 Vfontset_alias_alist);
1613 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
d6aaac9e 1614 fontset = make_fontset (Qnil, name, Qnil);
5f2d79e0
KH
1615 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1616 if (len < 0)
1617 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
581f7ac7 1618 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
d6aaac9e 1619 }
1d5d7200
KH
1620 else
1621 {
8510724d 1622 fontset = FONTSET_FROM_ID (id);
1d5d7200
KH
1623 free_realized_fontsets (fontset);
1624 Fset_char_table_range (fontset, Qt, Qnil);
0d407d77 1625 }
4ed46869 1626
1d5d7200
KH
1627 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1628 {
1629 Lisp_Object elt, script;
1630
1631 elt = Fcar (fontlist);
1632 script = Fcar (elt);
cc36ddbf
KH
1633 elt = Fcdr (elt);
1634 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1635 for (; CONSP (elt); elt = XCDR (elt))
1636 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1637 else
1638 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1d5d7200 1639 }
06f76f0d
KH
1640 return name;
1641}
1642
1643
452a78e0 1644/* Alist of automatically created fontsets. Each element is a cons
581f7ac7 1645 (FONT-SPEC . FONTSET-ID). */
452a78e0 1646static Lisp_Object auto_fontset_alist;
d6aaac9e
KH
1647
1648int
581f7ac7 1649fontset_from_font (font_object)
8aa07a8d
KH
1650 Lisp_Object font_object;
1651{
03ef560c 1652 Lisp_Object font_name = font_get_name (font_object);
581f7ac7
KH
1653 Lisp_Object font_spec = Fcopy_font_spec (font_object);
1654 Lisp_Object fontset_spec, alias, name, fontset;
1655 Lisp_Object val;
1656 int i;
8aa07a8d 1657
581f7ac7
KH
1658 val = assoc_no_quit (font_spec, auto_fontset_alist);
1659 if (CONSP (val))
1660 return XINT (FONTSET_ID (XCDR (val)));
03ef560c 1661 if (NILP (auto_fontset_alist))
581f7ac7 1662 alias = intern ("fontset-startup");
03ef560c
KH
1663 else
1664 {
1665 char temp[32];
1666 int len = XINT (Flength (auto_fontset_alist));
8aa07a8d 1667
03ef560c 1668 sprintf (temp, "fontset-auto%d", len);
581f7ac7 1669 alias = intern (temp);
03ef560c 1670 }
581f7ac7
KH
1671 fontset_spec = Fcopy_font_spec (font_spec);
1672 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
7ae2e7f0 1673 name = Ffont_xlfd_name (fontset_spec, Qnil);
03ef560c 1674 if (NILP (name))
581f7ac7 1675 abort ();
03ef560c 1676 fontset = make_fontset (Qnil, name, Qnil);
581f7ac7
KH
1677 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1678 Vfontset_alias_alist);
1679 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1680 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1681 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
03ef560c 1682 FONTSET_ASCII (fontset) = font_name;
9b9cd864 1683 font_spec = Fcopy_font_spec (font_spec);
41ad6003 1684 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso10646_1);
581f7ac7
KH
1685 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
1686 ASET (font_spec, i, Qnil);
38aaf336 1687 Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qnil);
1c82b4b3 1688 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
edfda783
AR
1689
1690#ifdef HAVE_NS
1691 nsfont_make_fontset_for_font(name, font_object);
1692#endif
1693
03ef560c 1694 return XINT (FONTSET_ID (fontset));
8aa07a8d
KH
1695}
1696
fe44f009
KH
1697/* Return a cons (FONT-OBJECT . GLYPH-CODE).
1698 FONT-OBJECT is the font for the character at POSITION in the current
1ff005e1 1699 buffer. This is computed from all the text properties and overlays
cf14fd6e 1700 that apply to POSITION. POSTION may be nil, in which case,
fe44f009 1701 FONT-SPEC is the font for displaying the character CH with the
cf14fd6e
KH
1702 default face.
1703
92c15c34
KH
1704 GLYPH-CODE is the glyph code in the font to use for the character.
1705
1706 If the 2nd optional arg CH is non-nil, it is a character to check
1707 the font instead of the character at POSITION.
1708
1709 It returns nil in the following cases:
1ff005e1
KH
1710
1711 (1) The window system doesn't have a font for the character (thus
1712 it is displayed by an empty box).
1713
1714 (2) The character code is invalid.
1715
cf14fd6e
KH
1716 (3) If POSITION is not nil, and the current buffer is not displayed
1717 in any window.
1ff005e1
KH
1718
1719 In addition, the returned font name may not take into account of
1720 such redisplay engine hooks as what used in jit-lock-mode if
1721 POSITION is currently not visible. */
1722
1723
92c15c34 1724DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
335c5470 1725 doc: /* For internal use only. */)
92c15c34
KH
1726 (position, ch)
1727 Lisp_Object position, ch;
1ff005e1 1728{
0f8b27ea 1729 EMACS_INT pos, pos_byte, dummy;
1ff005e1
KH
1730 int face_id;
1731 int c;
1ff005e1
KH
1732 struct frame *f;
1733 struct face *face;
f0be8ea0 1734 int cs_id;
1ff005e1 1735
cf14fd6e 1736 if (NILP (position))
92c15c34
KH
1737 {
1738 CHECK_CHARACTER (ch);
1739 c = XINT (ch);
cf14fd6e 1740 f = XFRAME (selected_frame);
f2cec7a9 1741 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
327719ee 1742 pos = -1;
f0be8ea0 1743 cs_id = -1;
cf14fd6e
KH
1744 }
1745 else
1746 {
2f80c0a2 1747 Lisp_Object window, charset;
cf14fd6e
KH
1748 struct window *w;
1749
1750 CHECK_NUMBER_COERCE_MARKER (position);
1751 pos = XINT (position);
1752 if (pos < BEGV || pos >= ZV)
1753 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1754 pos_byte = CHAR_TO_BYTE (pos);
1755 if (NILP (ch))
1756 c = FETCH_CHAR (pos_byte);
1757 else
1758 {
1759 CHECK_NATNUM (ch);
1760 c = XINT (ch);
1761 }
1762 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1763 if (NILP (window))
1764 return Qnil;
1765 w = XWINDOW (window);
1766 f = XFRAME (w->frame);
1767 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
2f80c0a2
KH
1768 charset = Fget_char_property (position, Qcharset, Qnil);
1769 if (CHARSETP (charset))
f0be8ea0 1770 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
2f80c0a2 1771 else
f0be8ea0 1772 cs_id = -1;
92c15c34 1773 }
1ff005e1 1774 if (! CHAR_VALID_P (c, 0))
1ff005e1 1775 return Qnil;
327719ee 1776 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1ff005e1 1777 face = FACE_FROM_ID (f, face_id);
581f7ac7 1778 if (face->font)
8aa07a8d 1779 {
fe44f009
KH
1780 unsigned code = face->font->driver->encode_char (face->font, c);
1781 Lisp_Object font_object;
581f7ac7
KH
1782 /* Assignment to EMACS_INT stops GCC whining about limited range
1783 of data type. */
1784 EMACS_INT cod = code;
1785
1786 if (code == FONT_INVALID_CODE)
1787 return Qnil;
fe44f009 1788 XSETFONT (font_object, face->font);
581f7ac7 1789 if (cod <= MOST_POSITIVE_FIXNUM)
fe44f009
KH
1790 return Fcons (font_object, make_number (code));
1791 return Fcons (font_object, Fcons (make_number (code >> 16),
581f7ac7 1792 make_number (code & 0xFFFF)));
92c15c34
KH
1793 }
1794 return Qnil;
1ff005e1
KH
1795}
1796
1797
1d5d7200
KH
1798DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1799 doc: /* Return information about a fontset FONTSET on frame FRAME.
eb152aa9 1800The value is a char-table whose elements have this form:
e2b45cf9 1801
1d5d7200 1802 ((FONT-PATTERN OPENED-FONT ...) ...)
1ff005e1 1803
1d5d7200 1804FONT-PATTERN is a vector:
1ff005e1 1805
1d5d7200 1806 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
1ff005e1 1807
1d5d7200 1808or a string of font name pattern.
1ff005e1 1809
d6aaac9e 1810OPENED-FONT is a name of a font actually opened.
1ff005e1 1811
d6aaac9e
KH
1812The char-table has one extra slot. The value is a char-table
1813containing the information about the derived fonts from the default
eb152aa9 1814fontset. The format is the same as above. */)
1d5d7200
KH
1815 (fontset, frame)
1816 Lisp_Object fontset, frame;
4ed46869
KH
1817{
1818 FRAME_PTR f;
cc7b6145
KH
1819 Lisp_Object *realized[2], fontsets[2], tables[2];
1820 Lisp_Object val, elt;
1821 int c, i, j, k;
fc8865fc 1822
4ed46869
KH
1823 (*check_window_system_func) ();
1824
1d5d7200 1825 fontset = check_fontset_name (fontset);
0d407d77 1826
4ed46869 1827 if (NILP (frame))
18f39d0e 1828 frame = selected_frame;
b7826503 1829 CHECK_LIVE_FRAME (frame);
18f39d0e 1830 f = XFRAME (frame);
4ed46869 1831
1d5d7200
KH
1832 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1833 in the table `realized'. */
cc7b6145
KH
1834 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1835 * ASIZE (Vfontset_table));
1836 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
0d407d77 1837 {
1ff005e1
KH
1838 elt = FONTSET_FROM_ID (i);
1839 if (!NILP (elt)
1d5d7200
KH
1840 && EQ (FONTSET_BASE (elt), fontset)
1841 && EQ (FONTSET_FRAME (elt), frame))
cc7b6145 1842 realized[0][j++] = elt;
0d407d77 1843 }
cc7b6145 1844 realized[0][j] = Qnil;
4ed46869 1845
cc7b6145
KH
1846 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1847 * ASIZE (Vfontset_table));
1848 for (i = j = 0; ! NILP (realized[0][i]); i++)
1849 {
1850 elt = FONTSET_DEFAULT (realized[0][i]);
1851 if (! NILP (elt))
1852 realized[1][j++] = elt;
1853 }
1854 realized[1][j] = Qnil;
1855
1856 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1857 tables[1] = Fmake_char_table (Qnil, Qnil);
1858 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
1859 fontsets[0] = fontset;
1860 fontsets[1] = Vdefault_fontset;
e2b45cf9 1861
1d5d7200
KH
1862 /* Accumulate information of the fontset in TABLE. The format of
1863 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
cc7b6145 1864 for (k = 0; k <= 1; k++)
0d407d77 1865 {
cc7b6145 1866 for (c = 0; c <= MAX_CHAR; )
d6aaac9e 1867 {
5b30be1a 1868 int from = c, to = MAX_5_BYTE_CHAR;
0d407d77 1869
cc7b6145
KH
1870 if (c <= MAX_5_BYTE_CHAR)
1871 {
1872 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
cc7b6145
KH
1873 }
1874 else
1875 {
1876 val = FONTSET_FALLBACK (fontsets[k]);
1877 to = MAX_CHAR;
1878 }
d6aaac9e 1879 if (VECTORP (val))
0d407d77 1880 {
d6aaac9e
KH
1881 Lisp_Object alist;
1882
1883 /* At first, set ALIST to ((FONT-SPEC) ...). */
1884 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
581f7ac7
KH
1885 if (! NILP (AREF (val, i)))
1886 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1887 alist);
d6aaac9e
KH
1888 alist = Fnreverse (alist);
1889
660100f3 1890 /* Then store opened font names to cdr of each elements. */
cc7b6145 1891 for (i = 0; ! NILP (realized[k][i]); i++)
1ff005e1 1892 {
cc7b6145
KH
1893 if (c <= MAX_5_BYTE_CHAR)
1894 val = FONTSET_REF (realized[k][i], c);
1895 else
1896 val = FONTSET_FALLBACK (realized[k][i]);
1897 if (! VECTORP (val))
d6aaac9e 1898 continue;
581f7ac7
KH
1899 /* VAL: [int ? [FACE-ID FONT-DEF FONT-OBJECT int] ... ] */
1900 for (j = 2; j < ASIZE (val); j++)
3d4448a8 1901 {
581f7ac7
KH
1902 elt = AREF (val, j);
1903 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1904 {
1905 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1906 Lisp_Object slot, name;
1907
1908 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1909 name = AREF (font_object, FONT_NAME_INDEX);
1910 if (NILP (Fmember (name, XCDR (slot))))
1911 nconc2 (slot, Fcons (name, Qnil));
1912 }
3d4448a8 1913 }
1ff005e1 1914 }
d6aaac9e
KH
1915
1916 /* Store ALIST in TBL for characters C..TO. */
cc7b6145
KH
1917 if (c <= MAX_5_BYTE_CHAR)
1918 char_table_set_range (tables[k], c, to, alist);
1919 else
1920 XCHAR_TABLE (tables[k])->defalt = alist;
581f7ac7
KH
1921
1922 /* At last, change each elements to font names. */
1923 for (; CONSP (alist); alist = XCDR (alist))
1924 {
1925 elt = XCAR (alist);
7ae2e7f0 1926 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
581f7ac7 1927 }
0d407d77 1928 }
d6aaac9e 1929 c = to + 1;
0d407d77
KH
1930 }
1931 }
a921395d 1932
cc7b6145 1933 return tables[0];
4ed46869
KH
1934}
1935
1d5d7200 1936
0fdf26e6 1937DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
335c5470 1938 doc: /* Return a font name pattern for character CH in fontset NAME.
0fdf26e6
KH
1939If NAME is t, find a pattern in the default fontset.
1940
1941The value has the form (FAMILY . REGISTRY), where FAMILY is a font
1942family name and REGISTRY is a font registry name. This is actually
1943the first font name pattern for CH in the fontset or in the default
1944fontset.
1945
1946If the 2nd optional arg ALL is non-nil, return a list of all font name
1947patterns. */)
1948 (name, ch, all)
1949 Lisp_Object name, ch, all;
0d407d77 1950{
1337ac77 1951 int c;
0fdf26e6
KH
1952 Lisp_Object fontset, elt, list, repertory, val;
1953 int i, j;
0d407d77
KH
1954
1955 fontset = check_fontset_name (name);
1956
06f76f0d 1957 CHECK_CHARACTER (ch);
0d407d77 1958 c = XINT (ch);
0fdf26e6
KH
1959 list = Qnil;
1960 while (1)
1961 {
1962 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
1963 i++, elt = FONTSET_FALLBACK (fontset))
1964 if (VECTORP (elt))
1965 for (j = 0; j < ASIZE (elt); j++)
1966 {
a124bbb6
KH
1967 Lisp_Object family, registry;
1968
0fdf26e6
KH
1969 val = AREF (elt, j);
1970 repertory = AREF (val, 1);
1971 if (INTEGERP (repertory))
1972 {
1973 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
1974
1975 if (! CHAR_CHARSET_P (c, charset))
1976 continue;
1977 }
1978 else if (CHAR_TABLE_P (repertory))
1979 {
1980 if (NILP (CHAR_TABLE_REF (repertory, c)))
1981 continue;
1982 }
1983 val = AREF (val, 0);
a124bbb6
KH
1984 /* VAL is a FONT-SPEC */
1985 family = AREF (val, FONT_FAMILY_INDEX);
1986 if (! NILP (family))
1987 family = SYMBOL_NAME (family);
1988 registry = AREF (val, FONT_REGISTRY_INDEX);
1989 if (! NILP (registry))
1990 registry = SYMBOL_NAME (registry);
1991 val = Fcons (family, registry);
0fdf26e6
KH
1992 if (NILP (all))
1993 return val;
1994 list = Fcons (val, list);
1995 }
1996 if (EQ (fontset, Vdefault_fontset))
1997 break;
1998 fontset = Vdefault_fontset;
1999 }
2000 return (Fnreverse (list));
0d407d77 2001}
0d407d77
KH
2002
2003DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
335c5470
PJ
2004 doc: /* Return a list of all defined fontset names. */)
2005 ()
0d407d77
KH
2006{
2007 Lisp_Object fontset, list;
2008 int i;
2009
2010 list = Qnil;
2011 for (i = 0; i < ASIZE (Vfontset_table); i++)
2012 {
2013 fontset = FONTSET_FROM_ID (i);
2014 if (!NILP (fontset)
2015 && BASE_FONTSET_P (fontset))
2016 list = Fcons (FONTSET_NAME (fontset), list);
2017 }
1ff005e1 2018
0d407d77
KH
2019 return list;
2020}
2021
452a78e0
KH
2022
2023#ifdef FONTSET_DEBUG
2024
2025Lisp_Object
2026dump_fontset (fontset)
2027 Lisp_Object fontset;
2028{
2029 Lisp_Object vec;
2030
2031 vec = Fmake_vector (make_number (3), Qnil);
2032 ASET (vec, 0, FONTSET_ID (fontset));
2033
2034 if (BASE_FONTSET_P (fontset))
2035 {
2036 ASET (vec, 1, FONTSET_NAME (fontset));
2037 }
2038 else
2039 {
2040 Lisp_Object frame;
2041
2042 frame = FONTSET_FRAME (fontset);
2043 if (FRAMEP (frame))
2044 {
2045 FRAME_PTR f = XFRAME (frame);
2046
2047 if (FRAME_LIVE_P (f))
1c82b4b3
KH
2048 ASET (vec, 1,
2049 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
452a78e0 2050 else
1c82b4b3
KH
2051 ASET (vec, 1,
2052 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
452a78e0 2053 }
eb36588a
KH
2054 if (!NILP (FONTSET_DEFAULT (fontset)))
2055 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
452a78e0
KH
2056 }
2057 return vec;
2058}
2059
2060DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2061 doc: /* Return a brief summary of all fontsets for debug use. */)
2062 ()
2063{
2064 Lisp_Object val;
2065 int i;
2066
2067 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2068 if (! NILP (AREF (Vfontset_table, i)))
2069 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2070 return (Fnreverse (val));
2071}
2072#endif /* FONTSET_DEBUG */
2073
dfcf069d 2074void
4ed46869
KH
2075syms_of_fontset ()
2076{
1d5d7200 2077 DEFSYM (Qfontset, "fontset");
eb36588a 2078 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
d6aaac9e
KH
2079 DEFSYM (Qfontset_info, "fontset-info");
2080 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
4ed46869 2081
1d5d7200
KH
2082 DEFSYM (Qprepend, "prepend");
2083 DEFSYM (Qappend, "append");
c3fb88cc 2084 DEFSYM (Qlatin, "latin");
4ed46869
KH
2085
2086 Vcached_fontset_data = Qnil;
2087 staticpro (&Vcached_fontset_data);
2088
0d407d77
KH
2089 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2090 staticpro (&Vfontset_table);
0d407d77
KH
2091
2092 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2093 staticpro (&Vdefault_fontset);
1ff005e1
KH
2094 FONTSET_ID (Vdefault_fontset) = make_number (0);
2095 FONTSET_NAME (Vdefault_fontset)
2096 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
ff7a2223 2097 ASET (Vfontset_table, 0, Vdefault_fontset);
1ff005e1 2098 next_fontset_id = 1;
4ed46869 2099
452a78e0
KH
2100 auto_fontset_alist = Qnil;
2101 staticpro (&auto_fontset_alist);
2102
28e2436a
KH
2103 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2104 doc: /*
2105Alist of charsets vs the charsets to determine the preferred font encoding.
eb152aa9 2106Each element looks like (CHARSET . ENCODING-CHARSET),
28e2436a
KH
2107where ENCODING-CHARSET is a charset registered in the variable
2108`font-encoding-alist' as ENCODING.
2109
2110When a text has a property `charset' and the value is CHARSET, a font
2111whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2112 Vfont_encoding_charset_alist = Qnil;
2113
6a7e6d80 2114 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
1d5d7200
KH
2115 doc: /*
2116Char table of characters whose ascent values should be ignored.
335c5470 2117If an entry for a character is non-nil, the ascent value of the glyph
eb152aa9 2118is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
335c5470
PJ
2119
2120This affects how a composite character which contains
2121such a character is displayed on screen. */);
2aeafb78
KH
2122 Vuse_default_ascent = Qnil;
2123
2124 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
1d5d7200 2125 doc: /*
eb152aa9 2126Char table of characters which are not composed relatively.
335c5470
PJ
2127If an entry for a character is non-nil, a composition sequence
2128which contains that character is displayed so that
2129the glyph of that character is put without considering
2130an ascent and descent value of a previous character. */);
810abb87 2131 Vignore_relative_composition = Qnil;
6a7e6d80 2132
01d4b817 2133 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
335c5470
PJ
2134 doc: /* Alist of fontname vs list of the alternate fontnames.
2135When a specified font name is not found, the corresponding
2136alternate fontnames (if any) are tried instead. */);
01d4b817 2137 Valternate_fontname_alist = Qnil;
8c83e4f9 2138
1c283e35 2139 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
335c5470 2140 doc: /* Alist of fontset names vs the aliases. */);
1ff005e1
KH
2141 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2142 build_string ("fontset-default")),
2143 Qnil);
1c283e35 2144
810abb87
KH
2145 DEFVAR_LISP ("vertical-centering-font-regexp",
2146 &Vvertical_centering_font_regexp,
335c5470
PJ
2147 doc: /* *Regexp matching font names that require vertical centering on display.
2148When a character is displayed with such fonts, the character is displayed
fc8865fc 2149at the vertical center of lines. */);
810abb87
KH
2150 Vvertical_centering_font_regexp = Qnil;
2151
21ff5ed6
KH
2152 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2153 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2154 Votf_script_alist = Qnil;
2155
4ed46869
KH
2156 defsubr (&Squery_fontset);
2157 defsubr (&Snew_fontset);
2158 defsubr (&Sset_fontset_font);
1ff005e1 2159 defsubr (&Sinternal_char_font);
4ed46869 2160 defsubr (&Sfontset_info);
0d407d77
KH
2161 defsubr (&Sfontset_font);
2162 defsubr (&Sfontset_list);
452a78e0
KH
2163#ifdef FONTSET_DEBUG
2164 defsubr (&Sfontset_list_all);
2165#endif
e3400864 2166}
92c15c34
KH
2167
2168/* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2169 (do not change this comment) */