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