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