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