]> code.delx.au - gnu-emacs/blob - src/fontset.c
(free_realized_fontsets): Call Fclear_face_cache instead
[gnu-emacs] / src / fontset.c
1 /* Fontset handler.
2 Copyright (C) 1995, 1997, 2000 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001, 2002
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
7
8 This file is part of GNU Emacs.
9
10 GNU Emacs is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs; see the file COPYING. If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25 /* #define FONTSET_DEBUG */
26
27 #include <config.h>
28
29 #ifdef FONTSET_DEBUG
30 #include <stdio.h>
31 #endif
32
33 #include "lisp.h"
34 #include "blockinput.h"
35 #include "buffer.h"
36 #include "character.h"
37 #include "charset.h"
38 #include "ccl.h"
39 #include "keyboard.h"
40 #include "frame.h"
41 #include "dispextern.h"
42 #include "fontset.h"
43 #include "window.h"
44
45 #ifdef FONTSET_DEBUG
46 #undef xassert
47 #define xassert(X) do {if (!(X)) abort ();} while (0)
48 #undef INLINE
49 #define INLINE
50 #endif
51
52
53 /* FONTSET
54
55 A fontset is a collection of font related information to give
56 similar appearance (style, etc) of characters. There are two kinds
57 of fontsets; base and realized. A base fontset is created by
58 `new-fontset' from Emacs Lisp explicitly. A realized fontset is
59 created implicitly when a face is realized for ASCII characters. A
60 face is also realized for non-ASCII characters based on an ASCII
61 face. All of non-ASCII faces based on the same ASCII face share
62 the same realized fontset.
63
64 A fontset object is implemented by a char-table whose default value
65 and parent are always nil.
66
67 An element of a base fontset is a font specification of the form:
68 [ FAMILY WEIGHT SLANT SWIDTH REGISTRY ] (vector of size 5)
69 or
70 FONT-NAME (strig)
71
72 FAMILY and REGISTRY are strings.
73
74 WEIGHT, SLANT, and SWIDTH must be symbols that set-face-attribute
75 accepts as attribute values for :weight, :slant, :swidth
76 respectively.
77
78
79 A fontset has 7 extra slots.
80
81 The 1st slot is an ID number of the fontset.
82
83 The 2nd slot is a name of the fontset in a base fontset, and nil in
84 a realized fontset.
85
86 The 3rd slot is nil in a base fontset, and a base fontset in a
87 realized fontset.
88
89 The 4th slot is a frame that the fontset belongs to. This is nil
90 in a base fontset.
91
92 The 5th slot is a cons of 0 and fontname for ASCII characters in a
93 base fontset, and nil in a realized face.
94
95 The 6th slot is an alist of a charset vs. the corresponding font
96 specification.
97
98 The 7th slot is an alist of a font specification vs. the
99 corresponding face ID. In a base fontset, the face IDs are all
100 nil.
101
102 All fontsets are recorded in Vfontset_table.
103
104
105 DEFAULT FONTSET
106
107 There's a special fontset named `default fontset' which defines the
108 default font specifications. When a base fontset doesn't specify a
109 font for a specific character, the corresponding value in the
110 default fontset is used. The format is the same as a base fontset.
111
112 The parent of a realized fontset created for such a face that has
113 no fontset is the default fontset.
114
115
116 These structures are hidden from the other codes than this file.
117 The other codes handle fontsets only by their ID numbers. They
118 usually use the variable name `fontset' for IDs. But, in this
119 file, we always use varialbe name `id' for IDs, and name `fontset'
120 for the actual fontset objects (i.e. char-table objects).
121
122 */
123
124 /********** VARIABLES and FUNCTION PROTOTYPES **********/
125
126 extern Lisp_Object Qfont;
127 Lisp_Object Qfontset;
128
129 /* Vector containing all fontsets. */
130 static Lisp_Object Vfontset_table;
131
132 /* Next possibly free fontset ID. Usually this keeps the minimum
133 fontset ID not yet used. */
134 static int next_fontset_id;
135
136 /* The default fontset. This gives default FAMILY and REGISTRY of
137 font for each character. */
138 static Lisp_Object Vdefault_fontset;
139
140 Lisp_Object Vfont_encoding_alist;
141 Lisp_Object Vuse_default_ascent;
142 Lisp_Object Vignore_relative_composition;
143 Lisp_Object Valternate_fontname_alist;
144 Lisp_Object Vfontset_alias_alist;
145 Lisp_Object Vvertical_centering_font_regexp;
146
147 /* The following six are declarations of callback functions depending
148 on window system. See the comments in src/fontset.h for more
149 detail. */
150
151 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
152 struct font_info *(*get_font_info_func) P_ ((FRAME_PTR f, int font_idx));
153
154 /* Return a list of font names which matches PATTERN. See the documentation
155 of `x-list-fonts' for more details. */
156 Lisp_Object (*list_fonts_func) P_ ((struct frame *f,
157 Lisp_Object pattern,
158 int size,
159 int maxnames));
160
161 /* Load a font named NAME for frame F and return a pointer to the
162 information of the loaded font. If loading is failed, return 0. */
163 struct font_info *(*load_font_func) P_ ((FRAME_PTR f, char *name, int));
164
165 /* Return a pointer to struct font_info of a font named NAME for frame F. */
166 struct font_info *(*query_font_func) P_ ((FRAME_PTR f, char *name));
167
168 /* Additional function for setting fontset or changing fontset
169 contents of frame F. */
170 void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
171 Lisp_Object oldval));
172
173 /* To find a CCL program, fs_load_font calls this function.
174 The argument is a pointer to the struct font_info.
175 This function set the member `encoder' of the structure. */
176 void (*find_ccl_program_func) P_ ((struct font_info *));
177
178 /* Check if any window system is used now. */
179 void (*check_window_system_func) P_ ((void));
180
181
182 /* Prototype declarations for static functions. */
183 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
184 static int fontset_id_valid_p P_ ((int));
185 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
186
187 \f
188 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
189
190 /* Return the fontset with ID. No check of ID's validness. */
191 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
192
193 /* Macros to access special values of FONTSET. */
194 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
195
196 /* Macros to access special values of (base) FONTSET. */
197 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
198 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
199
200 #define BASE_FONTSET_P(fontset) STRINGP (FONTSET_NAME (fontset))
201
202 /* Macros to access special values of (realized) FONTSET. */
203 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
204 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
205 #define FONTSET_CHARSET_ALIST(fontset) XCHAR_TABLE (fontset)->extras[5]
206 #define FONTSET_FACE_ALIST(fontset) XCHAR_TABLE (fontset)->extras[6]
207
208
209 /* Return the element of FONTSET (char-table) at index C (character). */
210
211 #define FONTSET_REF(fontset, c, etl) ((elt) = fontset_ref ((fontset), (c)))
212
213 static Lisp_Object
214 fontset_ref (fontset, c)
215 Lisp_Object fontset;
216 int c;
217 {
218 Lisp_Object elt;
219
220 while (1)
221 {
222 elt = CHAR_TABLE_REF (fontset, c);
223 if (NILP (elt) && ASCII_CHAR_P (c))
224 elt = FONTSET_ASCII (fontset);
225 if (NILP (elt))
226 {
227 Lisp_Object tail;
228 struct charset *charset;
229
230 for (tail = FONTSET_CHARSET_ALIST (fontset);
231 CONSP (tail); tail = XCDR (tail))
232 {
233 charset = CHARSET_FROM_ID (XCAR (XCAR (tail)));
234 if (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset))
235 {
236 elt = XCDR (XCAR (tail));
237 break;
238 }
239 }
240 }
241 if (! NILP (elt) || EQ (fontset, Vdefault_fontset))
242 break;
243 fontset = Vdefault_fontset;
244 }
245 return elt;
246 }
247
248
249 /* Set the element of FONTSET at index IDX to the value ELT. IDX may
250 be a character or a charset. */
251
252 #define FONTSET_SET(fontset, c, newelt) fontset_set(fontset, c, newelt)
253
254 static void
255 fontset_set (fontset, idx, elt)
256 Lisp_Object fontset, idx, elt;
257 {
258 if (SYMBOLP (idx))
259 {
260 Lisp_Object id, slot, tail;
261
262 id = make_number (CHARSET_SYMBOL_ID (idx));
263 if (id == charset_ascii)
264 Fset_char_table_range (fontset,
265 Fcons (make_number (0), make_number (127)),
266 elt);
267 else
268 {
269 slot = Fassq (id, FONTSET_CHARSET_ALIST (fontset));
270 if (CONSP (slot))
271 XCDR (slot) = elt;
272 else if (CONSP (FONTSET_CHARSET_ALIST (fontset)))
273 {
274 for (tail = FONTSET_CHARSET_ALIST (fontset);
275 CONSP (XCDR (tail)); tail = XCDR (tail));
276 XCDR (tail) = Fcons (Fcons (id, elt), Qnil);
277 }
278 else
279 FONTSET_CHARSET_ALIST (fontset) = Fcons (Fcons (id, elt), Qnil);
280 }
281 }
282 else
283 {
284 int from = XINT (XCAR (idx));
285 int to = XINT (XCDR (idx));
286
287 if (from == to)
288 CHAR_TABLE_SET (fontset, from, elt);
289 else
290 Fset_char_table_range (fontset, idx, elt);
291 }
292 }
293
294
295 /* Return a face registerd in the realized fontset FONTSET for the
296 character C. Return -1 if a face ID is not yet set. */
297
298 static struct face *
299 fontset_face (fontset, c)
300 Lisp_Object fontset;
301 int c;
302 {
303 Lisp_Object base, elt;
304 int id;
305 struct face *face;
306
307 base = FONTSET_BASE (fontset);
308 FONTSET_REF (base, c, elt);
309
310 if (NILP (elt))
311 return NULL;
312
313 elt = Fassoc (elt, FONTSET_FACE_ALIST (fontset));
314 if (! CONSP (elt))
315 return NULL;
316 id = XINT (XCDR (elt));
317 face = FACE_FROM_ID (XFRAME (FONTSET_FRAME (fontset)), id);
318 return face;
319 }
320
321
322 /* Return a newly created fontset with NAME. If BASE is nil, make a
323 base fontset. Otherwise make a realized fontset whose base is
324 BASE. */
325
326 static Lisp_Object
327 make_fontset (frame, name, base)
328 Lisp_Object frame, name, base;
329 {
330 Lisp_Object fontset;
331 int size = ASIZE (Vfontset_table);
332 int id = next_fontset_id;
333
334 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
335 the next available fontset ID. So it is expected that this loop
336 terminates quickly. In addition, as the last element of
337 Vfontset_table is always nil, we don't have to check the range of
338 id. */
339 while (!NILP (AREF (Vfontset_table, id))) id++;
340
341 if (id + 1 == size)
342 {
343 Lisp_Object tem;
344 int i;
345
346 tem = Fmake_vector (make_number (size + 32), Qnil);
347 for (i = 0; i < size; i++)
348 AREF (tem, i) = AREF (Vfontset_table, i);
349 Vfontset_table = tem;
350 }
351
352 fontset = Fmake_char_table (Qfontset, Qnil);
353
354 FONTSET_ID (fontset) = make_number (id);
355 if (NILP (base))
356 {
357 FONTSET_NAME (fontset) = name;
358 }
359 else
360 {
361 FONTSET_NAME (fontset) = Qnil;
362 FONTSET_FRAME (fontset) = frame;
363 FONTSET_BASE (fontset) = base;
364 }
365
366 ASET (Vfontset_table, id, fontset);
367 next_fontset_id = id + 1;
368 return fontset;
369 }
370
371
372 \f
373 /********** INTERFACES TO xfaces.c and dispextern.h **********/
374
375 /* Return name of the fontset with ID. */
376
377 Lisp_Object
378 fontset_name (id)
379 int id;
380 {
381 Lisp_Object fontset;
382
383 fontset = FONTSET_FROM_ID (id);
384 return FONTSET_NAME (fontset);
385 }
386
387
388 /* Return ASCII font name of the fontset with ID. */
389
390 Lisp_Object
391 fontset_ascii (id)
392 int id;
393 {
394 Lisp_Object fontset;
395
396 fontset= FONTSET_FROM_ID (id);
397 return FONTSET_ASCII (fontset);
398 }
399
400
401 /* Free fontset of FACE defined on frame F. Called from
402 free_realized_face. */
403
404 void
405 free_face_fontset (f, face)
406 FRAME_PTR f;
407 struct face *face;
408 {
409 AREF (Vfontset_table, face->fontset) = Qnil;
410 if (face->fontset < next_fontset_id)
411 next_fontset_id = face->fontset;
412 }
413
414
415 /* Return 1 iff FACE is suitable for displaying character C.
416 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
417 when C is not an ASCII character. */
418
419 int
420 face_suitable_for_char_p (face, c)
421 struct face *face;
422 int c;
423 {
424 Lisp_Object fontset;
425
426 fontset = FONTSET_FROM_ID (face->fontset);
427 return (face == fontset_face (fontset, c));
428 }
429
430
431 /* Return ID of face suitable for displaying character C on frame F.
432 The selection of face is done based on the fontset of FACE. FACE
433 must be reazlied for ASCII characters in advance. Called from the
434 macro FACE_FOR_CHAR when C is not an ASCII character. */
435
436 int
437 face_for_char (f, face, c)
438 FRAME_PTR f;
439 struct face *face;
440 int c;
441 {
442 Lisp_Object fontset, elt;
443 struct face *new_face;
444
445 xassert (fontset_id_valid_p (face->fontset));
446 fontset = FONTSET_FROM_ID (face->fontset);
447 xassert (!BASE_FONTSET_P (fontset));
448
449 new_face = fontset_face (fontset, c);
450 if (new_face)
451 return new_face->id;
452
453 /* No face is recorded for C in the fontset of FACE. Make a new
454 realized face for C that has the same fontset. */
455 return lookup_face (f, face->lface, c, face);
456 }
457
458
459 /* Make a realized fontset for ASCII face FACE on frame F from the
460 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
461 default fontset as the base. Value is the id of the new fontset.
462 Called from realize_x_face. */
463
464 int
465 make_fontset_for_ascii_face (f, base_fontset_id)
466 FRAME_PTR f;
467 int base_fontset_id;
468 {
469 Lisp_Object base_fontset, fontset, frame;
470
471 XSETFRAME (frame, f);
472 if (base_fontset_id >= 0)
473 {
474 base_fontset = FONTSET_FROM_ID (base_fontset_id);
475 if (!BASE_FONTSET_P (base_fontset))
476 base_fontset = FONTSET_BASE (base_fontset);
477 xassert (BASE_FONTSET_P (base_fontset));
478 }
479 else
480 base_fontset = Vdefault_fontset;
481
482 fontset = make_fontset (frame, Qnil, base_fontset);
483 return XINT (FONTSET_ID (fontset));
484 }
485
486
487 /* Return FONT-SPEC recorded in the fontset of FACE for character C.
488 If FACE is null, or the fontset doesn't contain information about
489 C, get the font name pattern from the default fontset. Called from
490 choose_face_font. */
491
492 Lisp_Object
493 fontset_font_pattern (f, face, c)
494 FRAME_PTR f;
495 struct face *face;
496 int c;
497 {
498 Lisp_Object fontset, base, elt;
499 int id = face ? face->fontset : -1;
500
501 if (id >= 0)
502 {
503 fontset = FONTSET_FROM_ID (id);
504 xassert (!BASE_FONTSET_P (fontset));
505 base = FONTSET_BASE (fontset);
506 }
507 else
508 {
509 base = Vdefault_fontset;
510 }
511
512 FONTSET_REF (base, c, elt);
513 if (face && ! NILP (elt))
514 {
515 Lisp_Object slot;
516
517 slot = Fassoc (elt, FONTSET_FACE_ALIST (fontset));
518 if (CONSP (slot))
519 XSETCDR (slot, make_number (face->id));
520 FONTSET_FACE_ALIST (fontset)
521 = Fcons (Fcons (elt, make_number (face->id)),
522 FONTSET_FACE_ALIST (fontset));
523 }
524 return elt;
525 }
526
527
528 #if defined(WINDOWSNT) && defined (_MSC_VER)
529 #pragma optimize("", off)
530 #endif
531
532 /* Load a font named FONTNAME on frame F. Return a pointer to the
533 struct font_info of the loaded font. If loading fails, return
534 NULL. */
535
536 struct font_info *
537 fs_load_font (f, fontname)
538 FRAME_PTR f;
539 char *fontname;
540 {
541 Lisp_Object tail, elt;
542 struct font_info *fontp;
543
544 if (!fontname)
545 /* No way to get fontname. */
546 return 0;
547
548 fontp = (*load_font_func) (f, fontname, 0);
549 if (!fontp)
550 return NULL;
551
552 /* Fill in members (charset, vertical_centering, encoding, etc) of
553 font_info structure that are not set by (*load_font_func). */
554 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
555 {
556 elt = XCAR (tail);
557 if (STRINGP (XCAR (elt)) && CHARSETP (XCDR (elt))
558 && fast_c_string_match_ignore_case (XCAR (elt), fontname) >= 0)
559 {
560 fontp->charset = CHARSET_SYMBOL_ID (XCDR (elt));
561 break;
562 }
563 }
564 if (! CONSP (tail))
565 return NULL;
566
567 fontp->vertical_centering
568 = (STRINGP (Vvertical_centering_font_regexp)
569 && (fast_c_string_match_ignore_case
570 (Vvertical_centering_font_regexp, fontp->full_name) >= 0));
571
572 fontp->font_encoder = NULL;
573
574 if (find_ccl_program_func)
575 (*find_ccl_program_func) (fontp);
576
577 return fontp;
578 }
579
580 #if defined(WINDOWSNT) && defined (_MSC_VER)
581 #pragma optimize("", on)
582 #endif
583
584 \f
585 /* Cache data used by fontset_pattern_regexp. The car part is a
586 pattern string containing at least one wild card, the cdr part is
587 the corresponding regular expression. */
588 static Lisp_Object Vcached_fontset_data;
589
590 #define CACHED_FONTSET_NAME (XSTRING (XCAR (Vcached_fontset_data))->data)
591 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
592
593 /* If fontset name PATTERN contains any wild card, return regular
594 expression corresponding to PATTERN. */
595
596 static Lisp_Object
597 fontset_pattern_regexp (pattern)
598 Lisp_Object pattern;
599 {
600 if (!index (XSTRING (pattern)->data, '*')
601 && !index (XSTRING (pattern)->data, '?'))
602 /* PATTERN does not contain any wild cards. */
603 return Qnil;
604
605 if (!CONSP (Vcached_fontset_data)
606 || strcmp (XSTRING (pattern)->data, CACHED_FONTSET_NAME))
607 {
608 /* We must at first update the cached data. */
609 char *regex = (char *) alloca (XSTRING (pattern)->size * 2 + 3);
610 char *p0, *p1 = regex;
611
612 /* Convert "*" to ".*", "?" to ".". */
613 *p1++ = '^';
614 for (p0 = (char *) XSTRING (pattern)->data; *p0; p0++)
615 {
616 if (*p0 == '*')
617 {
618 *p1++ = '.';
619 *p1++ = '*';
620 }
621 else if (*p0 == '?')
622 *p1++ = '.';
623 else
624 *p1++ = *p0;
625 }
626 *p1++ = '$';
627 *p1++ = 0;
628
629 Vcached_fontset_data = Fcons (build_string (XSTRING (pattern)->data),
630 build_string (regex));
631 }
632
633 return CACHED_FONTSET_REGEX;
634 }
635
636 /* Return ID of the base fontset named NAME. If there's no such
637 fontset, return -1. */
638
639 int
640 fs_query_fontset (name, regexpp)
641 Lisp_Object name;
642 int regexpp;
643 {
644 Lisp_Object tem;
645 int i;
646
647 name = Fdowncase (name);
648 if (!regexpp)
649 {
650 tem = Frassoc (name, Vfontset_alias_alist);
651 if (CONSP (tem) && STRINGP (XCAR (tem)))
652 name = XCAR (tem);
653 else
654 {
655 tem = fontset_pattern_regexp (name);
656 if (STRINGP (tem))
657 {
658 name = tem;
659 regexpp = 1;
660 }
661 }
662 }
663
664 for (i = 0; i < ASIZE (Vfontset_table); i++)
665 {
666 Lisp_Object fontset;
667 unsigned char *this_name;
668
669 fontset = FONTSET_FROM_ID (i);
670 if (NILP (fontset)
671 || !BASE_FONTSET_P (fontset))
672 continue;
673
674 this_name = XSTRING (FONTSET_NAME (fontset))->data;
675 if (regexpp
676 ? fast_c_string_match_ignore_case (name, this_name) >= 0
677 : !strcmp (XSTRING (name)->data, this_name))
678 return i;
679 }
680 return -1;
681 }
682
683
684 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
685 doc: /* Return the name of a fontset that matches PATTERN.
686 The value is nil if there is no matching fontset.
687 PATTERN can contain `*' or `?' as a wildcard
688 just as X font name matching algorithm allows.
689 If REGEXPP is non-nil, PATTERN is a regular expression. */)
690 (pattern, regexpp)
691 Lisp_Object pattern, regexpp;
692 {
693 Lisp_Object fontset;
694 int id;
695
696 (*check_window_system_func) ();
697
698 CHECK_STRING (pattern);
699
700 if (XSTRING (pattern)->size == 0)
701 return Qnil;
702
703 id = fs_query_fontset (pattern, !NILP (regexpp));
704 if (id < 0)
705 return Qnil;
706
707 fontset = FONTSET_FROM_ID (id);
708 return FONTSET_NAME (fontset);
709 }
710
711 /* Return a list of base fontset names matching PATTERN on frame F. */
712
713 Lisp_Object
714 list_fontsets (f, pattern, size)
715 FRAME_PTR f;
716 Lisp_Object pattern;
717 int size;
718 {
719 Lisp_Object frame, regexp, val;
720 int id;
721
722 XSETFRAME (frame, f);
723
724 regexp = fontset_pattern_regexp (pattern);
725 val = Qnil;
726
727 for (id = 0; id < ASIZE (Vfontset_table); id++)
728 {
729 Lisp_Object fontset;
730 unsigned char *name;
731
732 fontset = FONTSET_FROM_ID (id);
733 if (NILP (fontset)
734 || !BASE_FONTSET_P (fontset)
735 || !EQ (frame, FONTSET_FRAME (fontset)))
736 continue;
737 name = XSTRING (FONTSET_NAME (fontset))->data;
738
739 if (!NILP (regexp)
740 ? (fast_c_string_match_ignore_case (regexp, name) < 0)
741 : strcmp (XSTRING (pattern)->data, name))
742 continue;
743
744 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
745 }
746
747 return val;
748 }
749
750
751 /* Free all realized fontsets whose base fontset is BASE. */
752
753 static void
754 free_realized_fontsets (base)
755 Lisp_Object base;
756 {
757 int id;
758
759 #if 0
760 /* For the moment, this doesn't work because free_realized_face
761 doesn't remove FACE from a cache. Until we find a solution, we
762 suppress this code, and simply use Fclear_face_cache even though
763 that is not efficient. */
764 BLOCK_INPUT;
765 for (id = 0; id < ASIZE (Vfontset_table); id++)
766 {
767 Lisp_Object this = AREF (Vfontset_table, id);
768
769 if (EQ (FONTSET_BASE (this), base))
770 {
771 Lisp_Object tail;
772
773 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
774 tail = XCDR (tail))
775 {
776 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
777 int face_id = XINT (XCDR (XCAR (tail)));
778 struct face *face = FACE_FROM_ID (f, face_id);
779
780 /* Face THIS itself is also freed by the following call. */
781 free_realized_face (f, face);
782 }
783 }
784 }
785 UNBLOCK_INPUT;
786 #else /* not 0 */
787 Fclear_face_cache (Qt);
788 #endif /* not 0 */
789 }
790
791
792 /* Check validity of NAME as a fontset name and return the
793 corresponding fontset. If not valid, signal an error.
794 If NAME is t, return Vdefault_fontset. */
795
796 static Lisp_Object
797 check_fontset_name (name)
798 Lisp_Object name;
799 {
800 int id;
801
802 if (EQ (name, Qt))
803 return Vdefault_fontset;
804
805 CHECK_STRING (name);
806 id = fs_query_fontset (name, 0);
807 if (id < 0)
808 error ("Fontset `%s' does not exist", XSTRING (name)->data);
809 return FONTSET_FROM_ID (id);
810 }
811
812 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 4, 0,
813 doc: /* Modify fontset NAME to use FONT-SPEC for characters of CHARSETS.
814
815 CHARSET may be a cons; (FROM . TO), where FROM and TO are characters.
816 In that case, use FONT-SPEC for all characters in the range FROM and
817 TO (inclusive).
818
819 FONT-SPEC is be a vector; [ FAMILY WEIGHT SLANT WIDTH ADSTYLE REGISTRY ]
820
821 FONT-SPEC may be a cons; (FAMILY . REGISTRY), where FAMILY is a family
822 name of a font, REGSITRY is a registry name of a font.
823
824 FONT-SPEC may be a font name string. */)
825 (name, charset, font_spec, frame)
826 Lisp_Object name, charset, font_spec, frame;
827 {
828 Lisp_Object fontset;
829 Lisp_Object family, registry;
830 int charset_id;
831
832 fontset = check_fontset_name (name);
833
834 if (VECTORP (font_spec))
835 {
836 int i;
837 Lisp_Object val;
838
839 font_spec = Fcopy_sequence (font_spec);
840 for (i = 0; i < 5; i++)
841 {
842 val = Faref (font_spec, make_number (i));
843 if (! NILP (val))
844 {
845 CHECK_STRING (val);
846 ASET (font_spec, i, Fdowncase (val));
847 }
848 }
849 val = Faref (font_spec, make_number (5));
850 CHECK_STRING (val);
851 ASET (font_spec, 5, Fdowncase (val));
852 }
853 else if (STRINGP (font_spec))
854 font_spec = Fdowncase (font_spec);
855 else if (CONSP (font_spec))
856 {
857 CHECK_CONS (font_spec);
858 family = XCAR (font_spec);
859 registry = XCDR (font_spec);
860 font_spec = Fmake_vector (make_number (6), Qnil);
861 if (!NILP (family))
862 {
863 CHECK_STRING (family);
864 ASET (font_spec, 0, Fdowncase (family));
865 }
866 CHECK_STRING (registry);
867 ASET (font_spec, 5, Fdowncase (registry));
868 }
869
870 if (SYMBOLP (charset))
871 {
872 CHECK_CHARSET (charset);
873 }
874 else
875 {
876 Lisp_Object from, to;
877
878 /* CHARSET should be (FROM . TO). */
879 from = Fcar (charset);
880 to = Fcdr (charset);
881 CHECK_CHARACTER (from);
882 CHECK_CHARACTER (to);
883 }
884
885 /* The arg FRAME is kept for backward compatibility. We only check
886 the validity. */
887 if (!NILP (frame))
888 CHECK_LIVE_FRAME (frame);
889
890 FONTSET_SET (fontset, charset, font_spec);
891
892 /* Free all realized fontsets whose base is FONTSET. This way, the
893 specified character(s) are surely redisplayed by a correct
894 font. */
895 free_realized_fontsets (fontset);
896
897 return Qnil;
898 }
899
900
901 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
902 doc: /* Create a new fontset NAME from font information in FONTLIST.
903
904 FONTLIST is an alist of charsets vs corresponding font specifications.
905 Each element of FONTLIST has the form (CHARSET . FONT-SPEC), where
906 a character of CHARSET is displayed by a font that matches FONT-SPEC.
907
908 FONT-SPEC is a vector [ FAMILY WEIGHT SLANT WIDTH ADSTYLE REGISTRY ], where
909 FAMILY is a string specifying the font family,
910 WEIGHT is a string specifying the weight of the font,
911 SLANT is a string specifying the slant of the font,
912 WIDTH is a string specifying the width of the font,
913 ADSTYLE is a string specifying the adstyle of the font,
914 REGISTRY is a string specifying the charset-registry of the font.
915
916 See also the documentation of `set-face-attribute' for the detail of
917 these vector elements.
918
919 FONT-SPEC may be a font name (string). */)
920 (name, fontlist)
921 Lisp_Object name, fontlist;
922 {
923 Lisp_Object fontset, ascii_font;
924 Lisp_Object tem, tail;
925
926 CHECK_STRING (name);
927 CHECK_LIST (fontlist);
928
929 name = Fdowncase (name);
930 tem = Fquery_fontset (name, Qnil);
931 if (! NILP (tem))
932 free_realized_fontsets (tem);
933
934 fontset = make_fontset (Qnil, name, Qnil);
935
936 /* Check the validity of FONTLIST. */
937 ascii_font = Fcdr (Fassq (Qascii, fontlist));
938 if (NILP (ascii_font))
939 error ("No ascii font specified");
940 if (! STRINGP (ascii_font))
941 ascii_font = generate_ascii_font (name, ascii_font);
942
943 fontlist = Fcopy_sequence (fontlist);
944 for (tail = fontlist; ! NILP (tail); tail = Fcdr (tail))
945 Fset_fontset_font (name, Fcar (Fcar (tail)), Fcdr (Fcar (tail)), Qnil);
946
947 FONTSET_ASCII (fontset) = ascii_font;
948
949 return name;
950 }
951
952
953 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
954 doc: /* Return information about a font named NAME on frame FRAME.
955 If FRAME is omitted or nil, use the selected frame.
956 The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
957 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
958 where
959 OPENED-NAME is the name used for opening the font,
960 FULL-NAME is the full name of the font,
961 SIZE is the maximum bound width of the font,
962 HEIGHT is the height of the font,
963 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
964 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
965 how to compose characters.
966 If the named font is not yet loaded, return nil. */)
967 (name, frame)
968 Lisp_Object name, frame;
969 {
970 FRAME_PTR f;
971 struct font_info *fontp;
972 Lisp_Object info;
973
974 (*check_window_system_func) ();
975
976 CHECK_STRING (name);
977 name = Fdowncase (name);
978 if (NILP (frame))
979 frame = selected_frame;
980 CHECK_LIVE_FRAME (frame);
981 f = XFRAME (frame);
982
983 if (!query_font_func)
984 error ("Font query function is not supported");
985
986 fontp = (*query_font_func) (f, XSTRING (name)->data);
987 if (!fontp)
988 return Qnil;
989
990 info = Fmake_vector (make_number (7), Qnil);
991
992 XVECTOR (info)->contents[0] = build_string (fontp->name);
993 XVECTOR (info)->contents[1] = build_string (fontp->full_name);
994 XVECTOR (info)->contents[2] = make_number (fontp->size);
995 XVECTOR (info)->contents[3] = make_number (fontp->height);
996 XVECTOR (info)->contents[4] = make_number (fontp->baseline_offset);
997 XVECTOR (info)->contents[5] = make_number (fontp->relative_compose);
998 XVECTOR (info)->contents[6] = make_number (fontp->default_ascent);
999
1000 return info;
1001 }
1002
1003
1004 /* Return the font name for the character at POSITION in the current
1005 buffer. This is computed from all the text properties and overlays
1006 that apply to POSITION. It returns nil in the following cases:
1007
1008 (1) The window system doesn't have a font for the character (thus
1009 it is displayed by an empty box).
1010
1011 (2) The character code is invalid.
1012
1013 (3) The current buffer is not displayed in any window.
1014
1015 In addition, the returned font name may not take into account of
1016 such redisplay engine hooks as what used in jit-lock-mode if
1017 POSITION is currently not visible. */
1018
1019
1020 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 1, 0,
1021 doc: /* For internal use only. */)
1022 (position)
1023 Lisp_Object position;
1024 {
1025 int pos, pos_byte, dummy;
1026 int face_id;
1027 int c;
1028 Lisp_Object window;
1029 struct window *w;
1030 struct frame *f;
1031 struct face *face;
1032
1033 CHECK_NUMBER_COERCE_MARKER (position);
1034 pos = XINT (position);
1035 if (pos < BEGV || pos >= ZV)
1036 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1037 pos_byte = CHAR_TO_BYTE (pos);
1038 c = FETCH_CHAR (pos_byte);
1039 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1040 if (NILP (window))
1041 return Qnil;
1042 w = XWINDOW (window);
1043 f = XFRAME (w->frame);
1044 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
1045 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c);
1046 face = FACE_FROM_ID (f, face_id);
1047 return (face->font && face->font_name
1048 ? build_string (face->font_name)
1049 : Qnil);
1050 }
1051
1052
1053 /* Called from Ffontset_info via map_char_table on each leaf of
1054 fontset. ARG is a list (LAST FONT-INFO ...), where LAST is `(last
1055 ARG)' and FONT-INFOs have this form:
1056 (CHAR FONT-SPEC) or ((FROM . TO) FONT-SPEC)
1057 The current leaf is indexed by CHARACTER and has value ELT. This
1058 function add the information of the current leaf to ARG by
1059 appending a new element or modifying the last element.. */
1060
1061 static void
1062 accumulate_font_info (arg, character, elt)
1063 Lisp_Object arg, character, elt;
1064 {
1065 Lisp_Object last, last_char, last_elt;
1066
1067 if (!CONSP (elt) && !SINGLE_BYTE_CHAR_P (XINT (character)))
1068 FONTSET_REF (Vdefault_fontset, XINT (character), elt);
1069 if (!CONSP (elt))
1070 return;
1071 last = XCAR (arg);
1072 last_char = XCAR (XCAR (last));
1073 last_elt = XCAR (XCDR (XCAR (last)));
1074 elt = XCDR (elt);
1075 if (!NILP (Fequal (elt, last_elt)))
1076 {
1077 struct charset *this_charset = CHAR_CHARSET (XINT (character));
1078
1079 if (CONSP (last_char)) /* LAST_CHAR == (FROM . TO) */
1080 {
1081 if (this_charset == CHAR_CHARSET (XINT (XCAR (last_char))))
1082 {
1083 XSETCDR (last_char, character);
1084 return;
1085 }
1086 }
1087 else if (XINT (last_char) == XINT (character))
1088 return;
1089 else if (this_charset == CHAR_CHARSET (XINT (last_char)))
1090 {
1091 XSETCAR (XCAR (last), Fcons (last_char, character));
1092 return;
1093 }
1094 }
1095 XSETCDR (last, Fcons (Fcons (character, Fcons (elt, Qnil)), Qnil));
1096 XSETCAR (arg, XCDR (last));
1097 }
1098
1099
1100 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1101 doc: /* Return information about a fontset named NAME on frame FRAME.
1102 The value is a vector:
1103 [ SIZE HEIGHT ((CHARSET-OR-RANGE FONT-SPEC OPENED ...) ...) ],
1104 where,
1105 SIZE is the maximum bound width of ASCII font in the fontset,
1106 HEIGHT is the maximum bound height of ASCII font in the fontset,
1107 CHARSET-OR-RANGE is a charset or a cons of two characters specifying
1108 the range of characters.
1109 FONT-SPEC is a fontname pattern string or a vector
1110 [ FAMILY WEIGHT SLANT ADSTYLE REGISTRY ].
1111 See the documentation of `new-fontset' for the meanings those elements.
1112 OPENEDs are names of fonts actually opened.
1113 If the ASCII font is not yet opened, SIZE and HEIGHT are 0.
1114 If FRAME is omitted, it defaults to the currently selected frame. */)
1115 (name, frame)
1116 Lisp_Object name, frame;
1117 {
1118 Lisp_Object fontset;
1119 FRAME_PTR f;
1120 Lisp_Object val, tail, elt;
1121 Lisp_Object *realized;
1122 struct font_info *fontp = NULL;
1123 int n_realized = 0;
1124 int i;
1125
1126 (*check_window_system_func) ();
1127
1128 fontset = check_fontset_name (name);
1129
1130 if (NILP (frame))
1131 frame = selected_frame;
1132 CHECK_LIVE_FRAME (frame);
1133 f = XFRAME (frame);
1134
1135 /* Recode realized fontsets whose base is FONTSET in the table
1136 `realized'. */
1137 realized = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1138 * ASIZE (Vfontset_table));
1139 for (i = 0; i < ASIZE (Vfontset_table); i++)
1140 {
1141 elt = FONTSET_FROM_ID (i);
1142 if (!NILP (elt)
1143 && EQ (FONTSET_BASE (elt), fontset))
1144 realized[n_realized++] = elt;
1145 }
1146
1147 /* Accumulate information of the fontset in VAL. The format is
1148 (LAST FONT-INFO FONT-INFO ...), where FONT-INFO is (CHAR-OR-RANGE
1149 FONT-SPEC). See the comment for accumulate_font_info for the
1150 detail. */
1151 val = Fcons (Fcons (Qascii, Fcons (FONTSET_ASCII (fontset), Qnil)), Qnil);
1152 val = Fcons (val, val);
1153 for (i = 128; i <= MAX_CHAR; )
1154 {
1155 Lisp_Object elt;
1156 int from, to;
1157
1158 elt = char_table_ref_and_range (fontset, i, &from, &to);
1159 if (! NILP (elt))
1160 {
1161 elt = Fcons (Fcons (make_number (from), make_number (to)),
1162 Fcons (elt, Qnil));
1163 XSETCDR (XCAR (val), Fcons (elt, Qnil));
1164 XSETCAR (val, XCDR (XCAR (val)));
1165 }
1166 i = to + 1;
1167 }
1168
1169 for (tail = FONTSET_CHARSET_ALIST (fontset);
1170 CONSP (tail); tail = XCDR (tail))
1171 {
1172 elt = XCAR (tail);
1173 elt = Fcons (XCAR (elt), Fcons (XCDR (elt), Qnil));
1174 XSETCDR (XCAR (val), Fcons (elt, Qnil));
1175 XSETCAR (val, XCDR (XCAR (val)));
1176 }
1177
1178 val = XCDR (val);
1179
1180 /* If fonts are opened for FONT-SPEC, append the names of the fonts to
1181 FONT-SPEC. */
1182 for (tail = val; CONSP (tail); tail = XCDR (tail))
1183 {
1184 int c;
1185
1186 elt = XCAR (tail);
1187 for (i = 0; i < n_realized; i++)
1188 {
1189 int face_id;
1190 struct face *face;
1191 Lisp_Object face_list, fontname;
1192
1193 for (face_list = FONTSET_FACE_ALIST (realized[i]);
1194 CONSP (face_list); face_list = XCDR (face_list))
1195 {
1196 int face_id = XINT (XCDR (XCAR (face_list)));
1197 struct face *face = FACE_FROM_ID (f, face_id);
1198
1199 if (face->font && face->font_name)
1200 {
1201 fontname = build_string (face->font_name);
1202 if (NILP (Fmember (fontname, XCDR (XCDR (elt)))))
1203 XSETCDR (XCDR (elt), Fcons (fontname, XCDR (XCDR (elt))));
1204 }
1205 }
1206 }
1207 }
1208
1209 elt = XCDR (XCDR (XCAR (val)));
1210 if (CONSP (elt))
1211 fontp = (*query_font_func) (f, XSTRING (XCAR (elt))->data);
1212 val = Fmake_vector (make_number (3), val);
1213 AREF (val, 0) = fontp ? make_number (fontp->size) : make_number (0);
1214 AREF (val, 1) = fontp ? make_number (fontp->height) : make_number (0);
1215 return val;
1216 }
1217
1218 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 2, 0,
1219 doc: /* Return a font name pattern for character CH in fontset NAME.
1220 If NAME is t, find a font name pattern in the default fontset. */)
1221 (name, ch)
1222 Lisp_Object name, ch;
1223 {
1224 int c;
1225 Lisp_Object fontset, elt;
1226
1227 fontset = check_fontset_name (name);
1228
1229 CHECK_CHARACTER (ch);
1230 c = XINT (ch);
1231 FONTSET_REF (fontset, c, elt);
1232 return elt;
1233 }
1234
1235 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
1236 doc: /* Return a list of all defined fontset names. */)
1237 ()
1238 {
1239 Lisp_Object fontset, list;
1240 int i;
1241
1242 list = Qnil;
1243 for (i = 0; i < ASIZE (Vfontset_table); i++)
1244 {
1245 fontset = FONTSET_FROM_ID (i);
1246 if (!NILP (fontset)
1247 && BASE_FONTSET_P (fontset))
1248 list = Fcons (FONTSET_NAME (fontset), list);
1249 }
1250
1251 return list;
1252 }
1253
1254 void
1255 syms_of_fontset ()
1256 {
1257 if (!load_font_func)
1258 /* Window system initializer should have set proper functions. */
1259 abort ();
1260
1261 Qfontset = intern ("fontset");
1262 staticpro (&Qfontset);
1263 Fput (Qfontset, Qchar_table_extra_slots, make_number (7));
1264
1265 Vcached_fontset_data = Qnil;
1266 staticpro (&Vcached_fontset_data);
1267
1268 Vfontset_table = Fmake_vector (make_number (32), Qnil);
1269 staticpro (&Vfontset_table);
1270
1271 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
1272 staticpro (&Vdefault_fontset);
1273 FONTSET_ID (Vdefault_fontset) = make_number (0);
1274 FONTSET_NAME (Vdefault_fontset)
1275 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
1276 {
1277 Lisp_Object default_ascii_font;
1278
1279 #if defined (macintosh)
1280 default_ascii_font
1281 = build_string ("-apple-monaco-medium-r-*--*-120-*-*-*-*-mac-roman");
1282 #elif defined (WINDOWSNT)
1283 default_ascii_font
1284 = build_string ("-*-courier new-normal-r-*-*-*-100-*-*-*-*-iso8859-1");
1285 #else
1286 default_ascii_font
1287 = build_string ("-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
1288 #endif
1289 FONTSET_ASCII (Vdefault_fontset) = default_ascii_font;
1290 }
1291 AREF (Vfontset_table, 0) = Vdefault_fontset;
1292 next_fontset_id = 1;
1293
1294 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
1295 doc: /* Alist of fontname patterns vs corresponding encoding info.
1296 Each element looks like (REGEXP . ENCODING-INFO),
1297 where ENCODING-INFO is an alist of CHARSET vs ENCODING.
1298 ENCODING is one of the following integer values:
1299 0: code points 0x20..0x7F or 0x2020..0x7F7F are used,
1300 1: code points 0xA0..0xFF or 0xA0A0..0xFFFF are used,
1301 2: code points 0x20A0..0x7FFF are used,
1302 3: code points 0xA020..0xFF7F are used. */);
1303 Vfont_encoding_alist = Qnil;
1304
1305 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
1306 doc: /* Char table of characters whose ascent values should be ignored.
1307 If an entry for a character is non-nil, the ascent value of the glyph
1308 is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.
1309
1310 This affects how a composite character which contains
1311 such a character is displayed on screen. */);
1312 Vuse_default_ascent = Qnil;
1313
1314 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
1315 doc: /* Char table of characters which is not composed relatively.
1316 If an entry for a character is non-nil, a composition sequence
1317 which contains that character is displayed so that
1318 the glyph of that character is put without considering
1319 an ascent and descent value of a previous character. */);
1320 Vignore_relative_composition = Qnil;
1321
1322 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
1323 doc: /* Alist of fontname vs list of the alternate fontnames.
1324 When a specified font name is not found, the corresponding
1325 alternate fontnames (if any) are tried instead. */);
1326 Valternate_fontname_alist = Qnil;
1327
1328 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
1329 doc: /* Alist of fontset names vs the aliases. */);
1330 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
1331 build_string ("fontset-default")),
1332 Qnil);
1333
1334 DEFVAR_LISP ("vertical-centering-font-regexp",
1335 &Vvertical_centering_font_regexp,
1336 doc: /* *Regexp matching font names that require vertical centering on display.
1337 When a character is displayed with such fonts, the character is displayed
1338 at the vertical center of lines. */);
1339 Vvertical_centering_font_regexp = Qnil;
1340
1341 defsubr (&Squery_fontset);
1342 defsubr (&Snew_fontset);
1343 defsubr (&Sset_fontset_font);
1344 defsubr (&Sfont_info);
1345 defsubr (&Sinternal_char_font);
1346 defsubr (&Sfontset_info);
1347 defsubr (&Sfontset_font);
1348 defsubr (&Sfontset_list);
1349 }