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