]> code.delx.au - gnu-emacs/blob - src/fontset.c
Update copyright year to 2015
[gnu-emacs] / src / fontset.c
1 /* Fontset handler.
2
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
11
12 This file is part of GNU Emacs.
13
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.
18
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.
23
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/>. */
26
27 #include <config.h>
28 #include <stdio.h>
29
30 #include "lisp.h"
31 #include "blockinput.h"
32 #include "character.h"
33 #include "buffer.h"
34 #include "charset.h"
35 #include "ccl.h"
36 #include "keyboard.h"
37 #include "frame.h"
38 #include "dispextern.h"
39 #include "intervals.h"
40 #include "fontset.h"
41 #include "window.h"
42 #ifdef HAVE_WINDOW_SYSTEM
43 #include TERM_HEADER
44 #endif /* HAVE_WINDOW_SYSTEM */
45 #include "termhooks.h"
46 #include "font.h"
47
48 /* FONTSET
49
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.
55
56 Another role, the more important one, is to provide information
57 about which font to use for each non-ASCII character.
58
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.
65
66 A fontset object is implemented by a char-table whose default value
67 and parent are always nil.
68
69 An element of a base fontset is a vector of FONT-DEFs which itself
70 is a vector [ FONT-SPEC ENCODING REPERTORY ].
71
72 An element of a realized fontset is nil, t, 0, or a vector of this
73 form:
74
75 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
76 RFONT-DEF0 RFONT-DEF1 ... ]
77
78 RFONT-DEFn (i.e. Realized FONT-DEF) has this form:
79
80 [ FACE-ID FONT-DEF FONT-OBJECT SORTING-SCORE ]
81
82 RFONT-DEFn are automatically reordered by the current charset
83 priority list.
84
85 The value nil means that we have not yet generated the above vector
86 from the base of the fontset.
87
88 The value t means that no font is available for the corresponding
89 range of characters.
90
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
93 default fontset.
94
95 A fontset has 8 extra slots.
96
97 The 1st slot:
98 base: the ID number of the fontset
99 realized: Likewise
100
101 The 2nd slot:
102 base: the name of the fontset
103 realized: nil
104
105 The 3rd slot:
106 base: the font name for ASCII characters
107 realized: nil
108
109 The 4th slot:
110 base: nil
111 realized: the base fontset
112
113 The 5th slot:
114 base: nil
115 realized: the frame that the fontset belongs to
116
117 The 6th slot:
118 base: nil
119 realized: the ID number of a face to use for characters that
120 has no font in a realized fontset.
121
122 The 7th slot:
123 base: nil
124 realized: If the base is not the default fontset, a fontset
125 realized from the default fontset, else nil.
126
127 The 8th slot:
128 base: Same as element value (but for fallback fonts).
129 realized: Likewise.
130
131 All fontsets are recorded in the vector Vfontset_table.
132
133
134 DEFAULT FONTSET
135
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.
140
141 The parent of a realized fontset created for such a face that has
142 no fontset is the default fontset.
143
144
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.
150
151 */
152
153 /********** VARIABLES and FUNCTION PROTOTYPES **********/
154
155 static Lisp_Object Qfontset;
156 static Lisp_Object Qfontset_info;
157 static Lisp_Object Qprepend, Qappend;
158 Lisp_Object Qlatin;
159
160 /* Vector containing all fontsets. */
161 static Lisp_Object Vfontset_table;
162
163 /* Next possibly free fontset ID. Usually this keeps the minimum
164 fontset ID not yet used. */
165 static int next_fontset_id;
166
167 /* The default fontset. This gives default FAMILY and REGISTRY of
168 font for each character. */
169 static Lisp_Object Vdefault_fontset;
170
171 /* Prototype declarations for static functions. */
172 static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
173
174 /* Return true if ID is a valid fontset id.
175 Optimized away if ENABLE_CHECKING is not defined. */
176
177 static bool
178 fontset_id_valid_p (int id)
179 {
180 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
181 }
182
183
184 \f
185 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
186
187 /* Return the fontset with ID. No check of ID's validness. */
188 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
189
190 /* Access special values of FONTSET. */
191
192 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
193 static void
194 set_fontset_id (Lisp_Object fontset, Lisp_Object id)
195 {
196 set_char_table_extras (fontset, 0, id);
197 }
198
199 /* Access special values of (base) FONTSET. */
200
201 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
202 static void
203 set_fontset_name (Lisp_Object fontset, Lisp_Object name)
204 {
205 set_char_table_extras (fontset, 1, name);
206 }
207
208 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
209 static void
210 set_fontset_ascii (Lisp_Object fontset, Lisp_Object ascii)
211 {
212 set_char_table_extras (fontset, 2, ascii);
213 }
214
215 /* Access special values of (realized) FONTSET. */
216
217 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
218 static void
219 set_fontset_base (Lisp_Object fontset, Lisp_Object base)
220 {
221 set_char_table_extras (fontset, 3, base);
222 }
223
224 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
225 static void
226 set_fontset_frame (Lisp_Object fontset, Lisp_Object frame)
227 {
228 set_char_table_extras (fontset, 4, frame);
229 }
230
231 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
232 static void
233 set_fontset_nofont_face (Lisp_Object fontset, Lisp_Object face)
234 {
235 set_char_table_extras (fontset, 5, face);
236 }
237
238 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
239 static void
240 set_fontset_default (Lisp_Object fontset, Lisp_Object def)
241 {
242 set_char_table_extras (fontset, 6, def);
243 }
244
245 /* For both base and realized fontset. */
246
247 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
248 static void
249 set_fontset_fallback (Lisp_Object fontset, Lisp_Object fallback)
250 {
251 set_char_table_extras (fontset, 7, fallback);
252 }
253
254 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
255
256 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
257 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
258 do { \
259 (font_def) = make_uninit_vector (3); \
260 ASET ((font_def), 0, font_spec); \
261 ASET ((font_def), 1, encoding); \
262 ASET ((font_def), 2, repertory); \
263 } while (0)
264
265 #define FONT_DEF_SPEC(font_def) AREF (font_def, 0)
266 #define FONT_DEF_ENCODING(font_def) AREF (font_def, 1)
267 #define FONT_DEF_REPERTORY(font_def) AREF (font_def, 2)
268
269 #define RFONT_DEF_FACE(rfont_def) AREF (rfont_def, 0)
270 #define RFONT_DEF_SET_FACE(rfont_def, face_id) \
271 ASET ((rfont_def), 0, make_number (face_id))
272 #define RFONT_DEF_FONT_DEF(rfont_def) AREF (rfont_def, 1)
273 #define RFONT_DEF_SPEC(rfont_def) FONT_DEF_SPEC (AREF (rfont_def, 1))
274 #define RFONT_DEF_OBJECT(rfont_def) AREF (rfont_def, 2)
275 #define RFONT_DEF_SET_OBJECT(rfont_def, object) \
276 ASET ((rfont_def), 2, (object))
277 /* Score of RFONT_DEF is an integer value; the lowest 8 bits represent
278 the order of listing by font backends, the higher bits represents
279 the order given by charset priority list. The smaller value is
280 preferable. */
281 #define RFONT_DEF_SCORE(rfont_def) XINT (AREF (rfont_def, 3))
282 #define RFONT_DEF_SET_SCORE(rfont_def, score) \
283 ASET ((rfont_def), 3, make_number (score))
284 #define RFONT_DEF_NEW(rfont_def, font_def) \
285 do { \
286 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
287 ASET ((rfont_def), 1, (font_def)); \
288 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
289 } while (0)
290
291
292 /* Return the element of FONTSET for the character C. If FONTSET is a
293 base fontset other then the default fontset and FONTSET doesn't
294 contain information for C, return the information in the default
295 fontset. */
296
297 #define FONTSET_REF(fontset, c) \
298 (EQ (fontset, Vdefault_fontset) \
299 ? CHAR_TABLE_REF (fontset, c) \
300 : fontset_ref ((fontset), (c)))
301
302 static Lisp_Object
303 fontset_ref (Lisp_Object fontset, int c)
304 {
305 Lisp_Object elt;
306
307 elt = CHAR_TABLE_REF (fontset, c);
308 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
309 /* Don't check Vdefault_fontset for a realized fontset. */
310 && NILP (FONTSET_BASE (fontset)))
311 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
312 return elt;
313 }
314
315 /* Set elements of FONTSET for characters in RANGE to the value ELT.
316 RANGE is a cons (FROM . TO), where FROM and TO are character codes
317 specifying a range. */
318
319 #define FONTSET_SET(fontset, range, elt) \
320 Fset_char_table_range ((fontset), (range), (elt))
321
322
323 /* Modify the elements of FONTSET for characters in RANGE by replacing
324 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
325 and TO are character codes specifying a range. If ADD is nil,
326 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
327 append ELT. */
328
329 #define FONTSET_ADD(fontset, range, elt, add) \
330 (NILP (add) \
331 ? (NILP (range) \
332 ? (set_fontset_fallback \
333 (fontset, Fmake_vector (make_number (1), (elt)))) \
334 : ((void) \
335 Fset_char_table_range (fontset, range, \
336 Fmake_vector (make_number (1), elt)))) \
337 : fontset_add ((fontset), (range), (elt), (add)))
338
339 static void
340 fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
341 {
342 Lisp_Object args[2];
343 int idx = (EQ (add, Qappend) ? 0 : 1);
344
345 args[1 - idx] = Fmake_vector (make_number (1), elt);
346
347 if (CONSP (range))
348 {
349 int from = XINT (XCAR (range));
350 int to = XINT (XCDR (range));
351 int from1, to1;
352
353 do {
354 from1 = from, to1 = to;
355 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
356 char_table_set_range (fontset, from, to1,
357 NILP (args[idx]) ? args[1 - idx]
358 : Fvconcat (2, args));
359 from = to1 + 1;
360 } while (from < to);
361 }
362 else
363 {
364 args[idx] = FONTSET_FALLBACK (fontset);
365 set_fontset_fallback
366 (fontset, NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args));
367 }
368 }
369
370 static int
371 fontset_compare_rfontdef (const void *val1, const void *val2)
372 {
373 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
374 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
375 }
376
377 /* Update FONT-GROUP which has this form:
378 [ CHARSET-ORDERED-LIST-TICK PREFERRED-RFONT-DEF
379 RFONT-DEF0 RFONT-DEF1 ... ]
380 Reorder RFONT-DEFs according to the current language, and update
381 CHARSET-ORDERED-LIST-TICK.
382
383 If PREFERRED_FAMILY is not nil, that family has the higher priority
384 if the encoding charsets or languages in font-specs are the same. */
385
386 static void
387 reorder_font_vector (Lisp_Object font_group, struct font *font)
388 {
389 Lisp_Object vec, font_object;
390 int size;
391 int i;
392 bool score_changed = false;
393
394 if (font)
395 XSETFONT (font_object, font);
396 else
397 font_object = Qnil;
398
399 vec = XCDR (font_group);
400 size = ASIZE (vec);
401 /* Exclude the tailing nil element from the reordering. */
402 if (NILP (AREF (vec, size - 1)))
403 size--;
404
405 for (i = 0; i < size; i++)
406 {
407 Lisp_Object rfont_def = AREF (vec, i);
408 Lisp_Object font_def = RFONT_DEF_FONT_DEF (rfont_def);
409 Lisp_Object font_spec = FONT_DEF_SPEC (font_def);
410 int score = RFONT_DEF_SCORE (rfont_def) & 0xFF;
411 Lisp_Object otf_spec = Ffont_get (font_spec, QCotf);
412
413 if (! NILP (otf_spec))
414 /* A font-spec with :otf is preferable regardless of encoding
415 and language.. */
416 ;
417 else if (! font_match_p (font_spec, font_object))
418 {
419 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
420
421 if (! NILP (encoding))
422 {
423 Lisp_Object tail;
424
425 for (tail = Vcharset_ordered_list;
426 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
427 tail = XCDR (tail))
428 if (EQ (encoding, XCAR (tail)))
429 break;
430 else if (score <= min (INT_MAX, MOST_POSITIVE_FIXNUM) - 0x100)
431 score += 0x100;
432 }
433 else
434 {
435 Lisp_Object lang = Ffont_get (font_spec, QClang);
436
437 if (! NILP (lang)
438 && ! EQ (lang, Vcurrent_iso639_language)
439 && (! CONSP (Vcurrent_iso639_language)
440 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
441 score |= 0x100;
442 }
443 }
444 if (RFONT_DEF_SCORE (rfont_def) != score)
445 {
446 RFONT_DEF_SET_SCORE (rfont_def, score);
447 score_changed = true;
448 }
449 }
450
451 if (score_changed)
452 qsort (XVECTOR (vec)->contents, size, word_size,
453 fontset_compare_rfontdef);
454 EMACS_INT low_tick_bits = charset_ordered_list_tick & MOST_POSITIVE_FIXNUM;
455 XSETCAR (font_group, make_number (low_tick_bits));
456 }
457
458 /* Return a font-group (actually a cons (-1 . FONT-GROUP-VECTOR)) for
459 character C in FONTSET. If C is -1, return a fallback font-group.
460 If C is not -1, the value may be Qt (FONTSET doesn't have a font
461 for C even in the fallback group), or 0 (a font for C may be found
462 only in the fallback group). */
463
464 static Lisp_Object
465 fontset_get_font_group (Lisp_Object fontset, int c)
466 {
467 Lisp_Object font_group;
468 Lisp_Object base_fontset;
469 int from = 0, to = MAX_CHAR, i;
470
471 eassert (! BASE_FONTSET_P (fontset));
472 if (c >= 0)
473 font_group = CHAR_TABLE_REF (fontset, c);
474 else
475 font_group = FONTSET_FALLBACK (fontset);
476 if (! NILP (font_group))
477 return font_group;
478 base_fontset = FONTSET_BASE (fontset);
479 if (NILP (base_fontset))
480 font_group = Qnil;
481 else if (c >= 0)
482 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
483 else
484 font_group = FONTSET_FALLBACK (base_fontset);
485 if (NILP (font_group))
486 {
487 font_group = make_number (0);
488 if (c >= 0)
489 char_table_set_range (fontset, from, to, font_group);
490 return font_group;
491 }
492 if (!VECTORP (font_group))
493 return font_group;
494 font_group = Fcopy_sequence (font_group);
495 for (i = 0; i < ASIZE (font_group); i++)
496 if (! NILP (AREF (font_group, i)))
497 {
498 Lisp_Object rfont_def;
499
500 RFONT_DEF_NEW (rfont_def, AREF (font_group, i));
501 /* Remember the original order. */
502 RFONT_DEF_SET_SCORE (rfont_def, i);
503 ASET (font_group, i, rfont_def);
504 }
505 font_group = Fcons (make_number (-1), font_group);
506 if (c >= 0)
507 char_table_set_range (fontset, from, to, font_group);
508 else
509 set_fontset_fallback (fontset, font_group);
510 return font_group;
511 }
512
513 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
514 character C. If no font is found, return Qnil if there's a
515 possibility that the default fontset or the fallback font groups
516 have a proper font, and return Qt if not.
517
518 If a font is found but is not yet opened, open it (if FACE is not
519 NULL) or return Qnil (if FACE is NULL).
520
521 ID is a charset-id that must be preferred, or -1 meaning no
522 preference.
523
524 If FALLBACK, search only fallback fonts. */
525
526 static Lisp_Object
527 fontset_find_font (Lisp_Object fontset, int c, struct face *face, int id,
528 bool fallback)
529 {
530 Lisp_Object vec, font_group;
531 int i, charset_matched = 0, found_index;
532 struct frame *f = (FRAMEP (FONTSET_FRAME (fontset))
533 ? XFRAME (FONTSET_FRAME (fontset))
534 : XFRAME (selected_frame));
535 Lisp_Object rfont_def;
536
537 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
538 if (! CONSP (font_group))
539 return font_group;
540 vec = XCDR (font_group);
541 if (ASIZE (vec) == 0)
542 return Qnil;
543
544 if (ASIZE (vec) > 1)
545 {
546 if (XINT (XCAR (font_group)) != charset_ordered_list_tick)
547 /* We have just created the font-group,
548 or the charset priorities were changed. */
549 reorder_font_vector (font_group, face->ascii_face->font);
550 if (id >= 0)
551 /* Find a spec matching with the charset ID to try at
552 first. */
553 for (i = 0; i < ASIZE (vec); i++)
554 {
555 Lisp_Object repertory;
556
557 rfont_def = AREF (vec, i);
558 if (NILP (rfont_def))
559 break;
560 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
561
562 if (XINT (repertory) == id)
563 {
564 charset_matched = i;
565 break;
566 }
567 }
568 }
569
570 /* Find the first available font in the vector of RFONT-DEF. */
571 for (i = 0; i < ASIZE (vec); i++)
572 {
573 Lisp_Object font_def;
574 Lisp_Object font_entity, font_object;
575
576 found_index = i;
577 if (i == 0)
578 {
579 if (charset_matched > 0)
580 {
581 /* Try the element matching with the charset ID at first. */
582 found_index = charset_matched;
583 /* Make this negative so that we don't come here in the
584 next loop. */
585 charset_matched = - charset_matched;
586 /* We must try the first element in the next loop. */
587 i--;
588 }
589 }
590 else if (i == - charset_matched)
591 {
592 /* We have already tried this element and the followings
593 that have the same font specifications in the first
594 iteration. So, skip them all. */
595 rfont_def = AREF (vec, i);
596 font_def = RFONT_DEF_FONT_DEF (rfont_def);
597 for (; i + 1 < ASIZE (vec); i++)
598 {
599 rfont_def = AREF (vec, i + 1);
600 if (NILP (rfont_def))
601 break;
602 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
603 break;
604 }
605 continue;
606 }
607
608 rfont_def = AREF (vec, found_index);
609 if (NILP (rfont_def))
610 {
611 if (i < 0)
612 continue;
613 /* This is a sign of not to try the other fonts. */
614 return Qt;
615 }
616 if (INTEGERP (RFONT_DEF_FACE (rfont_def))
617 && XINT (RFONT_DEF_FACE (rfont_def)) < 0)
618 /* We couldn't open this font last time. */
619 continue;
620
621 font_object = RFONT_DEF_OBJECT (rfont_def);
622 if (NILP (font_object))
623 {
624 font_def = RFONT_DEF_FONT_DEF (rfont_def);
625
626 if (! face)
627 /* We have not yet opened the font. */
628 return Qnil;
629 /* Find a font best-matching with the spec without checking
630 the support of the character C. That checking is costly,
631 and even without the checking, the found font supports C
632 in high possibility. */
633 font_entity = font_find_for_lface (f, face->lface,
634 FONT_DEF_SPEC (font_def), -1);
635 if (NILP (font_entity))
636 {
637 /* Record that no font matches the spec. */
638 RFONT_DEF_SET_FACE (rfont_def, -1);
639 continue;
640 }
641 font_object = font_open_for_lface (f, font_entity, face->lface,
642 FONT_DEF_SPEC (font_def));
643 if (NILP (font_object))
644 {
645 /* Something strange happened, perhaps because of a
646 Font-backend problem. Too avoid crashing, record
647 that this spec is unusable. It may be better to find
648 another font of the same spec, but currently we don't
649 have such an API. */
650 RFONT_DEF_SET_FACE (rfont_def, -1);
651 continue;
652 }
653 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
654 }
655
656 if (font_has_char (f, font_object, c))
657 goto found;
658
659 /* Find a font already opened, matching with the current spec,
660 and supporting C. */
661 font_def = RFONT_DEF_FONT_DEF (rfont_def);
662 for (; found_index + 1 < ASIZE (vec); found_index++)
663 {
664 rfont_def = AREF (vec, found_index + 1);
665 if (NILP (rfont_def))
666 break;
667 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
668 break;
669 font_object = RFONT_DEF_OBJECT (rfont_def);
670 if (! NILP (font_object) && font_has_char (f, font_object, c))
671 {
672 found_index++;
673 goto found;
674 }
675 }
676
677 /* Find a font-entity with the current spec and supporting C. */
678 font_entity = font_find_for_lface (f, face->lface,
679 FONT_DEF_SPEC (font_def), c);
680 if (! NILP (font_entity))
681 {
682 /* We found a font. Open it and insert a new element for
683 that font in VEC. */
684 Lisp_Object new_vec;
685 int j;
686
687 font_object = font_open_for_lface (f, font_entity, face->lface,
688 Qnil);
689 if (NILP (font_object))
690 continue;
691 RFONT_DEF_NEW (rfont_def, font_def);
692 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
693 RFONT_DEF_SET_SCORE (rfont_def, RFONT_DEF_SCORE (rfont_def));
694 new_vec = Fmake_vector (make_number (ASIZE (vec) + 1), Qnil);
695 found_index++;
696 for (j = 0; j < found_index; j++)
697 ASET (new_vec, j, AREF (vec, j));
698 ASET (new_vec, j, rfont_def);
699 for (j++; j < ASIZE (new_vec); j++)
700 ASET (new_vec, j, AREF (vec, j - 1));
701 XSETCDR (font_group, new_vec);
702 vec = new_vec;
703 goto found;
704 }
705 if (i >= 0)
706 i = found_index;
707 }
708
709 FONTSET_SET (fontset, make_number (c), make_number (0));
710 return Qnil;
711
712 found:
713 if (fallback && found_index > 0)
714 {
715 /* The order of fonts in the fallback font-group is not that
716 important, and it is better to move the found font to the
717 first of the group so that the next try will find it
718 quickly. */
719 for (i = found_index; i > 0; i--)
720 ASET (vec, i, AREF (vec, i - 1));
721 ASET (vec, 0, rfont_def);
722 }
723 return rfont_def;
724 }
725
726
727 static Lisp_Object
728 fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
729 {
730 Lisp_Object rfont_def, default_rfont_def IF_LINT (= Qnil);
731 Lisp_Object base_fontset;
732
733 /* Try a font-group of FONTSET. */
734 FONT_DEFERRED_LOG ("current fontset: font for", make_number (c), Qnil);
735 rfont_def = fontset_find_font (fontset, c, face, id, 0);
736 if (VECTORP (rfont_def))
737 return rfont_def;
738 if (NILP (rfont_def))
739 FONTSET_SET (fontset, make_number (c), make_number (0));
740
741 /* Try a font-group of the default fontset. */
742 base_fontset = FONTSET_BASE (fontset);
743 if (! EQ (base_fontset, Vdefault_fontset))
744 {
745 if (NILP (FONTSET_DEFAULT (fontset)))
746 set_fontset_default
747 (fontset,
748 make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset));
749 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
750 default_rfont_def
751 = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 0);
752 if (VECTORP (default_rfont_def))
753 return default_rfont_def;
754 if (NILP (default_rfont_def))
755 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c),
756 make_number (0));
757 }
758
759 /* Try a fallback font-group of FONTSET. */
760 if (! EQ (rfont_def, Qt))
761 {
762 FONT_DEFERRED_LOG ("current fallback: font for", make_number (c), Qnil);
763 rfont_def = fontset_find_font (fontset, c, face, id, 1);
764 if (VECTORP (rfont_def))
765 return rfont_def;
766 /* Remember that FONTSET has no font for C. */
767 FONTSET_SET (fontset, make_number (c), Qt);
768 }
769
770 /* Try a fallback font-group of the default fontset. */
771 if (! EQ (base_fontset, Vdefault_fontset)
772 && ! EQ (default_rfont_def, Qt))
773 {
774 FONT_DEFERRED_LOG ("default fallback: font for", make_number (c), Qnil);
775 rfont_def = fontset_find_font (FONTSET_DEFAULT (fontset), c, face, id, 1);
776 if (VECTORP (rfont_def))
777 return rfont_def;
778 /* Remember that the default fontset has no font for C. */
779 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
780 }
781
782 return Qnil;
783 }
784
785 /* Return a newly created fontset with NAME. If BASE is nil, make a
786 base fontset. Otherwise make a realized fontset whose base is
787 BASE. */
788
789 static Lisp_Object
790 make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
791 {
792 Lisp_Object fontset;
793 int size = ASIZE (Vfontset_table);
794 int id = next_fontset_id;
795
796 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
797 the next available fontset ID. So it is expected that this loop
798 terminates quickly. In addition, as the last element of
799 Vfontset_table is always nil, we don't have to check the range of
800 id. */
801 while (!NILP (AREF (Vfontset_table, id))) id++;
802
803 if (id + 1 == size)
804 Vfontset_table = larger_vector (Vfontset_table, 1, -1);
805
806 fontset = Fmake_char_table (Qfontset, Qnil);
807
808 set_fontset_id (fontset, make_number (id));
809 if (NILP (base))
810 set_fontset_name (fontset, name);
811 else
812 {
813 set_fontset_name (fontset, Qnil);
814 set_fontset_frame (fontset, frame);
815 set_fontset_base (fontset, base);
816 }
817
818 ASET (Vfontset_table, id, fontset);
819 next_fontset_id = id + 1;
820 return fontset;
821 }
822
823 \f
824 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
825
826 /* Return the name of the fontset who has ID. */
827
828 Lisp_Object
829 fontset_name (int id)
830 {
831 Lisp_Object fontset;
832
833 fontset = FONTSET_FROM_ID (id);
834 return FONTSET_NAME (fontset);
835 }
836
837
838 /* Return the ASCII font name of the fontset who has ID. */
839
840 Lisp_Object
841 fontset_ascii (int id)
842 {
843 Lisp_Object fontset, elt;
844
845 fontset= FONTSET_FROM_ID (id);
846 elt = FONTSET_ASCII (fontset);
847 if (CONSP (elt))
848 elt = XCAR (elt);
849 return elt;
850 }
851
852 /* Free fontset of FACE defined on frame F. Called from
853 free_realized_face. */
854
855 void
856 free_face_fontset (struct frame *f, struct face *face)
857 {
858 Lisp_Object fontset;
859
860 fontset = FONTSET_FROM_ID (face->fontset);
861 if (NILP (fontset))
862 return;
863 eassert (! BASE_FONTSET_P (fontset));
864 eassert (f == XFRAME (FONTSET_FRAME (fontset)));
865 ASET (Vfontset_table, face->fontset, Qnil);
866 if (face->fontset < next_fontset_id)
867 next_fontset_id = face->fontset;
868 if (! NILP (FONTSET_DEFAULT (fontset)))
869 {
870 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
871
872 fontset = AREF (Vfontset_table, id);
873 eassert (!NILP (fontset) && ! BASE_FONTSET_P (fontset));
874 eassert (f == XFRAME (FONTSET_FRAME (fontset)));
875 ASET (Vfontset_table, id, Qnil);
876 if (id < next_fontset_id)
877 next_fontset_id = face->fontset;
878 }
879 face->fontset = -1;
880 }
881
882 /* Return ID of face suitable for displaying character C at buffer position
883 POS on frame F. FACE must be realized for ASCII characters in advance.
884 Called from the macro FACE_FOR_CHAR. */
885
886 int
887 face_for_char (struct frame *f, struct face *face, int c,
888 ptrdiff_t pos, Lisp_Object object)
889 {
890 Lisp_Object fontset, rfont_def, charset;
891 int face_id;
892 int id;
893
894 eassert (fontset_id_valid_p (face->fontset));
895
896 if (ASCII_CHAR_P (c) || CHAR_BYTE8_P (c))
897 return face->ascii_face->id;
898
899 #ifdef HAVE_NS
900 if (face->font)
901 {
902 /* Fonts often have characters in other scripts, like symbol, even if they
903 don't match script: symbol. So check if the character is present
904 in the current face first. Only enable for NS for now, but should
905 perhaps be general? */
906 Lisp_Object font_object;
907 XSETFONT (font_object, face->font);
908 if (font_has_char (f, font_object, c)) return face->id;
909 }
910 #endif
911
912 fontset = FONTSET_FROM_ID (face->fontset);
913 eassert (!BASE_FONTSET_P (fontset));
914
915 if (pos < 0)
916 {
917 id = -1;
918 charset = Qnil;
919 }
920 else
921 {
922 charset = Fget_char_property (make_number (pos), Qcharset, object);
923 if (CHARSETP (charset))
924 {
925 Lisp_Object val;
926
927 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
928 if (CONSP (val) && CHARSETP (XCDR (val)))
929 charset = XCDR (val);
930 id = XINT (CHARSET_SYMBOL_ID (charset));
931 }
932 else
933 id = -1;
934 }
935
936 rfont_def = fontset_font (fontset, c, face, id);
937 if (VECTORP (rfont_def))
938 {
939 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
940 face_id = XINT (RFONT_DEF_FACE (rfont_def));
941 else
942 {
943 Lisp_Object font_object;
944
945 font_object = RFONT_DEF_OBJECT (rfont_def);
946 face_id = face_for_font (f, font_object, face);
947 RFONT_DEF_SET_FACE (rfont_def, face_id);
948 }
949 }
950 else
951 {
952 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
953 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
954 else
955 {
956 face_id = face_for_font (f, Qnil, face);
957 set_fontset_nofont_face (fontset, make_number (face_id));
958 }
959 }
960 eassert (face_id >= 0);
961 return face_id;
962 }
963
964
965 Lisp_Object
966 font_for_char (struct face *face, int c, ptrdiff_t pos, Lisp_Object object)
967 {
968 Lisp_Object fontset, rfont_def, charset;
969 int id;
970
971 if (ASCII_CHAR_P (c))
972 {
973 Lisp_Object font_object;
974
975 XSETFONT (font_object, face->ascii_face->font);
976 return font_object;
977 }
978
979 eassert (fontset_id_valid_p (face->fontset));
980 fontset = FONTSET_FROM_ID (face->fontset);
981 eassert (!BASE_FONTSET_P (fontset));
982 if (pos < 0)
983 {
984 id = -1;
985 charset = Qnil;
986 }
987 else
988 {
989 charset = Fget_char_property (make_number (pos), Qcharset, object);
990 if (CHARSETP (charset))
991 {
992 Lisp_Object val;
993
994 val = assq_no_quit (charset, Vfont_encoding_charset_alist);
995 if (CONSP (val) && CHARSETP (XCDR (val)))
996 charset = XCDR (val);
997 id = XINT (CHARSET_SYMBOL_ID (charset));
998 }
999 else
1000 id = -1;
1001 }
1002
1003 rfont_def = fontset_font (fontset, c, face, id);
1004 return (VECTORP (rfont_def)
1005 ? RFONT_DEF_OBJECT (rfont_def)
1006 : Qnil);
1007 }
1008
1009
1010 /* Make a realized fontset for ASCII face FACE on frame F from the
1011 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
1012 default fontset as the base. Value is the id of the new fontset.
1013 Called from realize_x_face. */
1014
1015 int
1016 make_fontset_for_ascii_face (struct frame *f, int base_fontset_id, struct face *face)
1017 {
1018 Lisp_Object base_fontset, fontset, frame;
1019
1020 XSETFRAME (frame, f);
1021 if (base_fontset_id >= 0)
1022 {
1023 base_fontset = FONTSET_FROM_ID (base_fontset_id);
1024 if (!BASE_FONTSET_P (base_fontset))
1025 base_fontset = FONTSET_BASE (base_fontset);
1026 eassert (BASE_FONTSET_P (base_fontset));
1027 }
1028 else
1029 base_fontset = Vdefault_fontset;
1030
1031 fontset = make_fontset (frame, Qnil, base_fontset);
1032 return XINT (FONTSET_ID (fontset));
1033 }
1034
1035 \f
1036
1037 /* Cache data used by fontset_pattern_regexp. The car part is a
1038 pattern string containing at least one wild card, the cdr part is
1039 the corresponding regular expression. */
1040 static Lisp_Object Vcached_fontset_data;
1041
1042 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1043 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1044
1045 /* If fontset name PATTERN contains any wild card, return regular
1046 expression corresponding to PATTERN. */
1047
1048 static Lisp_Object
1049 fontset_pattern_regexp (Lisp_Object pattern)
1050 {
1051 if (!strchr (SSDATA (pattern), '*')
1052 && !strchr (SSDATA (pattern), '?'))
1053 /* PATTERN does not contain any wild cards. */
1054 return Qnil;
1055
1056 if (!CONSP (Vcached_fontset_data)
1057 || strcmp (SSDATA (pattern), CACHED_FONTSET_NAME))
1058 {
1059 /* We must at first update the cached data. */
1060 unsigned char *regex, *p0, *p1;
1061 int ndashes = 0, nstars = 0, nescs = 0;
1062
1063 for (p0 = SDATA (pattern); *p0; p0++)
1064 {
1065 if (*p0 == '-')
1066 ndashes++;
1067 else if (*p0 == '*')
1068 nstars++;
1069 else if (*p0 == '['
1070 || *p0 == '.' || *p0 == '\\'
1071 || *p0 == '+' || *p0 == '^'
1072 || *p0 == '$')
1073 nescs++;
1074 }
1075
1076 /* If PATTERN is not full XLFD we convert "*" to ".*". Otherwise
1077 we convert "*" to "[^-]*" which is much faster in regular
1078 expression matching. */
1079 ptrdiff_t regexsize = (SBYTES (pattern)
1080 + (ndashes < 14 ? 2 : 5) * nstars
1081 + 2 * nescs + 1);
1082 USE_SAFE_ALLOCA;
1083 p1 = regex = SAFE_ALLOCA (regexsize);
1084
1085 *p1++ = '^';
1086 for (p0 = SDATA (pattern); *p0; p0++)
1087 {
1088 if (*p0 == '*')
1089 {
1090 if (ndashes < 14)
1091 *p1++ = '.';
1092 else
1093 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1094 *p1++ = '*';
1095 }
1096 else if (*p0 == '?')
1097 *p1++ = '.';
1098 else if (*p0 == '['
1099 || *p0 == '.' || *p0 == '\\'
1100 || *p0 == '+' || *p0 == '^'
1101 || *p0 == '$')
1102 *p1++ = '\\', *p1++ = *p0;
1103 else
1104 *p1++ = *p0;
1105 }
1106 *p1++ = '$';
1107 *p1++ = 0;
1108
1109 Vcached_fontset_data = Fcons (build_string (SSDATA (pattern)),
1110 build_string ((char *) regex));
1111 SAFE_FREE ();
1112 }
1113
1114 return CACHED_FONTSET_REGEX;
1115 }
1116
1117 /* Return ID of the base fontset named NAME. If there's no such
1118 fontset, return -1. NAME_PATTERN specifies how to treat NAME as this:
1119 0: pattern containing '*' and '?' as wildcards
1120 1: regular expression
1121 2: literal fontset name
1122 */
1123
1124 int
1125 fs_query_fontset (Lisp_Object name, int name_pattern)
1126 {
1127 Lisp_Object tem;
1128 int i;
1129
1130 name = Fdowncase (name);
1131 if (name_pattern != 1)
1132 {
1133 tem = Frassoc (name, Vfontset_alias_alist);
1134 if (NILP (tem))
1135 tem = Fassoc (name, Vfontset_alias_alist);
1136 if (CONSP (tem) && STRINGP (XCAR (tem)))
1137 name = XCAR (tem);
1138 else if (name_pattern == 0)
1139 {
1140 tem = fontset_pattern_regexp (name);
1141 if (STRINGP (tem))
1142 {
1143 name = tem;
1144 name_pattern = 1;
1145 }
1146 }
1147 }
1148
1149 for (i = 0; i < ASIZE (Vfontset_table); i++)
1150 {
1151 Lisp_Object fontset, this_name;
1152
1153 fontset = FONTSET_FROM_ID (i);
1154 if (NILP (fontset)
1155 || !BASE_FONTSET_P (fontset))
1156 continue;
1157
1158 this_name = FONTSET_NAME (fontset);
1159 if (name_pattern == 1
1160 ? fast_string_match_ignore_case (name, this_name) >= 0
1161 : !xstrcasecmp (SSDATA (name), SSDATA (this_name)))
1162 return i;
1163 }
1164 return -1;
1165 }
1166
1167
1168 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
1169 doc: /* Return the name of a fontset that matches PATTERN.
1170 The value is nil if there is no matching fontset.
1171 PATTERN can contain `*' or `?' as a wildcard
1172 just as X font name matching algorithm allows.
1173 If REGEXPP is non-nil, PATTERN is a regular expression. */)
1174 (Lisp_Object pattern, Lisp_Object regexpp)
1175 {
1176 Lisp_Object fontset;
1177 int id;
1178
1179 check_window_system (NULL);
1180
1181 CHECK_STRING (pattern);
1182
1183 if (SCHARS (pattern) == 0)
1184 return Qnil;
1185
1186 id = fs_query_fontset (pattern, !NILP (regexpp));
1187 if (id < 0)
1188 return Qnil;
1189
1190 fontset = FONTSET_FROM_ID (id);
1191 return FONTSET_NAME (fontset);
1192 }
1193
1194 /* Return a list of base fontset names matching PATTERN on frame F. */
1195
1196 Lisp_Object
1197 list_fontsets (struct frame *f, Lisp_Object pattern, int size)
1198 {
1199 Lisp_Object frame, regexp, val;
1200 int id;
1201
1202 XSETFRAME (frame, f);
1203
1204 regexp = fontset_pattern_regexp (pattern);
1205 val = Qnil;
1206
1207 for (id = 0; id < ASIZE (Vfontset_table); id++)
1208 {
1209 Lisp_Object fontset, name;
1210
1211 fontset = FONTSET_FROM_ID (id);
1212 if (NILP (fontset)
1213 || !BASE_FONTSET_P (fontset)
1214 || !EQ (frame, FONTSET_FRAME (fontset)))
1215 continue;
1216 name = FONTSET_NAME (fontset);
1217
1218 if (STRINGP (regexp)
1219 ? (fast_string_match (regexp, name) < 0)
1220 : strcmp (SSDATA (pattern), SSDATA (name)))
1221 continue;
1222
1223 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1224 }
1225
1226 return val;
1227 }
1228
1229
1230 /* Free all realized fontsets whose base fontset is BASE. */
1231
1232 static void
1233 free_realized_fontsets (Lisp_Object base)
1234 {
1235 int id;
1236
1237 #if 0
1238 /* For the moment, this doesn't work because free_realized_face
1239 doesn't remove FACE from a cache. Until we find a solution, we
1240 suppress this code, and simply use Fclear_face_cache even though
1241 that is not efficient. */
1242 block_input ();
1243 for (id = 0; id < ASIZE (Vfontset_table); id++)
1244 {
1245 Lisp_Object this = AREF (Vfontset_table, id);
1246
1247 if (EQ (FONTSET_BASE (this), base))
1248 {
1249 Lisp_Object tail;
1250
1251 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1252 tail = XCDR (tail))
1253 {
1254 struct frame *f = XFRAME (FONTSET_FRAME (this));
1255 int face_id = XINT (XCDR (XCAR (tail)));
1256 struct face *face = FACE_FROM_ID (f, face_id);
1257
1258 /* Face THIS itself is also freed by the following call. */
1259 free_realized_face (f, face);
1260 }
1261 }
1262 }
1263 unblock_input ();
1264 #else /* not 0 */
1265 /* But, we don't have to call Fclear_face_cache if no fontset has
1266 been realized from BASE. */
1267 for (id = 0; id < ASIZE (Vfontset_table); id++)
1268 {
1269 Lisp_Object this = AREF (Vfontset_table, id);
1270
1271 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1272 {
1273 Fclear_face_cache (Qt);
1274 break;
1275 }
1276 }
1277 #endif /* not 0 */
1278 }
1279
1280
1281 /* Check validity of NAME as a fontset name and return the
1282 corresponding fontset. If not valid, signal an error.
1283
1284 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1285 fontset of *FRAME.
1286
1287 Set *FRAME to the actual frame. */
1288
1289 static Lisp_Object
1290 check_fontset_name (Lisp_Object name, Lisp_Object *frame)
1291 {
1292 int id;
1293 struct frame *f = decode_live_frame (*frame);
1294
1295 XSETFRAME (*frame, f);
1296
1297 if (EQ (name, Qt))
1298 return Vdefault_fontset;
1299 if (NILP (name))
1300 id = FRAME_FONTSET (f);
1301 else
1302 {
1303 CHECK_STRING (name);
1304 /* First try NAME as literal. */
1305 id = fs_query_fontset (name, 2);
1306 if (id < 0)
1307 /* For backward compatibility, try again NAME as pattern. */
1308 id = fs_query_fontset (name, 0);
1309 if (id < 0)
1310 error ("Fontset `%s' does not exist", SDATA (name));
1311 }
1312 return FONTSET_FROM_ID (id);
1313 }
1314
1315 static void
1316 accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
1317 {
1318 if (EQ (XCAR (arg), val))
1319 {
1320 if (CONSP (range))
1321 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1322 else
1323 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1324 }
1325 }
1326
1327
1328 /* Callback function for map_charset_chars in Fset_fontset_font.
1329 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1330
1331 In FONTSET, set FONT_DEF in a fashion specified by ADD for
1332 characters in RANGE and ranges in SCRIPT_RANGE_LIST before RANGE.
1333 The consumed ranges are popped up from SCRIPT_RANGE_LIST, and the
1334 new SCRIPT_RANGE_LIST is stored in ARG.
1335
1336 If ASCII is nil, don't set FONT_DEF for ASCII characters. It is
1337 assured that SCRIPT_RANGE_LIST doesn't contain ASCII in that
1338 case. */
1339
1340 static void
1341 set_fontset_font (Lisp_Object arg, Lisp_Object range)
1342 {
1343 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1344 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1345
1346 fontset = AREF (arg, 0);
1347 font_def = AREF (arg, 1);
1348 add = AREF (arg, 2);
1349 ascii = AREF (arg, 3);
1350 script_range_list = AREF (arg, 4);
1351
1352 if (NILP (ascii) && from < 0x80)
1353 {
1354 if (to < 0x80)
1355 return;
1356 from = 0x80;
1357 range = Fcons (make_number (0x80), XCDR (range));
1358 }
1359
1360 #define SCRIPT_FROM XINT (XCAR (XCAR (script_range_list)))
1361 #define SCRIPT_TO XINT (XCDR (XCAR (script_range_list)))
1362 #define POP_SCRIPT_RANGE() script_range_list = XCDR (script_range_list)
1363
1364 for (; CONSP (script_range_list) && SCRIPT_TO < from; POP_SCRIPT_RANGE ())
1365 FONTSET_ADD (fontset, XCAR (script_range_list), font_def, add);
1366 if (CONSP (script_range_list))
1367 {
1368 if (SCRIPT_FROM < from)
1369 range = Fcons (make_number (SCRIPT_FROM), XCDR (range));
1370 while (CONSP (script_range_list) && SCRIPT_TO <= to)
1371 POP_SCRIPT_RANGE ();
1372 if (CONSP (script_range_list) && SCRIPT_FROM <= to)
1373 XSETCAR (XCAR (script_range_list), make_number (to + 1));
1374 }
1375
1376 FONTSET_ADD (fontset, range, font_def, add);
1377 ASET (arg, 4, script_range_list);
1378 }
1379
1380 static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
1381
1382
1383 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1384 doc: /*
1385 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1386
1387 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1388 the default fontset.
1389
1390 TARGET may be a cons; (FROM . TO), where FROM and TO are characters.
1391 In that case, use FONT-SPEC for all characters in the range FROM and
1392 TO (inclusive).
1393
1394 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1395 all characters that belong to the script.
1396
1397 TARGET may be a charset. In that case, use FONT-SPEC for all
1398 characters in the charset.
1399
1400 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1401 that no FONT-SPEC is specified.
1402
1403 FONT-SPEC may one of these:
1404 * A font-spec object made by the function `font-spec' (which see).
1405 * A cons (FAMILY . REGISTRY), where FAMILY is a font family name and
1406 REGISTRY is a font registry name. FAMILY may contain foundry
1407 name, and REGISTRY may contain encoding name.
1408 * A font name string.
1409 * nil, which explicitly specifies that there's no font for TARGET.
1410
1411 Optional 4th argument FRAME is a frame or nil for the selected frame
1412 that is concerned in the case that NAME is nil.
1413
1414 Optional 5th argument ADD, if non-nil, specifies how to add FONT-SPEC
1415 to the font specifications for TARGET previously set. If it is
1416 `prepend', FONT-SPEC is prepended. If it is `append', FONT-SPEC is
1417 appended. By default, FONT-SPEC overrides the previous settings. */)
1418 (Lisp_Object name, Lisp_Object target, Lisp_Object font_spec, Lisp_Object frame, Lisp_Object add)
1419 {
1420 Lisp_Object fontset;
1421 Lisp_Object font_def, registry, family;
1422 Lisp_Object range_list;
1423 struct charset *charset = NULL;
1424 Lisp_Object fontname;
1425 bool ascii_changed = 0;
1426
1427 fontset = check_fontset_name (name, &frame);
1428
1429 fontname = Qnil;
1430 if (CONSP (font_spec))
1431 {
1432 Lisp_Object spec = Ffont_spec (0, NULL);
1433
1434 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1435 font_spec = spec;
1436 fontname = Ffont_xlfd_name (font_spec, Qnil);
1437 }
1438 else if (STRINGP (font_spec))
1439 {
1440 Lisp_Object args[2];
1441
1442 fontname = font_spec;
1443 args[0] = QCname;
1444 args[1] = font_spec;
1445 font_spec = Ffont_spec (2, args);
1446 }
1447 else if (FONT_SPEC_P (font_spec))
1448 fontname = Ffont_xlfd_name (font_spec, Qnil);
1449 else if (! NILP (font_spec))
1450 Fsignal (Qfont, list2 (build_string ("Invalid font-spec"), font_spec));
1451
1452 if (! NILP (font_spec))
1453 {
1454 Lisp_Object encoding, repertory;
1455
1456 family = AREF (font_spec, FONT_FAMILY_INDEX);
1457 if (! NILP (family) )
1458 family = SYMBOL_NAME (family);
1459 registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1460 if (! NILP (registry))
1461 registry = Fdowncase (SYMBOL_NAME (registry));
1462 AUTO_STRING (dash, "-");
1463 encoding = find_font_encoding (concat3 (family, dash, registry));
1464 if (NILP (encoding))
1465 encoding = Qascii;
1466
1467 if (SYMBOLP (encoding))
1468 {
1469 CHECK_CHARSET (encoding);
1470 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1471 }
1472 else
1473 {
1474 repertory = XCDR (encoding);
1475 encoding = XCAR (encoding);
1476 CHECK_CHARSET (encoding);
1477 encoding = CHARSET_SYMBOL_ID (encoding);
1478 if (! NILP (repertory) && SYMBOLP (repertory))
1479 {
1480 CHECK_CHARSET (repertory);
1481 repertory = CHARSET_SYMBOL_ID (repertory);
1482 }
1483 }
1484 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1485 }
1486 else
1487 font_def = Qnil;
1488
1489 if (CHARACTERP (target))
1490 {
1491 if (XFASTINT (target) < 0x80)
1492 error ("Can't set a font for partial ASCII range");
1493 range_list = list1 (Fcons (target, target));
1494 }
1495 else if (CONSP (target))
1496 {
1497 Lisp_Object from, to;
1498
1499 from = Fcar (target);
1500 to = Fcdr (target);
1501 CHECK_CHARACTER (from);
1502 CHECK_CHARACTER (to);
1503 if (XFASTINT (from) < 0x80)
1504 {
1505 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1506 error ("Can't set a font for partial ASCII range");
1507 ascii_changed = 1;
1508 }
1509 range_list = list1 (target);
1510 }
1511 else if (SYMBOLP (target) && !NILP (target))
1512 {
1513 Lisp_Object script_list;
1514 Lisp_Object val;
1515
1516 range_list = Qnil;
1517 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1518 if (! NILP (Fmemq (target, script_list)))
1519 {
1520 if (EQ (target, Qlatin))
1521 ascii_changed = 1;
1522 val = list1 (target);
1523 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1524 val);
1525 range_list = Fnreverse (XCDR (val));
1526 }
1527 if (CHARSETP (target))
1528 {
1529 CHECK_CHARSET_GET_CHARSET (target, charset);
1530 if (charset->ascii_compatible_p)
1531 ascii_changed = 1;
1532 }
1533 else if (NILP (range_list))
1534 error ("Invalid script or charset name: %s",
1535 SDATA (SYMBOL_NAME (target)));
1536 }
1537 else if (NILP (target))
1538 range_list = list1 (Qnil);
1539 else
1540 error ("Invalid target for setting a font");
1541
1542 if (ascii_changed)
1543 {
1544 Lisp_Object val;
1545
1546 if (NILP (font_spec))
1547 error ("Can't set ASCII font to nil");
1548 val = CHAR_TABLE_REF (fontset, 0);
1549 if (! NILP (val) && EQ (add, Qappend))
1550 /* We are going to change just an additional font for ASCII. */
1551 ascii_changed = 0;
1552 }
1553
1554 if (charset)
1555 {
1556 Lisp_Object arg;
1557
1558 arg = make_uninit_vector (5);
1559 ASET (arg, 0, fontset);
1560 ASET (arg, 1, font_def);
1561 ASET (arg, 2, add);
1562 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1563 ASET (arg, 4, range_list);
1564
1565 map_charset_chars (set_fontset_font, Qnil, arg, charset,
1566 CHARSET_MIN_CODE (charset),
1567 CHARSET_MAX_CODE (charset));
1568 range_list = AREF (arg, 4);
1569 }
1570 for (; CONSP (range_list); range_list = XCDR (range_list))
1571 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1572
1573 if (ascii_changed)
1574 {
1575 Lisp_Object tail, fr;
1576 int fontset_id = XINT (FONTSET_ID (fontset));
1577
1578 set_fontset_ascii (fontset, fontname);
1579 name = FONTSET_NAME (fontset);
1580 FOR_EACH_FRAME (tail, fr)
1581 {
1582 struct frame *f = XFRAME (fr);
1583 Lisp_Object font_object;
1584 struct face *face;
1585
1586 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1587 continue;
1588 if (fontset_id != FRAME_FONTSET (f))
1589 continue;
1590 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1591 if (face)
1592 font_object = font_load_for_lface (f, face->lface, font_spec);
1593 else
1594 font_object = font_open_by_spec (f, font_spec);
1595 if (! NILP (font_object))
1596 {
1597 update_auto_fontset_alist (font_object, fontset);
1598 AUTO_FRAME_ARG (arg, Qfont, Fcons (name, font_object));
1599 Fmodify_frame_parameters (fr, arg);
1600 }
1601 }
1602 }
1603
1604 /* Free all realized fontsets whose base is FONTSET. This way, the
1605 specified character(s) are surely redisplayed by a correct
1606 font. */
1607 free_realized_fontsets (fontset);
1608
1609 return Qnil;
1610 }
1611
1612
1613 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1614 doc: /* Create a new fontset NAME from font information in FONTLIST.
1615
1616 FONTLIST is an alist of scripts vs the corresponding font specification list.
1617 Each element of FONTLIST has the form (SCRIPT FONT-SPEC ...), where a
1618 character of SCRIPT is displayed by a font that matches one of
1619 FONT-SPEC.
1620
1621 SCRIPT is a symbol that appears in the first extra slot of the
1622 char-table `char-script-table'.
1623
1624 FONT-SPEC is a vector, a cons, or a string. See the documentation of
1625 `set-fontset-font' for the meaning. */)
1626 (Lisp_Object name, Lisp_Object fontlist)
1627 {
1628 Lisp_Object fontset;
1629 int id;
1630
1631 CHECK_STRING (name);
1632 CHECK_LIST (fontlist);
1633
1634 name = Fdowncase (name);
1635 id = fs_query_fontset (name, 0);
1636 if (id < 0)
1637 {
1638 Lisp_Object font_spec = Ffont_spec (0, NULL);
1639 Lisp_Object short_name;
1640 char xlfd[256];
1641 int len;
1642
1643 if (font_parse_xlfd (SSDATA (name), SBYTES (name), font_spec) < 0)
1644 error ("Fontset name must be in XLFD format");
1645 short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
1646 if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
1647 || SBYTES (SYMBOL_NAME (short_name)) < 9)
1648 error ("Registry field of fontset name must be \"fontset-*\"");
1649 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (short_name)),
1650 Vfontset_alias_alist);
1651 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso8859_1);
1652 fontset = make_fontset (Qnil, name, Qnil);
1653 len = font_unparse_xlfd (font_spec, 0, xlfd, 256);
1654 if (len < 0)
1655 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1656 set_fontset_ascii (fontset, make_unibyte_string (xlfd, len));
1657 }
1658 else
1659 {
1660 fontset = FONTSET_FROM_ID (id);
1661 free_realized_fontsets (fontset);
1662 Fset_char_table_range (fontset, Qt, Qnil);
1663 }
1664
1665 for (; CONSP (fontlist); fontlist = XCDR (fontlist))
1666 {
1667 Lisp_Object elt, script;
1668
1669 elt = XCAR (fontlist);
1670 script = Fcar (elt);
1671 elt = Fcdr (elt);
1672 if (CONSP (elt) && (NILP (XCDR (elt)) || CONSP (XCDR (elt))))
1673 for (; CONSP (elt); elt = XCDR (elt))
1674 Fset_fontset_font (name, script, XCAR (elt), Qnil, Qappend);
1675 else
1676 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1677 }
1678 return name;
1679 }
1680
1681
1682 /* Alist of automatically created fontsets. Each element is a cons
1683 (FONT-SPEC . FONTSET-ID). */
1684 static Lisp_Object auto_fontset_alist;
1685
1686 /* Number of automatically created fontsets. */
1687 static ptrdiff_t num_auto_fontsets;
1688
1689 /* Return a fontset synthesized from FONT-OBJECT. This is called from
1690 x_new_font when FONT-OBJECT is used for the default ASCII font of a
1691 frame, and the returned fontset is used for the default fontset of
1692 that frame. The fontset specifies a font of the same registry as
1693 FONT-OBJECT for all characters in the repertory of the registry
1694 (see Vfont_encoding_alist). If the repertory is not known, the
1695 fontset specifies the font for all Latin characters assuming that a
1696 user intends to use FONT-OBJECT for Latin characters. */
1697
1698 int
1699 fontset_from_font (Lisp_Object font_object)
1700 {
1701 Lisp_Object font_name = font_get_name (font_object);
1702 Lisp_Object font_spec = copy_font_spec (font_object);
1703 Lisp_Object registry = AREF (font_spec, FONT_REGISTRY_INDEX);
1704 Lisp_Object fontset_spec, alias, name, fontset;
1705 Lisp_Object val;
1706
1707 val = assoc_no_quit (font_spec, auto_fontset_alist);
1708 if (CONSP (val))
1709 return XINT (FONTSET_ID (XCDR (val)));
1710 if (num_auto_fontsets++ == 0)
1711 alias = intern ("fontset-startup");
1712 else
1713 {
1714 char temp[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1715
1716 sprintf (temp, "fontset-auto%"pD"d", num_auto_fontsets - 1);
1717 alias = intern (temp);
1718 }
1719 fontset_spec = copy_font_spec (font_spec);
1720 ASET (fontset_spec, FONT_REGISTRY_INDEX, alias);
1721 name = Ffont_xlfd_name (fontset_spec, Qnil);
1722 eassert (!NILP (name));
1723 fontset = make_fontset (Qnil, name, Qnil);
1724 Vfontset_alias_alist = Fcons (Fcons (name, SYMBOL_NAME (alias)),
1725 Vfontset_alias_alist);
1726 alias = Fdowncase (AREF (font_object, FONT_NAME_INDEX));
1727 Vfontset_alias_alist = Fcons (Fcons (name, alias), Vfontset_alias_alist);
1728 auto_fontset_alist = Fcons (Fcons (font_spec, fontset), auto_fontset_alist);
1729 font_spec = Ffont_spec (0, NULL);
1730 ASET (font_spec, FONT_REGISTRY_INDEX, registry);
1731 {
1732 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1733
1734 if (CONSP (target))
1735 target = XCDR (target);
1736 if (! CHARSETP (target))
1737 target = Qlatin;
1738 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1739 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1740 }
1741
1742 set_fontset_ascii (fontset, font_name);
1743
1744 return XINT (FONTSET_ID (fontset));
1745 }
1746
1747
1748 /* Update auto_fontset_alist for FONTSET. When an ASCII font of
1749 FONTSET is changed, we delete an entry of FONTSET if any from
1750 auto_fontset_alist so that FONTSET is not re-used by
1751 fontset_from_font. */
1752
1753 static void
1754 update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
1755 {
1756 Lisp_Object prev, tail;
1757
1758 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1759 prev = tail, tail = XCDR (tail))
1760 if (EQ (fontset, XCDR (XCAR (tail))))
1761 {
1762 if (NILP (prev))
1763 auto_fontset_alist = XCDR (tail);
1764 else
1765 XSETCDR (prev, XCDR (tail));
1766 break;
1767 }
1768 }
1769
1770
1771 /* Return a cons (FONT-OBJECT . GLYPH-CODE).
1772 FONT-OBJECT is the font for the character at POSITION in the current
1773 buffer. This is computed from all the text properties and overlays
1774 that apply to POSITION. POSITION may be nil, in which case,
1775 FONT-SPEC is the font for displaying the character CH with the
1776 default face.
1777
1778 GLYPH-CODE is the glyph code in the font to use for the character.
1779
1780 If the 2nd optional arg CH is non-nil, it is a character to check
1781 the font instead of the character at POSITION.
1782
1783 It returns nil in the following cases:
1784
1785 (1) The window system doesn't have a font for the character (thus
1786 it is displayed by an empty box).
1787
1788 (2) The character code is invalid.
1789
1790 (3) If POSITION is not nil, and the current buffer is not displayed
1791 in any window.
1792
1793 In addition, the returned font name may not take into account of
1794 such redisplay engine hooks as what used in jit-lock-mode if
1795 POSITION is currently not visible. */
1796
1797
1798 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
1799 doc: /* For internal use only. */)
1800 (Lisp_Object position, Lisp_Object ch)
1801 {
1802 ptrdiff_t pos, pos_byte, dummy;
1803 int face_id;
1804 int c;
1805 struct frame *f;
1806 struct face *face;
1807
1808 if (NILP (position))
1809 {
1810 CHECK_CHARACTER (ch);
1811 c = XINT (ch);
1812 f = XFRAME (selected_frame);
1813 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1814 pos = -1;
1815 }
1816 else
1817 {
1818 Lisp_Object window;
1819 struct window *w;
1820
1821 CHECK_NUMBER_COERCE_MARKER (position);
1822 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
1823 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1824 pos = XINT (position);
1825 pos_byte = CHAR_TO_BYTE (pos);
1826 if (NILP (ch))
1827 c = FETCH_CHAR (pos_byte);
1828 else
1829 {
1830 CHECK_NATNUM (ch);
1831 c = XINT (ch);
1832 }
1833 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1834 if (NILP (window))
1835 return Qnil;
1836 w = XWINDOW (window);
1837 f = XFRAME (w->frame);
1838 face_id = face_at_buffer_position (w, pos, &dummy,
1839 pos + 100, 0, -1);
1840 }
1841 if (! CHAR_VALID_P (c))
1842 return Qnil;
1843 if (!FRAME_WINDOW_P (f))
1844 return Qnil;
1845 /* We need the basic faces to be valid below, so recompute them if
1846 some code just happened to clear the face cache. */
1847 if (FRAME_FACE_CACHE (f)->used == 0)
1848 recompute_basic_faces (f);
1849 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
1850 face = FACE_FROM_ID (f, face_id);
1851 if (face->font)
1852 {
1853 unsigned code = face->font->driver->encode_char (face->font, c);
1854 Lisp_Object font_object;
1855
1856 if (code == FONT_INVALID_CODE)
1857 return Qnil;
1858 XSETFONT (font_object, face->font);
1859 return Fcons (font_object, INTEGER_TO_CONS (code));
1860 }
1861 return Qnil;
1862 }
1863
1864
1865 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1866 doc: /* Return information about a fontset FONTSET on frame FRAME.
1867
1868 FONTSET is a fontset name string, nil for the fontset of FRAME, or t
1869 for the default fontset. FRAME nil means the selected frame.
1870
1871 The value is a char-table whose elements have this form:
1872
1873 ((FONT OPENED-FONT ...) ...)
1874
1875 FONT is a name of font specified for a range of characters.
1876
1877 OPENED-FONT is a name of a font actually opened.
1878
1879 The char-table has one extra slot. If FONTSET is not the default
1880 fontset, the value the extra slot is a char-table containing the
1881 information about the derived fonts from the default fontset. The
1882 format is the same as above. */)
1883 (Lisp_Object fontset, Lisp_Object frame)
1884 {
1885 Lisp_Object *realized[2], fontsets[2], tables[2];
1886 Lisp_Object val, elt;
1887 int c, i, j, k;
1888
1889 check_window_system (NULL);
1890 fontset = check_fontset_name (fontset, &frame);
1891
1892 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1893 in the table `realized'. */
1894 USE_SAFE_ALLOCA;
1895 SAFE_ALLOCA_LISP (realized[0], 2 * ASIZE (Vfontset_table));
1896 realized[1] = realized[0] + ASIZE (Vfontset_table);
1897 for (i = j = 0; i < ASIZE (Vfontset_table); i++)
1898 {
1899 elt = FONTSET_FROM_ID (i);
1900 if (!NILP (elt)
1901 && EQ (FONTSET_BASE (elt), fontset)
1902 && EQ (FONTSET_FRAME (elt), frame))
1903 realized[0][j++] = elt;
1904 }
1905 realized[0][j] = Qnil;
1906
1907 for (i = j = 0; ! NILP (realized[0][i]); i++)
1908 {
1909 elt = FONTSET_DEFAULT (realized[0][i]);
1910 if (! NILP (elt))
1911 realized[1][j++] = elt;
1912 }
1913 realized[1][j] = Qnil;
1914
1915 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1916 fontsets[0] = fontset;
1917 if (!EQ (fontset, Vdefault_fontset))
1918 {
1919 tables[1] = Fmake_char_table (Qnil, Qnil);
1920 set_char_table_extras (tables[0], 0, tables[1]);
1921 fontsets[1] = Vdefault_fontset;
1922 }
1923
1924 /* Accumulate information of the fontset in TABLE. The format of
1925 each element is ((FONT-SPEC OPENED-FONT ...) ...). */
1926 for (k = 0; k <= 1; k++)
1927 {
1928 for (c = 0; c <= MAX_CHAR; )
1929 {
1930 int from = c, to = MAX_5_BYTE_CHAR;
1931
1932 if (c <= MAX_5_BYTE_CHAR)
1933 {
1934 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1935 }
1936 else
1937 {
1938 val = FONTSET_FALLBACK (fontsets[k]);
1939 to = MAX_CHAR;
1940 }
1941 if (VECTORP (val))
1942 {
1943 Lisp_Object alist;
1944
1945 /* At first, set ALIST to ((FONT-SPEC) ...). */
1946 for (alist = Qnil, i = 0; i < ASIZE (val); i++)
1947 if (! NILP (AREF (val, i)))
1948 alist = Fcons (Fcons (FONT_DEF_SPEC (AREF (val, i)), Qnil),
1949 alist);
1950 alist = Fnreverse (alist);
1951
1952 /* Then store opened font names to cdr of each elements. */
1953 for (i = 0; ! NILP (realized[k][i]); i++)
1954 {
1955 if (c <= MAX_5_BYTE_CHAR)
1956 val = FONTSET_REF (realized[k][i], c);
1957 else
1958 val = FONTSET_FALLBACK (realized[k][i]);
1959 if (! CONSP (val) || ! VECTORP (XCDR (val)))
1960 continue;
1961 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1962 val = XCDR (val);
1963 for (j = 0; j < ASIZE (val); j++)
1964 {
1965 elt = AREF (val, j);
1966 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1967 {
1968 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1969 Lisp_Object slot, name;
1970
1971 slot = Fassq (RFONT_DEF_SPEC (elt), alist);
1972 name = AREF (font_object, FONT_NAME_INDEX);
1973 if (NILP (Fmember (name, XCDR (slot))))
1974 nconc2 (slot, list1 (name));
1975 }
1976 }
1977 }
1978
1979 /* Store ALIST in TBL for characters C..TO. */
1980 if (c <= MAX_5_BYTE_CHAR)
1981 char_table_set_range (tables[k], c, to, alist);
1982 else
1983 set_char_table_defalt (tables[k], alist);
1984
1985 /* At last, change each elements to font names. */
1986 for (; CONSP (alist); alist = XCDR (alist))
1987 {
1988 elt = XCAR (alist);
1989 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
1990 }
1991 }
1992 c = to + 1;
1993 }
1994 if (EQ (fontset, Vdefault_fontset))
1995 break;
1996 }
1997
1998 SAFE_FREE ();
1999 return tables[0];
2000 }
2001
2002
2003 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 3, 0,
2004 doc: /* Return a font name pattern for character CH in fontset NAME.
2005 If NAME is t, find a pattern in the default fontset.
2006 If NAME is nil, find a pattern in the fontset of the selected frame.
2007
2008 The value has the form (FAMILY . REGISTRY), where FAMILY is a font
2009 family name and REGISTRY is a font registry name. This is actually
2010 the first font name pattern for CH in the fontset or in the default
2011 fontset.
2012
2013 If the 2nd optional arg ALL is non-nil, return a list of all font name
2014 patterns. */)
2015 (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
2016 {
2017 int c;
2018 Lisp_Object fontset, elt, list, repertory, val;
2019 int i, j;
2020 Lisp_Object frame;
2021
2022 frame = Qnil;
2023 fontset = check_fontset_name (name, &frame);
2024
2025 CHECK_CHARACTER (ch);
2026 c = XINT (ch);
2027 list = Qnil;
2028 while (1)
2029 {
2030 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2031 i++, elt = FONTSET_FALLBACK (fontset))
2032 if (VECTORP (elt))
2033 for (j = 0; j < ASIZE (elt); j++)
2034 {
2035 Lisp_Object family, registry;
2036
2037 val = AREF (elt, j);
2038 if (NILP (val))
2039 return Qnil;
2040 repertory = AREF (val, 1);
2041 if (INTEGERP (repertory))
2042 {
2043 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2044
2045 if (! CHAR_CHARSET_P (c, charset))
2046 continue;
2047 }
2048 else if (CHAR_TABLE_P (repertory))
2049 {
2050 if (NILP (CHAR_TABLE_REF (repertory, c)))
2051 continue;
2052 }
2053 val = AREF (val, 0);
2054 /* VAL is a FONT-SPEC */
2055 family = AREF (val, FONT_FAMILY_INDEX);
2056 if (! NILP (family))
2057 family = SYMBOL_NAME (family);
2058 registry = AREF (val, FONT_REGISTRY_INDEX);
2059 if (! NILP (registry))
2060 registry = SYMBOL_NAME (registry);
2061 val = Fcons (family, registry);
2062 if (NILP (all))
2063 return val;
2064 list = Fcons (val, list);
2065 }
2066 if (EQ (fontset, Vdefault_fontset))
2067 break;
2068 fontset = Vdefault_fontset;
2069 }
2070 return (Fnreverse (list));
2071 }
2072
2073 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2074 doc: /* Return a list of all defined fontset names. */)
2075 (void)
2076 {
2077 Lisp_Object fontset, list;
2078 int i;
2079
2080 list = Qnil;
2081 for (i = 0; i < ASIZE (Vfontset_table); i++)
2082 {
2083 fontset = FONTSET_FROM_ID (i);
2084 if (!NILP (fontset)
2085 && BASE_FONTSET_P (fontset))
2086 list = Fcons (FONTSET_NAME (fontset), list);
2087 }
2088
2089 return list;
2090 }
2091
2092
2093 #ifdef ENABLE_CHECKING
2094
2095 Lisp_Object dump_fontset (Lisp_Object) EXTERNALLY_VISIBLE;
2096
2097 Lisp_Object
2098 dump_fontset (Lisp_Object fontset)
2099 {
2100 Lisp_Object vec;
2101
2102 vec = Fmake_vector (make_number (3), Qnil);
2103 ASET (vec, 0, FONTSET_ID (fontset));
2104
2105 if (BASE_FONTSET_P (fontset))
2106 {
2107 ASET (vec, 1, FONTSET_NAME (fontset));
2108 }
2109 else
2110 {
2111 Lisp_Object frame;
2112
2113 frame = FONTSET_FRAME (fontset);
2114 if (FRAMEP (frame))
2115 {
2116 struct frame *f = XFRAME (frame);
2117
2118 if (FRAME_LIVE_P (f))
2119 ASET (vec, 1,
2120 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)),
2121 f->name));
2122 else
2123 ASET (vec, 1,
2124 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2125 }
2126 if (!NILP (FONTSET_DEFAULT (fontset)))
2127 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2128 }
2129 return vec;
2130 }
2131
2132 DEFUN ("fontset-list-all", Ffontset_list_all, Sfontset_list_all, 0, 0, 0,
2133 doc: /* Return a brief summary of all fontsets for debug use. */)
2134 (void)
2135 {
2136 Lisp_Object val;
2137 int i;
2138
2139 for (i = 0, val = Qnil; i < ASIZE (Vfontset_table); i++)
2140 if (! NILP (AREF (Vfontset_table, i)))
2141 val = Fcons (dump_fontset (AREF (Vfontset_table, i)), val);
2142 return (Fnreverse (val));
2143 }
2144 #endif /* ENABLE_CHECKING */
2145
2146 void
2147 syms_of_fontset (void)
2148 {
2149 DEFSYM (Qfontset, "fontset");
2150 Fput (Qfontset, Qchar_table_extra_slots, make_number (8));
2151 DEFSYM (Qfontset_info, "fontset-info");
2152 Fput (Qfontset_info, Qchar_table_extra_slots, make_number (1));
2153
2154 DEFSYM (Qprepend, "prepend");
2155 DEFSYM (Qappend, "append");
2156 DEFSYM (Qlatin, "latin");
2157
2158 Vcached_fontset_data = Qnil;
2159 staticpro (&Vcached_fontset_data);
2160
2161 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2162 staticpro (&Vfontset_table);
2163
2164 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2165 staticpro (&Vdefault_fontset);
2166 set_fontset_id (Vdefault_fontset, make_number (0));
2167 set_fontset_name
2168 (Vdefault_fontset,
2169 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2170 ASET (Vfontset_table, 0, Vdefault_fontset);
2171 next_fontset_id = 1;
2172
2173 auto_fontset_alist = Qnil;
2174 staticpro (&auto_fontset_alist);
2175
2176 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist,
2177 doc: /*
2178 Alist of charsets vs the charsets to determine the preferred font encoding.
2179 Each element looks like (CHARSET . ENCODING-CHARSET),
2180 where ENCODING-CHARSET is a charset registered in the variable
2181 `font-encoding-alist' as ENCODING.
2182
2183 When a text has a property `charset' and the value is CHARSET, a font
2184 whose encoding corresponds to ENCODING-CHARSET is preferred. */);
2185 Vfont_encoding_charset_alist = Qnil;
2186
2187 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent,
2188 doc: /*
2189 Char table of characters whose ascent values should be ignored.
2190 If an entry for a character is non-nil, the ascent value of the glyph
2191 is assumed to be specified by _MULE_DEFAULT_ASCENT property of a font.
2192
2193 This affects how a composite character which contains
2194 such a character is displayed on screen. */);
2195 Vuse_default_ascent = Qnil;
2196
2197 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition,
2198 doc: /*
2199 Char table of characters which are not composed relatively.
2200 If an entry for a character is non-nil, a composition sequence
2201 which contains that character is displayed so that
2202 the glyph of that character is put without considering
2203 an ascent and descent value of a previous character. */);
2204 Vignore_relative_composition = Qnil;
2205
2206 DEFVAR_LISP ("alternate-fontname-alist", Valternate_fontname_alist,
2207 doc: /* Alist of fontname vs list of the alternate fontnames.
2208 When a specified font name is not found, the corresponding
2209 alternate fontnames (if any) are tried instead. */);
2210 Valternate_fontname_alist = Qnil;
2211
2212 DEFVAR_LISP ("fontset-alias-alist", Vfontset_alias_alist,
2213 doc: /* Alist of fontset names vs the aliases. */);
2214 Vfontset_alias_alist
2215 = list1 (Fcons (FONTSET_NAME (Vdefault_fontset),
2216 build_pure_c_string ("fontset-default")));
2217
2218 DEFVAR_LISP ("vertical-centering-font-regexp",
2219 Vvertical_centering_font_regexp,
2220 doc: /* Regexp matching font names that require vertical centering on display.
2221 When a character is displayed with such fonts, the character is displayed
2222 at the vertical center of lines. */);
2223 Vvertical_centering_font_regexp = Qnil;
2224
2225 DEFVAR_LISP ("otf-script-alist", Votf_script_alist,
2226 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2227 Votf_script_alist = Qnil;
2228
2229 defsubr (&Squery_fontset);
2230 defsubr (&Snew_fontset);
2231 defsubr (&Sset_fontset_font);
2232 defsubr (&Sinternal_char_font);
2233 defsubr (&Sfontset_info);
2234 defsubr (&Sfontset_font);
2235 defsubr (&Sfontset_list);
2236 #ifdef ENABLE_CHECKING
2237 defsubr (&Sfontset_list_all);
2238 #endif
2239 }