* fontset.c (fontset_get_font_group):
[emacs.git] / src / fontset.c
blobcaa732b6bc6f736f749aecc256d93cd29a46df6f
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 of the License, or
17 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
27 /* #define FONTSET_DEBUG */
29 #include <config.h>
31 #ifdef FONTSET_DEBUG
32 #include <stdio.h>
33 #endif
35 #include "lisp.h"
36 #include "blockinput.h"
37 #include "buffer.h"
38 #include "character.h"
39 #include "charset.h"
40 #include "ccl.h"
41 #include "keyboard.h"
42 #include "frame.h"
43 #include "dispextern.h"
44 #include "intervals.h"
45 #include "fontset.h"
46 #include "window.h"
47 #ifdef HAVE_X_WINDOWS
48 #include "xterm.h"
49 #endif
50 #ifdef WINDOWSNT
51 #include "w32term.h"
52 #endif
53 #ifdef MAC_OS
54 #include "macterm.h"
55 #endif
56 #include "termhooks.h"
58 #include "font.h"
60 #undef xassert
61 #ifdef FONTSET_DEBUG
62 #define xassert(X) do {if (!(X)) abort ();} while (0)
63 #undef INLINE
64 #define INLINE
65 #else /* not FONTSET_DEBUG */
66 #define xassert(X) (void) 0
67 #endif /* not FONTSET_DEBUG */
69 EXFUN (Fclear_face_cache, 1);
71 /* FONTSET
73 A fontset is a collection of font related information to give
74 similar appearance (style, etc) of characters. A fontset has two
75 roles. One is to use for the frame parameter `font' as if it is an
76 ASCII font. In that case, Emacs uses the font specified for
77 `ascii' script for the frame's default font.
79 Another role, the more important one, is to provide information
80 about which font to use for each non-ASCII character.
82 There are two kinds of fontsets; base and realized. A base fontset
83 is created by `new-fontset' from Emacs Lisp explicitly. A realized
84 fontset is created implicitly when a face is realized for ASCII
85 characters. A face is also realized for non-ASCII characters based
86 on an ASCII face. All of non-ASCII faces based on the same ASCII
87 face share the same realized fontset.
89 A fontset object is implemented by a char-table whose default value
90 and parent are always nil.
92 An element of a base fontset is a vector of FONT-DEFs which itself
93 is a vector [ FONT-SPEC ENCODING REPERTORY ].
95 An element of a realized fontset is nil, t, or a vector of this form:
97 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
98 RFONT-DEF0 RFONT-DEF1 ... ]
100 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
102 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
104 RFONT-DEFn are automatically reordered by the current charset
105 priority list.
107 The value nil means that we have not yet generated the above vector
108 from the base of the fontset.
110 The value t means that no font is available for the corresponding
111 range of characters.
114 A fontset has 9 extra slots.
116 The 1st slot: the ID number of the fontset
118 The 2nd slot:
119 base: the name of the fontset
120 realized: nil
122 The 3rd slot:
123 base: nil
124 realized: the base fontset
126 The 4th slot:
127 base: nil
128 realized: the frame that the fontset belongs to
130 The 5th slot:
131 base: the font name for ASCII characters
132 realized: nil
134 The 6th slot:
135 base: nil
136 realized: the ID number of a face to use for characters that
137 has no font in a realized fontset.
139 The 7th slot:
140 base: nil
141 realized: Alist of font index vs the corresponding repertory
142 char-table.
144 The 8th slot:
145 base: nil
146 realized: If the base is not the default fontset, a fontset
147 realized from the default fontset, else nil.
149 The 9th slot:
150 base: Same as element value (but for fallback fonts).
151 realized: Likewise.
153 All fontsets are recorded in the vector Vfontset_table.
156 DEFAULT FONTSET
158 There's a special base fontset named `default fontset' which
159 defines the default font specifications. When a base fontset
160 doesn't specify a font for a specific character, the corresponding
161 value in the default fontset is used.
163 The parent of a realized fontset created for such a face that has
164 no fontset is the default fontset.
167 These structures are hidden from the other codes than this file.
168 The other codes handle fontsets only by their ID numbers. They
169 usually use the variable name `fontset' for IDs. But, in this
170 file, we always use varialbe name `id' for IDs, and name `fontset'
171 for an actual fontset object, i.e., char-table.
175 /********** VARIABLES and FUNCTION PROTOTYPES **********/
177 extern Lisp_Object Qfont;
178 static Lisp_Object Qfontset;
179 static Lisp_Object Qfontset_info;
180 static Lisp_Object Qprepend, Qappend;
181 static Lisp_Object Qlatin;
183 /* Vector containing all fontsets. */
184 static Lisp_Object Vfontset_table;
186 /* Next possibly free fontset ID. Usually this keeps the minimum
187 fontset ID not yet used. */
188 static int next_fontset_id;
190 /* The default fontset. This gives default FAMILY and REGISTRY of
191 font for each character. */
192 static Lisp_Object Vdefault_fontset;
194 Lisp_Object Vfont_encoding_charset_alist;
195 Lisp_Object Vuse_default_ascent;
196 Lisp_Object Vignore_relative_composition;
197 Lisp_Object Valternate_fontname_alist;
198 Lisp_Object Vfontset_alias_alist;
199 Lisp_Object Vvertical_centering_font_regexp;
200 Lisp_Object Votf_script_alist;
202 /* Check if any window system is used now. */
203 void (*check_window_system_func) P_ ((void));
206 /* Prototype declarations for static functions. */
207 static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
208 Lisp_Object));
209 static Lisp_Object fontset_find_font P_ ((Lisp_Object, int, struct face *,
210 int, int));
211 static void reorder_font_vector P_ ((Lisp_Object, struct font *));
212 static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
213 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
214 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
215 static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
216 Lisp_Object));
217 Lisp_Object find_font_encoding P_ ((Lisp_Object));
219 static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
221 #ifdef FONTSET_DEBUG
223 /* Return 1 if ID is a valid fontset id, else return 0. */
225 static int
226 fontset_id_valid_p (id)
227 int id;
229 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
232 #endif
236 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
238 /* Return the fontset with ID. No check of ID's validness. */
239 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
241 /* Macros to access special values of FONTSET. */
242 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
244 /* Macros to access special values of (base) FONTSET. */
245 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
246 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
247 #define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5]
249 /* Macros to access special values of (realized) FONTSET. */
250 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
251 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
252 #define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4]
253 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
254 #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
255 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
257 /* For both base and realized fontset. */
258 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
260 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
263 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
264 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
265 do { \
266 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
267 ASET ((font_def), 1, encoding); \
268 ASET ((font_def), 2, repertory); \
269 } while (0)
271 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
272 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
273 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
275 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
276 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
277 ASET ((rfont_def), 0, make_number (face_id))
278 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
279 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
280 #define RFONT_DEF_REPERTORY(rfont_def) FONT_DEF_REPERTORY (AREF (rfont_def, 1))
281 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
282 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
283 ASET ((rfont_def), 2, (object))
284 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
285 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
286 ASET ((rfont_def), 3, make_number (score))
287 #define RFONT_DEF_NEW(rfont_def, font_def) \
288 do { \
289 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
290 ASET ((rfont_def), 1, (font_def)); \
291 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
292 } while (0)
295 /* Return the element of FONTSET for the character C. If FONTSET is a
296 base fontset other then the default fontset and FONTSET doesn't
297 contain information for C, return the information in the default
298 fontset. */
300 #define FONTSET_REF(fontset, c) \
301 (EQ (fontset, Vdefault_fontset) \
302 ? CHAR_TABLE_REF (fontset, c) \
303 : fontset_ref ((fontset), (c)))
305 static Lisp_Object
306 fontset_ref (fontset, c)
307 Lisp_Object fontset;
308 int c;
310 Lisp_Object elt;
312 elt = CHAR_TABLE_REF (fontset, c);
313 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
314 /* Don't check Vdefault_fontset for a realized fontset. */
315 && NILP (FONTSET_BASE (fontset)))
316 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
317 return elt;
320 /* Set elements of FONTSET for characters in RANGE to the value ELT.
321 RANGE is a cons (FROM . TO), where FROM and TO are character codes
322 specifying a range. */
324 #define FONTSET_SET(fontset, range, elt) \
325 Fset_char_table_range ((fontset), (range), (elt))
328 /* Modify the elements of FONTSET for characters in RANGE by replacing
329 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
330 and TO are character codes specifying a range. If ADD is nil,
331 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
332 append ELT. */
334 #define FONTSET_ADD(fontset, range, elt, add) \
335 (NILP (add) \
336 ? (NILP (range) \
337 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
338 : Fset_char_table_range ((fontset), (range), \
339 Fmake_vector (make_number (1), (elt)))) \
340 : fontset_add ((fontset), (range), (elt), (add)))
342 static Lisp_Object
343 fontset_add (fontset, range, elt, add)
344 Lisp_Object fontset, range, elt, add;
346 Lisp_Object args[2];
347 int idx = (EQ (add, Qappend) ? 0 : 1);
349 args[1 - idx] = Fmake_vector (make_number (1), elt);
351 if (CONSP (range))
353 int from = XINT (XCAR (range));
354 int to = XINT (XCDR (range));
355 int from1, to1;
357 do {
358 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
359 if (to < to1)
360 to1 = to;
361 char_table_set_range (fontset, from, to1,
362 NILP (args[idx]) ? args[1 - idx]
363 : Fvconcat (2, args));
364 from = to1 + 1;
365 } while (from < to);
367 else
369 args[idx] = FONTSET_FALLBACK (fontset);
370 FONTSET_FALLBACK (fontset)
371 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
373 return Qnil;
376 static int
377 fontset_compare_rfontdef (val1, val2)
378 const void *val1, *val2;
380 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
381 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
384 /* Update FONT-GROUP which has this form:
385 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
386 RFONT-DEF0 RFONT-DEF1 ... ]
387 Reorder RFONT-DEFs according to the current language, and update
388 CHARSET-ORDERED-LIST-TICK.
390 If PREFERRED_FAMILY is not nil, that family has the higher priority
391 if the encoding charsets or languages in font-specs are the same. */
393 extern Lisp_Object Fassoc_string ();
395 static void
396 reorder_font_vector (font_group, font)
397 Lisp_Object font_group;
398 struct font *font;
400 Lisp_Object vec, font_object;
401 int size;
402 int i;
403 int score_changed = 0;
405 if (font)
406 XSETFONT (font_object, font);
407 else
408 font_object = Qnil;
410 vec = XCDR (font_group);
411 size = ASIZE (vec);
412 /* Exclude the tailing nil element from the reordering. */
413 if (NILP (AREF (vec, size - 1)))
414 size--;
416 for (i = 0; i < size; i++)
418 Lisp_Object rfont_def = AREF (vec, i);
419 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
420 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
421 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
423 if (! font_match_p (font_spec, font_object))
425 Lisp_Object repertory = FONT_DEF_REPERTORY (font_def);
427 if (! NILP (repertory))
429 Lisp_Object tail;
431 for (tail = Vcharset_ordered_list;
432 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
433 score += 0x100, tail = XCDR (tail))
434 if (EQ (repertory, XCAR (tail)))
435 break;
437 else
439 Lisp_Object lang = Ffont_get (font_spec, QClang);
441 if (! NILP (lang)
442 && ! EQ (lang, Vcurrent_iso639_language)
443 && (! CONSP (Vcurrent_iso639_language)
444 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
445 score |= 0x100;
448 if (RFONT_DEF_SCORE (rfont_def) != score)
450 RFONT_DEF_SET_SCORE (rfont_def, score);
451 score_changed = 1;
455 if (score_changed)
456 qsort (XVECTOR (vec)->contents, size, sizeof (Lisp_Object),
457 fontset_compare_rfontdef);
458 XSETCAR (font_group, make_number (charset_ordered_list_tick));
461 static Lisp_Object
462 fontset_get_font_group (Lisp_Object fontset, int c)
464 Lisp_Object font_group;
465 Lisp_Object base_fontset;
466 int from, to, i;
468 xassert (! BASE_FONTSET_P (fontset));
469 if (c >= 0)
470 font_group = CHAR_TABLE_REF (fontset, c);
471 else
472 font_group = FONTSET_FALLBACK (fontset);
473 if (! NILP (font_group))
474 return font_group;
475 base_fontset = FONTSET_BASE (fontset);
476 if (c >= 0)
477 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
478 else
479 font_group = FONTSET_FALLBACK (base_fontset);
480 if (NILP (font_group))
481 return Qnil;
482 font_group = Fcopy_sequence (font_group);
483 for (i = 0; i < ASIZE (font_group); i++)
484 if (! NILP (AREF (font_group, i)))
486 Lisp_Object rfont_def;
488 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
489 /* Remember the original order. */
490 RFONT_DEF_SET_SCORE (rfont_def, i);
491 ASET (font_group, i, rfont_def);
493 font_group = Fcons (make_number (-1), font_group);
494 if (c >= 0)
495 char_table_set_range (fontset, from, to, font_group);
496 else
497 FONTSET_FALLBACK (fontset) = font_group;
498 return font_group;
501 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
502 character C. If no font is found, return Qnil if there's a
503 possibility that the default fontset or the fallback font groups
504 have a proper font, and return Qt if not.
506 If a font is found but is not yet opened, open it (if FACE is not
507 NULL) or return Qnil (if FACE is NULL).
509 ID is a charset-id that must be preferred, or -1 meaning no
510 preference.
512 If FALLBACK is nonzero, search only fallback fonts. */
514 static Lisp_Object
515 fontset_find_font (fontset, c, face, id, fallback)
516 Lisp_Object fontset;
517 int c;
518 struct face *face;
519 int id, fallback;
521 Lisp_Object elt, vec, font_group;
522 int i;
523 FRAME_PTR f = XFRAME (FONTSET_FRAME (fontset));
524 int charset_matched = -1;
526 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
527 if (! CONSP (font_group))
528 return Qnil;
529 vec = XCDR (font_group);
530 if (ASIZE (vec) == 0)
531 return Qnil;
533 if (ASIZE (vec) > 1)
535 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
536 /* We have just created the font-group,
537 or the charset priorities were changed. */
538 reorder_font_vector (font_group, face->ascii_face->font);
539 if (id >= 0)
540 /* Find a spec matching with the charset ID to try at
541 first. */
542 for (i = 0; i < ASIZE (vec); i++)
544 Lisp_Object rfont_def = AREF (vec, i);
545 Lisp_Object repertory
546 = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
548 if (XINT (repertory) == id)
550 charset_matched = i;
551 break;
556 /* Find the first available font in the vector of RFONT-DEF. */
557 for (i = 0; i < ASIZE (vec); i++)
559 Lisp_Object font_entity, font_object;
561 if (i == 0 && charset_matched >= 0)
563 /* Try the element matching with the charset ID at first. */
564 elt = AREF (vec, charset_matched);
565 charset_matched = -1;
566 i--;
568 else if (i != charset_matched)
569 elt = AREF (vec, i);
570 else
571 continue;
573 if (NILP (elt))
574 /* This is a sign of not to try the other fonts. */
575 return Qt;
576 if (INTEGERP (RFONT_DEF_FACE (elt))
577 && XINT (AREF (elt, 1)) < 0)
578 /* We couldn't open this font last time. */
579 continue;
581 font_object = RFONT_DEF_OBJECT (elt);
582 if (NILP (font_object))
584 Lisp_Object font_def = RFONT_DEF_FONT_DEF (elt);
586 if (! face)
587 /* We have not yet opened the font. */
588 return Qnil;
589 font_entity = font_find_for_lface (f, face->lface,
590 FONT_DEF_SPEC (font_def), -1);
591 if (NILP (font_entity))
593 /* Record that no font matches the spec. */
594 RFONT_DEF_SET_FACE (elt, -1);
595 continue;
597 font_object = font_open_for_lface (f, font_entity, face->lface,
598 FONT_DEF_SPEC (font_def));
599 if (NILP (font_object))
601 /* Record that the font is unsable. */
602 RFONT_DEF_SET_FACE (elt, -1);
603 continue;
605 RFONT_DEF_SET_OBJECT (elt, font_object);
608 if (font_has_char (f, font_object, c))
609 return elt;
611 #if 0
612 /* The following code makes Emacs to find a font for C by fairly
613 exhausitive search. But, that takes long time especially for
614 X font backend. */
616 /* Try to find the different font maching with the current spec
617 and support C. */
618 font_def = RFONT_DEF_FONT_DEF (elt);
619 for (i++; i < ASIZE (vec); i++)
621 if (! EQ (RFONT_DEF_FONT_DEF (AREF (vec, i)), font_def))
622 break;
623 if (font_has_char (f, RFONT_DEF_OBJECT (AREF (vec, i)), c))
624 return AREF (vec, i);
626 /* Find an font-entity that support C. */
627 font_entity = font_find_for_lface (f, face->lface,
628 FONT_DEF_SPEC (font_def), c);
629 if (! NILP (font_entity))
631 Lisp_Object rfont_def, new_vec;
632 int j;
634 font_object = font_open_for_lface (f, font_entity, face->lface,
635 Qnil);
636 RFONT_DEF_NEW (rfont_def, font_def);
637 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
638 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (elt));
639 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
640 for (j = 0; j < i; j++)
641 ASET (new_vec, j, AREF (vec, j));
642 ASET (new_vec, j, rfont_def);
643 for (j++; j < ASIZE (new_vec); j++)
644 ASET (new_vec, j, AREF (vec, j - 1));
645 vec = new_vec;
646 return rfont_def;
648 i--;
649 #endif /* 0 */
652 FONTSET_SET (fontset, make_number (c), make_number (0));
653 return Qnil;
657 static Lisp_Object
658 fontset_font (fontset, c, face, id)
659 Lisp_Object fontset;
660 int c;
661 struct face *face;
662 int id;
664 Lisp_Object rfont_def;
665 Lisp_Object base_fontset;
667 /* Try a font-group of FONTSET. */
668 rfont_def = fontset_find_font (fontset, c, face, id, 0);
669 if (VECTORP (rfont_def))
670 return rfont_def;
671 if (EQ (rfont_def, Qt))
672 return Qnil;
674 /* Try a font-group of the default fontset. */
675 base_fontset = FONTSET_BASE (fontset);
676 if (! EQ (base_fontset, Vdefault_fontset))
678 if (NILP (FONTSET_DEFAULT (fontset)))
679 FONTSET_DEFAULT (fontset)
680 = make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset);
681 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
682 if (VECTORP (rfont_def))
683 return rfont_def;
684 if (EQ (rfont_def, Qt))
685 return Qnil;
688 /* Try a fallback font-group of FONTSET. */
689 rfont_def = fontset_find_font (fontset, c, face, id, 1);
690 if (VECTORP (rfont_def))
691 return rfont_def;
692 if (EQ (rfont_def, Qt))
693 return Qnil;
695 /* Try a fallback font-group of the default fontset . */
696 if (! EQ (base_fontset, Vdefault_fontset))
698 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
699 if (VECTORP (rfont_def))
700 return rfont_def;
703 /* Remeber that we have no font for C. */
704 FONTSET_SET (fontset, make_number (c), Qt);
706 return Qnil;
709 /* Return a newly created fontset with NAME. If BASE is nil, make a
710 base fontset. Otherwise make a realized fontset whose base is
711 BASE. */
713 static Lisp_Object
714 make_fontset (frame, name, base)
715 Lisp_Object frame, name, base;
717 Lisp_Object fontset;
718 int size = ASIZE (Vfontset_table);
719 int id = next_fontset_id;
721 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
722 the next available fontset ID. So it is expected that this loop
723 terminates quickly. In addition, as the last element of
724 Vfontset_table is always nil, we don't have to check the range of
725 id. */
726 while (!NILP (AREF (Vfontset_table, id))) id++;
728 if (id + 1 == size)
729 Vfontset_table = larger_vector (Vfontset_table, size + 32, Qnil);
731 fontset = Fmake_char_table (Qfontset, Qnil);
733 FONTSET_ID (fontset) = make_number (id);
734 if (NILP (base))
736 FONTSET_NAME (fontset) = name;
738 else
740 FONTSET_NAME (fontset) = Qnil;
741 FONTSET_FRAME (fontset) = frame;
742 FONTSET_BASE (fontset) = base;
745 ASET (Vfontset_table, id, fontset);
746 next_fontset_id = id + 1;
747 return fontset;
751 /* Set the ASCII font of the default fontset to FONTNAME if that is
752 not yet set. */
753 void
754 set_default_ascii_font (fontname)
755 Lisp_Object fontname;
757 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset)))
759 int id = fs_query_fontset (fontname, 2);
761 if (id >= 0)
762 fontname = FONTSET_ASCII (FONTSET_FROM_ID (id));
763 FONTSET_ASCII (Vdefault_fontset)= fontname;
768 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
770 /* Return the name of the fontset who has ID. */
772 Lisp_Object
773 fontset_name (id)
774 int id;
776 Lisp_Object fontset;
778 fontset = FONTSET_FROM_ID (id);
779 return FONTSET_NAME (fontset);
783 /* Return the ASCII font name of the fontset who has ID. */
785 Lisp_Object
786 fontset_ascii (id)
787 int id;
789 Lisp_Object fontset, elt;
791 fontset= FONTSET_FROM_ID (id);
792 elt = FONTSET_ASCII (fontset);
793 if (CONSP (elt))
794 elt = XCAR (elt);
795 return elt;
798 void
799 free_realized_fontset (f, fontset)
800 FRAME_PTR f;
801 Lisp_Object fontset;
803 Lisp_Object tail;
805 return;
806 for (tail = FONTSET_OBJLIST (fontset); CONSP (tail); tail = XCDR (tail))
808 xassert (FONT_OBJECT_P (XCAR (tail)));
809 font_close_object (f, XCAR (tail));
813 /* Free fontset of FACE defined on frame F. Called from
814 free_realized_face. */
816 void
817 free_face_fontset (f, face)
818 FRAME_PTR f;
819 struct face *face;
821 Lisp_Object fontset;
823 fontset = FONTSET_FROM_ID (face->fontset);
824 if (NILP (fontset))
825 return;
826 xassert (! BASE_FONTSET_P (fontset));
827 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
828 free_realized_fontset (f, fontset);
829 ASET (Vfontset_table, face->fontset, Qnil);
830 if (face->fontset < next_fontset_id)
831 next_fontset_id = face->fontset;
832 if (! NILP (FONTSET_DEFAULT (fontset)))
834 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
836 fontset = AREF (Vfontset_table, id);
837 xassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
838 xassert (f == XFRAME (FONTSET_FRAME (fontset)));
839 free_realized_fontset (f, fontset);
840 ASET (Vfontset_table, id, Qnil);
841 if (id < next_fontset_id)
842 next_fontset_id = face->fontset;
844 face->fontset = -1;
848 /* Return 1 if FACE is suitable for displaying character C.
849 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
850 when C is not an ASCII character. */
853 face_suitable_for_char_p (face, c)
854 struct face *face;
855 int c;
857 Lisp_Object fontset, rfont_def;
859 fontset = FONTSET_FROM_ID (face->fontset);
860 rfont_def = fontset_font (fontset, c, NULL, -1);
861 return (VECTORP (rfont_def)
862 && INTEGERP (RFONT_DEF_FACE (rfont_def))
863 && face->id == XINT (RFONT_DEF_FACE (rfont_def)));
867 /* Return ID of face suitable for displaying character C on frame F.
868 FACE must be reazlied for ASCII characters in advance. Called from
869 the macro FACE_FOR_CHAR. */
872 face_for_char (f, face, c, pos, object)
873 FRAME_PTR f;
874 struct face *face;
875 int c, pos;
876 Lisp_Object object;
878 Lisp_Object fontset, rfont_def;
879 int face_id;
880 int id;
882 if (ASCII_CHAR_P (c))
883 return face->ascii_face->id;
885 xassert (fontset_id_valid_p (face->fontset));
886 fontset = FONTSET_FROM_ID (face->fontset);
887 xassert (!BASE_FONTSET_P (fontset));
889 if (pos < 0)
890 id = -1;
891 else
893 Lisp_Object charset;
895 charset = Fget_char_property (make_number (pos), Qcharset, object);
896 if (NILP (charset))
897 id = -1;
898 else if (CHARSETP (charset))
900 Lisp_Object val;
902 val = assoc_no_quit (charset, Vfont_encoding_charset_alist);
903 if (CONSP (val) && CHARSETP (XCDR (val)))
904 charset = XCDR (val);
905 id = XINT (CHARSET_SYMBOL_ID (charset));
909 rfont_def = fontset_font (fontset, c, face, id);
910 if (VECTORP (rfont_def))
912 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
913 face_id = XINT (RFONT_DEF_FACE (rfont_def));
914 else
916 Lisp_Object font_object;
918 font_object = RFONT_DEF_OBJECT (rfont_def);
919 face_id = face_for_font (f, font_object, face);
920 RFONT_DEF_SET_FACE (rfont_def, face_id);
923 else
925 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
926 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
927 else
929 face_id = face_for_font (f, Qnil, face);
930 FONTSET_NOFONT_FACE (fontset) = make_number (face_id);
933 xassert (face_id >= 0);
934 return face_id;
938 /* Make a realized fontset for ASCII face FACE on frame F from the
939 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
940 default fontset as the base. Value is the id of the new fontset.
941 Called from realize_x_face. */
944 make_fontset_for_ascii_face (f, base_fontset_id, face)
945 FRAME_PTR f;
946 int base_fontset_id;
947 struct face *face;
949 Lisp_Object base_fontset, fontset, frame;
951 XSETFRAME (frame, f);
952 if (base_fontset_id >= 0)
954 base_fontset = FONTSET_FROM_ID (base_fontset_id);
955 if (!BASE_FONTSET_P (base_fontset))
956 base_fontset = FONTSET_BASE (base_fontset);
957 if (! BASE_FONTSET_P (base_fontset))
958 abort ();
960 else
961 base_fontset = Vdefault_fontset;
963 fontset = make_fontset (frame, Qnil, base_fontset);
964 return XINT (FONTSET_ID (fontset));
969 /* Cache data used by fontset_pattern_regexp. The car part is a
970 pattern string containing at least one wild card, the cdr part is
971 the corresponding regular expression. */
972 static Lisp_Object Vcached_fontset_data;
974 #define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
975 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
977 /* If fontset name PATTERN contains any wild card, return regular
978 expression corresponding to PATTERN. */
980 static Lisp_Object
981 fontset_pattern_regexp (pattern)
982 Lisp_Object pattern;
984 if (!index ((char *) SDATA (pattern), '*')
985 && !index ((char *) SDATA (pattern), '?'))
986 /* PATTERN does not contain any wild cards. */
987 return Qnil;
989 if (!CONSP (Vcached_fontset_data)
990 || strcmp ((char *) SDATA (pattern), CACHED_FONTSET_NAME))
992 /* We must at first update the cached data. */
993 unsigned char *regex, *p0, *p1;
994 int ndashes = 0, nstars = 0, nescs = 0;
996 for (p0 = SDATA (pattern); *p0; p0++)
998 if (*p0 == '-')
999 ndashes++;
1000 else if (*p0 == '*')
1001 nstars++;
1002 else if (*p0 == '['
1003 || *p0 == '.' || *p0 == '\\'
1004 || *p0 == '+' || *p0 == '^'
1005 || *p0 == '$')
1006 nescs++;
1009 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1010 we convert "*" to "[^-]*" which is much faster in regular
1011 expression matching. */
1012 if (ndashes < 14)
1013 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 2 * nstars + 2 * nescs + 1);
1014 else
1015 p1 = regex = (unsigned char *) alloca (SBYTES (pattern) + 5 * nstars + 2 * nescs + 1);
1017 *p1++ = '^';
1018 for (p0 = SDATA (pattern); *p0; p0++)
1020 if (*p0 == '*')
1022 if (ndashes < 14)
1023 *p1++ = '.';
1024 else
1025 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1026 *p1++ = '*';
1028 else if (*p0 == '?')
1029 *p1++ = '.';
1030 else if (*p0 == '['
1031 || *p0 == '.' || *p0 == '\\'
1032 || *p0 == '+' || *p0 == '^'
1033 || *p0 == '$')
1034 *p1++ = '\\', *p1++ = *p0;
1035 else
1036 *p1++ = *p0;
1038 *p1++ = '$';
1039 *p1++ = 0;
1041 Vcached_fontset_data = Fcons (build_string ((char *) SDATA (pattern)),
1042 build_string ((char *) regex));
1045 return CACHED_FONTSET_REGEX;
1048 /* Return ID of the base fontset named NAME. If there's no such
1049 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1050 0: pattern containing '*' and '?' as wildcards
1051 1: regular expression
1052 2: literal fontset name
1056 fs_query_fontset (name, name_pattern)
1057 Lisp_Object name;
1058 int name_pattern;
1060 Lisp_Object tem;
1061 int i;
1063 name = Fdowncase (name);
1064 if (name_pattern != 1)
1066 tem = Frassoc (name, Vfontset_alias_alist);
1067 if (NILP (tem))
1068 tem = Fassoc (name, Vfontset_alias_alist);
1069 if (CONSP (tem) && STRINGP (XCAR (tem)))
1070 name = XCAR (tem);
1071 else if (name_pattern == 0)
1073 tem = fontset_pattern_regexp (name);
1074 if (STRINGP (tem))
1076 name = tem;
1077 name_pattern = 1;
1082 for (i = 0; i < ASIZE (Vfontset_table); i++)
1084 Lisp_Object fontset, this_name;
1086 fontset = FONTSET_FROM_ID (i);
1087 if (NILP (fontset)
1088 || !BASE_FONTSET_P (fontset))
1089 continue;
1091 this_name = FONTSET_NAME (fontset);
1092 if (name_pattern == 1
1093 ? fast_string_match_ignore_case (name, this_name) >= 0
1094 : !xstrcasecmp (SDATA (name), SDATA (this_name)))
1095 return i;
1097 return -1;
1101 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1102 doc: /* Return the name of a fontset that matches PATTERN.
1103 The value is nil if there is no matching fontset.
1104 PATTERN can contain `*' or `?' as a wildcard
1105 just as X font name matching algorithm allows.
1106 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1107 (pattern, regexpp)
1108 Lisp_Object pattern, regexpp;
1110 Lisp_Object fontset;
1111 int id;
1113 (*check_window_system_func) ();
1115 CHECK_STRING (pattern);
1117 if (SCHARS (pattern) == 0)
1118 return Qnil;
1120 id = fs_query_fontset (pattern, !NILP (regexpp));
1121 if (id < 0)
1122 return Qnil;
1124 fontset = FONTSET_FROM_ID (id);
1125 return FONTSET_NAME (fontset);
1128 /* Return a list of base fontset names matching PATTERN on frame F. */
1130 Lisp_Object
1131 list_fontsets (f, pattern, size)
1132 FRAME_PTR f;
1133 Lisp_Object pattern;
1134 int size;
1136 Lisp_Object frame, regexp, val;
1137 int id;
1139 XSETFRAME (frame, f);
1141 regexp = fontset_pattern_regexp (pattern);
1142 val = Qnil;
1144 for (id = 0; id < ASIZE (Vfontset_table); id++)
1146 Lisp_Object fontset, name;
1148 fontset = FONTSET_FROM_ID (id);
1149 if (NILP (fontset)
1150 || !BASE_FONTSET_P (fontset)
1151 || !EQ (frame, FONTSET_FRAME (fontset)))
1152 continue;
1153 name = FONTSET_NAME (fontset);
1155 if (STRINGP (regexp)
1156 ? (fast_string_match (regexp, name) < 0)
1157 : strcmp ((char *) SDATA (pattern), (char *) SDATA (name)))
1158 continue;
1160 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1163 return val;
1167 /* Free all realized fontsets whose base fontset is BASE. */
1169 static void
1170 free_realized_fontsets (base)
1171 Lisp_Object base;
1173 int id;
1175 #if 0
1176 /* For the moment, this doesn't work because free_realized_face
1177 doesn't remove FACE from a cache. Until we find a solution, we
1178 suppress this code, and simply use Fclear_face_cache even though
1179 that is not efficient. */
1180 BLOCK_INPUT;
1181 for (id = 0; id < ASIZE (Vfontset_table); id++)
1183 Lisp_Object this = AREF (Vfontset_table, id);
1185 if (EQ (FONTSET_BASE (this), base))
1187 Lisp_Object tail;
1189 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1190 tail = XCDR (tail))
1192 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
1193 int face_id = XINT (XCDR (XCAR (tail)));
1194 struct face *face = FACE_FROM_ID (f, face_id);
1196 /* Face THIS itself is also freed by the following call. */
1197 free_realized_face (f, face);
1201 UNBLOCK_INPUT;
1202 #else /* not 0 */
1203 /* But, we don't have to call Fclear_face_cache if no fontset has
1204 been realized from BASE. */
1205 for (id = 0; id < ASIZE (Vfontset_table); id++)
1207 Lisp_Object this = AREF (Vfontset_table, id);
1209 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1211 Fclear_face_cache (Qt);
1212 break;
1215 #endif /* not 0 */
1219 /* Check validity of NAME as a fontset name and return the
1220 corresponding fontset. If not valid, signal an error.
1221 If NAME is t, return Vdefault_fontset. */
1223 static Lisp_Object
1224 check_fontset_name (name)
1225 Lisp_Object name;
1227 int id;
1229 if (EQ (name, Qt))
1230 return Vdefault_fontset;
1232 CHECK_STRING (name);
1233 /* First try NAME as literal. */
1234 id = fs_query_fontset (name, 2);
1235 if (id < 0)
1236 /* For backward compatibility, try again NAME as pattern. */
1237 id = fs_query_fontset (name, 0);
1238 if (id < 0)
1239 error ("Fontset `%s' does not exist", SDATA (name));
1240 return FONTSET_FROM_ID (id);
1243 static void
1244 accumulate_script_ranges (arg, range, val)
1245 Lisp_Object arg, range, val;
1247 if (EQ (XCAR (arg), val))
1249 if (CONSP (range))
1250 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1251 else
1252 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1257 /* Return an ASCII font name generated from fontset name NAME and
1258 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1260 static INLINE Lisp_Object
1261 generate_ascii_font_name (name, ascii_spec)
1262 Lisp_Object name, ascii_spec;
1264 Lisp_Object font_spec = Ffont_spec (0, NULL);
1265 int i;
1266 char xlfd[256];
1268 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
1269 error ("Not an XLFD font name: %s", SDATA (name));
1270 for (i = FONT_FOUNDRY_INDEX; i < FONT_EXTRA_INDEX; i++)
1271 if (! NILP (AREF (ascii_spec, i)))
1272 ASET (font_spec, i, AREF (ascii_spec, i));
1273 i = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1274 if (i < 0)
1275 error ("Not an XLFD font name: %s", SDATA (name));
1276 return make_unibyte_string (xlfd, i);
1279 /* Variables referred in set_fontset_font. They are set before
1280 map_charset_chars is called in Fset_fontset_font. */
1281 static Lisp_Object font_def_arg, add_arg;
1282 static int from_arg, to_arg;
1284 /* Callback function for map_charset_chars in Fset_fontset_font. In
1285 FONTSET, set font_def_arg in a fashion specified by add_arg for
1286 characters in RANGE while ignoring the range between from_arg and
1287 to_arg. */
1289 static void
1290 set_fontset_font (fontset, range)
1291 Lisp_Object fontset, range;
1293 if (from_arg < to_arg)
1295 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1297 if (from < from_arg)
1299 if (to > to_arg)
1301 Lisp_Object range2;
1303 range2 = Fcons (make_number (to_arg), XCDR (range));
1304 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1305 to = to_arg;
1307 if (to > from_arg)
1308 range = Fcons (XCAR (range), make_number (from_arg));
1310 else if (to <= to_arg)
1311 return;
1312 else
1314 if (from < to_arg)
1315 range = Fcons (make_number (to_arg), XCDR (range));
1318 FONTSET_ADD (fontset, range, font_def_arg, add_arg);
1321 extern Lisp_Object QCfamily, QCregistry;
1323 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1324 doc: /*
1325 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1327 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1328 In that case, use FONT-SPEC for all characters in the range FROM and
1329 TO (inclusive).
1331 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1332 all characters that belong to the script.
1334 TARGET may be a charset. In that case, use FONT-SPEC for all
1335 characters in the charset.
1337 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1338 that no FONT-SPEC is specified.
1340 FONT-SPEC may one of these:
1341 * A font-spec object made by the function `font-spec' (which see).
1342 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1343 REGISTRY is a font registry name. FAMILY may contain foundry
1344 name, and REGISTRY may contain encoding name.
1345 * A font name string.
1346 * nil, which explicitly specifies that there's no font for TARGET.
1348 Optional 4th argument FRAME, if non-nil, is a frame. This argument is
1349 kept for backward compatibility and has no meaning.
1351 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1352 to the font specifications for TARGET previously set. If it is
1353 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1354 appended. By default, FONT-SPEC overrides the previous settings. */)
1355 (name, target, font_spec, frame, add)
1356 Lisp_Object name, target, font_spec, frame, add;
1358 Lisp_Object fontset;
1359 Lisp_Object font_def, registry, family;
1360 Lisp_Object range_list;
1361 struct charset *charset = NULL;
1363 fontset = check_fontset_name (name);
1365 /* The arg FRAME is kept for backward compatibility. We only check
1366 the validity. */
1367 if (!NILP (frame))
1368 CHECK_LIVE_FRAME (frame);
1370 if (CONSP (font_spec))
1372 Lisp_Object spec = Ffont_spec (0, NULL);
1374 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1375 font_spec = spec;
1377 else if (STRINGP (font_spec))
1379 Lisp_Object args[2];
1380 extern Lisp_Object QCname;
1382 args[0] = QCname;
1383 args[1] = font_spec;
1384 font_spec = Ffont_spec (2, args);
1386 else if (! NILP (font_spec) && ! FONT_SPEC_P (font_spec))
1387 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1389 if (! NILP (font_spec))
1391 Lisp_Object encoding, repertory;
1393 family = AREF (font_spec, FONT_FAMILY_INDEX);
1394 if (! NILP (family) )
1395 family = SYMBOL_NAME (family);
1396 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1397 if (! NILP (registry))
1398 registry = Fdowncase (SYMBOL_NAME (registry));
1399 encoding = find_font_encoding (concat3 (family, build_string ("-"),
1400 registry));
1401 if (NILP (encoding))
1402 encoding = Qascii;
1404 if (SYMBOLP (encoding))
1406 CHECK_CHARSET (encoding);
1407 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1409 else
1411 repertory = XCDR (encoding);
1412 encoding = XCAR (encoding);
1413 CHECK_CHARSET (encoding);
1414 encoding = CHARSET_SYMBOL_ID (encoding);
1415 if (! NILP (repertory) && SYMBOLP (repertory))
1417 CHECK_CHARSET (repertory);
1418 repertory = CHARSET_SYMBOL_ID (repertory);
1421 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1423 else
1424 font_def = Qnil;
1426 if (CHARACTERP (target))
1427 range_list = Fcons (Fcons (target, target), Qnil);
1428 else if (CONSP (target))
1430 Lisp_Object from, to;
1432 from = Fcar (target);
1433 to = Fcdr (target);
1434 CHECK_CHARACTER (from);
1435 CHECK_CHARACTER (to);
1436 range_list = Fcons (target, Qnil);
1438 else if (SYMBOLP (target) && !NILP (target))
1440 Lisp_Object script_list;
1441 Lisp_Object val;
1443 range_list = Qnil;
1444 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1445 if (! NILP (Fmemq (target, script_list)))
1447 val = Fcons (target, Qnil);
1448 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1449 val);
1450 range_list = XCDR (val);
1451 if (EQ (target, Qlatin) && NILP (FONTSET_ASCII (fontset)))
1453 if (VECTORP (font_spec))
1454 val = generate_ascii_font_name (FONTSET_NAME (fontset),
1455 font_spec);
1456 else
1457 val = font_spec;
1458 FONTSET_ASCII (fontset) = val;
1461 if (CHARSETP (target))
1463 if (EQ (target, Qascii) && NILP (FONTSET_ASCII (fontset)))
1465 if (VECTORP (font_spec))
1466 font_spec = generate_ascii_font_name (FONTSET_NAME (fontset),
1467 font_spec);
1468 FONTSET_ASCII (fontset) = font_spec;
1469 range_list = Fcons (Fcons (make_number (0), make_number (127)),
1470 Qnil);
1472 else
1474 CHECK_CHARSET_GET_CHARSET (target, charset);
1477 else if (NILP (range_list))
1478 error ("Invalid script or charset name: %s",
1479 SDATA (SYMBOL_NAME (target)));
1481 else if (NILP (target))
1482 range_list = Fcons (Qnil, Qnil);
1483 else
1484 error ("Invalid target for setting a font");
1487 if (charset)
1489 font_def_arg = font_def;
1490 add_arg = add;
1491 if (NILP (range_list))
1492 from_arg = to_arg = 0;
1493 else
1494 from_arg = XINT (XCAR (XCAR (range_list))),
1495 to_arg = XINT (XCDR (XCAR (range_list)));
1497 map_charset_chars (set_fontset_font, Qnil, fontset, charset,
1498 CHARSET_MIN_CODE (charset),
1499 CHARSET_MAX_CODE (charset));
1501 for (; CONSP (range_list); range_list = XCDR (range_list))
1502 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1504 /* Free all realized fontsets whose base is FONTSET. This way, the
1505 specified character(s) are surely redisplayed by a correct
1506 font. */
1507 free_realized_fontsets (fontset);
1509 return Qnil;
1513 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1514 doc: /* Create a new fontset NAME from font information in FONTLIST.
1516 FONTLIST is an alist of scripts vs the corresponding font specification list.
1517 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1518 character of SCRIPT is displayed by a font that matches one of
1519 FONT-SPEC.
1521 SCRIPT is a symbol that appears in the first extra slot of the
1522 char-table `char-script-table'.
1524 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1525 `set-fontset-font' for the meaning. */)
1526 (name, fontlist)
1527 Lisp_Object name, fontlist;
1529 Lisp_Object fontset;
1530 int id;
1532 CHECK_STRING (name);
1533 CHECK_LIST (fontlist);
1535 name = Fdowncase (name);
1536 id = fs_query_fontset (name, 0);
1537 if (id < 0)
1539 Lisp_Object font_spec = Ffont_spec (0, NULL);
1540 Lisp_Object short_name;
1541 char xlfd[256];
1542 int len;
1544 if (font_parse_xlfd ((char *) SDATA (name), font_spec) < 0)
1545 error ("Fontset name must be in XLFD format");
1546 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1547 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1548 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1549 error ("Registry field of fontset name must be \"fontset-*\"");
1550 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1551 Vfontset_alias_alist);
1552 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1553 fontset = make_fontset (Qnil, name, Qnil);
1554 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1555 if (len < 0)
1556 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1557 FONTSET_ASCII (fontset) = make_unibyte_string (xlfd, len);
1559 else
1561 fontset = FONTSET_FROM_ID (id);;
1562 free_realized_fontsets (fontset);
1563 Fset_char_table_range (fontset, Qt, Qnil);
1566 for (; ! NILP (fontlist); fontlist = Fcdr (fontlist))
1568 Lisp_Object elt, script;
1570 elt = Fcar (fontlist);
1571 script = Fcar (elt);
1572 elt = Fcdr (elt);
1573 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1574 for (; CONSP (elt); elt = XCDR (elt))
1575 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1576 else
1577 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1579 return name;
1583 /* Alist of automatically created fontsets. Each element is a cons
1584 (FONT-SPEC . FONTSET-ID). */
1585 static Lisp_Object auto_fontset_alist;
1588 fontset_from_font (font_object)
1589 Lisp_Object font_object;
1591 Lisp_Object font_name = font_get_name (font_object);
1592 Lisp_Object font_spec = Fcopy_font_spec (font_object);
1593 Lisp_Object fontset_spec, alias, name, fontset;
1594 Lisp_Object val;
1595 int i;
1597 val = assoc_no_quit (font_spec, auto_fontset_alist);
1598 if (CONSP (val))
1599 return XINT (FONTSET_ID (XCDR (val)));
1600 if (NILP (auto_fontset_alist))
1601 alias = intern ("fontset-startup");
1602 else
1604 char temp[32];
1605 int len = XINT (Flength (auto_fontset_alist));
1607 sprintf (temp, "fontset-auto%d", len);
1608 alias = intern (temp);
1610 fontset_spec = Fcopy_font_spec (font_spec);
1611 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1612 name = Ffont_xlfd_name (fontset_spec, Qnil);
1613 if (NILP (name))
1614 abort ();
1615 fontset = make_fontset (Qnil, name, Qnil);
1616 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1617 Vfontset_alias_alist);
1618 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1619 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1620 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1621 FONTSET_ASCII (fontset) = font_name;
1622 font_spec = Fcopy_font_spec (font_spec);
1623 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso10646_1);
1624 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
1625 ASET (font_spec, i, Qnil);
1626 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1627 return XINT (FONTSET_ID (fontset));
1630 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1631 FONT-OBJECT is the font for the character at POSITION in the current
1632 buffer. This is computed from all the text properties and overlays
1633 that apply to POSITION. POSTION may be nil, in which case,
1634 FONT-SPEC is the font for displaying the character CH with the
1635 default face.
1637 GLYPH-CODE is the glyph code in the font to use for the character.
1639 If the 2nd optional arg CH is non-nil, it is a character to check
1640 the font instead of the character at POSITION.
1642 It returns nil in the following cases:
1644 (1) The window system doesn't have a font for the character (thus
1645 it is displayed by an empty box).
1647 (2) The character code is invalid.
1649 (3) If POSITION is not nil, and the current buffer is not displayed
1650 in any window.
1652 In addition, the returned font name may not take into account of
1653 such redisplay engine hooks as what used in jit-lock-mode if
1654 POSITION is currently not visible. */
1657 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1658 doc: /* For internal use only. */)
1659 (position, ch)
1660 Lisp_Object position, ch;
1662 EMACS_INT pos, pos_byte, dummy;
1663 int face_id;
1664 int c;
1665 struct frame *f;
1666 struct face *face;
1667 int cs_id;
1669 if (NILP (position))
1671 CHECK_CHARACTER (ch);
1672 c = XINT (ch);
1673 f = XFRAME (selected_frame);
1674 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1675 pos = -1;
1676 cs_id = -1;
1678 else
1680 Lisp_Object window, charset;
1681 struct window *w;
1683 CHECK_NUMBER_COERCE_MARKER (position);
1684 pos = XINT (position);
1685 if (pos < BEGV || pos >= ZV)
1686 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1687 pos_byte = CHAR_TO_BYTE (pos);
1688 if (NILP (ch))
1689 c = FETCH_CHAR (pos_byte);
1690 else
1692 CHECK_NATNUM (ch);
1693 c = XINT (ch);
1695 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1696 if (NILP (window))
1697 return Qnil;
1698 w = XWINDOW (window);
1699 f = XFRAME (w->frame);
1700 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
1701 charset = Fget_char_property (position, Qcharset, Qnil);
1702 if (CHARSETP (charset))
1703 cs_id = XINT (CHARSET_SYMBOL_ID (charset));
1704 else
1705 cs_id = -1;
1707 if (! CHAR_VALID_P (c, 0))
1708 return Qnil;
1709 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1710 face = FACE_FROM_ID (f, face_id);
1711 if (face->font)
1713 unsigned code = face->font->driver->encode_char (face->font, c);
1714 Lisp_Object font_object;
1715 /* Assignment to EMACS_INT stops GCC whining about limited range
1716 of data type. */
1717 EMACS_INT cod = code;
1719 if (code == FONT_INVALID_CODE)
1720 return Qnil;
1721 XSETFONT (font_object, face->font);
1722 if (cod <= MOST_POSITIVE_FIXNUM)
1723 return Fcons (font_object, make_number (code));
1724 return Fcons (font_object, Fcons (make_number (code >> 16),
1725 make_number (code & 0xFFFF)));
1727 return Qnil;
1731 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1732 doc: /* Return information about a fontset FONTSET on frame FRAME.
1733 The value is a char-table whose elements have this form:
1735 ((FONT-PATTERN OPENED-FONT ...) ...)
1737 FONT-PATTERN is a vector:
1739 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
1741 or a string of font name pattern.
1743 OPENED-FONT is a name of a font actually opened.
1745 The char-table has one extra slot. The value is a char-table
1746 containing the information about the derived fonts from the default
1747 fontset. The format is the same as above. */)
1748 (fontset, frame)
1749 Lisp_Object fontset, frame;
1751 FRAME_PTR f;
1752 Lisp_Object *realized[2], fontsets[2], tables[2];
1753 Lisp_Object val, elt;
1754 int c, i, j, k;
1756 (*check_window_system_func) ();
1758 fontset = check_fontset_name (fontset);
1760 if (NILP (frame))
1761 frame = selected_frame;
1762 CHECK_LIVE_FRAME (frame);
1763 f = XFRAME (frame);
1765 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1766 in the table `realized'. */
1767 realized[0] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1768 * ASIZE (Vfontset_table));
1769 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1771 elt = FONTSET_FROM_ID (i);
1772 if (!NILP (elt)
1773 && EQ (FONTSET_BASE (elt), fontset)
1774 && EQ (FONTSET_FRAME (elt), frame))
1775 realized[0][j++] = elt;
1777 realized[0][j] = Qnil;
1779 realized[1] = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1780 * ASIZE (Vfontset_table));
1781 for (i = j = 0; ! NILP (realized[0][i]); i++)
1783 elt = FONTSET_DEFAULT (realized[0][i]);
1784 if (! NILP (elt))
1785 realized[1][j++] = elt;
1787 realized[1][j] = Qnil;
1789 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1790 tables[1] = Fmake_char_table (Qnil, Qnil);
1791 XCHAR_TABLE (tables[0])->extras[0] = tables[1];
1792 fontsets[0] = fontset;
1793 fontsets[1] = Vdefault_fontset;
1795 /* Accumulate information of the fontset in TABLE. The format of
1796 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1797 for (k = 0; k <= 1; k++)
1799 for (c = 0; c <= MAX_CHAR; )
1801 int from, to;
1803 if (c <= MAX_5_BYTE_CHAR)
1805 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1806 if (to > MAX_5_BYTE_CHAR)
1807 to = MAX_5_BYTE_CHAR;
1809 else
1811 val = FONTSET_FALLBACK (fontsets[k]);
1812 to = MAX_CHAR;
1814 if (VECTORP (val))
1816 Lisp_Object alist;
1818 /* At first, set ALIST to ((FONT-SPEC) ...). */
1819 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
1820 if (! NILP (AREF (val, i)))
1821 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1822 alist);
1823 alist = Fnreverse (alist);
1825 /* Then store opened font names to cdr of each elements. */
1826 for (i = 0; ! NILP (realized[k][i]); i++)
1828 if (c <= MAX_5_BYTE_CHAR)
1829 val = FONTSET_REF (realized[k][i], c);
1830 else
1831 val = FONTSET_FALLBACK (realized[k][i]);
1832 if (! VECTORP (val))
1833 continue;
1834 /* VAL: [int ? [FACE-ID FONT-DEF FONT-OBJECT int] ... ] */
1835 for (j = 2; j < ASIZE (val); j++)
1837 elt = AREF (val, j);
1838 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1840 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1841 Lisp_Object slot, name;
1843 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1844 name = AREF (font_object, FONT_NAME_INDEX);
1845 if (NILP (Fmember (name, XCDR (slot))))
1846 nconc2 (slot, Fcons (name, Qnil));
1851 /* Store ALIST in TBL for characters C..TO. */
1852 if (c <= MAX_5_BYTE_CHAR)
1853 char_table_set_range (tables[k], c, to, alist);
1854 else
1855 XCHAR_TABLE (tables[k])->defalt = alist;
1857 /* At last, change each elements to font names. */
1858 for (; CONSP (alist); alist = XCDR (alist))
1860 elt = XCAR (alist);
1861 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
1864 c = to + 1;
1868 return tables[0];
1872 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
1873 doc: /* Return a font name pattern for character CH in fontset NAME.
1874 If NAME is t, find a pattern in the default fontset.
1876 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
1877 family name and REGISTRY is a font registry name. This is actually
1878 the first font name pattern for CH in the fontset or in the default
1879 fontset.
1881 If the 2nd optional arg ALL is non-nil, return a list of all font name
1882 patterns. */)
1883 (name, ch, all)
1884 Lisp_Object name, ch, all;
1886 int c;
1887 Lisp_Object fontset, elt, list, repertory, val;
1888 int i, j;
1890 fontset = check_fontset_name (name);
1892 CHECK_CHARACTER (ch);
1893 c = XINT (ch);
1894 list = Qnil;
1895 while (1)
1897 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
1898 i++, elt = FONTSET_FALLBACK (fontset))
1899 if (VECTORP (elt))
1900 for (j = 0; j < ASIZE (elt); j++)
1902 val = AREF (elt, j);
1903 repertory = AREF (val, 1);
1904 if (INTEGERP (repertory))
1906 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
1908 if (! CHAR_CHARSET_P (c, charset))
1909 continue;
1911 else if (CHAR_TABLE_P (repertory))
1913 if (NILP (CHAR_TABLE_REF (repertory, c)))
1914 continue;
1916 val = AREF (val, 0);
1917 val = Fcons (AREF (val, 0), AREF (val, 5));
1918 if (NILP (all))
1919 return val;
1920 list = Fcons (val, list);
1922 if (EQ (fontset, Vdefault_fontset))
1923 break;
1924 fontset = Vdefault_fontset;
1926 return (Fnreverse (list));
1929 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
1930 doc: /* Return a list of all defined fontset names. */)
1933 Lisp_Object fontset, list;
1934 int i;
1936 list = Qnil;
1937 for (i = 0; i < ASIZE (Vfontset_table); i++)
1939 fontset = FONTSET_FROM_ID (i);
1940 if (!NILP (fontset)
1941 && BASE_FONTSET_P (fontset))
1942 list = Fcons (FONTSET_NAME (fontset), list);
1945 return list;
1949 #ifdef FONTSET_DEBUG
1951 Lisp_Object
1952 dump_fontset (fontset)
1953 Lisp_Object fontset;
1955 Lisp_Object vec;
1957 vec = Fmake_vector (make_number (3), Qnil);
1958 ASET (vec, 0, FONTSET_ID (fontset));
1960 if (BASE_FONTSET_P (fontset))
1962 ASET (vec, 1, FONTSET_NAME (fontset));
1964 else
1966 Lisp_Object frame;
1968 frame = FONTSET_FRAME (fontset);
1969 if (FRAMEP (frame))
1971 FRAME_PTR f = XFRAME (frame);
1973 if (FRAME_LIVE_P (f))
1974 ASET (vec, 1,
1975 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), f->name));
1976 else
1977 ASET (vec, 1,
1978 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
1980 if (!NILP (FONTSET_DEFAULT (fontset)))
1981 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
1983 return vec;
1986 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
1987 doc: /* Return a brief summary of all fontsets for debug use. */)
1990 Lisp_Object val;
1991 int i;
1993 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
1994 if (! NILP (AREF (Vfontset_table, i)))
1995 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
1996 return (Fnreverse (val));
1998 #endif /* FONTSET_DEBUG */
2000 void
2001 syms_of_fontset ()
2003 DEFSYM (Qfontset, "fontset");
2004 Fput (Qfontset, Qchar_table_extra_slots, make_number (9));
2005 DEFSYM (Qfontset_info, "fontset-info");
2006 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2008 DEFSYM (Qprepend, "prepend");
2009 DEFSYM (Qappend, "append");
2010 DEFSYM (Qlatin, "latin");
2012 Vcached_fontset_data = Qnil;
2013 staticpro (&Vcached_fontset_data);
2015 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2016 staticpro (&Vfontset_table);
2018 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2019 staticpro (&Vdefault_fontset);
2020 FONTSET_ID (Vdefault_fontset) = make_number (0);
2021 FONTSET_NAME (Vdefault_fontset)
2022 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2023 ASET (Vfontset_table, 0, Vdefault_fontset);
2024 next_fontset_id = 1;
2026 auto_fontset_alist = Qnil;
2027 staticpro (&auto_fontset_alist);
2029 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist,
2030 doc: /*
2031 Alist of charsets vs the charsets to determine the preferred font encoding.
2032 Each element looks like (CHARSET . ENCODING-CHARSET),
2033 where ENCODING-CHARSET is a charset registered in the variable
2034 `font-encoding-alist' as ENCODING.
2036 When a text has a property `charset' and the value is CHARSET, a font
2037 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2038 Vfont_encoding_charset_alist = Qnil;
2040 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
2041 doc: /*
2042 Char table of characters whose ascent values should be ignored.
2043 If an entry for a character is non-nil, the ascent value of the glyph
2044 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2046 This affects how a composite character which contains
2047 such a character is displayed on screen. */);
2048 Vuse_default_ascent = Qnil;
2050 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
2051 doc: /*
2052 Char table of characters which are not composed relatively.
2053 If an entry for a character is non-nil, a composition sequence
2054 which contains that character is displayed so that
2055 the glyph of that character is put without considering
2056 an ascent and descent value of a previous character. */);
2057 Vignore_relative_composition = Qnil;
2059 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
2060 doc: /* Alist of fontname vs list of the alternate fontnames.
2061 When a specified font name is not found, the corresponding
2062 alternate fontnames (if any) are tried instead. */);
2063 Valternate_fontname_alist = Qnil;
2065 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
2066 doc: /* Alist of fontset names vs the aliases. */);
2067 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
2068 build_string ("fontset-default")),
2069 Qnil);
2071 DEFVAR_LISP ("vertical-centering-font-regexp",
2072 &Vvertical_centering_font_regexp,
2073 doc: /* *Regexp matching font names that require vertical centering on display.
2074 When a character is displayed with such fonts, the character is displayed
2075 at the vertical center of lines. */);
2076 Vvertical_centering_font_regexp = Qnil;
2078 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2079 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2080 Votf_script_alist = Qnil;
2082 defsubr (&Squery_fontset);
2083 defsubr (&Snew_fontset);
2084 defsubr (&Sset_fontset_font);
2085 defsubr (&Sinternal_char_font);
2086 defsubr (&Sfontset_info);
2087 defsubr (&Sfontset_font);
2088 defsubr (&Sfontset_list);
2089 #ifdef FONTSET_DEBUG
2090 defsubr (&Sfontset_list_all);
2091 #endif
2094 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2095 (do not change this comment) */