Update copyright years and GPL version.
[emacs.git] / src / fontset.c
bloba6d06c5d0404c03ced5bef0040a677492a4ca524
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_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]
310 /* For both base and realized fontset. */
311 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
313 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
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. */
321 #define FONTSET_REF(fontset, c) \
322 (EQ (fontset, Vdefault_fontset) \
323 ? CHAR_TABLE_REF (fontset, c) \
324 : fontset_ref ((fontset), (c)))
326 static Lisp_Object
327 fontset_ref (fontset, c)
328 Lisp_Object fontset;
329 int c;
331 Lisp_Object elt;
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;
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. */
345 #define FONTSET_SET(fontset, range, elt) \
346 Fset_char_table_range ((fontset), (range), (elt))
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. */
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)))
363 static Lisp_Object
364 fontset_add (fontset, range, elt, add)
365 Lisp_Object fontset, range, elt, add;
367 Lisp_Object args[2];
368 int idx = (EQ (add, Qappend) ? 0 : 1);
370 args[1 - idx] = Fmake_vector (make_number (1), elt);
372 if (CONSP (range))
374 int from = XINT (XCAR (range));
375 int to = XINT (XCDR (range));
376 int from1, to1;
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);
388 else
390 args[idx] = FONTSET_FALLBACK (fontset);
391 FONTSET_FALLBACK (fontset)
392 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
394 return Qnil;
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. */
405 static void
406 reorder_font_vector (font_group, charset_id, family)
407 Lisp_Object font_group;
408 int charset_id;
409 Lisp_Object family;
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;
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);
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++)
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);
436 charset_id_table[i] = -1;
437 if (! NILP (this_family)
438 && (fast_string_match_ignore_case (family, SYMBOL_NAME (this_family))
439 >= 0))
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);
447 else if (id == charset_id)
449 new_vec[idx++] = rfont_def;
450 ASET (font_group, i + 3, Qnil);
452 else if (! charset->supplementary_p)
453 charset_id_table[i] = id;
456 if (idx == 0
457 && (XINT (AREF (font_group, 0)) == charset_ordered_list_tick))
458 /* No need of reordering. */
459 return;
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);
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))
469 int id = XINT (XCAR (list));
470 struct charset *charset = CHARSET_FROM_ID (id);
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)))
478 new_vec[idx++] = AREF (font_group, i + 3);
479 ASET (font_group, i + 3, Qnil);
482 for (i = 0; i < size; i++)
483 if (! NILP (AREF (font_group, i + 3)))
484 new_vec[idx++] = AREF (font_group, i + 3);
486 /* At last, update elements of FONT-GROUP. */
487 for (i = 0; i < size; i++)
488 ASET (font_group, i + 3, new_vec[i]);
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. */
499 extern Lisp_Object QCname;
501 static int
502 load_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;
508 char *font_name;
509 struct font_info *font_info;
510 int charset;
511 Lisp_Object font_spec, name;
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;
523 if (NILP (AREF (font_def, 2))
524 && NILP (Fassq (make_number (font_info->font_idx),
525 FONTSET_REPERTORY (fontset))))
527 /* We must look into the font to get the correct repertory as a
528 char-table. */
529 Lisp_Object repertory;
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));
537 return font_info->font_idx;
540 static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
541 int, int));
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.
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).
551 ID is a charset-id that must be preferred, or -1 meaning no
552 preference.
554 If FALLBACK is nonzero, search only fallback fonts. */
556 static Lisp_Object
557 fontset_find_font (fontset, c, face, id, fallback)
558 Lisp_Object fontset;
559 int c;
560 struct face *face;
561 int id, fallback;
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));
568 base_fontset = FONTSET_BASE (fontset);
569 if (! fallback)
570 vec = CHAR_TABLE_REF (fontset, c);
571 else
572 vec = FONTSET_FALLBACK (fontset);
574 if (NILP (vec))
576 Lisp_Object range;
578 /* We have not yet decided a font for C. */
579 if (! face)
580 return Qnil;
581 if (! fallback)
583 elt = char_table_ref_and_range (base_fontset, c, &from, &to);
584 range = Fcons (make_number (from), make_number (to));
586 else
588 elt = FONTSET_FALLBACK (base_fontset);
590 if (NILP (elt))
592 /* This fontset doesn't specify any font for C. */
593 vec = make_number (0);
595 else if (ASIZE (elt) == 1 && NILP (AREF (elt, 0)))
597 /* Explicitly specified no font. */
598 vec = Qt;
600 else
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;
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)))
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++;
621 /* Then store it in the fontset. */
622 if (! fallback)
623 FONTSET_SET (fontset, range, vec);
624 else
625 FONTSET_FALLBACK (fontset) = vec;
628 if (! VECTORP (vec))
629 return (EQ (vec, Qt) ? Qt : Qnil);
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]);
641 /* Find the first available font in the vector of RFONT-DEF. */
642 for (i = 3; i < ASIZE (vec); i++)
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;
653 if (!face && NILP (AREF (elt, 1)))
654 /* We have not yet opened the font. */
655 return Qnil;
657 font_def = AREF (elt, 2);
658 /* FONT_DEF == [ FONT-SPEC ENCODING REPERTORY ] */
660 #ifdef USE_FONT_BACKEND
661 if (enable_font_backend)
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;
668 if (NILP (font_entity))
670 font_entity = font_find_for_lface (f, face->lface,
671 AREF (font_def, 0), -1);
672 if (NILP (font_entity))
674 ASET (elt, 1, make_number (-1));
675 continue;
677 ASET (elt, 3, font_entity);
679 else if (FONT_ENTITY_P (font_entity))
681 if (FONT_ENTITY_NOT_LOADABLE (font_entity))
682 continue;
684 has_char = font_has_char (f, font_entity, c);
685 if (! has_char)
686 continue;
687 if (! FONT_OBJECT_P (font_entity))
689 Lisp_Object font_object
690 = font_open_for_lface (f, font_entity, face->lface, Qnil);
692 if (NILP (font_object))
694 FONT_ENTITY_SET_NOT_LOADABLE (font_entity);
695 continue;
697 ASET (elt, 3, font_object);
698 if (has_char < 0)
700 has_char = font_has_char (f, font_object, c);
701 if (! has_char)
702 continue;
705 /* Decide to use this font. */
706 ASET (elt, 1, make_number (0));
708 else
709 #endif /* USE_FONT_BACKEND */
711 if (INTEGERP (AREF (font_def, 2)))
713 /* The repertory is specified by charset ID. */
714 struct charset *charset
715 = CHARSET_FROM_ID (XINT (AREF (font_def, 2)));
717 if (! CHAR_CHARSET_P (c, charset))
718 /* This font can't display C. */
719 continue;
721 else if (CHAR_TABLE_P (AREF (font_def, 2)))
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;
728 else
730 Lisp_Object slot;
732 if (! INTEGERP (AREF (elt, 1)))
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;
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));
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;
756 /* Now we have decided to use this font spec to display C. */
757 if (! INTEGERP (AREF (elt, 1)))
759 /* But not yet opened the best matching font. */
760 struct font_info *font_info;
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));
770 return elt;
773 return Qnil;
777 static Lisp_Object
778 fontset_font (fontset, c, face, id)
779 Lisp_Object fontset;
780 int c;
781 struct face *face;
782 int id;
784 Lisp_Object rfont_def;
785 Lisp_Object base_fontset;
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))
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);
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);
815 if (! VECTORP (rfont_def))
816 /* Remeber that we have no font for C. */
817 FONTSET_SET (fontset, make_number (c), Qt);
819 return rfont_def;
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. */
826 static Lisp_Object
827 make_fontset (frame, name, base)
828 Lisp_Object frame, name, base;
830 Lisp_Object fontset;
831 int size = ASIZE (Vfontset_table);
832 int id = next_fontset_id;
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++;
841 if (id + 1 == size)
842 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
844 fontset = Fmake_char_table (Qfontset, Qnil);
846 FONTSET_ID (fontset) = make_number (id);
847 if (NILP (base))
849 FONTSET_NAME (fontset) = name;
851 else
853 FONTSET_NAME (fontset) = Qnil;
854 FONTSET_FRAME (fontset) = frame;
855 FONTSET_BASE (fontset) = base;
858 ASET (Vfontset_table, id, fontset);
859 next_fontset_id = id + 1;
860 return fontset;
864 /* Set the ASCII font of the default fontset to FONTNAME if that is
865 not yet set. */
866 void
867 set_default_ascii_font (fontname)
868 Lisp_Object fontname;
870 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
872 int id = fs_query_fontset (fontname, 2);
874 if (id >= 0)
875 fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
876 FONTSET_ASCII (Vdefault_fontset)= fontname;
881 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
883 /* Return the name of the fontset who has ID. */
885 Lisp_Object
886 fontset_name (id)
887 int id;
889 Lisp_Object fontset;
891 fontset = FONTSET_FROM_ID (id);
892 return FONTSET_NAME (fontset);
896 /* Return the ASCII font name of the fontset who has ID. */
898 Lisp_Object
899 fontset_ascii (id)
900 int id;
902 Lisp_Object fontset, elt;
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;
916 /* Free fontset of FACE defined on frame F. Called from
917 free_realized_face. */
919 void
920 free_face_fontset (f, face)
921 FRAME_PTR f;
922 struct face *face;
924 Lisp_Object fontset;
926 fontset = AREF (Vfontset_table, 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)))
934 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
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;
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. */
951 face_suitable_for_char_p (face, c)
952 struct face *face;
953 int c;
955 Lisp_Object fontset, rfont_def;
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)));
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. */
970 face_for_char (f, face, c, pos, object)
971 FRAME_PTR f;
972 struct face *face;
973 int c, pos;
974 Lisp_Object object;
976 Lisp_Object fontset, charset, rfont_def;
977 int face_id;
978 int id;
980 if (ASCII_CHAR_P (c))
981 return face->ascii_face->id;
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
990 charset = Fget_char_property (make_number (pos), Qcharset, object);
991 if (NILP (charset))
992 id = -1;
993 else if (CHARSETP (charset))
995 Lisp_Object val;
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));
1003 rfont_def = fontset_font (fontset, c, face, id);
1004 if (VECTORP (rfont_def))
1006 #ifdef USE_FONT_BACKEND
1007 if (enable_font_backend
1008 && NILP (AREF (rfont_def, 0)))
1010 struct font *font = XSAVE_VALUE (AREF (rfont_def, 3))->pointer;
1012 face_id = face_for_font (f, font, face);
1013 ASET (rfont_def, 0, make_number (face_id));
1015 else
1016 #endif /* USE_FONT_BACKEND */
1017 if (NILP (AREF (rfont_def, 0)))
1019 /* We have not yet made a realized face that uses this font. */
1020 int font_idx = XINT (AREF (rfont_def, 1));
1022 face_id = lookup_non_ascii_face (f, font_idx, face);
1023 ASET (rfont_def, 0, make_number (face_id));
1025 return XINT (AREF (rfont_def, 0));
1028 if (NILP (FONTSET_NOFONT_FACE (fontset)))
1030 face_id = lookup_non_ascii_face (f, -1, face);
1031 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
1033 return XINT (FONTSET_NOFONT_FACE (fontset));
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. */
1043 make_fontset_for_ascii_face (f, base_fontset_id, face)
1044 FRAME_PTR f;
1045 int base_fontset_id;
1046 struct face *face;
1048 Lisp_Object base_fontset, fontset, frame;
1050 XSETFRAME (frame, f);
1051 if (base_fontset_id >= 0)
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 ();
1059 else
1060 base_fontset = Vdefault_fontset;
1062 fontset = make_fontset (frame, Qnil, base_fontset);
1063 return XINT (FONTSET_ID (fontset));
1067 #if defined(WINDOWSNT) && defined (_MSC_VER)
1068 #pragma optimize("", off)
1069 #endif
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. */
1076 struct font_info *
1077 fs_load_font (f, fontname, charset)
1078 FRAME_PTR f;
1079 char *fontname;
1080 int charset;
1082 struct font_info *fontp;
1083 Lisp_Object fullname;
1085 if (!fontname)
1086 /* No way to get fontname. */
1087 return NULL;
1089 fontp = (*load_font_func) (f, fontname, 0);
1090 if (! fontp || fontp->charset >= 0)
1091 return fontp;
1093 fontname = fontp->full_name;
1094 fullname = build_string (fontp->full_name);
1096 if (charset < 0)
1098 Lisp_Object charset_symbol;
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));
1107 fontp->charset = charset;
1108 fontp->vertical_centering = 0;
1109 fontp->font_encoder = NULL;
1111 if (charset != charset_ascii)
1113 fontp->vertical_centering
1114 = (STRINGP (Vvertical_centering_font_regexp)
1115 && (fast_string_match_ignore_case
1116 (Vvertical_centering_font_regexp, fullname) >= 0));
1118 if (find_ccl_program_func)
1119 (*find_ccl_program_func) (fontp);
1122 return fontp;
1125 #if defined(WINDOWSNT) && defined (_MSC_VER)
1126 #pragma optimize("", on)
1127 #endif
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. */
1135 Lisp_Object
1136 find_font_encoding (fontname)
1137 Lisp_Object fontname;
1139 Lisp_Object tail, elt;
1141 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
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));
1152 /* We don't know the encoding of this font. Let's assume `ascii'. */
1153 return Qascii;
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. */
1160 static Lisp_Object Vcached_fontset_data;
1162 #define CACHED_FONTSET_NAME (SDATA (XCAR (Vcached_fontset_data)))
1163 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1165 /* If fontset name PATTERN contains any wild card, return regular
1166 expression corresponding to PATTERN. */
1168 static Lisp_Object
1169 fontset_pattern_regexp (pattern)
1170 Lisp_Object pattern;
1172 if (!index (SDATA (pattern), '*')
1173 && !index (SDATA (pattern), '?'))
1174 /* PATTERN does not contain any wild cards. */
1175 return Qnil;
1177 if (!CONSP (Vcached_fontset_data)
1178 || strcmp (SDATA (pattern), CACHED_FONTSET_NAME))
1180 /* We must at first update the cached data. */
1181 unsigned char *regex, *p0, *p1;
1182 int ndashes = 0, nstars = 0;
1184 for (p0 = SDATA (pattern); *p0; p0++)
1186 if (*p0 == '-')
1187 ndashes++;
1188 else if (*p0 == '*')
1189 nstars++;
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);
1200 *p1++ = '^';
1201 for (p0 = SDATA (pattern); *p0; p0++)
1203 if (*p0 == '*')
1205 if (ndashes < 14)
1206 *p1++ = '.';
1207 else
1208 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1209 *p1++ = '*';
1211 else if (*p0 == '?')
1212 *p1++ = '.';
1213 else
1214 *p1++ = *p0;
1216 *p1++ = '$';
1217 *p1++ = 0;
1219 Vcached_fontset_data = Fcons (build_string (SDATA (pattern)),
1220 build_string (regex));
1223 return CACHED_FONTSET_REGEX;
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
1234 fs_query_fontset (name, name_pattern)
1235 Lisp_Object name;
1236 int name_pattern;
1238 Lisp_Object tem;
1239 int i;
1241 name = Fdowncase (name);
1242 if (name_pattern != 1)
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)
1251 tem = fontset_pattern_regexp (name);
1252 if (STRINGP (tem))
1254 name = tem;
1255 name_pattern = 1;
1260 for (i = 0; i < ASIZE (Vfontset_table); i++)
1262 Lisp_Object fontset, this_name;
1264 fontset = FONTSET_FROM_ID (i);
1265 if (NILP (fontset)
1266 || !BASE_FONTSET_P (fontset))
1267 continue;
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;
1275 return -1;
1279 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1280 doc: /* Return the name of a fontset that matches PATTERN.
1281 The value is nil if there is no matching fontset.
1282 PATTERN can contain `*' or `?' as a wildcard
1283 just as X font name matching algorithm allows.
1284 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1285 (pattern, regexpp)
1286 Lisp_Object pattern, regexpp;
1288 Lisp_Object fontset;
1289 int id;
1291 (*check_window_system_func) ();
1293 CHECK_STRING (pattern);
1295 if (SCHARS (pattern) == 0)
1296 return Qnil;
1298 id = fs_query_fontset (pattern, !NILP (regexpp));
1299 if (id < 0)
1300 return Qnil;
1302 fontset = FONTSET_FROM_ID (id);
1303 return FONTSET_NAME (fontset);
1306 /* Return a list of base fontset names matching PATTERN on frame F. */
1308 Lisp_Object
1309 list_fontsets (f, pattern, size)
1310 FRAME_PTR f;
1311 Lisp_Object pattern;
1312 int size;
1314 Lisp_Object frame, regexp, val;
1315 int id;
1317 XSETFRAME (frame, f);
1319 regexp = fontset_pattern_regexp (pattern);
1320 val = Qnil;
1322 for (id = 0; id < ASIZE (Vfontset_table); id++)
1324 Lisp_Object fontset, name;
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);
1333 if (STRINGP (regexp)
1334 ? (fast_string_match (regexp, name) < 0)
1335 : strcmp (SDATA (pattern), SDATA (name)))
1336 continue;
1338 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1341 return val;
1345 /* Free all realized fontsets whose base fontset is BASE. */
1347 static void
1348 free_realized_fontsets (base)
1349 Lisp_Object base;
1351 int id;
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++)
1361 Lisp_Object this = AREF (Vfontset_table, id);
1363 if (EQ (FONTSET_BASE (this), base))
1365 Lisp_Object tail;
1367 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1368 tail = XCDR (tail))
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);
1374 /* Face THIS itself is also freed by the following call. */
1375 free_realized_face (f, face);
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++)
1385 Lisp_Object this = AREF (Vfontset_table, id);
1387 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1389 Fclear_face_cache (Qt);
1390 break;
1393 #endif /* not 0 */
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. */
1401 static Lisp_Object
1402 check_fontset_name (name)
1403 Lisp_Object name;
1405 int id;
1407 if (EQ (name, Qt))
1408 return Vdefault_fontset;
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);
1421 static void
1422 accumulate_script_ranges (arg, range, val)
1423 Lisp_Object arg, range, val;
1425 if (EQ (XCAR (arg), val))
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)));
1435 /* Return an ASCII font name generated from fontset name NAME and
1436 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1438 static INLINE Lisp_Object
1439 generate_ascii_font_name (name, ascii_spec)
1440 Lisp_Object name, ascii_spec;
1442 Lisp_Object font_spec = Ffont_spec (0, NULL);
1443 Lisp_Object vec;
1444 int i;
1445 char xlfd[256];
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);
1458 /* Variables referred in set_fontset_font. They are set before
1459 map_charset_chars is called in Fset_fontset_font. */
1460 static Lisp_Object font_def_arg, add_arg;
1461 static int from_arg, to_arg;
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. */
1468 static void
1469 set_fontset_font (fontset, range)
1470 Lisp_Object fontset, range;
1472 if (from_arg < to_arg)
1474 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1476 if (from < from_arg)
1478 if (to > to_arg)
1480 Lisp_Object range2;
1482 range2 = Fcons (make_number (to_arg), XCDR (range));
1483 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1484 to = to_arg;
1486 if (to > from_arg)
1487 range = Fcons (XCAR (range), make_number (from_arg));
1489 else if (to <= to_arg)
1490 return;
1491 else
1493 if (from < to_arg)
1494 range = Fcons (make_number (to_arg), XCDR (range));
1497 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1500 extern Lisp_Object QCfamily, QCregistry;
1502 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1503 doc: /*
1504 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1506 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1507 In that case, use FONT-SPEC for all characters in the range FROM and
1508 TO (inclusive).
1510 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1511 all characters that belong to the script.
1513 TARGET may be a charset. In that case, use FONT-SPEC for all
1514 characters in the charset.
1516 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1517 that no FONT-SPEC is specified.
1519 FONT-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.
1526 Optional 4th argument FRAME, if non-nil, is a frame. This argument is
1527 kept for backward compatibility and has no meaning.
1529 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1530 to the font specifications for TARGET previously set. If it is
1531 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1532 appended. By default, FONT-SPEC overrides the previous settings. */)
1533 (name, target, font_spec, frame, add)
1534 Lisp_Object name, target, font_spec, frame, add;
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;
1542 fontset = check_fontset_name (name);
1544 /* The arg FRAME is kept for backward compatibility. We only check
1545 the validity. */
1546 if (!NILP (frame))
1547 CHECK_LIVE_FRAME (frame);
1549 if (VECTORP (font_spec))
1551 if (! FONT_SPEC_P (font_spec))
1552 Fsignal (Qfont, list2 (build_string ("invalid font-spec"), font_spec));
1554 else if (CONSP (font_spec))
1556 Lisp_Object args[4];
1557 int i= 0;
1559 family = XCAR (font_spec);
1560 registry = XCDR (font_spec);
1562 if (! NILP (family))
1564 CHECK_STRING (family);
1565 args[i++] = QCfamily;
1566 args[i++] = family;
1568 CHECK_STRING (registry);
1569 args[i++] = QCregistry;
1570 args[i++] = registry;
1571 font_spec = Ffont_spec (i, args);
1573 else if (STRINGP (font_spec))
1575 Lisp_Object args[2];
1577 args[0] = QCname;
1578 args[1] = font_spec;
1579 font_spec = Ffont_spec (2, args);
1581 else if (! NILP (font_spec))
1582 wrong_type_argument (intern ("font-spec"), font_spec);
1584 if (! NILP (font_spec))
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);
1593 encoding = find_font_encoding (concat2 (family, registry));
1594 if (NILP (encoding))
1595 encoding = Qascii;
1597 if (SYMBOLP (encoding))
1599 CHECK_CHARSET (encoding);
1600 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1602 else
1604 repertory = XCDR (encoding);
1605 encoding = XCAR (encoding);
1606 CHECK_CHARSET (encoding);
1607 encoding = CHARSET_SYMBOL_ID (encoding);
1608 if (! NILP (repertory) && SYMBOLP (repertory))
1610 CHECK_CHARSET (repertory);
1611 repertory = CHARSET_SYMBOL_ID (repertory);
1614 font_def = Fmake_vector (make_number (3), font_spec);
1615 ASET (font_def, 1, encoding);
1616 ASET (font_def, 2, repertory);
1618 else
1619 font_def = Qnil;
1621 if (CHARACTERP (target))
1622 range_list = Fcons (Fcons (target, target), Qnil);
1623 else if (CONSP (target))
1625 Lisp_Object from, to;
1627 from = Fcar (target);
1628 to = Fcdr (target);
1629 CHECK_CHARACTER (from);
1630 CHECK_CHARACTER (to);
1631 range_list = Fcons (target, Qnil);
1633 else if (SYMBOLP (target) && !NILP (target))
1635 Lisp_Object script_list;
1636 Lisp_Object val;
1638 range_list = Qnil;
1639 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1640 if (! NILP (Fmemq (target, script_list)))
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)))
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;
1656 if (CHARSETP (target))
1658 if (EQ (target, Qascii) && NILP (FONTSET_ASCII (fontset)))
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);
1667 else
1669 CHECK_CHARSET_GET_CHARSET (target, charset);
1672 else if (NILP (range_list))
1673 error ("Invalid script or charset name: %s",
1674 SDATA (SYMBOL_NAME (target)));
1676 else if (NILP (target))
1677 range_list = Fcons (Qnil, Qnil);
1678 else
1679 error ("Invalid target for setting a font");
1682 if (charset)
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)));
1692 map_charset_chars (set_fontset_font, Qnil, fontset, charset,
1693 CHARSET_MIN_CODE (charset),
1694 CHARSET_MAX_CODE (charset));
1696 for (; CONSP (range_list); range_list = XCDR (range_list))
1697 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
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);
1704 return Qnil;
1708 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1709 doc: /* Create a new fontset NAME from font information in FONTLIST.
1711 FONTLIST is an alist of scripts vs the corresponding font specification list.
1712 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1713 character of SCRIPT is displayed by a font that matches one of
1714 FONT-SPEC.
1716 SCRIPT is a symbol that appears in the first extra slot of the
1717 char-table `char-script-table'.
1719 FONT-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;
1724 Lisp_Object fontset;
1725 Lisp_Object val;
1726 int id;
1728 CHECK_STRING (name);
1729 CHECK_LIST (fontlist);
1731 id = fs_query_fontset (name, 0);
1732 if (id < 0)
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);
1749 else
1751 fontset = FONTSET_FROM_ID (id);;
1752 free_realized_fontsets (fontset);
1753 Fset_char_table_range (fontset, Qt, Qnil);
1756 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1758 Lisp_Object elt, script;
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);
1769 return name;
1773 /* Alist of automatically created fontsets. Each element is a cons
1774 (FONTNAME . FONTSET-ID). */
1775 static Lisp_Object auto_fontset_alist;
1778 new_fontset_from_font_name (Lisp_Object fontname)
1780 Lisp_Object val;
1781 Lisp_Object name;
1782 Lisp_Object vec;
1783 int id;
1785 fontname = Fdowncase (fontname);
1786 val = Fassoc (fontname, auto_fontset_alist);
1787 if (CONSP (val))
1788 return XINT (XCDR (val));
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))
1796 ASET (vec, 13, build_string ("startup"));
1797 name = build_font_name_from_vector (vec);
1799 else
1801 char temp[20];
1802 int len = XINT (Flength (auto_fontset_alist));
1804 sprintf (temp, "auto%d", len);
1805 ASET (vec, 13, build_string (temp));
1806 name = build_font_name_from_vector (vec);
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;
1818 #ifdef USE_FONT_BACKEND
1820 new_fontset_from_font (font_object)
1821 Lisp_Object font_object;
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;
1827 if (NILP (auto_fontset_alist))
1828 short_name = build_string ("fontset-startup");
1829 else
1831 char temp[32];
1832 int len = XINT (Flength (auto_fontset_alist));
1834 sprintf (temp, "fontset-auto%d", len);
1835 short_name = build_string (temp);
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))
1842 int i;
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 ();
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));
1862 struct font *
1863 fontset_ascii_font (f, id)
1864 FRAME_PTR f;
1865 int id;
1867 Lisp_Object fontset = FONTSET_FROM_ID (id);
1868 Lisp_Object ascii_slot = FONTSET_ASCII (fontset);
1869 Lisp_Object val, font_object;
1871 if (CONSP (ascii_slot))
1873 Lisp_Object ascii_font_name = XCAR (ascii_slot);
1875 font_object = Qnil;
1876 for (val = XCDR (ascii_slot); ! NILP (val); val = XCDR (val))
1878 Lisp_Object frame = font_get_frame (XCAR (val));
1880 if (NILP (frame) || XFRAME (frame) == f)
1882 font_object = XCAR (val);
1883 if (XSAVE_VALUE (font_object)->integer == 0)
1885 font_object = font_open_by_name (f, SDATA (ascii_font_name));
1886 XSETCAR (val, font_object);
1888 break;
1891 if (NILP (font_object))
1893 font_object = font_open_by_name (f, SDATA (ascii_font_name));
1894 XSETCDR (ascii_slot, Fcons (font_object, XCDR (ascii_slot)));
1897 else
1899 font_object = font_open_by_name (f, SDATA (ascii_slot));
1900 FONTSET_ASCII (fontset) = Fcons (ascii_slot, Fcons (font_object, Qnil));
1902 if (NILP (font_object))
1903 return NULL;
1904 return XSAVE_VALUE (font_object)->pointer;
1907 #endif /* USE_FONT_BACKEND */
1909 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
1910 doc: /* Return information about a font named NAME on frame FRAME.
1911 If FRAME is omitted or nil, use the selected frame.
1912 The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
1913 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
1914 where
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.
1922 If the named font is not yet loaded, return nil. */)
1923 (name, frame)
1924 Lisp_Object name, frame;
1926 FRAME_PTR f;
1927 struct font_info *fontp;
1928 Lisp_Object info;
1929 Lisp_Object font_object;
1931 (*check_window_system_func) ();
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);
1940 if (!query_font_func)
1941 error ("Font query function is not supported");
1943 #ifdef USE_FONT_BACKEND
1944 if (enable_font_backend)
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;
1952 else
1953 #endif /* USE_FONT_BACKEND */
1954 fontp = (*query_font_func) (f, SDATA (name));
1955 if (!fontp)
1956 return Qnil;
1958 info = Fmake_vector (make_number (7), Qnil);
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);
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;
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.
1983 GLYPH-CODE is the glyph code in the font to use for the character.
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.
1988 It returns nil in the following cases:
1990 (1) The window system doesn't have a font for the character (thus
1991 it is displayed by an empty box).
1993 (2) The character code is invalid.
1995 (3) If POSITION is not nil, and the current buffer is not displayed
1996 in any window.
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. */
2003 DEFUN ("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;
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;
2016 if (NILP (position))
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;
2025 else
2027 Lisp_Object window, charset;
2028 struct window *w;
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
2039 CHECK_NATNUM (ch);
2040 c = XINT (ch);
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;
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)
2062 if (VECTORP (rfont_def) && ! NILP (AREF (rfont_def, 3)))
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);
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)));
2076 return Qnil;
2078 #endif /* USE_FONT_BACKEND */
2079 if (VECTORP (rfont_def) && STRINGP (AREF (rfont_def, 3)))
2081 Lisp_Object font_def;
2082 struct font_info *fontp;
2083 struct charset *charset;
2084 XChar2b char2b;
2085 int code;
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)));
2098 return Qnil;
2102 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
2103 doc: /* Return information about a fontset FONTSET on frame FRAME.
2104 The value is a char-table of which elements has this form.
2106 ((FONT-PATTERN OPENED-FONT ...) ...)
2108 FONT-PATTERN is a vector:
2110 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
2112 or a string of font name pattern.
2114 OPENED-FONT is a name of a font actually opened.
2116 The char-table has one extra slot. The value is a char-table
2117 containing the information about the derived fonts from the default
2118 fontset. The format is the same as abobe. */)
2119 (fontset, frame)
2120 Lisp_Object fontset, frame;
2122 FRAME_PTR f;
2123 Lisp_Object *realized[2], fontsets[2], tables[2];
2124 Lisp_Object val, elt;
2125 int c, i, j, k;
2127 (*check_window_system_func) ();
2129 fontset = check_fontset_name (fontset);
2131 if (NILP (frame))
2132 frame = selected_frame;
2133 CHECK_LIVE_FRAME (frame);
2134 f = XFRAME (frame);
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++)
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;
2148 realized[0][j] = Qnil;
2150 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
2151 * ASIZE (Vfontset_table));
2152 for (i = j = 0; ! NILP (realized[0][i]); i++)
2154 elt = FONTSET_DEFAULT (realized[0][i]);
2155 if (! NILP (elt))
2156 realized[1][j++] = elt;
2158 realized[1][j] = Qnil;
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;
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++)
2170 for (c = 0; c <= MAX_CHAR; )
2172 int from, to;
2174 if (c <= MAX_5_BYTE_CHAR)
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;
2180 else
2182 val = FONTSET_FALLBACK (fontsets[k]);
2183 to = MAX_CHAR;
2185 if (VECTORP (val))
2187 Lisp_Object alist;
2189 /* At first, set ALIST to ((FONT-SPEC) ...). */
2190 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
2191 alist = Fcons (Fcons (AREF (AREF (val, i), 0), Qnil), alist);
2192 alist = Fnreverse (alist);
2194 /* Then store opend font names to cdr of each elements. */
2195 for (i = 0; ! NILP (realized[k][i]); i++)
2197 if (c <= MAX_5_BYTE_CHAR)
2198 val = FONTSET_REF (realized[k][i], c);
2199 else
2200 val = FONTSET_FALLBACK (realized[k][i]);
2201 if (! VECTORP (val))
2202 continue;
2203 /* VAL is [int int ?
2204 [FACE-ID FONT-INDEX FONT-DEF FONT-NAME] ...].
2205 If a font of an element is already opened,
2206 FONT-NAME is the name of a opened font. */
2207 for (j = 3; j < ASIZE (val); j++)
2208 if (STRINGP (AREF (AREF (val, j), 3)))
2210 Lisp_Object font_idx;
2212 font_idx = AREF (AREF (val, j), 1);
2213 elt = Fassq (AREF (AREF (AREF (val, j), 2), 0), alist);
2214 if (CONSP (elt)
2215 && NILP (Fmemq (font_idx, XCDR(elt))))
2216 nconc2 (elt, Fcons (font_idx, Qnil));
2219 for (val = alist; CONSP (val); val = XCDR (val))
2220 for (elt = XCDR (XCAR (val)); CONSP (elt); elt = XCDR (elt))
2222 struct font_info *font_info
2223 = (*get_font_info_func) (f, XINT (XCAR (elt)));
2224 XSETCAR (elt, build_string (font_info->full_name));
2227 /* Store ALIST in TBL for characters C..TO. */
2228 if (c <= MAX_5_BYTE_CHAR)
2229 char_table_set_range (tables[k], c, to, alist);
2230 else
2231 XCHAR_TABLE (tables[k])->defalt = alist;
2233 c = to + 1;
2237 return tables[0];
2241 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2242 doc: /* Return a font name pattern for character CH in fontset NAME.
2243 If NAME is t, find a pattern in the default fontset.
2245 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2246 family name and REGISTRY is a font registry name. This is actually
2247 the first font name pattern for CH in the fontset or in the default
2248 fontset.
2250 If the 2nd optional arg ALL is non-nil, return a list of all font name
2251 patterns. */)
2252 (name, ch, all)
2253 Lisp_Object name, ch, all;
2255 int c;
2256 Lisp_Object fontset, elt, list, repertory, val;
2257 int i, j;
2259 fontset = check_fontset_name (name);
2261 CHECK_CHARACTER (ch);
2262 c = XINT (ch);
2263 list = Qnil;
2264 while (1)
2266 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2267 i++, elt = FONTSET_FALLBACK (fontset))
2268 if (VECTORP (elt))
2269 for (j = 0; j < ASIZE (elt); j++)
2271 val = AREF (elt, j);
2272 repertory = AREF (val, 1);
2273 if (INTEGERP (repertory))
2275 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2277 if (! CHAR_CHARSET_P (c, charset))
2278 continue;
2280 else if (CHAR_TABLE_P (repertory))
2282 if (NILP (CHAR_TABLE_REF (repertory, c)))
2283 continue;
2285 val = AREF (val, 0);
2286 val = Fcons (AREF (val, 0), AREF (val, 5));
2287 if (NILP (all))
2288 return val;
2289 list = Fcons (val, list);
2291 if (EQ (fontset, Vdefault_fontset))
2292 break;
2293 fontset = Vdefault_fontset;
2295 return (Fnreverse (list));
2298 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2299 doc: /* Return a list of all defined fontset names. */)
2302 Lisp_Object fontset, list;
2303 int i;
2305 list = Qnil;
2306 for (i = 0; i < ASIZE (Vfontset_table); i++)
2308 fontset = FONTSET_FROM_ID (i);
2309 if (!NILP (fontset)
2310 && BASE_FONTSET_P (fontset))
2311 list = Fcons (FONTSET_NAME (fontset), list);
2314 return list;
2318 #ifdef FONTSET_DEBUG
2320 Lisp_Object
2321 dump_fontset (fontset)
2322 Lisp_Object fontset;
2324 Lisp_Object vec;
2326 vec = Fmake_vector (make_number (3), Qnil);
2327 ASET (vec, 0, FONTSET_ID (fontset));
2329 if (BASE_FONTSET_P (fontset))
2331 ASET (vec, 1, FONTSET_NAME (fontset));
2333 else
2335 Lisp_Object frame;
2337 frame = FONTSET_FRAME (fontset);
2338 if (FRAMEP (frame))
2340 FRAME_PTR f = XFRAME (frame);
2342 if (FRAME_LIVE_P (f))
2343 ASET (vec, 1,
2344 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
2345 else
2346 ASET (vec, 1,
2347 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2349 if (!NILP (FONTSET_DEFAULT (fontset)))
2350 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2352 return vec;
2355 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2356 doc: /* Return a brief summary of all fontsets for debug use. */)
2359 Lisp_Object val;
2360 int i;
2362 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2363 if (! NILP (AREF (Vfontset_table, i)))
2364 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2365 return (Fnreverse (val));
2367 #endif /* FONTSET_DEBUG */
2369 void
2370 syms_of_fontset ()
2372 if (!load_font_func)
2373 /* Window system initializer should have set proper functions. */
2374 abort ();
2376 DEFSYM (Qfontset, "fontset");
2377 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2378 DEFSYM (Qfontset_info, "fontset-info");
2379 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2381 DEFSYM (Qprepend, "prepend");
2382 DEFSYM (Qappend, "append");
2383 DEFSYM (Qlatin, "latin");
2385 Vcached_fontset_data = Qnil;
2386 staticpro (&Vcached_fontset_data);
2388 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2389 staticpro (&Vfontset_table);
2391 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2392 staticpro (&Vdefault_fontset);
2393 FONTSET_ID (Vdefault_fontset) = make_number (0);
2394 FONTSET_NAME (Vdefault_fontset)
2395 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2396 AREF (Vfontset_table, 0) = Vdefault_fontset;
2397 next_fontset_id = 1;
2399 auto_fontset_alist = Qnil;
2400 staticpro (&auto_fontset_alist);
2402 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
2403 doc: /*
2404 Alist of fontname patterns vs the corresponding encoding and repertory info.
2405 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
2406 where ENCODING is a charset or a char-table,
2407 and REPERTORY is a charset, a char-table, or nil.
2409 If ENCDING and REPERTORY are the same, the element can have the form
2410 \(REGEXP . ENCODING).
2412 ENCODING is for converting a character to a glyph code of the font.
2413 If ENCODING is a charset, encoding a character by the charset gives
2414 the corresponding glyph code. If ENCODING is a char-table, looking up
2415 the table by a character gives the corresponding glyph code.
2417 REPERTORY specifies a repertory of characters supported by the font.
2418 If REPERTORY is a charset, all characters beloging to the charset are
2419 supported. If REPERTORY is a char-table, all characters who have a
2420 non-nil value in the table are supported. It REPERTORY is nil, Emacs
2421 gets the repertory information by an opened font and ENCODING. */);
2422 Vfont_encoding_alist = Qnil;
2424 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2425 doc: /*
2426 Alist of charsets vs the charsets to determine the preferred font encoding.
2427 Each element looks like (CHARSET . ENCDOING-CHARSET),
2428 where ENCODING-CHARSET is a charset registered in the variable
2429 `font-encoding-alist' as ENCODING.
2431 When a text has a property `charset' and the value is CHARSET, a font
2432 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2433 Vfont_encoding_charset_alist = Qnil;
2435 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
2436 doc: /*
2437 Char table of characters whose ascent values should be ignored.
2438 If an entry for a character is non-nil, the ascent value of the glyph
2439 is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.
2441 This affects how a composite character which contains
2442 such a character is displayed on screen. */);
2443 Vuse_default_ascent = Qnil;
2445 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
2446 doc: /*
2447 Char table of characters which is not composed relatively.
2448 If an entry for a character is non-nil, a composition sequence
2449 which contains that character is displayed so that
2450 the glyph of that character is put without considering
2451 an ascent and descent value of a previous character. */);
2452 Vignore_relative_composition = Qnil;
2454 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
2455 doc: /* Alist of fontname vs list of the alternate fontnames.
2456 When a specified font name is not found, the corresponding
2457 alternate fontnames (if any) are tried instead. */);
2458 Valternate_fontname_alist = Qnil;
2460 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
2461 doc: /* Alist of fontset names vs the aliases. */);
2462 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2463 build_string ("fontset-default")),
2464 Qnil);
2466 DEFVAR_LISP ("vertical-centering-font-regexp",
2467 &Vvertical_centering_font_regexp,
2468 doc: /* *Regexp matching font names that require vertical centering on display.
2469 When a character is displayed with such fonts, the character is displayed
2470 at the vertical center of lines. */);
2471 Vvertical_centering_font_regexp = Qnil;
2473 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2474 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2475 Votf_script_alist = Qnil;
2477 defsubr (&Squery_fontset);
2478 defsubr (&Snew_fontset);
2479 defsubr (&Sset_fontset_font);
2480 defsubr (&Sfont_info);
2481 defsubr (&Sinternal_char_font);
2482 defsubr (&Sfontset_info);
2483 defsubr (&Sfontset_font);
2484 defsubr (&Sfontset_list);
2485 #ifdef FONTSET_DEBUG
2486 defsubr (&Sfontset_list_all);
2487 #endif
2490 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2491 (do not change this comment) */