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