3 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
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/>. */
31 #include "blockinput.h"
32 #include "character.h"
38 #include "dispextern.h"
39 #include "intervals.h"
42 #ifdef HAVE_WINDOW_SYSTEM
44 #endif /* HAVE_WINDOW_SYSTEM */
45 #include "termhooks.h"
50 A fontset is a collection of font related information to give
51 similar appearance (style, etc) of characters. A fontset has two
52 roles. One is to use for the frame parameter `font' as if it is an
53 ASCII font. In that case, Emacs uses the font specified for
54 `ascii' script for the frame's default font.
56 Another role, the more important one, is to provide information
57 about which font to use for each non-ASCII character.
59 There are two kinds of fontsets; base and realized. A base fontset
60 is created by `new-fontset' from Emacs Lisp explicitly. A realized
61 fontset is created implicitly when a face is realized for ASCII
62 characters. A face is also realized for non-ASCII characters based
63 on an ASCII face. All of non-ASCII faces based on the same ASCII
64 face share the same realized fontset.
66 A fontset object is implemented by a char-table whose default value
67 and parent are always nil.
69 An element of a base fontset is a vector of FONT-DEFs which itself
70 is a vector [ FONT-SPEC ENCODING REPERTORY ].
72 An element of a realized fontset is nil, t, 0, or a vector of this
75 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
76 RFONT-DEF0 RFONT-DEF1 ... ]
78 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
80 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
82 RFONT-DEFn are automatically reordered by the current charset
85 The value nil means that we have not yet generated the above vector
86 from the base of the fontset.
88 The value t means that no font is available for the corresponding
91 The value 0 means that no font is available for the corresponding
92 range of characters in this fontset, but may be available in the
95 A fontset has 8 extra slots.
98 base: the ID number of the fontset
102 base: the name of the fontset
106 base: the font name for ASCII characters
111 realized: the base fontset
115 realized: the frame that the fontset belongs to
119 realized: the ID number of a face to use for characters that
120 has no font in a realized fontset.
124 realized: If the base is not the default fontset, a fontset
125 realized from the default fontset, else nil.
128 base: Same as element value (but for fallback fonts).
131 All fontsets are recorded in the vector Vfontset_table.
136 There's a special base fontset named `default fontset' which
137 defines the default font specifications. When a base fontset
138 doesn't specify a font for a specific character, the corresponding
139 value in the default fontset is used.
141 The parent of a realized fontset created for such a face that has
142 no fontset is the default fontset.
145 These structures are hidden from the other codes than this file.
146 The other codes handle fontsets only by their ID numbers. They
147 usually use the variable name `fontset' for IDs. But, in this
148 file, we always use variable name `id' for IDs, and name `fontset'
149 for an actual fontset object, i.e., char-table.
153 /********** VARIABLES and FUNCTION PROTOTYPES **********/
155 static Lisp_Object Qfontset
;
156 static Lisp_Object Qfontset_info
;
157 static Lisp_Object Qprepend
, Qappend
;
160 /* Vector containing all fontsets. */
161 static Lisp_Object Vfontset_table
;
163 /* Next possibly free fontset ID. Usually this keeps the minimum
164 fontset ID not yet used. */
165 static int next_fontset_id
;
167 /* The default fontset. This gives default FAMILY and REGISTRY of
168 font for each character. */
169 static Lisp_Object Vdefault_fontset
;
171 /* Prototype declarations for static functions. */
172 static Lisp_Object
make_fontset (Lisp_Object
, Lisp_Object
, Lisp_Object
);
174 /* Return true if ID is a valid fontset id.
175 Optimized away if ENABLE_CHECKING is not defined. */
178 fontset_id_valid_p (int id
)
180 return (id
>= 0 && id
< ASIZE (Vfontset_table
) - 1);
185 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
187 /* Return the fontset with ID. No check of ID's validness. */
188 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
190 /* Access special values of FONTSET. */
192 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
194 set_fontset_id (Lisp_Object fontset
, Lisp_Object id
)
196 set_char_table_extras (fontset
, 0, id
);
199 /* Access special values of (base) FONTSET. */
201 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
203 set_fontset_name (Lisp_Object fontset
, Lisp_Object name
)
205 set_char_table_extras (fontset
, 1, name
);
208 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
210 set_fontset_ascii (Lisp_Object fontset
, Lisp_Object ascii
)
212 set_char_table_extras (fontset
, 2, ascii
);
215 /* Access special values of (realized) FONTSET. */
217 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
219 set_fontset_base (Lisp_Object fontset
, Lisp_Object base
)
221 set_char_table_extras (fontset
, 3, base
);
224 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
226 set_fontset_frame (Lisp_Object fontset
, Lisp_Object frame
)
228 set_char_table_extras (fontset
, 4, frame
);
231 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
233 set_fontset_nofont_face (Lisp_Object fontset
, Lisp_Object face
)
235 set_char_table_extras (fontset
, 5, face
);
238 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
240 set_fontset_default (Lisp_Object fontset
, Lisp_Object def
)
242 set_char_table_extras (fontset
, 6, def
);
245 /* For both base and realized fontset. */
247 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
249 set_fontset_fallback (Lisp_Object fontset
, Lisp_Object fallback
)
251 set_char_table_extras (fontset
, 7, fallback
);
254 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
256 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
257 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
259 (font_def) = make_uninit_vector (3); \
260 ASET ((font_def), 0, font_spec); \
261 ASET ((font_def), 1, encoding); \
262 ASET ((font_def), 2, repertory); \
265 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
266 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
267 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
269 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
270 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
271 ASET ((rfont_def), 0, make_number (face_id))
272 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
273 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
274 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
275 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
276 ASET ((rfont_def), 2, (object))
277 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
278 the order of listing by font backends, the higher bits represents
279 the order given by charset priority list. The smaller value is
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 (Lisp_Object fontset
, int c
)
307 elt
= CHAR_TABLE_REF (fontset
, c
);
308 if (NILP (elt
) && ! EQ (fontset
, Vdefault_fontset
)
309 /* Don't check Vdefault_fontset for a realized fontset. */
310 && NILP (FONTSET_BASE (fontset
)))
311 elt
= CHAR_TABLE_REF (Vdefault_fontset
, c
);
315 /* Set elements of FONTSET for characters in RANGE to the value ELT.
316 RANGE is a cons (FROM . TO), where FROM and TO are character codes
317 specifying a range. */
319 #define FONTSET_SET(fontset, range, elt) \
320 Fset_char_table_range ((fontset), (range), (elt))
323 /* Modify the elements of FONTSET for characters in RANGE by replacing
324 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
325 and TO are character codes specifying a range. If ADD is nil,
326 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
329 #define FONTSET_ADD(fontset, range, elt, add) \
332 ? (set_fontset_fallback \
333 (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 (Lisp_Object fontset
, Lisp_Object range
, Lisp_Object elt
, Lisp_Object add
)
343 int idx
= (EQ (add
, Qappend
) ? 0 : 1);
345 args
[1 - idx
] = Fmake_vector (make_number (1), elt
);
349 int from
= XINT (XCAR (range
));
350 int to
= XINT (XCDR (range
));
354 from1
= from
, to1
= to
;
355 args
[idx
] = char_table_ref_and_range (fontset
, from
, &from1
, &to1
);
356 char_table_set_range (fontset
, from
, to1
,
357 NILP (args
[idx
]) ? args
[1 - idx
]
358 : Fvconcat (2, args
));
364 args
[idx
] = FONTSET_FALLBACK (fontset
);
366 (fontset
, NILP (args
[idx
]) ? args
[1 - idx
] : Fvconcat (2, args
));
371 fontset_compare_rfontdef (const void *val1
, const void *val2
)
373 return (RFONT_DEF_SCORE (*(Lisp_Object
*) val1
)
374 - RFONT_DEF_SCORE (*(Lisp_Object
*) val2
));
377 /* Update FONT-GROUP which has this form:
378 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
379 RFONT-DEF0 RFONT-DEF1 ... ]
380 Reorder RFONT-DEFs according to the current language, and update
381 CHARSET-ORDERED-LIST-TICK.
383 If PREFERRED_FAMILY is not nil, that family has the higher priority
384 if the encoding charsets or languages in font-specs are the same. */
387 reorder_font_vector (Lisp_Object font_group
, struct font
*font
)
389 Lisp_Object vec
, font_object
;
392 bool score_changed
= false;
395 XSETFONT (font_object
, font
);
399 vec
= XCDR (font_group
);
401 /* Exclude the tailing nil element from the reordering. */
402 if (NILP (AREF (vec
, size
- 1)))
405 for (i
= 0; i
< size
; i
++)
407 Lisp_Object rfont_def
= AREF (vec
, i
);
408 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
409 Lisp_Object font_spec
= FONT_DEF_SPEC (font_def
);
410 int score
= RFONT_DEF_SCORE (rfont_def
) & 0xFF;
411 Lisp_Object otf_spec
= Ffont_get (font_spec
, QCotf
);
413 if (! NILP (otf_spec
))
414 /* A font-spec with :otf is preferable regardless of encoding
417 else if (! font_match_p (font_spec
, font_object
))
419 Lisp_Object encoding
= FONT_DEF_ENCODING (font_def
);
421 if (! NILP (encoding
))
425 for (tail
= Vcharset_ordered_list
;
426 ! EQ (tail
, Vcharset_non_preferred_head
) && CONSP (tail
);
428 if (EQ (encoding
, XCAR (tail
)))
430 else if (score
<= min (INT_MAX
, MOST_POSITIVE_FIXNUM
) - 0x100)
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
);
447 score_changed
= true;
452 qsort (XVECTOR (vec
)->contents
, size
, word_size
,
453 fontset_compare_rfontdef
);
454 EMACS_INT low_tick_bits
= charset_ordered_list_tick
& MOST_POSITIVE_FIXNUM
;
455 XSETCAR (font_group
, make_number (low_tick_bits
));
458 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
459 character C in FONTSET. If C is -1, return a fallback font-group.
460 If C is not -1, the value may be Qt (FONTSET doesn't have a font
461 for C even in the fallback group), or 0 (a font for C may be found
462 only in the fallback group). */
465 fontset_get_font_group (Lisp_Object fontset
, int c
)
467 Lisp_Object font_group
;
468 Lisp_Object base_fontset
;
469 int from
= 0, to
= MAX_CHAR
, i
;
471 eassert (! BASE_FONTSET_P (fontset
));
473 font_group
= CHAR_TABLE_REF (fontset
, c
);
475 font_group
= FONTSET_FALLBACK (fontset
);
476 if (! NILP (font_group
))
478 base_fontset
= FONTSET_BASE (fontset
);
479 if (NILP (base_fontset
))
482 font_group
= char_table_ref_and_range (base_fontset
, c
, &from
, &to
);
484 font_group
= FONTSET_FALLBACK (base_fontset
);
485 if (NILP (font_group
))
487 font_group
= make_number (0);
489 char_table_set_range (fontset
, from
, to
, font_group
);
492 if (!VECTORP (font_group
))
494 font_group
= Fcopy_sequence (font_group
);
495 for (i
= 0; i
< ASIZE (font_group
); i
++)
496 if (! NILP (AREF (font_group
, i
)))
498 Lisp_Object rfont_def
;
500 RFONT_DEF_NEW (rfont_def
, AREF (font_group
, i
));
501 /* Remember the original order. */
502 RFONT_DEF_SET_SCORE (rfont_def
, i
);
503 ASET (font_group
, i
, rfont_def
);
505 font_group
= Fcons (make_number (-1), font_group
);
507 char_table_set_range (fontset
, from
, to
, font_group
);
509 set_fontset_fallback (fontset
, font_group
);
513 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
514 character C. If no font is found, return Qnil if there's a
515 possibility that the default fontset or the fallback font groups
516 have a proper font, and return Qt if not.
518 If a font is found but is not yet opened, open it (if FACE is not
519 NULL) or return Qnil (if FACE is NULL).
521 ID is a charset-id that must be preferred, or -1 meaning no
524 If FALLBACK, search only fallback fonts. */
527 fontset_find_font (Lisp_Object fontset
, int c
, struct face
*face
, int id
,
530 Lisp_Object vec
, font_group
;
531 int i
, charset_matched
= 0, found_index
;
532 struct frame
*f
= (FRAMEP (FONTSET_FRAME (fontset
))
533 ? XFRAME (FONTSET_FRAME (fontset
))
534 : XFRAME (selected_frame
));
535 Lisp_Object rfont_def
;
537 font_group
= fontset_get_font_group (fontset
, fallback
? -1 : c
);
538 if (! CONSP (font_group
))
540 vec
= XCDR (font_group
);
541 if (ASIZE (vec
) == 0)
546 if (XINT (XCAR (font_group
)) != charset_ordered_list_tick
)
547 /* We have just created the font-group,
548 or the charset priorities were changed. */
549 reorder_font_vector (font_group
, face
->ascii_face
->font
);
551 /* Find a spec matching with the charset ID to try at
553 for (i
= 0; i
< ASIZE (vec
); i
++)
555 Lisp_Object repertory
;
557 rfont_def
= AREF (vec
, i
);
558 if (NILP (rfont_def
))
560 repertory
= FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def
));
562 if (XINT (repertory
) == id
)
570 /* Find the first available font in the vector of RFONT-DEF. */
571 for (i
= 0; i
< ASIZE (vec
); i
++)
573 Lisp_Object font_def
;
574 Lisp_Object font_entity
, font_object
;
579 if (charset_matched
> 0)
581 /* Try the element matching with the charset ID at first. */
582 found_index
= charset_matched
;
583 /* Make this negative so that we don't come here in the
585 charset_matched
= - charset_matched
;
586 /* We must try the first element in the next loop. */
590 else if (i
== - charset_matched
)
592 /* We have already tried this element and the followings
593 that have the same font specifications in the first
594 iteration. So, skip them all. */
595 rfont_def
= AREF (vec
, i
);
596 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
597 for (; i
+ 1 < ASIZE (vec
); i
++)
599 rfont_def
= AREF (vec
, i
+ 1);
600 if (NILP (rfont_def
))
602 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def
), font_def
))
608 rfont_def
= AREF (vec
, found_index
);
609 if (NILP (rfont_def
))
613 /* This is a sign of not to try the other fonts. */
616 if (INTEGERP (RFONT_DEF_FACE (rfont_def
))
617 && XINT (RFONT_DEF_FACE (rfont_def
)) < 0)
618 /* We couldn't open this font last time. */
621 font_object
= RFONT_DEF_OBJECT (rfont_def
);
622 if (NILP (font_object
))
624 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
627 /* We have not yet opened the font. */
629 /* Find a font best-matching with the spec without checking
630 the support of the character C. That checking is costly,
631 and even without the checking, the found font supports C
632 in high possibility. */
633 font_entity
= font_find_for_lface (f
, face
->lface
,
634 FONT_DEF_SPEC (font_def
), -1);
635 if (NILP (font_entity
))
637 /* Record that no font matches the spec. */
638 RFONT_DEF_SET_FACE (rfont_def
, -1);
641 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
642 FONT_DEF_SPEC (font_def
));
643 if (NILP (font_object
))
645 /* Something strange happened, perhaps because of a
646 Font-backend problem. Too avoid crashing, record
647 that this spec is unusable. It may be better to find
648 another font of the same spec, but currently we don't
650 RFONT_DEF_SET_FACE (rfont_def
, -1);
653 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
656 if (font_has_char (f
, font_object
, c
))
659 /* Find a font already opened, matching with the current spec,
661 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
662 for (; found_index
+ 1 < ASIZE (vec
); found_index
++)
664 rfont_def
= AREF (vec
, found_index
+ 1);
665 if (NILP (rfont_def
))
667 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def
), font_def
))
669 font_object
= RFONT_DEF_OBJECT (rfont_def
);
670 if (! NILP (font_object
) && font_has_char (f
, font_object
, c
))
677 /* Find a font-entity with the current spec and supporting C. */
678 font_entity
= font_find_for_lface (f
, face
->lface
,
679 FONT_DEF_SPEC (font_def
), c
);
680 if (! NILP (font_entity
))
682 /* We found a font. Open it and insert a new element for
687 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
689 if (NILP (font_object
))
691 RFONT_DEF_NEW (rfont_def
, font_def
);
692 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
693 RFONT_DEF_SET_SCORE (rfont_def
, RFONT_DEF_SCORE (rfont_def
));
694 new_vec
= Fmake_vector (make_number (ASIZE (vec
) + 1), Qnil
);
696 for (j
= 0; j
< found_index
; j
++)
697 ASET (new_vec
, j
, AREF (vec
, j
));
698 ASET (new_vec
, j
, rfont_def
);
699 for (j
++; j
< ASIZE (new_vec
); j
++)
700 ASET (new_vec
, j
, AREF (vec
, j
- 1));
701 XSETCDR (font_group
, new_vec
);
709 FONTSET_SET (fontset
, make_number (c
), make_number (0));
713 if (fallback
&& found_index
> 0)
715 /* The order of fonts in the fallback font-group is not that
716 important, and it is better to move the found font to the
717 first of the group so that the next try will find it
719 for (i
= found_index
; i
> 0; i
--)
720 ASET (vec
, i
, AREF (vec
, i
- 1));
721 ASET (vec
, 0, rfont_def
);
728 fontset_font (Lisp_Object fontset
, int c
, struct face
*face
, int id
)
730 Lisp_Object rfont_def
, default_rfont_def
IF_LINT (= Qnil
);
731 Lisp_Object base_fontset
;
733 /* Try a font-group of FONTSET. */
734 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c
), Qnil
);
735 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 0);
736 if (VECTORP (rfont_def
))
738 if (NILP (rfont_def
))
739 FONTSET_SET (fontset
, make_number (c
), make_number (0));
741 /* Try a font-group of the default fontset. */
742 base_fontset
= FONTSET_BASE (fontset
);
743 if (! EQ (base_fontset
, Vdefault_fontset
))
745 if (NILP (FONTSET_DEFAULT (fontset
)))
748 make_fontset (FONTSET_FRAME (fontset
), Qnil
, Vdefault_fontset
));
749 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c
), Qnil
);
751 = fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 0);
752 if (VECTORP (default_rfont_def
))
753 return default_rfont_def
;
754 if (NILP (default_rfont_def
))
755 FONTSET_SET (FONTSET_DEFAULT (fontset
), make_number (c
),
759 /* Try a fallback font-group of FONTSET. */
760 if (! EQ (rfont_def
, Qt
))
762 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c
), Qnil
);
763 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 1);
764 if (VECTORP (rfont_def
))
766 /* Remember that FONTSET has no font for C. */
767 FONTSET_SET (fontset
, make_number (c
), Qt
);
770 /* Try a fallback font-group of the default fontset. */
771 if (! EQ (base_fontset
, Vdefault_fontset
)
772 && ! EQ (default_rfont_def
, Qt
))
774 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c
), Qnil
);
775 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 1);
776 if (VECTORP (rfont_def
))
778 /* Remember that the default fontset has no font for C. */
779 FONTSET_SET (FONTSET_DEFAULT (fontset
), make_number (c
), Qt
);
785 /* Return a newly created fontset with NAME. If BASE is nil, make a
786 base fontset. Otherwise make a realized fontset whose base is
790 make_fontset (Lisp_Object frame
, Lisp_Object name
, Lisp_Object base
)
793 int size
= ASIZE (Vfontset_table
);
794 int id
= next_fontset_id
;
796 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
797 the next available fontset ID. So it is expected that this loop
798 terminates quickly. In addition, as the last element of
799 Vfontset_table is always nil, we don't have to check the range of
801 while (!NILP (AREF (Vfontset_table
, id
))) id
++;
804 Vfontset_table
= larger_vector (Vfontset_table
, 1, -1);
806 fontset
= Fmake_char_table (Qfontset
, Qnil
);
808 set_fontset_id (fontset
, make_number (id
));
810 set_fontset_name (fontset
, name
);
813 set_fontset_name (fontset
, Qnil
);
814 set_fontset_frame (fontset
, frame
);
815 set_fontset_base (fontset
, base
);
818 ASET (Vfontset_table
, id
, fontset
);
819 next_fontset_id
= id
+ 1;
824 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
826 /* Return the name of the fontset who has ID. */
829 fontset_name (int id
)
833 fontset
= FONTSET_FROM_ID (id
);
834 return FONTSET_NAME (fontset
);
838 /* Return the ASCII font name of the fontset who has ID. */
841 fontset_ascii (int id
)
843 Lisp_Object fontset
, elt
;
845 fontset
= FONTSET_FROM_ID (id
);
846 elt
= FONTSET_ASCII (fontset
);
852 /* Free fontset of FACE defined on frame F. Called from
853 free_realized_face. */
856 free_face_fontset (struct frame
*f
, struct face
*face
)
860 fontset
= FONTSET_FROM_ID (face
->fontset
);
863 eassert (! BASE_FONTSET_P (fontset
));
864 eassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
865 ASET (Vfontset_table
, face
->fontset
, Qnil
);
866 if (face
->fontset
< next_fontset_id
)
867 next_fontset_id
= face
->fontset
;
868 if (! NILP (FONTSET_DEFAULT (fontset
)))
870 int id
= XINT (FONTSET_ID (FONTSET_DEFAULT (fontset
)));
872 fontset
= AREF (Vfontset_table
, id
);
873 eassert (!NILP (fontset
) && ! BASE_FONTSET_P (fontset
));
874 eassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
875 ASET (Vfontset_table
, id
, Qnil
);
876 if (id
< next_fontset_id
)
877 next_fontset_id
= face
->fontset
;
882 /* Return ID of face suitable for displaying character C at buffer position
883 POS on frame F. FACE must be realized for ASCII characters in advance.
884 Called from the macro FACE_FOR_CHAR. */
887 face_for_char (struct frame
*f
, struct face
*face
, int c
,
888 ptrdiff_t pos
, Lisp_Object object
)
890 Lisp_Object fontset
, rfont_def
, charset
;
894 eassert (fontset_id_valid_p (face
->fontset
));
896 if (ASCII_CHAR_P (c
) || CHAR_BYTE8_P (c
))
897 return face
->ascii_face
->id
;
902 /* Fonts often have characters in other scripts, like symbol, even if they
903 don't match script: symbol. So check if the character is present
904 in the current face first. Only enable for NS for now, but should
905 perhaps be general? */
906 Lisp_Object font_object
;
907 XSETFONT (font_object
, face
->font
);
908 if (font_has_char (f
, font_object
, c
)) return face
->id
;
912 fontset
= FONTSET_FROM_ID (face
->fontset
);
913 eassert (!BASE_FONTSET_P (fontset
));
922 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
923 if (CHARSETP (charset
))
927 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
928 if (CONSP (val
) && CHARSETP (XCDR (val
)))
929 charset
= XCDR (val
);
930 id
= XINT (CHARSET_SYMBOL_ID (charset
));
936 rfont_def
= fontset_font (fontset
, c
, face
, id
);
937 if (VECTORP (rfont_def
))
939 if (INTEGERP (RFONT_DEF_FACE (rfont_def
)))
940 face_id
= XINT (RFONT_DEF_FACE (rfont_def
));
943 Lisp_Object font_object
;
945 font_object
= RFONT_DEF_OBJECT (rfont_def
);
946 face_id
= face_for_font (f
, font_object
, face
);
947 RFONT_DEF_SET_FACE (rfont_def
, face_id
);
952 if (INTEGERP (FONTSET_NOFONT_FACE (fontset
)))
953 face_id
= XINT (FONTSET_NOFONT_FACE (fontset
));
956 face_id
= face_for_font (f
, Qnil
, face
);
957 set_fontset_nofont_face (fontset
, make_number (face_id
));
960 eassert (face_id
>= 0);
966 font_for_char (struct face
*face
, int c
, ptrdiff_t pos
, Lisp_Object object
)
968 Lisp_Object fontset
, rfont_def
, charset
;
971 if (ASCII_CHAR_P (c
))
973 Lisp_Object font_object
;
975 XSETFONT (font_object
, face
->ascii_face
->font
);
979 eassert (fontset_id_valid_p (face
->fontset
));
980 fontset
= FONTSET_FROM_ID (face
->fontset
);
981 eassert (!BASE_FONTSET_P (fontset
));
989 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
990 if (CHARSETP (charset
))
994 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
995 if (CONSP (val
) && CHARSETP (XCDR (val
)))
996 charset
= XCDR (val
);
997 id
= XINT (CHARSET_SYMBOL_ID (charset
));
1003 rfont_def
= fontset_font (fontset
, c
, face
, id
);
1004 return (VECTORP (rfont_def
)
1005 ? RFONT_DEF_OBJECT (rfont_def
)
1010 /* Make a realized fontset for ASCII face FACE on frame F from the
1011 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1012 default fontset as the base. Value is the id of the new fontset.
1013 Called from realize_x_face. */
1016 make_fontset_for_ascii_face (struct frame
*f
, int base_fontset_id
, struct face
*face
)
1018 Lisp_Object base_fontset
, fontset
, frame
;
1020 XSETFRAME (frame
, f
);
1021 if (base_fontset_id
>= 0)
1023 base_fontset
= FONTSET_FROM_ID (base_fontset_id
);
1024 if (!BASE_FONTSET_P (base_fontset
))
1025 base_fontset
= FONTSET_BASE (base_fontset
);
1026 eassert (BASE_FONTSET_P (base_fontset
));
1029 base_fontset
= Vdefault_fontset
;
1031 fontset
= make_fontset (frame
, Qnil
, base_fontset
);
1032 return XINT (FONTSET_ID (fontset
));
1037 /* Cache data used by fontset_pattern_regexp. The car part is a
1038 pattern string containing at least one wild card, the cdr part is
1039 the corresponding regular expression. */
1040 static Lisp_Object Vcached_fontset_data
;
1042 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1043 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1045 /* If fontset name PATTERN contains any wild card, return regular
1046 expression corresponding to PATTERN. */
1049 fontset_pattern_regexp (Lisp_Object pattern
)
1051 if (!strchr (SSDATA (pattern
), '*')
1052 && !strchr (SSDATA (pattern
), '?'))
1053 /* PATTERN does not contain any wild cards. */
1056 if (!CONSP (Vcached_fontset_data
)
1057 || strcmp (SSDATA (pattern
), CACHED_FONTSET_NAME
))
1059 /* We must at first update the cached data. */
1060 unsigned char *regex
, *p0
, *p1
;
1061 int ndashes
= 0, nstars
= 0, nescs
= 0;
1063 for (p0
= SDATA (pattern
); *p0
; p0
++)
1067 else if (*p0
== '*')
1070 || *p0
== '.' || *p0
== '\\'
1071 || *p0
== '+' || *p0
== '^'
1076 /* If PATTERN is not full XLFD we convert "*" to ".*". Otherwise
1077 we convert "*" to "[^-]*" which is much faster in regular
1078 expression matching. */
1079 ptrdiff_t regexsize
= (SBYTES (pattern
)
1080 + (ndashes
< 14 ? 2 : 5) * nstars
1083 p1
= regex
= SAFE_ALLOCA (regexsize
);
1086 for (p0
= SDATA (pattern
); *p0
; p0
++)
1093 *p1
++ = '[', *p1
++ = '^', *p1
++ = '-', *p1
++ = ']';
1096 else if (*p0
== '?')
1099 || *p0
== '.' || *p0
== '\\'
1100 || *p0
== '+' || *p0
== '^'
1102 *p1
++ = '\\', *p1
++ = *p0
;
1109 Vcached_fontset_data
= Fcons (build_string (SSDATA (pattern
)),
1110 build_string ((char *) regex
));
1114 return CACHED_FONTSET_REGEX
;
1117 /* Return ID of the base fontset named NAME. If there's no such
1118 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1119 0: pattern containing '*' and '?' as wildcards
1120 1: regular expression
1121 2: literal fontset name
1125 fs_query_fontset (Lisp_Object name
, int name_pattern
)
1130 name
= Fdowncase (name
);
1131 if (name_pattern
!= 1)
1133 tem
= Frassoc (name
, Vfontset_alias_alist
);
1135 tem
= Fassoc (name
, Vfontset_alias_alist
);
1136 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
1138 else if (name_pattern
== 0)
1140 tem
= fontset_pattern_regexp (name
);
1149 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
1151 Lisp_Object fontset
, this_name
;
1153 fontset
= FONTSET_FROM_ID (i
);
1155 || !BASE_FONTSET_P (fontset
))
1158 this_name
= FONTSET_NAME (fontset
);
1159 if (name_pattern
== 1
1160 ? fast_string_match_ignore_case (name
, this_name
) >= 0
1161 : !xstrcasecmp (SSDATA (name
), SSDATA (this_name
)))
1168 DEFUN ("query-fontset", Fquery_fontset
, Squery_fontset
, 1, 2, 0,
1169 doc
: /* Return the name of a fontset that matches PATTERN.
1170 The value is nil if there is no matching fontset.
1171 PATTERN can contain `*' or `?' as a wildcard
1172 just as X font name matching algorithm allows.
1173 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1174 (Lisp_Object pattern
, Lisp_Object regexpp
)
1176 Lisp_Object fontset
;
1179 check_window_system (NULL
);
1181 CHECK_STRING (pattern
);
1183 if (SCHARS (pattern
) == 0)
1186 id
= fs_query_fontset (pattern
, !NILP (regexpp
));
1190 fontset
= FONTSET_FROM_ID (id
);
1191 return FONTSET_NAME (fontset
);
1194 /* Return a list of base fontset names matching PATTERN on frame F. */
1197 list_fontsets (struct frame
*f
, Lisp_Object pattern
, int size
)
1199 Lisp_Object frame
, regexp
, val
;
1202 XSETFRAME (frame
, f
);
1204 regexp
= fontset_pattern_regexp (pattern
);
1207 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1209 Lisp_Object fontset
, name
;
1211 fontset
= FONTSET_FROM_ID (id
);
1213 || !BASE_FONTSET_P (fontset
)
1214 || !EQ (frame
, FONTSET_FRAME (fontset
)))
1216 name
= FONTSET_NAME (fontset
);
1218 if (STRINGP (regexp
)
1219 ? (fast_string_match (regexp
, name
) < 0)
1220 : strcmp (SSDATA (pattern
), SSDATA (name
)))
1223 val
= Fcons (Fcopy_sequence (FONTSET_NAME (fontset
)), val
);
1230 /* Free all realized fontsets whose base fontset is BASE. */
1233 free_realized_fontsets (Lisp_Object base
)
1238 /* For the moment, this doesn't work because free_realized_face
1239 doesn't remove FACE from a cache. Until we find a solution, we
1240 suppress this code, and simply use Fclear_face_cache even though
1241 that is not efficient. */
1243 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1245 Lisp_Object
this = AREF (Vfontset_table
, id
);
1247 if (EQ (FONTSET_BASE (this), base
))
1251 for (tail
= FONTSET_FACE_ALIST (this); CONSP (tail
);
1254 struct frame
*f
= XFRAME (FONTSET_FRAME (this));
1255 int face_id
= XINT (XCDR (XCAR (tail
)));
1256 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1258 /* Face THIS itself is also freed by the following call. */
1259 free_realized_face (f
, face
);
1265 /* But, we don't have to call Fclear_face_cache if no fontset has
1266 been realized from BASE. */
1267 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1269 Lisp_Object
this = AREF (Vfontset_table
, id
);
1271 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base
))
1273 Fclear_face_cache (Qt
);
1281 /* Check validity of NAME as a fontset name and return the
1282 corresponding fontset. If not valid, signal an error.
1284 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1287 Set *FRAME to the actual frame. */
1290 check_fontset_name (Lisp_Object name
, Lisp_Object
*frame
)
1293 struct frame
*f
= decode_live_frame (*frame
);
1295 XSETFRAME (*frame
, f
);
1298 return Vdefault_fontset
;
1300 id
= FRAME_FONTSET (f
);
1303 CHECK_STRING (name
);
1304 /* First try NAME as literal. */
1305 id
= fs_query_fontset (name
, 2);
1307 /* For backward compatibility, try again NAME as pattern. */
1308 id
= fs_query_fontset (name
, 0);
1310 error ("Fontset `%s' does not exist", SDATA (name
));
1312 return FONTSET_FROM_ID (id
);
1316 accumulate_script_ranges (Lisp_Object arg
, Lisp_Object range
, Lisp_Object val
)
1318 if (EQ (XCAR (arg
), val
))
1321 XSETCDR (arg
, Fcons (Fcons (XCAR (range
), XCDR (range
)), XCDR (arg
)));
1323 XSETCDR (arg
, Fcons (Fcons (range
, range
), XCDR (arg
)));
1328 /* Callback function for map_charset_chars in Fset_fontset_font.
1329 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1331 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1332 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1333 The consumed ranges are popped up from SCRIPT_RANGE_LIST, and the
1334 new SCRIPT_RANGE_LIST is stored in ARG.
1336 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1337 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1341 set_fontset_font (Lisp_Object arg
, Lisp_Object range
)
1343 Lisp_Object fontset
, font_def
, add
, ascii
, script_range_list
;
1344 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
1346 fontset
= AREF (arg
, 0);
1347 font_def
= AREF (arg
, 1);
1348 add
= AREF (arg
, 2);
1349 ascii
= AREF (arg
, 3);
1350 script_range_list
= AREF (arg
, 4);
1352 if (NILP (ascii
) && from
< 0x80)
1357 range
= Fcons (make_number (0x80), XCDR (range
));
1360 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1361 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1362 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1364 for (; CONSP (script_range_list
) && SCRIPT_TO
< from
; POP_SCRIPT_RANGE ())
1365 FONTSET_ADD (fontset
, XCAR (script_range_list
), font_def
, add
);
1366 if (CONSP (script_range_list
))
1368 if (SCRIPT_FROM
< from
)
1369 range
= Fcons (make_number (SCRIPT_FROM
), XCDR (range
));
1370 while (CONSP (script_range_list
) && SCRIPT_TO
<= to
)
1371 POP_SCRIPT_RANGE ();
1372 if (CONSP (script_range_list
) && SCRIPT_FROM
<= to
)
1373 XSETCAR (XCAR (script_range_list
), make_number (to
+ 1));
1376 FONTSET_ADD (fontset
, range
, font_def
, add
);
1377 ASET (arg
, 4, script_range_list
);
1380 static void update_auto_fontset_alist (Lisp_Object
, Lisp_Object
);
1383 DEFUN ("set-fontset-font", Fset_fontset_font
, Sset_fontset_font
, 3, 5, 0,
1385 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1387 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1388 the default fontset.
1390 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1391 In that case, use FONT-SPEC for all characters in the range FROM and
1394 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1395 all characters that belong to the script.
1397 TARGET may be a charset. In that case, use FONT-SPEC for all
1398 characters in the charset.
1400 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1401 that no FONT-SPEC is specified.
1403 FONT-SPEC may one of these:
1404 * A font-spec object made by the function `font-spec' (which see).
1405 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1406 REGISTRY is a font registry name. FAMILY may contain foundry
1407 name, and REGISTRY may contain encoding name.
1408 * A font name string.
1409 * nil, which explicitly specifies that there's no font for TARGET.
1411 Optional 4th argument FRAME is a frame or nil for the selected frame
1412 that is concerned in the case that NAME is nil.
1414 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1415 to the font specifications for TARGET previously set. If it is
1416 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1417 appended. By default, FONT-SPEC overrides the previous settings. */)
1418 (Lisp_Object name
, Lisp_Object target
, Lisp_Object font_spec
, Lisp_Object frame
, Lisp_Object add
)
1420 Lisp_Object fontset
;
1421 Lisp_Object font_def
, registry
, family
;
1422 Lisp_Object range_list
;
1423 struct charset
*charset
= NULL
;
1424 Lisp_Object fontname
;
1425 bool ascii_changed
= 0;
1427 fontset
= check_fontset_name (name
, &frame
);
1430 if (CONSP (font_spec
))
1432 Lisp_Object spec
= Ffont_spec (0, NULL
);
1434 font_parse_family_registry (XCAR (font_spec
), XCDR (font_spec
), spec
);
1436 fontname
= Ffont_xlfd_name (font_spec
, Qnil
);
1438 else if (STRINGP (font_spec
))
1440 Lisp_Object args
[2];
1442 fontname
= font_spec
;
1444 args
[1] = font_spec
;
1445 font_spec
= Ffont_spec (2, args
);
1447 else if (FONT_SPEC_P (font_spec
))
1448 fontname
= Ffont_xlfd_name (font_spec
, Qnil
);
1449 else if (! NILP (font_spec
))
1450 Fsignal (Qfont
, list2 (build_string ("Invalid font-spec"), font_spec
));
1452 if (! NILP (font_spec
))
1454 Lisp_Object encoding
, repertory
;
1456 family
= AREF (font_spec
, FONT_FAMILY_INDEX
);
1457 if (! NILP (family
) )
1458 family
= SYMBOL_NAME (family
);
1459 registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1460 if (! NILP (registry
))
1461 registry
= Fdowncase (SYMBOL_NAME (registry
));
1462 AUTO_STRING (dash
, "-");
1463 encoding
= find_font_encoding (concat3 (family
, dash
, registry
));
1464 if (NILP (encoding
))
1467 if (SYMBOLP (encoding
))
1469 CHECK_CHARSET (encoding
);
1470 encoding
= repertory
= CHARSET_SYMBOL_ID (encoding
);
1474 repertory
= XCDR (encoding
);
1475 encoding
= XCAR (encoding
);
1476 CHECK_CHARSET (encoding
);
1477 encoding
= CHARSET_SYMBOL_ID (encoding
);
1478 if (! NILP (repertory
) && SYMBOLP (repertory
))
1480 CHECK_CHARSET (repertory
);
1481 repertory
= CHARSET_SYMBOL_ID (repertory
);
1484 FONT_DEF_NEW (font_def
, font_spec
, encoding
, repertory
);
1489 if (CHARACTERP (target
))
1491 if (XFASTINT (target
) < 0x80)
1492 error ("Can't set a font for partial ASCII range");
1493 range_list
= list1 (Fcons (target
, target
));
1495 else if (CONSP (target
))
1497 Lisp_Object from
, to
;
1499 from
= Fcar (target
);
1501 CHECK_CHARACTER (from
);
1502 CHECK_CHARACTER (to
);
1503 if (XFASTINT (from
) < 0x80)
1505 if (XFASTINT (from
) != 0 || XFASTINT (to
) < 0x7F)
1506 error ("Can't set a font for partial ASCII range");
1509 range_list
= list1 (target
);
1511 else if (SYMBOLP (target
) && !NILP (target
))
1513 Lisp_Object script_list
;
1517 script_list
= XCHAR_TABLE (Vchar_script_table
)->extras
[0];
1518 if (! NILP (Fmemq (target
, script_list
)))
1520 if (EQ (target
, Qlatin
))
1522 val
= list1 (target
);
1523 map_char_table (accumulate_script_ranges
, Qnil
, Vchar_script_table
,
1525 range_list
= Fnreverse (XCDR (val
));
1527 if (CHARSETP (target
))
1529 CHECK_CHARSET_GET_CHARSET (target
, charset
);
1530 if (charset
->ascii_compatible_p
)
1533 else if (NILP (range_list
))
1534 error ("Invalid script or charset name: %s",
1535 SDATA (SYMBOL_NAME (target
)));
1537 else if (NILP (target
))
1538 range_list
= list1 (Qnil
);
1540 error ("Invalid target for setting a font");
1546 if (NILP (font_spec
))
1547 error ("Can't set ASCII font to nil");
1548 val
= CHAR_TABLE_REF (fontset
, 0);
1549 if (! NILP (val
) && EQ (add
, Qappend
))
1550 /* We are going to change just an additional font for ASCII. */
1558 arg
= make_uninit_vector (5);
1559 ASET (arg
, 0, fontset
);
1560 ASET (arg
, 1, font_def
);
1562 ASET (arg
, 3, ascii_changed
? Qt
: Qnil
);
1563 ASET (arg
, 4, range_list
);
1565 map_charset_chars (set_fontset_font
, Qnil
, arg
, charset
,
1566 CHARSET_MIN_CODE (charset
),
1567 CHARSET_MAX_CODE (charset
));
1568 range_list
= AREF (arg
, 4);
1570 for (; CONSP (range_list
); range_list
= XCDR (range_list
))
1571 FONTSET_ADD (fontset
, XCAR (range_list
), font_def
, add
);
1575 Lisp_Object tail
, fr
;
1576 int fontset_id
= XINT (FONTSET_ID (fontset
));
1578 set_fontset_ascii (fontset
, fontname
);
1579 name
= FONTSET_NAME (fontset
);
1580 FOR_EACH_FRAME (tail
, fr
)
1582 struct frame
*f
= XFRAME (fr
);
1583 Lisp_Object font_object
;
1586 if (FRAME_INITIAL_P (f
) || FRAME_TERMCAP_P (f
))
1588 if (fontset_id
!= FRAME_FONTSET (f
))
1590 face
= FACE_FROM_ID (f
, DEFAULT_FACE_ID
);
1592 font_object
= font_load_for_lface (f
, face
->lface
, font_spec
);
1594 font_object
= font_open_by_spec (f
, font_spec
);
1595 if (! NILP (font_object
))
1597 update_auto_fontset_alist (font_object
, fontset
);
1598 AUTO_FRAME_ARG (arg
, Qfont
, Fcons (name
, font_object
));
1599 Fmodify_frame_parameters (fr
, arg
);
1604 /* Free all realized fontsets whose base is FONTSET. This way, the
1605 specified character(s) are surely redisplayed by a correct
1607 free_realized_fontsets (fontset
);
1613 DEFUN ("new-fontset", Fnew_fontset
, Snew_fontset
, 2, 2, 0,
1614 doc
: /* Create a new fontset NAME from font information in FONTLIST.
1616 FONTLIST is an alist of scripts vs the corresponding font specification list.
1617 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1618 character of SCRIPT is displayed by a font that matches one of
1621 SCRIPT is a symbol that appears in the first extra slot of the
1622 char-table `char-script-table'.
1624 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1625 `set-fontset-font' for the meaning. */)
1626 (Lisp_Object name
, Lisp_Object fontlist
)
1628 Lisp_Object fontset
;
1631 CHECK_STRING (name
);
1632 CHECK_LIST (fontlist
);
1634 name
= Fdowncase (name
);
1635 id
= fs_query_fontset (name
, 0);
1638 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1639 Lisp_Object short_name
;
1643 if (font_parse_xlfd (SSDATA (name
), SBYTES (name
), font_spec
) < 0)
1644 error ("Fontset name must be in XLFD format");
1645 short_name
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1646 if (strncmp (SSDATA (SYMBOL_NAME (short_name
)), "fontset-", 8)
1647 || SBYTES (SYMBOL_NAME (short_name
)) < 9)
1648 error ("Registry field of fontset name must be \"fontset-*\"");
1649 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (short_name
)),
1650 Vfontset_alias_alist
);
1651 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso8859_1
);
1652 fontset
= make_fontset (Qnil
, name
, Qnil
);
1653 len
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1655 error ("Invalid fontset name (perhaps too long): %s", SDATA (name
));
1656 set_fontset_ascii (fontset
, make_unibyte_string (xlfd
, len
));
1660 fontset
= FONTSET_FROM_ID (id
);
1661 free_realized_fontsets (fontset
);
1662 Fset_char_table_range (fontset
, Qt
, Qnil
);
1665 for (; CONSP (fontlist
); fontlist
= XCDR (fontlist
))
1667 Lisp_Object elt
, script
;
1669 elt
= XCAR (fontlist
);
1670 script
= Fcar (elt
);
1672 if (CONSP (elt
) && (NILP (XCDR (elt
)) || CONSP (XCDR (elt
))))
1673 for (; CONSP (elt
); elt
= XCDR (elt
))
1674 Fset_fontset_font (name
, script
, XCAR (elt
), Qnil
, Qappend
);
1676 Fset_fontset_font (name
, script
, elt
, Qnil
, Qappend
);
1682 /* Alist of automatically created fontsets. Each element is a cons
1683 (FONT-SPEC . FONTSET-ID). */
1684 static Lisp_Object auto_fontset_alist
;
1686 /* Number of automatically created fontsets. */
1687 static ptrdiff_t num_auto_fontsets
;
1689 /* Return a fontset synthesized from FONT-OBJECT. This is called from
1690 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1691 frame, and the returned fontset is used for the default fontset of
1692 that frame. The fontset specifies a font of the same registry as
1693 FONT-OBJECT for all characters in the repertory of the registry
1694 (see Vfont_encoding_alist). If the repertory is not known, the
1695 fontset specifies the font for all Latin characters assuming that a
1696 user intends to use FONT-OBJECT for Latin characters. */
1699 fontset_from_font (Lisp_Object font_object
)
1701 Lisp_Object font_name
= font_get_name (font_object
);
1702 Lisp_Object font_spec
= copy_font_spec (font_object
);
1703 Lisp_Object registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1704 Lisp_Object fontset_spec
, alias
, name
, fontset
;
1707 val
= assoc_no_quit (font_spec
, auto_fontset_alist
);
1709 return XINT (FONTSET_ID (XCDR (val
)));
1710 if (num_auto_fontsets
++ == 0)
1711 alias
= intern ("fontset-startup");
1714 char temp
[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1716 sprintf (temp
, "fontset-auto%"pD
"d", num_auto_fontsets
- 1);
1717 alias
= intern (temp
);
1719 fontset_spec
= copy_font_spec (font_spec
);
1720 ASET (fontset_spec
, FONT_REGISTRY_INDEX
, alias
);
1721 name
= Ffont_xlfd_name (fontset_spec
, Qnil
);
1722 eassert (!NILP (name
));
1723 fontset
= make_fontset (Qnil
, name
, Qnil
);
1724 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (alias
)),
1725 Vfontset_alias_alist
);
1726 alias
= Fdowncase (AREF (font_object
, FONT_NAME_INDEX
));
1727 Vfontset_alias_alist
= Fcons (Fcons (name
, alias
), Vfontset_alias_alist
);
1728 auto_fontset_alist
= Fcons (Fcons (font_spec
, fontset
), auto_fontset_alist
);
1729 font_spec
= Ffont_spec (0, NULL
);
1730 ASET (font_spec
, FONT_REGISTRY_INDEX
, registry
);
1732 Lisp_Object target
= find_font_encoding (SYMBOL_NAME (registry
));
1735 target
= XCDR (target
);
1736 if (! CHARSETP (target
))
1738 Fset_fontset_font (name
, target
, font_spec
, Qnil
, Qnil
);
1739 Fset_fontset_font (name
, Qnil
, font_spec
, Qnil
, Qnil
);
1742 set_fontset_ascii (fontset
, font_name
);
1744 return XINT (FONTSET_ID (fontset
));
1748 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1749 FONTSET is changed, we delete an entry of FONTSET if any from
1750 auto_fontset_alist so that FONTSET is not re-used by
1751 fontset_from_font. */
1754 update_auto_fontset_alist (Lisp_Object font_object
, Lisp_Object fontset
)
1756 Lisp_Object prev
, tail
;
1758 for (prev
= Qnil
, tail
= auto_fontset_alist
; CONSP (tail
);
1759 prev
= tail
, tail
= XCDR (tail
))
1760 if (EQ (fontset
, XCDR (XCAR (tail
))))
1763 auto_fontset_alist
= XCDR (tail
);
1765 XSETCDR (prev
, XCDR (tail
));
1771 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1772 FONT-OBJECT is the font for the character at POSITION in the current
1773 buffer. This is computed from all the text properties and overlays
1774 that apply to POSITION. POSITION may be nil, in which case,
1775 FONT-SPEC is the font for displaying the character CH with the
1778 GLYPH-CODE is the glyph code in the font to use for the character.
1780 If the 2nd optional arg CH is non-nil, it is a character to check
1781 the font instead of the character at POSITION.
1783 It returns nil in the following cases:
1785 (1) The window system doesn't have a font for the character (thus
1786 it is displayed by an empty box).
1788 (2) The character code is invalid.
1790 (3) If POSITION is not nil, and the current buffer is not displayed
1793 In addition, the returned font name may not take into account of
1794 such redisplay engine hooks as what used in jit-lock-mode if
1795 POSITION is currently not visible. */
1798 DEFUN ("internal-char-font", Finternal_char_font
, Sinternal_char_font
, 1, 2, 0,
1799 doc
: /* For internal use only. */)
1800 (Lisp_Object position
, Lisp_Object ch
)
1802 ptrdiff_t pos
, pos_byte
, dummy
;
1808 if (NILP (position
))
1810 CHECK_CHARACTER (ch
);
1812 f
= XFRAME (selected_frame
);
1813 face_id
= lookup_basic_face (f
, DEFAULT_FACE_ID
);
1821 CHECK_NUMBER_COERCE_MARKER (position
);
1822 if (! (BEGV
<= XINT (position
) && XINT (position
) < ZV
))
1823 args_out_of_range_3 (position
, make_number (BEGV
), make_number (ZV
));
1824 pos
= XINT (position
);
1825 pos_byte
= CHAR_TO_BYTE (pos
);
1827 c
= FETCH_CHAR (pos_byte
);
1833 window
= Fget_buffer_window (Fcurrent_buffer (), Qnil
);
1836 w
= XWINDOW (window
);
1837 f
= XFRAME (w
->frame
);
1838 face_id
= face_at_buffer_position (w
, pos
, &dummy
,
1841 if (! CHAR_VALID_P (c
))
1843 if (!FRAME_WINDOW_P (f
))
1845 /* We need the basic faces to be valid below, so recompute them if
1846 some code just happened to clear the face cache. */
1847 if (FRAME_FACE_CACHE (f
)->used
== 0)
1848 recompute_basic_faces (f
);
1849 face_id
= FACE_FOR_CHAR (f
, FACE_FROM_ID (f
, face_id
), c
, pos
, Qnil
);
1850 face
= FACE_FROM_ID (f
, face_id
);
1853 unsigned code
= face
->font
->driver
->encode_char (face
->font
, c
);
1854 Lisp_Object font_object
;
1856 if (code
== FONT_INVALID_CODE
)
1858 XSETFONT (font_object
, face
->font
);
1859 return Fcons (font_object
, INTEGER_TO_CONS (code
));
1865 DEFUN ("fontset-info", Ffontset_info
, Sfontset_info
, 1, 2, 0,
1866 doc
: /* Return information about a fontset FONTSET on frame FRAME.
1868 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1869 for the default fontset. FRAME nil means the selected frame.
1871 The value is a char-table whose elements have this form:
1873 ((FONT OPENED-FONT ...) ...)
1875 FONT is a name of font specified for a range of characters.
1877 OPENED-FONT is a name of a font actually opened.
1879 The char-table has one extra slot. If FONTSET is not the default
1880 fontset, the value the extra slot is a char-table containing the
1881 information about the derived fonts from the default fontset. The
1882 format is the same as above. */)
1883 (Lisp_Object fontset
, Lisp_Object frame
)
1885 Lisp_Object
*realized
[2], fontsets
[2], tables
[2];
1886 Lisp_Object val
, elt
;
1889 check_window_system (NULL
);
1890 fontset
= check_fontset_name (fontset
, &frame
);
1892 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1893 in the table `realized'. */
1895 SAFE_ALLOCA_LISP (realized
[0], 2 * ASIZE (Vfontset_table
));
1896 realized
[1] = realized
[0] + ASIZE (Vfontset_table
);
1897 for (i
= j
= 0; i
< ASIZE (Vfontset_table
); i
++)
1899 elt
= FONTSET_FROM_ID (i
);
1901 && EQ (FONTSET_BASE (elt
), fontset
)
1902 && EQ (FONTSET_FRAME (elt
), frame
))
1903 realized
[0][j
++] = elt
;
1905 realized
[0][j
] = Qnil
;
1907 for (i
= j
= 0; ! NILP (realized
[0][i
]); i
++)
1909 elt
= FONTSET_DEFAULT (realized
[0][i
]);
1911 realized
[1][j
++] = elt
;
1913 realized
[1][j
] = Qnil
;
1915 tables
[0] = Fmake_char_table (Qfontset_info
, Qnil
);
1916 fontsets
[0] = fontset
;
1917 if (!EQ (fontset
, Vdefault_fontset
))
1919 tables
[1] = Fmake_char_table (Qnil
, Qnil
);
1920 set_char_table_extras (tables
[0], 0, tables
[1]);
1921 fontsets
[1] = Vdefault_fontset
;
1924 /* Accumulate information of the fontset in TABLE. The format of
1925 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1926 for (k
= 0; k
<= 1; k
++)
1928 for (c
= 0; c
<= MAX_CHAR
; )
1930 int from
= c
, to
= MAX_5_BYTE_CHAR
;
1932 if (c
<= MAX_5_BYTE_CHAR
)
1934 val
= char_table_ref_and_range (fontsets
[k
], c
, &from
, &to
);
1938 val
= FONTSET_FALLBACK (fontsets
[k
]);
1945 /* At first, set ALIST to ((FONT-SPEC) ...). */
1946 for (alist
= Qnil
, i
= 0; i
< ASIZE (val
); i
++)
1947 if (! NILP (AREF (val
, i
)))
1948 alist
= Fcons (Fcons (FONT_DEF_SPEC (AREF (val
, i
)), Qnil
),
1950 alist
= Fnreverse (alist
);
1952 /* Then store opened font names to cdr of each elements. */
1953 for (i
= 0; ! NILP (realized
[k
][i
]); i
++)
1955 if (c
<= MAX_5_BYTE_CHAR
)
1956 val
= FONTSET_REF (realized
[k
][i
], c
);
1958 val
= FONTSET_FALLBACK (realized
[k
][i
]);
1959 if (! CONSP (val
) || ! VECTORP (XCDR (val
)))
1961 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1963 for (j
= 0; j
< ASIZE (val
); j
++)
1965 elt
= AREF (val
, j
);
1966 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt
)))
1968 Lisp_Object font_object
= RFONT_DEF_OBJECT (elt
);
1969 Lisp_Object slot
, name
;
1971 slot
= Fassq (RFONT_DEF_SPEC (elt
), alist
);
1972 name
= AREF (font_object
, FONT_NAME_INDEX
);
1973 if (NILP (Fmember (name
, XCDR (slot
))))
1974 nconc2 (slot
, list1 (name
));
1979 /* Store ALIST in TBL for characters C..TO. */
1980 if (c
<= MAX_5_BYTE_CHAR
)
1981 char_table_set_range (tables
[k
], c
, to
, alist
);
1983 set_char_table_defalt (tables
[k
], alist
);
1985 /* At last, change each elements to font names. */
1986 for (; CONSP (alist
); alist
= XCDR (alist
))
1989 XSETCAR (elt
, Ffont_xlfd_name (XCAR (elt
), Qnil
));
1994 if (EQ (fontset
, Vdefault_fontset
))
2003 DEFUN ("fontset-font", Ffontset_font
, Sfontset_font
, 2, 3, 0,
2004 doc
: /* Return a font name pattern for character CH in fontset NAME.
2005 If NAME is t, find a pattern in the default fontset.
2006 If NAME is nil, find a pattern in the fontset of the selected frame.
2008 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2009 family name and REGISTRY is a font registry name. This is actually
2010 the first font name pattern for CH in the fontset or in the default
2013 If the 2nd optional arg ALL is non-nil, return a list of all font name
2015 (Lisp_Object name
, Lisp_Object ch
, Lisp_Object all
)
2018 Lisp_Object fontset
, elt
, list
, repertory
, val
;
2023 fontset
= check_fontset_name (name
, &frame
);
2025 CHECK_CHARACTER (ch
);
2030 for (i
= 0, elt
= FONTSET_REF (fontset
, c
); i
< 2;
2031 i
++, elt
= FONTSET_FALLBACK (fontset
))
2033 for (j
= 0; j
< ASIZE (elt
); j
++)
2035 Lisp_Object family
, registry
;
2037 val
= AREF (elt
, j
);
2040 repertory
= AREF (val
, 1);
2041 if (INTEGERP (repertory
))
2043 struct charset
*charset
= CHARSET_FROM_ID (XINT (repertory
));
2045 if (! CHAR_CHARSET_P (c
, charset
))
2048 else if (CHAR_TABLE_P (repertory
))
2050 if (NILP (CHAR_TABLE_REF (repertory
, c
)))
2053 val
= AREF (val
, 0);
2054 /* VAL is a FONT-SPEC */
2055 family
= AREF (val
, FONT_FAMILY_INDEX
);
2056 if (! NILP (family
))
2057 family
= SYMBOL_NAME (family
);
2058 registry
= AREF (val
, FONT_REGISTRY_INDEX
);
2059 if (! NILP (registry
))
2060 registry
= SYMBOL_NAME (registry
);
2061 val
= Fcons (family
, registry
);
2064 list
= Fcons (val
, list
);
2066 if (EQ (fontset
, Vdefault_fontset
))
2068 fontset
= Vdefault_fontset
;
2070 return (Fnreverse (list
));
2073 DEFUN ("fontset-list", Ffontset_list
, Sfontset_list
, 0, 0, 0,
2074 doc
: /* Return a list of all defined fontset names. */)
2077 Lisp_Object fontset
, list
;
2081 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
2083 fontset
= FONTSET_FROM_ID (i
);
2085 && BASE_FONTSET_P (fontset
))
2086 list
= Fcons (FONTSET_NAME (fontset
), list
);
2093 #ifdef ENABLE_CHECKING
2095 Lisp_Object
dump_fontset (Lisp_Object
) EXTERNALLY_VISIBLE
;
2098 dump_fontset (Lisp_Object fontset
)
2102 vec
= Fmake_vector (make_number (3), Qnil
);
2103 ASET (vec
, 0, FONTSET_ID (fontset
));
2105 if (BASE_FONTSET_P (fontset
))
2107 ASET (vec
, 1, FONTSET_NAME (fontset
));
2113 frame
= FONTSET_FRAME (fontset
);
2116 struct frame
*f
= XFRAME (frame
);
2118 if (FRAME_LIVE_P (f
))
2120 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)),
2124 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), Qnil
));
2126 if (!NILP (FONTSET_DEFAULT (fontset
)))
2127 ASET (vec
, 2, FONTSET_ID (FONTSET_DEFAULT (fontset
)));
2132 DEFUN ("fontset-list-all", Ffontset_list_all
, Sfontset_list_all
, 0, 0, 0,
2133 doc
: /* Return a brief summary of all fontsets for debug use. */)
2139 for (i
= 0, val
= Qnil
; i
< ASIZE (Vfontset_table
); i
++)
2140 if (! NILP (AREF (Vfontset_table
, i
)))
2141 val
= Fcons (dump_fontset (AREF (Vfontset_table
, i
)), val
);
2142 return (Fnreverse (val
));
2144 #endif /* ENABLE_CHECKING */
2147 syms_of_fontset (void)
2149 DEFSYM (Qfontset
, "fontset");
2150 Fput (Qfontset
, Qchar_table_extra_slots
, make_number (8));
2151 DEFSYM (Qfontset_info
, "fontset-info");
2152 Fput (Qfontset_info
, Qchar_table_extra_slots
, make_number (1));
2154 DEFSYM (Qprepend
, "prepend");
2155 DEFSYM (Qappend
, "append");
2156 DEFSYM (Qlatin
, "latin");
2158 Vcached_fontset_data
= Qnil
;
2159 staticpro (&Vcached_fontset_data
);
2161 Vfontset_table
= Fmake_vector (make_number (32), Qnil
);
2162 staticpro (&Vfontset_table
);
2164 Vdefault_fontset
= Fmake_char_table (Qfontset
, Qnil
);
2165 staticpro (&Vdefault_fontset
);
2166 set_fontset_id (Vdefault_fontset
, make_number (0));
2169 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2170 ASET (Vfontset_table
, 0, Vdefault_fontset
);
2171 next_fontset_id
= 1;
2173 auto_fontset_alist
= Qnil
;
2174 staticpro (&auto_fontset_alist
);
2176 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist
,
2178 Alist of charsets vs the charsets to determine the preferred font encoding.
2179 Each element looks like (CHARSET . ENCODING-CHARSET),
2180 where ENCODING-CHARSET is a charset registered in the variable
2181 `font-encoding-alist' as ENCODING.
2183 When a text has a property `charset' and the value is CHARSET, a font
2184 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2185 Vfont_encoding_charset_alist
= Qnil
;
2187 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent
,
2189 Char table of characters whose ascent values should be ignored.
2190 If an entry for a character is non-nil, the ascent value of the glyph
2191 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2193 This affects how a composite character which contains
2194 such a character is displayed on screen. */);
2195 Vuse_default_ascent
= Qnil
;
2197 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition
,
2199 Char table of characters which are not composed relatively.
2200 If an entry for a character is non-nil, a composition sequence
2201 which contains that character is displayed so that
2202 the glyph of that character is put without considering
2203 an ascent and descent value of a previous character. */);
2204 Vignore_relative_composition
= Qnil
;
2206 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist
,
2207 doc
: /* Alist of fontname vs list of the alternate fontnames.
2208 When a specified font name is not found, the corresponding
2209 alternate fontnames (if any) are tried instead. */);
2210 Valternate_fontname_alist
= Qnil
;
2212 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist
,
2213 doc
: /* Alist of fontset names vs the aliases. */);
2214 Vfontset_alias_alist
2215 = list1 (Fcons (FONTSET_NAME (Vdefault_fontset
),
2216 build_pure_c_string ("fontset-default")));
2218 DEFVAR_LISP ("vertical-centering-font-regexp",
2219 Vvertical_centering_font_regexp
,
2220 doc
: /* Regexp matching font names that require vertical centering on display.
2221 When a character is displayed with such fonts, the character is displayed
2222 at the vertical center of lines. */);
2223 Vvertical_centering_font_regexp
= Qnil
;
2225 DEFVAR_LISP ("otf-script-alist", Votf_script_alist
,
2226 doc
: /* Alist of OpenType script tags vs the corresponding script names. */);
2227 Votf_script_alist
= Qnil
;
2229 defsubr (&Squery_fontset
);
2230 defsubr (&Snew_fontset
);
2231 defsubr (&Sset_fontset_font
);
2232 defsubr (&Sinternal_char_font
);
2233 defsubr (&Sfontset_info
);
2234 defsubr (&Sfontset_font
);
2235 defsubr (&Sfontset_list
);
2236 #ifdef ENABLE_CHECKING
2237 defsubr (&Sfontset_list_all
);