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