3 Copyright (C) 2001-2015 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 /* Vector containing all fontsets. */
156 static Lisp_Object Vfontset_table
;
158 /* Next possibly free fontset ID. Usually this keeps the minimum
159 fontset ID not yet used. */
160 static int next_fontset_id
;
162 /* The default fontset. This gives default FAMILY and REGISTRY of
163 font for each character. */
164 static Lisp_Object Vdefault_fontset
;
166 /* Prototype declarations for static functions. */
167 static Lisp_Object
make_fontset (Lisp_Object
, Lisp_Object
, Lisp_Object
);
169 /* Return true if ID is a valid fontset id.
170 Optimized away if ENABLE_CHECKING is not defined. */
173 fontset_id_valid_p (int id
)
175 return (id
>= 0 && id
< ASIZE (Vfontset_table
) - 1);
180 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
182 /* Return the fontset with ID. No check of ID's validness. */
183 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
185 /* Access special values of FONTSET. */
187 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
189 set_fontset_id (Lisp_Object fontset
, Lisp_Object id
)
191 set_char_table_extras (fontset
, 0, id
);
194 /* Access special values of (base) FONTSET. */
196 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
198 set_fontset_name (Lisp_Object fontset
, Lisp_Object name
)
200 set_char_table_extras (fontset
, 1, name
);
203 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
205 set_fontset_ascii (Lisp_Object fontset
, Lisp_Object ascii
)
207 set_char_table_extras (fontset
, 2, ascii
);
210 /* Access special values of (realized) FONTSET. */
212 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
214 set_fontset_base (Lisp_Object fontset
, Lisp_Object base
)
216 set_char_table_extras (fontset
, 3, base
);
219 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
221 set_fontset_frame (Lisp_Object fontset
, Lisp_Object frame
)
223 set_char_table_extras (fontset
, 4, frame
);
226 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
228 set_fontset_nofont_face (Lisp_Object fontset
, Lisp_Object face
)
230 set_char_table_extras (fontset
, 5, face
);
233 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
235 set_fontset_default (Lisp_Object fontset
, Lisp_Object def
)
237 set_char_table_extras (fontset
, 6, def
);
240 /* For both base and realized fontset. */
242 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
244 set_fontset_fallback (Lisp_Object fontset
, Lisp_Object fallback
)
246 set_char_table_extras (fontset
, 7, fallback
);
249 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
251 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
252 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
254 (font_def) = make_uninit_vector (3); \
255 ASET ((font_def), 0, font_spec); \
256 ASET ((font_def), 1, encoding); \
257 ASET ((font_def), 2, repertory); \
260 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
261 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
262 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
264 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
265 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
266 ASET ((rfont_def), 0, make_number (face_id))
267 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
268 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
269 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
270 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
271 ASET ((rfont_def), 2, (object))
272 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
273 the order of listing by font backends, the higher bits represents
274 the order given by charset priority list. The smaller value is
276 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
277 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
278 ASET ((rfont_def), 3, make_number (score))
279 #define RFONT_DEF_NEW(rfont_def, font_def) \
281 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
282 ASET ((rfont_def), 1, (font_def)); \
283 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
287 /* Return the element of FONTSET for the character C. If FONTSET is a
288 base fontset other then the default fontset and FONTSET doesn't
289 contain information for C, return the information in the default
292 #define FONTSET_REF(fontset, c) \
293 (EQ (fontset, Vdefault_fontset) \
294 ? CHAR_TABLE_REF (fontset, c) \
295 : fontset_ref ((fontset), (c)))
298 fontset_ref (Lisp_Object fontset
, int c
)
302 elt
= CHAR_TABLE_REF (fontset
, c
);
303 if (NILP (elt
) && ! EQ (fontset
, Vdefault_fontset
)
304 /* Don't check Vdefault_fontset for a realized fontset. */
305 && NILP (FONTSET_BASE (fontset
)))
306 elt
= CHAR_TABLE_REF (Vdefault_fontset
, c
);
310 /* Set elements of FONTSET for characters in RANGE to the value ELT.
311 RANGE is a cons (FROM . TO), where FROM and TO are character codes
312 specifying a range. */
314 #define FONTSET_SET(fontset, range, elt) \
315 Fset_char_table_range ((fontset), (range), (elt))
318 /* Modify the elements of FONTSET for characters in RANGE by replacing
319 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
320 and TO are character codes specifying a range. If ADD is nil,
321 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
324 #define FONTSET_ADD(fontset, range, elt, add) \
327 ? (set_fontset_fallback \
328 (fontset, Fmake_vector (make_number (1), (elt)))) \
330 Fset_char_table_range (fontset, range, \
331 Fmake_vector (make_number (1), elt)))) \
332 : fontset_add ((fontset), (range), (elt), (add)))
335 fontset_add (Lisp_Object fontset
, Lisp_Object range
, Lisp_Object elt
, Lisp_Object add
)
338 int idx
= (EQ (add
, Qappend
) ? 0 : 1);
340 args
[1 - idx
] = Fmake_vector (make_number (1), elt
);
344 int from
= XINT (XCAR (range
));
345 int to
= XINT (XCDR (range
));
349 from1
= from
, to1
= to
;
350 args
[idx
] = char_table_ref_and_range (fontset
, from
, &from1
, &to1
);
351 char_table_set_range (fontset
, from
, to1
,
352 NILP (args
[idx
]) ? args
[1 - idx
]
353 : Fvconcat (2, args
));
359 args
[idx
] = FONTSET_FALLBACK (fontset
);
361 (fontset
, NILP (args
[idx
]) ? args
[1 - idx
] : Fvconcat (2, args
));
366 fontset_compare_rfontdef (const void *val1
, const void *val2
)
368 return (RFONT_DEF_SCORE (*(Lisp_Object
*) val1
)
369 - RFONT_DEF_SCORE (*(Lisp_Object
*) val2
));
372 /* Update FONT-GROUP which has this form:
373 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
374 RFONT-DEF0 RFONT-DEF1 ... ]
375 Reorder RFONT-DEFs according to the current language, and update
376 CHARSET-ORDERED-LIST-TICK.
378 If PREFERRED_FAMILY is not nil, that family has the higher priority
379 if the encoding charsets or languages in font-specs are the same. */
382 reorder_font_vector (Lisp_Object font_group
, struct font
*font
)
384 Lisp_Object vec
, font_object
;
387 bool score_changed
= false;
390 XSETFONT (font_object
, font
);
394 vec
= XCDR (font_group
);
396 /* Exclude the tailing nil element from the reordering. */
397 if (NILP (AREF (vec
, size
- 1)))
400 for (i
= 0; i
< size
; i
++)
402 Lisp_Object rfont_def
= AREF (vec
, i
);
403 Lisp_Object font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
404 Lisp_Object font_spec
= FONT_DEF_SPEC (font_def
);
405 int score
= RFONT_DEF_SCORE (rfont_def
) & 0xFF;
406 Lisp_Object otf_spec
= Ffont_get (font_spec
, QCotf
);
408 if (! NILP (otf_spec
))
409 /* A font-spec with :otf is preferable regardless of encoding
412 else if (! font_match_p (font_spec
, font_object
))
414 Lisp_Object encoding
= FONT_DEF_ENCODING (font_def
);
416 if (! NILP (encoding
))
420 for (tail
= Vcharset_ordered_list
;
421 ! EQ (tail
, Vcharset_non_preferred_head
) && CONSP (tail
);
423 if (EQ (encoding
, XCAR (tail
)))
425 else if (score
<= min (INT_MAX
, MOST_POSITIVE_FIXNUM
) - 0x100)
430 Lisp_Object lang
= Ffont_get (font_spec
, QClang
);
433 && ! EQ (lang
, Vcurrent_iso639_language
)
434 && (! CONSP (Vcurrent_iso639_language
)
435 || NILP (Fmemq (lang
, Vcurrent_iso639_language
))))
439 if (RFONT_DEF_SCORE (rfont_def
) != score
)
441 RFONT_DEF_SET_SCORE (rfont_def
, score
);
442 score_changed
= true;
447 qsort (XVECTOR (vec
)->contents
, size
, word_size
,
448 fontset_compare_rfontdef
);
449 EMACS_INT low_tick_bits
= charset_ordered_list_tick
& MOST_POSITIVE_FIXNUM
;
450 XSETCAR (font_group
, make_number (low_tick_bits
));
453 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
454 character C in FONTSET. If C is -1, return a fallback font-group.
455 If C is not -1, the value may be Qt (FONTSET doesn't have a font
456 for C even in the fallback group), or 0 (a font for C may be found
457 only in the fallback group). */
460 fontset_get_font_group (Lisp_Object fontset
, int c
)
462 Lisp_Object font_group
;
463 Lisp_Object base_fontset
;
464 int from
= 0, to
= MAX_CHAR
, i
;
466 eassert (! BASE_FONTSET_P (fontset
));
468 font_group
= CHAR_TABLE_REF (fontset
, c
);
470 font_group
= FONTSET_FALLBACK (fontset
);
471 if (! NILP (font_group
))
473 base_fontset
= FONTSET_BASE (fontset
);
474 if (NILP (base_fontset
))
477 font_group
= char_table_ref_and_range (base_fontset
, c
, &from
, &to
);
479 font_group
= FONTSET_FALLBACK (base_fontset
);
480 if (NILP (font_group
))
482 font_group
= make_number (0);
484 char_table_set_range (fontset
, from
, to
, font_group
);
487 if (!VECTORP (font_group
))
489 font_group
= Fcopy_sequence (font_group
);
490 for (i
= 0; i
< ASIZE (font_group
); i
++)
491 if (! NILP (AREF (font_group
, i
)))
493 Lisp_Object rfont_def
;
495 RFONT_DEF_NEW (rfont_def
, AREF (font_group
, i
));
496 /* Remember the original order. */
497 RFONT_DEF_SET_SCORE (rfont_def
, i
);
498 ASET (font_group
, i
, rfont_def
);
500 font_group
= Fcons (make_number (-1), font_group
);
502 char_table_set_range (fontset
, from
, to
, font_group
);
504 set_fontset_fallback (fontset
, font_group
);
508 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
509 character C. If no font is found, return Qnil if there's a
510 possibility that the default fontset or the fallback font groups
511 have a proper font, and return Qt if not.
513 If a font is found but is not yet opened, open it (if FACE is not
514 NULL) or return Qnil (if FACE is NULL).
516 ID is a charset-id that must be preferred, or -1 meaning no
519 If FALLBACK, search only fallback fonts. */
522 fontset_find_font (Lisp_Object fontset
, int c
, struct face
*face
, int id
,
525 Lisp_Object vec
, font_group
;
526 int i
, charset_matched
= 0, found_index
;
527 struct frame
*f
= (FRAMEP (FONTSET_FRAME (fontset
))
528 ? XFRAME (FONTSET_FRAME (fontset
))
529 : XFRAME (selected_frame
));
530 Lisp_Object rfont_def
;
532 font_group
= fontset_get_font_group (fontset
, fallback
? -1 : c
);
533 if (! CONSP (font_group
))
535 vec
= XCDR (font_group
);
536 if (ASIZE (vec
) == 0)
541 if (XINT (XCAR (font_group
)) != charset_ordered_list_tick
)
542 /* We have just created the font-group,
543 or the charset priorities were changed. */
544 reorder_font_vector (font_group
, face
->ascii_face
->font
);
546 /* Find a spec matching with the charset ID to try at
548 for (i
= 0; i
< ASIZE (vec
); i
++)
550 Lisp_Object repertory
;
552 rfont_def
= AREF (vec
, i
);
553 if (NILP (rfont_def
))
555 repertory
= FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def
));
557 if (XINT (repertory
) == id
)
565 /* Find the first available font in the vector of RFONT-DEF. */
566 for (i
= 0; i
< ASIZE (vec
); i
++)
568 Lisp_Object font_def
;
569 Lisp_Object font_entity
, font_object
;
574 if (charset_matched
> 0)
576 /* Try the element matching with the charset ID at first. */
577 found_index
= charset_matched
;
578 /* Make this negative so that we don't come here in the
580 charset_matched
= - charset_matched
;
581 /* We must try the first element in the next loop. */
585 else if (i
== - charset_matched
)
587 /* We have already tried this element and the followings
588 that have the same font specifications in the first
589 iteration. So, skip them all. */
590 rfont_def
= AREF (vec
, i
);
591 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
592 for (; i
+ 1 < ASIZE (vec
); i
++)
594 rfont_def
= AREF (vec
, i
+ 1);
595 if (NILP (rfont_def
))
597 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def
), font_def
))
603 rfont_def
= AREF (vec
, found_index
);
604 if (NILP (rfont_def
))
608 /* This is a sign of not to try the other fonts. */
611 if (INTEGERP (RFONT_DEF_FACE (rfont_def
))
612 && XINT (RFONT_DEF_FACE (rfont_def
)) < 0)
613 /* We couldn't open this font last time. */
616 font_object
= RFONT_DEF_OBJECT (rfont_def
);
617 if (NILP (font_object
))
619 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
622 /* We have not yet opened the font. */
624 /* Find a font best-matching with the spec without checking
625 the support of the character C. That checking is costly,
626 and even without the checking, the found font supports C
627 in high possibility. */
628 font_entity
= font_find_for_lface (f
, face
->lface
,
629 FONT_DEF_SPEC (font_def
), -1);
630 if (NILP (font_entity
))
632 /* Record that no font matches the spec. */
633 RFONT_DEF_SET_FACE (rfont_def
, -1);
636 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
637 FONT_DEF_SPEC (font_def
));
638 if (NILP (font_object
))
640 /* Something strange happened, perhaps because of a
641 Font-backend problem. Too avoid crashing, record
642 that this spec is unusable. It may be better to find
643 another font of the same spec, but currently we don't
645 RFONT_DEF_SET_FACE (rfont_def
, -1);
648 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
651 if (font_has_char (f
, font_object
, c
))
654 /* Find a font already opened, matching with the current spec,
656 font_def
= RFONT_DEF_FONT_DEF (rfont_def
);
657 for (; found_index
+ 1 < ASIZE (vec
); found_index
++)
659 rfont_def
= AREF (vec
, found_index
+ 1);
660 if (NILP (rfont_def
))
662 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def
), font_def
))
664 font_object
= RFONT_DEF_OBJECT (rfont_def
);
665 if (! NILP (font_object
) && font_has_char (f
, font_object
, c
))
672 /* Find a font-entity with the current spec and supporting C. */
673 font_entity
= font_find_for_lface (f
, face
->lface
,
674 FONT_DEF_SPEC (font_def
), c
);
675 if (! NILP (font_entity
))
677 /* We found a font. Open it and insert a new element for
682 font_object
= font_open_for_lface (f
, font_entity
, face
->lface
,
684 if (NILP (font_object
))
686 RFONT_DEF_NEW (rfont_def
, font_def
);
687 RFONT_DEF_SET_OBJECT (rfont_def
, font_object
);
688 RFONT_DEF_SET_SCORE (rfont_def
, RFONT_DEF_SCORE (rfont_def
));
689 new_vec
= Fmake_vector (make_number (ASIZE (vec
) + 1), Qnil
);
691 for (j
= 0; j
< found_index
; j
++)
692 ASET (new_vec
, j
, AREF (vec
, j
));
693 ASET (new_vec
, j
, rfont_def
);
694 for (j
++; j
< ASIZE (new_vec
); j
++)
695 ASET (new_vec
, j
, AREF (vec
, j
- 1));
696 XSETCDR (font_group
, new_vec
);
704 FONTSET_SET (fontset
, make_number (c
), make_number (0));
708 if (fallback
&& found_index
> 0)
710 /* The order of fonts in the fallback font-group is not that
711 important, and it is better to move the found font to the
712 first of the group so that the next try will find it
714 for (i
= found_index
; i
> 0; i
--)
715 ASET (vec
, i
, AREF (vec
, i
- 1));
716 ASET (vec
, 0, rfont_def
);
723 fontset_font (Lisp_Object fontset
, int c
, struct face
*face
, int id
)
725 Lisp_Object rfont_def
, default_rfont_def
IF_LINT (= Qnil
);
726 Lisp_Object base_fontset
;
728 /* Try a font-group of FONTSET. */
729 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c
), Qnil
);
730 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 0);
731 if (VECTORP (rfont_def
))
733 if (NILP (rfont_def
))
734 FONTSET_SET (fontset
, make_number (c
), make_number (0));
736 /* Try a font-group of the default fontset. */
737 base_fontset
= FONTSET_BASE (fontset
);
738 if (! EQ (base_fontset
, Vdefault_fontset
))
740 if (NILP (FONTSET_DEFAULT (fontset
)))
743 make_fontset (FONTSET_FRAME (fontset
), Qnil
, Vdefault_fontset
));
744 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c
), Qnil
);
746 = fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 0);
747 if (VECTORP (default_rfont_def
))
748 return default_rfont_def
;
749 if (NILP (default_rfont_def
))
750 FONTSET_SET (FONTSET_DEFAULT (fontset
), make_number (c
),
754 /* Try a fallback font-group of FONTSET. */
755 if (! EQ (rfont_def
, Qt
))
757 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c
), Qnil
);
758 rfont_def
= fontset_find_font (fontset
, c
, face
, id
, 1);
759 if (VECTORP (rfont_def
))
761 /* Remember that FONTSET has no font for C. */
762 FONTSET_SET (fontset
, make_number (c
), Qt
);
765 /* Try a fallback font-group of the default fontset. */
766 if (! EQ (base_fontset
, Vdefault_fontset
)
767 && ! EQ (default_rfont_def
, Qt
))
769 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c
), Qnil
);
770 rfont_def
= fontset_find_font (FONTSET_DEFAULT (fontset
), c
, face
, id
, 1);
771 if (VECTORP (rfont_def
))
773 /* Remember that the default fontset has no font for C. */
774 FONTSET_SET (FONTSET_DEFAULT (fontset
), make_number (c
), Qt
);
780 /* Return a newly created fontset with NAME. If BASE is nil, make a
781 base fontset. Otherwise make a realized fontset whose base is
785 make_fontset (Lisp_Object frame
, Lisp_Object name
, Lisp_Object base
)
788 int size
= ASIZE (Vfontset_table
);
789 int id
= next_fontset_id
;
791 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
792 the next available fontset ID. So it is expected that this loop
793 terminates quickly. In addition, as the last element of
794 Vfontset_table is always nil, we don't have to check the range of
796 while (!NILP (AREF (Vfontset_table
, id
))) id
++;
799 Vfontset_table
= larger_vector (Vfontset_table
, 1, -1);
801 fontset
= Fmake_char_table (Qfontset
, Qnil
);
803 set_fontset_id (fontset
, make_number (id
));
805 set_fontset_name (fontset
, name
);
808 set_fontset_name (fontset
, Qnil
);
809 set_fontset_frame (fontset
, frame
);
810 set_fontset_base (fontset
, base
);
813 ASET (Vfontset_table
, id
, fontset
);
814 next_fontset_id
= id
+ 1;
819 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
821 /* Return the name of the fontset who has ID. */
824 fontset_name (int id
)
828 fontset
= FONTSET_FROM_ID (id
);
829 return FONTSET_NAME (fontset
);
833 /* Return the ASCII font name of the fontset who has ID. */
836 fontset_ascii (int id
)
838 Lisp_Object fontset
, elt
;
840 fontset
= FONTSET_FROM_ID (id
);
841 elt
= FONTSET_ASCII (fontset
);
847 /* Free fontset of FACE defined on frame F. Called from
848 free_realized_face. */
851 free_face_fontset (struct frame
*f
, struct face
*face
)
855 fontset
= FONTSET_FROM_ID (face
->fontset
);
858 eassert (! BASE_FONTSET_P (fontset
));
859 eassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
860 ASET (Vfontset_table
, face
->fontset
, Qnil
);
861 if (face
->fontset
< next_fontset_id
)
862 next_fontset_id
= face
->fontset
;
863 if (! NILP (FONTSET_DEFAULT (fontset
)))
865 int id
= XINT (FONTSET_ID (FONTSET_DEFAULT (fontset
)));
867 fontset
= AREF (Vfontset_table
, id
);
868 eassert (!NILP (fontset
) && ! BASE_FONTSET_P (fontset
));
869 eassert (f
== XFRAME (FONTSET_FRAME (fontset
)));
870 ASET (Vfontset_table
, id
, Qnil
);
871 if (id
< next_fontset_id
)
872 next_fontset_id
= face
->fontset
;
877 /* Return ID of face suitable for displaying character C at buffer position
878 POS on frame F. FACE must be realized for ASCII characters in advance.
879 Called from the macro FACE_FOR_CHAR. */
882 face_for_char (struct frame
*f
, struct face
*face
, int c
,
883 ptrdiff_t pos
, Lisp_Object object
)
885 Lisp_Object fontset
, rfont_def
, charset
;
889 eassert (fontset_id_valid_p (face
->fontset
));
891 if (ASCII_CHAR_P (c
) || CHAR_BYTE8_P (c
))
892 return face
->ascii_face
->id
;
897 /* Fonts often have characters in other scripts, like symbol, even if they
898 don't match script: symbol. So check if the character is present
899 in the current face first. Only enable for NS for now, but should
900 perhaps be general? */
901 Lisp_Object font_object
;
902 XSETFONT (font_object
, face
->font
);
903 if (font_has_char (f
, font_object
, c
)) return face
->id
;
907 fontset
= FONTSET_FROM_ID (face
->fontset
);
908 eassert (!BASE_FONTSET_P (fontset
));
917 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
918 if (CHARSETP (charset
))
922 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
923 if (CONSP (val
) && CHARSETP (XCDR (val
)))
924 charset
= XCDR (val
);
925 id
= XINT (CHARSET_SYMBOL_ID (charset
));
931 rfont_def
= fontset_font (fontset
, c
, face
, id
);
932 if (VECTORP (rfont_def
))
934 if (INTEGERP (RFONT_DEF_FACE (rfont_def
)))
935 face_id
= XINT (RFONT_DEF_FACE (rfont_def
));
938 Lisp_Object font_object
;
940 font_object
= RFONT_DEF_OBJECT (rfont_def
);
941 face_id
= face_for_font (f
, font_object
, face
);
942 RFONT_DEF_SET_FACE (rfont_def
, face_id
);
947 if (INTEGERP (FONTSET_NOFONT_FACE (fontset
)))
948 face_id
= XINT (FONTSET_NOFONT_FACE (fontset
));
951 face_id
= face_for_font (f
, Qnil
, face
);
952 set_fontset_nofont_face (fontset
, make_number (face_id
));
955 eassert (face_id
>= 0);
961 font_for_char (struct face
*face
, int c
, ptrdiff_t pos
, Lisp_Object object
)
963 Lisp_Object fontset
, rfont_def
, charset
;
966 if (ASCII_CHAR_P (c
))
968 Lisp_Object font_object
;
970 XSETFONT (font_object
, face
->ascii_face
->font
);
974 eassert (fontset_id_valid_p (face
->fontset
));
975 fontset
= FONTSET_FROM_ID (face
->fontset
);
976 eassert (!BASE_FONTSET_P (fontset
));
984 charset
= Fget_char_property (make_number (pos
), Qcharset
, object
);
985 if (CHARSETP (charset
))
989 val
= assq_no_quit (charset
, Vfont_encoding_charset_alist
);
990 if (CONSP (val
) && CHARSETP (XCDR (val
)))
991 charset
= XCDR (val
);
992 id
= XINT (CHARSET_SYMBOL_ID (charset
));
998 rfont_def
= fontset_font (fontset
, c
, face
, id
);
999 return (VECTORP (rfont_def
)
1000 ? RFONT_DEF_OBJECT (rfont_def
)
1005 /* Make a realized fontset for ASCII face FACE on frame F from the
1006 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1007 default fontset as the base. Value is the id of the new fontset.
1008 Called from realize_x_face. */
1011 make_fontset_for_ascii_face (struct frame
*f
, int base_fontset_id
, struct face
*face
)
1013 Lisp_Object base_fontset
, fontset
, frame
;
1015 XSETFRAME (frame
, f
);
1016 if (base_fontset_id
>= 0)
1018 base_fontset
= FONTSET_FROM_ID (base_fontset_id
);
1019 if (!BASE_FONTSET_P (base_fontset
))
1020 base_fontset
= FONTSET_BASE (base_fontset
);
1021 eassert (BASE_FONTSET_P (base_fontset
));
1024 base_fontset
= Vdefault_fontset
;
1026 fontset
= make_fontset (frame
, Qnil
, base_fontset
);
1027 return XINT (FONTSET_ID (fontset
));
1032 /* Cache data used by fontset_pattern_regexp. The car part is a
1033 pattern string containing at least one wild card, the cdr part is
1034 the corresponding regular expression. */
1035 static Lisp_Object Vcached_fontset_data
;
1037 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1038 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1040 /* If fontset name PATTERN contains any wild card, return regular
1041 expression corresponding to PATTERN. */
1044 fontset_pattern_regexp (Lisp_Object pattern
)
1046 if (!strchr (SSDATA (pattern
), '*')
1047 && !strchr (SSDATA (pattern
), '?'))
1048 /* PATTERN does not contain any wild cards. */
1051 if (!CONSP (Vcached_fontset_data
)
1052 || strcmp (SSDATA (pattern
), CACHED_FONTSET_NAME
))
1054 /* We must at first update the cached data. */
1055 unsigned char *regex
, *p0
, *p1
;
1056 int ndashes
= 0, nstars
= 0, nescs
= 0;
1058 for (p0
= SDATA (pattern
); *p0
; p0
++)
1062 else if (*p0
== '*')
1065 || *p0
== '.' || *p0
== '\\'
1066 || *p0
== '+' || *p0
== '^'
1071 /* If PATTERN is not full XLFD we convert "*" to ".*". Otherwise
1072 we convert "*" to "[^-]*" which is much faster in regular
1073 expression matching. */
1074 ptrdiff_t regexsize
= (SBYTES (pattern
)
1075 + (ndashes
< 14 ? 2 : 5) * nstars
1078 p1
= regex
= SAFE_ALLOCA (regexsize
);
1081 for (p0
= SDATA (pattern
); *p0
; p0
++)
1088 *p1
++ = '[', *p1
++ = '^', *p1
++ = '-', *p1
++ = ']';
1091 else if (*p0
== '?')
1094 || *p0
== '.' || *p0
== '\\'
1095 || *p0
== '+' || *p0
== '^'
1097 *p1
++ = '\\', *p1
++ = *p0
;
1104 Vcached_fontset_data
= Fcons (build_string (SSDATA (pattern
)),
1105 build_string ((char *) regex
));
1109 return CACHED_FONTSET_REGEX
;
1112 /* Return ID of the base fontset named NAME. If there's no such
1113 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1114 0: pattern containing '*' and '?' as wildcards
1115 1: regular expression
1116 2: literal fontset name
1120 fs_query_fontset (Lisp_Object name
, int name_pattern
)
1125 name
= Fdowncase (name
);
1126 if (name_pattern
!= 1)
1128 tem
= Frassoc (name
, Vfontset_alias_alist
);
1130 tem
= Fassoc (name
, Vfontset_alias_alist
);
1131 if (CONSP (tem
) && STRINGP (XCAR (tem
)))
1133 else if (name_pattern
== 0)
1135 tem
= fontset_pattern_regexp (name
);
1144 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
1146 Lisp_Object fontset
, this_name
;
1148 fontset
= FONTSET_FROM_ID (i
);
1150 || !BASE_FONTSET_P (fontset
))
1153 this_name
= FONTSET_NAME (fontset
);
1154 if (name_pattern
== 1
1155 ? fast_string_match_ignore_case (name
, this_name
) >= 0
1156 : !xstrcasecmp (SSDATA (name
), SSDATA (this_name
)))
1163 DEFUN ("query-fontset", Fquery_fontset
, Squery_fontset
, 1, 2, 0,
1164 doc
: /* Return the name of a fontset that matches PATTERN.
1165 The value is nil if there is no matching fontset.
1166 PATTERN can contain `*' or `?' as a wildcard
1167 just as X font name matching algorithm allows.
1168 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1169 (Lisp_Object pattern
, Lisp_Object regexpp
)
1171 Lisp_Object fontset
;
1174 check_window_system (NULL
);
1176 CHECK_STRING (pattern
);
1178 if (SCHARS (pattern
) == 0)
1181 id
= fs_query_fontset (pattern
, !NILP (regexpp
));
1185 fontset
= FONTSET_FROM_ID (id
);
1186 return FONTSET_NAME (fontset
);
1189 /* Return a list of base fontset names matching PATTERN on frame F. */
1192 list_fontsets (struct frame
*f
, Lisp_Object pattern
, int size
)
1194 Lisp_Object frame
, regexp
, val
;
1197 XSETFRAME (frame
, f
);
1199 regexp
= fontset_pattern_regexp (pattern
);
1202 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1204 Lisp_Object fontset
, name
;
1206 fontset
= FONTSET_FROM_ID (id
);
1208 || !BASE_FONTSET_P (fontset
)
1209 || !EQ (frame
, FONTSET_FRAME (fontset
)))
1211 name
= FONTSET_NAME (fontset
);
1213 if (STRINGP (regexp
)
1214 ? (fast_string_match (regexp
, name
) < 0)
1215 : strcmp (SSDATA (pattern
), SSDATA (name
)))
1218 val
= Fcons (Fcopy_sequence (FONTSET_NAME (fontset
)), val
);
1225 /* Free all realized fontsets whose base fontset is BASE. */
1228 free_realized_fontsets (Lisp_Object base
)
1233 /* For the moment, this doesn't work because free_realized_face
1234 doesn't remove FACE from a cache. Until we find a solution, we
1235 suppress this code, and simply use Fclear_face_cache even though
1236 that is not efficient. */
1238 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1240 Lisp_Object
this = AREF (Vfontset_table
, id
);
1242 if (EQ (FONTSET_BASE (this), base
))
1246 for (tail
= FONTSET_FACE_ALIST (this); CONSP (tail
);
1249 struct frame
*f
= XFRAME (FONTSET_FRAME (this));
1250 int face_id
= XINT (XCDR (XCAR (tail
)));
1251 struct face
*face
= FACE_FROM_ID (f
, face_id
);
1253 /* Face THIS itself is also freed by the following call. */
1254 free_realized_face (f
, face
);
1260 /* But, we don't have to call Fclear_face_cache if no fontset has
1261 been realized from BASE. */
1262 for (id
= 0; id
< ASIZE (Vfontset_table
); id
++)
1264 Lisp_Object
this = AREF (Vfontset_table
, id
);
1266 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base
))
1268 Fclear_face_cache (Qt
);
1276 /* Check validity of NAME as a fontset name and return the
1277 corresponding fontset. If not valid, signal an error.
1279 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1282 Set *FRAME to the actual frame. */
1285 check_fontset_name (Lisp_Object name
, Lisp_Object
*frame
)
1288 struct frame
*f
= decode_live_frame (*frame
);
1290 XSETFRAME (*frame
, f
);
1293 return Vdefault_fontset
;
1295 id
= FRAME_FONTSET (f
);
1298 CHECK_STRING (name
);
1299 /* First try NAME as literal. */
1300 id
= fs_query_fontset (name
, 2);
1302 /* For backward compatibility, try again NAME as pattern. */
1303 id
= fs_query_fontset (name
, 0);
1305 error ("Fontset `%s' does not exist", SDATA (name
));
1307 return FONTSET_FROM_ID (id
);
1311 accumulate_script_ranges (Lisp_Object arg
, Lisp_Object range
, Lisp_Object val
)
1313 if (EQ (XCAR (arg
), val
))
1316 XSETCDR (arg
, Fcons (Fcons (XCAR (range
), XCDR (range
)), XCDR (arg
)));
1318 XSETCDR (arg
, Fcons (Fcons (range
, range
), XCDR (arg
)));
1323 /* Callback function for map_charset_chars in Fset_fontset_font.
1324 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1326 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1327 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1328 The consumed ranges are popped up from SCRIPT_RANGE_LIST, and the
1329 new SCRIPT_RANGE_LIST is stored in ARG.
1331 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1332 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1336 set_fontset_font (Lisp_Object arg
, Lisp_Object range
)
1338 Lisp_Object fontset
, font_def
, add
, ascii
, script_range_list
;
1339 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
1341 fontset
= AREF (arg
, 0);
1342 font_def
= AREF (arg
, 1);
1343 add
= AREF (arg
, 2);
1344 ascii
= AREF (arg
, 3);
1345 script_range_list
= AREF (arg
, 4);
1347 if (NILP (ascii
) && from
< 0x80)
1352 range
= Fcons (make_number (0x80), XCDR (range
));
1355 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1356 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1357 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1359 for (; CONSP (script_range_list
) && SCRIPT_TO
< from
; POP_SCRIPT_RANGE ())
1360 FONTSET_ADD (fontset
, XCAR (script_range_list
), font_def
, add
);
1361 if (CONSP (script_range_list
))
1363 if (SCRIPT_FROM
< from
)
1364 range
= Fcons (make_number (SCRIPT_FROM
), XCDR (range
));
1365 while (CONSP (script_range_list
) && SCRIPT_TO
<= to
)
1366 POP_SCRIPT_RANGE ();
1367 if (CONSP (script_range_list
) && SCRIPT_FROM
<= to
)
1368 XSETCAR (XCAR (script_range_list
), make_number (to
+ 1));
1371 FONTSET_ADD (fontset
, range
, font_def
, add
);
1372 ASET (arg
, 4, script_range_list
);
1375 static void update_auto_fontset_alist (Lisp_Object
, Lisp_Object
);
1378 DEFUN ("set-fontset-font", Fset_fontset_font
, Sset_fontset_font
, 3, 5, 0,
1380 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1382 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1383 the default fontset.
1385 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1386 In that case, use FONT-SPEC for all characters in the range FROM and
1389 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1390 all characters that belong to the script.
1392 TARGET may be a charset. In that case, use FONT-SPEC for all
1393 characters in the charset.
1395 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1396 that no FONT-SPEC is specified.
1398 FONT-SPEC may one of these:
1399 * A font-spec object made by the function `font-spec' (which see).
1400 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1401 REGISTRY is a font registry name. FAMILY may contain foundry
1402 name, and REGISTRY may contain encoding name.
1403 * A font name string.
1404 * nil, which explicitly specifies that there's no font for TARGET.
1406 Optional 4th argument FRAME is a frame or nil for the selected frame
1407 that is concerned in the case that NAME is nil.
1409 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1410 to the font specifications for TARGET previously set. If it is
1411 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1412 appended. By default, FONT-SPEC overrides the previous settings. */)
1413 (Lisp_Object name
, Lisp_Object target
, Lisp_Object font_spec
, Lisp_Object frame
, Lisp_Object add
)
1415 Lisp_Object fontset
;
1416 Lisp_Object font_def
, registry
, family
;
1417 Lisp_Object range_list
;
1418 struct charset
*charset
= NULL
;
1419 Lisp_Object fontname
;
1420 bool ascii_changed
= 0;
1422 fontset
= check_fontset_name (name
, &frame
);
1425 if (CONSP (font_spec
))
1427 Lisp_Object spec
= Ffont_spec (0, NULL
);
1429 font_parse_family_registry (XCAR (font_spec
), XCDR (font_spec
), spec
);
1431 fontname
= Ffont_xlfd_name (font_spec
, Qnil
);
1433 else if (STRINGP (font_spec
))
1435 Lisp_Object args
[2];
1437 fontname
= font_spec
;
1439 args
[1] = font_spec
;
1440 font_spec
= Ffont_spec (2, args
);
1442 else if (FONT_SPEC_P (font_spec
))
1443 fontname
= Ffont_xlfd_name (font_spec
, Qnil
);
1444 else if (! NILP (font_spec
))
1445 Fsignal (Qfont
, list2 (build_string ("Invalid font-spec"), font_spec
));
1447 if (! NILP (font_spec
))
1449 Lisp_Object encoding
, repertory
;
1451 family
= AREF (font_spec
, FONT_FAMILY_INDEX
);
1452 if (! NILP (family
) )
1453 family
= SYMBOL_NAME (family
);
1454 registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1455 if (! NILP (registry
))
1456 registry
= Fdowncase (SYMBOL_NAME (registry
));
1457 AUTO_STRING (dash
, "-");
1458 encoding
= find_font_encoding (concat3 (family
, dash
, registry
));
1459 if (NILP (encoding
))
1462 if (SYMBOLP (encoding
))
1464 CHECK_CHARSET (encoding
);
1465 encoding
= repertory
= CHARSET_SYMBOL_ID (encoding
);
1469 repertory
= XCDR (encoding
);
1470 encoding
= XCAR (encoding
);
1471 CHECK_CHARSET (encoding
);
1472 encoding
= CHARSET_SYMBOL_ID (encoding
);
1473 if (! NILP (repertory
) && SYMBOLP (repertory
))
1475 CHECK_CHARSET (repertory
);
1476 repertory
= CHARSET_SYMBOL_ID (repertory
);
1479 FONT_DEF_NEW (font_def
, font_spec
, encoding
, repertory
);
1484 if (CHARACTERP (target
))
1486 if (XFASTINT (target
) < 0x80)
1487 error ("Can't set a font for partial ASCII range");
1488 range_list
= list1 (Fcons (target
, target
));
1490 else if (CONSP (target
))
1492 Lisp_Object from
, to
;
1494 from
= Fcar (target
);
1496 CHECK_CHARACTER (from
);
1497 CHECK_CHARACTER (to
);
1498 if (XFASTINT (from
) < 0x80)
1500 if (XFASTINT (from
) != 0 || XFASTINT (to
) < 0x7F)
1501 error ("Can't set a font for partial ASCII range");
1504 range_list
= list1 (target
);
1506 else if (SYMBOLP (target
) && !NILP (target
))
1508 Lisp_Object script_list
;
1512 script_list
= XCHAR_TABLE (Vchar_script_table
)->extras
[0];
1513 if (! NILP (Fmemq (target
, script_list
)))
1515 if (EQ (target
, Qlatin
))
1517 val
= list1 (target
);
1518 map_char_table (accumulate_script_ranges
, Qnil
, Vchar_script_table
,
1520 range_list
= Fnreverse (XCDR (val
));
1522 if (CHARSETP (target
))
1524 CHECK_CHARSET_GET_CHARSET (target
, charset
);
1525 if (charset
->ascii_compatible_p
)
1528 else if (NILP (range_list
))
1529 error ("Invalid script or charset name: %s",
1530 SDATA (SYMBOL_NAME (target
)));
1532 else if (NILP (target
))
1533 range_list
= list1 (Qnil
);
1535 error ("Invalid target for setting a font");
1541 if (NILP (font_spec
))
1542 error ("Can't set ASCII font to nil");
1543 val
= CHAR_TABLE_REF (fontset
, 0);
1544 if (! NILP (val
) && EQ (add
, Qappend
))
1545 /* We are going to change just an additional font for ASCII. */
1553 arg
= make_uninit_vector (5);
1554 ASET (arg
, 0, fontset
);
1555 ASET (arg
, 1, font_def
);
1557 ASET (arg
, 3, ascii_changed
? Qt
: Qnil
);
1558 ASET (arg
, 4, range_list
);
1560 map_charset_chars (set_fontset_font
, Qnil
, arg
, charset
,
1561 CHARSET_MIN_CODE (charset
),
1562 CHARSET_MAX_CODE (charset
));
1563 range_list
= AREF (arg
, 4);
1565 for (; CONSP (range_list
); range_list
= XCDR (range_list
))
1566 FONTSET_ADD (fontset
, XCAR (range_list
), font_def
, add
);
1570 Lisp_Object tail
, fr
;
1571 int fontset_id
= XINT (FONTSET_ID (fontset
));
1573 set_fontset_ascii (fontset
, fontname
);
1574 name
= FONTSET_NAME (fontset
);
1575 FOR_EACH_FRAME (tail
, fr
)
1577 struct frame
*f
= XFRAME (fr
);
1578 Lisp_Object font_object
;
1581 if (FRAME_INITIAL_P (f
) || FRAME_TERMCAP_P (f
))
1583 if (fontset_id
!= FRAME_FONTSET (f
))
1585 face
= FACE_FROM_ID (f
, DEFAULT_FACE_ID
);
1587 font_object
= font_load_for_lface (f
, face
->lface
, font_spec
);
1589 font_object
= font_open_by_spec (f
, font_spec
);
1590 if (! NILP (font_object
))
1592 update_auto_fontset_alist (font_object
, fontset
);
1593 AUTO_FRAME_ARG (arg
, Qfont
, Fcons (name
, font_object
));
1594 Fmodify_frame_parameters (fr
, arg
);
1599 /* Free all realized fontsets whose base is FONTSET. This way, the
1600 specified character(s) are surely redisplayed by a correct
1602 free_realized_fontsets (fontset
);
1608 DEFUN ("new-fontset", Fnew_fontset
, Snew_fontset
, 2, 2, 0,
1609 doc
: /* Create a new fontset NAME from font information in FONTLIST.
1611 FONTLIST is an alist of scripts vs the corresponding font specification list.
1612 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1613 character of SCRIPT is displayed by a font that matches one of
1616 SCRIPT is a symbol that appears in the first extra slot of the
1617 char-table `char-script-table'.
1619 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1620 `set-fontset-font' for the meaning. */)
1621 (Lisp_Object name
, Lisp_Object fontlist
)
1623 Lisp_Object fontset
;
1626 CHECK_STRING (name
);
1627 CHECK_LIST (fontlist
);
1629 name
= Fdowncase (name
);
1630 id
= fs_query_fontset (name
, 0);
1633 Lisp_Object font_spec
= Ffont_spec (0, NULL
);
1634 Lisp_Object short_name
;
1638 if (font_parse_xlfd (SSDATA (name
), SBYTES (name
), font_spec
) < 0)
1639 error ("Fontset name must be in XLFD format");
1640 short_name
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1641 if (strncmp (SSDATA (SYMBOL_NAME (short_name
)), "fontset-", 8)
1642 || SBYTES (SYMBOL_NAME (short_name
)) < 9)
1643 error ("Registry field of fontset name must be \"fontset-*\"");
1644 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (short_name
)),
1645 Vfontset_alias_alist
);
1646 ASET (font_spec
, FONT_REGISTRY_INDEX
, Qiso8859_1
);
1647 fontset
= make_fontset (Qnil
, name
, Qnil
);
1648 len
= font_unparse_xlfd (font_spec
, 0, xlfd
, 256);
1650 error ("Invalid fontset name (perhaps too long): %s", SDATA (name
));
1651 set_fontset_ascii (fontset
, make_unibyte_string (xlfd
, len
));
1655 fontset
= FONTSET_FROM_ID (id
);
1656 free_realized_fontsets (fontset
);
1657 Fset_char_table_range (fontset
, Qt
, Qnil
);
1660 for (; CONSP (fontlist
); fontlist
= XCDR (fontlist
))
1662 Lisp_Object elt
, script
;
1664 elt
= XCAR (fontlist
);
1665 script
= Fcar (elt
);
1667 if (CONSP (elt
) && (NILP (XCDR (elt
)) || CONSP (XCDR (elt
))))
1668 for (; CONSP (elt
); elt
= XCDR (elt
))
1669 Fset_fontset_font (name
, script
, XCAR (elt
), Qnil
, Qappend
);
1671 Fset_fontset_font (name
, script
, elt
, Qnil
, Qappend
);
1677 /* Alist of automatically created fontsets. Each element is a cons
1678 (FONT-SPEC . FONTSET-ID). */
1679 static Lisp_Object auto_fontset_alist
;
1681 /* Number of automatically created fontsets. */
1682 static ptrdiff_t num_auto_fontsets
;
1684 /* Return a fontset synthesized from FONT-OBJECT. This is called from
1685 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1686 frame, and the returned fontset is used for the default fontset of
1687 that frame. The fontset specifies a font of the same registry as
1688 FONT-OBJECT for all characters in the repertory of the registry
1689 (see Vfont_encoding_alist). If the repertory is not known, the
1690 fontset specifies the font for all Latin characters assuming that a
1691 user intends to use FONT-OBJECT for Latin characters. */
1694 fontset_from_font (Lisp_Object font_object
)
1696 Lisp_Object font_name
= font_get_name (font_object
);
1697 Lisp_Object font_spec
= copy_font_spec (font_object
);
1698 Lisp_Object registry
= AREF (font_spec
, FONT_REGISTRY_INDEX
);
1699 Lisp_Object fontset_spec
, alias
, name
, fontset
;
1702 val
= assoc_no_quit (font_spec
, auto_fontset_alist
);
1704 return XINT (FONTSET_ID (XCDR (val
)));
1705 if (num_auto_fontsets
++ == 0)
1706 alias
= intern ("fontset-startup");
1709 char temp
[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1711 sprintf (temp
, "fontset-auto%"pD
"d", num_auto_fontsets
- 1);
1712 alias
= intern (temp
);
1714 fontset_spec
= copy_font_spec (font_spec
);
1715 ASET (fontset_spec
, FONT_REGISTRY_INDEX
, alias
);
1716 name
= Ffont_xlfd_name (fontset_spec
, Qnil
);
1717 eassert (!NILP (name
));
1718 fontset
= make_fontset (Qnil
, name
, Qnil
);
1719 Vfontset_alias_alist
= Fcons (Fcons (name
, SYMBOL_NAME (alias
)),
1720 Vfontset_alias_alist
);
1721 alias
= Fdowncase (AREF (font_object
, FONT_NAME_INDEX
));
1722 Vfontset_alias_alist
= Fcons (Fcons (name
, alias
), Vfontset_alias_alist
);
1723 auto_fontset_alist
= Fcons (Fcons (font_spec
, fontset
), auto_fontset_alist
);
1724 font_spec
= Ffont_spec (0, NULL
);
1725 ASET (font_spec
, FONT_REGISTRY_INDEX
, registry
);
1727 Lisp_Object target
= find_font_encoding (SYMBOL_NAME (registry
));
1730 target
= XCDR (target
);
1731 if (! CHARSETP (target
))
1733 Fset_fontset_font (name
, target
, font_spec
, Qnil
, Qnil
);
1734 Fset_fontset_font (name
, Qnil
, font_spec
, Qnil
, Qnil
);
1737 set_fontset_ascii (fontset
, font_name
);
1739 return XINT (FONTSET_ID (fontset
));
1743 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1744 FONTSET is changed, we delete an entry of FONTSET if any from
1745 auto_fontset_alist so that FONTSET is not re-used by
1746 fontset_from_font. */
1749 update_auto_fontset_alist (Lisp_Object font_object
, Lisp_Object fontset
)
1751 Lisp_Object prev
, tail
;
1753 for (prev
= Qnil
, tail
= auto_fontset_alist
; CONSP (tail
);
1754 prev
= tail
, tail
= XCDR (tail
))
1755 if (EQ (fontset
, XCDR (XCAR (tail
))))
1758 auto_fontset_alist
= XCDR (tail
);
1760 XSETCDR (prev
, XCDR (tail
));
1766 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1767 FONT-OBJECT is the font for the character at POSITION in the current
1768 buffer. This is computed from all the text properties and overlays
1769 that apply to POSITION. POSITION may be nil, in which case,
1770 FONT-SPEC is the font for displaying the character CH with the
1773 GLYPH-CODE is the glyph code in the font to use for the character.
1775 If the 2nd optional arg CH is non-nil, it is a character to check
1776 the font instead of the character at POSITION.
1778 It returns nil in the following cases:
1780 (1) The window system doesn't have a font for the character (thus
1781 it is displayed by an empty box).
1783 (2) The character code is invalid.
1785 (3) If POSITION is not nil, and the current buffer is not displayed
1788 In addition, the returned font name may not take into account of
1789 such redisplay engine hooks as what used in jit-lock-mode if
1790 POSITION is currently not visible. */
1793 DEFUN ("internal-char-font", Finternal_char_font
, Sinternal_char_font
, 1, 2, 0,
1794 doc
: /* For internal use only. */)
1795 (Lisp_Object position
, Lisp_Object ch
)
1797 ptrdiff_t pos
, pos_byte
, dummy
;
1803 if (NILP (position
))
1805 CHECK_CHARACTER (ch
);
1807 f
= XFRAME (selected_frame
);
1808 face_id
= lookup_basic_face (f
, DEFAULT_FACE_ID
);
1816 CHECK_NUMBER_COERCE_MARKER (position
);
1817 if (! (BEGV
<= XINT (position
) && XINT (position
) < ZV
))
1818 args_out_of_range_3 (position
, make_number (BEGV
), make_number (ZV
));
1819 pos
= XINT (position
);
1820 pos_byte
= CHAR_TO_BYTE (pos
);
1822 c
= FETCH_CHAR (pos_byte
);
1828 window
= Fget_buffer_window (Fcurrent_buffer (), Qnil
);
1831 w
= XWINDOW (window
);
1832 f
= XFRAME (w
->frame
);
1833 face_id
= face_at_buffer_position (w
, pos
, &dummy
,
1836 if (! CHAR_VALID_P (c
))
1838 if (!FRAME_WINDOW_P (f
))
1840 /* We need the basic faces to be valid below, so recompute them if
1841 some code just happened to clear the face cache. */
1842 if (FRAME_FACE_CACHE (f
)->used
== 0)
1843 recompute_basic_faces (f
);
1844 face_id
= FACE_FOR_CHAR (f
, FACE_FROM_ID (f
, face_id
), c
, pos
, Qnil
);
1845 face
= FACE_FROM_ID (f
, face_id
);
1848 unsigned code
= face
->font
->driver
->encode_char (face
->font
, c
);
1849 Lisp_Object font_object
;
1851 if (code
== FONT_INVALID_CODE
)
1853 XSETFONT (font_object
, face
->font
);
1854 return Fcons (font_object
, INTEGER_TO_CONS (code
));
1860 DEFUN ("fontset-info", Ffontset_info
, Sfontset_info
, 1, 2, 0,
1861 doc
: /* Return information about a fontset FONTSET on frame FRAME.
1863 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1864 for the default fontset. FRAME nil means the selected frame.
1866 The value is a char-table whose elements have this form:
1868 ((FONT OPENED-FONT ...) ...)
1870 FONT is a name of font specified for a range of characters.
1872 OPENED-FONT is a name of a font actually opened.
1874 The char-table has one extra slot. If FONTSET is not the default
1875 fontset, the value the extra slot is a char-table containing the
1876 information about the derived fonts from the default fontset. The
1877 format is the same as above. */)
1878 (Lisp_Object fontset
, Lisp_Object frame
)
1880 Lisp_Object
*realized
[2], fontsets
[2], tables
[2];
1881 Lisp_Object val
, elt
;
1884 check_window_system (NULL
);
1885 fontset
= check_fontset_name (fontset
, &frame
);
1887 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1888 in the table `realized'. */
1890 SAFE_ALLOCA_LISP (realized
[0], 2 * ASIZE (Vfontset_table
));
1891 realized
[1] = realized
[0] + ASIZE (Vfontset_table
);
1892 for (i
= j
= 0; i
< ASIZE (Vfontset_table
); i
++)
1894 elt
= FONTSET_FROM_ID (i
);
1896 && EQ (FONTSET_BASE (elt
), fontset
)
1897 && EQ (FONTSET_FRAME (elt
), frame
))
1898 realized
[0][j
++] = elt
;
1900 realized
[0][j
] = Qnil
;
1902 for (i
= j
= 0; ! NILP (realized
[0][i
]); i
++)
1904 elt
= FONTSET_DEFAULT (realized
[0][i
]);
1906 realized
[1][j
++] = elt
;
1908 realized
[1][j
] = Qnil
;
1910 tables
[0] = Fmake_char_table (Qfontset_info
, Qnil
);
1911 fontsets
[0] = fontset
;
1912 if (!EQ (fontset
, Vdefault_fontset
))
1914 tables
[1] = Fmake_char_table (Qnil
, Qnil
);
1915 set_char_table_extras (tables
[0], 0, tables
[1]);
1916 fontsets
[1] = Vdefault_fontset
;
1919 /* Accumulate information of the fontset in TABLE. The format of
1920 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1921 for (k
= 0; k
<= 1; k
++)
1923 for (c
= 0; c
<= MAX_CHAR
; )
1925 int from
= c
, to
= MAX_5_BYTE_CHAR
;
1927 if (c
<= MAX_5_BYTE_CHAR
)
1929 val
= char_table_ref_and_range (fontsets
[k
], c
, &from
, &to
);
1933 val
= FONTSET_FALLBACK (fontsets
[k
]);
1940 /* At first, set ALIST to ((FONT-SPEC) ...). */
1941 for (alist
= Qnil
, i
= 0; i
< ASIZE (val
); i
++)
1942 if (! NILP (AREF (val
, i
)))
1943 alist
= Fcons (Fcons (FONT_DEF_SPEC (AREF (val
, i
)), Qnil
),
1945 alist
= Fnreverse (alist
);
1947 /* Then store opened font names to cdr of each elements. */
1948 for (i
= 0; ! NILP (realized
[k
][i
]); i
++)
1950 if (c
<= MAX_5_BYTE_CHAR
)
1951 val
= FONTSET_REF (realized
[k
][i
], c
);
1953 val
= FONTSET_FALLBACK (realized
[k
][i
]);
1954 if (! CONSP (val
) || ! VECTORP (XCDR (val
)))
1956 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1958 for (j
= 0; j
< ASIZE (val
); j
++)
1960 elt
= AREF (val
, j
);
1961 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt
)))
1963 Lisp_Object font_object
= RFONT_DEF_OBJECT (elt
);
1964 Lisp_Object slot
, name
;
1966 slot
= Fassq (RFONT_DEF_SPEC (elt
), alist
);
1967 name
= AREF (font_object
, FONT_NAME_INDEX
);
1968 if (NILP (Fmember (name
, XCDR (slot
))))
1969 nconc2 (slot
, list1 (name
));
1974 /* Store ALIST in TBL for characters C..TO. */
1975 if (c
<= MAX_5_BYTE_CHAR
)
1976 char_table_set_range (tables
[k
], c
, to
, alist
);
1978 set_char_table_defalt (tables
[k
], alist
);
1980 /* At last, change each elements to font names. */
1981 for (; CONSP (alist
); alist
= XCDR (alist
))
1984 XSETCAR (elt
, Ffont_xlfd_name (XCAR (elt
), Qnil
));
1989 if (EQ (fontset
, Vdefault_fontset
))
1998 DEFUN ("fontset-font", Ffontset_font
, Sfontset_font
, 2, 3, 0,
1999 doc
: /* Return a font name pattern for character CH in fontset NAME.
2000 If NAME is t, find a pattern in the default fontset.
2001 If NAME is nil, find a pattern in the fontset of the selected frame.
2003 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2004 family name and REGISTRY is a font registry name. This is actually
2005 the first font name pattern for CH in the fontset or in the default
2008 If the 2nd optional arg ALL is non-nil, return a list of all font name
2010 (Lisp_Object name
, Lisp_Object ch
, Lisp_Object all
)
2013 Lisp_Object fontset
, elt
, list
, repertory
, val
;
2018 fontset
= check_fontset_name (name
, &frame
);
2020 CHECK_CHARACTER (ch
);
2025 for (i
= 0, elt
= FONTSET_REF (fontset
, c
); i
< 2;
2026 i
++, elt
= FONTSET_FALLBACK (fontset
))
2028 for (j
= 0; j
< ASIZE (elt
); j
++)
2030 Lisp_Object family
, registry
;
2032 val
= AREF (elt
, j
);
2035 repertory
= AREF (val
, 1);
2036 if (INTEGERP (repertory
))
2038 struct charset
*charset
= CHARSET_FROM_ID (XINT (repertory
));
2040 if (! CHAR_CHARSET_P (c
, charset
))
2043 else if (CHAR_TABLE_P (repertory
))
2045 if (NILP (CHAR_TABLE_REF (repertory
, c
)))
2048 val
= AREF (val
, 0);
2049 /* VAL is a FONT-SPEC */
2050 family
= AREF (val
, FONT_FAMILY_INDEX
);
2051 if (! NILP (family
))
2052 family
= SYMBOL_NAME (family
);
2053 registry
= AREF (val
, FONT_REGISTRY_INDEX
);
2054 if (! NILP (registry
))
2055 registry
= SYMBOL_NAME (registry
);
2056 val
= Fcons (family
, registry
);
2059 list
= Fcons (val
, list
);
2061 if (EQ (fontset
, Vdefault_fontset
))
2063 fontset
= Vdefault_fontset
;
2065 return (Fnreverse (list
));
2068 DEFUN ("fontset-list", Ffontset_list
, Sfontset_list
, 0, 0, 0,
2069 doc
: /* Return a list of all defined fontset names. */)
2072 Lisp_Object fontset
, list
;
2076 for (i
= 0; i
< ASIZE (Vfontset_table
); i
++)
2078 fontset
= FONTSET_FROM_ID (i
);
2080 && BASE_FONTSET_P (fontset
))
2081 list
= Fcons (FONTSET_NAME (fontset
), list
);
2088 #ifdef ENABLE_CHECKING
2090 Lisp_Object
dump_fontset (Lisp_Object
) EXTERNALLY_VISIBLE
;
2093 dump_fontset (Lisp_Object fontset
)
2097 vec
= Fmake_vector (make_number (3), Qnil
);
2098 ASET (vec
, 0, FONTSET_ID (fontset
));
2100 if (BASE_FONTSET_P (fontset
))
2102 ASET (vec
, 1, FONTSET_NAME (fontset
));
2108 frame
= FONTSET_FRAME (fontset
);
2111 struct frame
*f
= XFRAME (frame
);
2113 if (FRAME_LIVE_P (f
))
2115 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)),
2119 Fcons (FONTSET_NAME (FONTSET_BASE (fontset
)), Qnil
));
2121 if (!NILP (FONTSET_DEFAULT (fontset
)))
2122 ASET (vec
, 2, FONTSET_ID (FONTSET_DEFAULT (fontset
)));
2127 DEFUN ("fontset-list-all", Ffontset_list_all
, Sfontset_list_all
, 0, 0, 0,
2128 doc
: /* Return a brief summary of all fontsets for debug use. */)
2134 for (i
= 0, val
= Qnil
; i
< ASIZE (Vfontset_table
); i
++)
2135 if (! NILP (AREF (Vfontset_table
, i
)))
2136 val
= Fcons (dump_fontset (AREF (Vfontset_table
, i
)), val
);
2137 return (Fnreverse (val
));
2139 #endif /* ENABLE_CHECKING */
2142 syms_of_fontset (void)
2144 DEFSYM (Qfontset
, "fontset");
2145 Fput (Qfontset
, Qchar_table_extra_slots
, make_number (8));
2146 DEFSYM (Qfontset_info
, "fontset-info");
2147 Fput (Qfontset_info
, Qchar_table_extra_slots
, make_number (1));
2149 DEFSYM (Qprepend
, "prepend");
2150 DEFSYM (Qappend
, "append");
2151 DEFSYM (Qlatin
, "latin");
2153 Vcached_fontset_data
= Qnil
;
2154 staticpro (&Vcached_fontset_data
);
2156 Vfontset_table
= Fmake_vector (make_number (32), Qnil
);
2157 staticpro (&Vfontset_table
);
2159 Vdefault_fontset
= Fmake_char_table (Qfontset
, Qnil
);
2160 staticpro (&Vdefault_fontset
);
2161 set_fontset_id (Vdefault_fontset
, make_number (0));
2164 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2165 ASET (Vfontset_table
, 0, Vdefault_fontset
);
2166 next_fontset_id
= 1;
2168 auto_fontset_alist
= Qnil
;
2169 staticpro (&auto_fontset_alist
);
2171 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist
,
2173 Alist of charsets vs the charsets to determine the preferred font encoding.
2174 Each element looks like (CHARSET . ENCODING-CHARSET),
2175 where ENCODING-CHARSET is a charset registered in the variable
2176 `font-encoding-alist' as ENCODING.
2178 When a text has a property `charset' and the value is CHARSET, a font
2179 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2180 Vfont_encoding_charset_alist
= Qnil
;
2182 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent
,
2184 Char table of characters whose ascent values should be ignored.
2185 If an entry for a character is non-nil, the ascent value of the glyph
2186 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2188 This affects how a composite character which contains
2189 such a character is displayed on screen. */);
2190 Vuse_default_ascent
= Qnil
;
2192 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition
,
2194 Char table of characters which are not composed relatively.
2195 If an entry for a character is non-nil, a composition sequence
2196 which contains that character is displayed so that
2197 the glyph of that character is put without considering
2198 an ascent and descent value of a previous character. */);
2199 Vignore_relative_composition
= Qnil
;
2201 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist
,
2202 doc
: /* Alist of fontname vs list of the alternate fontnames.
2203 When a specified font name is not found, the corresponding
2204 alternate fontnames (if any) are tried instead. */);
2205 Valternate_fontname_alist
= Qnil
;
2207 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist
,
2208 doc
: /* Alist of fontset names vs the aliases. */);
2209 Vfontset_alias_alist
2210 = list1 (Fcons (FONTSET_NAME (Vdefault_fontset
),
2211 build_pure_c_string ("fontset-default")));
2213 DEFVAR_LISP ("vertical-centering-font-regexp",
2214 Vvertical_centering_font_regexp
,
2215 doc
: /* Regexp matching font names that require vertical centering on display.
2216 When a character is displayed with such fonts, the character is displayed
2217 at the vertical center of lines. */);
2218 Vvertical_centering_font_regexp
= Qnil
;
2220 DEFVAR_LISP ("otf-script-alist", Votf_script_alist
,
2221 doc
: /* Alist of OpenType script tags vs the corresponding script names. */);
2222 Votf_script_alist
= Qnil
;
2224 defsubr (&Squery_fontset
);
2225 defsubr (&Snew_fontset
);
2226 defsubr (&Sset_fontset_font
);
2227 defsubr (&Sinternal_char_font
);
2228 defsubr (&Sfontset_info
);
2229 defsubr (&Sfontset_font
);
2230 defsubr (&Sfontset_list
);
2231 #ifdef ENABLE_CHECKING
2232 defsubr (&Sfontset_list_all
);