3 Copyright (C) 2001-2016 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 (at
17 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/>. */
32 #include "blockinput.h"
33 #include "character.h"
36 #include "dispextern.h"
38 #ifdef HAVE_WINDOW_SYSTEM
40 #endif /* HAVE_WINDOW_SYSTEM */
45 A fontset is a collection of font related information to give
46 similar appearance (style, etc) of characters. A fontset has two
47 roles. One is to use for the frame parameter `font' as if it is an
48 ASCII font. In that case, Emacs uses the font specified for
49 `ascii' script for the frame's default font.
51 Another role, the more important one, is to provide information
52 about which font to use for each non-ASCII character.
54 There are two kinds of fontsets; base and realized. A base fontset
55 is created by `new-fontset' from Emacs Lisp explicitly. A realized
56 fontset is created implicitly when a face is realized for ASCII
57 characters. A face is also realized for non-ASCII characters based
58 on an ASCII face. All of non-ASCII faces based on the same ASCII
59 face share the same realized fontset.
61 A fontset object is implemented by a char-table whose default value
62 and parent are always nil.
64 An element of a base fontset is a vector of FONT-DEFs which themselves
65 are vectors of the form [ FONT-SPEC ENCODING REPERTORY ].
67 An element of a realized fontset is nil, t, 0, or a cons that has
70 (CHARSET-ORDERED-LIST-TICK . FONT-GROUP)
72 CHARSET_ORDERED_LIST_TICK is the same as charset_ordered_list_tick or -1.
74 FONT-GROUP is a vector of elements that have this form:
76 [ RFONT-DEF0 RFONT-DEF1 ... ]
78 Each 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 considering the current
83 charset priority list, the current language environment, and
84 priorities determined by font-backends.
86 RFONT-DEFn may not be a vector in the following cases.
88 The value nil means that we have not yet generated the above vector
89 from the base of the fontset.
91 The value t means that no font is available for the corresponding
94 The value 0 means that no font is available for the corresponding
95 range of characters in this fontset, but may be available in the
96 fallback font-group or in the default fontset.
98 A fontset has 8 extra slots.
101 base: the ID number of the fontset
105 base: the name of the fontset
109 base: the font name for ASCII characters
114 realized: the base fontset
118 realized: the frame that the fontset belongs to
122 realized: the ID number of a face to use for characters that
123 has no font in a realized fontset.
127 realized: If the base is not the default fontset, a fontset
128 realized from the default fontset, else nil.
131 base: Same as element value (but for fallback fonts).
134 All fontsets are recorded in the vector Vfontset_table.
139 There's a special base fontset named `default fontset' which
140 defines the default font specifications. When a base fontset
141 doesn't specify a font for a specific character, the corresponding
142 value in the default fontset is used.
144 The parent of a realized fontset created for such a face that has
145 no fontset is the default fontset.
148 These structures are hidden from the other codes than this file.
149 The other codes handle fontsets only by their ID numbers. They
150 usually use the variable name `fontset' for IDs. But, in this
151 file, we always use variable name `id' for IDs, and name `fontset'
152 for an actual fontset object, i.e., char-table.
156 /********** VARIABLES and FUNCTION PROTOTYPES **********/
158 /* Vector containing all fontsets. */
159 static Lisp_Object Vfontset_table
;
161 /* Next possibly free fontset ID. Usually this keeps the minimum
162 fontset ID not yet used. */
163 static int next_fontset_id
;
165 /* The default fontset. This gives default FAMILY and REGISTRY of
166 font for each character. */
167 static Lisp_Object Vdefault_fontset
;
169 /* Prototype declarations for static functions. */
170 static Lisp_Object
make_fontset (Lisp_Object
, Lisp_Object
, Lisp_Object
);
172 /* Return true if ID is a valid fontset id.
173 Optimized away if ENABLE_CHECKING is not defined. */
176 fontset_id_valid_p (int id
)
178 return (id
>= 0 && id
< ASIZE (Vfontset_table
) - 1);
183 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
185 /* Return the fontset with ID. No check of ID's validness. */
186 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
188 /* Access special values of FONTSET. */
190 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
192 set_fontset_id (Lisp_Object fontset
, Lisp_Object id
)
194 set_char_table_extras (fontset
, 0, id
);
197 /* Access special values of (base) FONTSET. */
199 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
201 set_fontset_name (Lisp_Object fontset
, Lisp_Object name
)
203 set_char_table_extras (fontset
, 1, name
);
206 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
208 set_fontset_ascii (Lisp_Object fontset
, Lisp_Object ascii
)
210 set_char_table_extras (fontset
, 2, ascii
);
213 /* Access special values of (realized) FONTSET. */
215 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
217 set_fontset_base (Lisp_Object fontset
, Lisp_Object base
)
219 set_char_table_extras (fontset
, 3, base
);
222 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
224 set_fontset_frame (Lisp_Object fontset
, Lisp_Object frame
)
226 set_char_table_extras (fontset
, 4, frame
);
229 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
231 set_fontset_nofont_face (Lisp_Object fontset
, Lisp_Object face
)
233 set_char_table_extras (fontset
, 5, face
);
236 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
238 set_fontset_default (Lisp_Object fontset
, Lisp_Object def
)
240 set_char_table_extras (fontset
, 6, def
);
243 /* For both base and realized fontset. */
245 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
247 set_fontset_fallback (Lisp_Object fontset
, Lisp_Object fallback
)
249 set_char_table_extras (fontset
, 7, fallback
);
252 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
254 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
255 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
257 (font_def) = make_uninit_vector (3); \
258 ASET ((font_def), 0, font_spec); \
259 ASET ((font_def), 1, encoding); \
260 ASET ((font_def), 2, repertory); \
263 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
264 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
265 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
267 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
268 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
269 ASET ((rfont_def), 0, make_number (face_id))
270 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
271 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
272 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
273 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
274 ASET ((rfont_def), 2, (object))
275 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
276 the order of listing by font backends, the higher bits represents
277 the order given by charset priority list. The smaller value is
279 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
280 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
281 ASET ((rfont_def), 3, make_number (score))
282 #define RFONT_DEF_NEW(rfont_def, font_def) \
284 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
285 ASET ((rfont_def), 1, (font_def)); \
286 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
290 /* Return the element of FONTSET for the character C. If FONTSET is a
291 base fontset other then the default fontset and FONTSET doesn't
292 contain information for C, return the information in the default
295 #define FONTSET_REF(fontset, c) \
296 (EQ (fontset, Vdefault_fontset) \
297 ? CHAR_TABLE_REF (fontset, c) \
298 : fontset_ref ((fontset), (c)))
301 fontset_ref (Lisp_Object fontset
, int c
)
305 elt
= CHAR_TABLE_REF (fontset
, c
);
306 if (NILP (elt
) && ! EQ (fontset
, Vdefault_fontset
)
307 /* Don't check Vdefault_fontset for a realized fontset. */
308 && NILP (FONTSET_BASE (fontset
)))
309 elt
= CHAR_TABLE_REF (Vdefault_fontset
, c
);
313 /* Set elements of FONTSET for characters in RANGE to the value ELT.
314 RANGE is a cons (FROM . TO), where FROM and TO are character codes
315 specifying a range. */
317 #define FONTSET_SET(fontset, range, elt) \
318 Fset_char_table_range ((fontset), (range), (elt))
321 /* Modify the elements of FONTSET for characters in RANGE by replacing
322 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
323 and TO are character codes specifying a range. If ADD is nil,
324 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
327 #define FONTSET_ADD(fontset, range, elt, add) \
330 ? (set_fontset_fallback \
331 (fontset, Fmake_vector (make_number (1), (elt)))) \
333 Fset_char_table_range (fontset, range, \
334 Fmake_vector (make_number (1), elt)))) \
335 : fontset_add ((fontset), (range), (elt), (add)))
338 fontset_add (Lisp_Object fontset
, Lisp_Object range
, Lisp_Object elt
, Lisp_Object add
)
341 int idx
= (EQ (add
, Qappend
) ? 0 : 1);
343 args
[1 - idx
] = Fmake_vector (make_number (1), elt
);
347 int from
= XINT (XCAR (range
));
348 int to
= XINT (XCDR (range
));
352 from1
= from
, to1
= to
;
353 args
[idx
] = char_table_ref_and_range (fontset
, from
, &from1
, &to1
);
354 char_table_set_range (fontset
, from
, to1
,
355 (NILP (args
[idx
]) ? args
[1 - idx
]
356 : CALLMANY (Fvconcat
, args
)));
362 args
[idx
] = FONTSET_FALLBACK (fontset
);
363 set_fontset_fallback (fontset
,
364 (NILP (args
[idx
]) ? args
[1 - idx
]
365 : CALLMANY (Fvconcat
, args
)));
370 fontset_compare_rfontdef (const void *val1
, const void *val2
)
372 return (RFONT_DEF_SCORE (*(Lisp_Object
*) val1
)
373 - RFONT_DEF_SCORE (*(Lisp_Object
*) val2
));
376 /* Update a cons cell which has this form:
377 (CHARSET-ORDERED-LIST-TICK . FONT-GROUP)
378 where FONT-GROUP is of the form
379 [ PREFERRED-RFONT-DEF RFONT-DEF0 RFONT-DEF1 ... ]
380 Reorder RFONT-DEFs according to the current language, and update
381 CHARSET-ORDERED-LIST-TICK. */
384 reorder_font_vector (Lisp_Object font_group
, struct font
*font
)
386 Lisp_Object vec
, font_object
;
389 bool score_changed
= false;
392 XSETFONT (font_object
, font
);
396 vec
= XCDR (font_group
);
398 /* Exclude the tailing nil element from the reordering. */
399 if (NILP (AREF (vec
, size
- 1)))
402 for (i
= 0; i
< size
; i
++)
404 Lisp_Object rfont_def
= AREF (vec
, i
);
405 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
406 Lisp_Object font_spec
= FONT_DEF_SPEC (font_def
);
407 int score
= RFONT_DEF_SCORE (rfont_def
) & 0xFF;
408 Lisp_Object otf_spec
= Ffont_get (font_spec
, QCotf
);
410 if (! NILP (otf_spec
))
411 /* A font-spec with :otf is preferable regardless of encoding
414 else if (! font_match_p (font_spec
, font_object
))
416 Lisp_Object encoding
= FONT_DEF_ENCODING (font_def
);
418 if (! NILP (encoding
))
420 /* This spec specifies an encoding by a charset set
421 name. Reflect the preference order of that charset
422 in the upper bits of SCORE. */
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 /* This spec does not specify an encoding. If the spec
436 specifies a language, and the language is not for the
437 current language environment, make the score
439 Lisp_Object lang
= Ffont_get (font_spec
, QClang
);
442 && ! EQ (lang
, Vcurrent_iso639_language
)
443 && (! CONSP (Vcurrent_iso639_language
)
444 || NILP (Fmemq (lang
, Vcurrent_iso639_language
))))
448 if (RFONT_DEF_SCORE (rfont_def
) != score
)
450 RFONT_DEF_SET_SCORE (rfont_def
, score
);
451 score_changed
= true;
456 qsort (XVECTOR (vec
)->contents
, size
, word_size
,
457 fontset_compare_rfontdef
);
458 EMACS_INT low_tick_bits
= charset_ordered_list_tick
& MOST_POSITIVE_FIXNUM
;
459 XSETCAR (font_group
, make_number (low_tick_bits
));
462 /* Return a font-group (actually a cons (CHARSET_ORDERED_LIST_TICK
463 . FONT-GROUP)) for character C or a fallback font-group in the
464 realized fontset FONTSET. The elements of FONT-GROUP are
465 RFONT-DEFs. The value may not be a cons. See the comment at the
466 head of this file for the detail of the return value. */
469 fontset_get_font_group (Lisp_Object fontset
, int c
)
471 Lisp_Object font_group
;
472 Lisp_Object base_fontset
;
473 int from
= 0, to
= MAX_CHAR
, i
;
475 eassert (! BASE_FONTSET_P (fontset
));
477 font_group
= CHAR_TABLE_REF (fontset
, c
);
479 font_group
= FONTSET_FALLBACK (fontset
);
480 if (! NILP (font_group
))
481 /* We have already realized FONT-DEFs of this font group for C or
482 for fallback (FONT_GROUP is a cons), or we have already found
483 that no appropriate font was found (FONT_GROUP is t or 0). */
485 base_fontset
= FONTSET_BASE (fontset
);
486 if (NILP (base_fontset
))
487 /* Actually we never come here because FONTSET is a realized one,
488 and thus it should have a base. */
491 font_group
= char_table_ref_and_range (base_fontset
, c
, &from
, &to
);
493 font_group
= FONTSET_FALLBACK (base_fontset
);
495 /* FONT_GROUP not being a vector means that no fonts are specified
496 for C, or the fontset does not have fallback fonts. */
497 if (NILP (font_group
))
499 font_group
= make_number (0);
501 /* Record that FONTSET does not specify fonts for C. As
502 there's a possibility that a font is found in a fallback
503 font group, we set 0 at the moment. */
504 char_table_set_range (fontset
, from
, to
, font_group
);
507 if (!VECTORP (font_group
))
510 /* Now realize FONT-DEFs of this font group, and update the realized
512 font_group
= Fcopy_sequence (font_group
);
513 for (i
= 0; i
< ASIZE (font_group
); i
++)
514 if (! NILP (AREF (font_group
, i
)))
516 Lisp_Object rfont_def
;
518 RFONT_DEF_NEW (rfont_def
, AREF (font_group
, i
));
519 /* Remember the original order. */
520 RFONT_DEF_SET_SCORE (rfont_def
, i
);
521 ASET (font_group
, i
, rfont_def
);
523 font_group
= Fcons (make_number (-1), font_group
);
525 char_table_set_range (fontset
, from
, to
, font_group
);
527 set_fontset_fallback (fontset
, font_group
);
531 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
532 character C. If no font is found, return Qnil or 0 if there's a
533 possibility that the default fontset or the fallback font groups
534 have a proper font, and return Qt if not.
536 If a font is found but is not yet opened, open it (if FACE is not
537 NULL) or return Qnil (if FACE is NULL).
539 CHARSET_ID is a charset-id that must be preferred, or -1 meaning no
542 If FALLBACK, search only fallback fonts. */
545 fontset_find_font (Lisp_Object fontset
, int c
, struct face
*face
,
546 int charset_id
, bool fallback
)
548 Lisp_Object vec
, font_group
;
549 int i
, charset_matched
= 0, found_index
;
550 struct frame
*f
= (FRAMEP (FONTSET_FRAME (fontset
))
551 ? XFRAME (FONTSET_FRAME (fontset
))
552 : XFRAME (selected_frame
));
553 Lisp_Object rfont_def
;
555 font_group
= fontset_get_font_group (fontset
, fallback
? -1 : c
);
556 if (! CONSP (font_group
))
558 vec
= XCDR (font_group
);
559 if (ASIZE (vec
) == 0)
564 if (XINT (XCAR (font_group
)) != charset_ordered_list_tick
)
565 /* We have just created the font-group,
566 or the charset priorities were changed. */
567 reorder_font_vector (font_group
, face
->ascii_face
->font
);
569 /* Find a spec matching with CHARSET_ID to try it at
571 for (i
= 0; i
< ASIZE (vec
); i
++)
573 Lisp_Object repertory
;
575 rfont_def
= AREF (vec
, i
);
576 if (NILP (rfont_def
))
578 repertory
= FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def
));
580 if (XINT (repertory
) == charset_id
)
588 /* Find the first available font in the vector of RFONT-DEF. If
589 CHARSET_MATCHED > 0, try the corresponding RFONT-DEF first, then
591 for (i
= 0; i
< ASIZE (vec
); i
++)
593 Lisp_Object font_def
;
594 Lisp_Object font_entity
, font_object
;
599 if (charset_matched
> 0)
601 /* Try the element matching with CHARSET_ID at first. */
602 found_index
= charset_matched
;
603 /* Make this negative so that we don't come here in the
605 charset_matched
= - charset_matched
;
606 /* We must try the first element in the next loop. */
610 else if (i
== - charset_matched
)
612 /* We have already tried this element and the followings
613 that have the same font specifications in the first
614 iteration. So, skip them all. */
615 rfont_def
= AREF (vec
, i
);
616 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
617 for (; i
+ 1 < ASIZE (vec
); i
++)
619 rfont_def
= AREF (vec
, i
+ 1);
620 if (NILP (rfont_def
))
622 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def
), font_def
))
628 rfont_def
= AREF (vec
, found_index
);
629 if (NILP (rfont_def
))
633 /* This is a sign of not to try the other fonts. */
636 if (INTEGERP (RFONT_DEF_FACE (rfont_def
))
637 && XINT (RFONT_DEF_FACE (rfont_def
)) < 0)
638 /* We couldn't open this font last time. */
641 font_object
= RFONT_DEF_OBJECT (rfont_def
);
642 if (NILP (font_object
))
644 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
647 /* We have not yet opened the font. */
649 /* Find a font best-matching with the spec without checking
650 the support of the character C. That checking is costly,
651 and even without the checking, the found font supports C
652 in high possibility. */
653 font_entity
= font_find_for_lface (f
, face
->lface
,
654 FONT_DEF_SPEC (font_def
), -1);
655 if (NILP (font_entity
))
657 /* Record that no font matches the spec. */
658 RFONT_DEF_SET_FACE (rfont_def
, -1);
661 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
662 FONT_DEF_SPEC (font_def
));
663 if (NILP (font_object
))
665 /* Something strange happened, perhaps because of a
666 Font-backend problem. To avoid crashing, record
667 that this spec is unusable. It may be better to find
668 another font of the same spec, but currently we don't
669 have such an API in font-backend. */
670 RFONT_DEF_SET_FACE (rfont_def
, -1);
673 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
676 if (font_has_char (f
, font_object
, c
))
679 /* Find a font already opened, matching with the current spec,
681 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
682 for (; found_index
+ 1 < ASIZE (vec
); found_index
++)
684 rfont_def
= AREF (vec
, found_index
+ 1);
685 if (NILP (rfont_def
))
687 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def
), font_def
))
689 font_object
= RFONT_DEF_OBJECT (rfont_def
);
690 if (! NILP (font_object
) && font_has_char (f
, font_object
, c
))
697 /* Find a font-entity with the current spec and supporting C. */
698 font_entity
= font_find_for_lface (f
, face
->lface
,
699 FONT_DEF_SPEC (font_def
), c
);
700 if (! NILP (font_entity
))
702 /* We found a font. Open it and insert a new element for
707 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
709 if (NILP (font_object
))
711 RFONT_DEF_NEW (rfont_def
, font_def
);
712 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
713 RFONT_DEF_SET_SCORE (rfont_def
, RFONT_DEF_SCORE (rfont_def
));
714 new_vec
= Fmake_vector (make_number (ASIZE (vec
) + 1), Qnil
);
716 for (j
= 0; j
< found_index
; j
++)
717 ASET (new_vec
, j
, AREF (vec
, j
));
718 ASET (new_vec
, j
, rfont_def
);
719 for (j
++; j
< ASIZE (new_vec
); j
++)
720 ASET (new_vec
, j
, AREF (vec
, j
- 1));
721 XSETCDR (font_group
, new_vec
);
729 /* Record that no font in this font group supports C. */
730 FONTSET_SET (fontset
, make_number (c
), make_number (0));
734 if (fallback
&& found_index
> 0)
736 /* The order of fonts in the fallback font-group is not that
737 important, and it is better to move the found font to the
738 first of the group so that the next try will find it
740 for (i
= found_index
; i
> 0; i
--)
741 ASET (vec
, i
, AREF (vec
, i
- 1));
742 ASET (vec
, 0, rfont_def
);
748 /* Return RFONT-DEF (vector) corresponding to the font for character
749 C. The value is not a vector if no font is found for C. */
752 fontset_font (Lisp_Object fontset
, int c
, struct face
*face
, int id
)
754 Lisp_Object rfont_def
;
755 Lisp_Object default_rfont_def UNINIT
;
756 Lisp_Object base_fontset
;
758 /* Try a font-group of FONTSET. */
759 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c
), Qnil
);
760 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 0);
761 if (VECTORP (rfont_def
))
763 if (NILP (rfont_def
))
764 FONTSET_SET (fontset
, make_number (c
), make_number (0));
766 /* Try a font-group of the default fontset. */
767 base_fontset
= FONTSET_BASE (fontset
);
768 if (! EQ (base_fontset
, Vdefault_fontset
))
770 if (NILP (FONTSET_DEFAULT (fontset
)))
773 make_fontset (FONTSET_FRAME (fontset
), Qnil
, Vdefault_fontset
));
774 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c
), Qnil
);
776 = fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 0);
777 if (VECTORP (default_rfont_def
))
778 return default_rfont_def
;
779 if (NILP (default_rfont_def
))
780 FONTSET_SET (FONTSET_DEFAULT (fontset
), make_number (c
),
784 /* Try a fallback font-group of FONTSET. */
785 if (! EQ (rfont_def
, Qt
))
787 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c
), Qnil
);
788 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 1);
789 if (VECTORP (rfont_def
))
791 /* Remember that FONTSET has no font for C. */
792 FONTSET_SET (fontset
, make_number (c
), Qt
);
795 /* Try a fallback font-group of the default fontset. */
796 if (! EQ (base_fontset
, Vdefault_fontset
)
797 && ! EQ (default_rfont_def
, Qt
))
799 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c
), Qnil
);
800 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 1);
801 if (VECTORP (rfont_def
))
803 /* Remember that the default fontset has no font for C. */
804 FONTSET_SET (FONTSET_DEFAULT (fontset
), make_number (c
), Qt
);
810 /* Return a newly created fontset with NAME. If BASE is nil, make a
811 base fontset. Otherwise make a realized fontset whose base is
815 make_fontset (Lisp_Object frame
, Lisp_Object name
, Lisp_Object base
)
818 int size
= ASIZE (Vfontset_table
);
819 int id
= next_fontset_id
;
821 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
822 the next available fontset ID. So it is expected that this loop
823 terminates quickly. In addition, as the last element of
824 Vfontset_table is always nil, we don't have to check the range of
826 while (!NILP (AREF (Vfontset_table
, id
))) id
++;
829 Vfontset_table
= larger_vector (Vfontset_table
, 1, -1);
831 fontset
= Fmake_char_table (Qfontset
, Qnil
);
833 set_fontset_id (fontset
, make_number (id
));
835 set_fontset_name (fontset
, name
);
838 set_fontset_name (fontset
, Qnil
);
839 set_fontset_frame (fontset
, frame
);
840 set_fontset_base (fontset
, base
);
843 ASET (Vfontset_table
, id
, fontset
);
844 next_fontset_id
= id
+ 1;
849 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
851 /* Return the name of the fontset who has ID. */
854 fontset_name (int id
)
858 fontset
= FONTSET_FROM_ID (id
);
859 return FONTSET_NAME (fontset
);
863 /* Return the ASCII font name of the fontset who has ID. */
866 fontset_ascii (int id
)
868 Lisp_Object fontset
, elt
;
870 fontset
= FONTSET_FROM_ID (id
);
871 elt
= FONTSET_ASCII (fontset
);
877 /* Free fontset of FACE defined on frame F. Called from
878 free_realized_face. */
881 free_face_fontset (struct frame
*f
, struct face
*face
)
885 fontset
= FONTSET_FROM_ID (face
->fontset
);
888 eassert (! BASE_FONTSET_P (fontset
));
889 eassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
890 ASET (Vfontset_table
, face
->fontset
, Qnil
);
891 if (face
->fontset
< next_fontset_id
)
892 next_fontset_id
= face
->fontset
;
893 if (! NILP (FONTSET_DEFAULT (fontset
)))
895 int id
= XINT (FONTSET_ID (FONTSET_DEFAULT (fontset
)));
897 fontset
= AREF (Vfontset_table
, id
);
898 eassert (!NILP (fontset
) && ! BASE_FONTSET_P (fontset
));
899 eassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
900 ASET (Vfontset_table
, id
, Qnil
);
901 if (id
< next_fontset_id
)
902 next_fontset_id
= face
->fontset
;
907 /* Return ID of face suitable for displaying character C at buffer position
908 POS on frame F. FACE must be realized for ASCII characters in advance.
909 Called from the macro FACE_FOR_CHAR. */
912 face_for_char (struct frame
*f
, struct face
*face
, int c
,
913 ptrdiff_t pos
, Lisp_Object object
)
915 Lisp_Object fontset
, rfont_def
, charset
;
919 eassert (fontset_id_valid_p (face
->fontset
));
921 if (ASCII_CHAR_P (c
) || CHAR_BYTE8_P (c
))
922 return face
->ascii_face
->id
;
924 if (use_default_font_for_symbols
/* let the user disable this feature */
925 && c
> 0 && EQ (CHAR_TABLE_REF (Vchar_script_table
, c
), Qsymbol
))
927 /* Fonts often have characters for punctuation and other
928 symbols, even if they don't match the 'symbol' script. So
929 check if the character is present in the current ASCII face
930 first, and if so, use the same font as used by that face.
931 This avoids unnecessarily switching to another font when the
932 frame's default font will do. We only do this for symbols so
933 that users could still setup fontsets to force Emacs to use
934 specific fonts for characters from other scripts, because
935 choice of fonts is frequently affected by cultural
936 preferences and font features, not by font coverage.
937 However, these considerations are unlikely to be relevant to
938 punctuation and other symbols, since the latter generally
939 aren't specific to any culture, and don't require
940 sophisticated OTF features. */
941 Lisp_Object font_object
;
943 if (face
->ascii_face
->font
)
945 XSETFONT (font_object
, face
->ascii_face
->font
);
946 if (font_has_char (f
, font_object
, c
))
947 return face
->ascii_face
->id
;
951 /* Try the current face. Disabled because it can cause
952 counter-intuitive results, whereby the font used for some
953 character depends on the characters that precede it on
954 display. See the discussion of bug #15138. Note that the
955 original bug reported in #15138 was in a situation where face
956 == face->ascii_face, so the above code solves that situation
957 without risking the undesirable consequences. */
960 XSETFONT (font_object
, face
->font
);
961 if (font_has_char (f
, font_object
, c
)) return face
->id
;
966 fontset
= FONTSET_FROM_ID (face
->fontset
);
967 eassert (!BASE_FONTSET_P (fontset
));
976 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
977 if (CHARSETP (charset
))
981 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
982 if (CONSP (val
) && CHARSETP (XCDR (val
)))
983 charset
= XCDR (val
);
984 id
= XINT (CHARSET_SYMBOL_ID (charset
));
990 rfont_def
= fontset_font (fontset
, c
, face
, id
);
991 if (VECTORP (rfont_def
))
993 if (INTEGERP (RFONT_DEF_FACE (rfont_def
)))
994 face_id
= XINT (RFONT_DEF_FACE (rfont_def
));
997 Lisp_Object font_object
;
999 font_object
= RFONT_DEF_OBJECT (rfont_def
);
1000 face_id
= face_for_font (f
, font_object
, face
);
1001 RFONT_DEF_SET_FACE (rfont_def
, face_id
);
1006 if (INTEGERP (FONTSET_NOFONT_FACE (fontset
)))
1007 face_id
= XINT (FONTSET_NOFONT_FACE (fontset
));
1010 face_id
= face_for_font (f
, Qnil
, face
);
1011 set_fontset_nofont_face (fontset
, make_number (face_id
));
1014 eassert (face_id
>= 0);
1020 font_for_char (struct face
*face
, int c
, ptrdiff_t pos
, Lisp_Object object
)
1022 Lisp_Object fontset
, rfont_def
, charset
;
1025 if (ASCII_CHAR_P (c
))
1027 Lisp_Object font_object
;
1029 XSETFONT (font_object
, face
->ascii_face
->font
);
1033 eassert (fontset_id_valid_p (face
->fontset
));
1034 fontset
= FONTSET_FROM_ID (face
->fontset
);
1035 eassert (!BASE_FONTSET_P (fontset
));
1043 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
1044 if (CHARSETP (charset
))
1048 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
1049 if (CONSP (val
) && CHARSETP (XCDR (val
)))
1050 charset
= XCDR (val
);
1051 id
= XINT (CHARSET_SYMBOL_ID (charset
));
1057 rfont_def
= fontset_font (fontset
, c
, face
, id
);
1058 return (VECTORP (rfont_def
)
1059 ? RFONT_DEF_OBJECT (rfont_def
)
1064 /* Make a realized fontset for ASCII face FACE on frame F from the
1065 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1066 default fontset as the base. Value is the id of the new fontset.
1067 Called from realize_x_face. */
1070 make_fontset_for_ascii_face (struct frame
*f
, int base_fontset_id
, struct face
*face
)
1072 Lisp_Object base_fontset
, fontset
, frame
;
1074 XSETFRAME (frame
, f
);
1075 if (base_fontset_id
>= 0)
1077 base_fontset
= FONTSET_FROM_ID (base_fontset_id
);
1078 if (!BASE_FONTSET_P (base_fontset
))
1079 base_fontset
= FONTSET_BASE (base_fontset
);
1080 eassert (BASE_FONTSET_P (base_fontset
));
1083 base_fontset
= Vdefault_fontset
;
1085 fontset
= make_fontset (frame
, Qnil
, base_fontset
);
1086 return XINT (FONTSET_ID (fontset
));
1091 /* Cache data used by fontset_pattern_regexp. The car part is a
1092 pattern string containing at least one wild card, the cdr part is
1093 the corresponding regular expression. */
1094 static Lisp_Object Vcached_fontset_data
;
1096 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1097 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1099 /* If fontset name PATTERN contains any wild card, return regular
1100 expression corresponding to PATTERN. */
1103 fontset_pattern_regexp (Lisp_Object pattern
)
1105 if (!strchr (SSDATA (pattern
), '*')
1106 && !strchr (SSDATA (pattern
), '?'))
1107 /* PATTERN does not contain any wild cards. */
1110 if (!CONSP (Vcached_fontset_data
)
1111 || strcmp (SSDATA (pattern
), CACHED_FONTSET_NAME
))
1113 /* We must at first update the cached data. */
1114 unsigned char *regex
, *p0
, *p1
;
1115 int ndashes
= 0, nstars
= 0, nescs
= 0;
1117 for (p0
= SDATA (pattern
); *p0
; p0
++)
1121 else if (*p0
== '*')
1124 || *p0
== '.' || *p0
== '\\'
1125 || *p0
== '+' || *p0
== '^'
1130 /* If PATTERN is not full XLFD we convert "*" to ".*". Otherwise
1131 we convert "*" to "[^-]*" which is much faster in regular
1132 expression matching. */
1133 ptrdiff_t regexsize
= (SBYTES (pattern
)
1134 + (ndashes
< 14 ? 2 : 5) * nstars
1137 p1
= regex
= SAFE_ALLOCA (regexsize
);
1140 for (p0
= SDATA (pattern
); *p0
; p0
++)
1147 *p1
++ = '[', *p1
++ = '^', *p1
++ = '-', *p1
++ = ']';
1150 else if (*p0
== '?')
1153 || *p0
== '.' || *p0
== '\\'
1154 || *p0
== '+' || *p0
== '^'
1156 *p1
++ = '\\', *p1
++ = *p0
;
1163 Vcached_fontset_data
= Fcons (build_string (SSDATA (pattern
)),
1164 build_string ((char *) regex
));
1168 return CACHED_FONTSET_REGEX
;
1171 /* Return ID of the base fontset named NAME. If there's no such
1172 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1173 0: pattern containing '*' and '?' as wildcards
1174 1: regular expression
1175 2: literal fontset name
1179 fs_query_fontset (Lisp_Object name
, int name_pattern
)
1184 name
= Fdowncase (name
);
1185 if (name_pattern
!= 1)
1187 tem
= Frassoc (name
, Vfontset_alias_alist
);
1189 tem
= Fassoc (name
, Vfontset_alias_alist
);
1190 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
1192 else if (name_pattern
== 0)
1194 tem
= fontset_pattern_regexp (name
);
1203 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
1205 Lisp_Object fontset
, this_name
;
1207 fontset
= FONTSET_FROM_ID (i
);
1209 || !BASE_FONTSET_P (fontset
))
1212 this_name
= FONTSET_NAME (fontset
);
1213 if (name_pattern
== 1
1214 ? fast_string_match_ignore_case (name
, this_name
) >= 0
1215 : !xstrcasecmp (SSDATA (name
), SSDATA (this_name
)))
1222 DEFUN ("query-fontset", Fquery_fontset
, Squery_fontset
, 1, 2, 0,
1223 doc
: /* Return the name of a fontset that matches PATTERN.
1224 The value is nil if there is no matching fontset.
1225 PATTERN can contain `*' or `?' as a wildcard
1226 just as X font name matching algorithm allows.
1227 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1228 (Lisp_Object pattern
, Lisp_Object regexpp
)
1230 Lisp_Object fontset
;
1233 check_window_system (NULL
);
1235 CHECK_STRING (pattern
);
1237 if (SCHARS (pattern
) == 0)
1240 id
= fs_query_fontset (pattern
, !NILP (regexpp
));
1244 fontset
= FONTSET_FROM_ID (id
);
1245 return FONTSET_NAME (fontset
);
1248 /* Return a list of base fontset names matching PATTERN on frame F. */
1251 list_fontsets (struct frame
*f
, Lisp_Object pattern
, int size
)
1253 Lisp_Object frame
, regexp
, val
;
1256 XSETFRAME (frame
, f
);
1258 regexp
= fontset_pattern_regexp (pattern
);
1261 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1263 Lisp_Object fontset
, name
;
1265 fontset
= FONTSET_FROM_ID (id
);
1267 || !BASE_FONTSET_P (fontset
)
1268 || !EQ (frame
, FONTSET_FRAME (fontset
)))
1270 name
= FONTSET_NAME (fontset
);
1272 if (STRINGP (regexp
)
1273 ? (fast_string_match (regexp
, name
) < 0)
1274 : strcmp (SSDATA (pattern
), SSDATA (name
)))
1277 val
= Fcons (Fcopy_sequence (FONTSET_NAME (fontset
)), val
);
1284 /* Free all realized fontsets whose base fontset is BASE. */
1287 free_realized_fontsets (Lisp_Object base
)
1292 /* For the moment, this doesn't work because free_realized_face
1293 doesn't remove FACE from a cache. Until we find a solution, we
1294 suppress this code, and simply use Fclear_face_cache even though
1295 that is not efficient. */
1297 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1299 Lisp_Object
this = AREF (Vfontset_table
, id
);
1301 if (EQ (FONTSET_BASE (this), base
))
1305 for (tail
= FONTSET_FACE_ALIST (this); CONSP (tail
);
1308 struct frame
*f
= XFRAME (FONTSET_FRAME (this));
1309 int face_id
= XINT (XCDR (XCAR (tail
)));
1310 struct face
*face
= FACE_FROM_ID_OR_NULL (f
, face_id
);
1312 /* Face THIS itself is also freed by the following call. */
1313 free_realized_face (f
, face
);
1319 /* But, we don't have to call Fclear_face_cache if no fontset has
1320 been realized from BASE. */
1321 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1323 Lisp_Object
this = AREF (Vfontset_table
, id
);
1325 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base
))
1327 Fclear_face_cache (Qt
);
1335 /* Check validity of NAME as a fontset name and return the
1336 corresponding fontset. If not valid, signal an error.
1338 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1341 Set *FRAME to the actual frame. */
1344 check_fontset_name (Lisp_Object name
, Lisp_Object
*frame
)
1347 struct frame
*f
= decode_live_frame (*frame
);
1349 XSETFRAME (*frame
, f
);
1352 return Vdefault_fontset
;
1354 id
= FRAME_FONTSET (f
);
1357 CHECK_STRING (name
);
1358 /* First try NAME as literal. */
1359 id
= fs_query_fontset (name
, 2);
1361 /* For backward compatibility, try again NAME as pattern. */
1362 id
= fs_query_fontset (name
, 0);
1364 error ("Fontset `%s' does not exist", SDATA (name
));
1366 return FONTSET_FROM_ID (id
);
1370 accumulate_script_ranges (Lisp_Object arg
, Lisp_Object range
, Lisp_Object val
)
1372 if (EQ (XCAR (arg
), val
))
1375 XSETCDR (arg
, Fcons (Fcons (XCAR (range
), XCDR (range
)), XCDR (arg
)));
1377 XSETCDR (arg
, Fcons (Fcons (range
, range
), XCDR (arg
)));
1382 /* Callback function for map_charset_chars in Fset_fontset_font.
1383 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1385 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1386 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1387 The consumed ranges are popped up from SCRIPT_RANGE_LIST, and the
1388 new SCRIPT_RANGE_LIST is stored in ARG.
1390 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1391 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1395 set_fontset_font (Lisp_Object arg
, Lisp_Object range
)
1397 Lisp_Object fontset
, font_def
, add
, ascii
, script_range_list
;
1398 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
1400 fontset
= AREF (arg
, 0);
1401 font_def
= AREF (arg
, 1);
1402 add
= AREF (arg
, 2);
1403 ascii
= AREF (arg
, 3);
1404 script_range_list
= AREF (arg
, 4);
1406 if (NILP (ascii
) && from
< 0x80)
1411 range
= Fcons (make_number (0x80), XCDR (range
));
1414 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1415 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1416 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1418 for (; CONSP (script_range_list
) && SCRIPT_TO
< from
; POP_SCRIPT_RANGE ())
1419 FONTSET_ADD (fontset
, XCAR (script_range_list
), font_def
, add
);
1420 if (CONSP (script_range_list
))
1422 if (SCRIPT_FROM
< from
)
1423 range
= Fcons (make_number (SCRIPT_FROM
), XCDR (range
));
1424 while (CONSP (script_range_list
) && SCRIPT_TO
<= to
)
1425 POP_SCRIPT_RANGE ();
1426 if (CONSP (script_range_list
) && SCRIPT_FROM
<= to
)
1427 XSETCAR (XCAR (script_range_list
), make_number (to
+ 1));
1430 FONTSET_ADD (fontset
, range
, font_def
, add
);
1431 ASET (arg
, 4, script_range_list
);
1434 static void update_auto_fontset_alist (Lisp_Object
, Lisp_Object
);
1437 DEFUN ("set-fontset-font", Fset_fontset_font
, Sset_fontset_font
, 3, 5, 0,
1439 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1441 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1442 the default fontset.
1444 TARGET may be a single character to use FONT-SPEC for.
1446 Target may be a cons (FROM . TO), where FROM and TO are characters.
1447 In that case, use FONT-SPEC for all characters in the range FROM
1450 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1451 all characters that belong to the script.
1453 TARGET may be a charset. In that case, use FONT-SPEC for all
1454 characters in the charset.
1456 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1457 that no FONT-SPEC is specified.
1459 FONT-SPEC may one of these:
1460 * A font-spec object made by the function `font-spec' (which see).
1461 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1462 REGISTRY is a font registry name. FAMILY may contain foundry
1463 name, and REGISTRY may contain encoding name.
1464 * A font name string.
1465 * nil, which explicitly specifies that there's no font for TARGET.
1467 Optional 4th argument FRAME is a frame or nil for the selected frame
1468 that is concerned in the case that NAME is nil.
1470 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1471 to the font specifications for TARGET previously set. If it is
1472 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1473 appended. By default, FONT-SPEC overrides the previous settings. */)
1474 (Lisp_Object name
, Lisp_Object target
, Lisp_Object font_spec
, Lisp_Object frame
, Lisp_Object add
)
1476 Lisp_Object fontset
;
1477 Lisp_Object font_def
, registry
, family
;
1478 Lisp_Object range_list
;
1479 struct charset
*charset
= NULL
;
1480 Lisp_Object fontname
;
1481 bool ascii_changed
= 0;
1483 fontset
= check_fontset_name (name
, &frame
);
1486 if (CONSP (font_spec
))
1488 Lisp_Object spec
= Ffont_spec (0, NULL
);
1490 font_parse_family_registry (XCAR (font_spec
), XCDR (font_spec
), spec
);
1492 fontname
= Ffont_xlfd_name (font_spec
, Qnil
);
1494 else if (STRINGP (font_spec
))
1496 fontname
= font_spec
;
1497 font_spec
= CALLN (Ffont_spec
, QCname
, fontname
);
1499 else if (FONT_SPEC_P (font_spec
))
1500 fontname
= Ffont_xlfd_name (font_spec
, Qnil
);
1501 else if (! NILP (font_spec
))
1502 Fsignal (Qfont
, list2 (build_string ("Invalid font-spec"), font_spec
));
1504 if (! NILP (font_spec
))
1506 Lisp_Object encoding
, repertory
;
1508 family
= AREF (font_spec
, FONT_FAMILY_INDEX
);
1509 if (! NILP (family
) )
1510 family
= SYMBOL_NAME (family
);
1511 registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1512 if (! NILP (registry
))
1513 registry
= Fdowncase (SYMBOL_NAME (registry
));
1514 AUTO_STRING (dash
, "-");
1515 encoding
= find_font_encoding (concat3 (family
, dash
, registry
));
1516 if (NILP (encoding
))
1519 if (SYMBOLP (encoding
))
1521 CHECK_CHARSET (encoding
);
1522 encoding
= repertory
= CHARSET_SYMBOL_ID (encoding
);
1526 repertory
= XCDR (encoding
);
1527 encoding
= XCAR (encoding
);
1528 CHECK_CHARSET (encoding
);
1529 encoding
= CHARSET_SYMBOL_ID (encoding
);
1530 if (! NILP (repertory
) && SYMBOLP (repertory
))
1532 CHECK_CHARSET (repertory
);
1533 repertory
= CHARSET_SYMBOL_ID (repertory
);
1536 FONT_DEF_NEW (font_def
, font_spec
, encoding
, repertory
);
1541 if (CHARACTERP (target
))
1543 if (XFASTINT (target
) < 0x80)
1544 error ("Can't set a font for partial ASCII range");
1545 range_list
= list1 (Fcons (target
, target
));
1547 else if (CONSP (target
))
1549 Lisp_Object from
, to
;
1551 from
= Fcar (target
);
1553 CHECK_CHARACTER (from
);
1554 CHECK_CHARACTER (to
);
1555 if (XFASTINT (from
) < 0x80)
1557 if (XFASTINT (from
) != 0 || XFASTINT (to
) < 0x7F)
1558 error ("Can't set a font for partial ASCII range");
1561 range_list
= list1 (target
);
1563 else if (SYMBOLP (target
) && !NILP (target
))
1565 Lisp_Object script_list
;
1569 script_list
= XCHAR_TABLE (Vchar_script_table
)->extras
[0];
1570 if (! NILP (Fmemq (target
, script_list
)))
1572 if (EQ (target
, Qlatin
))
1574 val
= list1 (target
);
1575 map_char_table (accumulate_script_ranges
, Qnil
, Vchar_script_table
,
1577 range_list
= Fnreverse (XCDR (val
));
1579 if (CHARSETP (target
))
1581 CHECK_CHARSET_GET_CHARSET (target
, charset
);
1582 if (charset
->ascii_compatible_p
)
1585 else if (NILP (range_list
))
1586 error ("Invalid script or charset name: %s",
1587 SDATA (SYMBOL_NAME (target
)));
1589 else if (NILP (target
))
1590 range_list
= list1 (Qnil
);
1592 error ("Invalid target for setting a font");
1598 if (NILP (font_spec
))
1599 error ("Can't set ASCII font to nil");
1600 val
= CHAR_TABLE_REF (fontset
, 0);
1601 if (! NILP (val
) && EQ (add
, Qappend
))
1602 /* We are going to change just an additional font for ASCII. */
1610 arg
= make_uninit_vector (5);
1611 ASET (arg
, 0, fontset
);
1612 ASET (arg
, 1, font_def
);
1614 ASET (arg
, 3, ascii_changed
? Qt
: Qnil
);
1615 ASET (arg
, 4, range_list
);
1617 map_charset_chars (set_fontset_font
, Qnil
, arg
, charset
,
1618 CHARSET_MIN_CODE (charset
),
1619 CHARSET_MAX_CODE (charset
));
1620 range_list
= AREF (arg
, 4);
1622 for (; CONSP (range_list
); range_list
= XCDR (range_list
))
1623 FONTSET_ADD (fontset
, XCAR (range_list
), font_def
, add
);
1627 Lisp_Object tail
, fr
;
1628 int fontset_id
= XINT (FONTSET_ID (fontset
));
1630 set_fontset_ascii (fontset
, fontname
);
1631 name
= FONTSET_NAME (fontset
);
1632 FOR_EACH_FRAME (tail
, fr
)
1634 struct frame
*f
= XFRAME (fr
);
1635 Lisp_Object font_object
;
1638 if (FRAME_INITIAL_P (f
) || FRAME_TERMCAP_P (f
))
1640 if (fontset_id
!= FRAME_FONTSET (f
))
1642 face
= FACE_FROM_ID_OR_NULL (f
, DEFAULT_FACE_ID
);
1644 font_object
= font_load_for_lface (f
, face
->lface
, font_spec
);
1646 font_object
= font_open_by_spec (f
, font_spec
);
1647 if (! NILP (font_object
))
1649 update_auto_fontset_alist (font_object
, fontset
);
1650 AUTO_FRAME_ARG (arg
, Qfont
, Fcons (name
, font_object
));
1651 Fmodify_frame_parameters (fr
, arg
);
1656 /* Free all realized fontsets whose base is FONTSET. This way, the
1657 specified character(s) are surely redisplayed by a correct
1659 free_realized_fontsets (fontset
);
1665 DEFUN ("new-fontset", Fnew_fontset
, Snew_fontset
, 2, 2, 0,
1666 doc
: /* Create a new fontset NAME from font information in FONTLIST.
1668 FONTLIST is an alist of scripts vs the corresponding font specification list.
1669 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1670 character of SCRIPT is displayed by a font that matches one of
1673 SCRIPT is a symbol that appears in the first extra slot of the
1674 char-table `char-script-table'.
1676 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1677 `set-fontset-font' for the meaning. */)
1678 (Lisp_Object name
, Lisp_Object fontlist
)
1680 Lisp_Object fontset
;
1683 CHECK_STRING (name
);
1684 CHECK_LIST (fontlist
);
1686 name
= Fdowncase (name
);
1687 id
= fs_query_fontset (name
, 0);
1690 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1691 Lisp_Object short_name
;
1695 if (font_parse_xlfd (SSDATA (name
), SBYTES (name
), font_spec
) < 0)
1696 error ("Fontset name must be in XLFD format");
1697 short_name
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1698 if (strncmp (SSDATA (SYMBOL_NAME (short_name
)), "fontset-", 8)
1699 || SBYTES (SYMBOL_NAME (short_name
)) < 9)
1700 error ("Registry field of fontset name must be \"fontset-*\"");
1701 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (short_name
)),
1702 Vfontset_alias_alist
);
1703 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso8859_1
);
1704 fontset
= make_fontset (Qnil
, name
, Qnil
);
1705 len
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1707 error ("Invalid fontset name (perhaps too long): %s", SDATA (name
));
1708 set_fontset_ascii (fontset
, make_unibyte_string (xlfd
, len
));
1712 fontset
= FONTSET_FROM_ID (id
);
1713 free_realized_fontsets (fontset
);
1714 Fset_char_table_range (fontset
, Qt
, Qnil
);
1717 for (; CONSP (fontlist
); fontlist
= XCDR (fontlist
))
1719 Lisp_Object elt
, script
;
1721 elt
= XCAR (fontlist
);
1722 script
= Fcar (elt
);
1724 if (CONSP (elt
) && (NILP (XCDR (elt
)) || CONSP (XCDR (elt
))))
1725 for (; CONSP (elt
); elt
= XCDR (elt
))
1726 Fset_fontset_font (name
, script
, XCAR (elt
), Qnil
, Qappend
);
1728 Fset_fontset_font (name
, script
, elt
, Qnil
, Qappend
);
1734 /* Alist of automatically created fontsets. Each element is a cons
1735 (FONT-SPEC . FONTSET-ID). */
1736 static Lisp_Object auto_fontset_alist
;
1738 /* Number of automatically created fontsets. */
1739 static ptrdiff_t num_auto_fontsets
;
1741 /* Return a fontset synthesized from FONT-OBJECT. This is called from
1742 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1743 frame, and the returned fontset is used for the default fontset of
1744 that frame. The fontset specifies a font of the same registry as
1745 FONT-OBJECT for all characters in the repertory of the registry
1746 (see Vfont_encoding_alist). If the repertory is not known, the
1747 fontset specifies the font for all Latin characters assuming that a
1748 user intends to use FONT-OBJECT for Latin characters. */
1751 fontset_from_font (Lisp_Object font_object
)
1753 Lisp_Object font_name
= font_get_name (font_object
);
1754 Lisp_Object font_spec
= copy_font_spec (font_object
);
1755 Lisp_Object registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1756 Lisp_Object fontset_spec
, alias
, name
, fontset
;
1759 val
= assoc_no_quit (font_spec
, auto_fontset_alist
);
1761 return XINT (FONTSET_ID (XCDR (val
)));
1762 if (num_auto_fontsets
++ == 0)
1763 alias
= intern ("fontset-startup");
1766 char temp
[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1768 sprintf (temp
, "fontset-auto%"pD
"d", num_auto_fontsets
- 1);
1769 alias
= intern (temp
);
1771 fontset_spec
= copy_font_spec (font_spec
);
1772 ASET (fontset_spec
, FONT_REGISTRY_INDEX
, alias
);
1773 name
= Ffont_xlfd_name (fontset_spec
, Qnil
);
1774 eassert (!NILP (name
));
1775 fontset
= make_fontset (Qnil
, name
, Qnil
);
1776 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (alias
)),
1777 Vfontset_alias_alist
);
1778 alias
= Fdowncase (AREF (font_object
, FONT_NAME_INDEX
));
1779 Vfontset_alias_alist
= Fcons (Fcons (name
, alias
), Vfontset_alias_alist
);
1780 auto_fontset_alist
= Fcons (Fcons (font_spec
, fontset
), auto_fontset_alist
);
1781 font_spec
= Ffont_spec (0, NULL
);
1782 ASET (font_spec
, FONT_REGISTRY_INDEX
, registry
);
1784 Lisp_Object target
= find_font_encoding (SYMBOL_NAME (registry
));
1787 target
= XCDR (target
);
1788 if (! CHARSETP (target
))
1790 Fset_fontset_font (name
, target
, font_spec
, Qnil
, Qnil
);
1791 Fset_fontset_font (name
, Qnil
, font_spec
, Qnil
, Qnil
);
1794 set_fontset_ascii (fontset
, font_name
);
1796 return XINT (FONTSET_ID (fontset
));
1800 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1801 FONTSET is changed, we delete an entry of FONTSET if any from
1802 auto_fontset_alist so that FONTSET is not re-used by
1803 fontset_from_font. */
1806 update_auto_fontset_alist (Lisp_Object font_object
, Lisp_Object fontset
)
1808 Lisp_Object prev
, tail
;
1810 for (prev
= Qnil
, tail
= auto_fontset_alist
; CONSP (tail
);
1811 prev
= tail
, tail
= XCDR (tail
))
1812 if (EQ (fontset
, XCDR (XCAR (tail
))))
1815 auto_fontset_alist
= XCDR (tail
);
1817 XSETCDR (prev
, XCDR (tail
));
1823 DEFUN ("fontset-info", Ffontset_info
, Sfontset_info
, 1, 2, 0,
1824 doc
: /* Return information about a fontset FONTSET on frame FRAME.
1826 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1827 for the default fontset. FRAME nil means the selected frame.
1829 The value is a char-table whose elements have this form:
1831 ((FONT OPENED-FONT ...) ...)
1833 FONT is a name of font specified for a range of characters.
1835 OPENED-FONT is a name of a font actually opened.
1837 The char-table has one extra slot. If FONTSET is not the default
1838 fontset, the value the extra slot is a char-table containing the
1839 information about the derived fonts from the default fontset. The
1840 format is the same as above. */)
1841 (Lisp_Object fontset
, Lisp_Object frame
)
1843 Lisp_Object
*realized
[2], fontsets
[2], tables
[2];
1844 Lisp_Object val
, elt
;
1847 check_window_system (NULL
);
1848 fontset
= check_fontset_name (fontset
, &frame
);
1850 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1851 in the table `realized'. */
1853 SAFE_ALLOCA_LISP (realized
[0], 2 * ASIZE (Vfontset_table
));
1854 realized
[1] = realized
[0] + ASIZE (Vfontset_table
);
1855 for (i
= j
= 0; i
< ASIZE (Vfontset_table
); i
++)
1857 elt
= FONTSET_FROM_ID (i
);
1859 && EQ (FONTSET_BASE (elt
), fontset
)
1860 && EQ (FONTSET_FRAME (elt
), frame
))
1861 realized
[0][j
++] = elt
;
1863 realized
[0][j
] = Qnil
;
1865 for (i
= j
= 0; ! NILP (realized
[0][i
]); i
++)
1867 elt
= FONTSET_DEFAULT (realized
[0][i
]);
1869 realized
[1][j
++] = elt
;
1871 realized
[1][j
] = Qnil
;
1873 tables
[0] = Fmake_char_table (Qfontset_info
, Qnil
);
1874 fontsets
[0] = fontset
;
1875 if (!EQ (fontset
, Vdefault_fontset
))
1877 tables
[1] = Fmake_char_table (Qnil
, Qnil
);
1878 set_char_table_extras (tables
[0], 0, tables
[1]);
1879 fontsets
[1] = Vdefault_fontset
;
1882 /* Accumulate information of the fontset in TABLE. The format of
1883 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1884 for (k
= 0; k
<= 1; k
++)
1886 for (c
= 0; c
<= MAX_CHAR
; )
1888 int from
= c
, to
= MAX_5_BYTE_CHAR
;
1890 if (c
<= MAX_5_BYTE_CHAR
)
1892 val
= char_table_ref_and_range (fontsets
[k
], c
, &from
, &to
);
1896 val
= FONTSET_FALLBACK (fontsets
[k
]);
1903 /* At first, set ALIST to ((FONT-SPEC) ...). */
1904 for (alist
= Qnil
, i
= 0; i
< ASIZE (val
); i
++)
1905 if (! NILP (AREF (val
, i
)))
1906 alist
= Fcons (Fcons (FONT_DEF_SPEC (AREF (val
, i
)), Qnil
),
1908 alist
= Fnreverse (alist
);
1910 /* Then store opened font names to cdr of each elements. */
1911 for (i
= 0; ! NILP (realized
[k
][i
]); i
++)
1913 if (c
<= MAX_5_BYTE_CHAR
)
1914 val
= FONTSET_REF (realized
[k
][i
], c
);
1916 val
= FONTSET_FALLBACK (realized
[k
][i
]);
1917 if (! CONSP (val
) || ! VECTORP (XCDR (val
)))
1919 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1921 for (j
= 0; j
< ASIZE (val
); j
++)
1923 elt
= AREF (val
, j
);
1924 if (!NILP (elt
) && FONT_OBJECT_P (RFONT_DEF_OBJECT (elt
)))
1926 Lisp_Object font_object
= RFONT_DEF_OBJECT (elt
);
1927 Lisp_Object slot
, name
;
1929 slot
= Fassq (RFONT_DEF_SPEC (elt
), alist
);
1930 name
= AREF (font_object
, FONT_NAME_INDEX
);
1931 if (NILP (Fmember (name
, XCDR (slot
))))
1932 nconc2 (slot
, list1 (name
));
1937 /* Store ALIST in TBL for characters C..TO. */
1938 if (c
<= MAX_5_BYTE_CHAR
)
1939 char_table_set_range (tables
[k
], c
, to
, alist
);
1941 set_char_table_defalt (tables
[k
], alist
);
1943 /* At last, change each elements to font names. */
1944 for (; CONSP (alist
); alist
= XCDR (alist
))
1947 XSETCAR (elt
, Ffont_xlfd_name (XCAR (elt
), Qnil
));
1952 if (EQ (fontset
, Vdefault_fontset
))
1961 DEFUN ("fontset-font", Ffontset_font
, Sfontset_font
, 2, 3, 0,
1962 doc
: /* Return a font name pattern for character CH in fontset NAME.
1963 If NAME is t, find a pattern in the default fontset.
1964 If NAME is nil, find a pattern in the fontset of the selected frame.
1966 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
1967 family name and REGISTRY is a font registry name. This is actually
1968 the first font name pattern for CH in the fontset or in the default
1971 If the 2nd optional arg ALL is non-nil, return a list of all font name
1973 (Lisp_Object name
, Lisp_Object ch
, Lisp_Object all
)
1976 Lisp_Object fontset
, elt
, list
, repertory
, val
;
1981 fontset
= check_fontset_name (name
, &frame
);
1983 CHECK_CHARACTER (ch
);
1988 for (i
= 0, elt
= FONTSET_REF (fontset
, c
); i
< 2;
1989 i
++, elt
= FONTSET_FALLBACK (fontset
))
1991 for (j
= 0; j
< ASIZE (elt
); j
++)
1993 Lisp_Object family
, registry
;
1995 val
= AREF (elt
, j
);
1998 repertory
= AREF (val
, 1);
1999 if (INTEGERP (repertory
))
2001 struct charset
*charset
= CHARSET_FROM_ID (XINT (repertory
));
2003 if (! CHAR_CHARSET_P (c
, charset
))
2006 else if (CHAR_TABLE_P (repertory
))
2008 if (NILP (CHAR_TABLE_REF (repertory
, c
)))
2011 val
= AREF (val
, 0);
2012 /* VAL is a FONT-SPEC */
2013 family
= AREF (val
, FONT_FAMILY_INDEX
);
2014 if (! NILP (family
))
2015 family
= SYMBOL_NAME (family
);
2016 registry
= AREF (val
, FONT_REGISTRY_INDEX
);
2017 if (! NILP (registry
))
2018 registry
= SYMBOL_NAME (registry
);
2019 val
= Fcons (family
, registry
);
2022 list
= Fcons (val
, list
);
2024 if (EQ (fontset
, Vdefault_fontset
))
2026 fontset
= Vdefault_fontset
;
2028 return (Fnreverse (list
));
2031 DEFUN ("fontset-list", Ffontset_list
, Sfontset_list
, 0, 0, 0,
2032 doc
: /* Return a list of all defined fontset names. */)
2035 Lisp_Object fontset
, list
;
2039 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
2041 fontset
= FONTSET_FROM_ID (i
);
2043 && BASE_FONTSET_P (fontset
))
2044 list
= Fcons (FONTSET_NAME (fontset
), list
);
2051 #ifdef ENABLE_CHECKING
2053 Lisp_Object
dump_fontset (Lisp_Object
) EXTERNALLY_VISIBLE
;
2056 dump_fontset (Lisp_Object fontset
)
2060 vec
= Fmake_vector (make_number (3), Qnil
);
2061 ASET (vec
, 0, FONTSET_ID (fontset
));
2063 if (BASE_FONTSET_P (fontset
))
2065 ASET (vec
, 1, FONTSET_NAME (fontset
));
2071 frame
= FONTSET_FRAME (fontset
);
2074 struct frame
*f
= XFRAME (frame
);
2076 if (FRAME_LIVE_P (f
))
2078 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)),
2082 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), Qnil
));
2084 if (!NILP (FONTSET_DEFAULT (fontset
)))
2085 ASET (vec
, 2, FONTSET_ID (FONTSET_DEFAULT (fontset
)));
2090 DEFUN ("fontset-list-all", Ffontset_list_all
, Sfontset_list_all
, 0, 0, 0,
2091 doc
: /* Return a brief summary of all fontsets for debug use. */)
2097 for (i
= 0, val
= Qnil
; i
< ASIZE (Vfontset_table
); i
++)
2098 if (! NILP (AREF (Vfontset_table
, i
)))
2099 val
= Fcons (dump_fontset (AREF (Vfontset_table
, i
)), val
);
2100 return (Fnreverse (val
));
2102 #endif /* ENABLE_CHECKING */
2105 syms_of_fontset (void)
2107 DEFSYM (Qfontset
, "fontset");
2108 Fput (Qfontset
, Qchar_table_extra_slots
, make_number (8));
2109 DEFSYM (Qfontset_info
, "fontset-info");
2110 Fput (Qfontset_info
, Qchar_table_extra_slots
, make_number (1));
2112 DEFSYM (Qappend
, "append");
2113 DEFSYM (Qlatin
, "latin");
2115 Vcached_fontset_data
= Qnil
;
2116 staticpro (&Vcached_fontset_data
);
2118 Vfontset_table
= Fmake_vector (make_number (32), Qnil
);
2119 staticpro (&Vfontset_table
);
2121 Vdefault_fontset
= Fmake_char_table (Qfontset
, Qnil
);
2122 staticpro (&Vdefault_fontset
);
2123 set_fontset_id (Vdefault_fontset
, make_number (0));
2126 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2127 ASET (Vfontset_table
, 0, Vdefault_fontset
);
2128 next_fontset_id
= 1;
2130 auto_fontset_alist
= Qnil
;
2131 staticpro (&auto_fontset_alist
);
2133 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist
,
2135 Alist of charsets vs the charsets to determine the preferred font encoding.
2136 Each element looks like (CHARSET . ENCODING-CHARSET),
2137 where ENCODING-CHARSET is a charset registered in the variable
2138 `font-encoding-alist' as ENCODING.
2140 When a text has a property `charset' and the value is CHARSET, a font
2141 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2142 Vfont_encoding_charset_alist
= Qnil
;
2144 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent
,
2146 Char table of characters whose ascent values should be ignored.
2147 If an entry for a character is non-nil, the ascent value of the glyph
2148 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2150 This affects how a composite character which contains
2151 such a character is displayed on screen. */);
2152 Vuse_default_ascent
= Qnil
;
2154 DEFVAR_BOOL ("use-default-font-for-symbols", use_default_font_for_symbols
,
2156 If non-nil, use the default face's font for symbols and punctuation.
2158 By default, Emacs will try to use the default face's font for
2159 displaying symbol and punctuation characters, disregarding the
2160 fontsets, if the default font can display the character.
2161 Set this to nil to make Emacs honor the fontsets instead. */);
2162 use_default_font_for_symbols
= 1;
2164 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition
,
2166 Char table of characters which are not composed relatively.
2167 If an entry for a character is non-nil, a composition sequence
2168 which contains that character is displayed so that
2169 the glyph of that character is put without considering
2170 an ascent and descent value of a previous character. */);
2171 Vignore_relative_composition
= Qnil
;
2173 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist
,
2174 doc
: /* Alist of fontname vs list of the alternate fontnames.
2175 When a specified font name is not found, the corresponding
2176 alternate fontnames (if any) are tried instead. */);
2177 Valternate_fontname_alist
= Qnil
;
2179 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist
,
2180 doc
: /* Alist of fontset names vs the aliases. */);
2181 Vfontset_alias_alist
2182 = list1 (Fcons (FONTSET_NAME (Vdefault_fontset
),
2183 build_pure_c_string ("fontset-default")));
2185 DEFVAR_LISP ("vertical-centering-font-regexp",
2186 Vvertical_centering_font_regexp
,
2187 doc
: /* Regexp matching font names that require vertical centering on display.
2188 When a character is displayed with such fonts, the character is displayed
2189 at the vertical center of lines. */);
2190 Vvertical_centering_font_regexp
= Qnil
;
2192 DEFVAR_LISP ("otf-script-alist", Votf_script_alist
,
2193 doc
: /* Alist of OpenType script tags vs the corresponding script names. */);
2194 Votf_script_alist
= Qnil
;
2196 defsubr (&Squery_fontset
);
2197 defsubr (&Snew_fontset
);
2198 defsubr (&Sset_fontset_font
);
2199 defsubr (&Sfontset_info
);
2200 defsubr (&Sfontset_font
);
2201 defsubr (&Sfontset_list
);
2202 #ifdef ENABLE_CHECKING
2203 defsubr (&Sfontset_list_all
);