(selection-coding-system): Make it a defcustom, and add the properties
[bpt/emacs.git] / src / fontset.c
... / ...
CommitLineData
1/* Fontset handler.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
8 Copyright (C) 2003, 2006
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
11
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
16the Free Software Foundation; either version 3, or (at your option)
17any later version.
18
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.
23
24You should have received a copy of the GNU General Public License
25along with GNU Emacs; see the file COPYING. If not, write to
26the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27Boston, MA 02110-1301, USA. */
28
29/* #define FONTSET_DEBUG */
30
31#include <config.h>
32
33#ifdef FONTSET_DEBUG
34#include <stdio.h>
35#endif
36
37#include "lisp.h"
38#include "blockinput.h"
39#include "buffer.h"
40#include "character.h"
41#include "charset.h"
42#include "ccl.h"
43#include "keyboard.h"
44#include "frame.h"
45#include "dispextern.h"
46#include "intervals.h"
47#include "fontset.h"
48#include "window.h"
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
58#include "termhooks.h"
59
60#include "font.h"
61
62#undef xassert
63#ifdef FONTSET_DEBUG
64#define xassert(X) do {if (!(X)) abort ();} while (0)
65#undef INLINE
66#define INLINE
67#else /* not FONTSET_DEBUG */
68#define xassert(X) (void) 0
69#endif /* not FONTSET_DEBUG */
70
71EXFUN (Fclear_face_cache, 1);
72
73/* FONTSET
74
75 A fontset is a collection of font related information to give
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.
90
91 A fontset object is implemented by a char-table whose default value
92 and parent are always nil.
93
94 An element of a base fontset is a vector of FONT-DEFs which itself
95 is a vector [ FONT-SPEC ENCODING REPERTORY ].
96
97 FONT-SPEC is a font-spec created by `font-spec' or
98 ( FAMILY . REGISTRY )
99 or
100 FONT-NAME
101 where FAMILY, REGISTRY, and FONT-NAME are strings.
102
103 ENCODING is a charset ID that can convert characters to glyph codes
104 of the corresponding font.
105
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.
111
112 ENCODING and REPERTORY are extracted from the variable
113 Vfont_encoding_alist by using a font name generated from FONT-SPEC
114 (if it is a vector) or FONT-NAME as a matching target.
115
116
117 An element of a realized fontset is nil or t, or has this form:
118
119 [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-FAMILY
120 RFONT-DEF0 RFONT-DEF1 ...].
121
122 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
123
124 [ FACE-ID FONT-INDEX FONT-DEF OPENED-FONT-NAME ]
125
126 RFONT-DEFn is automatically reordered by the current charset
127 priority list.
128
129 The value nil means that we have not yet generated the above vector
130 from the base of the fontset.
131
132 The value t means that no font is available for the corresponding
133 range of characters.
134
135
136 A fontset has 9 extra slots.
137
138 The 1st slot: the ID number of the fontset
139
140 The 2nd slot:
141 base: the name of the fontset
142 realized: nil
143
144 The 3rd slot:
145 base: nil
146 realized: the base fontset
147
148 The 4th slot:
149 base: nil
150 realized: the frame that the fontset belongs to
151
152 The 5th slot:
153 base: the font name for ASCII characters
154 realized: nil
155
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.
160
161 The 7th slot:
162 base: nil
163 realized: Alist of font index vs the corresponding repertory
164 char-table.
165
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.
170
171 The 9th slot:
172 base: Same as element value (but for fallback fonts).
173 realized: Likewise.
174
175 All fontsets are recorded in the vector Vfontset_table.
176
177
178 DEFAULT FONTSET
179
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.
184
185 The parent of a realized fontset created for such a face that has
186 no fontset is the default fontset.
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
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'
193 for an actual fontset object, i.e., char-table.
194
195*/
196
197/********** VARIABLES and FUNCTION PROTOTYPES **********/
198
199extern Lisp_Object Qfont;
200static Lisp_Object Qfontset;
201static Lisp_Object Qfontset_info;
202static Lisp_Object Qprepend, Qappend;
203static Lisp_Object Qlatin;
204
205/* Vector containing all fontsets. */
206static Lisp_Object Vfontset_table;
207
208/* Next possibly free fontset ID. Usually this keeps the minimum
209 fontset ID not yet used. */
210static int next_fontset_id;
211
212/* The default fontset. This gives default FAMILY and REGISTRY of
213 font for each character. */
214static Lisp_Object Vdefault_fontset;
215
216Lisp_Object Vfont_encoding_alist;
217Lisp_Object Vfont_encoding_charset_alist;
218Lisp_Object Vuse_default_ascent;
219Lisp_Object Vignore_relative_composition;
220Lisp_Object Valternate_fontname_alist;
221Lisp_Object Vfontset_alias_alist;
222Lisp_Object Vvertical_centering_font_regexp;
223Lisp_Object Votf_script_alist;
224
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. */
228
229/* Return a pointer to struct font_info of font FONT_IDX of frame F. */
230struct font_info *(*get_font_info_func) P_ ((FRAME_PTR f, int font_idx));
231
232/* Return a list of font names which matches PATTERN. See the documentation
233 of `x-list-fonts' for more details. */
234Lisp_Object (*list_fonts_func) P_ ((struct frame *f,
235 Lisp_Object pattern,
236 int size,
237 int maxnames));
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. */
241struct font_info *(*load_font_func) P_ ((FRAME_PTR f, char *name, int));
242
243/* Return a pointer to struct font_info of a font named NAME for frame F. */
244struct font_info *(*query_font_func) P_ ((FRAME_PTR f, char *name));
245
246/* Additional function for setting fontset or changing fontset
247 contents of frame F. */
248void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
249 Lisp_Object oldval));
250
251/* To find a CCL program, fs_load_font calls this function.
252 The argument is a pointer to the struct font_info.
253 This function set the member `encoder' of the structure. */
254void (*find_ccl_program_func) P_ ((struct font_info *));
255
256Lisp_Object (*get_font_repertory_func) P_ ((struct frame *,
257 struct font_info *));
258
259/* Check if any window system is used now. */
260void (*check_window_system_func) P_ ((void));
261
262
263/* Prototype declarations for static functions. */
264static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
265 Lisp_Object));
266static void reorder_font_vector P_ ((Lisp_Object, int, Lisp_Object));
267static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
268static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
269static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
270static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
271 Lisp_Object));
272Lisp_Object find_font_encoding P_ ((Lisp_Object));
273
274static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
275
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
289
290\f
291/********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
292
293/* Return the fontset with ID. No check of ID's validness. */
294#define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
295
296/* Macros to access special values of FONTSET. */
297#define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
298
299/* Macros to access special values of (base) FONTSET. */
300#define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
301#define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
302
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]
306#define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
307#define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
308#define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
309
310/* For both base and realized fontset. */
311#define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
312
313#define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
314
315
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. */
320
321#define FONTSET_REF(fontset, c) \
322 (EQ (fontset, Vdefault_fontset) \
323 ? CHAR_TABLE_REF (fontset, c) \
324 : fontset_ref ((fontset), (c)))
325
326static Lisp_Object
327fontset_ref (fontset, c)
328 Lisp_Object fontset;
329 int c;
330{
331 Lisp_Object elt;
332
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);
338 return elt;
339}
340
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
348
349/* Modify the elements of FONTSET for characters in RANGE by replacing
350 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
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
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)))) \
361 : fontset_add ((fontset), (range), (elt), (add)))
362
363static Lisp_Object
364fontset_add (fontset, range, elt, add)
365 Lisp_Object fontset, range, elt, add;
366{
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 }
394 return Qnil;
395}
396
397
398/* Update FONT-GROUP which has this form:
399 [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-FAMILY
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
403 the latest value. */
404
405static void
406reorder_font_vector (font_group, charset_id, family)
407 Lisp_Object font_group;
408 int charset_id;
409 Lisp_Object family;
410{
411 Lisp_Object list, *new_vec;
412 int size;
413 int *charset_id_table;
414 int i, idx;
415 Lisp_Object preferred_by_charset, preferred_by_family;
416
417 size = ASIZE (font_group) - 3;
418 /* Exclude the tailing nil elements from the reordering. */
419 while (NILP (AREF (font_group, size - 1))) size--;
420 charset_id_table = (int *) alloca (sizeof (int) * size);
421 new_vec = (Lisp_Object *) alloca (sizeof (Lisp_Object) * size);
422
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++)
429 {
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;
454 }
455
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
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))
468 {
469 int id = XINT (XCAR (list));
470 struct charset *charset = CHARSET_FROM_ID (id);
471
472 if (charset->supplementary_p)
473 break;
474 for (i = 0; i < size; i++)
475 if (charset_id_table[i] == XINT (XCAR (list))
476 && ! NILP (AREF (font_group, i + 3)))
477 {
478 new_vec[idx++] = AREF (font_group, i + 3);
479 ASET (font_group, i + 3, Qnil);
480 }
481 }
482 for (i = 0; i < size; i++)
483 if (! NILP (AREF (font_group, i + 3)))
484 new_vec[idx++] = AREF (font_group, i + 3);
485
486 /* At last, update elements of FONT-GROUP. */
487 for (i = 0; i < size; i++)
488 ASET (font_group, i + 3, new_vec[i]);
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
499extern Lisp_Object QCname;
500
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;
510 int charset;
511 Lisp_Object font_spec, name;
512
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);
519 charset = XINT (AREF (font_def, 1));
520 if (! (font_info = fs_load_font (f, font_name, charset)))
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),
534 FONTSET_REPERTORY (fontset));
535 }
536
537 return font_info->font_idx;
538}
539
540static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
541 int, int));
542
543/* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
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
551 ID is a charset-id that must be preferred, or -1 meaning no
552 preference.
553
554 If FALLBACK is nonzero, search only fallback fonts. */
555
556static Lisp_Object
557fontset_find_font (fontset, c, face, id, fallback)
558 Lisp_Object fontset;
559 int c;
560 struct face *face;
561 int id, fallback;
562{
563 Lisp_Object base_fontset, elt, vec, font_def;
564 int i, from, to;
565 int font_idx;
566 FRAME_PTR f = XFRAME (FONTSET_FRAME (fontset));
567
568 base_fontset = FONTSET_BASE (fontset);
569 if (! fallback)
570 vec = CHAR_TABLE_REF (fontset, c);
571 else
572 vec = FONTSET_FALLBACK (fontset);
573
574 if (NILP (vec))
575 {
576 Lisp_Object range;
577
578 /* We have not yet decided a font for C. */
579 if (! face)
580 return Qnil;
581 if (! fallback)
582 {
583 elt = char_table_ref_and_range (base_fontset, c, &from, &to);
584 range = Fcons (make_number (from), make_number (to));
585 }
586 else
587 {
588 elt = FONTSET_FALLBACK (base_fontset);
589 }
590 if (NILP (elt))
591 {
592 /* This fontset doesn't specify any font for C. */
593 vec = make_number (0);
594 }
595 else if (ASIZE (elt) == 1 && NILP (AREF (elt, 0)))
596 {
597 /* Explicitly specified no font. */
598 vec = Qt;
599 }
600 else
601 {
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]. */
605 int size = ASIZE (elt);
606 int j;
607
608 vec = Fmake_vector (make_number (size + 3), Qnil);
609 ASET (vec, 0, make_number (-1));
610 ASET (vec, 1, make_number (-1));
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 }
620 }
621 /* Then store it in the fontset. */
622 if (! fallback)
623 FONTSET_SET (fontset, range, vec);
624 else
625 FONTSET_FALLBACK (fontset) = vec;
626
627 }
628 if (! VECTORP (vec))
629 return (EQ (vec, Qt) ? Qt : Qnil);
630
631 if (ASIZE (vec) > 4
632 && (XINT (AREF (vec, 0)) != charset_ordered_list_tick
633 || (id >= 0 && XINT (AREF (vec, 1)) != id)
634 || NILP (Fequal (AREF (vec, 2), face->lface[LFACE_FAMILY_INDEX]))))
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]);
640
641 /* Find the first available font in the vector of RFONT-DEF. */
642 for (i = 3; i < ASIZE (vec); i++)
643 {
644 elt = AREF (vec, i);
645 if (NILP (elt))
646 /* This is the sign of not to try fallback fonts. */
647 return Qt;
648 /* ELT == [ FACE-ID FONT-INDEX FONT-DEF ... ] */
649 if (INTEGERP (AREF (elt, 1)) && XINT (AREF (elt, 1)) < 0)
650 /* We couldn't open this font last time. */
651 continue;
652
653 if (!face && NILP (AREF (elt, 1)))
654 /* We have not yet opened the font. */
655 return Qnil;
656
657 font_def = AREF (elt, 2);
658 /* FONT_DEF == [ FONT-SPEC ENCODING REPERTORY ] */
659
660#ifdef USE_FONT_BACKEND
661 if (enable_font_backend)
662 {
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))
669 {
670 font_entity = font_find_for_lface (f, face->lface,
671 AREF (font_def, 0), -1);
672 if (NILP (font_entity))
673 {
674 ASET (elt, 1, make_number (-1));
675 continue;
676 }
677 ASET (elt, 3, font_entity);
678 }
679 else if (FONT_ENTITY_P (font_entity))
680 {
681 if (FONT_ENTITY_NOT_LOADABLE (font_entity))
682 continue;
683 }
684 has_char = font_has_char (f, font_entity, c);
685 if (! has_char)
686 continue;
687 if (! FONT_OBJECT_P (font_entity))
688 {
689 Lisp_Object font_object
690 = font_open_for_lface (f, font_entity, face->lface, Qnil);
691
692 if (NILP (font_object))
693 {
694 FONT_ENTITY_SET_NOT_LOADABLE (font_entity);
695 continue;
696 }
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 }
704 }
705 /* Decide to use this font. */
706 ASET (elt, 1, make_number (0));
707 }
708 else
709#endif /* USE_FONT_BACKEND */
710
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))
718 /* This font can't display C. */
719 continue;
720 }
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 }
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. */
737 struct font_info *font_info;
738
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;
745 font_info = (*get_font_info_func) (f, font_idx);
746 ASET (elt, 3, build_string (font_info->full_name));
747 }
748
749 slot = Fassq (AREF (elt, 1), FONTSET_REPERTORY (fontset));
750 xassert (CONSP (slot));
751 if (NILP (CHAR_TABLE_REF (XCDR (slot), c)))
752 /* This font can't display C. */
753 continue;
754 }
755
756 /* Now we have decided to use this font spec to display C. */
757 if (! INTEGERP (AREF (elt, 1)))
758 {
759 /* But not yet opened the best matching font. */
760 struct font_info *font_info;
761
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)
765 /* Can't open it. Try the other one. */
766 continue;
767 font_info = (*get_font_info_func) (f, font_idx);
768 ASET (elt, 3, build_string (font_info->full_name));
769 }
770 return elt;
771 }
772
773 return Qnil;
774}
775
776
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;
786
787 /* Try a font-group for C. */
788 rfont_def = fontset_find_font (fontset, c, face, id, 0);
789 if (VECTORP (rfont_def))
790 return rfont_def;
791 if (EQ (rfont_def, Qt))
792 return Qnil;
793 base_fontset = FONTSET_BASE (fontset);
794 /* Try a font-group for C of the default fontset. */
795 if (! EQ (base_fontset, Vdefault_fontset))
796 {
797 if (NILP (FONTSET_DEFAULT (fontset)))
798 FONTSET_DEFAULT (fontset)
799 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
800 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
801 if (VECTORP (rfont_def))
802 return (rfont_def);
803 if (! NILP (rfont_def))
804 /* Remeber that we have no font for C. */
805 FONTSET_SET (fontset, make_number (c), Qt);
806 }
807
808 /* Try a fallback font-group. */
809 rfont_def = fontset_find_font (fontset, c, face, id, 1);
810 if (! VECTORP (rfont_def)
811 && ! EQ (base_fontset, Vdefault_fontset))
812 /* Try a fallback font-group of the default fontset . */
813 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
814
815 if (! VECTORP (rfont_def))
816 /* Remeber that we have no font for C. */
817 FONTSET_SET (fontset, make_number (c), Qt);
818
819 return rfont_def;
820}
821
822/* Return a newly created fontset with NAME. If BASE is nil, make a
823 base fontset. Otherwise make a realized fontset whose base is
824 BASE. */
825
826static Lisp_Object
827make_fontset (frame, name, base)
828 Lisp_Object frame, name, base;
829{
830 Lisp_Object fontset;
831 int size = ASIZE (Vfontset_table);
832 int id = next_fontset_id;
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
837 Vfontset_table is always nil, we don't have to check the range of
838 id. */
839 while (!NILP (AREF (Vfontset_table, id))) id++;
840
841 if (id + 1 == size)
842 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
843
844 fontset = Fmake_char_table (Qfontset, Qnil);
845
846 FONTSET_ID (fontset) = make_number (id);
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 }
857
858 ASET (Vfontset_table, id, fontset);
859 next_fontset_id = id + 1;
860 return fontset;
861}
862
863
864/* Set the ASCII font of the default fontset to FONTNAME if that is
865 not yet set. */
866void
867set_default_ascii_font (fontname)
868 Lisp_Object fontname;
869{
870 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
871 {
872 int id = fs_query_fontset (fontname, 2);
873
874 if (id >= 0)
875 fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
876 FONTSET_ASCII (Vdefault_fontset)= fontname;
877 }
878}
879
880\f
881/********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
882
883/* Return the name of the fontset who has ID. */
884
885Lisp_Object
886fontset_name (id)
887 int id;
888{
889 Lisp_Object fontset;
890
891 fontset = FONTSET_FROM_ID (id);
892 return FONTSET_NAME (fontset);
893}
894
895
896/* Return the ASCII font name of the fontset who has ID. */
897
898Lisp_Object
899fontset_ascii (id)
900 int id;
901{
902 Lisp_Object fontset, elt;
903
904 fontset= FONTSET_FROM_ID (id);
905 elt = FONTSET_ASCII (fontset);
906#ifdef USE_FONT_BACKEND
907 if (CONSP (elt))
908 elt = XCAR (elt);
909#endif /* USE_FONT_BACKEND */
910 /* It is assured that ELT is always a string (i.e. fontname
911 pattern). */
912 return elt;
913}
914
915
916/* Free fontset of FACE defined on frame F. Called from
917 free_realized_face. */
918
919void
920free_face_fontset (f, face)
921 FRAME_PTR f;
922 struct face *face;
923{
924 Lisp_Object fontset;
925
926 fontset = FONTSET_FROM_ID (face->fontset);
927 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
928 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
929 ASET (Vfontset_table, face->fontset, Qnil);
930 if (face->fontset < next_fontset_id)
931 next_fontset_id = face->fontset;
932 if (! NILP (FONTSET_DEFAULT (fontset)))
933 {
934 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
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 }
943}
944
945
946/* Return 1 if FACE is suitable for displaying character C.
947 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
948 when C is not an ASCII character. */
949
950int
951face_suitable_for_char_p (face, c)
952 struct face *face;
953 int c;
954{
955 Lisp_Object fontset, rfont_def;
956
957 fontset = FONTSET_FROM_ID (face->fontset);
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)));
962}
963
964
965/* Return ID of face suitable for displaying character C on frame F.
966 FACE must be reazlied for ASCII characters in advance. Called from
967 the macro FACE_FOR_CHAR. */
968
969int
970face_for_char (f, face, c, pos, object)
971 FRAME_PTR f;
972 struct face *face;
973 int c, pos;
974 Lisp_Object object;
975{
976 Lisp_Object fontset, charset, rfont_def;
977 int face_id;
978 int id;
979
980 if (ASCII_CHAR_P (c))
981 return face->ascii_face->id;
982
983 xassert (fontset_id_valid_p (face->fontset));
984 fontset = FONTSET_FROM_ID (face->fontset);
985 xassert (!BASE_FONTSET_P (fontset));
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))
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 }
1002 }
1003 rfont_def = fontset_font (fontset, c, face, id);
1004 if (VECTORP (rfont_def))
1005 {
1006#ifdef USE_FONT_BACKEND
1007 if (enable_font_backend
1008 && NILP (AREF (rfont_def, 0)))
1009 {
1010 struct font *font = XSAVE_VALUE (AREF (rfont_def, 3))->pointer;
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 */
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));
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
1043make_fontset_for_ascii_face (f, base_fontset_id, face)
1044 FRAME_PTR f;
1045 int base_fontset_id;
1046 struct face *face;
1047{
1048 Lisp_Object base_fontset, fontset, frame;
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);
1056 if (! BASE_FONTSET_P (base_fontset))
1057 abort ();
1058 }
1059 else
1060 base_fontset = Vdefault_fontset;
1061
1062 fontset = make_fontset (frame, Qnil, base_fontset);
1063 return XINT (FONTSET_ID (fontset));
1064}
1065
1066
1067#if defined(WINDOWSNT) && defined (_MSC_VER)
1068#pragma optimize("", off)
1069#endif
1070
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
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. */
1075
1076struct font_info *
1077fs_load_font (f, fontname, charset)
1078 FRAME_PTR f;
1079 char *fontname;
1080 int charset;
1081{
1082 struct font_info *fontp;
1083 Lisp_Object fullname;
1084
1085 if (!fontname)
1086 /* No way to get fontname. */
1087 return NULL;
1088
1089 fontp = (*load_font_func) (f, fontname, 0);
1090 if (! fontp || fontp->charset >= 0)
1091 return fontp;
1092
1093 fontname = fontp->full_name;
1094 fullname = build_string (fontp->full_name);
1095
1096 if (charset < 0)
1097 {
1098 Lisp_Object charset_symbol;
1099
1100 charset_symbol = find_font_encoding (fullname);
1101 if (CONSP (charset_symbol))
1102 charset_symbol = XCAR (charset_symbol);
1103 if (NILP (charset_symbol))
1104 charset_symbol = Qascii;
1105 charset = XINT (CHARSET_SYMBOL_ID (charset_symbol));
1106 }
1107 fontp->charset = charset;
1108 fontp->vertical_centering = 0;
1109 fontp->font_encoder = NULL;
1110
1111 if (charset != charset_ascii)
1112 {
1113 fontp->vertical_centering
1114 = (STRINGP (Vvertical_centering_font_regexp)
1115 && (fast_string_match_ignore_case
1116 (Vvertical_centering_font_regexp, fullname) >= 0));
1117
1118 if (find_ccl_program_func)
1119 (*find_ccl_program_func) (fontp);
1120 }
1121
1122 return fontp;
1123}
1124
1125#if defined(WINDOWSNT) && defined (_MSC_VER)
1126#pragma optimize("", on)
1127#endif
1128
1129\f
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. */
1133
1134
1135Lisp_Object
1136find_font_encoding (fontname)
1137 Lisp_Object fontname;
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))
1146 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
1147 && (SYMBOLP (XCDR (elt))
1148 ? CHARSETP (XCDR (elt))
1149 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
1150 return (XCDR (elt));
1151 }
1152 /* We don't know the encoding of this font. Let's assume `ascii'. */
1153 return Qascii;
1154}
1155
1156
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
1162#define CACHED_FONTSET_NAME (SDATA (XCAR (Vcached_fontset_data)))
1163#define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1164
1165/* If fontset name PATTERN contains any wild card, return regular
1166 expression corresponding to PATTERN. */
1167
1168static Lisp_Object
1169fontset_pattern_regexp (pattern)
1170 Lisp_Object pattern;
1171{
1172 if (!index (SDATA (pattern), '*')
1173 && !index (SDATA (pattern), '?'))
1174 /* PATTERN does not contain any wild cards. */
1175 return Qnil;
1176
1177 if (!CONSP (Vcached_fontset_data)
1178 || strcmp (SDATA (pattern), CACHED_FONTSET_NAME))
1179 {
1180 /* We must at first update the cached data. */
1181 unsigned char *regex, *p0, *p1;
1182 int ndashes = 0, nstars = 0;
1183
1184 for (p0 = SDATA (pattern); *p0; p0++)
1185 {
1186 if (*p0 == '-')
1187 ndashes++;
1188 else if (*p0 == '*')
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)
1196 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 1);
1197 else
1198 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 1);
1199
1200 *p1++ = '^';
1201 for (p0 = SDATA (pattern); *p0; p0++)
1202 {
1203 if (*p0 == '*')
1204 {
1205 if (ndashes < 14)
1206 *p1++ = '.';
1207 else
1208 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1209 *p1++ = '*';
1210 }
1211 else if (*p0 == '?')
1212 *p1++ = '.';
1213 else
1214 *p1++ = *p0;
1215 }
1216 *p1++ = '$';
1217 *p1++ = 0;
1218
1219 Vcached_fontset_data = Fcons (build_string (SDATA (pattern)),
1220 build_string (regex));
1221 }
1222
1223 return CACHED_FONTSET_REGEX;
1224}
1225
1226/* Return ID of the base fontset named NAME. If there's no such
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*/
1232
1233int
1234fs_query_fontset (name, name_pattern)
1235 Lisp_Object name;
1236 int name_pattern;
1237{
1238 Lisp_Object tem;
1239 int i;
1240
1241 name = Fdowncase (name);
1242 if (name_pattern != 1)
1243 {
1244 tem = Frassoc (name, Vfontset_alias_alist);
1245 if (NILP (tem))
1246 tem = Fassoc (name, Vfontset_alias_alist);
1247 if (CONSP (tem) && STRINGP (XCAR (tem)))
1248 name = XCAR (tem);
1249 else if (name_pattern == 0)
1250 {
1251 tem = fontset_pattern_regexp (name);
1252 if (STRINGP (tem))
1253 {
1254 name = tem;
1255 name_pattern = 1;
1256 }
1257 }
1258 }
1259
1260 for (i = 0; i < ASIZE (Vfontset_table); i++)
1261 {
1262 Lisp_Object fontset, this_name;
1263
1264 fontset = FONTSET_FROM_ID (i);
1265 if (NILP (fontset)
1266 || !BASE_FONTSET_P (fontset))
1267 continue;
1268
1269 this_name = FONTSET_NAME (fontset);
1270 if (name_pattern == 1
1271 ? fast_string_match (name, this_name) >= 0
1272 : !strcmp (SDATA (name), SDATA (this_name)))
1273 return i;
1274 }
1275 return -1;
1276}
1277
1278
1279DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
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)
1286 Lisp_Object pattern, regexpp;
1287{
1288 Lisp_Object fontset;
1289 int id;
1290
1291 (*check_window_system_func) ();
1292
1293 CHECK_STRING (pattern);
1294
1295 if (SCHARS (pattern) == 0)
1296 return Qnil;
1297
1298 id = fs_query_fontset (pattern, !NILP (regexpp));
1299 if (id < 0)
1300 return Qnil;
1301
1302 fontset = FONTSET_FROM_ID (id);
1303 return FONTSET_NAME (fontset);
1304}
1305
1306/* Return a list of base fontset names matching PATTERN on frame F. */
1307
1308Lisp_Object
1309list_fontsets (f, pattern, size)
1310 FRAME_PTR f;
1311 Lisp_Object pattern;
1312 int size;
1313{
1314 Lisp_Object frame, regexp, val;
1315 int id;
1316
1317 XSETFRAME (frame, f);
1318
1319 regexp = fontset_pattern_regexp (pattern);
1320 val = Qnil;
1321
1322 for (id = 0; id < ASIZE (Vfontset_table); id++)
1323 {
1324 Lisp_Object fontset, name;
1325
1326 fontset = FONTSET_FROM_ID (id);
1327 if (NILP (fontset)
1328 || !BASE_FONTSET_P (fontset)
1329 || !EQ (frame, FONTSET_FRAME (fontset)))
1330 continue;
1331 name = FONTSET_NAME (fontset);
1332
1333 if (STRINGP (regexp)
1334 ? (fast_string_match (regexp, name) < 0)
1335 : strcmp (SDATA (pattern), SDATA (name)))
1336 continue;
1337
1338 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1339 }
1340
1341 return val;
1342}
1343
1344
1345/* Free all realized fontsets whose base fontset is BASE. */
1346
1347static void
1348free_realized_fontsets (base)
1349 Lisp_Object base;
1350{
1351 int id;
1352
1353#if 0
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. */
1358 BLOCK_INPUT;
1359 for (id = 0; id < ASIZE (Vfontset_table); id++)
1360 {
1361 Lisp_Object this = AREF (Vfontset_table, id);
1362
1363 if (EQ (FONTSET_BASE (this), base))
1364 {
1365 Lisp_Object tail;
1366
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);
1373
1374 /* Face THIS itself is also freed by the following call. */
1375 free_realized_face (f, face);
1376 }
1377 }
1378 }
1379 UNBLOCK_INPUT;
1380#else /* not 0 */
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
1387 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1388 {
1389 Fclear_face_cache (Qt);
1390 break;
1391 }
1392 }
1393#endif /* not 0 */
1394}
1395
1396
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;
1409
1410 CHECK_STRING (name);
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);
1416 if (id < 0)
1417 error ("Fontset `%s' does not exist", SDATA (name));
1418 return FONTSET_FROM_ID (id);
1419}
1420
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
1435/* Return an ASCII font name generated from fontset name NAME and
1436 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1437
1438static INLINE Lisp_Object
1439generate_ascii_font_name (name, ascii_spec)
1440 Lisp_Object name, ascii_spec;
1441{
1442 Lisp_Object font_spec = Ffont_spec (0, NULL);
1443 Lisp_Object vec;
1444 int i;
1445 char xlfd[256];
1446
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++)
1450 if (! NILP (AREF (ascii_spec, i)))
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);
1456}
1457
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
1468static void
1469set_fontset_font (fontset, range)
1470 Lisp_Object fontset, range;
1471{
1472 if (from_arg < to_arg)
1473 {
1474 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1475
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);
1498}
1499
1500extern Lisp_Object QCfamily, QCregistry;
1501
1502DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1503 doc: /*
1504Modify fontset NAME to use FONT-SPEC for TARGET characters.
1505
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).
1509
1510TARGET may be a script name symbol. In that case, use FONT-SPEC for
1511all characters that belong to the script.
1512
1513TARGET may be a charset. In that case, use FONT-SPEC for all
1514characters in the charset.
1515
1516TARGET may be nil. In that case, use FONT-SPEC for any characters for
1517that no FONT-SPEC is specified.
1518
1519FONT-SPEC may one of these:
1520 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1521 REGISTRY is a font registry name. FAMILY may contains foundry
1522 name, and REGISTRY may contains encoding name.
1523 * A font name string.
1524 * nil, which explicitly specifies that there's no font for TARGET.
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
1530to the font specifications for TARGET previously set. If it is
1531`prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1532appended. By default, FONT-SPEC overrides the previous settings. */)
1533 (name, target, font_spec, frame, add)
1534 Lisp_Object name, target, font_spec, frame, add;
1535{
1536 Lisp_Object fontset;
1537 Lisp_Object font_def, registry, family;
1538 Lisp_Object encoding, repertory;
1539 Lisp_Object range_list;
1540 struct charset *charset = NULL;
1541
1542 fontset = check_fontset_name (name);
1543
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
1549 if (VECTORP (font_spec))
1550 {
1551 if (! FONT_SPEC_P (font_spec))
1552 Fsignal (Qfont, list2 (build_string ("invalid font-spec"), font_spec));
1553 }
1554 else if (CONSP (font_spec))
1555 {
1556 Lisp_Object args[4];
1557 int i= 0;
1558
1559 family = XCAR (font_spec);
1560 registry = XCDR (font_spec);
1561
1562 if (! NILP (family))
1563 {
1564 CHECK_STRING (family);
1565 args[i++] = QCfamily;
1566 args[i++] = family;
1567 }
1568 CHECK_STRING (registry);
1569 args[i++] = QCregistry;
1570 args[i++] = registry;
1571 font_spec = Ffont_spec (i, args);
1572 }
1573 else if (STRINGP (font_spec))
1574 {
1575 Lisp_Object args[2];
1576
1577 args[0] = QCname;
1578 args[1] = font_spec;
1579 font_spec = Ffont_spec (2, args);
1580 }
1581 else if (! NILP (font_spec))
1582 wrong_type_argument (intern ("font-spec"), font_spec);
1583
1584 if (! NILP (font_spec))
1585 {
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
1603 {
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 }
1613 }
1614 font_def = Fmake_vector (make_number (3), font_spec);
1615 ASET (font_def, 1, encoding);
1616 ASET (font_def, 2, repertory);
1617 }
1618 else
1619 font_def = Qnil;
1620
1621 if (CHARACTERP (target))
1622 range_list = Fcons (Fcons (target, target), Qnil);
1623 else if (CONSP (target))
1624 {
1625 Lisp_Object from, to;
1626
1627 from = Fcar (target);
1628 to = Fcdr (target);
1629 CHECK_CHARACTER (from);
1630 CHECK_CHARACTER (to);
1631 range_list = Fcons (target, Qnil);
1632 }
1633 else if (SYMBOLP (target) && !NILP (target))
1634 {
1635 Lisp_Object script_list;
1636 Lisp_Object val;
1637
1638 range_list = Qnil;
1639 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1640 if (! NILP (Fmemq (target, script_list)))
1641 {
1642 val = Fcons (target, Qnil);
1643 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1644 val);
1645 range_list = XCDR (val);
1646 if (EQ (target, Qlatin) && NILP (FONTSET_ASCII (fontset)))
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 }
1655 }
1656 if (CHARSETP (target))
1657 {
1658 if (EQ (target, Qascii) && NILP (FONTSET_ASCII (fontset)))
1659 {
1660 if (VECTORP (font_spec))
1661 font_spec = generate_ascii_font_name (FONTSET_NAME (fontset),
1662 font_spec);
1663 FONTSET_ASCII (fontset) = font_spec;
1664 range_list = Fcons (Fcons (make_number (0), make_number (127)),
1665 Qnil);
1666 }
1667 else
1668 {
1669 CHECK_CHARSET_GET_CHARSET (target, charset);
1670 }
1671 }
1672 else if (NILP (range_list))
1673 error ("Invalid script or charset name: %s",
1674 SDATA (SYMBOL_NAME (target)));
1675 }
1676 else if (NILP (target))
1677 range_list = Fcons (Qnil, Qnil);
1678 else
1679 error ("Invalid target for setting a font");
1680
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 }
1696 for (; CONSP (range_list); range_list = XCDR (range_list))
1697 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1698
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);
1703
1704 return Qnil;
1705}
1706
1707
1708DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1709 doc: /* Create a new fontset NAME from font information in FONTLIST.
1710
1711FONTLIST is an alist of scripts vs the corresponding font specification list.
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.
1715
1716SCRIPT is a symbol that appears in the first extra slot of the
1717char-table `char-script-table'.
1718
1719FONT-SPEC is a vector, a cons, or a string. See the documentation of
1720`set-fontset-font' for the meaning. */)
1721 (name, fontlist)
1722 Lisp_Object name, fontlist;
1723{
1724 Lisp_Object fontset;
1725 Lisp_Object val;
1726 int id;
1727
1728 CHECK_STRING (name);
1729 CHECK_LIST (fontlist);
1730
1731 id = fs_query_fontset (name, 0);
1732 if (id < 0)
1733 {
1734 name = Fdowncase (name);
1735 val = split_font_name_into_vector (name);
1736 if (NILP (val) || NILP (AREF (val, 12)) || NILP (AREF (val, 13)))
1737 error ("Fontset name must be in XLFD format");
1738 if (strcmp (SDATA (AREF (val, 12)), "fontset"))
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 }
1749 else
1750 {
1751 fontset = FONTSET_FROM_ID (id);;
1752 free_realized_fontsets (fontset);
1753 Fset_char_table_range (fontset, Qt, Qnil);
1754 }
1755
1756 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1757 {
1758 Lisp_Object elt, script;
1759
1760 elt = Fcar (fontlist);
1761 script = Fcar (elt);
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);
1768 }
1769 return name;
1770}
1771
1772
1773/* Alist of automatically created fontsets. Each element is a cons
1774 (FONTNAME . FONTSET-ID). */
1775static Lisp_Object auto_fontset_alist;
1776
1777int
1778new_fontset_from_font_name (Lisp_Object fontname)
1779{
1780 Lisp_Object val;
1781 Lisp_Object name;
1782 Lisp_Object vec;
1783 int id;
1784
1785 fontname = Fdowncase (fontname);
1786 val = Fassoc (fontname, auto_fontset_alist);
1787 if (CONSP (val))
1788 return XINT (XCDR (val));
1789
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"));
1794 if (NILP (auto_fontset_alist))
1795 {
1796 ASET (vec, 13, build_string ("startup"));
1797 name = build_font_name_from_vector (vec);
1798 }
1799 else
1800 {
1801 char temp[20];
1802 int len = XINT (Flength (auto_fontset_alist));
1803
1804 sprintf (temp, "auto%d", len);
1805 ASET (vec, 13, build_string (temp));
1806 name = build_font_name_from_vector (vec);
1807 }
1808 name = Fnew_fontset (name, list2 (list2 (Qascii, fontname),
1809 list2 (Fcons (make_number (0),
1810 make_number (MAX_CHAR)),
1811 fontname)));
1812 id = fs_query_fontset (name, 0);
1813 auto_fontset_alist
1814 = Fcons (Fcons (fontname, make_number (id)), auto_fontset_alist);
1815 return id;
1816}
1817
1818#ifdef USE_FONT_BACKEND
1819int
1820new_fontset_from_font (font_object)
1821 Lisp_Object font_object;
1822{
1823 Lisp_Object font_name = font_get_name (font_object);
1824 Lisp_Object font_spec = font_get_spec (font_object);
1825 Lisp_Object fontset_spec, short_name, name, fontset;
1826
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));
1833
1834 sprintf (temp, "fontset-auto%d", len);
1835 short_name = build_string (temp);
1836 }
1837 fontset_spec = Fcopy_sequence (font_spec);
1838 ASET (fontset_spec, FONT_REGISTRY_INDEX, short_name);
1839 name = Ffont_xlfd_name (fontset_spec);
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))
1846 ASET (fontset_spec, i, Qnil);
1847 name = Ffont_xlfd_name (fontset_spec);
1848 if (NILP (name))
1849 abort ();
1850 }
1851 fontset = make_fontset (Qnil, name, Qnil);
1852 FONTSET_ASCII (fontset) = font_name;
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);
1856 XSETCDR (font_spec, build_string ("iso10646-1"));
1857 Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qappend);
1858 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1859 return XINT (FONTSET_ID (fontset));
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 }
1902 if (NILP (font_object))
1903 return NULL;
1904 return XSAVE_VALUE (font_object)->pointer;
1905}
1906
1907#endif /* USE_FONT_BACKEND */
1908
1909DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
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)
1924 Lisp_Object name, frame;
1925{
1926 FRAME_PTR f;
1927 struct font_info *fontp;
1928 Lisp_Object info;
1929 Lisp_Object font_object;
1930
1931 (*check_window_system_func) ();
1932
1933 CHECK_STRING (name);
1934 name = Fdowncase (name);
1935 if (NILP (frame))
1936 frame = selected_frame;
1937 CHECK_LIVE_FRAME (frame);
1938 f = XFRAME (frame);
1939
1940 if (!query_font_func)
1941 error ("Font query function is not supported");
1942
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 */
1954 fontp = (*query_font_func) (f, SDATA (name));
1955 if (!fontp)
1956 return Qnil;
1957
1958 info = Fmake_vector (make_number (7), Qnil);
1959
1960 XVECTOR (info)->contents[0] = build_string (fontp->name);
1961 XVECTOR (info)->contents[1] = build_string (fontp->full_name);
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);
1967
1968#ifdef USE_FONT_BACKEND
1969 if (! NILP (font_object))
1970 font_close_object (f, font_object);
1971#endif /* USE_FONT_BACKEND */
1972 return info;
1973}
1974
1975
1976/* Return a cons (FONT-NAME . GLYPH-CODE).
1977 FONT-NAME is the font name for the character at POSITION in the current
1978 buffer. This is computed from all the text properties and overlays
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
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:
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
1995 (3) If POSITION is not nil, and the current buffer is not displayed
1996 in any window.
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
2003DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
2004 doc: /* For internal use only. */)
2005 (position, ch)
2006 Lisp_Object position, ch;
2007{
2008 int pos, pos_byte, dummy;
2009 int face_id;
2010 int c;
2011 struct frame *f;
2012 struct face *face;
2013 Lisp_Object charset, rfont_def;
2014 int cs_id;
2015
2016 if (NILP (position))
2017 {
2018 CHECK_CHARACTER (ch);
2019 c = XINT (ch);
2020 f = XFRAME (selected_frame);
2021 face_id = DEFAULT_FACE_ID;
2022 pos = -1;
2023 cs_id = -1;
2024 }
2025 else
2026 {
2027 Lisp_Object window, charset;
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);
2048 charset = Fget_char_property (position, Qcharset, Qnil);
2049 if (CHARSETP (charset))
2050 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
2051 else
2052 cs_id = -1;
2053 }
2054 if (! CHAR_VALID_P (c, 0))
2055 return Qnil;
2056 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
2057 face = FACE_FROM_ID (f, face_id);
2058 rfont_def = fontset_font (FONTSET_FROM_ID (face->fontset), c, face, cs_id);
2059#ifdef USE_FONT_BACKEND
2060 if (enable_font_backend)
2061 {
2062 if (VECTORP (rfont_def) && ! NILP (AREF (rfont_def, 3)))
2063 {
2064 Lisp_Object font_object = AREF (rfont_def, 3);
2065 struct font *font = XSAVE_VALUE (font_object)->pointer;
2066 unsigned code = font->driver->encode_char (font, c);
2067 Lisp_Object fontname = font_get_name (font_object);
2068
2069 if (code == FONT_INVALID_CODE)
2070 return Qnil;
2071 if (code <= MOST_POSITIVE_FIXNUM)
2072 return Fcons (fontname, make_number (code));
2073 return Fcons (fontname, Fcons (make_number (code >> 16),
2074 make_number (code & 0xFFFF)));
2075 }
2076 return Qnil;
2077 }
2078#endif /* USE_FONT_BACKEND */
2079 if (VECTORP (rfont_def) && STRINGP (AREF (rfont_def, 3)))
2080 {
2081 Lisp_Object font_def;
2082 struct font_info *fontp;
2083 struct charset *charset;
2084 XChar2b char2b;
2085 int code;
2086
2087 font_def = AREF (rfont_def, 2);
2088 charset = CHARSET_FROM_ID (XINT (AREF (font_def, 1)));
2089 code = ENCODE_CHAR (charset, c);
2090 if (code == CHARSET_INVALID_CODE (charset))
2091 return (Fcons (AREF (rfont_def, 3), Qnil));
2092 STORE_XCHAR2B (&char2b, ((code >> 8) & 0xFF), (code & 0xFF));
2093 fontp = (*get_font_info_func) (f, XINT (AREF (rfont_def, 1)));
2094 FRAME_RIF (f)->encode_char (c, &char2b, fontp, charset, NULL);
2095 code = (XCHAR2B_BYTE1 (&char2b) << 8) | XCHAR2B_BYTE2 (&char2b);
2096 return (Fcons (AREF (rfont_def, 3), make_number (code)));
2097 }
2098 return Qnil;
2099}
2100
2101
2102DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
2103 doc: /* Return information about a fontset FONTSET on frame FRAME.
2104The value is a char-table of which elements has this form.
2105
2106 ((FONT-PATTERN OPENED-FONT ...) ...)
2107
2108FONT-PATTERN is a vector:
2109
2110 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
2111
2112or a string of font name pattern.
2113
2114OPENED-FONT is a name of a font actually opened.
2115
2116The char-table has one extra slot. The value is a char-table
2117containing the information about the derived fonts from the default
2118fontset. The format is the same as abobe. */)
2119 (fontset, frame)
2120 Lisp_Object fontset, frame;
2121{
2122 FRAME_PTR f;
2123 Lisp_Object *realized[2], fontsets[2], tables[2];
2124 Lisp_Object val, elt;
2125 int c, i, j, k;
2126
2127 (*check_window_system_func) ();
2128
2129 fontset = check_fontset_name (fontset);
2130
2131 if (NILP (frame))
2132 frame = selected_frame;
2133 CHECK_LIVE_FRAME (frame);
2134 f = XFRAME (frame);
2135
2136 /* Recode fontsets realized on FRAME from the base fontset FONTSET
2137 in the table `realized'. */
2138 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
2139 * ASIZE (Vfontset_table));
2140 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
2141 {
2142 elt = FONTSET_FROM_ID (i);
2143 if (!NILP (elt)
2144 && EQ (FONTSET_BASE (elt), fontset)
2145 && EQ (FONTSET_FRAME (elt), frame))
2146 realized[0][j++] = elt;
2147 }
2148 realized[0][j] = Qnil;
2149
2150 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
2151 * ASIZE (Vfontset_table));
2152 for (i = j = 0; ! NILP (realized[0][i]); i++)
2153 {
2154 elt = FONTSET_DEFAULT (realized[0][i]);
2155 if (! NILP (elt))
2156 realized[1][j++] = elt;
2157 }
2158 realized[1][j] = Qnil;
2159
2160 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
2161 tables[1] = Fmake_char_table (Qnil, Qnil);
2162 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
2163 fontsets[0] = fontset;
2164 fontsets[1] = Vdefault_fontset;
2165
2166 /* Accumulate information of the fontset in TABLE. The format of
2167 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
2168 for (k = 0; k <= 1; k++)
2169 {
2170 for (c = 0; c <= MAX_CHAR; )
2171 {
2172 int from, to;
2173
2174 if (c <= MAX_5_BYTE_CHAR)
2175 {
2176 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
2177 if (to > MAX_5_BYTE_CHAR)
2178 to = MAX_5_BYTE_CHAR;
2179 }
2180 else
2181 {
2182 val = FONTSET_FALLBACK (fontsets[k]);
2183 to = MAX_CHAR;
2184 }
2185 if (VECTORP (val))
2186 {
2187 Lisp_Object alist;
2188
2189 /* At first, set ALIST to ((FONT-SPEC) ...). */
2190 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
2191 {
2192 if (NILP (AREF (val, i)))
2193 alist = Fcons (Qnil, alist);
2194 else
2195 alist = Fcons (Fcons (AREF (AREF (val, i), 0), Qnil), alist);
2196 }
2197 alist = Fnreverse (alist);
2198
2199 /* Then store opend font names to cdr of each elements. */
2200 for (i = 0; ! NILP (realized[k][i]); i++)
2201 {
2202 if (c <= MAX_5_BYTE_CHAR)
2203 val = FONTSET_REF (realized[k][i], c);
2204 else
2205 val = FONTSET_FALLBACK (realized[k][i]);
2206 if (! VECTORP (val))
2207 continue;
2208#ifdef USE_FONT_BACKEND
2209 /* VAL: [int int ?
2210 [FACE-ID FONT-INDEX FONT-DEF FONT-ENTITY/OBJECT]
2211 ...] */
2212 if (enable_font_backend)
2213 for (j = 3; j < ASIZE (val); j++)
2214 {
2215 elt = AREF (val, j);
2216 if (INTEGERP (AREF (elt, 1))
2217 && XINT (AREF (elt, 1)) >= 0)
2218 {
2219 Lisp_Object font_object = AREF (elt, 3);
2220
2221 if (FONT_OBJECT_P (font_object))
2222 {
2223 struct font *font
2224 = XSAVE_VALUE (font_object)->pointer;
2225 char *name = font->font.full_name;;
2226 int len = strlen (name);
2227 Lisp_Object slot;
2228
2229 slot = Fassq (AREF (AREF (elt, 2), 0), alist);
2230 nconc2 (slot,
2231 Fcons (make_unibyte_string (name, len),
2232 Qnil));
2233 }
2234 }
2235 }
2236 else
2237#endif /* not USE_FONT_BACKEND */
2238 {
2239 /* VAL is [int int ?
2240 [FACE-ID FONT-INDEX FONT-DEF FONT-NAME] ...].
2241 If a font of an element is already opened,
2242 FONT-NAME is the name of a opened font. */
2243 for (j = 3; j < ASIZE (val); j++)
2244 if (STRINGP (AREF (AREF (val, j), 3)))
2245 {
2246 Lisp_Object font_idx;
2247
2248 font_idx = AREF (AREF (val, j), 1);
2249 elt = Fassq (AREF (AREF (AREF (val, j), 2), 0),
2250 alist);
2251 if (CONSP (elt)
2252 && NILP (Fmemq (font_idx, XCDR(elt))))
2253 nconc2 (elt, Fcons (font_idx, Qnil));
2254 }
2255 for (val = alist; CONSP (val); val = XCDR (val))
2256 for (elt = XCDR (XCAR (val)); CONSP (elt);
2257 elt = XCDR (elt))
2258 {
2259 struct font_info *font_info
2260 = (*get_font_info_func) (f, XINT (XCAR (elt)));
2261 XSETCAR (elt, build_string (font_info->full_name));
2262 }
2263 }
2264 }
2265
2266 /* Store ALIST in TBL for characters C..TO. */
2267 if (c <= MAX_5_BYTE_CHAR)
2268 char_table_set_range (tables[k], c, to, alist);
2269 else
2270 XCHAR_TABLE (tables[k])->defalt = alist;
2271 }
2272 c = to + 1;
2273 }
2274 }
2275
2276 return tables[0];
2277}
2278
2279
2280DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2281 doc: /* Return a font name pattern for character CH in fontset NAME.
2282If NAME is t, find a pattern in the default fontset.
2283
2284The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2285family name and REGISTRY is a font registry name. This is actually
2286the first font name pattern for CH in the fontset or in the default
2287fontset.
2288
2289If the 2nd optional arg ALL is non-nil, return a list of all font name
2290patterns. */)
2291 (name, ch, all)
2292 Lisp_Object name, ch, all;
2293{
2294 int c;
2295 Lisp_Object fontset, elt, list, repertory, val;
2296 int i, j;
2297
2298 fontset = check_fontset_name (name);
2299
2300 CHECK_CHARACTER (ch);
2301 c = XINT (ch);
2302 list = Qnil;
2303 while (1)
2304 {
2305 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2306 i++, elt = FONTSET_FALLBACK (fontset))
2307 if (VECTORP (elt))
2308 for (j = 0; j < ASIZE (elt); j++)
2309 {
2310 val = AREF (elt, j);
2311 repertory = AREF (val, 1);
2312 if (INTEGERP (repertory))
2313 {
2314 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2315
2316 if (! CHAR_CHARSET_P (c, charset))
2317 continue;
2318 }
2319 else if (CHAR_TABLE_P (repertory))
2320 {
2321 if (NILP (CHAR_TABLE_REF (repertory, c)))
2322 continue;
2323 }
2324 val = AREF (val, 0);
2325 val = Fcons (AREF (val, 0), AREF (val, 5));
2326 if (NILP (all))
2327 return val;
2328 list = Fcons (val, list);
2329 }
2330 if (EQ (fontset, Vdefault_fontset))
2331 break;
2332 fontset = Vdefault_fontset;
2333 }
2334 return (Fnreverse (list));
2335}
2336
2337DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2338 doc: /* Return a list of all defined fontset names. */)
2339 ()
2340{
2341 Lisp_Object fontset, list;
2342 int i;
2343
2344 list = Qnil;
2345 for (i = 0; i < ASIZE (Vfontset_table); i++)
2346 {
2347 fontset = FONTSET_FROM_ID (i);
2348 if (!NILP (fontset)
2349 && BASE_FONTSET_P (fontset))
2350 list = Fcons (FONTSET_NAME (fontset), list);
2351 }
2352
2353 return list;
2354}
2355
2356
2357#ifdef FONTSET_DEBUG
2358
2359Lisp_Object
2360dump_fontset (fontset)
2361 Lisp_Object fontset;
2362{
2363 Lisp_Object vec;
2364
2365 vec = Fmake_vector (make_number (3), Qnil);
2366 ASET (vec, 0, FONTSET_ID (fontset));
2367
2368 if (BASE_FONTSET_P (fontset))
2369 {
2370 ASET (vec, 1, FONTSET_NAME (fontset));
2371 }
2372 else
2373 {
2374 Lisp_Object frame;
2375
2376 frame = FONTSET_FRAME (fontset);
2377 if (FRAMEP (frame))
2378 {
2379 FRAME_PTR f = XFRAME (frame);
2380
2381 if (FRAME_LIVE_P (f))
2382 ASET (vec, 1,
2383 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2384 else
2385 ASET (vec, 1,
2386 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2387 }
2388 if (!NILP (FONTSET_DEFAULT (fontset)))
2389 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2390 }
2391 return vec;
2392}
2393
2394DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2395 doc: /* Return a brief summary of all fontsets for debug use. */)
2396 ()
2397{
2398 Lisp_Object val;
2399 int i;
2400
2401 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2402 if (! NILP (AREF (Vfontset_table, i)))
2403 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2404 return (Fnreverse (val));
2405}
2406#endif /* FONTSET_DEBUG */
2407
2408void
2409syms_of_fontset ()
2410{
2411 if (!load_font_func)
2412 /* Window system initializer should have set proper functions. */
2413 abort ();
2414
2415 DEFSYM (Qfontset, "fontset");
2416 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2417 DEFSYM (Qfontset_info, "fontset-info");
2418 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2419
2420 DEFSYM (Qprepend, "prepend");
2421 DEFSYM (Qappend, "append");
2422 DEFSYM (Qlatin, "latin");
2423
2424 Vcached_fontset_data = Qnil;
2425 staticpro (&Vcached_fontset_data);
2426
2427 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2428 staticpro (&Vfontset_table);
2429
2430 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2431 staticpro (&Vdefault_fontset);
2432 FONTSET_ID (Vdefault_fontset) = make_number (0);
2433 FONTSET_NAME (Vdefault_fontset)
2434 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2435 ASET (Vfontset_table, 0, Vdefault_fontset);
2436 next_fontset_id = 1;
2437
2438 auto_fontset_alist = Qnil;
2439 staticpro (&auto_fontset_alist);
2440
2441 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
2442 doc: /*
2443Alist of fontname patterns vs the corresponding encoding and repertory info.
2444Each element looks like (REGEXP . (ENCODING . REPERTORY)),
2445where ENCODING is a charset or a char-table,
2446and REPERTORY is a charset, a char-table, or nil.
2447
2448If ENCDING and REPERTORY are the same, the element can have the form
2449\(REGEXP . ENCODING).
2450
2451ENCODING is for converting a character to a glyph code of the font.
2452If ENCODING is a charset, encoding a character by the charset gives
2453the corresponding glyph code. If ENCODING is a char-table, looking up
2454the table by a character gives the corresponding glyph code.
2455
2456REPERTORY specifies a repertory of characters supported by the font.
2457If REPERTORY is a charset, all characters beloging to the charset are
2458supported. If REPERTORY is a char-table, all characters who have a
2459non-nil value in the table are supported. It REPERTORY is nil, Emacs
2460gets the repertory information by an opened font and ENCODING. */);
2461 Vfont_encoding_alist = Qnil;
2462
2463 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2464 doc: /*
2465Alist of charsets vs the charsets to determine the preferred font encoding.
2466Each element looks like (CHARSET . ENCDOING-CHARSET),
2467where ENCODING-CHARSET is a charset registered in the variable
2468`font-encoding-alist' as ENCODING.
2469
2470When a text has a property `charset' and the value is CHARSET, a font
2471whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2472 Vfont_encoding_charset_alist = Qnil;
2473
2474 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
2475 doc: /*
2476Char table of characters whose ascent values should be ignored.
2477If an entry for a character is non-nil, the ascent value of the glyph
2478is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.
2479
2480This affects how a composite character which contains
2481such a character is displayed on screen. */);
2482 Vuse_default_ascent = Qnil;
2483
2484 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
2485 doc: /*
2486Char table of characters which is not composed relatively.
2487If an entry for a character is non-nil, a composition sequence
2488which contains that character is displayed so that
2489the glyph of that character is put without considering
2490an ascent and descent value of a previous character. */);
2491 Vignore_relative_composition = Qnil;
2492
2493 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
2494 doc: /* Alist of fontname vs list of the alternate fontnames.
2495When a specified font name is not found, the corresponding
2496alternate fontnames (if any) are tried instead. */);
2497 Valternate_fontname_alist = Qnil;
2498
2499 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
2500 doc: /* Alist of fontset names vs the aliases. */);
2501 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2502 build_string ("fontset-default")),
2503 Qnil);
2504
2505 DEFVAR_LISP ("vertical-centering-font-regexp",
2506 &Vvertical_centering_font_regexp,
2507 doc: /* *Regexp matching font names that require vertical centering on display.
2508When a character is displayed with such fonts, the character is displayed
2509at the vertical center of lines. */);
2510 Vvertical_centering_font_regexp = Qnil;
2511
2512 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2513 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2514 Votf_script_alist = Qnil;
2515
2516 defsubr (&Squery_fontset);
2517 defsubr (&Snew_fontset);
2518 defsubr (&Sset_fontset_font);
2519 defsubr (&Sfont_info);
2520 defsubr (&Sinternal_char_font);
2521 defsubr (&Sfontset_info);
2522 defsubr (&Sfontset_font);
2523 defsubr (&Sfontset_list);
2524#ifdef FONTSET_DEBUG
2525 defsubr (&Sfontset_list_all);
2526#endif
2527}
2528
2529/* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2530 (do not change this comment) */