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