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