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