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