]> code.delx.au - gnu-emacs/blob - src/fontset.c
Compute C decls for DEFSYMs automatically
[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 /* Vector containing all fontsets. */
156 static Lisp_Object Vfontset_table;
157
158 /* Next possibly free fontset ID. Usually this keeps the minimum
159 fontset ID not yet used. */
160 static int next_fontset_id;
161
162 /* The default fontset. This gives default FAMILY and REGISTRY of
163 font for each character. */
164 static Lisp_Object Vdefault_fontset;
165
166 /* Prototype declarations for static functions. */
167 static Lisp_Object make_fontset (Lisp_Object, Lisp_Object, Lisp_Object);
168
169 /* Return true if ID is a valid fontset id.
170 Optimized away if ENABLE_CHECKING is not defined. */
171
172 static bool
173 fontset_id_valid_p (int id)
174 {
175 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
176 }
177
178
179 \f
180 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
181
182 /* Return the fontset with ID. No check of ID's validness. */
183 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
184
185 /* Access special values of FONTSET. */
186
187 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
188 static void
189 set_fontset_id (Lisp_Object fontset, Lisp_Object id)
190 {
191 set_char_table_extras (fontset, 0, id);
192 }
193
194 /* Access special values of (base) FONTSET. */
195
196 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
197 static void
198 set_fontset_name (Lisp_Object fontset, Lisp_Object name)
199 {
200 set_char_table_extras (fontset, 1, name);
201 }
202
203 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[2]
204 static void
205 set_fontset_ascii (Lisp_Object fontset, Lisp_Object ascii)
206 {
207 set_char_table_extras (fontset, 2, ascii);
208 }
209
210 /* Access special values of (realized) FONTSET. */
211
212 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[3]
213 static void
214 set_fontset_base (Lisp_Object fontset, Lisp_Object base)
215 {
216 set_char_table_extras (fontset, 3, base);
217 }
218
219 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[4]
220 static void
221 set_fontset_frame (Lisp_Object fontset, Lisp_Object frame)
222 {
223 set_char_table_extras (fontset, 4, frame);
224 }
225
226 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
227 static void
228 set_fontset_nofont_face (Lisp_Object fontset, Lisp_Object face)
229 {
230 set_char_table_extras (fontset, 5, face);
231 }
232
233 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[6]
234 static void
235 set_fontset_default (Lisp_Object fontset, Lisp_Object def)
236 {
237 set_char_table_extras (fontset, 6, def);
238 }
239
240 /* For both base and realized fontset. */
241
242 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[7]
243 static void
244 set_fontset_fallback (Lisp_Object fontset, Lisp_Object fallback)
245 {
246 set_char_table_extras (fontset, 7, fallback);
247 }
248
249 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
250
251 /* Macros for FONT-DEF and RFONT-DEF of fontset. */
252 #define FONT_DEF_NEW(font_def, font_spec, encoding, repertory) \
253 do { \
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); \
258 } while (0)
259
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)
263
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
275 preferable. */
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) \
280 do { \
281 (rfont_def) = Fmake_vector (make_number (4), Qnil); \
282 ASET ((rfont_def), 1, (font_def)); \
283 RFONT_DEF_SET_SCORE ((rfont_def), 0); \
284 } while (0)
285
286
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
290 fontset. */
291
292 #define FONTSET_REF(fontset, c) \
293 (EQ (fontset, Vdefault_fontset) \
294 ? CHAR_TABLE_REF (fontset, c) \
295 : fontset_ref ((fontset), (c)))
296
297 static Lisp_Object
298 fontset_ref (Lisp_Object fontset, int c)
299 {
300 Lisp_Object elt;
301
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);
307 return elt;
308 }
309
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. */
313
314 #define FONTSET_SET(fontset, range, elt) \
315 Fset_char_table_range ((fontset), (range), (elt))
316
317
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,
322 append ELT. */
323
324 #define FONTSET_ADD(fontset, range, elt, add) \
325 (NILP (add) \
326 ? (NILP (range) \
327 ? (set_fontset_fallback \
328 (fontset, Fmake_vector (make_number (1), (elt)))) \
329 : ((void) \
330 Fset_char_table_range (fontset, range, \
331 Fmake_vector (make_number (1), elt)))) \
332 : fontset_add ((fontset), (range), (elt), (add)))
333
334 static void
335 fontset_add (Lisp_Object fontset, Lisp_Object range, Lisp_Object elt, Lisp_Object add)
336 {
337 Lisp_Object args[2];
338 int idx = (EQ (add, Qappend) ? 0 : 1);
339
340 args[1 - idx] = Fmake_vector (make_number (1), elt);
341
342 if (CONSP (range))
343 {
344 int from = XINT (XCAR (range));
345 int to = XINT (XCDR (range));
346 int from1, to1;
347
348 do {
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));
354 from = to1 + 1;
355 } while (from < to);
356 }
357 else
358 {
359 args[idx] = FONTSET_FALLBACK (fontset);
360 set_fontset_fallback
361 (fontset, NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args));
362 }
363 }
364
365 static int
366 fontset_compare_rfontdef (const void *val1, const void *val2)
367 {
368 return (RFONT_DEF_SCORE (*(Lisp_Object *) val1)
369 - RFONT_DEF_SCORE (*(Lisp_Object *) val2));
370 }
371
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.
377
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. */
380
381 static void
382 reorder_font_vector (Lisp_Object font_group, struct font *font)
383 {
384 Lisp_Object vec, font_object;
385 int size;
386 int i;
387 bool score_changed = false;
388
389 if (font)
390 XSETFONT (font_object, font);
391 else
392 font_object = Qnil;
393
394 vec = XCDR (font_group);
395 size = ASIZE (vec);
396 /* Exclude the tailing nil element from the reordering. */
397 if (NILP (AREF (vec, size - 1)))
398 size--;
399
400 for (i = 0; i < size; i++)
401 {
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);
407
408 if (! NILP (otf_spec))
409 /* A font-spec with :otf is preferable regardless of encoding
410 and language.. */
411 ;
412 else if (! font_match_p (font_spec, font_object))
413 {
414 Lisp_Object encoding = FONT_DEF_ENCODING (font_def);
415
416 if (! NILP (encoding))
417 {
418 Lisp_Object tail;
419
420 for (tail = Vcharset_ordered_list;
421 ! EQ (tail, Vcharset_non_preferred_head) && CONSP (tail);
422 tail = XCDR (tail))
423 if (EQ (encoding, XCAR (tail)))
424 break;
425 else if (score <= min (INT_MAX, MOST_POSITIVE_FIXNUM) - 0x100)
426 score += 0x100;
427 }
428 else
429 {
430 Lisp_Object lang = Ffont_get (font_spec, QClang);
431
432 if (! NILP (lang)
433 && ! EQ (lang, Vcurrent_iso639_language)
434 && (! CONSP (Vcurrent_iso639_language)
435 || NILP (Fmemq (lang, Vcurrent_iso639_language))))
436 score |= 0x100;
437 }
438 }
439 if (RFONT_DEF_SCORE (rfont_def) != score)
440 {
441 RFONT_DEF_SET_SCORE (rfont_def, score);
442 score_changed = true;
443 }
444 }
445
446 if (score_changed)
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));
451 }
452
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). */
458
459 static Lisp_Object
460 fontset_get_font_group (Lisp_Object fontset, int c)
461 {
462 Lisp_Object font_group;
463 Lisp_Object base_fontset;
464 int from = 0, to = MAX_CHAR, i;
465
466 eassert (! BASE_FONTSET_P (fontset));
467 if (c >= 0)
468 font_group = CHAR_TABLE_REF (fontset, c);
469 else
470 font_group = FONTSET_FALLBACK (fontset);
471 if (! NILP (font_group))
472 return font_group;
473 base_fontset = FONTSET_BASE (fontset);
474 if (NILP (base_fontset))
475 font_group = Qnil;
476 else if (c >= 0)
477 font_group = char_table_ref_and_range (base_fontset, c, &from, &to);
478 else
479 font_group = FONTSET_FALLBACK (base_fontset);
480 if (NILP (font_group))
481 {
482 font_group = make_number (0);
483 if (c >= 0)
484 char_table_set_range (fontset, from, to, font_group);
485 return font_group;
486 }
487 if (!VECTORP (font_group))
488 return 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)))
492 {
493 Lisp_Object rfont_def;
494
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);
499 }
500 font_group = Fcons (make_number (-1), font_group);
501 if (c >= 0)
502 char_table_set_range (fontset, from, to, font_group);
503 else
504 set_fontset_fallback (fontset, font_group);
505 return font_group;
506 }
507
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.
512
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).
515
516 ID is a charset-id that must be preferred, or -1 meaning no
517 preference.
518
519 If FALLBACK, search only fallback fonts. */
520
521 static Lisp_Object
522 fontset_find_font (Lisp_Object fontset, int c, struct face *face, int id,
523 bool fallback)
524 {
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;
531
532 font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
533 if (! CONSP (font_group))
534 return font_group;
535 vec = XCDR (font_group);
536 if (ASIZE (vec) == 0)
537 return Qnil;
538
539 if (ASIZE (vec) > 1)
540 {
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);
545 if (id >= 0)
546 /* Find a spec matching with the charset ID to try at
547 first. */
548 for (i = 0; i < ASIZE (vec); i++)
549 {
550 Lisp_Object repertory;
551
552 rfont_def = AREF (vec, i);
553 if (NILP (rfont_def))
554 break;
555 repertory = FONT_DEF_REPERTORY (RFONT_DEF_FONT_DEF (rfont_def));
556
557 if (XINT (repertory) == id)
558 {
559 charset_matched = i;
560 break;
561 }
562 }
563 }
564
565 /* Find the first available font in the vector of RFONT-DEF. */
566 for (i = 0; i < ASIZE (vec); i++)
567 {
568 Lisp_Object font_def;
569 Lisp_Object font_entity, font_object;
570
571 found_index = i;
572 if (i == 0)
573 {
574 if (charset_matched > 0)
575 {
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
579 next loop. */
580 charset_matched = - charset_matched;
581 /* We must try the first element in the next loop. */
582 i--;
583 }
584 }
585 else if (i == - charset_matched)
586 {
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++)
593 {
594 rfont_def = AREF (vec, i + 1);
595 if (NILP (rfont_def))
596 break;
597 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
598 break;
599 }
600 continue;
601 }
602
603 rfont_def = AREF (vec, found_index);
604 if (NILP (rfont_def))
605 {
606 if (i < 0)
607 continue;
608 /* This is a sign of not to try the other fonts. */
609 return Qt;
610 }
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. */
614 continue;
615
616 font_object = RFONT_DEF_OBJECT (rfont_def);
617 if (NILP (font_object))
618 {
619 font_def = RFONT_DEF_FONT_DEF (rfont_def);
620
621 if (! face)
622 /* We have not yet opened the font. */
623 return Qnil;
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))
631 {
632 /* Record that no font matches the spec. */
633 RFONT_DEF_SET_FACE (rfont_def, -1);
634 continue;
635 }
636 font_object = font_open_for_lface (f, font_entity, face->lface,
637 FONT_DEF_SPEC (font_def));
638 if (NILP (font_object))
639 {
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
644 have such an API. */
645 RFONT_DEF_SET_FACE (rfont_def, -1);
646 continue;
647 }
648 RFONT_DEF_SET_OBJECT (rfont_def, font_object);
649 }
650
651 if (font_has_char (f, font_object, c))
652 goto found;
653
654 /* Find a font already opened, matching with the current spec,
655 and supporting C. */
656 font_def = RFONT_DEF_FONT_DEF (rfont_def);
657 for (; found_index + 1 < ASIZE (vec); found_index++)
658 {
659 rfont_def = AREF (vec, found_index + 1);
660 if (NILP (rfont_def))
661 break;
662 if (! EQ (RFONT_DEF_FONT_DEF (rfont_def), font_def))
663 break;
664 font_object = RFONT_DEF_OBJECT (rfont_def);
665 if (! NILP (font_object) && font_has_char (f, font_object, c))
666 {
667 found_index++;
668 goto found;
669 }
670 }
671
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))
676 {
677 /* We found a font. Open it and insert a new element for
678 that font in VEC. */
679 Lisp_Object new_vec;
680 int j;
681
682 font_object = font_open_for_lface (f, font_entity, face->lface,
683 Qnil);
684 if (NILP (font_object))
685 continue;
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);
690 found_index++;
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);
697 vec = new_vec;
698 goto found;
699 }
700 if (i >= 0)
701 i = found_index;
702 }
703
704 FONTSET_SET (fontset, make_number (c), make_number (0));
705 return Qnil;
706
707 found:
708 if (fallback && found_index > 0)
709 {
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
713 quickly. */
714 for (i = found_index; i > 0; i--)
715 ASET (vec, i, AREF (vec, i - 1));
716 ASET (vec, 0, rfont_def);
717 }
718 return rfont_def;
719 }
720
721
722 static Lisp_Object
723 fontset_font (Lisp_Object fontset, int c, struct face *face, int id)
724 {
725 Lisp_Object rfont_def, default_rfont_def IF_LINT (= Qnil);
726 Lisp_Object base_fontset;
727
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))
732 return rfont_def;
733 if (NILP (rfont_def))
734 FONTSET_SET (fontset, make_number (c), make_number (0));
735
736 /* Try a font-group of the default fontset. */
737 base_fontset = FONTSET_BASE (fontset);
738 if (! EQ (base_fontset, Vdefault_fontset))
739 {
740 if (NILP (FONTSET_DEFAULT (fontset)))
741 set_fontset_default
742 (fontset,
743 make_fontset (FONTSET_FRAME (fontset), Qnil, Vdefault_fontset));
744 FONT_DEFERRED_LOG ("default fontset: font for", make_number (c), Qnil);
745 default_rfont_def
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),
751 make_number (0));
752 }
753
754 /* Try a fallback font-group of FONTSET. */
755 if (! EQ (rfont_def, Qt))
756 {
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))
760 return rfont_def;
761 /* Remember that FONTSET has no font for C. */
762 FONTSET_SET (fontset, make_number (c), Qt);
763 }
764
765 /* Try a fallback font-group of the default fontset. */
766 if (! EQ (base_fontset, Vdefault_fontset)
767 && ! EQ (default_rfont_def, Qt))
768 {
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))
772 return rfont_def;
773 /* Remember that the default fontset has no font for C. */
774 FONTSET_SET (FONTSET_DEFAULT (fontset), make_number (c), Qt);
775 }
776
777 return Qnil;
778 }
779
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
782 BASE. */
783
784 static Lisp_Object
785 make_fontset (Lisp_Object frame, Lisp_Object name, Lisp_Object base)
786 {
787 Lisp_Object fontset;
788 int size = ASIZE (Vfontset_table);
789 int id = next_fontset_id;
790
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
795 id. */
796 while (!NILP (AREF (Vfontset_table, id))) id++;
797
798 if (id + 1 == size)
799 Vfontset_table = larger_vector (Vfontset_table, 1, -1);
800
801 fontset = Fmake_char_table (Qfontset, Qnil);
802
803 set_fontset_id (fontset, make_number (id));
804 if (NILP (base))
805 set_fontset_name (fontset, name);
806 else
807 {
808 set_fontset_name (fontset, Qnil);
809 set_fontset_frame (fontset, frame);
810 set_fontset_base (fontset, base);
811 }
812
813 ASET (Vfontset_table, id, fontset);
814 next_fontset_id = id + 1;
815 return fontset;
816 }
817
818 \f
819 /********** INTERFACES TO xfaces.c, xfns.c, and dispextern.h **********/
820
821 /* Return the name of the fontset who has ID. */
822
823 Lisp_Object
824 fontset_name (int id)
825 {
826 Lisp_Object fontset;
827
828 fontset = FONTSET_FROM_ID (id);
829 return FONTSET_NAME (fontset);
830 }
831
832
833 /* Return the ASCII font name of the fontset who has ID. */
834
835 Lisp_Object
836 fontset_ascii (int id)
837 {
838 Lisp_Object fontset, elt;
839
840 fontset= FONTSET_FROM_ID (id);
841 elt = FONTSET_ASCII (fontset);
842 if (CONSP (elt))
843 elt = XCAR (elt);
844 return elt;
845 }
846
847 /* Free fontset of FACE defined on frame F. Called from
848 free_realized_face. */
849
850 void
851 free_face_fontset (struct frame *f, struct face *face)
852 {
853 Lisp_Object fontset;
854
855 fontset = FONTSET_FROM_ID (face->fontset);
856 if (NILP (fontset))
857 return;
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)))
864 {
865 int id = XINT (FONTSET_ID (FONTSET_DEFAULT (fontset)));
866
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;
873 }
874 face->fontset = -1;
875 }
876
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. */
880
881 int
882 face_for_char (struct frame *f, struct face *face, int c,
883 ptrdiff_t pos, Lisp_Object object)
884 {
885 Lisp_Object fontset, rfont_def, charset;
886 int face_id;
887 int id;
888
889 eassert (fontset_id_valid_p (face->fontset));
890
891 if (ASCII_CHAR_P (c) || CHAR_BYTE8_P (c))
892 return face->ascii_face->id;
893
894 #ifdef HAVE_NS
895 if (face->font)
896 {
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;
904 }
905 #endif
906
907 fontset = FONTSET_FROM_ID (face->fontset);
908 eassert (!BASE_FONTSET_P (fontset));
909
910 if (pos < 0)
911 {
912 id = -1;
913 charset = Qnil;
914 }
915 else
916 {
917 charset = Fget_char_property (make_number (pos), Qcharset, object);
918 if (CHARSETP (charset))
919 {
920 Lisp_Object val;
921
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));
926 }
927 else
928 id = -1;
929 }
930
931 rfont_def = fontset_font (fontset, c, face, id);
932 if (VECTORP (rfont_def))
933 {
934 if (INTEGERP (RFONT_DEF_FACE (rfont_def)))
935 face_id = XINT (RFONT_DEF_FACE (rfont_def));
936 else
937 {
938 Lisp_Object font_object;
939
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);
943 }
944 }
945 else
946 {
947 if (INTEGERP (FONTSET_NOFONT_FACE (fontset)))
948 face_id = XINT (FONTSET_NOFONT_FACE (fontset));
949 else
950 {
951 face_id = face_for_font (f, Qnil, face);
952 set_fontset_nofont_face (fontset, make_number (face_id));
953 }
954 }
955 eassert (face_id >= 0);
956 return face_id;
957 }
958
959
960 Lisp_Object
961 font_for_char (struct face *face, int c, ptrdiff_t pos, Lisp_Object object)
962 {
963 Lisp_Object fontset, rfont_def, charset;
964 int id;
965
966 if (ASCII_CHAR_P (c))
967 {
968 Lisp_Object font_object;
969
970 XSETFONT (font_object, face->ascii_face->font);
971 return font_object;
972 }
973
974 eassert (fontset_id_valid_p (face->fontset));
975 fontset = FONTSET_FROM_ID (face->fontset);
976 eassert (!BASE_FONTSET_P (fontset));
977 if (pos < 0)
978 {
979 id = -1;
980 charset = Qnil;
981 }
982 else
983 {
984 charset = Fget_char_property (make_number (pos), Qcharset, object);
985 if (CHARSETP (charset))
986 {
987 Lisp_Object val;
988
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));
993 }
994 else
995 id = -1;
996 }
997
998 rfont_def = fontset_font (fontset, c, face, id);
999 return (VECTORP (rfont_def)
1000 ? RFONT_DEF_OBJECT (rfont_def)
1001 : Qnil);
1002 }
1003
1004
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. */
1009
1010 int
1011 make_fontset_for_ascii_face (struct frame *f, int base_fontset_id, struct face *face)
1012 {
1013 Lisp_Object base_fontset, fontset, frame;
1014
1015 XSETFRAME (frame, f);
1016 if (base_fontset_id >= 0)
1017 {
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));
1022 }
1023 else
1024 base_fontset = Vdefault_fontset;
1025
1026 fontset = make_fontset (frame, Qnil, base_fontset);
1027 return XINT (FONTSET_ID (fontset));
1028 }
1029
1030 \f
1031
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;
1036
1037 #define CACHED_FONTSET_NAME SSDATA (XCAR (Vcached_fontset_data))
1038 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
1039
1040 /* If fontset name PATTERN contains any wild card, return regular
1041 expression corresponding to PATTERN. */
1042
1043 static Lisp_Object
1044 fontset_pattern_regexp (Lisp_Object pattern)
1045 {
1046 if (!strchr (SSDATA (pattern), '*')
1047 && !strchr (SSDATA (pattern), '?'))
1048 /* PATTERN does not contain any wild cards. */
1049 return Qnil;
1050
1051 if (!CONSP (Vcached_fontset_data)
1052 || strcmp (SSDATA (pattern), CACHED_FONTSET_NAME))
1053 {
1054 /* We must at first update the cached data. */
1055 unsigned char *regex, *p0, *p1;
1056 int ndashes = 0, nstars = 0, nescs = 0;
1057
1058 for (p0 = SDATA (pattern); *p0; p0++)
1059 {
1060 if (*p0 == '-')
1061 ndashes++;
1062 else if (*p0 == '*')
1063 nstars++;
1064 else if (*p0 == '['
1065 || *p0 == '.' || *p0 == '\\'
1066 || *p0 == '+' || *p0 == '^'
1067 || *p0 == '$')
1068 nescs++;
1069 }
1070
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
1076 + 2 * nescs + 1);
1077 USE_SAFE_ALLOCA;
1078 p1 = regex = SAFE_ALLOCA (regexsize);
1079
1080 *p1++ = '^';
1081 for (p0 = SDATA (pattern); *p0; p0++)
1082 {
1083 if (*p0 == '*')
1084 {
1085 if (ndashes < 14)
1086 *p1++ = '.';
1087 else
1088 *p1++ = '[', *p1++ = '^', *p1++ = '-', *p1++ = ']';
1089 *p1++ = '*';
1090 }
1091 else if (*p0 == '?')
1092 *p1++ = '.';
1093 else if (*p0 == '['
1094 || *p0 == '.' || *p0 == '\\'
1095 || *p0 == '+' || *p0 == '^'
1096 || *p0 == '$')
1097 *p1++ = '\\', *p1++ = *p0;
1098 else
1099 *p1++ = *p0;
1100 }
1101 *p1++ = '$';
1102 *p1++ = 0;
1103
1104 Vcached_fontset_data = Fcons (build_string (SSDATA (pattern)),
1105 build_string ((char *) regex));
1106 SAFE_FREE ();
1107 }
1108
1109 return CACHED_FONTSET_REGEX;
1110 }
1111
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
1117 */
1118
1119 int
1120 fs_query_fontset (Lisp_Object name, int name_pattern)
1121 {
1122 Lisp_Object tem;
1123 int i;
1124
1125 name = Fdowncase (name);
1126 if (name_pattern != 1)
1127 {
1128 tem = Frassoc (name, Vfontset_alias_alist);
1129 if (NILP (tem))
1130 tem = Fassoc (name, Vfontset_alias_alist);
1131 if (CONSP (tem) && STRINGP (XCAR (tem)))
1132 name = XCAR (tem);
1133 else if (name_pattern == 0)
1134 {
1135 tem = fontset_pattern_regexp (name);
1136 if (STRINGP (tem))
1137 {
1138 name = tem;
1139 name_pattern = 1;
1140 }
1141 }
1142 }
1143
1144 for (i = 0; i < ASIZE (Vfontset_table); i++)
1145 {
1146 Lisp_Object fontset, this_name;
1147
1148 fontset = FONTSET_FROM_ID (i);
1149 if (NILP (fontset)
1150 || !BASE_FONTSET_P (fontset))
1151 continue;
1152
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)))
1157 return i;
1158 }
1159 return -1;
1160 }
1161
1162
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)
1170 {
1171 Lisp_Object fontset;
1172 int id;
1173
1174 check_window_system (NULL);
1175
1176 CHECK_STRING (pattern);
1177
1178 if (SCHARS (pattern) == 0)
1179 return Qnil;
1180
1181 id = fs_query_fontset (pattern, !NILP (regexpp));
1182 if (id < 0)
1183 return Qnil;
1184
1185 fontset = FONTSET_FROM_ID (id);
1186 return FONTSET_NAME (fontset);
1187 }
1188
1189 /* Return a list of base fontset names matching PATTERN on frame F. */
1190
1191 Lisp_Object
1192 list_fontsets (struct frame *f, Lisp_Object pattern, int size)
1193 {
1194 Lisp_Object frame, regexp, val;
1195 int id;
1196
1197 XSETFRAME (frame, f);
1198
1199 regexp = fontset_pattern_regexp (pattern);
1200 val = Qnil;
1201
1202 for (id = 0; id < ASIZE (Vfontset_table); id++)
1203 {
1204 Lisp_Object fontset, name;
1205
1206 fontset = FONTSET_FROM_ID (id);
1207 if (NILP (fontset)
1208 || !BASE_FONTSET_P (fontset)
1209 || !EQ (frame, FONTSET_FRAME (fontset)))
1210 continue;
1211 name = FONTSET_NAME (fontset);
1212
1213 if (STRINGP (regexp)
1214 ? (fast_string_match (regexp, name) < 0)
1215 : strcmp (SSDATA (pattern), SSDATA (name)))
1216 continue;
1217
1218 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
1219 }
1220
1221 return val;
1222 }
1223
1224
1225 /* Free all realized fontsets whose base fontset is BASE. */
1226
1227 static void
1228 free_realized_fontsets (Lisp_Object base)
1229 {
1230 int id;
1231
1232 #if 0
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. */
1237 block_input ();
1238 for (id = 0; id < ASIZE (Vfontset_table); id++)
1239 {
1240 Lisp_Object this = AREF (Vfontset_table, id);
1241
1242 if (EQ (FONTSET_BASE (this), base))
1243 {
1244 Lisp_Object tail;
1245
1246 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
1247 tail = XCDR (tail))
1248 {
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);
1252
1253 /* Face THIS itself is also freed by the following call. */
1254 free_realized_face (f, face);
1255 }
1256 }
1257 }
1258 unblock_input ();
1259 #else /* not 0 */
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++)
1263 {
1264 Lisp_Object this = AREF (Vfontset_table, id);
1265
1266 if (CHAR_TABLE_P (this) && EQ (FONTSET_BASE (this), base))
1267 {
1268 Fclear_face_cache (Qt);
1269 break;
1270 }
1271 }
1272 #endif /* not 0 */
1273 }
1274
1275
1276 /* Check validity of NAME as a fontset name and return the
1277 corresponding fontset. If not valid, signal an error.
1278
1279 If NAME is t, return Vdefault_fontset. If NAME is nil, return the
1280 fontset of *FRAME.
1281
1282 Set *FRAME to the actual frame. */
1283
1284 static Lisp_Object
1285 check_fontset_name (Lisp_Object name, Lisp_Object *frame)
1286 {
1287 int id;
1288 struct frame *f = decode_live_frame (*frame);
1289
1290 XSETFRAME (*frame, f);
1291
1292 if (EQ (name, Qt))
1293 return Vdefault_fontset;
1294 if (NILP (name))
1295 id = FRAME_FONTSET (f);
1296 else
1297 {
1298 CHECK_STRING (name);
1299 /* First try NAME as literal. */
1300 id = fs_query_fontset (name, 2);
1301 if (id < 0)
1302 /* For backward compatibility, try again NAME as pattern. */
1303 id = fs_query_fontset (name, 0);
1304 if (id < 0)
1305 error ("Fontset `%s' does not exist", SDATA (name));
1306 }
1307 return FONTSET_FROM_ID (id);
1308 }
1309
1310 static void
1311 accumulate_script_ranges (Lisp_Object arg, Lisp_Object range, Lisp_Object val)
1312 {
1313 if (EQ (XCAR (arg), val))
1314 {
1315 if (CONSP (range))
1316 XSETCDR (arg, Fcons (Fcons (XCAR (range), XCDR (range)), XCDR (arg)));
1317 else
1318 XSETCDR (arg, Fcons (Fcons (range, range), XCDR (arg)));
1319 }
1320 }
1321
1322
1323 /* Callback function for map_charset_chars in Fset_fontset_font.
1324 ARG is a vector [ FONTSET FONT_DEF ADD ASCII SCRIPT_RANGE_LIST ].
1325
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.
1330
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
1333 case. */
1334
1335 static void
1336 set_fontset_font (Lisp_Object arg, Lisp_Object range)
1337 {
1338 Lisp_Object fontset, font_def, add, ascii, script_range_list;
1339 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1340
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);
1346
1347 if (NILP (ascii) && from < 0x80)
1348 {
1349 if (to < 0x80)
1350 return;
1351 from = 0x80;
1352 range = Fcons (make_number (0x80), XCDR (range));
1353 }
1354
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)
1358
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))
1362 {
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));
1369 }
1370
1371 FONTSET_ADD (fontset, range, font_def, add);
1372 ASET (arg, 4, script_range_list);
1373 }
1374
1375 static void update_auto_fontset_alist (Lisp_Object, Lisp_Object);
1376
1377
1378 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 5, 0,
1379 doc: /*
1380 Modify fontset NAME to use FONT-SPEC for TARGET characters.
1381
1382 NAME is a fontset name string, nil for the fontset of FRAME, or t for
1383 the default fontset.
1384
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
1387 TO (inclusive).
1388
1389 TARGET may be a script name symbol. In that case, use FONT-SPEC for
1390 all characters that belong to the script.
1391
1392 TARGET may be a charset. In that case, use FONT-SPEC for all
1393 characters in the charset.
1394
1395 TARGET may be nil. In that case, use FONT-SPEC for any characters for
1396 that no FONT-SPEC is specified.
1397
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.
1405
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.
1408
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)
1414 {
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;
1421
1422 fontset = check_fontset_name (name, &frame);
1423
1424 fontname = Qnil;
1425 if (CONSP (font_spec))
1426 {
1427 Lisp_Object spec = Ffont_spec (0, NULL);
1428
1429 font_parse_family_registry (XCAR (font_spec), XCDR (font_spec), spec);
1430 font_spec = spec;
1431 fontname = Ffont_xlfd_name (font_spec, Qnil);
1432 }
1433 else if (STRINGP (font_spec))
1434 {
1435 Lisp_Object args[2];
1436
1437 fontname = font_spec;
1438 args[0] = QCname;
1439 args[1] = font_spec;
1440 font_spec = Ffont_spec (2, args);
1441 }
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));
1446
1447 if (! NILP (font_spec))
1448 {
1449 Lisp_Object encoding, repertory;
1450
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))
1460 encoding = Qascii;
1461
1462 if (SYMBOLP (encoding))
1463 {
1464 CHECK_CHARSET (encoding);
1465 encoding = repertory = CHARSET_SYMBOL_ID (encoding);
1466 }
1467 else
1468 {
1469 repertory = XCDR (encoding);
1470 encoding = XCAR (encoding);
1471 CHECK_CHARSET (encoding);
1472 encoding = CHARSET_SYMBOL_ID (encoding);
1473 if (! NILP (repertory) && SYMBOLP (repertory))
1474 {
1475 CHECK_CHARSET (repertory);
1476 repertory = CHARSET_SYMBOL_ID (repertory);
1477 }
1478 }
1479 FONT_DEF_NEW (font_def, font_spec, encoding, repertory);
1480 }
1481 else
1482 font_def = Qnil;
1483
1484 if (CHARACTERP (target))
1485 {
1486 if (XFASTINT (target) < 0x80)
1487 error ("Can't set a font for partial ASCII range");
1488 range_list = list1 (Fcons (target, target));
1489 }
1490 else if (CONSP (target))
1491 {
1492 Lisp_Object from, to;
1493
1494 from = Fcar (target);
1495 to = Fcdr (target);
1496 CHECK_CHARACTER (from);
1497 CHECK_CHARACTER (to);
1498 if (XFASTINT (from) < 0x80)
1499 {
1500 if (XFASTINT (from) != 0 || XFASTINT (to) < 0x7F)
1501 error ("Can't set a font for partial ASCII range");
1502 ascii_changed = 1;
1503 }
1504 range_list = list1 (target);
1505 }
1506 else if (SYMBOLP (target) && !NILP (target))
1507 {
1508 Lisp_Object script_list;
1509 Lisp_Object val;
1510
1511 range_list = Qnil;
1512 script_list = XCHAR_TABLE (Vchar_script_table)->extras[0];
1513 if (! NILP (Fmemq (target, script_list)))
1514 {
1515 if (EQ (target, Qlatin))
1516 ascii_changed = 1;
1517 val = list1 (target);
1518 map_char_table (accumulate_script_ranges, Qnil, Vchar_script_table,
1519 val);
1520 range_list = Fnreverse (XCDR (val));
1521 }
1522 if (CHARSETP (target))
1523 {
1524 CHECK_CHARSET_GET_CHARSET (target, charset);
1525 if (charset->ascii_compatible_p)
1526 ascii_changed = 1;
1527 }
1528 else if (NILP (range_list))
1529 error ("Invalid script or charset name: %s",
1530 SDATA (SYMBOL_NAME (target)));
1531 }
1532 else if (NILP (target))
1533 range_list = list1 (Qnil);
1534 else
1535 error ("Invalid target for setting a font");
1536
1537 if (ascii_changed)
1538 {
1539 Lisp_Object val;
1540
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. */
1546 ascii_changed = 0;
1547 }
1548
1549 if (charset)
1550 {
1551 Lisp_Object arg;
1552
1553 arg = make_uninit_vector (5);
1554 ASET (arg, 0, fontset);
1555 ASET (arg, 1, font_def);
1556 ASET (arg, 2, add);
1557 ASET (arg, 3, ascii_changed ? Qt : Qnil);
1558 ASET (arg, 4, range_list);
1559
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);
1564 }
1565 for (; CONSP (range_list); range_list = XCDR (range_list))
1566 FONTSET_ADD (fontset, XCAR (range_list), font_def, add);
1567
1568 if (ascii_changed)
1569 {
1570 Lisp_Object tail, fr;
1571 int fontset_id = XINT (FONTSET_ID (fontset));
1572
1573 set_fontset_ascii (fontset, fontname);
1574 name = FONTSET_NAME (fontset);
1575 FOR_EACH_FRAME (tail, fr)
1576 {
1577 struct frame *f = XFRAME (fr);
1578 Lisp_Object font_object;
1579 struct face *face;
1580
1581 if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f))
1582 continue;
1583 if (fontset_id != FRAME_FONTSET (f))
1584 continue;
1585 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
1586 if (face)
1587 font_object = font_load_for_lface (f, face->lface, font_spec);
1588 else
1589 font_object = font_open_by_spec (f, font_spec);
1590 if (! NILP (font_object))
1591 {
1592 update_auto_fontset_alist (font_object, fontset);
1593 AUTO_FRAME_ARG (arg, Qfont, Fcons (name, font_object));
1594 Fmodify_frame_parameters (fr, arg);
1595 }
1596 }
1597 }
1598
1599 /* Free all realized fontsets whose base is FONTSET. This way, the
1600 specified character(s) are surely redisplayed by a correct
1601 font. */
1602 free_realized_fontsets (fontset);
1603
1604 return Qnil;
1605 }
1606
1607
1608 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
1609 doc: /* Create a new fontset NAME from font information in FONTLIST.
1610
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
1614 FONT-SPEC.
1615
1616 SCRIPT is a symbol that appears in the first extra slot of the
1617 char-table `char-script-table'.
1618
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)
1622 {
1623 Lisp_Object fontset;
1624 int id;
1625
1626 CHECK_STRING (name);
1627 CHECK_LIST (fontlist);
1628
1629 name = Fdowncase (name);
1630 id = fs_query_fontset (name, 0);
1631 if (id < 0)
1632 {
1633 Lisp_Object font_spec = Ffont_spec (0, NULL);
1634 Lisp_Object short_name;
1635 char xlfd[256];
1636 int len;
1637
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);
1649 if (len < 0)
1650 error ("Invalid fontset name (perhaps too long): %s", SDATA (name));
1651 set_fontset_ascii (fontset, make_unibyte_string (xlfd, len));
1652 }
1653 else
1654 {
1655 fontset = FONTSET_FROM_ID (id);
1656 free_realized_fontsets (fontset);
1657 Fset_char_table_range (fontset, Qt, Qnil);
1658 }
1659
1660 for (; CONSP (fontlist); fontlist = XCDR (fontlist))
1661 {
1662 Lisp_Object elt, script;
1663
1664 elt = XCAR (fontlist);
1665 script = Fcar (elt);
1666 elt = Fcdr (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);
1670 else
1671 Fset_fontset_font (name, script, elt, Qnil, Qappend);
1672 }
1673 return name;
1674 }
1675
1676
1677 /* Alist of automatically created fontsets. Each element is a cons
1678 (FONT-SPEC . FONTSET-ID). */
1679 static Lisp_Object auto_fontset_alist;
1680
1681 /* Number of automatically created fontsets. */
1682 static ptrdiff_t num_auto_fontsets;
1683
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. */
1692
1693 int
1694 fontset_from_font (Lisp_Object font_object)
1695 {
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;
1700 Lisp_Object val;
1701
1702 val = assoc_no_quit (font_spec, auto_fontset_alist);
1703 if (CONSP (val))
1704 return XINT (FONTSET_ID (XCDR (val)));
1705 if (num_auto_fontsets++ == 0)
1706 alias = intern ("fontset-startup");
1707 else
1708 {
1709 char temp[sizeof "fontset-auto" + INT_STRLEN_BOUND (ptrdiff_t)];
1710
1711 sprintf (temp, "fontset-auto%"pD"d", num_auto_fontsets - 1);
1712 alias = intern (temp);
1713 }
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);
1726 {
1727 Lisp_Object target = find_font_encoding (SYMBOL_NAME (registry));
1728
1729 if (CONSP (target))
1730 target = XCDR (target);
1731 if (! CHARSETP (target))
1732 target = Qlatin;
1733 Fset_fontset_font (name, target, font_spec, Qnil, Qnil);
1734 Fset_fontset_font (name, Qnil, font_spec, Qnil, Qnil);
1735 }
1736
1737 set_fontset_ascii (fontset, font_name);
1738
1739 return XINT (FONTSET_ID (fontset));
1740 }
1741
1742
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. */
1747
1748 static void
1749 update_auto_fontset_alist (Lisp_Object font_object, Lisp_Object fontset)
1750 {
1751 Lisp_Object prev, tail;
1752
1753 for (prev = Qnil, tail = auto_fontset_alist; CONSP (tail);
1754 prev = tail, tail = XCDR (tail))
1755 if (EQ (fontset, XCDR (XCAR (tail))))
1756 {
1757 if (NILP (prev))
1758 auto_fontset_alist = XCDR (tail);
1759 else
1760 XSETCDR (prev, XCDR (tail));
1761 break;
1762 }
1763 }
1764
1765
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
1771 default face.
1772
1773 GLYPH-CODE is the glyph code in the font to use for the character.
1774
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.
1777
1778 It returns nil in the following cases:
1779
1780 (1) The window system doesn't have a font for the character (thus
1781 it is displayed by an empty box).
1782
1783 (2) The character code is invalid.
1784
1785 (3) If POSITION is not nil, and the current buffer is not displayed
1786 in any window.
1787
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. */
1791
1792
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)
1796 {
1797 ptrdiff_t pos, pos_byte, dummy;
1798 int face_id;
1799 int c;
1800 struct frame *f;
1801 struct face *face;
1802
1803 if (NILP (position))
1804 {
1805 CHECK_CHARACTER (ch);
1806 c = XINT (ch);
1807 f = XFRAME (selected_frame);
1808 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
1809 pos = -1;
1810 }
1811 else
1812 {
1813 Lisp_Object window;
1814 struct window *w;
1815
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);
1821 if (NILP (ch))
1822 c = FETCH_CHAR (pos_byte);
1823 else
1824 {
1825 CHECK_NATNUM (ch);
1826 c = XINT (ch);
1827 }
1828 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1829 if (NILP (window))
1830 return Qnil;
1831 w = XWINDOW (window);
1832 f = XFRAME (w->frame);
1833 face_id = face_at_buffer_position (w, pos, &dummy,
1834 pos + 100, 0, -1);
1835 }
1836 if (! CHAR_VALID_P (c))
1837 return Qnil;
1838 if (!FRAME_WINDOW_P (f))
1839 return Qnil;
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);
1846 if (face->font)
1847 {
1848 unsigned code = face->font->driver->encode_char (face->font, c);
1849 Lisp_Object font_object;
1850
1851 if (code == FONT_INVALID_CODE)
1852 return Qnil;
1853 XSETFONT (font_object, face->font);
1854 return Fcons (font_object, INTEGER_TO_CONS (code));
1855 }
1856 return Qnil;
1857 }
1858
1859
1860 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1861 doc: /* Return information about a fontset FONTSET on frame FRAME.
1862
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.
1865
1866 The value is a char-table whose elements have this form:
1867
1868 ((FONT OPENED-FONT ...) ...)
1869
1870 FONT is a name of font specified for a range of characters.
1871
1872 OPENED-FONT is a name of a font actually opened.
1873
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)
1879 {
1880 Lisp_Object *realized[2], fontsets[2], tables[2];
1881 Lisp_Object val, elt;
1882 int c, i, j, k;
1883
1884 check_window_system (NULL);
1885 fontset = check_fontset_name (fontset, &frame);
1886
1887 /* Recode fontsets realized on FRAME from the base fontset FONTSET
1888 in the table `realized'. */
1889 USE_SAFE_ALLOCA;
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++)
1893 {
1894 elt = FONTSET_FROM_ID (i);
1895 if (!NILP (elt)
1896 && EQ (FONTSET_BASE (elt), fontset)
1897 && EQ (FONTSET_FRAME (elt), frame))
1898 realized[0][j++] = elt;
1899 }
1900 realized[0][j] = Qnil;
1901
1902 for (i = j = 0; ! NILP (realized[0][i]); i++)
1903 {
1904 elt = FONTSET_DEFAULT (realized[0][i]);
1905 if (! NILP (elt))
1906 realized[1][j++] = elt;
1907 }
1908 realized[1][j] = Qnil;
1909
1910 tables[0] = Fmake_char_table (Qfontset_info, Qnil);
1911 fontsets[0] = fontset;
1912 if (!EQ (fontset, Vdefault_fontset))
1913 {
1914 tables[1] = Fmake_char_table (Qnil, Qnil);
1915 set_char_table_extras (tables[0], 0, tables[1]);
1916 fontsets[1] = Vdefault_fontset;
1917 }
1918
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++)
1922 {
1923 for (c = 0; c <= MAX_CHAR; )
1924 {
1925 int from = c, to = MAX_5_BYTE_CHAR;
1926
1927 if (c <= MAX_5_BYTE_CHAR)
1928 {
1929 val = char_table_ref_and_range (fontsets[k], c, &from, &to);
1930 }
1931 else
1932 {
1933 val = FONTSET_FALLBACK (fontsets[k]);
1934 to = MAX_CHAR;
1935 }
1936 if (VECTORP (val))
1937 {
1938 Lisp_Object alist;
1939
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),
1944 alist);
1945 alist = Fnreverse (alist);
1946
1947 /* Then store opened font names to cdr of each elements. */
1948 for (i = 0; ! NILP (realized[k][i]); i++)
1949 {
1950 if (c <= MAX_5_BYTE_CHAR)
1951 val = FONTSET_REF (realized[k][i], c);
1952 else
1953 val = FONTSET_FALLBACK (realized[k][i]);
1954 if (! CONSP (val) || ! VECTORP (XCDR (val)))
1955 continue;
1956 /* VAL: (int . [[FACE-ID FONT-DEF FONT-OBJECT int] ... ]) */
1957 val = XCDR (val);
1958 for (j = 0; j < ASIZE (val); j++)
1959 {
1960 elt = AREF (val, j);
1961 if (FONT_OBJECT_P (RFONT_DEF_OBJECT (elt)))
1962 {
1963 Lisp_Object font_object = RFONT_DEF_OBJECT (elt);
1964 Lisp_Object slot, name;
1965
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));
1970 }
1971 }
1972 }
1973
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);
1977 else
1978 set_char_table_defalt (tables[k], alist);
1979
1980 /* At last, change each elements to font names. */
1981 for (; CONSP (alist); alist = XCDR (alist))
1982 {
1983 elt = XCAR (alist);
1984 XSETCAR (elt, Ffont_xlfd_name (XCAR (elt), Qnil));
1985 }
1986 }
1987 c = to + 1;
1988 }
1989 if (EQ (fontset, Vdefault_fontset))
1990 break;
1991 }
1992
1993 SAFE_FREE ();
1994 return tables[0];
1995 }
1996
1997
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.
2002
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
2006 fontset.
2007
2008 If the 2nd optional arg ALL is non-nil, return a list of all font name
2009 patterns. */)
2010 (Lisp_Object name, Lisp_Object ch, Lisp_Object all)
2011 {
2012 int c;
2013 Lisp_Object fontset, elt, list, repertory, val;
2014 int i, j;
2015 Lisp_Object frame;
2016
2017 frame = Qnil;
2018 fontset = check_fontset_name (name, &frame);
2019
2020 CHECK_CHARACTER (ch);
2021 c = XINT (ch);
2022 list = Qnil;
2023 while (1)
2024 {
2025 for (i = 0, elt = FONTSET_REF (fontset, c); i < 2;
2026 i++, elt = FONTSET_FALLBACK (fontset))
2027 if (VECTORP (elt))
2028 for (j = 0; j < ASIZE (elt); j++)
2029 {
2030 Lisp_Object family, registry;
2031
2032 val = AREF (elt, j);
2033 if (NILP (val))
2034 return Qnil;
2035 repertory = AREF (val, 1);
2036 if (INTEGERP (repertory))
2037 {
2038 struct charset *charset = CHARSET_FROM_ID (XINT (repertory));
2039
2040 if (! CHAR_CHARSET_P (c, charset))
2041 continue;
2042 }
2043 else if (CHAR_TABLE_P (repertory))
2044 {
2045 if (NILP (CHAR_TABLE_REF (repertory, c)))
2046 continue;
2047 }
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);
2057 if (NILP (all))
2058 return val;
2059 list = Fcons (val, list);
2060 }
2061 if (EQ (fontset, Vdefault_fontset))
2062 break;
2063 fontset = Vdefault_fontset;
2064 }
2065 return (Fnreverse (list));
2066 }
2067
2068 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
2069 doc: /* Return a list of all defined fontset names. */)
2070 (void)
2071 {
2072 Lisp_Object fontset, list;
2073 int i;
2074
2075 list = Qnil;
2076 for (i = 0; i < ASIZE (Vfontset_table); i++)
2077 {
2078 fontset = FONTSET_FROM_ID (i);
2079 if (!NILP (fontset)
2080 && BASE_FONTSET_P (fontset))
2081 list = Fcons (FONTSET_NAME (fontset), list);
2082 }
2083
2084 return list;
2085 }
2086
2087
2088 #ifdef ENABLE_CHECKING
2089
2090 Lisp_Object dump_fontset (Lisp_Object) EXTERNALLY_VISIBLE;
2091
2092 Lisp_Object
2093 dump_fontset (Lisp_Object fontset)
2094 {
2095 Lisp_Object vec;
2096
2097 vec = Fmake_vector (make_number (3), Qnil);
2098 ASET (vec, 0, FONTSET_ID (fontset));
2099
2100 if (BASE_FONTSET_P (fontset))
2101 {
2102 ASET (vec, 1, FONTSET_NAME (fontset));
2103 }
2104 else
2105 {
2106 Lisp_Object frame;
2107
2108 frame = FONTSET_FRAME (fontset);
2109 if (FRAMEP (frame))
2110 {
2111 struct frame *f = XFRAME (frame);
2112
2113 if (FRAME_LIVE_P (f))
2114 ASET (vec, 1,
2115 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)),
2116 f->name));
2117 else
2118 ASET (vec, 1,
2119 Fcons (FONTSET_NAME (FONTSET_BASE (fontset)), Qnil));
2120 }
2121 if (!NILP (FONTSET_DEFAULT (fontset)))
2122 ASET (vec, 2, FONTSET_ID (FONTSET_DEFAULT (fontset)));
2123 }
2124 return vec;
2125 }
2126
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. */)
2129 (void)
2130 {
2131 Lisp_Object val;
2132 int i;
2133
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));
2138 }
2139 #endif /* ENABLE_CHECKING */
2140
2141 void
2142 syms_of_fontset (void)
2143 {
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));
2148
2149 DEFSYM (Qprepend, "prepend");
2150 DEFSYM (Qappend, "append");
2151 DEFSYM (Qlatin, "latin");
2152
2153 Vcached_fontset_data = Qnil;
2154 staticpro (&Vcached_fontset_data);
2155
2156 Vfontset_table = Fmake_vector (make_number (32), Qnil);
2157 staticpro (&Vfontset_table);
2158
2159 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
2160 staticpro (&Vdefault_fontset);
2161 set_fontset_id (Vdefault_fontset, make_number (0));
2162 set_fontset_name
2163 (Vdefault_fontset,
2164 build_pure_c_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default"));
2165 ASET (Vfontset_table, 0, Vdefault_fontset);
2166 next_fontset_id = 1;
2167
2168 auto_fontset_alist = Qnil;
2169 staticpro (&auto_fontset_alist);
2170
2171 DEFVAR_LISP ("font-encoding-charset-alist", Vfont_encoding_charset_alist,
2172 doc: /*
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.
2177
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;
2181
2182 DEFVAR_LISP ("use-default-ascent", Vuse_default_ascent,
2183 doc: /*
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.
2187
2188 This affects how a composite character which contains
2189 such a character is displayed on screen. */);
2190 Vuse_default_ascent = Qnil;
2191
2192 DEFVAR_LISP ("ignore-relative-composition", Vignore_relative_composition,
2193 doc: /*
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;
2200
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;
2206
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")));
2212
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;
2219
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;
2223
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);
2233 #endif
2234 }