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