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