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