(post-read-decode-hz)
[emacs.git] / src / fontset.c
blob34d27ce92a489d8972ebe410fd18609d1ce1b77c
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
8 This file is part of GNU Emacs.
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.
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.
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. */
25 /* #define FONTSET_DEBUG */
27 #include <config.h>
29 #ifdef FONTSET_DEBUG
30 #include <stdio.h>
31 #endif
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"
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
52 EXFUN (Fclear_face_cache, 1);
54 /* FONTSET
56 A fontset is a collection of font related information to give
57 similar appearance (style, etc) of characters. There are two kinds
58 of fontsets; base and realized. A base fontset is created by
59 `new-fontset' from Emacs Lisp explicitly. A realized fontset is
60 created implicitly when a face is realized for ASCII characters. A
61 face is also realized for non-ASCII characters based on an ASCII
62 face. All of non-ASCII faces based on the same ASCII face share
63 the same realized fontset.
65 A fontset object is implemented by a char-table whose default value
66 and parent are always nil.
68 An element of a base fontset is a font specification of the form:
69 [ FAMILY WEIGHT SLANT SWIDTH REGISTRY ] (vector of size 5)
71 FONT-NAME (strig)
73 FAMILY and REGISTRY are strings.
75 WEIGHT, SLANT, and SWIDTH must be symbols that set-face-attribute
76 accepts as attribute values for :weight, :slant, :swidth
77 respectively.
80 A fontset has 7 extra slots.
82 The 1st slot is an ID number of the fontset.
84 The 2nd slot is a name of the fontset in a base fontset, and nil in
85 a realized fontset.
87 The 3rd slot is nil in a base fontset, and a base fontset in a
88 realized fontset.
90 The 4th slot is a frame that the fontset belongs to. This is nil
91 in a base fontset.
93 The 5th slot is a cons of 0 and fontname for ASCII characters in a
94 base fontset, and nil in a realized face.
96 The 6th slot is an alist of a charset vs. the corresponding font
97 specification.
99 The 7th slot is an alist of a font specification vs. the
100 corresponding face ID. In a base fontset, the face IDs are all
101 nil.
103 All fontsets are recorded in Vfontset_table.
106 DEFAULT FONTSET
108 There's a special fontset named `default fontset' which defines the
109 default font specifications. When a base fontset doesn't specify a
110 font for a specific character, the corresponding value in the
111 default fontset is used. The format is the same as a base fontset.
113 The parent of a realized fontset created for such a face that has
114 no fontset is the default fontset.
117 These structures are hidden from the other codes than this file.
118 The other codes handle fontsets only by their ID numbers. They
119 usually use the variable name `fontset' for IDs. But, in this
120 file, we always use varialbe name `id' for IDs, and name `fontset'
121 for the actual fontset objects (i.e. char-table objects).
125 /********** VARIABLES and FUNCTION PROTOTYPES **********/
127 extern Lisp_Object Qfont;
128 Lisp_Object Qfontset;
130 /* Vector containing all fontsets. */
131 static Lisp_Object Vfontset_table;
133 /* Next possibly free fontset ID. Usually this keeps the minimum
134 fontset ID not yet used. */
135 static int next_fontset_id;
137 /* The default fontset. This gives default FAMILY and REGISTRY of
138 font for each character. */
139 static Lisp_Object Vdefault_fontset;
141 Lisp_Object Vfont_encoding_alist;
142 Lisp_Object Vuse_default_ascent;
143 Lisp_Object Vignore_relative_composition;
144 Lisp_Object Valternate_fontname_alist;
145 Lisp_Object Vfontset_alias_alist;
146 Lisp_Object Vvertical_centering_font_regexp;
148 /* The following six are declarations of callback functions depending
149 on window system. See the comments in src/fontset.h for more
150 detail. */
152 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
153 struct font_info *(*get_font_info_func) P_ ((FRAME_PTR f, int font_idx));
155 /* Return a list of font names which matches PATTERN. See the documentation
156 of `x-list-fonts' for more details. */
157 Lisp_Object (*list_fonts_func) P_ ((struct frame *f,
158 Lisp_Object pattern,
159 int size,
160 int maxnames));
162 /* Load a font named NAME for frame F and return a pointer to the
163 information of the loaded font. If loading is failed, return 0. */
164 struct font_info *(*load_font_func) P_ ((FRAME_PTR f, char *name, int));
166 /* Return a pointer to struct font_info of a font named NAME for frame F. */
167 struct font_info *(*query_font_func) P_ ((FRAME_PTR f, char *name));
169 /* Additional function for setting fontset or changing fontset
170 contents of frame F. */
171 void (*set_frame_fontset_func) P_ ((FRAME_PTR f, Lisp_Object arg,
172 Lisp_Object oldval));
174 /* To find a CCL program, fs_load_font calls this function.
175 The argument is a pointer to the struct font_info.
176 This function set the member `encoder' of the structure. */
177 void (*find_ccl_program_func) P_ ((struct font_info *));
179 /* Check if any window system is used now. */
180 void (*check_window_system_func) P_ ((void));
183 /* Prototype declarations for static functions. */
184 static Lisp_Object make_fontset P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
185 static int fontset_id_valid_p P_ ((int));
186 static Lisp_Object fontset_pattern_regexp P_ ((Lisp_Object));
189 /********** MACROS AND FUNCTIONS TO HANDLE FONTSET **********/
191 /* Return the fontset with ID. No check of ID's validness. */
192 #define FONTSET_FROM_ID(id) AREF (Vfontset_table, id)
194 /* Macros to access special values of FONTSET. */
195 #define FONTSET_ID(fontset) XCHAR_TABLE (fontset)->extras[0]
197 /* Macros to access special values of (base) FONTSET. */
198 #define FONTSET_NAME(fontset) XCHAR_TABLE (fontset)->extras[1]
199 #define FONTSET_ASCII(fontset) XCHAR_TABLE (fontset)->extras[4]
201 #define BASE_FONTSET_P(fontset) STRINGP (FONTSET_NAME (fontset))
203 /* Macros to access special values of (realized) FONTSET. */
204 #define FONTSET_BASE(fontset) XCHAR_TABLE (fontset)->extras[2]
205 #define FONTSET_FRAME(fontset) XCHAR_TABLE (fontset)->extras[3]
206 #define FONTSET_CHARSET_ALIST(fontset) XCHAR_TABLE (fontset)->extras[5]
207 #define FONTSET_FACE_ALIST(fontset) XCHAR_TABLE (fontset)->extras[6]
210 /* Return the element of FONTSET (char-table) at index C (character). */
212 #define FONTSET_REF(fontset, c, etl) ((elt) = fontset_ref ((fontset), (c)))
214 static Lisp_Object
215 fontset_ref (fontset, c)
216 Lisp_Object fontset;
217 int c;
219 Lisp_Object elt;
221 while (1)
223 elt = CHAR_TABLE_REF (fontset, c);
224 if (NILP (elt) && ASCII_CHAR_P (c))
225 elt = FONTSET_ASCII (fontset);
226 if (NILP (elt))
228 Lisp_Object tail;
229 struct charset *charset;
231 for (tail = FONTSET_CHARSET_ALIST (fontset);
232 CONSP (tail); tail = XCDR (tail))
234 charset = CHARSET_FROM_ID (XCAR (XCAR (tail)));
235 if (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset))
237 elt = XCDR (XCAR (tail));
238 break;
242 if (! NILP (elt) || EQ (fontset, Vdefault_fontset))
243 break;
244 fontset = Vdefault_fontset;
246 return elt;
250 /* Set the element of FONTSET at index IDX to the value ELT. IDX may
251 be a character or a charset. */
253 #define FONTSET_SET(fontset, c, newelt) fontset_set(fontset, c, newelt)
255 static void
256 fontset_set (fontset, idx, elt)
257 Lisp_Object fontset, idx, elt;
259 if (SYMBOLP (idx))
261 Lisp_Object id, slot, tail;
263 id = make_number (CHARSET_SYMBOL_ID (idx));
264 if (id == charset_ascii)
265 Fset_char_table_range (fontset,
266 Fcons (make_number (0), make_number (127)),
267 elt);
268 else
270 slot = Fassq (id, FONTSET_CHARSET_ALIST (fontset));
271 if (CONSP (slot))
272 XCDR (slot) = elt;
273 else if (CONSP (FONTSET_CHARSET_ALIST (fontset)))
275 for (tail = FONTSET_CHARSET_ALIST (fontset);
276 CONSP (XCDR (tail)); tail = XCDR (tail));
277 XCDR (tail) = Fcons (Fcons (id, elt), Qnil);
279 else
280 FONTSET_CHARSET_ALIST (fontset) = Fcons (Fcons (id, elt), Qnil);
283 else
285 int from = XINT (XCAR (idx));
286 int to = XINT (XCDR (idx));
288 if (from == to)
289 CHAR_TABLE_SET (fontset, from, elt);
290 else
291 Fset_char_table_range (fontset, idx, elt);
296 /* Return a face registerd in the realized fontset FONTSET for the
297 character C. Return -1 if a face ID is not yet set. */
299 static struct face *
300 fontset_face (fontset, c)
301 Lisp_Object fontset;
302 int c;
304 Lisp_Object base, elt;
305 int id;
306 struct face *face;
308 base = FONTSET_BASE (fontset);
309 FONTSET_REF (base, c, elt);
311 if (NILP (elt))
312 return NULL;
314 elt = Fassoc (elt, FONTSET_FACE_ALIST (fontset));
315 if (! CONSP (elt))
316 return NULL;
317 id = XINT (XCDR (elt));
318 face = FACE_FROM_ID (XFRAME (FONTSET_FRAME (fontset)), id);
319 return face;
323 /* Return a newly created fontset with NAME. If BASE is nil, make a
324 base fontset. Otherwise make a realized fontset whose base is
325 BASE. */
327 static Lisp_Object
328 make_fontset (frame, name, base)
329 Lisp_Object frame, name, base;
331 Lisp_Object fontset;
332 int size = ASIZE (Vfontset_table);
333 int id = next_fontset_id;
335 /* Find a free slot in Vfontset_table. Usually, next_fontset_id is
336 the next available fontset ID. So it is expected that this loop
337 terminates quickly. In addition, as the last element of
338 Vfontset_table is always nil, we don't have to check the range of
339 id. */
340 while (!NILP (AREF (Vfontset_table, id))) id++;
342 if (id + 1 == size)
344 Lisp_Object tem;
345 int i;
347 tem = Fmake_vector (make_number (size + 32), Qnil);
348 for (i = 0; i < size; i++)
349 AREF (tem, i) = AREF (Vfontset_table, i);
350 Vfontset_table = tem;
353 fontset = Fmake_char_table (Qfontset, Qnil);
355 FONTSET_ID (fontset) = make_number (id);
356 if (NILP (base))
358 FONTSET_NAME (fontset) = name;
360 else
362 FONTSET_NAME (fontset) = Qnil;
363 FONTSET_FRAME (fontset) = frame;
364 FONTSET_BASE (fontset) = base;
367 ASET (Vfontset_table, id, fontset);
368 next_fontset_id = id + 1;
369 return fontset;
374 /********** INTERFACES TO xfaces.c and dispextern.h **********/
376 /* Return name of the fontset with ID. */
378 Lisp_Object
379 fontset_name (id)
380 int id;
382 Lisp_Object fontset;
384 fontset = FONTSET_FROM_ID (id);
385 return FONTSET_NAME (fontset);
389 /* Return ASCII font name of the fontset with ID. */
391 Lisp_Object
392 fontset_ascii (id)
393 int id;
395 Lisp_Object fontset;
397 fontset= FONTSET_FROM_ID (id);
398 return FONTSET_ASCII (fontset);
402 /* Free fontset of FACE defined on frame F. Called from
403 free_realized_face. */
405 void
406 free_face_fontset (f, face)
407 FRAME_PTR f;
408 struct face *face;
410 AREF (Vfontset_table, face->fontset) = Qnil;
411 if (face->fontset < next_fontset_id)
412 next_fontset_id = face->fontset;
416 /* Return 1 iff FACE is suitable for displaying character C.
417 Otherwise return 0. Called from the macro FACE_SUITABLE_FOR_CHAR_P
418 when C is not an ASCII character. */
421 face_suitable_for_char_p (face, c)
422 struct face *face;
423 int c;
425 Lisp_Object fontset;
427 fontset = FONTSET_FROM_ID (face->fontset);
428 return (face == fontset_face (fontset, c));
432 /* Return ID of face suitable for displaying character C on frame F.
433 The selection of face is done based on the fontset of FACE. FACE
434 must be reazlied for ASCII characters in advance. Called from the
435 macro FACE_FOR_CHAR when C is not an ASCII character. */
438 face_for_char (f, face, c)
439 FRAME_PTR f;
440 struct face *face;
441 int c;
443 Lisp_Object fontset;
444 struct face *new_face;
446 xassert (fontset_id_valid_p (face->fontset));
447 fontset = FONTSET_FROM_ID (face->fontset);
448 xassert (!BASE_FONTSET_P (fontset));
450 new_face = fontset_face (fontset, c);
451 if (new_face)
452 return new_face->id;
454 /* No face is recorded for C in the fontset of FACE. Make a new
455 realized face for C that has the same fontset. */
456 return lookup_face (f, face->lface, c, face);
460 /* Make a realized fontset for ASCII face FACE on frame F from the
461 base fontset BASE_FONTSET_ID. If BASE_FONTSET_ID is -1, use the
462 default fontset as the base. Value is the id of the new fontset.
463 Called from realize_x_face. */
466 make_fontset_for_ascii_face (f, base_fontset_id)
467 FRAME_PTR f;
468 int base_fontset_id;
470 Lisp_Object base_fontset, fontset, frame;
472 XSETFRAME (frame, f);
473 if (base_fontset_id >= 0)
475 base_fontset = FONTSET_FROM_ID (base_fontset_id);
476 if (!BASE_FONTSET_P (base_fontset))
477 base_fontset = FONTSET_BASE (base_fontset);
478 xassert (BASE_FONTSET_P (base_fontset));
480 else
481 base_fontset = Vdefault_fontset;
483 fontset = make_fontset (frame, Qnil, base_fontset);
484 return XINT (FONTSET_ID (fontset));
488 /* Return FONT-SPEC recorded in the fontset of FACE for character C.
489 If FACE is null, or the fontset doesn't contain information about
490 C, get the font name pattern from the default fontset. Called from
491 choose_face_font. */
493 Lisp_Object
494 fontset_font_pattern (f, face, c)
495 FRAME_PTR f;
496 struct face *face;
497 int c;
499 Lisp_Object fontset, base, elt;
500 int id = face ? face->fontset : -1;
502 if (id >= 0)
504 fontset = FONTSET_FROM_ID (id);
505 xassert (!BASE_FONTSET_P (fontset));
506 base = FONTSET_BASE (fontset);
508 else
510 base = Vdefault_fontset;
513 FONTSET_REF (base, c, elt);
514 if (face && ! NILP (elt))
516 Lisp_Object slot;
518 slot = Fassoc (elt, FONTSET_FACE_ALIST (fontset));
519 if (CONSP (slot))
520 XSETCDR (slot, make_number (face->id));
521 FONTSET_FACE_ALIST (fontset)
522 = Fcons (Fcons (elt, make_number (face->id)),
523 FONTSET_FACE_ALIST (fontset));
525 return elt;
529 #if defined(WINDOWSNT) && defined (_MSC_VER)
530 #pragma optimize("", off)
531 #endif
533 /* Load a font named FONTNAME on frame F. Return a pointer to the
534 struct font_info of the loaded font. If loading fails, return
535 NULL. */
537 struct font_info *
538 fs_load_font (f, fontname)
539 FRAME_PTR f;
540 char *fontname;
542 Lisp_Object tail, elt;
543 struct font_info *fontp;
545 if (!fontname)
546 /* No way to get fontname. */
547 return 0;
549 fontp = (*load_font_func) (f, fontname, 0);
550 if (!fontp)
551 return NULL;
553 fontname = fontp->full_name;
554 /* Fill in members (charset, vertical_centering, encoding, etc) of
555 font_info structure that are not set by (*load_font_func). */
556 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
558 elt = XCAR (tail);
559 if (STRINGP (XCAR (elt)) && CHARSETP (XCDR (elt))
560 && fast_c_string_match_ignore_case (XCAR (elt), fontname) >= 0)
562 fontp->charset = CHARSET_SYMBOL_ID (XCDR (elt));
563 break;
566 if (! CONSP (tail))
567 return NULL;
569 fontp->vertical_centering
570 = (STRINGP (Vvertical_centering_font_regexp)
571 && (fast_c_string_match_ignore_case
572 (Vvertical_centering_font_regexp, fontname) >= 0));
574 fontp->font_encoder = NULL;
576 if (find_ccl_program_func)
577 (*find_ccl_program_func) (fontp);
579 return fontp;
582 #if defined(WINDOWSNT) && defined (_MSC_VER)
583 #pragma optimize("", on)
584 #endif
587 /* Cache data used by fontset_pattern_regexp. The car part is a
588 pattern string containing at least one wild card, the cdr part is
589 the corresponding regular expression. */
590 static Lisp_Object Vcached_fontset_data;
592 #define CACHED_FONTSET_NAME (XSTRING (XCAR (Vcached_fontset_data))->data)
593 #define CACHED_FONTSET_REGEX (XCDR (Vcached_fontset_data))
595 /* If fontset name PATTERN contains any wild card, return regular
596 expression corresponding to PATTERN. */
598 static Lisp_Object
599 fontset_pattern_regexp (pattern)
600 Lisp_Object pattern;
602 if (!index (XSTRING (pattern)->data, '*')
603 && !index (XSTRING (pattern)->data, '?'))
604 /* PATTERN does not contain any wild cards. */
605 return Qnil;
607 if (!CONSP (Vcached_fontset_data)
608 || strcmp (XSTRING (pattern)->data, CACHED_FONTSET_NAME))
610 /* We must at first update the cached data. */
611 char *regex = (char *) alloca (XSTRING (pattern)->size * 2 + 3);
612 char *p0, *p1 = regex;
614 /* Convert "*" to ".*", "?" to ".". */
615 *p1++ = '^';
616 for (p0 = (char *) XSTRING (pattern)->data; *p0; p0++)
618 if (*p0 == '*')
620 *p1++ = '.';
621 *p1++ = '*';
623 else if (*p0 == '?')
624 *p1++ = '.';
625 else
626 *p1++ = *p0;
628 *p1++ = '$';
629 *p1++ = 0;
631 Vcached_fontset_data = Fcons (build_string (XSTRING (pattern)->data),
632 build_string (regex));
635 return CACHED_FONTSET_REGEX;
638 /* Return ID of the base fontset named NAME. If there's no such
639 fontset, return -1. */
642 fs_query_fontset (name, regexpp)
643 Lisp_Object name;
644 int regexpp;
646 Lisp_Object tem;
647 int i;
649 name = Fdowncase (name);
650 if (!regexpp)
652 tem = Frassoc (name, Vfontset_alias_alist);
653 if (CONSP (tem) && STRINGP (XCAR (tem)))
654 name = XCAR (tem);
655 else
657 tem = fontset_pattern_regexp (name);
658 if (STRINGP (tem))
660 name = tem;
661 regexpp = 1;
666 for (i = 0; i < ASIZE (Vfontset_table); i++)
668 Lisp_Object fontset;
669 unsigned char *this_name;
671 fontset = FONTSET_FROM_ID (i);
672 if (NILP (fontset)
673 || !BASE_FONTSET_P (fontset))
674 continue;
676 this_name = XSTRING (FONTSET_NAME (fontset))->data;
677 if (regexpp
678 ? fast_c_string_match_ignore_case (name, this_name) >= 0
679 : !strcmp (XSTRING (name)->data, this_name))
680 return i;
682 return -1;
686 DEFUN ("query-fontset", Fquery_fontset, Squery_fontset, 1, 2, 0,
687 doc: /* Return the name of a fontset that matches PATTERN.
688 The value is nil if there is no matching fontset.
689 PATTERN can contain `*' or `?' as a wildcard
690 just as X font name matching algorithm allows.
691 If REGEXPP is non-nil, PATTERN is a regular expression. */)
692 (pattern, regexpp)
693 Lisp_Object pattern, regexpp;
695 Lisp_Object fontset;
696 int id;
698 (*check_window_system_func) ();
700 CHECK_STRING (pattern);
702 if (XSTRING (pattern)->size == 0)
703 return Qnil;
705 id = fs_query_fontset (pattern, !NILP (regexpp));
706 if (id < 0)
707 return Qnil;
709 fontset = FONTSET_FROM_ID (id);
710 return FONTSET_NAME (fontset);
713 /* Return a list of base fontset names matching PATTERN on frame F. */
715 Lisp_Object
716 list_fontsets (f, pattern, size)
717 FRAME_PTR f;
718 Lisp_Object pattern;
719 int size;
721 Lisp_Object frame, regexp, val;
722 int id;
724 XSETFRAME (frame, f);
726 regexp = fontset_pattern_regexp (pattern);
727 val = Qnil;
729 for (id = 0; id < ASIZE (Vfontset_table); id++)
731 Lisp_Object fontset;
732 unsigned char *name;
734 fontset = FONTSET_FROM_ID (id);
735 if (NILP (fontset)
736 || !BASE_FONTSET_P (fontset)
737 || !EQ (frame, FONTSET_FRAME (fontset)))
738 continue;
739 name = XSTRING (FONTSET_NAME (fontset))->data;
741 if (!NILP (regexp)
742 ? (fast_c_string_match_ignore_case (regexp, name) < 0)
743 : strcmp (XSTRING (pattern)->data, name))
744 continue;
746 val = Fcons (Fcopy_sequence (FONTSET_NAME (fontset)), val);
749 return val;
753 /* Free all realized fontsets whose base fontset is BASE. */
755 static void
756 free_realized_fontsets (base)
757 Lisp_Object base;
759 #if 0
760 int id;
762 /* For the moment, this doesn't work because free_realized_face
763 doesn't remove FACE from a cache. Until we find a solution, we
764 suppress this code, and simply use Fclear_face_cache even though
765 that is not efficient. */
766 BLOCK_INPUT;
767 for (id = 0; id < ASIZE (Vfontset_table); id++)
769 Lisp_Object this = AREF (Vfontset_table, id);
771 if (EQ (FONTSET_BASE (this), base))
773 Lisp_Object tail;
775 for (tail = FONTSET_FACE_ALIST (this); CONSP (tail);
776 tail = XCDR (tail))
778 FRAME_PTR f = XFRAME (FONTSET_FRAME (this));
779 int face_id = XINT (XCDR (XCAR (tail)));
780 struct face *face = FACE_FROM_ID (f, face_id);
782 /* Face THIS itself is also freed by the following call. */
783 free_realized_face (f, face);
787 UNBLOCK_INPUT;
788 #else /* not 0 */
789 Fclear_face_cache (Qt);
790 #endif /* not 0 */
794 /* Check validity of NAME as a fontset name and return the
795 corresponding fontset. If not valid, signal an error.
796 If NAME is t, return Vdefault_fontset. */
798 static Lisp_Object
799 check_fontset_name (name)
800 Lisp_Object name;
802 int id;
804 if (EQ (name, Qt))
805 return Vdefault_fontset;
807 CHECK_STRING (name);
808 id = fs_query_fontset (name, 0);
809 if (id < 0)
810 error ("Fontset `%s' does not exist", XSTRING (name)->data);
811 return FONTSET_FROM_ID (id);
814 DEFUN ("set-fontset-font", Fset_fontset_font, Sset_fontset_font, 3, 4, 0,
815 doc: /* Modify fontset NAME to use FONT-SPEC for characters of CHARSETS.
817 CHARSET may be a cons; (FROM . TO), where FROM and TO are characters.
818 In that case, use FONT-SPEC for all characters in the range FROM and
819 TO (inclusive).
821 FONT-SPEC is be a vector; [ FAMILY WEIGHT SLANT WIDTH ADSTYLE REGISTRY ]
823 FONT-SPEC may be a cons; (FAMILY . REGISTRY), where FAMILY is a family
824 name of a font, REGSITRY is a registry name of a font.
826 FONT-SPEC may be a font name string. */)
827 (name, charset, font_spec, frame)
828 Lisp_Object name, charset, font_spec, frame;
830 Lisp_Object fontset;
831 Lisp_Object family, registry;
833 fontset = check_fontset_name (name);
835 if (VECTORP (font_spec))
837 int i;
838 Lisp_Object val;
840 font_spec = Fcopy_sequence (font_spec);
841 for (i = 0; i < 5; i++)
843 val = Faref (font_spec, make_number (i));
844 if (! NILP (val))
846 CHECK_STRING (val);
847 ASET (font_spec, i, Fdowncase (val));
850 val = Faref (font_spec, make_number (5));
851 CHECK_STRING (val);
852 ASET (font_spec, 5, Fdowncase (val));
854 else if (STRINGP (font_spec))
855 font_spec = Fdowncase (font_spec);
856 else if (CONSP (font_spec))
858 CHECK_CONS (font_spec);
859 family = XCAR (font_spec);
860 registry = XCDR (font_spec);
861 font_spec = Fmake_vector (make_number (6), Qnil);
862 if (!NILP (family))
864 CHECK_STRING (family);
865 ASET (font_spec, 0, Fdowncase (family));
867 CHECK_STRING (registry);
868 ASET (font_spec, 5, Fdowncase (registry));
871 if (SYMBOLP (charset))
873 CHECK_CHARSET (charset);
875 else
877 Lisp_Object from, to;
879 /* CHARSET should be (FROM . TO). */
880 from = Fcar (charset);
881 to = Fcdr (charset);
882 CHECK_CHARACTER (from);
883 CHECK_CHARACTER (to);
886 /* The arg FRAME is kept for backward compatibility. We only check
887 the validity. */
888 if (!NILP (frame))
889 CHECK_LIVE_FRAME (frame);
891 FONTSET_SET (fontset, charset, font_spec);
893 /* Free all realized fontsets whose base is FONTSET. This way, the
894 specified character(s) are surely redisplayed by a correct
895 font. */
896 free_realized_fontsets (fontset);
898 return Qnil;
902 DEFUN ("new-fontset", Fnew_fontset, Snew_fontset, 2, 2, 0,
903 doc: /* Create a new fontset NAME from font information in FONTLIST.
905 FONTLIST is an alist of charsets vs corresponding font specifications.
906 Each element of FONTLIST has the form (CHARSET . FONT-SPEC), where
907 a character of CHARSET is displayed by a font that matches FONT-SPEC.
909 FONT-SPEC is a vector [ FAMILY WEIGHT SLANT WIDTH ADSTYLE REGISTRY ], where
910 FAMILY is a string specifying the font family,
911 WEIGHT is a string specifying the weight of the font,
912 SLANT is a string specifying the slant of the font,
913 WIDTH is a string specifying the width of the font,
914 ADSTYLE is a string specifying the adstyle of the font,
915 REGISTRY is a string specifying the charset-registry of the font.
917 See also the documentation of `set-face-attribute' for the detail of
918 these vector elements.
920 FONT-SPEC may be a font name (string). */)
921 (name, fontlist)
922 Lisp_Object name, fontlist;
924 Lisp_Object fontset, ascii_font;
925 Lisp_Object tem, tail;
927 CHECK_STRING (name);
928 CHECK_LIST (fontlist);
930 name = Fdowncase (name);
931 tem = Fquery_fontset (name, Qnil);
932 if (! NILP (tem))
933 free_realized_fontsets (tem);
935 fontset = make_fontset (Qnil, name, Qnil);
937 /* Check the validity of FONTLIST. */
938 ascii_font = Fcdr (Fassq (Qascii, fontlist));
939 if (NILP (ascii_font))
940 error ("No ascii font specified");
941 if (! STRINGP (ascii_font))
942 ascii_font = generate_ascii_font (name, ascii_font);
944 fontlist = Fcopy_sequence (fontlist);
945 for (tail = fontlist; ! NILP (tail); tail = Fcdr (tail))
946 Fset_fontset_font (name, Fcar (Fcar (tail)), Fcdr (Fcar (tail)), Qnil);
948 FONTSET_ASCII (fontset) = ascii_font;
950 return name;
954 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
955 doc: /* Return information about a font named NAME on frame FRAME.
956 If FRAME is omitted or nil, use the selected frame.
957 The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
958 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
959 where
960 OPENED-NAME is the name used for opening the font,
961 FULL-NAME is the full name of the font,
962 SIZE is the maximum bound width of the font,
963 HEIGHT is the height of the font,
964 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
965 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
966 how to compose characters.
967 If the named font is not yet loaded, return nil. */)
968 (name, frame)
969 Lisp_Object name, frame;
971 FRAME_PTR f;
972 struct font_info *fontp;
973 Lisp_Object info;
975 (*check_window_system_func) ();
977 CHECK_STRING (name);
978 name = Fdowncase (name);
979 if (NILP (frame))
980 frame = selected_frame;
981 CHECK_LIVE_FRAME (frame);
982 f = XFRAME (frame);
984 if (!query_font_func)
985 error ("Font query function is not supported");
987 fontp = (*query_font_func) (f, XSTRING (name)->data);
988 if (!fontp)
989 return Qnil;
991 info = Fmake_vector (make_number (7), Qnil);
993 XVECTOR (info)->contents[0] = build_string (fontp->name);
994 XVECTOR (info)->contents[1] = build_string (fontp->full_name);
995 XVECTOR (info)->contents[2] = make_number (fontp->size);
996 XVECTOR (info)->contents[3] = make_number (fontp->height);
997 XVECTOR (info)->contents[4] = make_number (fontp->baseline_offset);
998 XVECTOR (info)->contents[5] = make_number (fontp->relative_compose);
999 XVECTOR (info)->contents[6] = make_number (fontp->default_ascent);
1001 return info;
1005 /* Return the font name for the character at POSITION in the current
1006 buffer. This is computed from all the text properties and overlays
1007 that apply to POSITION. It returns nil in the following cases:
1009 (1) The window system doesn't have a font for the character (thus
1010 it is displayed by an empty box).
1012 (2) The character code is invalid.
1014 (3) The current buffer is not displayed in any window.
1016 In addition, the returned font name may not take into account of
1017 such redisplay engine hooks as what used in jit-lock-mode if
1018 POSITION is currently not visible. */
1021 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 1, 0,
1022 doc: /* For internal use only. */)
1023 (position)
1024 Lisp_Object position;
1026 int pos, pos_byte, dummy;
1027 int face_id;
1028 int c;
1029 Lisp_Object window;
1030 struct window *w;
1031 struct frame *f;
1032 struct face *face;
1034 CHECK_NUMBER_COERCE_MARKER (position);
1035 pos = XINT (position);
1036 if (pos < BEGV || pos >= ZV)
1037 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1038 pos_byte = CHAR_TO_BYTE (pos);
1039 c = FETCH_CHAR (pos_byte);
1040 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1041 if (NILP (window))
1042 return Qnil;
1043 w = XWINDOW (window);
1044 f = XFRAME (w->frame);
1045 face_id = face_at_buffer_position (w, pos, -1, -1, &dummy, pos + 100, 0);
1046 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c);
1047 face = FACE_FROM_ID (f, face_id);
1048 return (face->font && face->font_name
1049 ? build_string (face->font_name)
1050 : Qnil);
1054 #if 0 /* unused */
1055 /* Called from Ffontset_info via map_char_table on each leaf of
1056 fontset. ARG is a list (LAST FONT-INFO ...), where LAST is `(last
1057 ARG)' and FONT-INFOs have this form:
1058 (CHAR FONT-SPEC) or ((FROM . TO) FONT-SPEC)
1059 The current leaf is indexed by CHARACTER and has value ELT. This
1060 function add the information of the current leaf to ARG by
1061 appending a new element or modifying the last element.. */
1063 static void
1064 accumulate_font_info (arg, character, elt)
1065 Lisp_Object arg, character, elt;
1067 Lisp_Object last, last_char, last_elt;
1069 if (!CONSP (elt) && !SINGLE_BYTE_CHAR_P (XINT (character)))
1070 FONTSET_REF (Vdefault_fontset, XINT (character), elt);
1071 if (!CONSP (elt))
1072 return;
1073 last = XCAR (arg);
1074 last_char = XCAR (XCAR (last));
1075 last_elt = XCAR (XCDR (XCAR (last)));
1076 elt = XCDR (elt);
1077 if (!NILP (Fequal (elt, last_elt)))
1079 struct charset *this_charset = CHAR_CHARSET (XINT (character));
1081 if (CONSP (last_char)) /* LAST_CHAR == (FROM . TO) */
1083 if (this_charset == CHAR_CHARSET (XINT (XCAR (last_char))))
1085 XSETCDR (last_char, character);
1086 return;
1089 else if (XINT (last_char) == XINT (character))
1090 return;
1091 else if (this_charset == CHAR_CHARSET (XINT (last_char)))
1093 XSETCAR (XCAR (last), Fcons (last_char, character));
1094 return;
1097 XSETCDR (last, Fcons (Fcons (character, Fcons (elt, Qnil)), Qnil));
1098 XSETCAR (arg, XCDR (last));
1100 #endif /* 0 */
1102 DEFUN ("fontset-info", Ffontset_info, Sfontset_info, 1, 2, 0,
1103 doc: /* Return information about a fontset named NAME on frame FRAME.
1104 The value is a vector:
1105 [ SIZE HEIGHT ((CHARSET-OR-RANGE FONT-SPEC OPENED ...) ...) ],
1106 where,
1107 SIZE is the maximum bound width of ASCII font in the fontset,
1108 HEIGHT is the maximum bound height of ASCII font in the fontset,
1109 CHARSET-OR-RANGE is a charset or a cons of two characters specifying
1110 the range of characters.
1111 FONT-SPEC is a fontname pattern string or a vector
1112 [ FAMILY WEIGHT SLANT WIDTH ADSTYLE REGISTRY ].
1113 See the documentation of `new-fontset' for the meanings those elements.
1114 OPENEDs are names of fonts actually opened.
1115 If the ASCII font is not yet opened, SIZE and HEIGHT are 0.
1116 If FRAME is omitted, it defaults to the currently selected frame. */)
1117 (name, frame)
1118 Lisp_Object name, frame;
1120 Lisp_Object fontset;
1121 FRAME_PTR f;
1122 Lisp_Object val, tail, elt;
1123 Lisp_Object *realized;
1124 struct font_info *fontp = NULL;
1125 int n_realized = 0;
1126 int i;
1128 (*check_window_system_func) ();
1130 fontset = check_fontset_name (name);
1132 if (NILP (frame))
1133 frame = selected_frame;
1134 CHECK_LIVE_FRAME (frame);
1135 f = XFRAME (frame);
1137 /* Recode realized fontsets whose base is FONTSET in the table
1138 `realized'. */
1139 realized = (Lisp_Object *) alloca (sizeof (Lisp_Object)
1140 * ASIZE (Vfontset_table));
1141 for (i = 0; i < ASIZE (Vfontset_table); i++)
1143 elt = FONTSET_FROM_ID (i);
1144 if (!NILP (elt)
1145 && EQ (FONTSET_BASE (elt), fontset))
1146 realized[n_realized++] = elt;
1149 /* Accumulate information of the fontset in VAL. The format is
1150 (LAST FONT-INFO FONT-INFO ...), where FONT-INFO is (CHAR-OR-RANGE
1151 FONT-SPEC). See the comment for accumulate_font_info for the
1152 detail. */
1153 val = Fcons (Fcons (Qascii, Fcons (FONTSET_ASCII (fontset), Qnil)), Qnil);
1154 val = Fcons (val, val);
1155 for (i = 128; i <= MAX_CHAR; )
1157 Lisp_Object elt;
1158 int from, to;
1160 elt = char_table_ref_and_range (fontset, i, &from, &to);
1161 if (! NILP (elt))
1163 elt = Fcons (Fcons (make_number (from), make_number (to)),
1164 Fcons (elt, Qnil));
1165 XSETCDR (XCAR (val), Fcons (elt, Qnil));
1166 XSETCAR (val, XCDR (XCAR (val)));
1168 i = to + 1;
1171 for (tail = FONTSET_CHARSET_ALIST (fontset);
1172 CONSP (tail); tail = XCDR (tail))
1174 elt = XCAR (tail);
1175 elt = Fcons ((INTEGERP (XCAR (elt))
1176 ? CHARSET_NAME (CHARSET_FROM_ID (XCAR (elt)))
1177 : XCAR (elt)),
1178 Fcons (XCDR (elt), Qnil));
1179 XSETCDR (XCAR (val), Fcons (elt, Qnil));
1180 XSETCAR (val, XCDR (XCAR (val)));
1183 val = XCDR (val);
1185 /* If fonts are opened for FONT-SPEC, append the names of the fonts to
1186 FONT-SPEC. */
1187 for (tail = val; CONSP (tail); tail = XCDR (tail))
1189 elt = XCAR (tail);
1190 for (i = 0; i < n_realized; i++)
1192 Lisp_Object face_list, fontname;
1194 for (face_list = FONTSET_FACE_ALIST (realized[i]);
1195 CONSP (face_list); face_list = XCDR (face_list))
1197 int face_id = XINT (XCDR (XCAR (face_list)));
1198 struct face *face = FACE_FROM_ID (f, face_id);
1200 if (face->font && face->font_name)
1202 fontname = build_string (face->font_name);
1203 if (NILP (Fmember (fontname, XCDR (XCDR (elt)))))
1204 XSETCDR (XCDR (elt), Fcons (fontname, XCDR (XCDR (elt))));
1210 elt = XCDR (XCDR (XCAR (val)));
1211 if (CONSP (elt))
1212 fontp = (*query_font_func) (f, XSTRING (XCAR (elt))->data);
1213 val = Fmake_vector (make_number (3), val);
1214 AREF (val, 0) = fontp ? make_number (fontp->size) : make_number (0);
1215 AREF (val, 1) = fontp ? make_number (fontp->height) : make_number (0);
1216 return val;
1219 DEFUN ("fontset-font", Ffontset_font, Sfontset_font, 2, 2, 0,
1220 doc: /* Return a font name pattern for character CH in fontset NAME.
1221 If NAME is t, find a font name pattern in the default fontset. */)
1222 (name, ch)
1223 Lisp_Object name, ch;
1225 int c;
1226 Lisp_Object fontset, elt;
1228 fontset = check_fontset_name (name);
1230 CHECK_CHARACTER (ch);
1231 c = XINT (ch);
1232 FONTSET_REF (fontset, c, elt);
1233 return elt;
1236 DEFUN ("fontset-list", Ffontset_list, Sfontset_list, 0, 0, 0,
1237 doc: /* Return a list of all defined fontset names. */)
1240 Lisp_Object fontset, list;
1241 int i;
1243 list = Qnil;
1244 for (i = 0; i < ASIZE (Vfontset_table); i++)
1246 fontset = FONTSET_FROM_ID (i);
1247 if (!NILP (fontset)
1248 && BASE_FONTSET_P (fontset))
1249 list = Fcons (FONTSET_NAME (fontset), list);
1252 return list;
1255 void
1256 syms_of_fontset ()
1258 if (!load_font_func)
1259 /* Window system initializer should have set proper functions. */
1260 abort ();
1262 Qfontset = intern ("fontset");
1263 staticpro (&Qfontset);
1264 Fput (Qfontset, Qchar_table_extra_slots, make_number (7));
1266 Vcached_fontset_data = Qnil;
1267 staticpro (&Vcached_fontset_data);
1269 Vfontset_table = Fmake_vector (make_number (32), Qnil);
1270 staticpro (&Vfontset_table);
1272 Vdefault_fontset = Fmake_char_table (Qfontset, Qnil);
1273 staticpro (&Vdefault_fontset);
1274 FONTSET_ID (Vdefault_fontset) = make_number (0);
1275 FONTSET_NAME (Vdefault_fontset)
1276 = build_string ("-*-*-*-*-*-*-*-*-*-*-*-*-fontset-default");
1278 Lisp_Object default_ascii_font;
1280 #if defined (macintosh)
1281 default_ascii_font
1282 = build_string ("-apple-monaco-medium-r-*--*-120-*-*-*-*-mac-roman");
1283 #elif defined (WINDOWSNT)
1284 default_ascii_font
1285 = build_string ("-*-courier new-normal-r-*-*-*-100-*-*-*-*-iso8859-1");
1286 #else
1287 default_ascii_font
1288 = build_string ("-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
1289 #endif
1290 FONTSET_ASCII (Vdefault_fontset) = default_ascii_font;
1292 AREF (Vfontset_table, 0) = Vdefault_fontset;
1293 next_fontset_id = 1;
1295 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
1296 doc: /* Alist of fontname patterns vs corresponding encoding info.
1297 Each element looks like (REGEXP . CHARSET), where CHARSET is an
1298 Emacs charset symbol. */);
1299 Vfont_encoding_alist = Qnil;
1301 DEFVAR_LISP ("use-default-ascent", &Vuse_default_ascent,
1302 doc: /* Char table of characters whose ascent values should be ignored.
1303 If an entry for a character is non-nil, the ascent value of the glyph
1304 is assumed to be what specified by _MULE_DEFAULT_ASCENT property of a font.
1306 This affects how a composite character which contains
1307 such a character is displayed on screen. */);
1308 Vuse_default_ascent = Qnil;
1310 DEFVAR_LISP ("ignore-relative-composition", &Vignore_relative_composition,
1311 doc: /* Char table of characters which is not composed relatively.
1312 If an entry for a character is non-nil, a composition sequence
1313 which contains that character is displayed so that
1314 the glyph of that character is put without considering
1315 an ascent and descent value of a previous character. */);
1316 Vignore_relative_composition = Qnil;
1318 DEFVAR_LISP ("alternate-fontname-alist", &Valternate_fontname_alist,
1319 doc: /* Alist of fontname vs list of the alternate fontnames.
1320 When a specified font name is not found, the corresponding
1321 alternate fontnames (if any) are tried instead. */);
1322 Valternate_fontname_alist = Qnil;
1324 DEFVAR_LISP ("fontset-alias-alist", &Vfontset_alias_alist,
1325 doc: /* Alist of fontset names vs the aliases. */);
1326 Vfontset_alias_alist = Fcons (Fcons (FONTSET_NAME (Vdefault_fontset),
1327 build_string ("fontset-default")),
1328 Qnil);
1330 DEFVAR_LISP ("vertical-centering-font-regexp",
1331 &Vvertical_centering_font_regexp,
1332 doc: /* *Regexp matching font names that require vertical centering on display.
1333 When a character is displayed with such fonts, the character is displayed
1334 at the vertical center of lines. */);
1335 Vvertical_centering_font_regexp = Qnil;
1337 defsubr (&Squery_fontset);
1338 defsubr (&Snew_fontset);
1339 defsubr (&Sset_fontset_font);
1340 defsubr (&Sfont_info);
1341 defsubr (&Sinternal_char_font);
1342 defsubr (&Sfontset_info);
1343 defsubr (&Sfontset_font);
1344 defsubr (&Sfontset_list);