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, or a vector of this form:
94 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
95 RFONT-DEF0 RFONT-DEF1 ... ]
97 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
99 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
101 RFONT-DEFn are automatically reordered by the current charset
104 The value nil means that we have not yet generated the above vector
105 from the base of the fontset.
107 The value t means that no font is available for the corresponding
111 A fontset has 9 extra slots.
113 The 1st slot: the ID number of the fontset
116 base: the name of the fontset
121 realized: the base fontset
125 realized: the frame that the fontset belongs to
128 base: the font name for ASCII characters
133 realized: the ID number of a face to use for characters that
134 has no font in a realized fontset.
138 realized: Alist of font index vs the corresponding repertory
143 realized: If the base is not the default fontset, a fontset
144 realized from the default fontset, else nil.
147 base: Same as element value (but for fallback fonts).
150 All fontsets are recorded in the vector Vfontset_table.
155 There's a special base fontset named `default fontset' which
156 defines the default font specifications. When a base fontset
157 doesn't specify a font for a specific character, the corresponding
158 value in the default fontset is used.
160 The parent of a realized fontset created for such a face that has
161 no fontset is the default fontset.
164 These structures are hidden from the other codes than this file.
165 The other codes handle fontsets only by their ID numbers. They
166 usually use the variable name `fontset' for IDs. But, in this
167 file, we always use varialbe name `id' for IDs, and name `fontset'
168 for an actual fontset object, i.e., char-table.
172 /********** VARIABLES and FUNCTION PROTOTYPES **********/
174 extern Lisp_Object Qfont
;
175 static Lisp_Object Qfontset
;
176 static Lisp_Object Qfontset_info
;
177 static Lisp_Object Qprepend
, Qappend
;
178 static Lisp_Object Qlatin
;
180 /* Vector containing all fontsets. */
181 static Lisp_Object Vfontset_table
;
183 /* Next possibly free fontset ID. Usually this keeps the minimum
184 fontset ID not yet used. */
185 static int next_fontset_id
;
187 /* The default fontset. This gives default FAMILY and REGISTRY of
188 font for each character. */
189 static Lisp_Object Vdefault_fontset
;
191 Lisp_Object Vfont_encoding_charset_alist
;
192 Lisp_Object Vuse_default_ascent
;
193 Lisp_Object Vignore_relative_composition
;
194 Lisp_Object Valternate_fontname_alist
;
195 Lisp_Object Vfontset_alias_alist
;
196 Lisp_Object Vvertical_centering_font_regexp
;
197 Lisp_Object Votf_script_alist
;
199 /* Check if any window system is used now. */
200 void (*check_window_system_func
) P_ ((void));
203 /* Prototype declarations for static functions. */
204 static Lisp_Object fontset_add
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
206 static Lisp_Object fontset_find_font
P_ ((Lisp_Object
, int, struct face
*,
208 static void reorder_font_vector
P_ ((Lisp_Object
, struct font
*));
209 static Lisp_Object fontset_font
P_ ((Lisp_Object
, int, struct face
*, int));
210 static Lisp_Object make_fontset
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
211 static Lisp_Object fontset_pattern_regexp
P_ ((Lisp_Object
));
212 static void accumulate_script_ranges
P_ ((Lisp_Object
, Lisp_Object
,
214 Lisp_Object find_font_encoding
P_ ((Lisp_Object
));
216 static void set_fontset_font
P_ ((Lisp_Object
, Lisp_Object
));
220 /* Return 1 if ID is a valid fontset id, else return 0. */
223 fontset_id_valid_p (id
)
226 return (id
>= 0 && id
< ASIZE (Vfontset_table
) - 1);
233 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
235 /* Return the fontset with ID. No check of ID's validness. */
236 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
238 /* Macros to access special values of FONTSET. */
239 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
241 /* Macros to access special values of (base) FONTSET. */
242 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
243 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
244 #define FONTSET_SPEC(fontset) XCHAR_TABLE (fontset)->extras[5]
246 /* Macros to access special values of (realized) FONTSET. */
247 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
248 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
249 #define FONTSET_OBJLIST(fontset) XCHAR_TABLE (fontset)->extras[4]
250 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
251 #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
252 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
254 /* For both base and realized fontset. */
255 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
257 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
260 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
261 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
263 (font_def) = Fmake_vector (make_number (3), (font_spec)); \
264 ASET ((font_def), 1, encoding); \
265 ASET ((font_def), 2, repertory); \
268 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
269 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
270 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
272 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
273 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
274 ASET ((rfont_def), 0, make_number (face_id))
275 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
276 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
277 #define RFONT_DEF_REPERTORY(rfont_def) FONT_DEF_REPERTORY (AREF (rfont_def, 1))
278 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
279 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
280 ASET ((rfont_def), 2, (object))
281 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
282 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
283 ASET ((rfont_def), 3, make_number (score))
284 #define RFONT_DEF_NEW(rfont_def, font_def) \
286 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
287 ASET ((rfont_def), 1, (font_def)); \
288 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
292 /* Return the element of FONTSET for the character C. If FONTSET is a
293 base fontset other then the default fontset and FONTSET doesn't
294 contain information for C, return the information in the default
297 #define FONTSET_REF(fontset, c) \
298 (EQ (fontset, Vdefault_fontset) \
299 ? CHAR_TABLE_REF (fontset, c) \
300 : fontset_ref ((fontset), (c)))
303 fontset_ref (fontset
, c
)
309 elt
= CHAR_TABLE_REF (fontset
, c
);
310 if (NILP (elt
) && ! EQ (fontset
, Vdefault_fontset
)
311 /* Don't check Vdefault_fontset for a realized fontset. */
312 && NILP (FONTSET_BASE (fontset
)))
313 elt
= CHAR_TABLE_REF (Vdefault_fontset
, c
);
317 /* Set elements of FONTSET for characters in RANGE to the value ELT.
318 RANGE is a cons (FROM . TO), where FROM and TO are character codes
319 specifying a range. */
321 #define FONTSET_SET(fontset, range, elt) \
322 Fset_char_table_range ((fontset), (range), (elt))
325 /* Modify the elements of FONTSET for characters in RANGE by replacing
326 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
327 and TO are character codes specifying a range. If ADD is nil,
328 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
331 #define FONTSET_ADD(fontset, range, elt, add) \
334 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
335 : Fset_char_table_range ((fontset), (range), \
336 Fmake_vector (make_number (1), (elt)))) \
337 : fontset_add ((fontset), (range), (elt), (add)))
340 fontset_add (fontset
, range
, elt
, add
)
341 Lisp_Object fontset
, range
, elt
, add
;
344 int idx
= (EQ (add
, Qappend
) ? 0 : 1);
346 args
[1 - idx
] = Fmake_vector (make_number (1), elt
);
350 int from
= XINT (XCAR (range
));
351 int to
= XINT (XCDR (range
));
355 from1
= from
, to1
= to
;
356 args
[idx
] = char_table_ref_and_range (fontset
, from
, &from1
, &to1
);
357 char_table_set_range (fontset
, from
, to1
,
358 NILP (args
[idx
]) ? args
[1 - idx
]
359 : Fvconcat (2, args
));
365 args
[idx
] = FONTSET_FALLBACK (fontset
);
366 FONTSET_FALLBACK (fontset
)
367 = NILP (args
[idx
]) ? args
[1 - idx
] : Fvconcat (2, args
);
373 fontset_compare_rfontdef (val1
, val2
)
374 const void *val1
, *val2
;
376 return (RFONT_DEF_SCORE (*(Lisp_Object
*) val1
)
377 - RFONT_DEF_SCORE (*(Lisp_Object
*) val2
));
380 /* Update FONT-GROUP which has this form:
381 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
382 RFONT-DEF0 RFONT-DEF1 ... ]
383 Reorder RFONT-DEFs according to the current language, and update
384 CHARSET-ORDERED-LIST-TICK.
386 If PREFERRED_FAMILY is not nil, that family has the higher priority
387 if the encoding charsets or languages in font-specs are the same. */
389 extern Lisp_Object
Fassoc_string ();
392 reorder_font_vector (font_group
, font
)
393 Lisp_Object font_group
;
396 Lisp_Object vec
, font_object
;
399 int score_changed
= 0;
402 XSETFONT (font_object
, font
);
406 vec
= XCDR (font_group
);
408 /* Exclude the tailing nil element from the reordering. */
409 if (NILP (AREF (vec
, size
- 1)))
412 for (i
= 0; i
< size
; i
++)
414 Lisp_Object rfont_def
= AREF (vec
, i
);
415 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
416 Lisp_Object font_spec
= FONT_DEF_SPEC (font_def
);
417 int score
= RFONT_DEF_SCORE (rfont_def
) & 0xFF;
419 if (! font_match_p (font_spec
, font_object
))
421 Lisp_Object encoding
= FONT_DEF_ENCODING (font_def
);
423 if (! NILP (encoding
))
427 for (tail
= Vcharset_ordered_list
;
428 ! EQ (tail
, Vcharset_non_preferred_head
) && CONSP (tail
);
429 score
+= 0x100, tail
= XCDR (tail
))
430 if (EQ (encoding
, XCAR (tail
)))
435 Lisp_Object lang
= Ffont_get (font_spec
, QClang
);
438 && ! EQ (lang
, Vcurrent_iso639_language
)
439 && (! CONSP (Vcurrent_iso639_language
)
440 || NILP (Fmemq (lang
, Vcurrent_iso639_language
))))
444 if (RFONT_DEF_SCORE (rfont_def
) != score
)
446 RFONT_DEF_SET_SCORE (rfont_def
, score
);
452 qsort (XVECTOR (vec
)->contents
, size
, sizeof (Lisp_Object
),
453 fontset_compare_rfontdef
);
454 XSETCAR (font_group
, make_number (charset_ordered_list_tick
));
458 fontset_get_font_group (Lisp_Object fontset
, int c
)
460 Lisp_Object font_group
;
461 Lisp_Object base_fontset
;
462 int from
= 0, to
= MAX_CHAR
, i
;
464 xassert (! BASE_FONTSET_P (fontset
));
466 font_group
= CHAR_TABLE_REF (fontset
, c
);
468 font_group
= FONTSET_FALLBACK (fontset
);
469 if (! NILP (font_group
))
471 base_fontset
= FONTSET_BASE (fontset
);
473 font_group
= char_table_ref_and_range (base_fontset
, c
, &from
, &to
);
475 font_group
= FONTSET_FALLBACK (base_fontset
);
476 if (NILP (font_group
))
478 font_group
= Fcopy_sequence (font_group
);
479 for (i
= 0; i
< ASIZE (font_group
); i
++)
480 if (! NILP (AREF (font_group
, i
)))
482 Lisp_Object rfont_def
;
484 RFONT_DEF_NEW (rfont_def
, AREF (font_group
, i
));
485 /* Remember the original order. */
486 RFONT_DEF_SET_SCORE (rfont_def
, i
);
487 ASET (font_group
, i
, rfont_def
);
489 font_group
= Fcons (make_number (-1), font_group
);
491 char_table_set_range (fontset
, from
, to
, font_group
);
493 FONTSET_FALLBACK (fontset
) = font_group
;
497 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
498 character C. If no font is found, return Qnil if there's a
499 possibility that the default fontset or the fallback font groups
500 have a proper font, and return Qt if not.
502 If a font is found but is not yet opened, open it (if FACE is not
503 NULL) or return Qnil (if FACE is NULL).
505 ID is a charset-id that must be preferred, or -1 meaning no
508 If FALLBACK is nonzero, search only fallback fonts. */
511 fontset_find_font (fontset
, c
, face
, id
, fallback
)
517 Lisp_Object elt
, vec
, font_group
;
518 int i
, charset_matched
= -1;
519 FRAME_PTR f
= (FRAMEP (FONTSET_FRAME (fontset
)))
520 ? XFRAME (selected_frame
) : XFRAME (FONTSET_FRAME (fontset
));
522 font_group
= fontset_get_font_group (fontset
, fallback
? -1 : c
);
523 if (! CONSP (font_group
))
525 vec
= XCDR (font_group
);
526 if (ASIZE (vec
) == 0)
531 if (XINT (XCAR (font_group
)) != charset_ordered_list_tick
)
532 /* We have just created the font-group,
533 or the charset priorities were changed. */
534 reorder_font_vector (font_group
, face
->ascii_face
->font
);
536 /* Find a spec matching with the charset ID to try at
538 for (i
= 0; i
< ASIZE (vec
); i
++)
540 Lisp_Object rfont_def
= AREF (vec
, i
);
541 Lisp_Object repertory
542 = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def
));
544 if (XINT (repertory
) == id
)
552 /* Find the first available font in the vector of RFONT-DEF. */
553 for (i
= 0; i
< ASIZE (vec
); i
++)
555 Lisp_Object font_entity
, font_object
;
557 if (i
== 0 && charset_matched
>= 0)
559 /* Try the element matching with the charset ID at first. */
560 elt
= AREF (vec
, charset_matched
);
561 charset_matched
= -1;
564 else if (i
!= charset_matched
)
570 /* This is a sign of not to try the other fonts. */
572 if (INTEGERP (RFONT_DEF_FACE (elt
))
573 && XINT (AREF (elt
, 1)) < 0)
574 /* We couldn't open this font last time. */
577 font_object
= RFONT_DEF_OBJECT (elt
);
578 if (NILP (font_object
))
580 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (elt
);
583 /* We have not yet opened the font. */
585 font_entity
= font_find_for_lface (f
, face
->lface
,
586 FONT_DEF_SPEC (font_def
), -1);
587 if (NILP (font_entity
))
589 /* Record that no font matches the spec. */
590 RFONT_DEF_SET_FACE (elt
, -1);
593 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
594 FONT_DEF_SPEC (font_def
));
595 if (NILP (font_object
))
597 /* Record that the font is unsable. */
598 RFONT_DEF_SET_FACE (elt
, -1);
601 RFONT_DEF_SET_OBJECT (elt
, font_object
);
604 if (font_has_char (f
, font_object
, c
))
608 /* The following code makes Emacs to find a font for C by fairly
609 exhausitive search. But, that takes long time especially for
612 /* Try to find the different font maching with the current spec
614 font_def
= RFONT_DEF_FONT_DEF (elt
);
615 for (i
++; i
< ASIZE (vec
); i
++)
617 if (! EQ (RFONT_DEF_FONT_DEF (AREF (vec
, i
)), font_def
))
619 if (font_has_char (f
, RFONT_DEF_OBJECT (AREF (vec
, i
)), c
))
620 return AREF (vec
, i
);
622 /* Find an font-entity that support C. */
623 font_entity
= font_find_for_lface (f
, face
->lface
,
624 FONT_DEF_SPEC (font_def
), c
);
625 if (! NILP (font_entity
))
627 Lisp_Object rfont_def
, new_vec
;
630 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
632 RFONT_DEF_NEW (rfont_def
, font_def
);
633 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
634 RFONT_DEF_SET_SCORE (rfont_def
, RFONT_DEF_SCORE (elt
));
635 new_vec
= Fmake_vector (make_number (ASIZE (vec
) + 1), Qnil
);
636 for (j
= 0; j
< i
; j
++)
637 ASET (new_vec
, j
, AREF (vec
, j
));
638 ASET (new_vec
, j
, rfont_def
);
639 for (j
++; j
< ASIZE (new_vec
); j
++)
640 ASET (new_vec
, j
, AREF (vec
, j
- 1));
648 FONTSET_SET (fontset
, make_number (c
), make_number (0));
654 fontset_font (fontset
, c
, face
, id
)
660 Lisp_Object rfont_def
;
661 Lisp_Object base_fontset
;
663 /* Try a font-group of FONTSET. */
664 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 0);
665 if (VECTORP (rfont_def
))
667 if (EQ (rfont_def
, Qt
))
670 /* Try a font-group of the default fontset. */
671 base_fontset
= FONTSET_BASE (fontset
);
672 if (! EQ (base_fontset
, Vdefault_fontset
))
674 if (NILP (FONTSET_DEFAULT (fontset
)))
675 FONTSET_DEFAULT (fontset
)
676 = make_fontset (FONTSET_FRAME (fontset
), Qnil
, Vdefault_fontset
);
677 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 0);
678 if (VECTORP (rfont_def
))
680 if (EQ (rfont_def
, Qt
))
684 /* Try a fallback font-group of FONTSET. */
685 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 1);
686 if (VECTORP (rfont_def
))
688 if (EQ (rfont_def
, Qt
))
691 /* Try a fallback font-group of the default fontset . */
692 if (! EQ (base_fontset
, Vdefault_fontset
))
694 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 1);
695 if (VECTORP (rfont_def
))
699 /* Remember that we have no font for C. */
700 FONTSET_SET (fontset
, make_number (c
), Qt
);
705 /* Return a newly created fontset with NAME. If BASE is nil, make a
706 base fontset. Otherwise make a realized fontset whose base is
710 make_fontset (frame
, name
, base
)
711 Lisp_Object frame
, name
, base
;
714 int size
= ASIZE (Vfontset_table
);
715 int id
= next_fontset_id
;
717 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
718 the next available fontset ID. So it is expected that this loop
719 terminates quickly. In addition, as the last element of
720 Vfontset_table is always nil, we don't have to check the range of
722 while (!NILP (AREF (Vfontset_table
, id
))) id
++;
725 Vfontset_table
= larger_vector (Vfontset_table
, size
+ 32, Qnil
);
727 fontset
= Fmake_char_table (Qfontset
, Qnil
);
729 FONTSET_ID (fontset
) = make_number (id
);
732 FONTSET_NAME (fontset
) = name
;
736 FONTSET_NAME (fontset
) = Qnil
;
737 FONTSET_FRAME (fontset
) = frame
;
738 FONTSET_BASE (fontset
) = base
;
741 ASET (Vfontset_table
, id
, fontset
);
742 next_fontset_id
= id
+ 1;
747 /* Set the ASCII font of the default fontset to FONTNAME if that is
750 set_default_ascii_font (fontname
)
751 Lisp_Object fontname
;
753 if (! STRINGP (FONTSET_ASCII (Vdefault_fontset
)))
755 int id
= fs_query_fontset (fontname
, 2);
758 fontname
= FONTSET_ASCII (FONTSET_FROM_ID (id
));
759 FONTSET_ASCII (Vdefault_fontset
)= fontname
;
764 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
766 /* Return the name of the fontset who has ID. */
774 fontset
= FONTSET_FROM_ID (id
);
775 return FONTSET_NAME (fontset
);
779 /* Return the ASCII font name of the fontset who has ID. */
785 Lisp_Object fontset
, elt
;
787 fontset
= FONTSET_FROM_ID (id
);
788 elt
= FONTSET_ASCII (fontset
);
795 free_realized_fontset (f
, fontset
)
802 for (tail
= FONTSET_OBJLIST (fontset
); CONSP (tail
); tail
= XCDR (tail
))
804 xassert (FONT_OBJECT_P (XCAR (tail
)));
805 font_close_object (f
, XCAR (tail
));
809 /* Free fontset of FACE defined on frame F. Called from
810 free_realized_face. */
813 free_face_fontset (f
, face
)
819 fontset
= FONTSET_FROM_ID (face
->fontset
);
822 xassert (! BASE_FONTSET_P (fontset
));
823 xassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
824 free_realized_fontset (f
, fontset
);
825 ASET (Vfontset_table
, face
->fontset
, Qnil
);
826 if (face
->fontset
< next_fontset_id
)
827 next_fontset_id
= face
->fontset
;
828 if (! NILP (FONTSET_DEFAULT (fontset
)))
830 int id
= XINT (FONTSET_ID (FONTSET_DEFAULT (fontset
)));
832 fontset
= AREF (Vfontset_table
, id
);
833 xassert (!NILP (fontset
) && ! BASE_FONTSET_P (fontset
));
834 xassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
835 free_realized_fontset (f
, fontset
);
836 ASET (Vfontset_table
, id
, Qnil
);
837 if (id
< next_fontset_id
)
838 next_fontset_id
= face
->fontset
;
844 /* Return 1 if FACE is suitable for displaying character C.
845 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
846 when C is not an ASCII character. */
849 face_suitable_for_char_p (face
, c
)
853 Lisp_Object fontset
, rfont_def
;
855 fontset
= FONTSET_FROM_ID (face
->fontset
);
856 rfont_def
= fontset_font (fontset
, c
, NULL
, -1);
857 return (VECTORP (rfont_def
)
858 && INTEGERP (RFONT_DEF_FACE (rfont_def
))
859 && face
->id
== XINT (RFONT_DEF_FACE (rfont_def
)));
863 /* Return ID of face suitable for displaying character C on frame F.
864 FACE must be reazlied for ASCII characters in advance. Called from
865 the macro FACE_FOR_CHAR. */
868 face_for_char (f
, face
, c
, pos
, object
)
874 Lisp_Object fontset
, rfont_def
, charset
;
878 /* If face->fontset is negative (that happens when no font is found
879 for face), just return face->ascii_face because we can't do
880 anything. Perhaps, we should fix the callers to assure
881 that face->fontset is always valid. */
882 if (ASCII_CHAR_P (c
) || face
->fontset
< 0)
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
));
896 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
897 if (CHARSETP (charset
))
901 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
902 if (CONSP (val
) && CHARSETP (XCDR (val
)))
903 charset
= XCDR (val
);
904 id
= XINT (CHARSET_SYMBOL_ID (charset
));
910 font_deferred_log ("font for", Fcons (make_number (c
), charset
), Qnil
);
911 rfont_def
= fontset_font (fontset
, c
, face
, id
);
912 if (VECTORP (rfont_def
))
914 if (INTEGERP (RFONT_DEF_FACE (rfont_def
)))
915 face_id
= XINT (RFONT_DEF_FACE (rfont_def
));
918 Lisp_Object font_object
;
920 font_object
= RFONT_DEF_OBJECT (rfont_def
);
921 face_id
= face_for_font (f
, font_object
, face
);
922 RFONT_DEF_SET_FACE (rfont_def
, face_id
);
927 if (INTEGERP (FONTSET_NOFONT_FACE (fontset
)))
928 face_id
= XINT (FONTSET_NOFONT_FACE (fontset
));
931 face_id
= face_for_font (f
, Qnil
, face
);
932 FONTSET_NOFONT_FACE (fontset
) = make_number (face_id
);
935 xassert (face_id
>= 0);
941 font_for_char (face
, c
, pos
, object
)
946 Lisp_Object fontset
, rfont_def
, charset
;
950 if (ASCII_CHAR_P (c
))
952 Lisp_Object font_object
;
954 XSETFONT (font_object
, face
->ascii_face
->font
);
958 xassert (fontset_id_valid_p (face
->fontset
));
959 fontset
= FONTSET_FROM_ID (face
->fontset
);
960 xassert (!BASE_FONTSET_P (fontset
));
968 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
969 if (CHARSETP (charset
))
973 val
= assoc_no_quit (charset
, Vfont_encoding_charset_alist
);
974 if (CONSP (val
) && CHARSETP (XCDR (val
)))
975 charset
= XCDR (val
);
976 id
= XINT (CHARSET_SYMBOL_ID (charset
));
982 font_deferred_log ("font for", Fcons (make_number (c
), charset
), Qnil
);
983 rfont_def
= fontset_font (fontset
, c
, face
, id
);
984 return (VECTORP (rfont_def
)
985 ? RFONT_DEF_OBJECT (rfont_def
)
990 /* Make a realized fontset for ASCII face FACE on frame F from the
991 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
992 default fontset as the base. Value is the id of the new fontset.
993 Called from realize_x_face. */
996 make_fontset_for_ascii_face (f
, base_fontset_id
, face
)
1001 Lisp_Object base_fontset
, fontset
, frame
;
1003 XSETFRAME (frame
, f
);
1004 if (base_fontset_id
>= 0)
1006 base_fontset
= FONTSET_FROM_ID (base_fontset_id
);
1007 if (!BASE_FONTSET_P (base_fontset
))
1008 base_fontset
= FONTSET_BASE (base_fontset
);
1009 if (! BASE_FONTSET_P (base_fontset
))
1013 base_fontset
= Vdefault_fontset
;
1015 fontset
= make_fontset (frame
, Qnil
, base_fontset
);
1016 return XINT (FONTSET_ID (fontset
));
1021 /* Cache data used by fontset_pattern_regexp. The car part is a
1022 pattern string containing at least one wild card, the cdr part is
1023 the corresponding regular expression. */
1024 static Lisp_Object Vcached_fontset_data
;
1026 #define CACHED_FONTSET_NAME ((char *) SDATA (XCAR (Vcached_fontset_data)))
1027 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1029 /* If fontset name PATTERN contains any wild card, return regular
1030 expression corresponding to PATTERN. */
1033 fontset_pattern_regexp (pattern
)
1034 Lisp_Object pattern
;
1036 if (!index ((char *) SDATA (pattern
), '*')
1037 && !index ((char *) SDATA (pattern
), '?'))
1038 /* PATTERN does not contain any wild cards. */
1041 if (!CONSP (Vcached_fontset_data
)
1042 || strcmp ((char *) SDATA (pattern
), CACHED_FONTSET_NAME
))
1044 /* We must at first update the cached data. */
1045 unsigned char *regex
, *p0
, *p1
;
1046 int ndashes
= 0, nstars
= 0, nescs
= 0;
1048 for (p0
= SDATA (pattern
); *p0
; p0
++)
1052 else if (*p0
== '*')
1055 || *p0
== '.' || *p0
== '\\'
1056 || *p0
== '+' || *p0
== '^'
1061 /* If PATTERN is not full XLFD we conert "*" to ".*". Otherwise
1062 we convert "*" to "[^-]*" which is much faster in regular
1063 expression matching. */
1065 p1
= regex
= (unsigned char *) alloca (SBYTES (pattern
) + 2 * nstars
+ 2 * nescs
+ 1);
1067 p1
= regex
= (unsigned char *) alloca (SBYTES (pattern
) + 5 * nstars
+ 2 * nescs
+ 1);
1070 for (p0
= SDATA (pattern
); *p0
; p0
++)
1077 *p1
++ = '[', *p1
++ = '^', *p1
++ = '-', *p1
++ = ']';
1080 else if (*p0
== '?')
1083 || *p0
== '.' || *p0
== '\\'
1084 || *p0
== '+' || *p0
== '^'
1086 *p1
++ = '\\', *p1
++ = *p0
;
1093 Vcached_fontset_data
= Fcons (build_string ((char *) SDATA (pattern
)),
1094 build_string ((char *) regex
));
1097 return CACHED_FONTSET_REGEX
;
1100 /* Return ID of the base fontset named NAME. If there's no such
1101 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1102 0: pattern containing '*' and '?' as wildcards
1103 1: regular expression
1104 2: literal fontset name
1108 fs_query_fontset (name
, name_pattern
)
1115 name
= Fdowncase (name
);
1116 if (name_pattern
!= 1)
1118 tem
= Frassoc (name
, Vfontset_alias_alist
);
1120 tem
= Fassoc (name
, Vfontset_alias_alist
);
1121 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
1123 else if (name_pattern
== 0)
1125 tem
= fontset_pattern_regexp (name
);
1134 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
1136 Lisp_Object fontset
, this_name
;
1138 fontset
= FONTSET_FROM_ID (i
);
1140 || !BASE_FONTSET_P (fontset
))
1143 this_name
= FONTSET_NAME (fontset
);
1144 if (name_pattern
== 1
1145 ? fast_string_match_ignore_case (name
, this_name
) >= 0
1146 : !xstrcasecmp (SDATA (name
), SDATA (this_name
)))
1153 DEFUN ("query-fontset", Fquery_fontset
, Squery_fontset
, 1, 2, 0,
1154 doc
: /* Return the name of a fontset that matches PATTERN.
1155 The value is nil if there is no matching fontset.
1156 PATTERN can contain `*' or `?' as a wildcard
1157 just as X font name matching algorithm allows.
1158 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1160 Lisp_Object pattern
, regexpp
;
1162 Lisp_Object fontset
;
1165 (*check_window_system_func
) ();
1167 CHECK_STRING (pattern
);
1169 if (SCHARS (pattern
) == 0)
1172 id
= fs_query_fontset (pattern
, !NILP (regexpp
));
1176 fontset
= FONTSET_FROM_ID (id
);
1177 return FONTSET_NAME (fontset
);
1180 /* Return a list of base fontset names matching PATTERN on frame F. */
1183 list_fontsets (f
, pattern
, size
)
1185 Lisp_Object pattern
;
1188 Lisp_Object frame
, regexp
, val
;
1191 XSETFRAME (frame
, f
);
1193 regexp
= fontset_pattern_regexp (pattern
);
1196 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1198 Lisp_Object fontset
, name
;
1200 fontset
= FONTSET_FROM_ID (id
);
1202 || !BASE_FONTSET_P (fontset
)
1203 || !EQ (frame
, FONTSET_FRAME (fontset
)))
1205 name
= FONTSET_NAME (fontset
);
1207 if (STRINGP (regexp
)
1208 ? (fast_string_match (regexp
, name
) < 0)
1209 : strcmp ((char *) SDATA (pattern
), (char *) SDATA (name
)))
1212 val
= Fcons (Fcopy_sequence (FONTSET_NAME (fontset
)), val
);
1219 /* Free all realized fontsets whose base fontset is BASE. */
1222 free_realized_fontsets (base
)
1228 /* For the moment, this doesn't work because free_realized_face
1229 doesn't remove FACE from a cache. Until we find a solution, we
1230 suppress this code, and simply use Fclear_face_cache even though
1231 that is not efficient. */
1233 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1235 Lisp_Object
this = AREF (Vfontset_table
, id
);
1237 if (EQ (FONTSET_BASE (this), base
))
1241 for (tail
= FONTSET_FACE_ALIST (this); CONSP (tail
);
1244 FRAME_PTR f
= XFRAME (FONTSET_FRAME (this));
1245 int face_id
= XINT (XCDR (XCAR (tail
)));
1246 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1248 /* Face THIS itself is also freed by the following call. */
1249 free_realized_face (f
, face
);
1255 /* But, we don't have to call Fclear_face_cache if no fontset has
1256 been realized from BASE. */
1257 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1259 Lisp_Object
this = AREF (Vfontset_table
, id
);
1261 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base
))
1263 Fclear_face_cache (Qt
);
1271 /* Check validity of NAME as a fontset name and return the
1272 corresponding fontset. If not valid, signal an error.
1273 If NAME is t, return Vdefault_fontset. */
1276 check_fontset_name (name
)
1282 return Vdefault_fontset
;
1284 CHECK_STRING (name
);
1285 /* First try NAME as literal. */
1286 id
= fs_query_fontset (name
, 2);
1288 /* For backward compatibility, try again NAME as pattern. */
1289 id
= fs_query_fontset (name
, 0);
1291 error ("Fontset `%s' does not exist", SDATA (name
));
1292 return FONTSET_FROM_ID (id
);
1296 accumulate_script_ranges (arg
, range
, val
)
1297 Lisp_Object arg
, range
, val
;
1299 if (EQ (XCAR (arg
), val
))
1302 XSETCDR (arg
, Fcons (Fcons (XCAR (range
), XCDR (range
)), XCDR (arg
)));
1304 XSETCDR (arg
, Fcons (Fcons (range
, range
), XCDR (arg
)));
1309 /* Return an ASCII font name generated from fontset name NAME and
1310 font-spec ASCII_SPEC. NAME is a string conforming to XLFD. */
1312 static INLINE Lisp_Object
1313 generate_ascii_font_name (name
, ascii_spec
)
1314 Lisp_Object name
, ascii_spec
;
1316 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1320 if (font_parse_xlfd ((char *) SDATA (name
), font_spec
) < 0)
1321 error ("Not an XLFD font name: %s", SDATA (name
));
1322 for (i
= FONT_FOUNDRY_INDEX
; i
< FONT_EXTRA_INDEX
; i
++)
1323 if (! NILP (AREF (ascii_spec
, i
)))
1324 ASET (font_spec
, i
, AREF (ascii_spec
, i
));
1325 i
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1327 error ("Not an XLFD font name: %s", SDATA (name
));
1328 return make_unibyte_string (xlfd
, i
);
1331 /* Variables referred in set_fontset_font. They are set before
1332 map_charset_chars is called in Fset_fontset_font. */
1333 static Lisp_Object font_def_arg
, add_arg
;
1334 static int from_arg
, to_arg
;
1336 /* Callback function for map_charset_chars in Fset_fontset_font. In
1337 FONTSET, set font_def_arg in a fashion specified by add_arg for
1338 characters in RANGE while ignoring the range between from_arg and
1342 set_fontset_font (fontset
, range
)
1343 Lisp_Object fontset
, range
;
1345 if (from_arg
< to_arg
)
1347 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
1349 if (from
< from_arg
)
1355 range2
= Fcons (make_number (to_arg
), XCDR (range
));
1356 FONTSET_ADD (fontset
, range
, font_def_arg
, add_arg
);
1360 range
= Fcons (XCAR (range
), make_number (from_arg
));
1362 else if (to
<= to_arg
)
1367 range
= Fcons (make_number (to_arg
), XCDR (range
));
1370 FONTSET_ADD (fontset
, range
, font_def_arg
, add_arg
);
1373 extern Lisp_Object QCfamily
, QCregistry
;
1375 DEFUN ("set-fontset-font", Fset_fontset_font
, Sset_fontset_font
, 3, 5, 0,
1377 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1379 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1380 In that case, use FONT-SPEC for all characters in the range FROM and
1383 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1384 all characters that belong to the script.
1386 TARGET may be a charset. In that case, use FONT-SPEC for all
1387 characters in the charset.
1389 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1390 that no FONT-SPEC is specified.
1392 FONT-SPEC may one of these:
1393 * A font-spec object made by the function `font-spec' (which see).
1394 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1395 REGISTRY is a font registry name. FAMILY may contain foundry
1396 name, and REGISTRY may contain encoding name.
1397 * A font name string.
1398 * nil, which explicitly specifies that there's no font for TARGET.
1400 Optional 4th argument FRAME, if non-nil, is a frame. This argument is
1401 kept for backward compatibility and has no meaning.
1403 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1404 to the font specifications for TARGET previously set. If it is
1405 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1406 appended. By default, FONT-SPEC overrides the previous settings. */)
1407 (name
, target
, font_spec
, frame
, add
)
1408 Lisp_Object name
, target
, font_spec
, frame
, add
;
1410 Lisp_Object fontset
;
1411 Lisp_Object font_def
, registry
, family
;
1412 Lisp_Object range_list
;
1413 struct charset
*charset
= NULL
;
1415 fontset
= check_fontset_name (name
);
1417 /* The arg FRAME is kept for backward compatibility. We only check
1420 CHECK_LIVE_FRAME (frame
);
1422 if (CONSP (font_spec
))
1424 Lisp_Object spec
= Ffont_spec (0, NULL
);
1426 font_parse_family_registry (XCAR (font_spec
), XCDR (font_spec
), spec
);
1429 else if (STRINGP (font_spec
))
1431 Lisp_Object args
[2];
1432 extern Lisp_Object QCname
;
1435 args
[1] = font_spec
;
1436 font_spec
= Ffont_spec (2, args
);
1438 else if (! NILP (font_spec
) && ! FONT_SPEC_P (font_spec
))
1439 Fsignal (Qfont
, list2 (build_string ("Invalid font-spec"), font_spec
));
1441 if (! NILP (font_spec
))
1443 Lisp_Object encoding
, repertory
;
1445 family
= AREF (font_spec
, FONT_FAMILY_INDEX
);
1446 if (! NILP (family
) )
1447 family
= SYMBOL_NAME (family
);
1448 registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1449 if (! NILP (registry
))
1450 registry
= Fdowncase (SYMBOL_NAME (registry
));
1451 encoding
= find_font_encoding (concat3 (family
, build_string ("-"),
1453 if (NILP (encoding
))
1456 if (SYMBOLP (encoding
))
1458 CHECK_CHARSET (encoding
);
1459 encoding
= repertory
= CHARSET_SYMBOL_ID (encoding
);
1463 repertory
= XCDR (encoding
);
1464 encoding
= XCAR (encoding
);
1465 CHECK_CHARSET (encoding
);
1466 encoding
= CHARSET_SYMBOL_ID (encoding
);
1467 if (! NILP (repertory
) && SYMBOLP (repertory
))
1469 CHECK_CHARSET (repertory
);
1470 repertory
= CHARSET_SYMBOL_ID (repertory
);
1473 FONT_DEF_NEW (font_def
, font_spec
, encoding
, repertory
);
1478 if (CHARACTERP (target
))
1479 range_list
= Fcons (Fcons (target
, target
), Qnil
);
1480 else if (CONSP (target
))
1482 Lisp_Object from
, to
;
1484 from
= Fcar (target
);
1486 CHECK_CHARACTER (from
);
1487 CHECK_CHARACTER (to
);
1488 range_list
= Fcons (target
, Qnil
);
1490 else if (SYMBOLP (target
) && !NILP (target
))
1492 Lisp_Object script_list
;
1496 script_list
= XCHAR_TABLE (Vchar_script_table
)->extras
[0];
1497 if (! NILP (Fmemq (target
, script_list
)))
1499 val
= Fcons (target
, Qnil
);
1500 map_char_table (accumulate_script_ranges
, Qnil
, Vchar_script_table
,
1502 range_list
= XCDR (val
);
1503 if (EQ (target
, Qlatin
) && NILP (FONTSET_ASCII (fontset
)))
1505 if (VECTORP (font_spec
))
1506 val
= generate_ascii_font_name (FONTSET_NAME (fontset
),
1510 FONTSET_ASCII (fontset
) = val
;
1513 if (CHARSETP (target
))
1515 if (EQ (target
, Qascii
) && NILP (FONTSET_ASCII (fontset
)))
1517 if (VECTORP (font_spec
))
1518 font_spec
= generate_ascii_font_name (FONTSET_NAME (fontset
),
1520 FONTSET_ASCII (fontset
) = font_spec
;
1521 range_list
= Fcons (Fcons (make_number (0), make_number (127)),
1526 CHECK_CHARSET_GET_CHARSET (target
, charset
);
1529 else if (NILP (range_list
))
1530 error ("Invalid script or charset name: %s",
1531 SDATA (SYMBOL_NAME (target
)));
1533 else if (NILP (target
))
1534 range_list
= Fcons (Qnil
, Qnil
);
1536 error ("Invalid target for setting a font");
1541 font_def_arg
= font_def
;
1543 if (NILP (range_list
))
1544 from_arg
= to_arg
= 0;
1546 from_arg
= XINT (XCAR (XCAR (range_list
))),
1547 to_arg
= XINT (XCDR (XCAR (range_list
)));
1549 map_charset_chars (set_fontset_font
, Qnil
, fontset
, charset
,
1550 CHARSET_MIN_CODE (charset
),
1551 CHARSET_MAX_CODE (charset
));
1553 for (; CONSP (range_list
); range_list
= XCDR (range_list
))
1554 FONTSET_ADD (fontset
, XCAR (range_list
), font_def
, add
);
1556 /* Free all realized fontsets whose base is FONTSET. This way, the
1557 specified character(s) are surely redisplayed by a correct
1559 free_realized_fontsets (fontset
);
1565 DEFUN ("new-fontset", Fnew_fontset
, Snew_fontset
, 2, 2, 0,
1566 doc
: /* Create a new fontset NAME from font information in FONTLIST.
1568 FONTLIST is an alist of scripts vs the corresponding font specification list.
1569 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1570 character of SCRIPT is displayed by a font that matches one of
1573 SCRIPT is a symbol that appears in the first extra slot of the
1574 char-table `char-script-table'.
1576 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1577 `set-fontset-font' for the meaning. */)
1579 Lisp_Object name
, fontlist
;
1581 Lisp_Object fontset
;
1584 CHECK_STRING (name
);
1585 CHECK_LIST (fontlist
);
1587 name
= Fdowncase (name
);
1588 id
= fs_query_fontset (name
, 0);
1591 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1592 Lisp_Object short_name
;
1596 if (font_parse_xlfd ((char *) SDATA (name
), font_spec
) < 0)
1597 error ("Fontset name must be in XLFD format");
1598 short_name
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1599 if (strncmp ((char *) SDATA (SYMBOL_NAME (short_name
)), "fontset-", 8)
1600 || SBYTES (SYMBOL_NAME (short_name
)) < 9)
1601 error ("Registry field of fontset name must be \"fontset-*\"");
1602 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (short_name
)),
1603 Vfontset_alias_alist
);
1604 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso8859_1
);
1605 fontset
= make_fontset (Qnil
, name
, Qnil
);
1606 len
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1608 error ("Invalid fontset name (perhaps too long): %s", SDATA (name
));
1609 FONTSET_ASCII (fontset
) = make_unibyte_string (xlfd
, len
);
1613 fontset
= FONTSET_FROM_ID (id
);;
1614 free_realized_fontsets (fontset
);
1615 Fset_char_table_range (fontset
, Qt
, Qnil
);
1618 for (; ! NILP (fontlist
); fontlist
= Fcdr (fontlist
))
1620 Lisp_Object elt
, script
;
1622 elt
= Fcar (fontlist
);
1623 script
= Fcar (elt
);
1625 if (CONSP (elt
) && (NILP (XCDR (elt
)) || CONSP (XCDR (elt
))))
1626 for (; CONSP (elt
); elt
= XCDR (elt
))
1627 Fset_fontset_font (name
, script
, XCAR (elt
), Qnil
, Qappend
);
1629 Fset_fontset_font (name
, script
, elt
, Qnil
, Qappend
);
1635 /* Alist of automatically created fontsets. Each element is a cons
1636 (FONT-SPEC . FONTSET-ID). */
1637 static Lisp_Object auto_fontset_alist
;
1640 fontset_from_font (font_object
)
1641 Lisp_Object font_object
;
1643 Lisp_Object font_name
= font_get_name (font_object
);
1644 Lisp_Object font_spec
= Fcopy_font_spec (font_object
);
1645 Lisp_Object fontset_spec
, alias
, name
, fontset
;
1649 val
= assoc_no_quit (font_spec
, auto_fontset_alist
);
1651 return XINT (FONTSET_ID (XCDR (val
)));
1652 if (NILP (auto_fontset_alist
))
1653 alias
= intern ("fontset-startup");
1657 int len
= XINT (Flength (auto_fontset_alist
));
1659 sprintf (temp
, "fontset-auto%d", len
);
1660 alias
= intern (temp
);
1662 fontset_spec
= Fcopy_font_spec (font_spec
);
1663 ASET (fontset_spec
, FONT_REGISTRY_INDEX
, alias
);
1664 name
= Ffont_xlfd_name (fontset_spec
, Qnil
);
1667 fontset
= make_fontset (Qnil
, name
, Qnil
);
1668 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (alias
)),
1669 Vfontset_alias_alist
);
1670 alias
= Fdowncase (AREF (font_object
, FONT_NAME_INDEX
));
1671 Vfontset_alias_alist
= Fcons (Fcons (name
, alias
), Vfontset_alias_alist
);
1672 auto_fontset_alist
= Fcons (Fcons (font_spec
, fontset
), auto_fontset_alist
);
1673 FONTSET_ASCII (fontset
) = font_name
;
1674 font_spec
= Fcopy_font_spec (font_spec
);
1675 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso10646_1
);
1676 for (i
= FONT_WEIGHT_INDEX
; i
< FONT_EXTRA_INDEX
; i
++)
1677 ASET (font_spec
, i
, Qnil
);
1678 Fset_fontset_font (name
, Qlatin
, font_spec
, Qnil
, Qnil
);
1679 Fset_fontset_font (name
, Qnil
, font_spec
, Qnil
, Qnil
);
1682 nsfont_make_fontset_for_font(name
, font_object
);
1685 return XINT (FONTSET_ID (fontset
));
1688 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1689 FONT-OBJECT is the font for the character at POSITION in the current
1690 buffer. This is computed from all the text properties and overlays
1691 that apply to POSITION. POSTION may be nil, in which case,
1692 FONT-SPEC is the font for displaying the character CH with the
1695 GLYPH-CODE is the glyph code in the font to use for the character.
1697 If the 2nd optional arg CH is non-nil, it is a character to check
1698 the font instead of the character at POSITION.
1700 It returns nil in the following cases:
1702 (1) The window system doesn't have a font for the character (thus
1703 it is displayed by an empty box).
1705 (2) The character code is invalid.
1707 (3) If POSITION is not nil, and the current buffer is not displayed
1710 In addition, the returned font name may not take into account of
1711 such redisplay engine hooks as what used in jit-lock-mode if
1712 POSITION is currently not visible. */
1715 DEFUN ("internal-char-font", Finternal_char_font
, Sinternal_char_font
, 1, 2, 0,
1716 doc
: /* For internal use only. */)
1718 Lisp_Object position
, ch
;
1720 EMACS_INT pos
, pos_byte
, dummy
;
1727 if (NILP (position
))
1729 CHECK_CHARACTER (ch
);
1731 f
= XFRAME (selected_frame
);
1732 face_id
= lookup_basic_face (f
, DEFAULT_FACE_ID
);
1738 Lisp_Object window
, charset
;
1741 CHECK_NUMBER_COERCE_MARKER (position
);
1742 pos
= XINT (position
);
1743 if (pos
< BEGV
|| pos
>= ZV
)
1744 args_out_of_range_3 (position
, make_number (BEGV
), make_number (ZV
));
1745 pos_byte
= CHAR_TO_BYTE (pos
);
1747 c
= FETCH_CHAR (pos_byte
);
1753 window
= Fget_buffer_window (Fcurrent_buffer (), Qnil
);
1756 w
= XWINDOW (window
);
1757 f
= XFRAME (w
->frame
);
1758 face_id
= face_at_buffer_position (w
, pos
, -1, -1, &dummy
, pos
+ 100, 0);
1759 charset
= Fget_char_property (position
, Qcharset
, Qnil
);
1760 if (CHARSETP (charset
))
1761 cs_id
= XINT (CHARSET_SYMBOL_ID (charset
));
1765 if (! CHAR_VALID_P (c
, 0))
1767 face_id
= FACE_FOR_CHAR (f
, FACE_FROM_ID (f
, face_id
), c
, pos
, Qnil
);
1768 face
= FACE_FROM_ID (f
, face_id
);
1771 unsigned code
= face
->font
->driver
->encode_char (face
->font
, c
);
1772 Lisp_Object font_object
;
1773 /* Assignment to EMACS_INT stops GCC whining about limited range
1775 EMACS_INT cod
= code
;
1777 if (code
== FONT_INVALID_CODE
)
1779 XSETFONT (font_object
, face
->font
);
1780 if (cod
<= MOST_POSITIVE_FIXNUM
)
1781 return Fcons (font_object
, make_number (code
));
1782 return Fcons (font_object
, Fcons (make_number (code
>> 16),
1783 make_number (code
& 0xFFFF)));
1789 DEFUN ("fontset-info", Ffontset_info
, Sfontset_info
, 1, 2, 0,
1790 doc
: /* Return information about a fontset FONTSET on frame FRAME.
1791 The value is a char-table whose elements have this form:
1793 ((FONT-PATTERN OPENED-FONT ...) ...)
1795 FONT-PATTERN is a vector:
1797 [ FAMILY WEIGHT SLANT SWIDTH ADSTYLE REGISTRY ]
1799 or a string of font name pattern.
1801 OPENED-FONT is a name of a font actually opened.
1803 The char-table has one extra slot. The value is a char-table
1804 containing the information about the derived fonts from the default
1805 fontset. The format is the same as above. */)
1807 Lisp_Object fontset
, frame
;
1810 Lisp_Object
*realized
[2], fontsets
[2], tables
[2];
1811 Lisp_Object val
, elt
;
1814 (*check_window_system_func
) ();
1816 fontset
= check_fontset_name (fontset
);
1819 frame
= selected_frame
;
1820 CHECK_LIVE_FRAME (frame
);
1823 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1824 in the table `realized'. */
1825 realized
[0] = (Lisp_Object
*) alloca (sizeof (Lisp_Object
)
1826 * ASIZE (Vfontset_table
));
1827 for (i
= j
= 0; i
< ASIZE (Vfontset_table
); i
++)
1829 elt
= FONTSET_FROM_ID (i
);
1831 && EQ (FONTSET_BASE (elt
), fontset
)
1832 && EQ (FONTSET_FRAME (elt
), frame
))
1833 realized
[0][j
++] = elt
;
1835 realized
[0][j
] = Qnil
;
1837 realized
[1] = (Lisp_Object
*) alloca (sizeof (Lisp_Object
)
1838 * ASIZE (Vfontset_table
));
1839 for (i
= j
= 0; ! NILP (realized
[0][i
]); i
++)
1841 elt
= FONTSET_DEFAULT (realized
[0][i
]);
1843 realized
[1][j
++] = elt
;
1845 realized
[1][j
] = Qnil
;
1847 tables
[0] = Fmake_char_table (Qfontset_info
, Qnil
);
1848 tables
[1] = Fmake_char_table (Qnil
, Qnil
);
1849 XCHAR_TABLE (tables
[0])->extras
[0] = tables
[1];
1850 fontsets
[0] = fontset
;
1851 fontsets
[1] = Vdefault_fontset
;
1853 /* Accumulate information of the fontset in TABLE. The format of
1854 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1855 for (k
= 0; k
<= 1; k
++)
1857 for (c
= 0; c
<= MAX_CHAR
; )
1859 int from
= c
, to
= MAX_5_BYTE_CHAR
;
1861 if (c
<= MAX_5_BYTE_CHAR
)
1863 val
= char_table_ref_and_range (fontsets
[k
], c
, &from
, &to
);
1867 val
= FONTSET_FALLBACK (fontsets
[k
]);
1874 /* At first, set ALIST to ((FONT-SPEC) ...). */
1875 for (alist
= Qnil
, i
= 0; i
< ASIZE (val
); i
++)
1876 if (! NILP (AREF (val
, i
)))
1877 alist
= Fcons (Fcons (FONT_DEF_SPEC (AREF (val
, i
)), Qnil
),
1879 alist
= Fnreverse (alist
);
1881 /* Then store opened font names to cdr of each elements. */
1882 for (i
= 0; ! NILP (realized
[k
][i
]); i
++)
1884 if (c
<= MAX_5_BYTE_CHAR
)
1885 val
= FONTSET_REF (realized
[k
][i
], c
);
1887 val
= FONTSET_FALLBACK (realized
[k
][i
]);
1888 if (! VECTORP (val
))
1890 /* VAL: [int ? [FACE-ID FONT-DEF FONT-OBJECT int] ... ] */
1891 for (j
= 2; j
< ASIZE (val
); j
++)
1893 elt
= AREF (val
, j
);
1894 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt
)))
1896 Lisp_Object font_object
= RFONT_DEF_OBJECT (elt
);
1897 Lisp_Object slot
, name
;
1899 slot
= Fassq (RFONT_DEF_SPEC (elt
), alist
);
1900 name
= AREF (font_object
, FONT_NAME_INDEX
);
1901 if (NILP (Fmember (name
, XCDR (slot
))))
1902 nconc2 (slot
, Fcons (name
, Qnil
));
1907 /* Store ALIST in TBL for characters C..TO. */
1908 if (c
<= MAX_5_BYTE_CHAR
)
1909 char_table_set_range (tables
[k
], c
, to
, alist
);
1911 XCHAR_TABLE (tables
[k
])->defalt
= alist
;
1913 /* At last, change each elements to font names. */
1914 for (; CONSP (alist
); alist
= XCDR (alist
))
1917 XSETCAR (elt
, Ffont_xlfd_name (XCAR (elt
), Qnil
));
1928 DEFUN ("fontset-font", Ffontset_font
, Sfontset_font
, 2, 3, 0,
1929 doc
: /* Return a font name pattern for character CH in fontset NAME.
1930 If NAME is t, find a pattern in the default fontset.
1932 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
1933 family name and REGISTRY is a font registry name. This is actually
1934 the first font name pattern for CH in the fontset or in the default
1937 If the 2nd optional arg ALL is non-nil, return a list of all font name
1940 Lisp_Object name
, ch
, all
;
1943 Lisp_Object fontset
, elt
, list
, repertory
, val
;
1946 fontset
= check_fontset_name (name
);
1948 CHECK_CHARACTER (ch
);
1953 for (i
= 0, elt
= FONTSET_REF (fontset
, c
); i
< 2;
1954 i
++, elt
= FONTSET_FALLBACK (fontset
))
1956 for (j
= 0; j
< ASIZE (elt
); j
++)
1958 Lisp_Object family
, registry
;
1960 val
= AREF (elt
, j
);
1961 repertory
= AREF (val
, 1);
1962 if (INTEGERP (repertory
))
1964 struct charset
*charset
= CHARSET_FROM_ID (XINT (repertory
));
1966 if (! CHAR_CHARSET_P (c
, charset
))
1969 else if (CHAR_TABLE_P (repertory
))
1971 if (NILP (CHAR_TABLE_REF (repertory
, c
)))
1974 val
= AREF (val
, 0);
1975 /* VAL is a FONT-SPEC */
1976 family
= AREF (val
, FONT_FAMILY_INDEX
);
1977 if (! NILP (family
))
1978 family
= SYMBOL_NAME (family
);
1979 registry
= AREF (val
, FONT_REGISTRY_INDEX
);
1980 if (! NILP (registry
))
1981 registry
= SYMBOL_NAME (registry
);
1982 val
= Fcons (family
, registry
);
1985 list
= Fcons (val
, list
);
1987 if (EQ (fontset
, Vdefault_fontset
))
1989 fontset
= Vdefault_fontset
;
1991 return (Fnreverse (list
));
1994 DEFUN ("fontset-list", Ffontset_list
, Sfontset_list
, 0, 0, 0,
1995 doc
: /* Return a list of all defined fontset names. */)
1998 Lisp_Object fontset
, list
;
2002 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
2004 fontset
= FONTSET_FROM_ID (i
);
2006 && BASE_FONTSET_P (fontset
))
2007 list
= Fcons (FONTSET_NAME (fontset
), list
);
2014 #ifdef FONTSET_DEBUG
2017 dump_fontset (fontset
)
2018 Lisp_Object fontset
;
2022 vec
= Fmake_vector (make_number (3), Qnil
);
2023 ASET (vec
, 0, FONTSET_ID (fontset
));
2025 if (BASE_FONTSET_P (fontset
))
2027 ASET (vec
, 1, FONTSET_NAME (fontset
));
2033 frame
= FONTSET_FRAME (fontset
);
2036 FRAME_PTR f
= XFRAME (frame
);
2038 if (FRAME_LIVE_P (f
))
2040 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), f
->name
));
2043 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), Qnil
));
2045 if (!NILP (FONTSET_DEFAULT (fontset
)))
2046 ASET (vec
, 2, FONTSET_ID (FONTSET_DEFAULT (fontset
)));
2051 DEFUN ("fontset-list-all", Ffontset_list_all
, Sfontset_list_all
, 0, 0, 0,
2052 doc
: /* Return a brief summary of all fontsets for debug use. */)
2058 for (i
= 0, val
= Qnil
; i
< ASIZE (Vfontset_table
); i
++)
2059 if (! NILP (AREF (Vfontset_table
, i
)))
2060 val
= Fcons (dump_fontset (AREF (Vfontset_table
, i
)), val
);
2061 return (Fnreverse (val
));
2063 #endif /* FONTSET_DEBUG */
2068 DEFSYM (Qfontset
, "fontset");
2069 Fput (Qfontset
, Qchar_table_extra_slots
, make_number (9));
2070 DEFSYM (Qfontset_info
, "fontset-info");
2071 Fput (Qfontset_info
, Qchar_table_extra_slots
, make_number (1));
2073 DEFSYM (Qprepend
, "prepend");
2074 DEFSYM (Qappend
, "append");
2075 DEFSYM (Qlatin
, "latin");
2077 Vcached_fontset_data
= Qnil
;
2078 staticpro (&Vcached_fontset_data
);
2080 Vfontset_table
= Fmake_vector (make_number (32), Qnil
);
2081 staticpro (&Vfontset_table
);
2083 Vdefault_fontset
= Fmake_char_table (Qfontset
, Qnil
);
2084 staticpro (&Vdefault_fontset
);
2085 FONTSET_ID (Vdefault_fontset
) = make_number (0);
2086 FONTSET_NAME (Vdefault_fontset
)
2087 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
2088 ASET (Vfontset_table
, 0, Vdefault_fontset
);
2089 next_fontset_id
= 1;
2091 auto_fontset_alist
= Qnil
;
2092 staticpro (&auto_fontset_alist
);
2094 DEFVAR_LISP ("font-encoding-charset-alist", &Vfont_encoding_charset_alist
,
2096 Alist of charsets vs the charsets to determine the preferred font encoding.
2097 Each element looks like (CHARSET . ENCODING-CHARSET),
2098 where ENCODING-CHARSET is a charset registered in the variable
2099 `font-encoding-alist' as ENCODING.
2101 When a text has a property `charset' and the value is CHARSET, a font
2102 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2103 Vfont_encoding_charset_alist
= Qnil
;
2105 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent
,
2107 Char table of characters whose ascent values should be ignored.
2108 If an entry for a character is non-nil, the ascent value of the glyph
2109 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2111 This affects how a composite character which contains
2112 such a character is displayed on screen. */);
2113 Vuse_default_ascent
= Qnil
;
2115 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition
,
2117 Char table of characters which are not composed relatively.
2118 If an entry for a character is non-nil, a composition sequence
2119 which contains that character is displayed so that
2120 the glyph of that character is put without considering
2121 an ascent and descent value of a previous character. */);
2122 Vignore_relative_composition
= Qnil
;
2124 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist
,
2125 doc
: /* Alist of fontname vs list of the alternate fontnames.
2126 When a specified font name is not found, the corresponding
2127 alternate fontnames (if any) are tried instead. */);
2128 Valternate_fontname_alist
= Qnil
;
2130 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist
,
2131 doc
: /* Alist of fontset names vs the aliases. */);
2132 Vfontset_alias_alist
= Fcons (Fcons (FONTSET_NAME (Vdefault_fontset
),
2133 build_string ("fontset-default")),
2136 DEFVAR_LISP ("vertical-centering-font-regexp",
2137 &Vvertical_centering_font_regexp
,
2138 doc
: /* *Regexp matching font names that require vertical centering on display.
2139 When a character is displayed with such fonts, the character is displayed
2140 at the vertical center of lines. */);
2141 Vvertical_centering_font_regexp
= Qnil
;
2143 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist
,
2144 doc
: /* Alist of OpenType script tags vs the corresponding script names. */);
2145 Votf_script_alist
= Qnil
;
2147 defsubr (&Squery_fontset
);
2148 defsubr (&Snew_fontset
);
2149 defsubr (&Sset_fontset_font
);
2150 defsubr (&Sinternal_char_font
);
2151 defsubr (&Sfontset_info
);
2152 defsubr (&Sfontset_font
);
2153 defsubr (&Sfontset_list
);
2154 #ifdef FONTSET_DEBUG
2155 defsubr (&Sfontset_list_all
);
2159 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2160 (do not change this comment) */