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