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