Fix previous change.
[emacs.git] / src / fontset.c
blob48a32ea2dd5f296562a42f2e47dd8026182ef489
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
12 This file is part of GNU Emacs.
14 GNU Emacs is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3, or (at your option)
17 any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA. */
29 /* #define FONTSET_DEBUG */
31 #include <config.h>
33 #ifdef FONTSET_DEBUG
34 #include <stdio.h>
35 #endif
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"
60 #include "font.h"
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 */
71 EXFUN (Fclear_face_cache, 1);
73 /* FONTSET
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.
81 Another role, the more important one, is to provide information
82 about which font to use for each non-ASCII character.
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.
91 A fontset object is implemented by a char-table whose default value
92 and parent are always nil.
94 An element of a base fontset is a vector of FONT-DEFs which itself
95 is a vector [ FONT-SPEC ENCODING REPERTORY ].
97 FONT-SPEC is a font-spec created by `font-spec' or
98 ( FAMILY . REGISTRY )
100 FONT-NAME
101 where FAMILY, REGISTRY, and FONT-NAME are strings.
103 ENCODING is a charset ID that can convert characters to glyph codes
104 of the corresponding font.
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.
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.
117 An element of a realized fontset is nil or t, or has this form:
119 [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-FAMILY
120 RFONT-DEF0 RFONT-DEF1 ...].
122 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
124 [ FACE-ID FONT-INDEX FONT-DEF OPENED-FONT-NAME ]
126 RFONT-DEFn is automatically reordered by the current charset
127 priority list.
129 The value nil means that we have not yet generated the above vector
130 from the base of the fontset.
132 The value t means that no font is available for the corresponding
133 range of characters.
136 A fontset has 9 extra slots.
138 The 1st slot: the ID number of the fontset
140 The 2nd slot:
141 base: the name of the fontset
142 realized: nil
144 The 3rd slot:
145 base: nil
146 realized: the base fontset
148 The 4th slot:
149 base: nil
150 realized: the frame that the fontset belongs to
152 The 5th slot:
153 base: the font name for ASCII characters
154 realized: nil
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.
161 The 7th slot:
162 base: nil
163 realized: Alist of font index vs the corresponding repertory
164 char-table.
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.
171 The 9th slot:
172 base: Same as element value (but for fallback fonts).
173 realized: Likewise.
175 All fontsets are recorded in the vector Vfontset_table.
178 DEFAULT FONTSET
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.
185 The parent of a realized fontset created for such a face that has
186 no fontset is the default fontset.
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.
197 /********** VARIABLES and FUNCTION PROTOTYPES **********/
199 extern Lisp_Object Qfont;
200 static Lisp_Object Qfontset;
201 static Lisp_Object Qfontset_info;
202 static Lisp_Object Qprepend, Qappend;
203 static Lisp_Object Qlatin;
205 /* Vector containing all fontsets. */
206 static Lisp_Object Vfontset_table;
208 /* Next possibly free fontset ID. Usually this keeps the minimum
209 fontset ID not yet used. */
210 static int next_fontset_id;
212 /* The default fontset. This gives default FAMILY and REGISTRY of
213 font for each character. */
214 static Lisp_Object Vdefault_fontset;
216 Lisp_Object Vfont_encoding_alist;
217 Lisp_Object Vfont_encoding_charset_alist;
218 Lisp_Object Vuse_default_ascent;
219 Lisp_Object Vignore_relative_composition;
220 Lisp_Object Valternate_fontname_alist;
221 Lisp_Object Vfontset_alias_alist;
222 Lisp_Object Vvertical_centering_font_regexp;
223 Lisp_Object Votf_script_alist;
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. */
229 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
230 struct font_info *(*get_font_info_func) P_ ((FRAME_PTR f, int font_idx));
232 /* Return a list of font names which matches PATTERN. See the documentation
233 of `x-list-fonts' for more details. */
234 Lisp_Object (*list_fonts_func) P_ ((struct frame *f,
235 Lisp_Object pattern,
236 int size,
237 int maxnames));
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. */
241 struct font_info *(*load_font_func) P_ ((FRAME_PTR f, char *name, int));
243 /* Return a pointer to struct font_info of a font named NAME for frame F. */
244 struct font_info *(*query_font_func) P_ ((FRAME_PTR f, char *name));
246 /* Additional function for setting fontset or changing fontset
247 contents of frame F. */
248 void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
249 Lisp_Object oldval));
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. */
254 void (*find_ccl_program_func) P_ ((struct font_info *));
256 Lisp_Object (*get_font_repertory_func) P_ ((struct frame *,
257 struct font_info *));
259 /* Check if any window system is used now. */
260 void (*check_window_system_func) P_ ((void));
263 /* Prototype declarations for static functions. */
264 static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
265 Lisp_Object));
266 static void reorder_font_vector P_ ((Lisp_Object, int, Lisp_Object));
267 static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
268 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
269 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
270 static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
271 Lisp_Object));
272 Lisp_Object find_font_encoding P_ ((Lisp_Object));
274 static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
276 #ifdef FONTSET_DEBUG
278 /* Return 1 if ID is a valid fontset id, else return 0. */
280 static int
281 fontset_id_valid_p (id)
282 int id;
284 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
287 #endif
291 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
293 /* Return the fontset with ID. No check of ID's validness. */
294 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
296 /* Macros to access special values of FONTSET. */
297 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
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]
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_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4]
307 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
308 #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
309 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
311 /* For both base and realized fontset. */
312 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
314 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
317 /* Return the element of FONTSET for the character C. If FONTSET is a
318 base fontset other then the default fontset and FONTSET doesn't
319 contain information for C, return the information in the default
320 fontset. */
322 #define FONTSET_REF(fontset, c) \
323 (EQ (fontset, Vdefault_fontset) \
324 ? CHAR_TABLE_REF (fontset, c) \
325 : fontset_ref ((fontset), (c)))
327 static Lisp_Object
328 fontset_ref (fontset, c)
329 Lisp_Object fontset;
330 int c;
332 Lisp_Object elt;
334 elt = CHAR_TABLE_REF (fontset, c);
335 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
336 /* Don't check Vdefault_fontset for a realized fontset. */
337 && NILP (FONTSET_BASE (fontset)))
338 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
339 return elt;
342 /* Set elements of FONTSET for characters in RANGE to the value ELT.
343 RANGE is a cons (FROM . TO), where FROM and TO are character codes
344 specifying a range. */
346 #define FONTSET_SET(fontset, range, elt) \
347 Fset_char_table_range ((fontset), (range), (elt))
350 /* Modify the elements of FONTSET for characters in RANGE by replacing
351 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
352 and TO are character codes specifying a range. If ADD is nil,
353 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
354 append ELT. */
356 #define FONTSET_ADD(fontset, range, elt, add) \
357 (NILP (add) \
358 ? (NILP (range) \
359 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
360 : Fset_char_table_range ((fontset), (range), \
361 Fmake_vector (make_number (1), (elt)))) \
362 : fontset_add ((fontset), (range), (elt), (add)))
364 static Lisp_Object
365 fontset_add (fontset, range, elt, add)
366 Lisp_Object fontset, range, elt, add;
368 Lisp_Object args[2];
369 int idx = (EQ (add, Qappend) ? 0 : 1);
371 args[1 - idx] = Fmake_vector (make_number (1), elt);
373 if (CONSP (range))
375 int from = XINT (XCAR (range));
376 int to = XINT (XCDR (range));
377 int from1, to1;
379 do {
380 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
381 if (to < to1)
382 to1 = to;
383 char_table_set_range (fontset, from, to1,
384 NILP (args[idx]) ? args[1 - idx]
385 : Fvconcat (2, args));
386 from = to1 + 1;
387 } while (from < to);
389 else
391 args[idx] = FONTSET_FALLBACK (fontset);
392 FONTSET_FALLBACK (fontset)
393 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
395 return Qnil;
399 /* Update FONT-GROUP which has this form:
400 [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-FAMILY
401 RFONT-DEF0 RFONT-DEF1 ...].
402 Reorder RFONT-DEFs according to the current order of charset
403 (Vcharset_ordered_list), and update CHARSET-ORDERED-LIST-TICK to
404 the latest value. */
406 static void
407 reorder_font_vector (font_group, charset_id, family)
408 Lisp_Object font_group;
409 int charset_id;
410 Lisp_Object family;
412 Lisp_Object list, *new_vec;
413 int size;
414 int *charset_id_table;
415 int i, idx;
416 Lisp_Object preferred_by_charset, preferred_by_family;
418 size = ASIZE (font_group) - 3;
419 /* Exclude the tailing nil elements from the reordering. */
420 while (NILP (AREF (font_group, size - 1))) size--;
421 charset_id_table = (int *) alloca (sizeof (int) * size);
422 new_vec = (Lisp_Object *) alloca (sizeof (Lisp_Object) * size);
424 /* At first, extract ENCODING (a chaset ID) from RFONT_DEF which
425 has this form:
426 [FACE-ID FONT-INDEX [ FONT-SPEC ENCODING REPERTORY ]]
427 In addtion, if RFONT_DEF is preferred by family or charset, store
428 it from the start of new_vec. */
429 for (i = 0, idx = 0; i < size; i++)
431 Lisp_Object rfont_def = AREF (font_group, i + 3);
432 Lisp_Object font_spec = AREF (AREF (rfont_def, 2), 0);
433 Lisp_Object this_family = AREF (font_spec, FONT_FAMILY_INDEX);
434 int id = XINT (AREF (AREF (rfont_def, 2), 1));
435 struct charset *charset = CHARSET_FROM_ID (id);
437 charset_id_table[i] = -1;
438 if (! NILP (this_family)
439 && (fast_string_match_ignore_case (family, SYMBOL_NAME (this_family))
440 >= 0))
442 if (idx > 0)
443 memmove (new_vec + 1, new_vec, sizeof (Lisp_Object) * idx);
444 new_vec[0] = rfont_def;
445 idx++;
446 ASET (font_group, i + 3, Qnil);
448 else if (id == charset_id)
450 new_vec[idx++] = rfont_def;
451 ASET (font_group, i + 3, Qnil);
453 else if (! charset->supplementary_p)
454 charset_id_table[i] = id;
457 if (idx == 0
458 && (XINT (AREF (font_group, 0)) == charset_ordered_list_tick))
459 /* No need of reordering. */
460 return;
462 ASET (font_group, 0, make_number (charset_ordered_list_tick));
463 ASET (font_group, 1, make_number (charset_id));
464 ASET (font_group, 2, family);
466 /* Then, store the remaining RFONT-DEFs in NEW_VEC in the correct
467 order. */
468 for (list = Vcharset_ordered_list; idx < size; list = XCDR (list))
470 int id = XINT (XCAR (list));
471 struct charset *charset = CHARSET_FROM_ID (id);
473 if (charset->supplementary_p)
474 break;
475 for (i = 0; i < size; i++)
476 if (charset_id_table[i] == XINT (XCAR (list))
477 && ! NILP (AREF (font_group, i + 3)))
479 new_vec[idx++] = AREF (font_group, i + 3);
480 ASET (font_group, i + 3, Qnil);
483 for (i = 0; i < size; i++)
484 if (! NILP (AREF (font_group, i + 3)))
485 new_vec[idx++] = AREF (font_group, i + 3);
487 /* At last, update elements of FONT-GROUP. */
488 for (i = 0; i < size; i++)
489 ASET (font_group, i + 3, new_vec[i]);
493 /* Load a font matching the font related attributes in FACE->lface and
494 font pattern in FONT_DEF of FONTSET, and return an index of the
495 font. FONT_DEF has this form:
496 [ FONT-SPEC ENCODING REPERTORY ]
497 If REPERTORY is nil, generate a char-table representing the font
498 repertory by looking into the font itself. */
500 extern Lisp_Object QCname;
502 static int
503 load_font_get_repertory (f, face, font_def, fontset)
504 FRAME_PTR f;
505 struct face *face;
506 Lisp_Object font_def;
507 Lisp_Object fontset;
509 char *font_name;
510 struct font_info *font_info;
511 int charset;
512 Lisp_Object font_spec, name;
514 font_spec = AREF (font_def, 0);
515 name = Ffont_get (font_spec, QCname);
516 if (! NILP (name))
517 font_name = choose_face_font (f, face->lface, name, NULL);
518 else
519 font_name = choose_face_font (f, face->lface, font_spec, NULL);
520 charset = XINT (AREF (font_def, 1));
521 if (! (font_info = fs_load_font (f, font_name, charset)))
522 return -1;
524 if (NILP (AREF (font_def, 2))
525 && NILP (Fassq (make_number (font_info->font_idx),
526 FONTSET_REPERTORY (fontset))))
528 /* We must look into the font to get the correct repertory as a
529 char-table. */
530 Lisp_Object repertory;
532 repertory = (*get_font_repertory_func) (f, font_info);
533 FONTSET_REPERTORY (fontset)
534 = Fcons (Fcons (make_number (font_info->font_idx), repertory),
535 FONTSET_REPERTORY (fontset));
538 return font_info->font_idx;
541 static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
542 int, int));
544 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
545 character C. If no font is found, return Qnil if there's a
546 possibility that the default fontset or the fallback font groups
547 have a proper font, and return Qt if not.
549 If a font is found but is not yet opened, open it (if FACE is not
550 NULL) or return Qnil (if FACE is NULL).
552 ID is a charset-id that must be preferred, or -1 meaning no
553 preference.
555 If FALLBACK is nonzero, search only fallback fonts. */
557 static Lisp_Object
558 fontset_find_font (fontset, c, face, id, fallback)
559 Lisp_Object fontset;
560 int c;
561 struct face *face;
562 int id, fallback;
564 Lisp_Object base_fontset, elt, vec, font_def;
565 int i, from, to;
566 int font_idx;
567 FRAME_PTR f = XFRAME (FONTSET_FRAME (fontset));
569 base_fontset = FONTSET_BASE (fontset);
570 if (! fallback)
571 vec = CHAR_TABLE_REF (fontset, c);
572 else
573 vec = FONTSET_FALLBACK (fontset);
575 if (NILP (vec))
577 Lisp_Object range;
579 /* We have not yet decided a font for C. */
580 if (! face)
581 return Qnil;
582 if (! fallback)
584 elt = char_table_ref_and_range (base_fontset, c, &from, &to);
585 range = Fcons (make_number (from), make_number (to));
587 else
589 elt = FONTSET_FALLBACK (base_fontset);
591 if (NILP (elt))
593 /* This fontset doesn't specify any font for C. */
594 vec = make_number (0);
596 else if (ASIZE (elt) == 1 && NILP (AREF (elt, 0)))
598 /* Explicitly specified no font. */
599 vec = Qt;
601 else
603 /* Build a vector [ -1 -1 nil NEW-ELT0 NEW-ELT1 NEW-ELT2 ... ],
604 where the first -1 is to force reordering of NEW-ELTn,
605 NEW-ELTn is [nil nil AREF (elt, n) nil]. */
606 int size = ASIZE (elt);
607 int j;
609 vec = Fmake_vector (make_number (size + 3), Qnil);
610 ASET (vec, 0, make_number (-1));
611 ASET (vec, 1, make_number (-1));
612 for (i = j = 0; i < size; i++)
613 if (! NILP (AREF (elt, i)))
615 Lisp_Object tmp;
616 tmp = Fmake_vector (make_number (5), Qnil);
617 ASET (tmp, 2, AREF (elt, i));
618 ASET (vec, j + 3, tmp);
619 j++;
622 /* Then store it in the fontset. */
623 if (! fallback)
624 FONTSET_SET (fontset, range, vec);
625 else
626 FONTSET_FALLBACK (fontset) = vec;
629 if (! VECTORP (vec))
630 return (EQ (vec, Qt) ? Qt : Qnil);
632 if (ASIZE (vec) > 4
633 && (XINT (AREF (vec, 0)) != charset_ordered_list_tick
634 || (id >= 0 && XINT (AREF (vec, 1)) != id)
635 || NILP (Fequal (AREF (vec, 2), face->lface[LFACE_FAMILY_INDEX]))))
636 /* We have just created VEC,
637 or the charset priorities were changed,
638 or the preferred charset was changed,
639 or the preferred family was changed. */
640 reorder_font_vector (vec, id, face->lface[LFACE_FAMILY_INDEX]);
642 /* Find the first available font in the vector of RFONT-DEF. */
643 for (i = 3; i < ASIZE (vec); i++)
645 elt = AREF (vec, i);
646 if (NILP (elt))
647 /* This is the sign of not to try fallback fonts. */
648 return Qt;
649 /* ELT == [ FACE-ID FONT-INDEX FONT-DEF ... ] */
650 if (INTEGERP (AREF (elt, 1)) && XINT (AREF (elt, 1)) < 0)
651 /* We couldn't open this font last time. */
652 continue;
654 if (!face && NILP (AREF (elt, 1)))
655 /* We have not yet opened the font. */
656 return Qnil;
658 font_def = AREF (elt, 2);
659 /* FONT_DEF == [ FONT-SPEC ENCODING REPERTORY ] */
661 #ifdef USE_FONT_BACKEND
662 if (enable_font_backend)
664 /* ELT == [ FACE-ID FONT-INDEX FONT-DEF FONT-ENTITY ]
665 where FONT-ENTITY turns to a font-object once opened. */
666 Lisp_Object font_entity = AREF (elt, 3);
667 int has_char = 0;
669 if (NILP (font_entity))
671 font_entity = font_find_for_lface (f, face->lface,
672 AREF (font_def, 0), -1);
673 if (NILP (font_entity))
675 ASET (elt, 1, make_number (-1));
676 continue;
678 ASET (elt, 3, font_entity);
680 else if (FONT_ENTITY_P (font_entity))
682 if (FONT_ENTITY_NOT_LOADABLE (font_entity))
683 continue;
685 has_char = font_has_char (f, font_entity, c);
686 if (! has_char)
687 continue;
688 if (! FONT_OBJECT_P (font_entity))
690 Lisp_Object font_object
691 = font_open_for_lface (f, font_entity, face->lface, Qnil);
693 if (NILP (font_object))
695 FONT_ENTITY_SET_NOT_LOADABLE (font_entity);
696 continue;
698 FONTSET_OBJLIST (fontset)
699 = Fcons (font_object, FONTSET_OBJLIST (fontset));
700 ASET (elt, 3, font_object);
701 if (has_char < 0)
703 has_char = font_has_char (f, font_object, c);
704 if (! has_char)
705 continue;
708 /* Decide to use this font. */
709 ASET (elt, 1, make_number (0));
711 else
712 #endif /* USE_FONT_BACKEND */
714 if (INTEGERP (AREF (font_def, 2)))
716 /* The repertory is specified by charset ID. */
717 struct charset *charset
718 = CHARSET_FROM_ID (XINT (AREF (font_def, 2)));
720 if (! CHAR_CHARSET_P (c, charset))
721 /* This font can't display C. */
722 continue;
724 else if (CHAR_TABLE_P (AREF (font_def, 2)))
726 /* The repertory is specified by a char table. */
727 if (NILP (CHAR_TABLE_REF (AREF (font_def, 2), c)))
728 /* This font can't display C. */
729 continue;
731 else
733 Lisp_Object slot;
735 if (! INTEGERP (AREF (elt, 1)))
737 /* We have not yet opened a font matching this spec.
738 Open the best matching font now and register the
739 repertory. */
740 struct font_info *font_info;
742 font_idx = load_font_get_repertory (f, face, font_def, fontset);
743 ASET (elt, 1, make_number (font_idx));
744 if (font_idx < 0)
745 /* This means that we couldn't find a font matching
746 FONT_DEF. */
747 continue;
748 font_info = (*get_font_info_func) (f, font_idx);
749 ASET (elt, 3, build_string (font_info->full_name));
752 slot = Fassq (AREF (elt, 1), FONTSET_REPERTORY (fontset));
753 xassert (CONSP (slot));
754 if (NILP (CHAR_TABLE_REF (XCDR (slot), c)))
755 /* This font can't display C. */
756 continue;
759 /* Now we have decided to use this font spec to display C. */
760 if (! INTEGERP (AREF (elt, 1)))
762 /* But not yet opened the best matching font. */
763 struct font_info *font_info;
765 font_idx = load_font_get_repertory (f, face, font_def, fontset);
766 ASET (elt, 1, make_number (font_idx));
767 if (font_idx < 0)
768 /* Can't open it. Try the other one. */
769 continue;
770 font_info = (*get_font_info_func) (f, font_idx);
771 ASET (elt, 3, build_string (font_info->full_name));
773 return elt;
776 return Qnil;
780 static Lisp_Object
781 fontset_font (fontset, c, face, id)
782 Lisp_Object fontset;
783 int c;
784 struct face *face;
785 int id;
787 Lisp_Object rfont_def;
788 Lisp_Object base_fontset;
790 /* Try a font-group for C. */
791 rfont_def = fontset_find_font (fontset, c, face, id, 0);
792 if (VECTORP (rfont_def))
793 return rfont_def;
794 if (EQ (rfont_def, Qt))
795 return Qnil;
796 base_fontset = FONTSET_BASE (fontset);
797 /* Try a font-group for C of the default fontset. */
798 if (! EQ (base_fontset, Vdefault_fontset))
800 if (NILP (FONTSET_DEFAULT (fontset)))
801 FONTSET_DEFAULT (fontset)
802 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
803 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
804 if (VECTORP (rfont_def))
805 return (rfont_def);
806 if (! NILP (rfont_def))
807 /* Remeber that we have no font for C. */
808 FONTSET_SET (fontset, make_number (c), Qt);
811 /* Try a fallback font-group. */
812 rfont_def = fontset_find_font (fontset, c, face, id, 1);
813 if (! VECTORP (rfont_def)
814 && ! EQ (base_fontset, Vdefault_fontset))
815 /* Try a fallback font-group of the default fontset . */
816 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
818 if (! VECTORP (rfont_def))
819 /* Remeber that we have no font for C. */
820 FONTSET_SET (fontset, make_number (c), Qt);
822 return rfont_def;
825 /* Return a newly created fontset with NAME. If BASE is nil, make a
826 base fontset. Otherwise make a realized fontset whose base is
827 BASE. */
829 static Lisp_Object
830 make_fontset (frame, name, base)
831 Lisp_Object frame, name, base;
833 Lisp_Object fontset;
834 int size = ASIZE (Vfontset_table);
835 int id = next_fontset_id;
837 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
838 the next available fontset ID. So it is expected that this loop
839 terminates quickly. In addition, as the last element of
840 Vfontset_table is always nil, we don't have to check the range of
841 id. */
842 while (!NILP (AREF (Vfontset_table, id))) id++;
844 if (id + 1 == size)
845 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
847 fontset = Fmake_char_table (Qfontset, Qnil);
849 FONTSET_ID (fontset) = make_number (id);
850 if (NILP (base))
852 FONTSET_NAME (fontset) = name;
854 else
856 FONTSET_NAME (fontset) = Qnil;
857 FONTSET_FRAME (fontset) = frame;
858 FONTSET_BASE (fontset) = base;
861 ASET (Vfontset_table, id, fontset);
862 next_fontset_id = id + 1;
863 return fontset;
867 /* Set the ASCII font of the default fontset to FONTNAME if that is
868 not yet set. */
869 void
870 set_default_ascii_font (fontname)
871 Lisp_Object fontname;
873 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
875 int id = fs_query_fontset (fontname, 2);
877 if (id >= 0)
878 fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
879 FONTSET_ASCII (Vdefault_fontset)= fontname;
884 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
886 /* Return the name of the fontset who has ID. */
888 Lisp_Object
889 fontset_name (id)
890 int id;
892 Lisp_Object fontset;
894 fontset = FONTSET_FROM_ID (id);
895 return FONTSET_NAME (fontset);
899 /* Return the ASCII font name of the fontset who has ID. */
901 Lisp_Object
902 fontset_ascii (id)
903 int id;
905 Lisp_Object fontset, elt;
907 fontset= FONTSET_FROM_ID (id);
908 elt = FONTSET_ASCII (fontset);
909 #ifdef USE_FONT_BACKEND
910 if (CONSP (elt))
911 elt = XCAR (elt);
912 #endif /* USE_FONT_BACKEND */
913 /* It is assured that ELT is always a string (i.e. fontname
914 pattern). */
915 return elt;
918 void
919 free_realized_fontset (f, fontset)
920 FRAME_PTR f;
921 Lisp_Object fontset;
923 int i;
924 Lisp_Object tail;
926 return;
927 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
929 xassert (FONT_OBJECT_P (XCAR (tail)));
930 font_close_object (f, XCAR (tail));
934 /* Free fontset of FACE defined on frame F. Called from
935 free_realized_face. */
937 void
938 free_face_fontset (f, face)
939 FRAME_PTR f;
940 struct face *face;
942 Lisp_Object fontset;
944 fontset = FONTSET_FROM_ID (face->fontset);
945 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
946 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
947 free_realized_fontset (f, fontset);
948 ASET (Vfontset_table, face->fontset, Qnil);
949 if (face->fontset < next_fontset_id)
950 next_fontset_id = face->fontset;
951 if (! NILP (FONTSET_DEFAULT (fontset)))
953 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
955 fontset = AREF (Vfontset_table, id);
956 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
957 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
958 free_realized_fontset (f, fontset);
959 ASET (Vfontset_table, id, Qnil);
960 if (id < next_fontset_id)
961 next_fontset_id = face->fontset;
966 /* Return 1 if FACE is suitable for displaying character C.
967 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
968 when C is not an ASCII character. */
971 face_suitable_for_char_p (face, c)
972 struct face *face;
973 int c;
975 Lisp_Object fontset, rfont_def;
977 fontset = FONTSET_FROM_ID (face->fontset);
978 rfont_def = fontset_font (fontset, c, NULL, -1);
979 return (VECTORP (rfont_def)
980 && INTEGERP (AREF (rfont_def, 0))
981 && face->id == XINT (AREF (rfont_def, 0)));
985 /* Return ID of face suitable for displaying character C on frame F.
986 FACE must be reazlied for ASCII characters in advance. Called from
987 the macro FACE_FOR_CHAR. */
990 face_for_char (f, face, c, pos, object)
991 FRAME_PTR f;
992 struct face *face;
993 int c, pos;
994 Lisp_Object object;
996 Lisp_Object fontset, charset, rfont_def;
997 int face_id;
998 int id;
1000 if (ASCII_CHAR_P (c))
1001 return face->ascii_face->id;
1003 xassert (fontset_id_valid_p (face->fontset));
1004 fontset = FONTSET_FROM_ID (face->fontset);
1005 xassert (!BASE_FONTSET_P (fontset));
1006 if (pos < 0)
1007 id = -1;
1008 else
1010 charset = Fget_char_property (make_number (pos), Qcharset, object);
1011 if (NILP (charset))
1012 id = -1;
1013 else if (CHARSETP (charset))
1015 Lisp_Object val;
1017 val = assoc_no_quit (charset, Vfont_encoding_charset_alist);
1018 if (CONSP (val) && CHARSETP (XCDR (val)))
1019 charset = XCDR (val);
1020 id = XINT (CHARSET_SYMBOL_ID (charset));
1023 rfont_def = fontset_font (fontset, c, face, id);
1024 if (VECTORP (rfont_def))
1026 #ifdef USE_FONT_BACKEND
1027 if (enable_font_backend
1028 && NILP (AREF (rfont_def, 0)))
1030 struct font *font = XSAVE_VALUE (AREF (rfont_def, 3))->pointer;
1032 face_id = face_for_font (f, font, face);
1033 ASET (rfont_def, 0, make_number (face_id));
1035 else
1036 #endif /* USE_FONT_BACKEND */
1037 if (NILP (AREF (rfont_def, 0)))
1039 /* We have not yet made a realized face that uses this font. */
1040 int font_idx = XINT (AREF (rfont_def, 1));
1042 face_id = lookup_non_ascii_face (f, font_idx, face);
1043 ASET (rfont_def, 0, make_number (face_id));
1045 return XINT (AREF (rfont_def, 0));
1048 if (NILP (FONTSET_NOFONT_FACE (fontset)))
1050 face_id = lookup_non_ascii_face (f, -1, face);
1051 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
1053 return XINT (FONTSET_NOFONT_FACE (fontset));
1057 /* Make a realized fontset for ASCII face FACE on frame F from the
1058 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1059 default fontset as the base. Value is the id of the new fontset.
1060 Called from realize_x_face. */
1063 make_fontset_for_ascii_face (f, base_fontset_id, face)
1064 FRAME_PTR f;
1065 int base_fontset_id;
1066 struct face *face;
1068 Lisp_Object base_fontset, fontset, frame;
1070 XSETFRAME (frame, f);
1071 if (base_fontset_id >= 0)
1073 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1074 if (!BASE_FONTSET_P (base_fontset))
1075 base_fontset = FONTSET_BASE (base_fontset);
1076 if (! BASE_FONTSET_P (base_fontset))
1077 abort ();
1079 else
1080 base_fontset = Vdefault_fontset;
1082 fontset = make_fontset (frame, Qnil, base_fontset);
1083 return XINT (FONTSET_ID (fontset));
1087 #if defined(WINDOWSNT) && defined (_MSC_VER)
1088 #pragma optimize("", off)
1089 #endif
1091 /* Load a font named FONTNAME on frame F. Return a pointer to the
1092 struct font_info of the loaded font. If loading fails, return
1093 NULL. CHARSET is an ID of charset to encode characters for this
1094 font. If it is -1, find one from Vfont_encoding_alist. */
1096 struct font_info *
1097 fs_load_font (f, fontname, charset)
1098 FRAME_PTR f;
1099 char *fontname;
1100 int charset;
1102 struct font_info *fontp;
1103 Lisp_Object fullname;
1105 if (!fontname)
1106 /* No way to get fontname. */
1107 return NULL;
1109 fontp = (*load_font_func) (f, fontname, 0);
1110 if (! fontp || fontp->charset >= 0)
1111 return fontp;
1113 fontname = fontp->full_name;
1114 fullname = build_string (fontp->full_name);
1116 if (charset < 0)
1118 Lisp_Object charset_symbol;
1120 charset_symbol = find_font_encoding (fullname);
1121 if (CONSP (charset_symbol))
1122 charset_symbol = XCAR (charset_symbol);
1123 if (NILP (charset_symbol))
1124 charset_symbol = Qascii;
1125 charset = XINT (CHARSET_SYMBOL_ID (charset_symbol));
1127 fontp->charset = charset;
1128 fontp->vertical_centering = 0;
1129 fontp->font_encoder = NULL;
1131 if (charset != charset_ascii)
1133 fontp->vertical_centering
1134 = (STRINGP (Vvertical_centering_font_regexp)
1135 && (fast_string_match_ignore_case
1136 (Vvertical_centering_font_regexp, fullname) >= 0));
1138 if (find_ccl_program_func)
1139 (*find_ccl_program_func) (fontp);
1142 return fontp;
1145 #if defined(WINDOWSNT) && defined (_MSC_VER)
1146 #pragma optimize("", on)
1147 #endif
1150 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
1151 FONTNAME. ENCODING is a charset symbol that specifies the encoding
1152 of the font. REPERTORY is a charset symbol or nil. */
1155 Lisp_Object
1156 find_font_encoding (fontname)
1157 Lisp_Object fontname;
1159 Lisp_Object tail, elt;
1161 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
1163 elt = XCAR (tail);
1164 if (CONSP (elt)
1165 && STRINGP (XCAR (elt))
1166 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
1167 && (SYMBOLP (XCDR (elt))
1168 ? CHARSETP (XCDR (elt))
1169 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
1170 return (XCDR (elt));
1172 /* We don't know the encoding of this font. Let's assume `ascii'. */
1173 return Qascii;
1177 /* Cache data used by fontset_pattern_regexp. The car part is a
1178 pattern string containing at least one wild card, the cdr part is
1179 the corresponding regular expression. */
1180 static Lisp_Object Vcached_fontset_data;
1182 #define CACHED_FONTSET_NAME (SDATA (XCAR (Vcached_fontset_data)))
1183 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1185 /* If fontset name PATTERN contains any wild card, return regular
1186 expression corresponding to PATTERN. */
1188 static Lisp_Object
1189 fontset_pattern_regexp (pattern)
1190 Lisp_Object pattern;
1192 if (!index (SDATA (pattern), '*')
1193 && !index (SDATA (pattern), '?'))
1194 /* PATTERN does not contain any wild cards. */
1195 return Qnil;
1197 if (!CONSP (Vcached_fontset_data)
1198 || strcmp (SDATA (pattern), CACHED_FONTSET_NAME))
1200 /* We must at first update the cached data. */
1201 unsigned char *regex, *p0, *p1;
1202 int ndashes = 0, nstars = 0;
1204 for (p0 = SDATA (pattern); *p0; p0++)
1206 if (*p0 == '-')
1207 ndashes++;
1208 else if (*p0 == '*')
1209 nstars++;
1212 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1213 we convert "*" to "[^-]*" which is much faster in regular
1214 expression matching. */
1215 if (ndashes < 14)
1216 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 1);
1217 else
1218 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 1);
1220 *p1++ = '^';
1221 for (p0 = SDATA (pattern); *p0; p0++)
1223 if (*p0 == '*')
1225 if (ndashes < 14)
1226 *p1++ = '.';
1227 else
1228 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1229 *p1++ = '*';
1231 else if (*p0 == '?')
1232 *p1++ = '.';
1233 else
1234 *p1++ = *p0;
1236 *p1++ = '$';
1237 *p1++ = 0;
1239 Vcached_fontset_data = Fcons (build_string (SDATA (pattern)),
1240 build_string (regex));
1243 return CACHED_FONTSET_REGEX;
1246 /* Return ID of the base fontset named NAME. If there's no such
1247 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1248 0: pattern containing '*' and '?' as wildcards
1249 1: regular expression
1250 2: literal fontset name
1254 fs_query_fontset (name, name_pattern)
1255 Lisp_Object name;
1256 int name_pattern;
1258 Lisp_Object tem;
1259 int i;
1261 name = Fdowncase (name);
1262 if (name_pattern != 1)
1264 tem = Frassoc (name, Vfontset_alias_alist);
1265 if (NILP (tem))
1266 tem = Fassoc (name, Vfontset_alias_alist);
1267 if (CONSP (tem) && STRINGP (XCAR (tem)))
1268 name = XCAR (tem);
1269 else if (name_pattern == 0)
1271 tem = fontset_pattern_regexp (name);
1272 if (STRINGP (tem))
1274 name = tem;
1275 name_pattern = 1;
1280 for (i = 0; i < ASIZE (Vfontset_table); i++)
1282 Lisp_Object fontset, this_name;
1284 fontset = FONTSET_FROM_ID (i);
1285 if (NILP (fontset)
1286 || !BASE_FONTSET_P (fontset))
1287 continue;
1289 this_name = FONTSET_NAME (fontset);
1290 if (name_pattern == 1
1291 ? fast_string_match (name, this_name) >= 0
1292 : !strcmp (SDATA (name), SDATA (this_name)))
1293 return i;
1295 return -1;
1299 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1300 doc: /* Return the name of a fontset that matches PATTERN.
1301 The value is nil if there is no matching fontset.
1302 PATTERN can contain `*' or `?' as a wildcard
1303 just as X font name matching algorithm allows.
1304 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1305 (pattern, regexpp)
1306 Lisp_Object pattern, regexpp;
1308 Lisp_Object fontset;
1309 int id;
1311 (*check_window_system_func) ();
1313 CHECK_STRING (pattern);
1315 if (SCHARS (pattern) == 0)
1316 return Qnil;
1318 id = fs_query_fontset (pattern, !NILP (regexpp));
1319 if (id < 0)
1320 return Qnil;
1322 fontset = FONTSET_FROM_ID (id);
1323 return FONTSET_NAME (fontset);
1326 /* Return a list of base fontset names matching PATTERN on frame F. */
1328 Lisp_Object
1329 list_fontsets (f, pattern, size)
1330 FRAME_PTR f;
1331 Lisp_Object pattern;
1332 int size;
1334 Lisp_Object frame, regexp, val;
1335 int id;
1337 XSETFRAME (frame, f);
1339 regexp = fontset_pattern_regexp (pattern);
1340 val = Qnil;
1342 for (id = 0; id < ASIZE (Vfontset_table); id++)
1344 Lisp_Object fontset, name;
1346 fontset = FONTSET_FROM_ID (id);
1347 if (NILP (fontset)
1348 || !BASE_FONTSET_P (fontset)
1349 || !EQ (frame, FONTSET_FRAME (fontset)))
1350 continue;
1351 name = FONTSET_NAME (fontset);
1353 if (STRINGP (regexp)
1354 ? (fast_string_match (regexp, name) < 0)
1355 : strcmp (SDATA (pattern), SDATA (name)))
1356 continue;
1358 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1361 return val;
1365 /* Free all realized fontsets whose base fontset is BASE. */
1367 static void
1368 free_realized_fontsets (base)
1369 Lisp_Object base;
1371 int id;
1373 #if 0
1374 /* For the moment, this doesn't work because free_realized_face
1375 doesn't remove FACE from a cache. Until we find a solution, we
1376 suppress this code, and simply use Fclear_face_cache even though
1377 that is not efficient. */
1378 BLOCK_INPUT;
1379 for (id = 0; id < ASIZE (Vfontset_table); id++)
1381 Lisp_Object this = AREF (Vfontset_table, id);
1383 if (EQ (FONTSET_BASE (this), base))
1385 Lisp_Object tail;
1387 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1388 tail = XCDR (tail))
1390 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1391 int face_id = XINT (XCDR (XCAR (tail)));
1392 struct face *face = FACE_FROM_ID (f, face_id);
1394 /* Face THIS itself is also freed by the following call. */
1395 free_realized_face (f, face);
1399 UNBLOCK_INPUT;
1400 #else /* not 0 */
1401 /* But, we don't have to call Fclear_face_cache if no fontset has
1402 been realized from BASE. */
1403 for (id = 0; id < ASIZE (Vfontset_table); id++)
1405 Lisp_Object this = AREF (Vfontset_table, id);
1407 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1409 Fclear_face_cache (Qt);
1410 break;
1413 #endif /* not 0 */
1417 /* Check validity of NAME as a fontset name and return the
1418 corresponding fontset. If not valid, signal an error.
1419 If NAME is t, return Vdefault_fontset. */
1421 static Lisp_Object
1422 check_fontset_name (name)
1423 Lisp_Object name;
1425 int id;
1427 if (EQ (name, Qt))
1428 return Vdefault_fontset;
1430 CHECK_STRING (name);
1431 /* First try NAME as literal. */
1432 id = fs_query_fontset (name, 2);
1433 if (id < 0)
1434 /* For backward compatibility, try again NAME as pattern. */
1435 id = fs_query_fontset (name, 0);
1436 if (id < 0)
1437 error ("Fontset `%s' does not exist", SDATA (name));
1438 return FONTSET_FROM_ID (id);
1441 static void
1442 accumulate_script_ranges (arg, range, val)
1443 Lisp_Object arg, range, val;
1445 if (EQ (XCAR (arg), val))
1447 if (CONSP (range))
1448 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1449 else
1450 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1455 /* Return an ASCII font name generated from fontset name NAME and
1456 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1458 static INLINE Lisp_Object
1459 generate_ascii_font_name (name, ascii_spec)
1460 Lisp_Object name, ascii_spec;
1462 Lisp_Object font_spec = Ffont_spec (0, NULL);
1463 Lisp_Object vec;
1464 int i;
1465 char xlfd[256];
1467 if (font_parse_xlfd (SDATA (name), font_spec) < 0)
1468 error ("Not an XLFD font name: %s", SDATA (name));
1469 for (i = FONT_FOUNDRY_INDEX; i <= FONT_WIDTH_INDEX; i++)
1470 if (! NILP (AREF (ascii_spec, i)))
1471 ASET (font_spec, i, AREF (ascii_spec, i));
1472 i = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1473 if (i < 0)
1474 error ("Not an XLFD font name: %s", SDATA (name));
1475 return make_unibyte_string (xlfd, i);
1478 /* Variables referred in set_fontset_font. They are set before
1479 map_charset_chars is called in Fset_fontset_font. */
1480 static Lisp_Object font_def_arg, add_arg;
1481 static int from_arg, to_arg;
1483 /* Callback function for map_charset_chars in Fset_fontset_font. In
1484 FONTSET, set font_def_arg in a fashion specified by add_arg for
1485 characters in RANGE while ignoring the range between from_arg and
1486 to_arg. */
1488 static void
1489 set_fontset_font (fontset, range)
1490 Lisp_Object fontset, range;
1492 if (from_arg < to_arg)
1494 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1496 if (from < from_arg)
1498 if (to > to_arg)
1500 Lisp_Object range2;
1502 range2 = Fcons (make_number (to_arg), XCDR (range));
1503 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1504 to = to_arg;
1506 if (to > from_arg)
1507 range = Fcons (XCAR (range), make_number (from_arg));
1509 else if (to <= to_arg)
1510 return;
1511 else
1513 if (from < to_arg)
1514 range = Fcons (make_number (to_arg), XCDR (range));
1517 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1520 extern Lisp_Object QCfamily, QCregistry;
1522 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1523 doc: /*
1524 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1526 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1527 In that case, use FONT-SPEC for all characters in the range FROM and
1528 TO (inclusive).
1530 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1531 all characters that belong to the script.
1533 TARGET may be a charset. In that case, use FONT-SPEC for all
1534 characters in the charset.
1536 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1537 that no FONT-SPEC is specified.
1539 FONT-SPEC may one of these:
1540 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1541 REGISTRY is a font registry name. FAMILY may contains foundry
1542 name, and REGISTRY may contains encoding name.
1543 * A font name string.
1544 * nil, which explicitly specifies that there's no font for TARGET.
1546 Optional 4th argument FRAME, if non-nil, is a frame. This argument is
1547 kept for backward compatibility and has no meaning.
1549 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1550 to the font specifications for TARGET previously set. If it is
1551 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1552 appended. By default, FONT-SPEC overrides the previous settings. */)
1553 (name, target, font_spec, frame, add)
1554 Lisp_Object name, target, font_spec, frame, add;
1556 Lisp_Object fontset;
1557 Lisp_Object font_def, registry, family;
1558 Lisp_Object encoding, repertory;
1559 Lisp_Object range_list;
1560 struct charset *charset = NULL;
1562 fontset = check_fontset_name (name);
1564 /* The arg FRAME is kept for backward compatibility. We only check
1565 the validity. */
1566 if (!NILP (frame))
1567 CHECK_LIVE_FRAME (frame);
1569 if (VECTORP (font_spec))
1571 if (! FONT_SPEC_P (font_spec))
1572 Fsignal (Qfont, list2 (build_string ("invalid font-spec"), font_spec));
1574 else if (CONSP (font_spec))
1576 Lisp_Object args[4];
1577 int i= 0;
1579 family = XCAR (font_spec);
1580 registry = XCDR (font_spec);
1582 if (! NILP (family))
1584 CHECK_STRING (family);
1585 args[i++] = QCfamily;
1586 args[i++] = family;
1588 CHECK_STRING (registry);
1589 args[i++] = QCregistry;
1590 args[i++] = registry;
1591 font_spec = Ffont_spec (i, args);
1593 else if (STRINGP (font_spec))
1595 Lisp_Object args[2];
1597 args[0] = QCname;
1598 args[1] = font_spec;
1599 font_spec = Ffont_spec (2, args);
1601 else if (! NILP (font_spec))
1602 wrong_type_argument (intern ("font-spec"), font_spec);
1604 if (! NILP (font_spec))
1606 family = AREF (font_spec, FONT_FAMILY_INDEX);
1607 if (! NILP (family) && SYMBOLP (family))
1608 family = SYMBOL_NAME (family);
1609 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1610 if (! NILP (registry) && SYMBOLP (registry))
1611 registry = SYMBOL_NAME (registry);
1613 encoding = find_font_encoding (concat2 (family, registry));
1614 if (NILP (encoding))
1615 encoding = Qascii;
1617 if (SYMBOLP (encoding))
1619 CHECK_CHARSET (encoding);
1620 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1622 else
1624 repertory = XCDR (encoding);
1625 encoding = XCAR (encoding);
1626 CHECK_CHARSET (encoding);
1627 encoding = CHARSET_SYMBOL_ID (encoding);
1628 if (! NILP (repertory) && SYMBOLP (repertory))
1630 CHECK_CHARSET (repertory);
1631 repertory = CHARSET_SYMBOL_ID (repertory);
1634 font_def = Fmake_vector (make_number (3), font_spec);
1635 ASET (font_def, 1, encoding);
1636 ASET (font_def, 2, repertory);
1638 else
1639 font_def = Qnil;
1641 if (CHARACTERP (target))
1642 range_list = Fcons (Fcons (target, target), Qnil);
1643 else if (CONSP (target))
1645 Lisp_Object from, to;
1647 from = Fcar (target);
1648 to = Fcdr (target);
1649 CHECK_CHARACTER (from);
1650 CHECK_CHARACTER (to);
1651 range_list = Fcons (target, Qnil);
1653 else if (SYMBOLP (target) && !NILP (target))
1655 Lisp_Object script_list;
1656 Lisp_Object val;
1658 range_list = Qnil;
1659 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1660 if (! NILP (Fmemq (target, script_list)))
1662 val = Fcons (target, Qnil);
1663 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1664 val);
1665 range_list = XCDR (val);
1666 if (EQ (target, Qlatin) && NILP (FONTSET_ASCII (fontset)))
1668 if (VECTORP (font_spec))
1669 val = generate_ascii_font_name (FONTSET_NAME (fontset),
1670 font_spec);
1671 else
1672 val = font_spec;
1673 FONTSET_ASCII (fontset) = val;
1676 if (CHARSETP (target))
1678 if (EQ (target, Qascii) && NILP (FONTSET_ASCII (fontset)))
1680 if (VECTORP (font_spec))
1681 font_spec = generate_ascii_font_name (FONTSET_NAME (fontset),
1682 font_spec);
1683 FONTSET_ASCII (fontset) = font_spec;
1684 range_list = Fcons (Fcons (make_number (0), make_number (127)),
1685 Qnil);
1687 else
1689 CHECK_CHARSET_GET_CHARSET (target, charset);
1692 else if (NILP (range_list))
1693 error ("Invalid script or charset name: %s",
1694 SDATA (SYMBOL_NAME (target)));
1696 else if (NILP (target))
1697 range_list = Fcons (Qnil, Qnil);
1698 else
1699 error ("Invalid target for setting a font");
1702 if (charset)
1704 font_def_arg = font_def;
1705 add_arg = add;
1706 if (NILP (range_list))
1707 from_arg = to_arg = 0;
1708 else
1709 from_arg = XINT (XCAR (XCAR (range_list))),
1710 to_arg = XINT (XCDR (XCAR (range_list)));
1712 map_charset_chars (set_fontset_font, Qnil, fontset, charset,
1713 CHARSET_MIN_CODE (charset),
1714 CHARSET_MAX_CODE (charset));
1716 for (; CONSP (range_list); range_list = XCDR (range_list))
1717 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1719 /* Free all realized fontsets whose base is FONTSET. This way, the
1720 specified character(s) are surely redisplayed by a correct
1721 font. */
1722 free_realized_fontsets (fontset);
1724 return Qnil;
1728 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1729 doc: /* Create a new fontset NAME from font information in FONTLIST.
1731 FONTLIST is an alist of scripts vs the corresponding font specification list.
1732 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1733 character of SCRIPT is displayed by a font that matches one of
1734 FONT-SPEC.
1736 SCRIPT is a symbol that appears in the first extra slot of the
1737 char-table `char-script-table'.
1739 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1740 `set-fontset-font' for the meaning. */)
1741 (name, fontlist)
1742 Lisp_Object name, fontlist;
1744 Lisp_Object fontset;
1745 Lisp_Object val;
1746 int id;
1748 CHECK_STRING (name);
1749 CHECK_LIST (fontlist);
1751 id = fs_query_fontset (name, 0);
1752 if (id < 0)
1754 name = Fdowncase (name);
1755 val = split_font_name_into_vector (name);
1756 if (NILP (val) || NILP (AREF (val, 12)) || NILP (AREF (val, 13)))
1757 error ("Fontset name must be in XLFD format");
1758 if (strcmp (SDATA (AREF (val, 12)), "fontset"))
1759 error ("Registry field of fontset name must be \"fontset\"");
1760 Vfontset_alias_alist
1761 = Fcons (Fcons (name,
1762 concat2 (concat2 (AREF (val, 12), build_string ("-")),
1763 AREF (val, 13))),
1764 Vfontset_alias_alist);
1765 ASET (val, 12, build_string ("iso8859-1"));
1766 fontset = make_fontset (Qnil, name, Qnil);
1767 FONTSET_ASCII (fontset) = build_font_name_from_vector (val);
1769 else
1771 fontset = FONTSET_FROM_ID (id);;
1772 free_realized_fontsets (fontset);
1773 Fset_char_table_range (fontset, Qt, Qnil);
1776 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1778 Lisp_Object elt, script;
1780 elt = Fcar (fontlist);
1781 script = Fcar (elt);
1782 elt = Fcdr (elt);
1783 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1784 for (; CONSP (elt); elt = XCDR (elt))
1785 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1786 else
1787 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1789 return name;
1793 /* Alist of automatically created fontsets. Each element is a cons
1794 (FONTNAME . FONTSET-ID). */
1795 static Lisp_Object auto_fontset_alist;
1798 new_fontset_from_font_name (Lisp_Object fontname)
1800 Lisp_Object val;
1801 Lisp_Object name;
1802 Lisp_Object vec;
1803 int id;
1805 fontname = Fdowncase (fontname);
1806 val = Fassoc (fontname, auto_fontset_alist);
1807 if (CONSP (val))
1808 return XINT (XCDR (val));
1810 vec = split_font_name_into_vector (fontname);
1811 if ( NILP (vec))
1812 vec = Fmake_vector (make_number (14), build_string (""));
1813 ASET (vec, 12, build_string ("fontset"));
1814 if (NILP (auto_fontset_alist))
1816 ASET (vec, 13, build_string ("startup"));
1817 name = build_font_name_from_vector (vec);
1819 else
1821 char temp[20];
1822 int len = XINT (Flength (auto_fontset_alist));
1824 sprintf (temp, "auto%d", len);
1825 ASET (vec, 13, build_string (temp));
1826 name = build_font_name_from_vector (vec);
1828 name = Fnew_fontset (name, list2 (list2 (Qascii, fontname),
1829 list2 (Fcons (make_number (0),
1830 make_number (MAX_CHAR)),
1831 fontname)));
1832 id = fs_query_fontset (name, 0);
1833 auto_fontset_alist
1834 = Fcons (Fcons (fontname, make_number (id)), auto_fontset_alist);
1835 return id;
1838 #ifdef USE_FONT_BACKEND
1840 new_fontset_from_font (font_object)
1841 Lisp_Object font_object;
1843 Lisp_Object font_name = font_get_name (font_object);
1844 Lisp_Object font_spec = font_get_spec (font_object);
1845 Lisp_Object fontset_spec, short_name, name, fontset;
1847 if (NILP (auto_fontset_alist))
1848 short_name = build_string ("fontset-startup");
1849 else
1851 char temp[32];
1852 int len = XINT (Flength (auto_fontset_alist));
1854 sprintf (temp, "fontset-auto%d", len);
1855 short_name = build_string (temp);
1857 fontset_spec = Fcopy_sequence (font_spec);
1858 ASET (fontset_spec, FONT_REGISTRY_INDEX, short_name);
1859 name = Ffont_xlfd_name (fontset_spec);
1860 if (NILP (name))
1862 int i;
1864 for (i = 0; i < FONT_SIZE_INDEX; i++)
1865 if ((i != FONT_FAMILY_INDEX) && (i != FONT_REGISTRY_INDEX))
1866 ASET (fontset_spec, i, Qnil);
1867 name = Ffont_xlfd_name (fontset_spec);
1868 if (NILP (name))
1869 abort ();
1871 fontset = make_fontset (Qnil, name, Qnil);
1872 FONTSET_ASCII (fontset) = font_name;
1873 font_spec = Fcons (SYMBOL_NAME (AREF (font_spec, FONT_FAMILY_INDEX)),
1874 SYMBOL_NAME (AREF (font_spec, FONT_REGISTRY_INDEX)));
1875 Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qnil);
1876 XSETCDR (font_spec, build_string ("iso10646-1"));
1877 Fset_fontset_font (name, Qlatin, font_spec, Qnil, Qappend);
1878 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1879 return XINT (FONTSET_ID (fontset));
1882 struct font *
1883 fontset_ascii_font (f, id)
1884 FRAME_PTR f;
1885 int id;
1887 Lisp_Object fontset = FONTSET_FROM_ID (id);
1888 Lisp_Object ascii_slot = FONTSET_ASCII (fontset);
1889 Lisp_Object val, font_object;
1891 if (CONSP (ascii_slot))
1893 Lisp_Object ascii_font_name = XCAR (ascii_slot);
1895 font_object = Qnil;
1896 for (val = XCDR (ascii_slot); ! NILP (val); val = XCDR (val))
1898 Lisp_Object frame = font_get_frame (XCAR (val));
1900 if (NILP (frame) || XFRAME (frame) == f)
1902 font_object = XCAR (val);
1903 if (XSAVE_VALUE (font_object)->integer == 0)
1905 font_object = font_open_by_name (f, SDATA (ascii_font_name));
1906 XSETCAR (val, font_object);
1908 break;
1911 if (NILP (font_object))
1913 font_object = font_open_by_name (f, SDATA (ascii_font_name));
1914 XSETCDR (ascii_slot, Fcons (font_object, XCDR (ascii_slot)));
1917 else
1919 font_object = font_open_by_name (f, SDATA (ascii_slot));
1920 FONTSET_ASCII (fontset) = Fcons (ascii_slot, Fcons (font_object, Qnil));
1922 if (NILP (font_object))
1923 return NULL;
1924 return XSAVE_VALUE (font_object)->pointer;
1927 #endif /* USE_FONT_BACKEND */
1929 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
1930 doc: /* Return information about a font named NAME on frame FRAME.
1931 If FRAME is omitted or nil, use the selected frame.
1932 The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
1933 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
1934 where
1935 OPENED-NAME is the name used for opening the font,
1936 FULL-NAME is the full name of the font,
1937 SIZE is the maximum bound width of the font,
1938 HEIGHT is the height of the font,
1939 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
1940 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
1941 how to compose characters.
1942 If the named font is not yet loaded, return nil. */)
1943 (name, frame)
1944 Lisp_Object name, frame;
1946 FRAME_PTR f;
1947 struct font_info *fontp;
1948 Lisp_Object info;
1949 Lisp_Object font_object;
1951 (*check_window_system_func) ();
1953 CHECK_STRING (name);
1954 name = Fdowncase (name);
1955 if (NILP (frame))
1956 frame = selected_frame;
1957 CHECK_LIVE_FRAME (frame);
1958 f = XFRAME (frame);
1960 if (!query_font_func)
1961 error ("Font query function is not supported");
1963 #ifdef USE_FONT_BACKEND
1964 if (enable_font_backend)
1966 font_object = font_open_by_name (f, SDATA (name));
1967 if (NILP (font_object))
1968 fontp = NULL;
1969 else
1970 fontp = (struct font_info *) XSAVE_VALUE (font_object)->pointer;
1972 else
1973 #endif /* USE_FONT_BACKEND */
1974 fontp = (*query_font_func) (f, SDATA (name));
1975 if (!fontp)
1976 return Qnil;
1978 info = Fmake_vector (make_number (7), Qnil);
1980 XVECTOR (info)->contents[0] = build_string (fontp->name);
1981 XVECTOR (info)->contents[1] = build_string (fontp->full_name);
1982 XVECTOR (info)->contents[2] = make_number (fontp->size);
1983 XVECTOR (info)->contents[3] = make_number (fontp->height);
1984 XVECTOR (info)->contents[4] = make_number (fontp->baseline_offset);
1985 XVECTOR (info)->contents[5] = make_number (fontp->relative_compose);
1986 XVECTOR (info)->contents[6] = make_number (fontp->default_ascent);
1988 #ifdef USE_FONT_BACKEND
1989 if (enable_font_backend && ! NILP (font_object))
1990 font_close_object (f, font_object);
1991 #endif /* USE_FONT_BACKEND */
1992 return info;
1996 /* Return a cons (FONT-NAME . GLYPH-CODE).
1997 FONT-NAME is the font name for the character at POSITION in the current
1998 buffer. This is computed from all the text properties and overlays
1999 that apply to POSITION. POSTION may be nil, in which case,
2000 FONT-NAME is the font name for display the character CH with the
2001 default face.
2003 GLYPH-CODE is the glyph code in the font to use for the character.
2005 If the 2nd optional arg CH is non-nil, it is a character to check
2006 the font instead of the character at POSITION.
2008 It returns nil in the following cases:
2010 (1) The window system doesn't have a font for the character (thus
2011 it is displayed by an empty box).
2013 (2) The character code is invalid.
2015 (3) If POSITION is not nil, and the current buffer is not displayed
2016 in any window.
2018 In addition, the returned font name may not take into account of
2019 such redisplay engine hooks as what used in jit-lock-mode if
2020 POSITION is currently not visible. */
2023 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
2024 doc: /* For internal use only. */)
2025 (position, ch)
2026 Lisp_Object position, ch;
2028 EMACS_INT pos, pos_byte, dummy;
2029 int face_id;
2030 int c;
2031 struct frame *f;
2032 struct face *face;
2033 Lisp_Object charset, rfont_def;
2034 int cs_id;
2036 if (NILP (position))
2038 CHECK_CHARACTER (ch);
2039 c = XINT (ch);
2040 f = XFRAME (selected_frame);
2041 face_id = DEFAULT_FACE_ID;
2042 pos = -1;
2043 cs_id = -1;
2045 else
2047 Lisp_Object window, charset;
2048 struct window *w;
2050 CHECK_NUMBER_COERCE_MARKER (position);
2051 pos = XINT (position);
2052 if (pos < BEGV || pos >= ZV)
2053 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
2054 pos_byte = CHAR_TO_BYTE (pos);
2055 if (NILP (ch))
2056 c = FETCH_CHAR (pos_byte);
2057 else
2059 CHECK_NATNUM (ch);
2060 c = XINT (ch);
2062 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
2063 if (NILP (window))
2064 return Qnil;
2065 w = XWINDOW (window);
2066 f = XFRAME (w->frame);
2067 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
2068 charset = Fget_char_property (position, Qcharset, Qnil);
2069 if (CHARSETP (charset))
2070 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
2071 else
2072 cs_id = -1;
2074 if (! CHAR_VALID_P (c, 0))
2075 return Qnil;
2076 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
2077 face = FACE_FROM_ID (f, face_id);
2078 rfont_def = fontset_font (FONTSET_FROM_ID (face->fontset), c, face, cs_id);
2079 #ifdef USE_FONT_BACKEND
2080 if (enable_font_backend)
2082 if (VECTORP (rfont_def) && ! NILP (AREF (rfont_def, 3)))
2084 Lisp_Object font_object = AREF (rfont_def, 3);
2085 struct font *font = XSAVE_VALUE (font_object)->pointer;
2086 unsigned code = font->driver->encode_char (font, c);
2087 Lisp_Object fontname = font_get_name (font_object);
2088 /* Assignment to EMACS_INT stops GCC whining about limited range
2089 of data type. */
2090 EMACS_INT cod = code;
2092 if (code == FONT_INVALID_CODE)
2093 return Qnil;
2094 if (cod <= MOST_POSITIVE_FIXNUM)
2095 return Fcons (fontname, make_number (code));
2096 return Fcons (fontname, Fcons (make_number (code >> 16),
2097 make_number (code & 0xFFFF)));
2099 return Qnil;
2101 #endif /* USE_FONT_BACKEND */
2102 if (VECTORP (rfont_def) && STRINGP (AREF (rfont_def, 3)))
2104 Lisp_Object font_def;
2105 struct font_info *fontp;
2106 struct charset *charset;
2107 XChar2b char2b;
2108 int code;
2110 font_def = AREF (rfont_def, 2);
2111 charset = CHARSET_FROM_ID (XINT (AREF (font_def, 1)));
2112 code = ENCODE_CHAR (charset, c);
2113 if (code == CHARSET_INVALID_CODE (charset))
2114 return (Fcons (AREF (rfont_def, 3), Qnil));
2115 STORE_XCHAR2B (&char2b, ((code >> 8) & 0xFF), (code & 0xFF));
2116 fontp = (*get_font_info_func) (f, XINT (AREF (rfont_def, 1)));
2117 FRAME_RIF (f)->encode_char (c, &char2b, fontp, charset, NULL);
2118 code = (XCHAR2B_BYTE1 (&char2b) << 8) | XCHAR2B_BYTE2 (&char2b);
2119 return (Fcons (AREF (rfont_def, 3), make_number (code)));
2121 return Qnil;
2125 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
2126 doc: /* Return information about a fontset FONTSET on frame FRAME.
2127 The value is a char-table of which elements has this form.
2129 ((FONT-PATTERN OPENED-FONT ...) ...)
2131 FONT-PATTERN is a vector:
2133 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
2135 or a string of font name pattern.
2137 OPENED-FONT is a name of a font actually opened.
2139 The char-table has one extra slot. The value is a char-table
2140 containing the information about the derived fonts from the default
2141 fontset. The format is the same as abobe. */)
2142 (fontset, frame)
2143 Lisp_Object fontset, frame;
2145 FRAME_PTR f;
2146 Lisp_Object *realized[2], fontsets[2], tables[2];
2147 Lisp_Object val, elt;
2148 int c, i, j, k;
2150 (*check_window_system_func) ();
2152 fontset = check_fontset_name (fontset);
2154 if (NILP (frame))
2155 frame = selected_frame;
2156 CHECK_LIVE_FRAME (frame);
2157 f = XFRAME (frame);
2159 /* Recode fontsets realized on FRAME from the base fontset FONTSET
2160 in the table `realized'. */
2161 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
2162 * ASIZE (Vfontset_table));
2163 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
2165 elt = FONTSET_FROM_ID (i);
2166 if (!NILP (elt)
2167 && EQ (FONTSET_BASE (elt), fontset)
2168 && EQ (FONTSET_FRAME (elt), frame))
2169 realized[0][j++] = elt;
2171 realized[0][j] = Qnil;
2173 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
2174 * ASIZE (Vfontset_table));
2175 for (i = j = 0; ! NILP (realized[0][i]); i++)
2177 elt = FONTSET_DEFAULT (realized[0][i]);
2178 if (! NILP (elt))
2179 realized[1][j++] = elt;
2181 realized[1][j] = Qnil;
2183 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
2184 tables[1] = Fmake_char_table (Qnil, Qnil);
2185 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
2186 fontsets[0] = fontset;
2187 fontsets[1] = Vdefault_fontset;
2189 /* Accumulate information of the fontset in TABLE. The format of
2190 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
2191 for (k = 0; k <= 1; k++)
2193 for (c = 0; c <= MAX_CHAR; )
2195 int from, to;
2197 if (c <= MAX_5_BYTE_CHAR)
2199 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
2200 if (to > MAX_5_BYTE_CHAR)
2201 to = MAX_5_BYTE_CHAR;
2203 else
2205 val = FONTSET_FALLBACK (fontsets[k]);
2206 to = MAX_CHAR;
2208 if (VECTORP (val))
2210 Lisp_Object alist;
2212 /* At first, set ALIST to ((FONT-SPEC) ...). */
2213 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
2215 if (NILP (AREF (val, i)))
2216 alist = Fcons (Qnil, alist);
2217 else
2218 alist = Fcons (Fcons (AREF (AREF (val, i), 0), Qnil), alist);
2220 alist = Fnreverse (alist);
2222 /* Then store opend font names to cdr of each elements. */
2223 for (i = 0; ! NILP (realized[k][i]); i++)
2225 if (c <= MAX_5_BYTE_CHAR)
2226 val = FONTSET_REF (realized[k][i], c);
2227 else
2228 val = FONTSET_FALLBACK (realized[k][i]);
2229 if (! VECTORP (val))
2230 continue;
2231 #ifdef USE_FONT_BACKEND
2232 /* VAL: [int int ?
2233 [FACE-ID FONT-INDEX FONT-DEF FONT-ENTITY/OBJECT]
2234 ...] */
2235 if (enable_font_backend)
2236 for (j = 3; j < ASIZE (val); j++)
2238 elt = AREF (val, j);
2239 if (INTEGERP (AREF (elt, 1))
2240 && XINT (AREF (elt, 1)) >= 0)
2242 Lisp_Object font_object = AREF (elt, 3);
2244 if (FONT_OBJECT_P (font_object))
2246 struct font *font
2247 = XSAVE_VALUE (font_object)->pointer;
2248 char *name = font->font.full_name;
2249 int len = strlen (name);
2250 Lisp_Object slot;
2252 slot = Fassq (AREF (AREF (elt, 2), 0), alist);
2253 nconc2 (slot,
2254 Fcons (make_unibyte_string (name, len),
2255 Qnil));
2259 else
2260 #endif /* not USE_FONT_BACKEND */
2262 /* VAL is [int int ?
2263 [FACE-ID FONT-INDEX FONT-DEF FONT-NAME] ...].
2264 If a font of an element is already opened,
2265 FONT-NAME is the name of a opened font. */
2266 for (j = 3; j < ASIZE (val); j++)
2267 if (STRINGP (AREF (AREF (val, j), 3)))
2269 Lisp_Object font_idx;
2271 font_idx = AREF (AREF (val, j), 1);
2272 elt = Fassq (AREF (AREF (AREF (val, j), 2), 0),
2273 alist);
2274 if (CONSP (elt)
2275 && NILP (Fmemq (font_idx, XCDR(elt))))
2276 nconc2 (elt, Fcons (font_idx, Qnil));
2278 for (val = alist; CONSP (val); val = XCDR (val))
2279 for (elt = XCDR (XCAR (val)); CONSP (elt);
2280 elt = XCDR (elt))
2282 struct font_info *font_info
2283 = (*get_font_info_func) (f, XINT (XCAR (elt)));
2284 XSETCAR (elt, build_string (font_info->full_name));
2289 /* Store ALIST in TBL for characters C..TO. */
2290 if (c <= MAX_5_BYTE_CHAR)
2291 char_table_set_range (tables[k], c, to, alist);
2292 else
2293 XCHAR_TABLE (tables[k])->defalt = alist;
2295 c = to + 1;
2299 return tables[0];
2303 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2304 doc: /* Return a font name pattern for character CH in fontset NAME.
2305 If NAME is t, find a pattern in the default fontset.
2307 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2308 family name and REGISTRY is a font registry name. This is actually
2309 the first font name pattern for CH in the fontset or in the default
2310 fontset.
2312 If the 2nd optional arg ALL is non-nil, return a list of all font name
2313 patterns. */)
2314 (name, ch, all)
2315 Lisp_Object name, ch, all;
2317 int c;
2318 Lisp_Object fontset, elt, list, repertory, val;
2319 int i, j;
2321 fontset = check_fontset_name (name);
2323 CHECK_CHARACTER (ch);
2324 c = XINT (ch);
2325 list = Qnil;
2326 while (1)
2328 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2329 i++, elt = FONTSET_FALLBACK (fontset))
2330 if (VECTORP (elt))
2331 for (j = 0; j < ASIZE (elt); j++)
2333 val = AREF (elt, j);
2334 repertory = AREF (val, 1);
2335 if (INTEGERP (repertory))
2337 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2339 if (! CHAR_CHARSET_P (c, charset))
2340 continue;
2342 else if (CHAR_TABLE_P (repertory))
2344 if (NILP (CHAR_TABLE_REF (repertory, c)))
2345 continue;
2347 val = AREF (val, 0);
2348 val = Fcons (AREF (val, 0), AREF (val, 5));
2349 if (NILP (all))
2350 return val;
2351 list = Fcons (val, list);
2353 if (EQ (fontset, Vdefault_fontset))
2354 break;
2355 fontset = Vdefault_fontset;
2357 return (Fnreverse (list));
2360 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2361 doc: /* Return a list of all defined fontset names. */)
2364 Lisp_Object fontset, list;
2365 int i;
2367 list = Qnil;
2368 for (i = 0; i < ASIZE (Vfontset_table); i++)
2370 fontset = FONTSET_FROM_ID (i);
2371 if (!NILP (fontset)
2372 && BASE_FONTSET_P (fontset))
2373 list = Fcons (FONTSET_NAME (fontset), list);
2376 return list;
2380 #ifdef FONTSET_DEBUG
2382 Lisp_Object
2383 dump_fontset (fontset)
2384 Lisp_Object fontset;
2386 Lisp_Object vec;
2388 vec = Fmake_vector (make_number (3), Qnil);
2389 ASET (vec, 0, FONTSET_ID (fontset));
2391 if (BASE_FONTSET_P (fontset))
2393 ASET (vec, 1, FONTSET_NAME (fontset));
2395 else
2397 Lisp_Object frame;
2399 frame = FONTSET_FRAME (fontset);
2400 if (FRAMEP (frame))
2402 FRAME_PTR f = XFRAME (frame);
2404 if (FRAME_LIVE_P (f))
2405 ASET (vec, 1,
2406 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2407 else
2408 ASET (vec, 1,
2409 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2411 if (!NILP (FONTSET_DEFAULT (fontset)))
2412 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2414 return vec;
2417 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2418 doc: /* Return a brief summary of all fontsets for debug use. */)
2421 Lisp_Object val;
2422 int i;
2424 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2425 if (! NILP (AREF (Vfontset_table, i)))
2426 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2427 return (Fnreverse (val));
2429 #endif /* FONTSET_DEBUG */
2431 void
2432 syms_of_fontset ()
2434 if (!load_font_func)
2435 /* Window system initializer should have set proper functions. */
2436 abort ();
2438 DEFSYM (Qfontset, "fontset");
2439 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2440 DEFSYM (Qfontset_info, "fontset-info");
2441 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2443 DEFSYM (Qprepend, "prepend");
2444 DEFSYM (Qappend, "append");
2445 DEFSYM (Qlatin, "latin");
2447 Vcached_fontset_data = Qnil;
2448 staticpro (&Vcached_fontset_data);
2450 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2451 staticpro (&Vfontset_table);
2453 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2454 staticpro (&Vdefault_fontset);
2455 FONTSET_ID (Vdefault_fontset) = make_number (0);
2456 FONTSET_NAME (Vdefault_fontset)
2457 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2458 ASET (Vfontset_table, 0, Vdefault_fontset);
2459 next_fontset_id = 1;
2461 auto_fontset_alist = Qnil;
2462 staticpro (&auto_fontset_alist);
2464 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
2465 doc: /*
2466 Alist of fontname patterns vs the corresponding encoding and repertory info.
2467 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
2468 where ENCODING is a charset or a char-table,
2469 and REPERTORY is a charset, a char-table, or nil.
2471 If ENCDING and REPERTORY are the same, the element can have the form
2472 \(REGEXP . ENCODING).
2474 ENCODING is for converting a character to a glyph code of the font.
2475 If ENCODING is a charset, encoding a character by the charset gives
2476 the corresponding glyph code. If ENCODING is a char-table, looking up
2477 the table by a character gives the corresponding glyph code.
2479 REPERTORY specifies a repertory of characters supported by the font.
2480 If REPERTORY is a charset, all characters beloging to the charset are
2481 supported. If REPERTORY is a char-table, all characters who have a
2482 non-nil value in the table are supported. It REPERTORY is nil, Emacs
2483 gets the repertory information by an opened font and ENCODING. */);
2484 Vfont_encoding_alist = Qnil;
2486 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2487 doc: /*
2488 Alist of charsets vs the charsets to determine the preferred font encoding.
2489 Each element looks like (CHARSET . ENCDOING-CHARSET),
2490 where ENCODING-CHARSET is a charset registered in the variable
2491 `font-encoding-alist' as ENCODING.
2493 When a text has a property `charset' and the value is CHARSET, a font
2494 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2495 Vfont_encoding_charset_alist = Qnil;
2497 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
2498 doc: /*
2499 Char table of characters whose ascent values should be ignored.
2500 If an entry for a character is non-nil, the ascent value of the glyph
2501 is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.
2503 This affects how a composite character which contains
2504 such a character is displayed on screen. */);
2505 Vuse_default_ascent = Qnil;
2507 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
2508 doc: /*
2509 Char table of characters which is not composed relatively.
2510 If an entry for a character is non-nil, a composition sequence
2511 which contains that character is displayed so that
2512 the glyph of that character is put without considering
2513 an ascent and descent value of a previous character. */);
2514 Vignore_relative_composition = Qnil;
2516 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
2517 doc: /* Alist of fontname vs list of the alternate fontnames.
2518 When a specified font name is not found, the corresponding
2519 alternate fontnames (if any) are tried instead. */);
2520 Valternate_fontname_alist = Qnil;
2522 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
2523 doc: /* Alist of fontset names vs the aliases. */);
2524 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2525 build_string ("fontset-default")),
2526 Qnil);
2528 DEFVAR_LISP ("vertical-centering-font-regexp",
2529 &Vvertical_centering_font_regexp,
2530 doc: /* *Regexp matching font names that require vertical centering on display.
2531 When a character is displayed with such fonts, the character is displayed
2532 at the vertical center of lines. */);
2533 Vvertical_centering_font_regexp = Qnil;
2535 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2536 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2537 Votf_script_alist = Qnil;
2539 defsubr (&Squery_fontset);
2540 defsubr (&Snew_fontset);
2541 defsubr (&Sset_fontset_font);
2542 defsubr (&Sfont_info);
2543 defsubr (&Sinternal_char_font);
2544 defsubr (&Sfontset_info);
2545 defsubr (&Sfontset_font);
2546 defsubr (&Sfontset_list);
2547 #ifdef FONTSET_DEBUG
2548 defsubr (&Sfontset_list_all);
2549 #endif
2552 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2553 (do not change this comment) */