Merge from emacs--devo--0
[bpt/emacs.git] / 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 Lisp_Object Votf_script_alist;
224
225 /* The following six are declarations of callback functions depending
226 on window system. See the comments in src/fontset.h for more
227 detail. */
228
229 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
230 struct font_info *(*get_font_info_func) P_ ((FRAME_PTR f, int font_idx));
231
232 /* Return a list of font names which matches PATTERN. See the documentation
233 of `x-list-fonts' for more details. */
234 Lisp_Object (*list_fonts_func) P_ ((struct frame *f,
235 Lisp_Object pattern,
236 int size,
237 int maxnames));
238
239 /* Load a font named NAME for frame F and return a pointer to the
240 information of the loaded font. If loading is failed, return 0. */
241 struct font_info *(*load_font_func) P_ ((FRAME_PTR f, char *name, int));
242
243 /* Return a pointer to struct font_info of a font named NAME for frame F. */
244 struct font_info *(*query_font_func) P_ ((FRAME_PTR f, char *name));
245
246 /* Additional function for setting fontset or changing fontset
247 contents of frame F. */
248 void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
249 Lisp_Object oldval));
250
251 /* To find a CCL program, fs_load_font calls this function.
252 The argument is a pointer to the struct font_info.
253 This function set the member `encoder' of the structure. */
254 void (*find_ccl_program_func) P_ ((struct font_info *));
255
256 Lisp_Object (*get_font_repertory_func) P_ ((struct frame *,
257 struct font_info *));
258
259 /* Check if any window system is used now. */
260 void (*check_window_system_func) P_ ((void));
261
262
263 /* Prototype declarations for static functions. */
264 static Lisp_Object fontset_add P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
265 Lisp_Object));
266 static Lisp_Object fontset_font P_ ((Lisp_Object, int, struct face *, int));
267 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
268 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
269 static void accumulate_script_ranges P_ ((Lisp_Object, Lisp_Object,
270 Lisp_Object));
271 Lisp_Object find_font_encoding P_ ((Lisp_Object));
272
273 static void set_fontset_font P_ ((Lisp_Object, Lisp_Object));
274
275 #ifdef FONTSET_DEBUG
276
277 /* Return 1 if ID is a valid fontset id, else return 0. */
278
279 static int
280 fontset_id_valid_p (id)
281 int id;
282 {
283 return (id >= 0 && id < ASIZE (Vfontset_table) - 1);
284 }
285
286 #endif
287
288
289 \f
290 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
291
292 /* Return the fontset with ID. No check of ID's validness. */
293 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
294
295 /* Macros to access special values of FONTSET. */
296 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
297
298 /* Macros to access special values of (base) FONTSET. */
299 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
300 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
301
302 /* Macros to access special values of (realized) FONTSET. */
303 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
304 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
305 #define FONTSET_NOFONT_FACE(fontset) XCHAR_TABLE (fontset)->extras[5]
306 #define FONTSET_REPERTORY(fontset) XCHAR_TABLE (fontset)->extras[6]
307 #define FONTSET_DEFAULT(fontset) XCHAR_TABLE (fontset)->extras[7]
308
309 /* For both base and realized fontset. */
310 #define FONTSET_FALLBACK(fontset) XCHAR_TABLE (fontset)->extras[8]
311
312 #define BASE_FONTSET_P(fontset) (NILP (FONTSET_BASE (fontset)))
313
314
315 /* Return the element of FONTSET for the character C. If FONTSET is a
316 base fontset other then the default fontset and FONTSET doesn't
317 contain information for C, return the information in the default
318 fontset. */
319
320 #define FONTSET_REF(fontset, c) \
321 (EQ (fontset, Vdefault_fontset) \
322 ? CHAR_TABLE_REF (fontset, c) \
323 : fontset_ref ((fontset), (c)))
324
325 static Lisp_Object
326 fontset_ref (fontset, c)
327 Lisp_Object fontset;
328 int c;
329 {
330 Lisp_Object elt;
331
332 elt = CHAR_TABLE_REF (fontset, c);
333 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
334 /* Don't check Vdefault_fontset for a realized fontset. */
335 && NILP (FONTSET_BASE (fontset)))
336 elt = CHAR_TABLE_REF (Vdefault_fontset, c);
337 return elt;
338 }
339
340
341 /* Return the element of FONTSET for the character C, set FROM and TO
342 to the range of characters around C that have the same value as C.
343 If FONTSET is a base fontset other then the default fontset and
344 FONTSET doesn't contain information for C, return the information
345 in the default fontset. */
346
347 #define FONTSET_REF_AND_RANGE(fontset, c, form, to) \
348 (EQ (fontset, Vdefault_fontset) \
349 ? char_table_ref_and_range (fontset, c, &from, &to) \
350 : fontset_ref_and_range (fontset, c, &from, &to))
351
352 static Lisp_Object
353 fontset_ref_and_range (fontset, c, from, to)
354 Lisp_Object fontset;
355 int c;
356 int *from, *to;
357 {
358 Lisp_Object elt;
359
360 elt = char_table_ref_and_range (fontset, c, from, to);
361 if (NILP (elt) && ! EQ (fontset, Vdefault_fontset)
362 /* Don't check Vdefault_fontset for a realized fontset. */
363 && NILP (FONTSET_BASE (fontset)))
364 {
365 int from1, to1;
366
367 elt = char_table_ref_and_range (Vdefault_fontset, c, &from1, &to1);
368 if (*from < from1)
369 *from = from1;
370 if (*to > to1)
371 *to = to1;
372 }
373 return elt;
374 }
375
376
377 /* Set elements of FONTSET for characters in RANGE to the value ELT.
378 RANGE is a cons (FROM . TO), where FROM and TO are character codes
379 specifying a range. */
380
381 #define FONTSET_SET(fontset, range, elt) \
382 Fset_char_table_range ((fontset), (range), (elt))
383
384
385 /* Modify the elements of FONTSET for characters in RANGE by replacing
386 with ELT or adding ELT. RANGE is a cons (FROM . TO), where FROM
387 and TO are character codes specifying a range. If ADD is nil,
388 replace with ELT, if ADD is `prepend', prepend ELT, otherwise,
389 append ELT. */
390
391 #define FONTSET_ADD(fontset, range, elt, add) \
392 (NILP (add) \
393 ? (NILP (range) \
394 ? (FONTSET_FALLBACK (fontset) = Fmake_vector (make_number (1), (elt))) \
395 : Fset_char_table_range ((fontset), (range), \
396 Fmake_vector (make_number (1), (elt)))) \
397 : fontset_add ((fontset), (range), (elt), (add)))
398
399 static Lisp_Object
400 fontset_add (fontset, range, elt, add)
401 Lisp_Object fontset, range, elt, add;
402 {
403 Lisp_Object args[2];
404 int idx = (EQ (add, Qappend) ? 0 : 1);
405
406 args[1 - idx] = Fmake_vector (make_number (1), elt);
407
408 if (CONSP (range))
409 {
410 int from = XINT (XCAR (range));
411 int to = XINT (XCDR (range));
412 int from1, to1;
413
414 do {
415 args[idx] = char_table_ref_and_range (fontset, from, &from1, &to1);
416 if (to < to1)
417 to1 = to;
418 char_table_set_range (fontset, from, to1,
419 NILP (args[idx]) ? args[1 - idx]
420 : Fvconcat (2, args));
421 from = to1 + 1;
422 } while (from < to);
423 }
424 else
425 {
426 args[idx] = FONTSET_FALLBACK (fontset);
427 FONTSET_FALLBACK (fontset)
428 = NILP (args[idx]) ? args[1 - idx] : Fvconcat (2, args);
429 }
430 return Qnil;
431 }
432
433
434 /* Update FONTSET_ELEMENT which has this form:
435 [CHARSET-ORDERED-LIST-TICK PREFERRED-CHARSET-ID PREFERRED-RFONT-DEF
436 RFONT-DEF0 RFONT-DEF1 ...].
437 Reorder RFONT-DEFs according to the current order of charset
438 (Vcharset_ordered_list), and update CHARSET-ORDERED-LIST-TICK to
439 the latest value. */
440
441 static void
442 reorder_font_vector (fontset_element)
443 Lisp_Object fontset_element;
444 {
445 Lisp_Object list, *new_vec;
446 Lisp_Object font_def;
447 int size;
448 int *charset_id_table;
449 int i, idx;
450
451 ASET (fontset_element, 0, make_number (charset_ordered_list_tick));
452 size = ASIZE (fontset_element) - 3;
453 if (size <= 1)
454 /* No need to reorder VEC. */
455 return;
456 charset_id_table = (int *) alloca (sizeof (int) * size);
457 new_vec = (Lisp_Object *) alloca (sizeof (Lisp_Object) * size);
458
459 /* At first, extract ENCODING (a chaset ID) from each FONT-DEF.
460 FONT-DEF has this form:
461 [FACE-ID FONT-INDEX [ FONT-SPEC ENCODING REPERTORY ]] */
462 for (i = 0; i < size; i++)
463 {
464 font_def = AREF (fontset_element, i + 3);
465 if (! NILP (AREF (font_def, 2)))
466 charset_id_table[i] = XINT (AREF (AREF (font_def, 2), 1));
467 else
468 charset_id_table[i] = -1;
469 }
470
471 /* Then, store FONT-DEFs in NEW_VEC in the correct order. */
472 for (idx = 0, list = Vcharset_ordered_list;
473 idx < size && CONSP (list); list = XCDR (list))
474 {
475 for (i = 0; i < size; i++)
476 if (charset_id_table[i] == XINT (XCAR (list)))
477 new_vec[idx++] = AREF (fontset_element, i + 3);
478 }
479 for (i = 0; i < size; i++)
480 if (charset_id_table[i] < 0)
481 new_vec[idx++] = AREF (fontset_element, i + 3);
482
483 /* At last, update FONT-DEFs. */
484 for (i = 0; i < size; i++)
485 ASET (fontset_element, i + 3, new_vec[i]);
486 }
487
488
489 /* Load a font matching the font related attributes in FACE->lface and
490 font pattern in FONT_DEF of FONTSET, and return an index of the
491 font. FONT_DEF has this form:
492 [ FONT-SPEC ENCODING REPERTORY ]
493 If REPERTORY is nil, generate a char-table representing the font
494 repertory by looking into the font itself. */
495
496 static int
497 load_font_get_repertory (f, face, font_def, fontset)
498 FRAME_PTR f;
499 struct face *face;
500 Lisp_Object font_def;
501 Lisp_Object fontset;
502 {
503 char *font_name;
504 struct font_info *font_info;
505 int charset;
506
507 font_name = choose_face_font (f, face->lface, AREF (font_def, 0), NULL);
508 charset = XINT (AREF (font_def, 1));
509 if (! (font_info = fs_load_font (f, font_name, charset)))
510 return -1;
511
512 if (NILP (AREF (font_def, 2))
513 && NILP (Fassq (make_number (font_info->font_idx),
514 FONTSET_REPERTORY (fontset))))
515 {
516 /* We must look into the font to get the correct repertory as a
517 char-table. */
518 Lisp_Object repertory;
519
520 repertory = (*get_font_repertory_func) (f, font_info);
521 FONTSET_REPERTORY (fontset)
522 = Fcons (Fcons (make_number (font_info->font_idx), repertory),
523 FONTSET_REPERTORY (fontset));
524 }
525
526 return font_info->font_idx;
527 }
528
529
530 /* Return RFONT-DEF (vector) in the realized fontset FONTSET for the
531 character C. If the corresponding font is not yet opened, open it
532 (if FACE is not NULL) or return Qnil (if FACE is NULL).
533 If no proper font is found for C, return Qnil. */
534
535 static Lisp_Object
536 fontset_font (fontset, c, face, id)
537 Lisp_Object fontset;
538 int c;
539 struct face *face;
540 int id;
541 {
542 Lisp_Object base_fontset, elt, vec;
543 int i, from, to;
544 int font_idx;
545 FRAME_PTR f = XFRAME (FONTSET_FRAME (fontset));
546
547 base_fontset = FONTSET_BASE (fontset);
548 vec = CHAR_TABLE_REF (fontset, c);
549 if (EQ (vec, Qt))
550 goto try_fallback;
551
552 if (NILP (vec))
553 {
554 /* We have not yet decided a face for C. */
555 Lisp_Object range;
556
557 if (! face)
558 return Qnil;
559 elt = FONTSET_REF_AND_RANGE (base_fontset, c, from, to);
560 range = Fcons (make_number (from), make_number (to));
561 if (NILP (elt))
562 {
563 /* Record that we have no font for characters of this
564 range. */
565 vec = Qt;
566 FONTSET_SET (fontset, range, vec);
567 goto try_fallback;
568 }
569 /* Build a vector [ -1 -1 nil NEW-ELT0 NEW-ELT1 NEW-ELT2 ... ],
570 where the first -1 is to force reordering of NEW-ELTn,
571 NEW-ETLn is [nil nil AREF (elt, n) nil]. */
572 #ifdef USE_FONT_BACKEND
573 if (enable_font_backend
574 && EQ (base_fontset, Vdefault_fontset))
575 vec = Fmake_vector (make_number (ASIZE (elt) + 4), make_number (-1));
576 else
577 #endif /* not USE_FONT_BACKEND */
578 vec = Fmake_vector (make_number (ASIZE (elt) + 3), make_number (-1));
579 ASET (vec, 2, Qnil);
580 for (i = 0; i < ASIZE (elt); i++)
581 {
582 Lisp_Object tmp;
583
584 #ifdef USE_FONT_BACKEND
585 if (enable_font_backend)
586 tmp = Fmake_vector (make_number (5), Qnil);
587 else
588 #endif /* USE_FONT_BACKEND */
589 tmp = Fmake_vector (make_number (4), Qnil);
590 ASET (tmp, 2, AREF (elt, i));
591 ASET (vec, 3 + i, tmp);
592 }
593 #ifdef USE_FONT_BACKEND
594 if (enable_font_backend
595 && EQ (base_fontset, Vdefault_fontset))
596 {
597 Lisp_Object script, font_spec, tmp;
598
599 script = CHAR_TABLE_REF (Vchar_script_table, c);
600 if (NILP (script))
601 script = intern ("latin");
602 font_spec = Ffont_spec (0, NULL);
603 ASET (font_spec, FONT_REGISTRY_INDEX, Qiso10646_1);
604 ASET (font_spec, FONT_EXTRA_INDEX,
605 Fcons (Fcons (QCscript, script), Qnil));
606 tmp = Fmake_vector (make_number (5), Qnil);
607 ASET (tmp, 3, font_spec);
608 ASET (vec, 3 + i, tmp);
609 }
610 #endif /* USE_FONT_BACKEND */
611
612 /* Then store it in the fontset. */
613 FONTSET_SET (fontset, range, vec);
614 }
615
616 retry:
617 if (XINT (AREF (vec, 0)) != charset_ordered_list_tick)
618 /* The priority of charsets is changed after we selected a face
619 for C last time. */
620 reorder_font_vector (vec);
621
622 if (id < 0)
623 i = 3;
624 else if (id == XFASTINT (AREF (vec, 1)))
625 i = 2;
626 else
627 {
628 ASET (vec, 1, make_number (id));
629 for (i = 3; i < ASIZE (vec); i++)
630 if (id == XFASTINT (AREF (AREF (AREF (vec, i), 2), 1)))
631 break;
632 if (i < ASIZE (vec))
633 {
634 ASET (vec, 2, AREF (vec, i));
635 i = 2;
636 }
637 else
638 {
639 ASET (vec, 2, Qnil);
640 i = 3;
641 }
642 }
643
644 /* Find the first available font in the vector of RFONT-DEF. */
645 for (; i < ASIZE (vec); i++)
646 {
647 Lisp_Object font_def;
648
649 elt = AREF (vec, i);
650 if (NILP (elt))
651 continue;
652 /* ELT == [ FACE-ID FONT-INDEX FONT-DEF OPENED-FONT-NAME ] */
653 if (INTEGERP (AREF (elt, 1)) && XINT (AREF (elt, 1)) < 0)
654 /* We couldn't open this font last time. */
655 continue;
656
657 if (!face && NILP (AREF (elt, 1)))
658 /* We have not yet opened the font. */
659 return Qnil;
660
661 font_def = AREF (elt, 2);
662 /* FONT_DEF == [ FONT-SPEC ENCODING REPERTORY ] */
663
664 #ifdef USE_FONT_BACKEND
665 if (enable_font_backend)
666 {
667 /* ELT == [ FACE-ID FONT-INDEX FONT-DEF FONT-ENTITY FONT-OBJECT ] */
668 Lisp_Object font_entity = AREF (elt, 3);
669 Lisp_Object font_object = AREF (elt, 4);
670 int has_char;
671
672 if (NILP (font_entity) && ! NILP (AREF (font_def, 0)))
673 {
674 Lisp_Object tmp = AREF (font_def, 0);
675 Lisp_Object spec = Ffont_spec (0, NULL);
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 DEFVAR_LISP ("otf-script-alist", &Votf_script_alist,
2467 doc: /* Alist of OpenType script tags vs the corresponding script names. */);
2468 Votf_script_alist = Qnil;
2469
2470 defsubr (&Squery_fontset);
2471 defsubr (&Snew_fontset);
2472 defsubr (&Sset_fontset_font);
2473 defsubr (&Sfont_info);
2474 defsubr (&Sinternal_char_font);
2475 defsubr (&Sfontset_info);
2476 defsubr (&Sfontset_font);
2477 defsubr (&Sfontset_list);
2478 #ifdef FONTSET_DEBUG
2479 defsubr (&Sfontset_list_all);
2480 #endif
2481 }
2482
2483 /* arch-tag: ea861585-2f5f-4e5b-9849-d04a9c3a3537
2484 (do not change this comment) */