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