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