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