2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009
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 */
33 #include "blockinput.h"
35 #include "character.h"
40 #include "dispextern.h"
41 #include "intervals.h"
53 #include "termhooks.h"
59 #define xassert(X) do {if (!(X)) abort ();} while (0)
62 #else /* not FONTSET_DEBUG */
63 #define xassert(X) (void) 0
64 #endif /* not FONTSET_DEBUG */
66 EXFUN (Fclear_face_cache
, 1);
70 A fontset is a collection of font related information to give
71 similar appearance (style, etc) of characters. A fontset has two
72 roles. One is to use for the frame parameter `font' as if it is an
73 ASCII font. In that case, Emacs uses the font specified for
74 `ascii' script for the frame's default font.
76 Another role, the more important one, is to provide information
77 about which font to use for each non-ASCII character.
79 There are two kinds of fontsets; base and realized. A base fontset
80 is created by `new-fontset' from Emacs Lisp explicitly. A realized
81 fontset is created implicitly when a face is realized for ASCII
82 characters. A face is also realized for non-ASCII characters based
83 on an ASCII face. All of non-ASCII faces based on the same ASCII
84 face share the same realized fontset.
86 A fontset object is implemented by a char-table whose default value
87 and parent are always nil.
89 An element of a base fontset is a vector of FONT-DEFs which itself
90 is a vector [ FONT-SPEC ENCODING REPERTORY ].
92 An element of a realized fontset is nil, t, 0, or a vector of this
95 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
96 RFONT-DEF0 RFONT-DEF1 ... ]
98 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
100 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
102 RFONT-DEFn are automatically reordered by the current charset
105 The value nil means that we have not yet generated the above vector
106 from the base of the fontset.
108 The value t means that no font is available for the corresponding
111 The value 0 means that no font is available for the corresponding
112 range of characters in this fontset, but may be available in the
116 A fontset has 9 extra slots.
118 The 1st slot: the ID number of the fontset
121 base: the name of the fontset
126 realized: the base fontset
130 realized: the frame that the fontset belongs to
133 base: the font name for ASCII characters
138 realized: the ID number of a face to use for characters that
139 has no font in a realized fontset.
143 realized: Alist of font index vs the corresponding repertory
148 realized: If the base is not the default fontset, a fontset
149 realized from the default fontset, else nil.
152 base: Same as element value (but for fallback fonts).
155 All fontsets are recorded in the vector Vfontset_table.
160 There's a special base fontset named `default fontset' which
161 defines the default font specifications. When a base fontset
162 doesn't specify a font for a specific character, the corresponding
163 value in the default fontset is used.
165 The parent of a realized fontset created for such a face that has
166 no fontset is the default fontset.
169 These structures are hidden from the other codes than this file.
170 The other codes handle fontsets only by their ID numbers. They
171 usually use the variable name `fontset' for IDs. But, in this
172 file, we always use varialbe name `id' for IDs, and name `fontset'
173 for an actual fontset object, i.e., char-table.
177 /********** VARIABLES and FUNCTION PROTOTYPES **********/
179 extern Lisp_Object Qfont
;
180 static Lisp_Object Qfontset
;
181 static Lisp_Object Qfontset_info
;
182 static Lisp_Object Qprepend
, Qappend
;
183 static Lisp_Object Qlatin
;
185 /* Vector containing all fontsets. */
186 static Lisp_Object Vfontset_table
;
188 /* Next possibly free fontset ID. Usually this keeps the minimum
189 fontset ID not yet used. */
190 static int next_fontset_id
;
192 /* The default fontset. This gives default FAMILY and REGISTRY of
193 font for each character. */
194 static Lisp_Object Vdefault_fontset
;
196 Lisp_Object Vfont_encoding_charset_alist
;
197 Lisp_Object Vuse_default_ascent
;
198 Lisp_Object Vignore_relative_composition
;
199 Lisp_Object Valternate_fontname_alist
;
200 Lisp_Object Vfontset_alias_alist
;
201 Lisp_Object Vvertical_centering_font_regexp
;
202 Lisp_Object Votf_script_alist
;
204 /* Check if any window system is used now. */
205 void (*check_window_system_func
) P_ ((void));
208 /* Prototype declarations for static functions. */
209 static Lisp_Object fontset_add
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
211 static Lisp_Object fontset_find_font
P_ ((Lisp_Object
, int, struct face
*,
213 static void reorder_font_vector
P_ ((Lisp_Object
, struct font
*));
214 static Lisp_Object fontset_font
P_ ((Lisp_Object
, int, struct face
*, int));
215 static Lisp_Object make_fontset
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
216 static Lisp_Object fontset_pattern_regexp
P_ ((Lisp_Object
));
217 static void accumulate_script_ranges
P_ ((Lisp_Object
, Lisp_Object
,
219 Lisp_Object find_font_encoding
P_ ((Lisp_Object
));
221 static void set_fontset_font
P_ ((Lisp_Object
, Lisp_Object
));
225 /* Return 1 if ID is a valid fontset id, else return 0. */
228 fontset_id_valid_p (id
)
231 return (id
>= 0 && id
< ASIZE (Vfontset_table
) - 1);
238 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
240 /* Return the fontset with ID. No check of ID's validness. */
241 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
243 /* Macros to access special values of FONTSET. */
244 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
246 /* Macros to access special values of (base) FONTSET. */
247 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
248 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
249 #define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5]
251 /* Macros to access special values of (realized) FONTSET. */
252 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
253 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
254 #define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4]
255 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
256 #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
257 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
259 /* For both base and realized fontset. */
260 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
262 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
265 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
266 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
268 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
269 ASET ((font_def), 1, encoding); \
270 ASET ((font_def), 2, repertory); \
273 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
274 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
275 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
277 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
278 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
279 ASET ((rfont_def), 0, make_number (face_id))
280 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
281 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
282 #define RFONT_DEF_REPERTORY(rfont_def) FONT_DEF_REPERTORY (AREF (rfont_def, 1))
283 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
284 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
285 ASET ((rfont_def), 2, (object))
286 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
287 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
288 ASET ((rfont_def), 3, make_number (score))
289 #define RFONT_DEF_NEW(rfont_def, font_def) \
291 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
292 ASET ((rfont_def), 1, (font_def)); \
293 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
297 /* Return the element of FONTSET for the character C. If FONTSET is a
298 base fontset other then the default fontset and FONTSET doesn't
299 contain information for C, return the information in the default
302 #define FONTSET_REF(fontset, c) \
303 (EQ (fontset, Vdefault_fontset) \
304 ? CHAR_TABLE_REF (fontset, c) \
305 : fontset_ref ((fontset), (c)))
308 fontset_ref (fontset
, c
)
314 elt
= CHAR_TABLE_REF (fontset
, c
);
315 if (NILP (elt
) && ! EQ (fontset
, Vdefault_fontset
)
316 /* Don't check Vdefault_fontset for a realized fontset. */
317 && NILP (FONTSET_BASE (fontset
)))
318 elt
= CHAR_TABLE_REF (Vdefault_fontset
, c
);
322 /* Set elements of FONTSET for characters in RANGE to the value ELT.
323 RANGE is a cons (FROM . TO), where FROM and TO are character codes
324 specifying a range. */
326 #define FONTSET_SET(fontset, range, elt) \
327 Fset_char_table_range ((fontset), (range), (elt))
330 /* Modify the elements of FONTSET for characters in RANGE by replacing
331 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
332 and TO are character codes specifying a range. If ADD is nil,
333 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
336 #define FONTSET_ADD(fontset, range, elt, add) \
339 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
340 : Fset_char_table_range ((fontset), (range), \
341 Fmake_vector (make_number (1), (elt)))) \
342 : fontset_add ((fontset), (range), (elt), (add)))
345 fontset_add (fontset
, range
, elt
, add
)
346 Lisp_Object fontset
, range
, elt
, add
;
349 int idx
= (EQ (add
, Qappend
) ? 0 : 1);
351 args
[1 - idx
] = Fmake_vector (make_number (1), elt
);
355 int from
= XINT (XCAR (range
));
356 int to
= XINT (XCDR (range
));
360 from1
= from
, to1
= to
;
361 args
[idx
] = char_table_ref_and_range (fontset
, from
, &from1
, &to1
);
362 char_table_set_range (fontset
, from
, to1
,
363 NILP (args
[idx
]) ? args
[1 - idx
]
364 : Fvconcat (2, args
));
370 args
[idx
] = FONTSET_FALLBACK (fontset
);
371 FONTSET_FALLBACK (fontset
)
372 = NILP (args
[idx
]) ? args
[1 - idx
] : Fvconcat (2, args
);
378 fontset_compare_rfontdef (val1
, val2
)
379 const void *val1
, *val2
;
381 return (RFONT_DEF_SCORE (*(Lisp_Object
*) val1
)
382 - RFONT_DEF_SCORE (*(Lisp_Object
*) val2
));
385 /* Update FONT-GROUP which has this form:
386 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
387 RFONT-DEF0 RFONT-DEF1 ... ]
388 Reorder RFONT-DEFs according to the current language, and update
389 CHARSET-ORDERED-LIST-TICK.
391 If PREFERRED_FAMILY is not nil, that family has the higher priority
392 if the encoding charsets or languages in font-specs are the same. */
394 extern Lisp_Object
Fassoc_string ();
397 reorder_font_vector (font_group
, font
)
398 Lisp_Object font_group
;
401 Lisp_Object vec
, font_object
;
404 int score_changed
= 0;
407 XSETFONT (font_object
, font
);
411 vec
= XCDR (font_group
);
413 /* Exclude the tailing nil element from the reordering. */
414 if (NILP (AREF (vec
, size
- 1)))
417 for (i
= 0; i
< size
; i
++)
419 Lisp_Object rfont_def
= AREF (vec
, i
);
420 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
421 Lisp_Object font_spec
= FONT_DEF_SPEC (font_def
);
422 int score
= RFONT_DEF_SCORE (rfont_def
) & 0xFF;
424 if (! font_match_p (font_spec
, font_object
))
426 Lisp_Object encoding
= FONT_DEF_ENCODING (font_def
);
428 if (! NILP (encoding
))
432 for (tail
= Vcharset_ordered_list
;
433 ! EQ (tail
, Vcharset_non_preferred_head
) && CONSP (tail
);
434 score
+= 0x100, tail
= XCDR (tail
))
435 if (EQ (encoding
, XCAR (tail
)))
440 Lisp_Object lang
= Ffont_get (font_spec
, QClang
);
443 && ! EQ (lang
, Vcurrent_iso639_language
)
444 && (! CONSP (Vcurrent_iso639_language
)
445 || NILP (Fmemq (lang
, Vcurrent_iso639_language
))))
449 if (RFONT_DEF_SCORE (rfont_def
) != score
)
451 RFONT_DEF_SET_SCORE (rfont_def
, score
);
457 qsort (XVECTOR (vec
)->contents
, size
, sizeof (Lisp_Object
),
458 fontset_compare_rfontdef
);
459 XSETCAR (font_group
, make_number (charset_ordered_list_tick
));
463 fontset_get_font_group (Lisp_Object fontset
, int c
)
465 Lisp_Object font_group
;
466 Lisp_Object base_fontset
;
467 int from
= 0, to
= MAX_CHAR
, i
;
469 xassert (! BASE_FONTSET_P (fontset
));
471 font_group
= CHAR_TABLE_REF (fontset
, c
);
473 font_group
= FONTSET_FALLBACK (fontset
);
474 if (! NILP (font_group
))
476 base_fontset
= FONTSET_BASE (fontset
);
478 font_group
= char_table_ref_and_range (base_fontset
, c
, &from
, &to
);
480 font_group
= FONTSET_FALLBACK (base_fontset
);
481 if (NILP (font_group
))
484 char_table_set_range (fontset
, from
, to
, make_number (0));
487 font_group
= Fcopy_sequence (font_group
);
488 for (i
= 0; i
< ASIZE (font_group
); i
++)
489 if (! NILP (AREF (font_group
, i
)))
491 Lisp_Object rfont_def
;
493 RFONT_DEF_NEW (rfont_def
, AREF (font_group
, i
));
494 /* Remember the original order. */
495 RFONT_DEF_SET_SCORE (rfont_def
, i
);
496 ASET (font_group
, i
, rfont_def
);
498 font_group
= Fcons (make_number (-1), font_group
);
500 char_table_set_range (fontset
, from
, to
, font_group
);
502 FONTSET_FALLBACK (fontset
) = font_group
;
506 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
507 character C. If no font is found, return Qnil if there's a
508 possibility that the default fontset or the fallback font groups
509 have a proper font, and return Qt if not.
511 If a font is found but is not yet opened, open it (if FACE is not
512 NULL) or return Qnil (if FACE is NULL).
514 ID is a charset-id that must be preferred, or -1 meaning no
517 If FALLBACK is nonzero, search only fallback fonts. */
520 fontset_find_font (fontset
, c
, face
, id
, fallback
)
526 Lisp_Object elt
, vec
, font_group
;
527 int i
, charset_matched
= -1;
528 FRAME_PTR f
= (FRAMEP (FONTSET_FRAME (fontset
)))
529 ? XFRAME (selected_frame
) : XFRAME (FONTSET_FRAME (fontset
));
531 font_group
= fontset_get_font_group (fontset
, fallback
? -1 : c
);
532 if (! CONSP (font_group
))
534 vec
= XCDR (font_group
);
535 if (ASIZE (vec
) == 0)
540 if (XINT (XCAR (font_group
)) != charset_ordered_list_tick
)
541 /* We have just created the font-group,
542 or the charset priorities were changed. */
543 reorder_font_vector (font_group
, face
->ascii_face
->font
);
545 /* Find a spec matching with the charset ID to try at
547 for (i
= 0; i
< ASIZE (vec
); i
++)
549 Lisp_Object rfont_def
= AREF (vec
, i
);
550 Lisp_Object repertory
551 = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def
));
553 if (XINT (repertory
) == id
)
561 /* Find the first available font in the vector of RFONT-DEF. */
562 for (i
= 0; i
< ASIZE (vec
); i
++)
564 Lisp_Object font_entity
, font_object
;
566 if (i
== 0 && charset_matched
>= 0)
568 /* Try the element matching with the charset ID at first. */
569 elt
= AREF (vec
, charset_matched
);
570 charset_matched
= -1;
573 else if (i
!= charset_matched
)
579 /* This is a sign of not to try the other fonts. */
581 if (INTEGERP (RFONT_DEF_FACE (elt
))
582 && XINT (RFONT_DEF_FACE (elt
)) < 0)
583 /* We couldn't open this font last time. */
586 font_object
= RFONT_DEF_OBJECT (elt
);
587 if (NILP (font_object
))
589 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (elt
);
592 /* We have not yet opened the font. */
594 font_entity
= font_find_for_lface (f
, face
->lface
,
595 FONT_DEF_SPEC (font_def
), -1);
596 if (NILP (font_entity
))
598 /* Record that no font matches the spec. */
599 RFONT_DEF_SET_FACE (elt
, -1);
602 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
603 FONT_DEF_SPEC (font_def
));
604 if (NILP (font_object
))
606 /* Record that the font is unsable. */
607 RFONT_DEF_SET_FACE (elt
, -1);
610 RFONT_DEF_SET_OBJECT (elt
, font_object
);
613 if (font_has_char (f
, font_object
, c
))
617 /* The following code makes Emacs to find a font for C by fairly
618 exhausitive search. But, that takes long time especially for
621 /* Try to find the different font maching with the current spec
623 font_def
= RFONT_DEF_FONT_DEF (elt
);
624 for (i
++; i
< ASIZE (vec
); i
++)
626 if (! EQ (RFONT_DEF_FONT_DEF (AREF (vec
, i
)), font_def
))
628 if (font_has_char (f
, RFONT_DEF_OBJECT (AREF (vec
, i
)), c
))
629 return AREF (vec
, i
);
631 /* Find an font-entity that support C. */
632 font_entity
= font_find_for_lface (f
, face
->lface
,
633 FONT_DEF_SPEC (font_def
), c
);
634 if (! NILP (font_entity
))
636 Lisp_Object rfont_def
, new_vec
;
639 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
641 RFONT_DEF_NEW (rfont_def
, font_def
);
642 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
643 RFONT_DEF_SET_SCORE (rfont_def
, RFONT_DEF_SCORE (elt
));
644 new_vec
= Fmake_vector (make_number (ASIZE (vec
) + 1), Qnil
);
645 for (j
= 0; j
< i
; j
++)
646 ASET (new_vec
, j
, AREF (vec
, j
));
647 ASET (new_vec
, j
, rfont_def
);
648 for (j
++; j
< ASIZE (new_vec
); j
++)
649 ASET (new_vec
, j
, AREF (vec
, j
- 1));
657 FONTSET_SET (fontset
, make_number (c
), make_number (0));
663 fontset_font (fontset
, c
, face
, id
)
669 Lisp_Object rfont_def
;
670 Lisp_Object base_fontset
;
672 /* Try a font-group of FONTSET. */
673 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 0);
674 if (VECTORP (rfont_def
))
676 if (EQ (rfont_def
, Qt
))
679 /* Try a font-group of the default fontset. */
680 base_fontset
= FONTSET_BASE (fontset
);
681 if (! EQ (base_fontset
, Vdefault_fontset
))
683 if (NILP (FONTSET_DEFAULT (fontset
)))
684 FONTSET_DEFAULT (fontset
)
685 = make_fontset (FONTSET_FRAME (fontset
), Qnil
, Vdefault_fontset
);
686 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 0);
687 if (VECTORP (rfont_def
))
689 if (EQ (rfont_def
, Qt
))
693 /* Try a fallback font-group of FONTSET. */
694 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 1);
695 if (VECTORP (rfont_def
))
697 if (EQ (rfont_def
, Qt
))
700 /* Try a fallback font-group of the default fontset . */
701 if (! EQ (base_fontset
, Vdefault_fontset
))
703 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 1);
704 if (VECTORP (rfont_def
))
708 /* Remember that we have no font for C. */
709 FONTSET_SET (fontset
, make_number (c
), Qt
);
714 /* Return a newly created fontset with NAME. If BASE is nil, make a
715 base fontset. Otherwise make a realized fontset whose base is
719 make_fontset (frame
, name
, base
)
720 Lisp_Object frame
, name
, base
;
723 int size
= ASIZE (Vfontset_table
);
724 int id
= next_fontset_id
;
726 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
727 the next available fontset ID. So it is expected that this loop
728 terminates quickly. In addition, as the last element of
729 Vfontset_table is always nil, we don't have to check the range of
731 while (!NILP (AREF (Vfontset_table
, id
))) id
++;
734 Vfontset_table
= larger_vector (Vfontset_table
, size
+ 32, Qnil
);
736 fontset
= Fmake_char_table (Qfontset
, Qnil
);
738 FONTSET_ID (fontset
) = make_number (id
);
741 FONTSET_NAME (fontset
) = name
;
745 FONTSET_NAME (fontset
) = Qnil
;
746 FONTSET_FRAME (fontset
) = frame
;
747 FONTSET_BASE (fontset
) = base
;
750 ASET (Vfontset_table
, id
, fontset
);
751 next_fontset_id
= id
+ 1;
756 /* Set the ASCII font of the default fontset to FONTNAME if that is
759 set_default_ascii_font (fontname
)
760 Lisp_Object fontname
;
762 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset
)))
764 int id
= fs_query_fontset (fontname
, 2);
767 fontname
= FONTSET_ASCII (FONTSET_FROM_ID (id
));
768 FONTSET_ASCII (Vdefault_fontset
)= fontname
;
773 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
775 /* Return the name of the fontset who has ID. */
783 fontset
= FONTSET_FROM_ID (id
);
784 return FONTSET_NAME (fontset
);
788 /* Return the ASCII font name of the fontset who has ID. */
794 Lisp_Object fontset
, elt
;
796 fontset
= FONTSET_FROM_ID (id
);
797 elt
= FONTSET_ASCII (fontset
);
804 free_realized_fontset (f
, fontset
)
811 for (tail
= FONTSET_OBJLIST (fontset
); CONSP (tail
); tail
= XCDR (tail
))
813 xassert (FONT_OBJECT_P (XCAR (tail
)));
814 font_close_object (f
, XCAR (tail
));
818 /* Free fontset of FACE defined on frame F. Called from
819 free_realized_face. */
822 free_face_fontset (f
, face
)
828 fontset
= FONTSET_FROM_ID (face
->fontset
);
831 xassert (! BASE_FONTSET_P (fontset
));
832 xassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
833 free_realized_fontset (f
, fontset
);
834 ASET (Vfontset_table
, face
->fontset
, Qnil
);
835 if (face
->fontset
< next_fontset_id
)
836 next_fontset_id
= face
->fontset
;
837 if (! NILP (FONTSET_DEFAULT (fontset
)))
839 int id
= XINT (FONTSET_ID (FONTSET_DEFAULT (fontset
)));
841 fontset
= AREF (Vfontset_table
, id
);
842 xassert (!NILP (fontset
) && ! BASE_FONTSET_P (fontset
));
843 xassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
844 free_realized_fontset (f
, fontset
);
845 ASET (Vfontset_table
, id
, Qnil
);
846 if (id
< next_fontset_id
)
847 next_fontset_id
= face
->fontset
;
853 /* Return 1 if FACE is suitable for displaying character C.
854 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
855 when C is not an ASCII character. */
858 face_suitable_for_char_p (face
, c
)
862 Lisp_Object fontset
, rfont_def
;
864 fontset
= FONTSET_FROM_ID (face
->fontset
);
865 rfont_def
= fontset_font (fontset
, c
, NULL
, -1);
866 return (VECTORP (rfont_def
)
867 && INTEGERP (RFONT_DEF_FACE (rfont_def
))
868 && face
->id
== XINT (RFONT_DEF_FACE (rfont_def
)));
872 /* Return ID of face suitable for displaying character C on frame F.
873 FACE must be reazlied for ASCII characters in advance. Called from
874 the macro FACE_FOR_CHAR. */
877 face_for_char (f
, face
, c
, pos
, object
)
883 Lisp_Object fontset
, rfont_def
, charset
;
887 /* If face->fontset is negative (that happens when no font is found
888 for face), just return face->ascii_face because we can't do
889 anything. Perhaps, we should fix the callers to assure
890 that face->fontset is always valid. */
891 if (ASCII_CHAR_P (c
) || face
->fontset
< 0)
892 return face
->ascii_face
->id
;
894 xassert (fontset_id_valid_p (face
->fontset
));
895 fontset
= FONTSET_FROM_ID (face
->fontset
);
896 xassert (!BASE_FONTSET_P (fontset
));
905 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
906 if (CHARSETP (charset
))
910 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
911 if (CONSP (val
) && CHARSETP (XCDR (val
)))
912 charset
= XCDR (val
);
913 id
= XINT (CHARSET_SYMBOL_ID (charset
));
919 font_deferred_log ("font for", Fcons (make_number (c
), charset
), Qnil
);
920 rfont_def
= fontset_font (fontset
, c
, face
, id
);
921 if (VECTORP (rfont_def
))
923 if (INTEGERP (RFONT_DEF_FACE (rfont_def
)))
924 face_id
= XINT (RFONT_DEF_FACE (rfont_def
));
927 Lisp_Object font_object
;
929 font_object
= RFONT_DEF_OBJECT (rfont_def
);
930 face_id
= face_for_font (f
, font_object
, face
);
931 RFONT_DEF_SET_FACE (rfont_def
, face_id
);
936 if (INTEGERP (FONTSET_NOFONT_FACE (fontset
)))
937 face_id
= XINT (FONTSET_NOFONT_FACE (fontset
));
940 face_id
= face_for_font (f
, Qnil
, face
);
941 FONTSET_NOFONT_FACE (fontset
) = make_number (face_id
);
944 xassert (face_id
>= 0);
950 font_for_char (face
, c
, pos
, object
)
955 Lisp_Object fontset
, rfont_def
, charset
;
959 if (ASCII_CHAR_P (c
))
961 Lisp_Object font_object
;
963 XSETFONT (font_object
, face
->ascii_face
->font
);
967 xassert (fontset_id_valid_p (face
->fontset
));
968 fontset
= FONTSET_FROM_ID (face
->fontset
);
969 xassert (!BASE_FONTSET_P (fontset
));
977 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
978 if (CHARSETP (charset
))
982 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
983 if (CONSP (val
) && CHARSETP (XCDR (val
)))
984 charset
= XCDR (val
);
985 id
= XINT (CHARSET_SYMBOL_ID (charset
));
991 font_deferred_log ("font for", Fcons (make_number (c
), charset
), Qnil
);
992 rfont_def
= fontset_font (fontset
, c
, face
, id
);
993 return (VECTORP (rfont_def
)
994 ? RFONT_DEF_OBJECT (rfont_def
)
999 /* Make a realized fontset for ASCII face FACE on frame F from the
1000 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1001 default fontset as the base. Value is the id of the new fontset.
1002 Called from realize_x_face. */
1005 make_fontset_for_ascii_face (f
, base_fontset_id
, face
)
1007 int base_fontset_id
;
1010 Lisp_Object base_fontset
, fontset
, frame
;
1012 XSETFRAME (frame
, f
);
1013 if (base_fontset_id
>= 0)
1015 base_fontset
= FONTSET_FROM_ID (base_fontset_id
);
1016 if (!BASE_FONTSET_P (base_fontset
))
1017 base_fontset
= FONTSET_BASE (base_fontset
);
1018 if (! BASE_FONTSET_P (base_fontset
))
1022 base_fontset
= Vdefault_fontset
;
1024 fontset
= make_fontset (frame
, Qnil
, base_fontset
);
1025 return XINT (FONTSET_ID (fontset
));
1030 /* Cache data used by fontset_pattern_regexp. The car part is a
1031 pattern string containing at least one wild card, the cdr part is
1032 the corresponding regular expression. */
1033 static Lisp_Object Vcached_fontset_data
;
1035 #define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
1036 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1038 /* If fontset name PATTERN contains any wild card, return regular
1039 expression corresponding to PATTERN. */
1042 fontset_pattern_regexp (pattern
)
1043 Lisp_Object pattern
;
1045 if (!index ((char *) SDATA (pattern
), '*')
1046 && !index ((char *) SDATA (pattern
), '?'))
1047 /* PATTERN does not contain any wild cards. */
1050 if (!CONSP (Vcached_fontset_data
)
1051 || strcmp ((char *) SDATA (pattern
), CACHED_FONTSET_NAME
))
1053 /* We must at first update the cached data. */
1054 unsigned char *regex
, *p0
, *p1
;
1055 int ndashes
= 0, nstars
= 0, nescs
= 0;
1057 for (p0
= SDATA (pattern
); *p0
; p0
++)
1061 else if (*p0
== '*')
1064 || *p0
== '.' || *p0
== '\\'
1065 || *p0
== '+' || *p0
== '^'
1070 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1071 we convert "*" to "[^-]*" which is much faster in regular
1072 expression matching. */
1074 p1
= regex
= (unsigned char *) alloca (SBYTES (pattern
) + 2 * nstars
+ 2 * nescs
+ 1);
1076 p1
= regex
= (unsigned char *) alloca (SBYTES (pattern
) + 5 * nstars
+ 2 * nescs
+ 1);
1079 for (p0
= SDATA (pattern
); *p0
; p0
++)
1086 *p1
++ = '[', *p1
++ = '^', *p1
++ = '-', *p1
++ = ']';
1089 else if (*p0
== '?')
1092 || *p0
== '.' || *p0
== '\\'
1093 || *p0
== '+' || *p0
== '^'
1095 *p1
++ = '\\', *p1
++ = *p0
;
1102 Vcached_fontset_data
= Fcons (build_string ((char *) SDATA (pattern
)),
1103 build_string ((char *) regex
));
1106 return CACHED_FONTSET_REGEX
;
1109 /* Return ID of the base fontset named NAME. If there's no such
1110 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1111 0: pattern containing '*' and '?' as wildcards
1112 1: regular expression
1113 2: literal fontset name
1117 fs_query_fontset (name
, name_pattern
)
1124 name
= Fdowncase (name
);
1125 if (name_pattern
!= 1)
1127 tem
= Frassoc (name
, Vfontset_alias_alist
);
1129 tem
= Fassoc (name
, Vfontset_alias_alist
);
1130 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
1132 else if (name_pattern
== 0)
1134 tem
= fontset_pattern_regexp (name
);
1143 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
1145 Lisp_Object fontset
, this_name
;
1147 fontset
= FONTSET_FROM_ID (i
);
1149 || !BASE_FONTSET_P (fontset
))
1152 this_name
= FONTSET_NAME (fontset
);
1153 if (name_pattern
== 1
1154 ? fast_string_match_ignore_case (name
, this_name
) >= 0
1155 : !xstrcasecmp (SDATA (name
), SDATA (this_name
)))
1162 DEFUN ("query-fontset", Fquery_fontset
, Squery_fontset
, 1, 2, 0,
1163 doc
: /* Return the name of a fontset that matches PATTERN.
1164 The value is nil if there is no matching fontset.
1165 PATTERN can contain `*' or `?' as a wildcard
1166 just as X font name matching algorithm allows.
1167 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1169 Lisp_Object pattern
, regexpp
;
1171 Lisp_Object fontset
;
1174 (*check_window_system_func
) ();
1176 CHECK_STRING (pattern
);
1178 if (SCHARS (pattern
) == 0)
1181 id
= fs_query_fontset (pattern
, !NILP (regexpp
));
1185 fontset
= FONTSET_FROM_ID (id
);
1186 return FONTSET_NAME (fontset
);
1189 /* Return a list of base fontset names matching PATTERN on frame F. */
1192 list_fontsets (f
, pattern
, size
)
1194 Lisp_Object pattern
;
1197 Lisp_Object frame
, regexp
, val
;
1200 XSETFRAME (frame
, f
);
1202 regexp
= fontset_pattern_regexp (pattern
);
1205 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1207 Lisp_Object fontset
, name
;
1209 fontset
= FONTSET_FROM_ID (id
);
1211 || !BASE_FONTSET_P (fontset
)
1212 || !EQ (frame
, FONTSET_FRAME (fontset
)))
1214 name
= FONTSET_NAME (fontset
);
1216 if (STRINGP (regexp
)
1217 ? (fast_string_match (regexp
, name
) < 0)
1218 : strcmp ((char *) SDATA (pattern
), (char *) SDATA (name
)))
1221 val
= Fcons (Fcopy_sequence (FONTSET_NAME (fontset
)), val
);
1228 /* Free all realized fontsets whose base fontset is BASE. */
1231 free_realized_fontsets (base
)
1237 /* For the moment, this doesn't work because free_realized_face
1238 doesn't remove FACE from a cache. Until we find a solution, we
1239 suppress this code, and simply use Fclear_face_cache even though
1240 that is not efficient. */
1242 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1244 Lisp_Object
this = AREF (Vfontset_table
, id
);
1246 if (EQ (FONTSET_BASE (this), base
))
1250 for (tail
= FONTSET_FACE_ALIST (this); CONSP (tail
);
1253 FRAME_PTR f
= XFRAME (FONTSET_FRAME (this));
1254 int face_id
= XINT (XCDR (XCAR (tail
)));
1255 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1257 /* Face THIS itself is also freed by the following call. */
1258 free_realized_face (f
, face
);
1264 /* But, we don't have to call Fclear_face_cache if no fontset has
1265 been realized from BASE. */
1266 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1268 Lisp_Object
this = AREF (Vfontset_table
, id
);
1270 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base
))
1272 Fclear_face_cache (Qt
);
1280 /* Check validity of NAME as a fontset name and return the
1281 corresponding fontset. If not valid, signal an error.
1282 If NAME is t, return Vdefault_fontset. */
1285 check_fontset_name (name
)
1291 return Vdefault_fontset
;
1293 CHECK_STRING (name
);
1294 /* First try NAME as literal. */
1295 id
= fs_query_fontset (name
, 2);
1297 /* For backward compatibility, try again NAME as pattern. */
1298 id
= fs_query_fontset (name
, 0);
1300 error ("Fontset `%s' does not exist", SDATA (name
));
1301 return FONTSET_FROM_ID (id
);
1305 accumulate_script_ranges (arg
, range
, val
)
1306 Lisp_Object arg
, range
, val
;
1308 if (EQ (XCAR (arg
), val
))
1311 XSETCDR (arg
, Fcons (Fcons (XCAR (range
), XCDR (range
)), XCDR (arg
)));
1313 XSETCDR (arg
, Fcons (Fcons (range
, range
), XCDR (arg
)));
1318 /* Return an ASCII font name generated from fontset name NAME and
1319 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1321 static INLINE Lisp_Object
1322 generate_ascii_font_name (name
, ascii_spec
)
1323 Lisp_Object name
, ascii_spec
;
1325 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1329 if (font_parse_xlfd ((char *) SDATA (name
), font_spec
) < 0)
1330 error ("Not an XLFD font name: %s", SDATA (name
));
1331 for (i
= FONT_FOUNDRY_INDEX
; i
< FONT_EXTRA_INDEX
; i
++)
1332 if (! NILP (AREF (ascii_spec
, i
)))
1333 ASET (font_spec
, i
, AREF (ascii_spec
, i
));
1334 i
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1336 error ("Not an XLFD font name: %s", SDATA (name
));
1337 return make_unibyte_string (xlfd
, i
);
1340 /* Variables referred in set_fontset_font. They are set before
1341 map_charset_chars is called in Fset_fontset_font. */
1342 static Lisp_Object font_def_arg
, add_arg
;
1343 static int from_arg
, to_arg
;
1345 /* Callback function for map_charset_chars in Fset_fontset_font. In
1346 FONTSET, set font_def_arg in a fashion specified by add_arg for
1347 characters in RANGE while ignoring the range between from_arg and
1351 set_fontset_font (fontset
, range
)
1352 Lisp_Object fontset
, range
;
1354 if (from_arg
< to_arg
)
1356 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
1358 if (from
< from_arg
)
1364 range2
= Fcons (make_number (to_arg
), XCDR (range
));
1365 FONTSET_ADD (fontset
, range
, font_def_arg
, add_arg
);
1369 range
= Fcons (XCAR (range
), make_number (from_arg
));
1371 else if (to
<= to_arg
)
1376 range
= Fcons (make_number (to_arg
), XCDR (range
));
1379 FONTSET_ADD (fontset
, range
, font_def_arg
, add_arg
);
1382 extern Lisp_Object QCfamily
, QCregistry
;
1384 DEFUN ("set-fontset-font", Fset_fontset_font
, Sset_fontset_font
, 3, 5, 0,
1386 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1388 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1389 In that case, use FONT-SPEC for all characters in the range FROM and
1392 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1393 all characters that belong to the script.
1395 TARGET may be a charset. In that case, use FONT-SPEC for all
1396 characters in the charset.
1398 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1399 that no FONT-SPEC is specified.
1401 FONT-SPEC may one of these:
1402 * A font-spec object made by the function `font-spec' (which see).
1403 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1404 REGISTRY is a font registry name. FAMILY may contain foundry
1405 name, and REGISTRY may contain encoding name.
1406 * A font name string.
1407 * nil, which explicitly specifies that there's no font for TARGET.
1409 Optional 4th argument FRAME, if non-nil, is a frame. This argument is
1410 kept for backward compatibility and has no meaning.
1412 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1413 to the font specifications for TARGET previously set. If it is
1414 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1415 appended. By default, FONT-SPEC overrides the previous settings. */)
1416 (name
, target
, font_spec
, frame
, add
)
1417 Lisp_Object name
, target
, font_spec
, frame
, add
;
1419 Lisp_Object fontset
;
1420 Lisp_Object font_def
, registry
, family
;
1421 Lisp_Object range_list
;
1422 struct charset
*charset
= NULL
;
1424 fontset
= check_fontset_name (name
);
1426 /* The arg FRAME is kept for backward compatibility. We only check
1429 CHECK_LIVE_FRAME (frame
);
1431 if (CONSP (font_spec
))
1433 Lisp_Object spec
= Ffont_spec (0, NULL
);
1435 font_parse_family_registry (XCAR (font_spec
), XCDR (font_spec
), spec
);
1438 else if (STRINGP (font_spec
))
1440 Lisp_Object args
[2];
1441 extern Lisp_Object QCname
;
1444 args
[1] = font_spec
;
1445 font_spec
= Ffont_spec (2, args
);
1447 else if (! NILP (font_spec
) && ! FONT_SPEC_P (font_spec
))
1448 Fsignal (Qfont
, list2 (build_string ("Invalid font-spec"), font_spec
));
1450 if (! NILP (font_spec
))
1452 Lisp_Object encoding
, repertory
;
1454 family
= AREF (font_spec
, FONT_FAMILY_INDEX
);
1455 if (! NILP (family
) )
1456 family
= SYMBOL_NAME (family
);
1457 registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1458 if (! NILP (registry
))
1459 registry
= Fdowncase (SYMBOL_NAME (registry
));
1460 encoding
= find_font_encoding (concat3 (family
, build_string ("-"),
1462 if (NILP (encoding
))
1465 if (SYMBOLP (encoding
))
1467 CHECK_CHARSET (encoding
);
1468 encoding
= repertory
= CHARSET_SYMBOL_ID (encoding
);
1472 repertory
= XCDR (encoding
);
1473 encoding
= XCAR (encoding
);
1474 CHECK_CHARSET (encoding
);
1475 encoding
= CHARSET_SYMBOL_ID (encoding
);
1476 if (! NILP (repertory
) && SYMBOLP (repertory
))
1478 CHECK_CHARSET (repertory
);
1479 repertory
= CHARSET_SYMBOL_ID (repertory
);
1482 FONT_DEF_NEW (font_def
, font_spec
, encoding
, repertory
);
1487 if (CHARACTERP (target
))
1488 range_list
= Fcons (Fcons (target
, target
), Qnil
);
1489 else if (CONSP (target
))
1491 Lisp_Object from
, to
;
1493 from
= Fcar (target
);
1495 CHECK_CHARACTER (from
);
1496 CHECK_CHARACTER (to
);
1497 range_list
= Fcons (target
, Qnil
);
1499 else if (SYMBOLP (target
) && !NILP (target
))
1501 Lisp_Object script_list
;
1505 script_list
= XCHAR_TABLE (Vchar_script_table
)->extras
[0];
1506 if (! NILP (Fmemq (target
, script_list
)))
1508 val
= Fcons (target
, Qnil
);
1509 map_char_table (accumulate_script_ranges
, Qnil
, Vchar_script_table
,
1511 range_list
= XCDR (val
);
1512 if (EQ (target
, Qlatin
) && NILP (FONTSET_ASCII (fontset
)))
1514 if (VECTORP (font_spec
))
1515 val
= generate_ascii_font_name (FONTSET_NAME (fontset
),
1519 FONTSET_ASCII (fontset
) = val
;
1522 if (CHARSETP (target
))
1524 if (EQ (target
, Qascii
) && NILP (FONTSET_ASCII (fontset
)))
1526 if (VECTORP (font_spec
))
1527 font_spec
= generate_ascii_font_name (FONTSET_NAME (fontset
),
1529 FONTSET_ASCII (fontset
) = font_spec
;
1530 range_list
= Fcons (Fcons (make_number (0), make_number (127)),
1535 CHECK_CHARSET_GET_CHARSET (target
, charset
);
1538 else if (NILP (range_list
))
1539 error ("Invalid script or charset name: %s",
1540 SDATA (SYMBOL_NAME (target
)));
1542 else if (NILP (target
))
1543 range_list
= Fcons (Qnil
, Qnil
);
1545 error ("Invalid target for setting a font");
1550 font_def_arg
= font_def
;
1552 if (NILP (range_list
))
1553 from_arg
= to_arg
= 0;
1555 from_arg
= XINT (XCAR (XCAR (range_list
))),
1556 to_arg
= XINT (XCDR (XCAR (range_list
)));
1558 map_charset_chars (set_fontset_font
, Qnil
, fontset
, charset
,
1559 CHARSET_MIN_CODE (charset
),
1560 CHARSET_MAX_CODE (charset
));
1562 for (; CONSP (range_list
); range_list
= XCDR (range_list
))
1563 FONTSET_ADD (fontset
, XCAR (range_list
), font_def
, add
);
1565 /* Free all realized fontsets whose base is FONTSET. This way, the
1566 specified character(s) are surely redisplayed by a correct
1568 free_realized_fontsets (fontset
);
1574 DEFUN ("new-fontset", Fnew_fontset
, Snew_fontset
, 2, 2, 0,
1575 doc
: /* Create a new fontset NAME from font information in FONTLIST.
1577 FONTLIST is an alist of scripts vs the corresponding font specification list.
1578 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1579 character of SCRIPT is displayed by a font that matches one of
1582 SCRIPT is a symbol that appears in the first extra slot of the
1583 char-table `char-script-table'.
1585 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1586 `set-fontset-font' for the meaning. */)
1588 Lisp_Object name
, fontlist
;
1590 Lisp_Object fontset
;
1593 CHECK_STRING (name
);
1594 CHECK_LIST (fontlist
);
1596 name
= Fdowncase (name
);
1597 id
= fs_query_fontset (name
, 0);
1600 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1601 Lisp_Object short_name
;
1605 if (font_parse_xlfd ((char *) SDATA (name
), font_spec
) < 0)
1606 error ("Fontset name must be in XLFD format");
1607 short_name
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1608 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name
)), "fontset-", 8)
1609 || SBYTES (SYMBOL_NAME (short_name
)) < 9)
1610 error ("Registry field of fontset name must be \"fontset-*\"");
1611 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (short_name
)),
1612 Vfontset_alias_alist
);
1613 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso8859_1
);
1614 fontset
= make_fontset (Qnil
, name
, Qnil
);
1615 len
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1617 error ("Invalid fontset name (perhaps too long): %s", SDATA (name
));
1618 FONTSET_ASCII (fontset
) = make_unibyte_string (xlfd
, len
);
1622 fontset
= FONTSET_FROM_ID (id
);
1623 free_realized_fontsets (fontset
);
1624 Fset_char_table_range (fontset
, Qt
, Qnil
);
1627 for (; ! NILP (fontlist
); fontlist
= Fcdr (fontlist
))
1629 Lisp_Object elt
, script
;
1631 elt
= Fcar (fontlist
);
1632 script
= Fcar (elt
);
1634 if (CONSP (elt
) && (NILP (XCDR (elt
)) || CONSP (XCDR (elt
))))
1635 for (; CONSP (elt
); elt
= XCDR (elt
))
1636 Fset_fontset_font (name
, script
, XCAR (elt
), Qnil
, Qappend
);
1638 Fset_fontset_font (name
, script
, elt
, Qnil
, Qappend
);
1644 /* Alist of automatically created fontsets. Each element is a cons
1645 (FONT-SPEC . FONTSET-ID). */
1646 static Lisp_Object auto_fontset_alist
;
1649 fontset_from_font (font_object
)
1650 Lisp_Object font_object
;
1652 Lisp_Object font_name
= font_get_name (font_object
);
1653 Lisp_Object font_spec
= Fcopy_font_spec (font_object
);
1654 Lisp_Object fontset_spec
, alias
, name
, fontset
;
1658 val
= assoc_no_quit (font_spec
, auto_fontset_alist
);
1660 return XINT (FONTSET_ID (XCDR (val
)));
1661 if (NILP (auto_fontset_alist
))
1662 alias
= intern ("fontset-startup");
1666 int len
= XINT (Flength (auto_fontset_alist
));
1668 sprintf (temp
, "fontset-auto%d", len
);
1669 alias
= intern (temp
);
1671 fontset_spec
= Fcopy_font_spec (font_spec
);
1672 ASET (fontset_spec
, FONT_REGISTRY_INDEX
, alias
);
1673 name
= Ffont_xlfd_name (fontset_spec
, Qnil
);
1676 fontset
= make_fontset (Qnil
, name
, Qnil
);
1677 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (alias
)),
1678 Vfontset_alias_alist
);
1679 alias
= Fdowncase (AREF (font_object
, FONT_NAME_INDEX
));
1680 Vfontset_alias_alist
= Fcons (Fcons (name
, alias
), Vfontset_alias_alist
);
1681 auto_fontset_alist
= Fcons (Fcons (font_spec
, fontset
), auto_fontset_alist
);
1682 FONTSET_ASCII (fontset
) = font_name
;
1683 font_spec
= Fcopy_font_spec (font_spec
);
1684 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso10646_1
);
1685 for (i
= FONT_WEIGHT_INDEX
; i
< FONT_EXTRA_INDEX
; i
++)
1686 ASET (font_spec
, i
, Qnil
);
1687 Fset_fontset_font (name
, Qlatin
, font_spec
, Qnil
, Qnil
);
1688 Fset_fontset_font (name
, Qnil
, font_spec
, Qnil
, Qnil
);
1691 nsfont_make_fontset_for_font(name
, font_object
);
1694 return XINT (FONTSET_ID (fontset
));
1697 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1698 FONT-OBJECT is the font for the character at POSITION in the current
1699 buffer. This is computed from all the text properties and overlays
1700 that apply to POSITION. POSTION may be nil, in which case,
1701 FONT-SPEC is the font for displaying the character CH with the
1704 GLYPH-CODE is the glyph code in the font to use for the character.
1706 If the 2nd optional arg CH is non-nil, it is a character to check
1707 the font instead of the character at POSITION.
1709 It returns nil in the following cases:
1711 (1) The window system doesn't have a font for the character (thus
1712 it is displayed by an empty box).
1714 (2) The character code is invalid.
1716 (3) If POSITION is not nil, and the current buffer is not displayed
1719 In addition, the returned font name may not take into account of
1720 such redisplay engine hooks as what used in jit-lock-mode if
1721 POSITION is currently not visible. */
1724 DEFUN ("internal-char-font", Finternal_char_font
, Sinternal_char_font
, 1, 2, 0,
1725 doc
: /* For internal use only. */)
1727 Lisp_Object position
, ch
;
1729 EMACS_INT pos
, pos_byte
, dummy
;
1736 if (NILP (position
))
1738 CHECK_CHARACTER (ch
);
1740 f
= XFRAME (selected_frame
);
1741 face_id
= lookup_basic_face (f
, DEFAULT_FACE_ID
);
1747 Lisp_Object window
, charset
;
1750 CHECK_NUMBER_COERCE_MARKER (position
);
1751 pos
= XINT (position
);
1752 if (pos
< BEGV
|| pos
>= ZV
)
1753 args_out_of_range_3 (position
, make_number (BEGV
), make_number (ZV
));
1754 pos_byte
= CHAR_TO_BYTE (pos
);
1756 c
= FETCH_CHAR (pos_byte
);
1762 window
= Fget_buffer_window (Fcurrent_buffer (), Qnil
);
1765 w
= XWINDOW (window
);
1766 f
= XFRAME (w
->frame
);
1767 face_id
= face_at_buffer_position (w
, pos
, -1, -1, &dummy
, pos
+ 100, 0);
1768 charset
= Fget_char_property (position
, Qcharset
, Qnil
);
1769 if (CHARSETP (charset
))
1770 cs_id
= XINT (CHARSET_SYMBOL_ID (charset
));
1774 if (! CHAR_VALID_P (c
, 0))
1776 face_id
= FACE_FOR_CHAR (f
, FACE_FROM_ID (f
, face_id
), c
, pos
, Qnil
);
1777 face
= FACE_FROM_ID (f
, face_id
);
1780 unsigned code
= face
->font
->driver
->encode_char (face
->font
, c
);
1781 Lisp_Object font_object
;
1782 /* Assignment to EMACS_INT stops GCC whining about limited range
1784 EMACS_INT cod
= code
;
1786 if (code
== FONT_INVALID_CODE
)
1788 XSETFONT (font_object
, face
->font
);
1789 if (cod
<= MOST_POSITIVE_FIXNUM
)
1790 return Fcons (font_object
, make_number (code
));
1791 return Fcons (font_object
, Fcons (make_number (code
>> 16),
1792 make_number (code
& 0xFFFF)));
1798 DEFUN ("fontset-info", Ffontset_info
, Sfontset_info
, 1, 2, 0,
1799 doc
: /* Return information about a fontset FONTSET on frame FRAME.
1800 The value is a char-table whose elements have this form:
1802 ((FONT-PATTERN OPENED-FONT ...) ...)
1804 FONT-PATTERN is a vector:
1806 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
1808 or a string of font name pattern.
1810 OPENED-FONT is a name of a font actually opened.
1812 The char-table has one extra slot. The value is a char-table
1813 containing the information about the derived fonts from the default
1814 fontset. The format is the same as above. */)
1816 Lisp_Object fontset
, frame
;
1819 Lisp_Object
*realized
[2], fontsets
[2], tables
[2];
1820 Lisp_Object val
, elt
;
1823 (*check_window_system_func
) ();
1825 fontset
= check_fontset_name (fontset
);
1828 frame
= selected_frame
;
1829 CHECK_LIVE_FRAME (frame
);
1832 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1833 in the table `realized'. */
1834 realized
[0] = (Lisp_Object
*) alloca (sizeof (Lisp_Object
)
1835 * ASIZE (Vfontset_table
));
1836 for (i
= j
= 0; i
< ASIZE (Vfontset_table
); i
++)
1838 elt
= FONTSET_FROM_ID (i
);
1840 && EQ (FONTSET_BASE (elt
), fontset
)
1841 && EQ (FONTSET_FRAME (elt
), frame
))
1842 realized
[0][j
++] = elt
;
1844 realized
[0][j
] = Qnil
;
1846 realized
[1] = (Lisp_Object
*) alloca (sizeof (Lisp_Object
)
1847 * ASIZE (Vfontset_table
));
1848 for (i
= j
= 0; ! NILP (realized
[0][i
]); i
++)
1850 elt
= FONTSET_DEFAULT (realized
[0][i
]);
1852 realized
[1][j
++] = elt
;
1854 realized
[1][j
] = Qnil
;
1856 tables
[0] = Fmake_char_table (Qfontset_info
, Qnil
);
1857 tables
[1] = Fmake_char_table (Qnil
, Qnil
);
1858 XCHAR_TABLE (tables
[0])->extras
[0] = tables
[1];
1859 fontsets
[0] = fontset
;
1860 fontsets
[1] = Vdefault_fontset
;
1862 /* Accumulate information of the fontset in TABLE. The format of
1863 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1864 for (k
= 0; k
<= 1; k
++)
1866 for (c
= 0; c
<= MAX_CHAR
; )
1868 int from
= c
, to
= MAX_5_BYTE_CHAR
;
1870 if (c
<= MAX_5_BYTE_CHAR
)
1872 val
= char_table_ref_and_range (fontsets
[k
], c
, &from
, &to
);
1876 val
= FONTSET_FALLBACK (fontsets
[k
]);
1883 /* At first, set ALIST to ((FONT-SPEC) ...). */
1884 for (alist
= Qnil
, i
= 0; i
< ASIZE (val
); i
++)
1885 if (! NILP (AREF (val
, i
)))
1886 alist
= Fcons (Fcons (FONT_DEF_SPEC (AREF (val
, i
)), Qnil
),
1888 alist
= Fnreverse (alist
);
1890 /* Then store opened font names to cdr of each elements. */
1891 for (i
= 0; ! NILP (realized
[k
][i
]); i
++)
1893 if (c
<= MAX_5_BYTE_CHAR
)
1894 val
= FONTSET_REF (realized
[k
][i
], c
);
1896 val
= FONTSET_FALLBACK (realized
[k
][i
]);
1897 if (! VECTORP (val
))
1899 /* VAL: [int ? [FACE-ID FONT-DEF FONT-OBJECT int] ... ] */
1900 for (j
= 2; j
< ASIZE (val
); j
++)
1902 elt
= AREF (val
, j
);
1903 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt
)))
1905 Lisp_Object font_object
= RFONT_DEF_OBJECT (elt
);
1906 Lisp_Object slot
, name
;
1908 slot
= Fassq (RFONT_DEF_SPEC (elt
), alist
);
1909 name
= AREF (font_object
, FONT_NAME_INDEX
);
1910 if (NILP (Fmember (name
, XCDR (slot
))))
1911 nconc2 (slot
, Fcons (name
, Qnil
));
1916 /* Store ALIST in TBL for characters C..TO. */
1917 if (c
<= MAX_5_BYTE_CHAR
)
1918 char_table_set_range (tables
[k
], c
, to
, alist
);
1920 XCHAR_TABLE (tables
[k
])->defalt
= alist
;
1922 /* At last, change each elements to font names. */
1923 for (; CONSP (alist
); alist
= XCDR (alist
))
1926 XSETCAR (elt
, Ffont_xlfd_name (XCAR (elt
), Qnil
));
1937 DEFUN ("fontset-font", Ffontset_font
, Sfontset_font
, 2, 3, 0,
1938 doc
: /* Return a font name pattern for character CH in fontset NAME.
1939 If NAME is t, find a pattern in the default fontset.
1941 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
1942 family name and REGISTRY is a font registry name. This is actually
1943 the first font name pattern for CH in the fontset or in the default
1946 If the 2nd optional arg ALL is non-nil, return a list of all font name
1949 Lisp_Object name
, ch
, all
;
1952 Lisp_Object fontset
, elt
, list
, repertory
, val
;
1955 fontset
= check_fontset_name (name
);
1957 CHECK_CHARACTER (ch
);
1962 for (i
= 0, elt
= FONTSET_REF (fontset
, c
); i
< 2;
1963 i
++, elt
= FONTSET_FALLBACK (fontset
))
1965 for (j
= 0; j
< ASIZE (elt
); j
++)
1967 Lisp_Object family
, registry
;
1969 val
= AREF (elt
, j
);
1970 repertory
= AREF (val
, 1);
1971 if (INTEGERP (repertory
))
1973 struct charset
*charset
= CHARSET_FROM_ID (XINT (repertory
));
1975 if (! CHAR_CHARSET_P (c
, charset
))
1978 else if (CHAR_TABLE_P (repertory
))
1980 if (NILP (CHAR_TABLE_REF (repertory
, c
)))
1983 val
= AREF (val
, 0);
1984 /* VAL is a FONT-SPEC */
1985 family
= AREF (val
, FONT_FAMILY_INDEX
);
1986 if (! NILP (family
))
1987 family
= SYMBOL_NAME (family
);
1988 registry
= AREF (val
, FONT_REGISTRY_INDEX
);
1989 if (! NILP (registry
))
1990 registry
= SYMBOL_NAME (registry
);
1991 val
= Fcons (family
, registry
);
1994 list
= Fcons (val
, list
);
1996 if (EQ (fontset
, Vdefault_fontset
))
1998 fontset
= Vdefault_fontset
;
2000 return (Fnreverse (list
));
2003 DEFUN ("fontset-list", Ffontset_list
, Sfontset_list
, 0, 0, 0,
2004 doc
: /* Return a list of all defined fontset names. */)
2007 Lisp_Object fontset
, list
;
2011 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
2013 fontset
= FONTSET_FROM_ID (i
);
2015 && BASE_FONTSET_P (fontset
))
2016 list
= Fcons (FONTSET_NAME (fontset
), list
);
2023 #ifdef FONTSET_DEBUG
2026 dump_fontset (fontset
)
2027 Lisp_Object fontset
;
2031 vec
= Fmake_vector (make_number (3), Qnil
);
2032 ASET (vec
, 0, FONTSET_ID (fontset
));
2034 if (BASE_FONTSET_P (fontset
))
2036 ASET (vec
, 1, FONTSET_NAME (fontset
));
2042 frame
= FONTSET_FRAME (fontset
);
2045 FRAME_PTR f
= XFRAME (frame
);
2047 if (FRAME_LIVE_P (f
))
2049 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), f
->name
));
2052 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), Qnil
));
2054 if (!NILP (FONTSET_DEFAULT (fontset
)))
2055 ASET (vec
, 2, FONTSET_ID (FONTSET_DEFAULT (fontset
)));
2060 DEFUN ("fontset-list-all", Ffontset_list_all
, Sfontset_list_all
, 0, 0, 0,
2061 doc
: /* Return a brief summary of all fontsets for debug use. */)
2067 for (i
= 0, val
= Qnil
; i
< ASIZE (Vfontset_table
); i
++)
2068 if (! NILP (AREF (Vfontset_table
, i
)))
2069 val
= Fcons (dump_fontset (AREF (Vfontset_table
, i
)), val
);
2070 return (Fnreverse (val
));
2072 #endif /* FONTSET_DEBUG */
2077 DEFSYM (Qfontset
, "fontset");
2078 Fput (Qfontset
, Qchar_table_extra_slots
, make_number (9));
2079 DEFSYM (Qfontset_info
, "fontset-info");
2080 Fput (Qfontset_info
, Qchar_table_extra_slots
, make_number (1));
2082 DEFSYM (Qprepend
, "prepend");
2083 DEFSYM (Qappend
, "append");
2084 DEFSYM (Qlatin
, "latin");
2086 Vcached_fontset_data
= Qnil
;
2087 staticpro (&Vcached_fontset_data
);
2089 Vfontset_table
= Fmake_vector (make_number (32), Qnil
);
2090 staticpro (&Vfontset_table
);
2092 Vdefault_fontset
= Fmake_char_table (Qfontset
, Qnil
);
2093 staticpro (&Vdefault_fontset
);
2094 FONTSET_ID (Vdefault_fontset
) = make_number (0);
2095 FONTSET_NAME (Vdefault_fontset
)
2096 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2097 ASET (Vfontset_table
, 0, Vdefault_fontset
);
2098 next_fontset_id
= 1;
2100 auto_fontset_alist
= Qnil
;
2101 staticpro (&auto_fontset_alist
);
2103 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist
,
2105 Alist of charsets vs the charsets to determine the preferred font encoding.
2106 Each element looks like (CHARSET . ENCODING-CHARSET),
2107 where ENCODING-CHARSET is a charset registered in the variable
2108 `font-encoding-alist' as ENCODING.
2110 When a text has a property `charset' and the value is CHARSET, a font
2111 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2112 Vfont_encoding_charset_alist
= Qnil
;
2114 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent
,
2116 Char table of characters whose ascent values should be ignored.
2117 If an entry for a character is non-nil, the ascent value of the glyph
2118 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2120 This affects how a composite character which contains
2121 such a character is displayed on screen. */);
2122 Vuse_default_ascent
= Qnil
;
2124 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition
,
2126 Char table of characters which are not composed relatively.
2127 If an entry for a character is non-nil, a composition sequence
2128 which contains that character is displayed so that
2129 the glyph of that character is put without considering
2130 an ascent and descent value of a previous character. */);
2131 Vignore_relative_composition
= Qnil
;
2133 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist
,
2134 doc
: /* Alist of fontname vs list of the alternate fontnames.
2135 When a specified font name is not found, the corresponding
2136 alternate fontnames (if any) are tried instead. */);
2137 Valternate_fontname_alist
= Qnil
;
2139 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist
,
2140 doc
: /* Alist of fontset names vs the aliases. */);
2141 Vfontset_alias_alist
= Fcons (Fcons (FONTSET_NAME (Vdefault_fontset
),
2142 build_string ("fontset-default")),
2145 DEFVAR_LISP ("vertical-centering-font-regexp",
2146 &Vvertical_centering_font_regexp
,
2147 doc
: /* *Regexp matching font names that require vertical centering on display.
2148 When a character is displayed with such fonts, the character is displayed
2149 at the vertical center of lines. */);
2150 Vvertical_centering_font_regexp
= Qnil
;
2152 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist
,
2153 doc
: /* Alist of OpenType script tags vs the corresponding script names. */);
2154 Votf_script_alist
= Qnil
;
2156 defsubr (&Squery_fontset
);
2157 defsubr (&Snew_fontset
);
2158 defsubr (&Sset_fontset_font
);
2159 defsubr (&Sinternal_char_font
);
2160 defsubr (&Sfontset_info
);
2161 defsubr (&Sfontset_font
);
2162 defsubr (&Sfontset_list
);
2163 #ifdef FONTSET_DEBUG
2164 defsubr (&Sfontset_list_all
);
2168 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2169 (do not change this comment) */