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