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