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