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