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