Replace `string-to-int' by `string-to-number'.
[bpt/emacs.git] / lisp / international / fontset.el
CommitLineData
60370d40 1;;; fontset.el --- commands for handling fontset
4ed46869 2
2fd125a3
KH
3;; Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003
5;; National Institute of Advanced Industrial Science and Technology (AIST)
6;; Registration Number H14PRO021
4ed46869
KH
7
8;; Keywords: mule, multilingual, fontset
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
369314dc
KH
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
4ed46869 26
60370d40
PJ
27;;; Commentary:
28
4ed46869
KH
29;;; Code:
30
2ad00186
KH
31;; Set standard fontname specification of characters in the default
32;; fontset to find an appropriate font for each charset. This is used
33;; to generate a font name for a fontset if the fontset doesn't
34;; specify a font name for a specific character. The specification
35;; has the form (FAMILY . REGISTRY). FAMILY may be nil, in which
36;; case, the family name of default face is used. If REGISTRY
37;; contains a character `-', the string before that is embedded in
38;; `CHARSET_REGISTRY' field, and the string after that is embedded in
39;; `CHARSET_ENCODING' field. If it does not contain `-', the whole
40;; string is embedded in `CHARSET_REGISTRY' field, and a wild card
41;; character `*' is embedded in `CHARSET_ENCODING' field. The
42;; REGISTRY for ASCII characters are predefined as "ISO8859-1".
43
c963bbc2
KH
44(defun setup-default-fontset ()
45 "Setup the default fontset."
a030d0e1
KH
46 (dolist (elt
47 `((latin-iso8859-1 . (nil . "ISO8859-1"))
48 (latin-iso8859-2 . (nil . "ISO8859-2"))
49 (latin-iso8859-3 . (nil . "ISO8859-3"))
50 (latin-iso8859-4 . (nil . "ISO8859-4"))
804ad4c9
KH
51 ;; Setting "*" family is for a workaround of the problem
52 ;; that a font of wrong size is preferred if the font
53 ;; family matches with a requested one.
54 (thai-tis620 . ("*" . "TIS620"))
8d9a9cbd
KH
55 (greek-iso8859-7 . (nil . "ISO8859-7"))
56 (arabic-iso8859-6 . (nil . "ISO8859-6"))
57 (hebrew-iso8859-8 . (nil . "ISO8859-8"))
58 (katakana-jisx0201 . (nil . "JISX0201"))
a030d0e1 59 (latin-jisx0201 . (nil . "JISX0201"))
8d9a9cbd 60 (cyrillic-iso8859-5 . (nil . "ISO8859-5"))
a030d0e1 61 (latin-iso8859-9 . (nil . "ISO8859-9"))
8d9a9cbd
KH
62 (japanese-jisx0208-1978 . (nil . "JISX0208.1978"))
63 (chinese-gb2312 . (nil . "GB2312.1980"))
64 (japanese-jisx0208 . (nil . "JISX0208.1990"))
65 (korean-ksc5601 . (nil . "KSC5601.1989"))
66 (japanese-jisx0212 . (nil . "JISX0212"))
67 (chinese-cns11643-1 . (nil . "CNS11643.1992-1"))
68 (chinese-cns11643-2 . (nil . "CNS11643.1992-2"))
69 (chinese-cns11643-3 . (nil . "CNS11643.1992-3"))
70 (chinese-cns11643-4 . (nil . "CNS11643.1992-4"))
71 (chinese-cns11643-5 . (nil . "CNS11643.1992-5"))
72 (chinese-cns11643-6 . (nil . "CNS11643.1992-6"))
73 (chinese-cns11643-7 . (nil . "CNS11643.1992-7"))
74 (chinese-big5-1 . (nil . "Big5"))
75 (chinese-big5-2 . (nil . "Big5"))
a030d0e1
KH
76 (chinese-sisheng . (nil . "sisheng_cwnn"))
77 (vietnamese-viscii-lower . (nil . "VISCII1.1"))
78 (vietnamese-viscii-upper . (nil . "VISCII1.1"))
8d9a9cbd
KH
79 (arabic-digit . (nil . "MuleArabic-0"))
80 (arabic-1-column . (nil . "MuleArabic-1"))
81 (arabic-2-column . (nil . "MuleArabic-2"))
a030d0e1 82 (ipa . (nil . "MuleIPA"))
8d9a9cbd 83 (ethiopic . (nil . "Ethiopic-Unicode"))
a030d0e1 84 (ascii-right-to-left . (nil . "ISO8859-1"))
8d9a9cbd
KH
85 (indian-is13194 . (nil . "IS13194-Devanagari"))
86 (indian-2-column . (nil . "MuleIndian-2"))
87 (lao . (nil . "MuleLao-1"))
a030d0e1 88 (tibetan . ("proportional" . "MuleTibetan-2"))
8d9a9cbd 89 (tibetan-1-column . (nil . "MuleTibetan-1"))
a030d0e1
KH
90 (latin-iso8859-14 . (nil . "ISO8859-14"))
91 (latin-iso8859-15 . (nil . "ISO8859-15"))
92 (mule-unicode-0100-24ff . (nil . "ISO10646-1"))
93 (mule-unicode-2500-33ff . (nil . "ISO10646-1"))
94 (mule-unicode-e000-ffff . (nil . "ISO10646-1"))
8d9a9cbd
KH
95 (japanese-jisx0213-1 . (nil . "JISX0213.2000-1"))
96 (japanese-jisx0213-2 . (nil . "JISX0213.2000-2"))
a030d0e1
KH
97 ;; unicode
98 ((,(decode-char 'ucs #x0900) . ,(decode-char 'ucs #x097F))
8d9a9cbd 99 . (nil . "ISO10646.indian-1"))
a030d0e1
KH
100 ;; Indian CDAC
101 (,(indian-font-char-range 'cdac:dv-ttsurekh)
8d9a9cbd 102 . (nil . "Devanagari-CDAC"))
a030d0e1 103 (,(indian-font-char-range 'cdac:sd-ttsurekh)
8d9a9cbd 104 . (nil . "Sanskrit-CDAC"))
a030d0e1 105 (,(indian-font-char-range 'cdac:bn-ttdurga)
8d9a9cbd 106 . (nil . "Bengali-CDAC"))
a030d0e1 107 (,(indian-font-char-range 'cdac:as-ttdurga)
8d9a9cbd 108 . (nil . "Assamese-CDAC"))
a030d0e1 109 (,(indian-font-char-range 'cdac:pn-ttamar)
8d9a9cbd 110 . (nil . "Punjabi-CDAC"))
a030d0e1 111 (,(indian-font-char-range 'cdac:gj-ttavantika)
8d9a9cbd 112 . (nil . "Gujarati-CDAC"))
a030d0e1 113 (,(indian-font-char-range 'cdac:or-ttsarala)
8d9a9cbd 114 . (nil . "Oriya-CDAC"))
a030d0e1 115 (,(indian-font-char-range 'cdac:tm-ttvalluvar)
8d9a9cbd 116 . (nil . "Tamil-CDAC"))
a030d0e1 117 (,(indian-font-char-range 'cdac:tl-tthemalatha)
8d9a9cbd 118 . (nil . "Telugu-CDAC"))
a030d0e1 119 (,(indian-font-char-range 'cdac:kn-ttuma)
8d9a9cbd 120 . (nil . "Kannada-CDAC"))
a030d0e1 121 (,(indian-font-char-range 'cdac:ml-ttkarthika)
8d9a9cbd 122 . (nil . "Malayalam-CDAC"))
a030d0e1
KH
123 ;; Indian AKRUTI
124 (,(indian-font-char-range 'akruti:dev)
8d9a9cbd 125 . (nil . "Devanagari-Akruti"))
a030d0e1 126 (,(indian-font-char-range 'akruti:bng)
8d9a9cbd 127 . (nil . "Bengali-Akruti"))
a030d0e1 128 (,(indian-font-char-range 'akruti:pnj)
8d9a9cbd 129 . (nil . "Punjabi-Akruti"))
a030d0e1 130 (,(indian-font-char-range 'akruti:guj)
8d9a9cbd 131 . (nil . "Gujarati-Akruti"))
a030d0e1 132 (,(indian-font-char-range 'akruti:ori)
8d9a9cbd 133 . (nil . "Oriay-Akruti"))
a030d0e1 134 (,(indian-font-char-range 'akruti:tml)
8d9a9cbd 135 . (nil . "Tamil-Akruti"))
a030d0e1 136 (,(indian-font-char-range 'akruti:tlg)
8d9a9cbd 137 . (nil . "Telugu-Akruti"))
a030d0e1 138 (,(indian-font-char-range 'akruti:knd)
8d9a9cbd 139 . (nil . "Kannada-Akruti"))
a030d0e1 140 (,(indian-font-char-range 'akruti:mal)
8d9a9cbd 141 . (nil . "Malayalam-Akruti"))
a030d0e1 142 ))
c963bbc2 143 (set-fontset-font "fontset-default" (car elt) (cdr elt))))
4ed46869
KH
144
145;; Set arguments in `font-encoding-alist' (which see).
146(defun set-font-encoding (pattern charset encoding)
147 (let ((slot (assoc pattern font-encoding-alist)))
148 (if slot
149 (let ((place (assq charset (cdr slot))))
150 (if place
151 (setcdr place encoding)
152 (setcdr slot (cons (cons charset encoding) (cdr slot)))))
153 (setq font-encoding-alist
154 (cons (list pattern (cons charset encoding)) font-encoding-alist)))
155 ))
156
84fa2951
KH
157;; Allow display of arbitrary characters with an iso-10646-encoded
158;; (`Unicode') font.
159(define-translation-table 'ucs-mule-to-mule-unicode
160 ucs-mule-to-mule-unicode)
161(define-translation-hash-table 'ucs-mule-cjk-to-unicode
162 ucs-mule-cjk-to-unicode)
163
adbff2a7 164(define-ccl-program ccl-encode-unicode-font
12385722 165 `(0
84fa2951
KH
166 ;; r0: charset-id
167 ;; r1: 1st position code
168 ;; r2: 2nd position code (if r0 is 2D charset)
169 ((if (r0 == ,(charset-id 'ascii))
170 ((r2 = r1)
171 (r1 = 0))
172 ;; At first, try to get a Unicode code point directly.
173 ((if (r2 >= 0)
174 ;; This is a 2D charset.
175 (r1 = ((r1 << 7) | r2)))
176 (lookup-character ucs-mule-cjk-to-unicode r0 r1)
177 (if r7
178 ;; We got it!
179 ((r1 = (r0 >> 8))
180 (r2 = (r0 & #xFF)))
181 ;; Look for a translation for non-ASCII chars.
182 ((translate-character ucs-mule-to-mule-unicode r0 r1)
183 (if (r0 == ,(charset-id 'latin-iso8859-1))
184 ((r2 = (r1 + 128))
185 (r1 = 0))
186 ((r2 = (r1 & #x7F))
187 (r1 >>= 7)
188 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff))
189 ((r1 *= 96)
190 (r1 += r2)
191 (r1 += ,(- #x100 (* 32 96) 32))
192 (r1 >8= 0)
193 (r2 = r7))
194 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff))
195 ((r1 *= 96)
196 (r1 += r2)
197 (r1 += ,(- #x2500 (* 32 96) 32))
198 (r1 >8= 0)
199 (r2 = r7))
200 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff))
201 ((r1 *= 96)
202 (r1 += r2)
203 (r1 += ,(- #xe000 (* 32 96) 32))
204 (r1 >8= 0)
205 (r2 = r7))
206 ;; No way, use the glyph for U+FFFD.
207 ((r1 = #xFF)
208 (r2 = #xFD)))))))))))))
209 "Encode characters for display with iso10646 font.
210Translate through the translation-hash-table named
211`ucs-mule-cjk-to-unicode' and the translation-table named
212`ucs-mule-to-mule-unicode' initially.")
12385722 213
84fa2951
KH
214;; Use the above CCL encoder for Unicode fonts. Please note that the
215;; regexp is not simply "ISO10646-1" because there exists, for
216;; instance, the following Devanagari Unicode fonts:
217;; -misc-fixed-medium-r-normal--24-240-72-72-c-120-iso10646.indian-1
218;; -sibal-devanagari-medium-r-normal--24-240-75-75-P--iso10646-dev
12385722 219(setq font-ccl-encoder-alist
84fa2951 220 (cons '("ISO10646.*-*" . ccl-encode-unicode-font)
12385722
KH
221 font-ccl-encoder-alist))
222
4ed46869
KH
223;; Setting for suppressing XLoadQueryFont on big fonts.
224(setq x-pixel-size-width-font-regexp
225 "gb2312\\|jisx0208\\|ksc5601\\|cns11643\\|big5")
226
6eca8d93 227;; These fonts require vertical centering.
0c4f97d0 228(setq vertical-centering-font-regexp
6eca8d93 229 "gb2312\\|jisx0208\\|jisx0212\\|ksc5601\\|cns11643\\|big5")
0c4f97d0 230
e46947a5
KH
231;; CDAC fonts are actually smaller than their design sizes.
232(setq face-font-rescale-alist
233 '(("-cdac$" . 1.3)))
234
6fb8f8bd
KH
235(defvar x-font-name-charset-alist
236 '(("iso8859-1" ascii latin-iso8859-1)
237 ("iso8859-2" ascii latin-iso8859-2)
238 ("iso8859-3" ascii latin-iso8859-3)
239 ("iso8859-4" ascii latin-iso8859-4)
240 ("iso8859-5" ascii cyrillic-iso8859-5)
241 ("iso8859-6" ascii arabic-iso8859-6)
242 ("iso8859-7" ascii greek-iso8859-7)
243 ("iso8859-8" ascii hebrew-iso8859-8)
7a860cf2
DL
244 ("iso8859-14" ascii latin-iso8859-14)
245 ("iso8859-15" ascii latin-iso8859-15)
6fb8f8bd
KH
246 ("tis620" ascii thai-tis620)
247 ("koi8" ascii cyrillic-iso8859-5)
248 ("viscii" ascii vietnamese-viscii-upper vietnamese-viscii-lower)
249 ("vscii" ascii vietnamese-viscii-upper vietnamese-viscii-lower)
12385722 250 ("mulelao-1" ascii lao)
adbff2a7
KH
251 ("iso10646-1" ascii latin-iso8859-1 mule-unicode-0100-24ff
252 mule-unicode-2500-33ff mule-unicode-e000-ffff))
6fb8f8bd
KH
253 "Alist of font names vs list of charsets the font can display.
254
255When a font name which matches some element of this alist is given as
256`-fn' command line argument or is specified by X resource, a fontset
257which uses the specified font for the corresponding charsets are
258created and used for the initial frame.")
259
4ed46869
KH
260;;; XLFD (X Logical Font Description) format handler.
261
262;; Define XLFD's field index numbers. ; field name
263(defconst xlfd-regexp-foundry-subnum 0) ; FOUNDRY
264(defconst xlfd-regexp-family-subnum 1) ; FAMILY_NAME
265(defconst xlfd-regexp-weight-subnum 2) ; WEIGHT_NAME
266(defconst xlfd-regexp-slant-subnum 3) ; SLANT
267(defconst xlfd-regexp-swidth-subnum 4) ; SETWIDTH_NAME
268(defconst xlfd-regexp-adstyle-subnum 5) ; ADD_STYLE_NAME
269(defconst xlfd-regexp-pixelsize-subnum 6) ; PIXEL_SIZE
270(defconst xlfd-regexp-pointsize-subnum 7) ; POINT_SIZE
271(defconst xlfd-regexp-resx-subnum 8) ; RESOLUTION_X
272(defconst xlfd-regexp-resy-subnum 9) ; RESOLUTION_Y
273(defconst xlfd-regexp-spacing-subnum 10) ; SPACING
274(defconst xlfd-regexp-avgwidth-subnum 11) ; AVERAGE_WIDTH
275(defconst xlfd-regexp-registry-subnum 12) ; CHARSET_REGISTRY
276(defconst xlfd-regexp-encoding-subnum 13) ; CHARSET_ENCODING
277
278;; Regular expression matching against a fontname which conforms to
279;; XLFD (X Logical Font Description). All fields in XLFD should be
280;; not be omitted (but can be a wild card) to be matched.
281(defconst xlfd-tight-regexp
282 "^\
283-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
284-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
285-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)$")
286
287;; List of field numbers of XLFD whose values are numeric.
288(defconst xlfd-regexp-numeric-subnums
289 (list xlfd-regexp-pixelsize-subnum ;6
290 xlfd-regexp-pointsize-subnum ;7
291 xlfd-regexp-resx-subnum ;8
292 xlfd-regexp-resy-subnum ;9
293 xlfd-regexp-avgwidth-subnum ;11
294 ))
295
296(defun x-decompose-font-name (pattern)
297 "Decompose PATTERN into XLFD's fields and return vector of the fields.
298The length of the vector is 14.
299
300If PATTERN doesn't conform to XLFD, try to get a full XLFD name from
301X server and use the information of the full name to decompose
302PATTERN. If no full XLFD name is gotten, return nil."
303 (let (xlfd-fields fontname)
304 (if (string-match xlfd-tight-regexp pattern)
305 (let ((i 0))
306 (setq xlfd-fields (make-vector 14 nil))
307 (while (< i 14)
308 (aset xlfd-fields i (match-string (1+ i) pattern))
309 (setq i (1+ i)))
310 xlfd-fields)
311 (setq fontname (condition-case nil
312 (x-resolve-font-name pattern)
313 (error)))
314 (if (and fontname
315 (string-match xlfd-tight-regexp fontname))
190ce634 316 ;; We get a full XLFD name.
4ed46869
KH
317 (let ((len (length pattern))
318 (i 0)
319 l)
190ce634
KH
320 ;; Setup xlfd-fields by the full XLFD name. Each element
321 ;; should be a cons of matched index and matched string.
4ed46869
KH
322 (setq xlfd-fields (make-vector 14 nil))
323 (while (< i 14)
324 (aset xlfd-fields i
325 (cons (match-beginning (1+ i))
326 (match-string (1+ i) fontname)))
327 (setq i (1+ i)))
190ce634
KH
328
329 ;; Replace wild cards in PATTERN by regexp codes.
4ed46869
KH
330 (setq i 0)
331 (while (< i len)
332 (let ((ch (aref pattern i)))
333 (if (= ch ??)
334 (setq pattern (concat (substring pattern 0 i)
335 "\\(.\\)"
336 (substring pattern (1+ i)))
337 len (+ len 4)
338 i (+ i 4))
339 (if (= ch ?*)
340 (setq pattern (concat (substring pattern 0 i)
341 "\\(.*\\)"
342 (substring pattern (1+ i)))
343 len (+ len 5)
344 i (+ i 5))
345 (setq i (1+ i))))))
190ce634
KH
346
347 ;; Set each element of xlfd-fields to proper strings.
348 (if (string-match pattern fontname)
349 ;; The regular expression PATTERN matchs the full XLFD
350 ;; name. Set elements that correspond to a wild card
351 ;; in PATTERN to "*", set the other elements to the
352 ;; exact strings in PATTERN.
353 (let ((l (cdr (cdr (match-data)))))
354 (setq i 0)
355 (while (< i 14)
356 (if (or (null l) (< (car (aref xlfd-fields i)) (car l)))
357 (progn
358 (aset xlfd-fields i (cdr (aref xlfd-fields i)))
359 (setq i (1+ i)))
360 (if (< (car (aref xlfd-fields i)) (car (cdr l)))
361 (progn
362 (aset xlfd-fields i "*")
363 (setq i (1+ i)))
364 (setq l (cdr (cdr l)))))))
365 ;; Set each element of xlfd-fields to the exact string
366 ;; in the corresonding fields in full XLFD name.
367 (setq i 0)
368 (while (< i 14)
369 (aset xlfd-fields i (cdr (aref xlfd-fields i)))
370 (setq i (1+ i))))
4ed46869
KH
371 xlfd-fields)))))
372
441038a6
KH
373;; Replace consecutive wild-cards (`*') in NAME to one.
374;; Ex. (x-reduce-font-name "-*-*-*-iso8859-1") => "-*-iso8859-1"
375(defsubst x-reduce-font-name (name)
376 (while (string-match "-\\*-\\(\\*-\\)+" name)
377 (setq name (replace-match "-*-" t t name)))
378 name)
379
bb98ead9 380(defun x-compose-font-name (fields &optional reduce)
4ed46869 381 "Compose X's fontname from FIELDS.
bb98ead9 382FIELDS is a vector of XLFD fields, the length 14.
441038a6 383If a field is nil, wild-card letter `*' is embedded.
6eca8d93
KH
384Optional argument REDUCE is always ignored. It exists just for
385backward compatibility."
386 (concat "-" (mapconcat (lambda (x) (or x "*")) fields "-")))
bcb68cff 387
4fbe2b13
GM
388
389(defun x-must-resolve-font-name (xlfd-fields)
390 "Like `x-resolve-font-name', but always return a font name.
391XLFD-FIELDS is a vector of XLFD (X Logical Font Description) fields.
392If no font matching XLFD-FIELDS is available, successively replace
393parts of the font name pattern with \"*\" until some font is found.
394Value is name of that font."
395 (let ((ascii-font nil) (index 0))
396 (while (and (null ascii-font) (<= index xlfd-regexp-encoding-subnum))
397 (let ((pattern (x-compose-font-name xlfd-fields)))
398 (condition-case nil
399 (setq ascii-font (x-resolve-font-name pattern))
400 (error
401 (message "Warning: no fonts matching `%s' available" pattern)
402 (aset xlfd-fields index "*")
403 (setq index (1+ index))))))
404 (unless ascii-font
37aad8f5 405 (error "No fonts found"))
4fbe2b13
GM
406 ascii-font))
407
408
4ed46869 409(defun x-complement-fontset-spec (xlfd-fields fontlist)
6eca8d93 410 "Complement FONTLIST for charsets based on XLFD-FIELDS and return it.
4ed46869 411XLFD-FIELDS is a vector of XLFD (X Logical Font Description) fields.
6fb8f8bd 412FONTLIST is an alist of charsets vs the corresponding font names.
4ed46869 413
6eca8d93
KH
414The fonts are complemented as below.
415
416If FONTLIST doesn't specify a font for ASCII charset, generate a font
417name for the charset from XLFD-FIELDS, and add that information to
418FONTLIST.
419
420If a font specifid for ASCII supports the other charsets (see the
421variable `x-font-name-charset-alist'), add that information to FONTLIST."
f6cab6c8 422 (let* ((slot (assq 'ascii fontlist))
6bf84cde
KH
423 (ascii-font (cdr slot))
424 ascii-font-spec)
f6cab6c8
KH
425 (if ascii-font
426 (setcdr slot (setq ascii-font (x-resolve-font-name ascii-font)))
427 ;; If font for ASCII is not specified, add it.
2ad00186
KH
428 (aset xlfd-fields xlfd-regexp-registry-subnum "iso8859")
429 (aset xlfd-fields xlfd-regexp-encoding-subnum "1")
4fbe2b13 430 (setq ascii-font (x-must-resolve-font-name xlfd-fields))
2ad00186 431 (setq fontlist (cons (cons 'ascii ascii-font) fontlist)))
6eca8d93
KH
432
433 ;; If the font for ASCII also supports the other charsets, and
434 ;; they are not specified in FONTLIST, add them.
6bf84cde
KH
435 (setq xlfd-fields (x-decompose-font-name ascii-font))
436 (if (not xlfd-fields)
437 (setq ascii-font-spec ascii-font)
438 (setq ascii-font-spec
439 (cons (format "%s-%s"
440 (aref xlfd-fields xlfd-regexp-foundry-subnum)
441 (aref xlfd-fields xlfd-regexp-family-subnum))
442 (format "%s-%s"
443 (aref xlfd-fields xlfd-regexp-registry-subnum)
444 (aref xlfd-fields xlfd-regexp-encoding-subnum)))))
6eca8d93
KH
445 (let ((tail x-font-name-charset-alist)
446 elt)
447 (while tail
448 (setq elt (car tail) tail (cdr tail))
449 (if (string-match (car elt) ascii-font)
450 (let ((charsets (cdr elt))
451 charset)
452 (while charsets
453 (setq charset (car charsets) charsets (cdr charsets))
454 (or (assq charset fontlist)
455 (setq fontlist
6bf84cde 456 (cons (cons charset ascii-font-spec) fontlist))))))))
a1506d29 457
6eca8d93 458 fontlist))
4ed46869 459
35d4066a
KH
460(defun fontset-name-p (fontset)
461 "Return non-nil if FONTSET is valid as fontset name.
462A valid fontset name should conform to XLFD (X Logical Font Description)
463with \"fontset\" in `<CHARSET_REGISTRY> field."
464 (and (string-match xlfd-tight-regexp fontset)
465 (string= (match-string (1+ xlfd-regexp-registry-subnum) fontset)
466 "fontset")))
467
4ed46869
KH
468;; Return a list to be appended to `x-fixed-font-alist' when
469;; `mouse-set-font' is called.
470(defun generate-fontset-menu ()
6eca8d93 471 (let ((fontsets (fontset-list))
4ed46869
KH
472 fontset-name
473 l)
474 (while fontsets
6eca8d93 475 (setq fontset-name (car fontsets) fontsets (cdr fontsets))
494ec9bc 476 (setq l (cons (list (fontset-plain-name fontset-name) fontset-name) l)))
80ae5f3a
KH
477 (cons "Fontset"
478 (sort l (function (lambda (x y) (string< (car x) (car y))))))))
4ed46869
KH
479
480(defun fontset-plain-name (fontset)
481 "Return a plain and descriptive name of FONTSET."
494ec9bc
KH
482 (if (not (setq fontset (query-fontset fontset)))
483 (error "Invalid fontset: %s" fontset))
4ed46869
KH
484 (let ((xlfd-fields (x-decompose-font-name fontset)))
485 (if xlfd-fields
486 (let ((weight (aref xlfd-fields xlfd-regexp-weight-subnum))
487 (slant (aref xlfd-fields xlfd-regexp-slant-subnum))
488 (swidth (aref xlfd-fields xlfd-regexp-swidth-subnum))
489 (size (aref xlfd-fields xlfd-regexp-pixelsize-subnum))
494ec9bc
KH
490 (charset (aref xlfd-fields xlfd-regexp-registry-subnum))
491 (nickname (aref xlfd-fields xlfd-regexp-encoding-subnum))
4ed46869 492 name)
494ec9bc 493 (if (not (string= "fontset" charset))
4ed46869 494 fontset
027a4b6b 495 (if (> (string-to-number size) 0)
494ec9bc
KH
496 (setq name (format "%s: %s-dot" nickname size))
497 (setq name nickname))
498 (cond ((string-match "^medium$" weight)
499 (setq name (concat name " " "medium")))
500 ((string-match "^bold$\\|^demibold$" weight)
501 (setq name (concat name " " weight))))
502 (cond ((string-match "^i$" slant)
503 (setq name (concat name " " "italic")))
504 ((string-match "^o$" slant)
505 (setq name (concat name " " "slant")))
506 ((string-match "^ri$" slant)
507 (setq name (concat name " " "reverse italic")))
508 ((string-match "^ro$" slant)
509 (setq name (concat name " " "reverse slant"))))
4ed46869
KH
510 name))
511 fontset)))
512
c963bbc2 513
6fb8f8bd 514(defun create-fontset-from-fontset-spec (fontset-spec
bb98ead9 515 &optional style-variant noerror)
4ed46869
KH
516 "Create a fontset from fontset specification string FONTSET-SPEC.
517FONTSET-SPEC is a string of the format:
518 FONTSET-NAME,CHARSET-NAME0:FONT-NAME0,CHARSET-NAME1:FONT-NAME1, ...
494ec9bc 519Any number of SPACE, TAB, and NEWLINE can be put before and after commas.
bb98ead9 520
6eca8d93
KH
521Optional 2nd argument is ignored. It exists just for backward
522compatibility.
bb98ead9 523
883e21bd 524If this function attempts to create already existing fontset, error is
1f50fef9
KH
525signaled unless the optional 3rd argument NOERROR is non-nil.
526
527It returns a name of the created fontset."
494ec9bc
KH
528 (if (not (string-match "^[^,]+" fontset-spec))
529 (error "Invalid fontset spec: %s" fontset-spec))
6eca8d93 530 (setq fontset-spec (downcase fontset-spec))
494ec9bc
KH
531 (let ((idx (match-end 0))
532 (name (match-string 0 fontset-spec))
6eca8d93 533 xlfd-fields charset fontlist ascii-font)
6fb8f8bd 534 (if (query-fontset name)
a1506d29 535 (or noerror
86e411c9 536 (error "Fontset \"%s\" already exists" name))
6eca8d93
KH
537 (setq xlfd-fields (x-decompose-font-name name))
538 (or xlfd-fields
539 (error "Fontset \"%s\" not conforming to XLFD" name))
540
6fb8f8bd
KH
541 ;; At first, extract pairs of charset and fontname from FONTSET-SPEC.
542 (while (string-match "[, \t\n]*\\([^:]+\\):\\([^,]+\\)" fontset-spec idx)
543 (setq idx (match-end 0))
544 (setq charset (intern (match-string 1 fontset-spec)))
545 (if (charsetp charset)
546 (setq fontlist (cons (cons charset (match-string 2 fontset-spec))
547 fontlist))))
f6cab6c8 548 (setq ascii-font (cdr (assq 'ascii fontlist)))
6eca8d93
KH
549
550 ;; Complement FONTLIST.
551 (setq fontlist (x-complement-fontset-spec xlfd-fields fontlist))
552
553 (new-fontset name fontlist)
554
555 ;; Define the short name alias.
556 (if (and (string-match "fontset-.*$" name)
557 (not (assoc name fontset-alias-alist)))
558 (let ((alias (match-string 0 name)))
559 (or (rassoc alias fontset-alias-alist)
560 (setq fontset-alias-alist
561 (cons (cons name alias) fontset-alias-alist)))))
562
563 ;; Define the ASCII font name alias.
f6cab6c8
KH
564 (or ascii-font
565 (setq ascii-font (cdr (assq 'ascii fontlist))))
6eca8d93
KH
566 (or (rassoc ascii-font fontset-alias-alist)
567 (setq fontset-alias-alist
568 (cons (cons name ascii-font)
569 fontset-alias-alist))))
6fb8f8bd 570
1f50fef9
KH
571 name))
572
573(defun create-fontset-from-ascii-font (font &optional resolved-font
574 fontset-name)
575 "Create a fontset from an ASCII font FONT.
576
577Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
37aad8f5 578omitted, `x-resolve-font-name' is called to get the resolved name. At
56d25ff5 579this time, if FONT is not available, error is signaled.
1f50fef9
KH
580
581Optional 2nd arg FONTSET-NAME is a string to be used in
582`<CHARSET_ENCODING>' fields of a new fontset name. If it is omitted,
583an appropriate name is generated automatically.
584
1f50fef9 585It returns a name of the created fontset."
6eca8d93
KH
586 (setq font (downcase font))
587 (if resolved-font
588 (setq resolved-font (downcase resolved-font))
589 (setq resolved-font (downcase (x-resolve-font-name font))))
590 (let ((xlfd (x-decompose-font-name font))
591 (resolved-xlfd (x-decompose-font-name resolved-font))
592 fontset fontset-spec)
1f50fef9
KH
593 (aset xlfd xlfd-regexp-foundry-subnum nil)
594 (aset xlfd xlfd-regexp-family-subnum nil)
595 (aset xlfd xlfd-regexp-registry-subnum "fontset")
6eca8d93
KH
596 (if fontset-name
597 (setq fontset-name (downcase fontset-name))
598 (setq fontset-name
599 (format "%s_%s_%s"
600 (aref resolved-xlfd xlfd-regexp-registry-subnum)
601 (aref resolved-xlfd xlfd-regexp-encoding-subnum)
602 (aref resolved-xlfd xlfd-regexp-pixelsize-subnum))))
1f50fef9 603 (aset xlfd xlfd-regexp-encoding-subnum fontset-name)
1f50fef9
KH
604 (setq fontset (x-compose-font-name xlfd))
605 (or (query-fontset fontset)
6eca8d93
KH
606 (create-fontset-from-fontset-spec (concat fontset ", ascii:" font)))))
607
4ed46869 608\f
acfb10b8 609;; Create standard fontset from 16 dots fonts which are the most widely
80d4ea92
KH
610;; installed fonts. Fonts for Chinese-GB, Korean, and Chinese-CNS are
611;; specified here because FAMILY of those fonts are not "fixed" in
612;; many cases.
acfb10b8 613(defvar standard-fontset-spec
cdd675ad 614 (purecopy "-*-fixed-medium-r-normal-*-16-*-*-*-*-*-fontset-standard,
4ed46869
KH
615 chinese-gb2312:-*-medium-r-normal-*-16-*-gb2312*-*,
616 korean-ksc5601:-*-medium-r-normal-*-16-*-ksc5601*-*,
617 chinese-cns11643-1:-*-medium-r-normal-*-16-*-cns11643*-1,
618 chinese-cns11643-2:-*-medium-r-normal-*-16-*-cns11643*-2,
619 chinese-cns11643-3:-*-medium-r-normal-*-16-*-cns11643*-3,
620 chinese-cns11643-4:-*-medium-r-normal-*-16-*-cns11643*-4,
621 chinese-cns11643-5:-*-medium-r-normal-*-16-*-cns11643*-5,
622 chinese-cns11643-6:-*-medium-r-normal-*-16-*-cns11643*-6,
cdd675ad 623 chinese-cns11643-7:-*-medium-r-normal-*-16-*-cns11643*-7")
acfb10b8
KH
624 "String of fontset spec of the standard fontset.
625You have the biggest chance to display international characters
626with correct glyphs by using the standard fontset.
4ed46869
KH
627See the documentation of `create-fontset-from-fontset-spec' for the format.")
628
629;; Create fontsets from X resources of the name `fontset-N (class
630;; Fontset-N)' where N is integer 0, 1, ...
631;; The values of the resources the string of the same format as
acfb10b8 632;; `standard-fontset-spec'.
4ed46869
KH
633
634(defun create-fontset-from-x-resource ()
635 (let ((idx 0)
636 fontset-spec)
cdd675ad
DL
637 (while (setq fontset-spec (x-get-resource (format "fontset-%d" idx)
638 (format "Fontset-%d" idx)))
a5695549 639 (create-fontset-from-fontset-spec fontset-spec t 'noerror)
4ed46869
KH
640 (setq idx (1+ idx)))))
641
4ed46869
KH
642;;
643(provide 'fontset)
644
ab5796a9 645;;; arch-tag: bb53e629-0234-403c-950e-551e61554849
4ed46869 646;;; fontset.el ends here