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