1 ;;; fontset.el --- Commands for handling fontset.
3 ;; Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: mule, multilingual, fontset
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Set standard fontname specification of characters in the default
28 ;; fontset to find an appropriate font for each charset. This is used
29 ;; to generate a font name for a fontset if the fontset doesn't
30 ;; specify a font name for a specific character. The specification
31 ;; has the form (FAMILY . REGISTRY). FAMILY may be nil, in which
32 ;; case, the family name of default face is used. If REGISTRY
33 ;; contains a character `-', the string before that is embedded in
34 ;; `CHARSET_REGISTRY' field, and the string after that is embedded in
35 ;; `CHARSET_ENCODING' field. If it does not contain `-', the whole
36 ;; string is embedded in `CHARSET_REGISTRY' field, and a wild card
37 ;; character `*' is embedded in `CHARSET_ENCODING' field. The
38 ;; REGISTRY for ASCII characters are predefined as "ISO8859-1".
40 (let ((l `((latin-iso8859-1 .
(nil .
"ISO8859-1"))
41 (latin-iso8859-2 .
(nil .
"ISO8859-2"))
42 (latin-iso8859-3 .
(nil .
"ISO8859-3"))
43 (latin-iso8859-4 .
(nil .
"ISO8859-4"))
44 (thai-tis620 .
("*" .
"TIS620"))
45 (greek-iso8859-7 .
("*" .
"ISO8859-7"))
46 (arabic-iso8859-6 .
("*" .
"ISO8859-6"))
47 (hebrew-iso8859-8 .
("*" .
"ISO8859-8"))
48 (katakana-jisx0201 .
("*" .
"JISX0201"))
49 (latin-jisx0201 .
(nil .
"JISX0201"))
50 (cyrillic-iso8859-5 .
("*" .
"ISO8859-5"))
51 (latin-iso8859-9 .
(nil .
"ISO8859-9"))
52 (japanese-jisx0208-1978 .
("*" .
"JISX0208.1978"))
53 (chinese-gb2312 .
("*" .
"GB2312"))
54 (japanese-jisx0208 .
("*" .
"JISX0208.1983"))
55 (korean-ksc5601 .
("*" .
"KSC5601"))
56 (japanese-jisx0212 .
("*" .
"JISX0212"))
57 (chinese-cns11643-1 .
("*" .
"CNS11643.1992-1"))
58 (chinese-cns11643-2 .
("*" .
"CNS11643.1992-2"))
59 (chinese-cns11643-3 .
("*" .
"CNS11643.1992-3"))
60 (chinese-cns11643-4 .
("*" .
"CNS11643.1992-4"))
61 (chinese-cns11643-5 .
("*" .
"CNS11643.1992-5"))
62 (chinese-cns11643-6 .
("*" .
"CNS11643.1992-6"))
63 (chinese-cns11643-7 .
("*" .
"CNS11643.1992-7"))
64 (chinese-big5-1 .
("*" .
"Big5"))
65 (chinese-big5-2 .
("*" .
"Big5"))
66 (chinese-sisheng .
(nil .
"sisheng_cwnn"))
67 (vietnamese-viscii-lower .
(nil .
"VISCII1.1"))
68 (vietnamese-viscii-upper .
(nil .
"VISCII1.1"))
69 (arabic-digit .
("*" .
"MuleArabic-0"))
70 (arabic-1-column .
("*" .
"MuleArabic-1"))
71 (arabic-2-column .
("*" .
"MuleArabic-2"))
72 (ipa .
(nil .
"MuleIPA"))
73 (ethiopic .
("*" .
"Ethiopic-Unicode"))
74 (ascii-right-to-left .
(nil .
"ISO8859-1"))
75 (indian-is13194 .
("*" .
"IS13194-Devanagari"))
76 (indian-2-column .
("*" .
"MuleIndian-2"))
77 (indian-1-column .
("*" .
"MuleIndian-1"))
78 (lao .
("*" .
"MuleLao-1"))
79 (tibetan .
("proportional" .
"MuleTibetan-2"))
80 (tibetan-1-column .
("*" .
"MuleTibetan-1"))
81 (latin-iso8859-14 .
(nil .
"ISO8859-14"))
82 (latin-iso8859-15 .
(nil .
"ISO8859-15"))
83 (mule-unicode-0100-24ff .
(nil .
"ISO10646-1"))
84 (mule-unicode-2500-33ff .
(nil .
"ISO10646-1"))
85 (mule-unicode-e000-ffff .
(nil .
"ISO10646-1"))
86 (japanese-jisx0213-1 .
("*" .
"JISX0213"))
87 (japanese-jisx0213-2 .
("*" .
"JISX0213"))
89 charset font-spec arg
)
91 (setq charset
(car (car l
)) font-spec
(cdr (car l
)) l
(cdr l
))
92 (or (string-match "-" (cdr font-spec
))
93 (setcdr font-spec
(concat (cdr font-spec
) "*")))
95 (setq arg
(make-char charset
))
97 (set-fontset-font "fontset-default" arg font-spec
)))
99 ;; Set arguments in `font-encoding-alist' (which see).
100 (defun set-font-encoding (pattern charset encoding
)
101 (let ((slot (assoc pattern font-encoding-alist
)))
103 (let ((place (assq charset
(cdr slot
))))
105 (setcdr place encoding
)
106 (setcdr slot
(cons (cons charset encoding
) (cdr slot
)))))
107 (setq font-encoding-alist
108 (cons (list pattern
(cons charset encoding
)) font-encoding-alist
)))
111 (set-font-encoding "ISO8859-1" 'ascii
0)
112 (set-font-encoding "JISX0201" 'latin-jisx0201
0)
114 (define-ccl-program ccl-encode-unicode-font
116 (if (r0 == ,(charset-id 'ascii
))
119 (if (r0 == ,(charset-id 'latin-iso8859-1
))
122 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff
))
125 (r1 += ,(- #x100
(* 32 96) 32))
128 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff
))
131 (r1 += ,(- #x2500
(* 32 96) 32))
134 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff
))
137 (r1 += ,(- #xe000
(* 32 96) 32))
141 (setq font-ccl-encoder-alist
142 (cons '("ISO10646-1" . ccl-encode-unicode-font
)
143 font-ccl-encoder-alist
))
145 ;; Setting for suppressing XLoadQueryFont on big fonts.
146 (setq x-pixel-size-width-font-regexp
147 "gb2312\\|jisx0208\\|ksc5601\\|cns11643\\|big5")
149 ;; These fonts require vertical centering.
150 (setq vertical-centering-font-regexp
151 "gb2312\\|jisx0208\\|jisx0212\\|ksc5601\\|cns11643\\|big5")
153 (defvar x-font-name-charset-alist
154 '(("iso8859-1" ascii latin-iso8859-1
)
155 ("iso8859-2" ascii latin-iso8859-2
)
156 ("iso8859-3" ascii latin-iso8859-3
)
157 ("iso8859-4" ascii latin-iso8859-4
)
158 ("iso8859-5" ascii cyrillic-iso8859-5
)
159 ("iso8859-6" ascii arabic-iso8859-6
)
160 ("iso8859-7" ascii greek-iso8859-7
)
161 ("iso8859-8" ascii hebrew-iso8859-8
)
162 ("iso8859-14" ascii latin-iso8859-14
)
163 ("iso8859-15" ascii latin-iso8859-15
)
164 ("tis620" ascii thai-tis620
)
165 ("koi8" ascii cyrillic-iso8859-5
)
166 ("viscii" ascii vietnamese-viscii-upper vietnamese-viscii-lower
)
167 ("vscii" ascii vietnamese-viscii-upper vietnamese-viscii-lower
)
168 ("mulelao-1" ascii lao
)
169 ("iso10646-1" ascii latin-iso8859-1 mule-unicode-0100-24ff
170 mule-unicode-2500-33ff mule-unicode-e000-ffff
))
171 "Alist of font names vs list of charsets the font can display.
173 When a font name which matches some element of this alist is given as
174 `-fn' command line argument or is specified by X resource, a fontset
175 which uses the specified font for the corresponding charsets are
176 created and used for the initial frame.")
178 ;;; XLFD (X Logical Font Description) format handler.
180 ;; Define XLFD's field index numbers. ; field name
181 (defconst xlfd-regexp-foundry-subnum
0) ; FOUNDRY
182 (defconst xlfd-regexp-family-subnum
1) ; FAMILY_NAME
183 (defconst xlfd-regexp-weight-subnum
2) ; WEIGHT_NAME
184 (defconst xlfd-regexp-slant-subnum
3) ; SLANT
185 (defconst xlfd-regexp-swidth-subnum
4) ; SETWIDTH_NAME
186 (defconst xlfd-regexp-adstyle-subnum
5) ; ADD_STYLE_NAME
187 (defconst xlfd-regexp-pixelsize-subnum
6) ; PIXEL_SIZE
188 (defconst xlfd-regexp-pointsize-subnum
7) ; POINT_SIZE
189 (defconst xlfd-regexp-resx-subnum
8) ; RESOLUTION_X
190 (defconst xlfd-regexp-resy-subnum
9) ; RESOLUTION_Y
191 (defconst xlfd-regexp-spacing-subnum
10) ; SPACING
192 (defconst xlfd-regexp-avgwidth-subnum
11) ; AVERAGE_WIDTH
193 (defconst xlfd-regexp-registry-subnum
12) ; CHARSET_REGISTRY
194 (defconst xlfd-regexp-encoding-subnum
13) ; CHARSET_ENCODING
196 ;; Regular expression matching against a fontname which conforms to
197 ;; XLFD (X Logical Font Description). All fields in XLFD should be
198 ;; not be omitted (but can be a wild card) to be matched.
199 (defconst xlfd-tight-regexp
201 -\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
202 -\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
203 -\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)$")
205 ;; List of field numbers of XLFD whose values are numeric.
206 (defconst xlfd-regexp-numeric-subnums
207 (list xlfd-regexp-pixelsize-subnum
;6
208 xlfd-regexp-pointsize-subnum
;7
209 xlfd-regexp-resx-subnum
;8
210 xlfd-regexp-resy-subnum
;9
211 xlfd-regexp-avgwidth-subnum
;11
214 (defun x-decompose-font-name (pattern)
215 "Decompose PATTERN into XLFD's fields and return vector of the fields.
216 The length of the vector is 14.
218 If PATTERN doesn't conform to XLFD, try to get a full XLFD name from
219 X server and use the information of the full name to decompose
220 PATTERN. If no full XLFD name is gotten, return nil."
221 (let (xlfd-fields fontname
)
222 (if (string-match xlfd-tight-regexp pattern
)
224 (setq xlfd-fields
(make-vector 14 nil
))
226 (aset xlfd-fields i
(match-string (1+ i
) pattern
))
229 (setq fontname
(condition-case nil
230 (x-resolve-font-name pattern
)
233 (string-match xlfd-tight-regexp fontname
))
234 ;; We get a full XLFD name.
235 (let ((len (length pattern
))
238 ;; Setup xlfd-fields by the full XLFD name. Each element
239 ;; should be a cons of matched index and matched string.
240 (setq xlfd-fields
(make-vector 14 nil
))
243 (cons (match-beginning (1+ i
))
244 (match-string (1+ i
) fontname
)))
247 ;; Replace wild cards in PATTERN by regexp codes.
250 (let ((ch (aref pattern i
)))
252 (setq pattern
(concat (substring pattern
0 i
)
254 (substring pattern
(1+ i
)))
258 (setq pattern
(concat (substring pattern
0 i
)
260 (substring pattern
(1+ i
)))
265 ;; Set each element of xlfd-fields to proper strings.
266 (if (string-match pattern fontname
)
267 ;; The regular expression PATTERN matchs the full XLFD
268 ;; name. Set elements that correspond to a wild card
269 ;; in PATTERN to "*", set the other elements to the
270 ;; exact strings in PATTERN.
271 (let ((l (cdr (cdr (match-data)))))
274 (if (or (null l
) (< (car (aref xlfd-fields i
)) (car l
)))
276 (aset xlfd-fields i
(cdr (aref xlfd-fields i
)))
278 (if (< (car (aref xlfd-fields i
)) (car (cdr l
)))
280 (aset xlfd-fields i
"*")
282 (setq l
(cdr (cdr l
)))))))
283 ;; Set each element of xlfd-fields to the exact string
284 ;; in the corresonding fields in full XLFD name.
287 (aset xlfd-fields i
(cdr (aref xlfd-fields i
)))
291 ;; Replace consecutive wild-cards (`*') in NAME to one.
292 ;; Ex. (x-reduce-font-name "-*-*-*-iso8859-1") => "-*-iso8859-1"
293 (defsubst x-reduce-font-name
(name)
294 (while (string-match "-\\*-\\(\\*-\\)+" name
)
295 (setq name
(replace-match "-*-" t t name
)))
298 (defun x-compose-font-name (fields &optional reduce
)
299 "Compose X's fontname from FIELDS.
300 FIELDS is a vector of XLFD fields, the length 14.
301 If a field is nil, wild-card letter `*' is embedded.
302 Optional argument REDUCE is always ignored. It exists just for
303 backward compatibility."
304 (concat "-" (mapconcat (lambda (x) (or x
"*")) fields
"-")))
306 (defun x-complement-fontset-spec (xlfd-fields fontlist
)
307 "Complement FONTLIST for charsets based on XLFD-FIELDS and return it.
308 XLFD-FIELDS is a vector of XLFD (X Logical Font Description) fields.
309 FONTLIST is an alist of charsets vs the corresponding font names.
311 The fonts are complemented as below.
313 If FONTLIST doesn't specify a font for ASCII charset, generate a font
314 name for the charset from XLFD-FIELDS, and add that information to
317 If a font specifid for ASCII supports the other charsets (see the
318 variable `x-font-name-charset-alist'), add that information to FONTLIST."
319 (let ((ascii-font (cdr (assq 'ascii fontlist
))))
321 ;; If font for ASCII is not specified, add it.
323 (aset xlfd-fields xlfd-regexp-registry-subnum
"iso8859")
324 (aset xlfd-fields xlfd-regexp-encoding-subnum
"1")
325 (setq ascii-font
(x-compose-font-name xlfd-fields
))
326 (setq fontlist
(cons (cons 'ascii ascii-font
) fontlist
)))
328 ;; If the font for ASCII also supports the other charsets, and
329 ;; they are not specified in FONTLIST, add them.
330 (let ((tail x-font-name-charset-alist
)
333 (setq elt
(car tail
) tail
(cdr tail
))
334 (if (string-match (car elt
) ascii-font
)
335 (let ((charsets (cdr elt
))
338 (setq charset
(car charsets
) charsets
(cdr charsets
))
339 (or (assq charset fontlist
)
341 (cons (cons charset ascii-font
) fontlist
))))))))
345 (defun fontset-name-p (fontset)
346 "Return non-nil if FONTSET is valid as fontset name.
347 A valid fontset name should conform to XLFD (X Logical Font Description)
348 with \"fontset\" in `<CHARSET_REGISTRY> field."
349 (and (string-match xlfd-tight-regexp fontset
)
350 (string= (match-string (1+ xlfd-regexp-registry-subnum
) fontset
)
353 ;; Return a list to be appended to `x-fixed-font-alist' when
354 ;; `mouse-set-font' is called.
355 (defun generate-fontset-menu ()
356 (let ((fontsets (fontset-list))
360 (setq fontset-name
(car fontsets
) fontsets
(cdr fontsets
))
361 (setq l
(cons (list (fontset-plain-name fontset-name
) fontset-name
) l
)))
363 (sort l
(function (lambda (x y
) (string< (car x
) (car y
))))))))
365 (defun fontset-plain-name (fontset)
366 "Return a plain and descriptive name of FONTSET."
367 (if (not (setq fontset
(query-fontset fontset
)))
368 (error "Invalid fontset: %s" fontset
))
369 (let ((xlfd-fields (x-decompose-font-name fontset
)))
371 (let ((weight (aref xlfd-fields xlfd-regexp-weight-subnum
))
372 (slant (aref xlfd-fields xlfd-regexp-slant-subnum
))
373 (swidth (aref xlfd-fields xlfd-regexp-swidth-subnum
))
374 (size (aref xlfd-fields xlfd-regexp-pixelsize-subnum
))
375 (charset (aref xlfd-fields xlfd-regexp-registry-subnum
))
376 (nickname (aref xlfd-fields xlfd-regexp-encoding-subnum
))
378 (if (not (string= "fontset" charset
))
380 (if (> (string-to-int size
) 0)
381 (setq name
(format "%s: %s-dot" nickname size
))
382 (setq name nickname
))
383 (cond ((string-match "^medium$" weight
)
384 (setq name
(concat name
" " "medium")))
385 ((string-match "^bold$\\|^demibold$" weight
)
386 (setq name
(concat name
" " weight
))))
387 (cond ((string-match "^i$" slant
)
388 (setq name
(concat name
" " "italic")))
389 ((string-match "^o$" slant
)
390 (setq name
(concat name
" " "slant")))
391 ((string-match "^ri$" slant
)
392 (setq name
(concat name
" " "reverse italic")))
393 ((string-match "^ro$" slant
)
394 (setq name
(concat name
" " "reverse slant"))))
399 (defun create-fontset-from-fontset-spec (fontset-spec
400 &optional style-variant noerror
)
401 "Create a fontset from fontset specification string FONTSET-SPEC.
402 FONTSET-SPEC is a string of the format:
403 FONTSET-NAME,CHARSET-NAME0:FONT-NAME0,CHARSET-NAME1:FONT-NAME1, ...
404 Any number of SPACE, TAB, and NEWLINE can be put before and after commas.
406 Optional 2nd argument is ignored. It exists just for backward
409 If this function attempts to create already existing fontset, error is
410 signaled unless the optional 3rd argument NOERROR is non-nil.
412 It returns a name of the created fontset."
413 (if (not (string-match "^[^,]+" fontset-spec
))
414 (error "Invalid fontset spec: %s" fontset-spec
))
415 (setq fontset-spec
(downcase fontset-spec
))
416 (let ((idx (match-end 0))
417 (name (match-string 0 fontset-spec
))
418 xlfd-fields charset fontlist ascii-font
)
419 (if (query-fontset name
)
421 (error "Fontset \"%s\" already exists" name
))
422 (setq xlfd-fields
(x-decompose-font-name name
))
424 (error "Fontset \"%s\" not conforming to XLFD" name
))
426 ;; At first, extract pairs of charset and fontname from FONTSET-SPEC.
427 (while (string-match "[, \t\n]*\\([^:]+\\):\\([^,]+\\)" fontset-spec idx
)
428 (setq idx
(match-end 0))
429 (setq charset
(intern (match-string 1 fontset-spec
)))
430 (if (charsetp charset
)
431 (setq fontlist
(cons (cons charset
(match-string 2 fontset-spec
))
434 ;; Complement FONTLIST.
435 (setq fontlist
(x-complement-fontset-spec xlfd-fields fontlist
))
437 (new-fontset name fontlist
)
439 ;; Define the short name alias.
440 (if (and (string-match "fontset-.*$" name
)
441 (not (assoc name fontset-alias-alist
)))
442 (let ((alias (match-string 0 name
)))
443 (or (rassoc alias fontset-alias-alist
)
444 (setq fontset-alias-alist
445 (cons (cons name alias
) fontset-alias-alist
)))))
447 ;; Define the ASCII font name alias.
448 (setq ascii-font
(cdr (assq 'ascii fontlist
)))
449 (or (rassoc ascii-font fontset-alias-alist
)
450 (setq fontset-alias-alist
451 (cons (cons name ascii-font
)
452 fontset-alias-alist
))))
456 (defun create-fontset-from-ascii-font (font &optional resolved-font
458 "Create a fontset from an ASCII font FONT.
460 Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
461 omitted, x-resolve-font-name is called to get the resolved name. At
462 this time, if FONT is not available, error is signaled.
464 Optional 2nd arg FONTSET-NAME is a string to be used in
465 `<CHARSET_ENCODING>' fields of a new fontset name. If it is omitted,
466 an appropriate name is generated automatically.
468 It returns a name of the created fontset."
469 (setq font
(downcase font
))
471 (setq resolved-font
(downcase resolved-font
))
472 (setq resolved-font
(downcase (x-resolve-font-name font
))))
473 (let ((xlfd (x-decompose-font-name font
))
474 (resolved-xlfd (x-decompose-font-name resolved-font
))
475 fontset fontset-spec
)
476 (aset xlfd xlfd-regexp-foundry-subnum nil
)
477 (aset xlfd xlfd-regexp-family-subnum nil
)
478 (aset xlfd xlfd-regexp-registry-subnum
"fontset")
480 (setq fontset-name
(downcase fontset-name
))
483 (aref resolved-xlfd xlfd-regexp-registry-subnum
)
484 (aref resolved-xlfd xlfd-regexp-encoding-subnum
)
485 (aref resolved-xlfd xlfd-regexp-pixelsize-subnum
))))
486 (aset xlfd xlfd-regexp-encoding-subnum fontset-name
)
487 (setq fontset
(x-compose-font-name xlfd
))
488 (or (query-fontset fontset
)
489 (create-fontset-from-fontset-spec (concat fontset
", ascii:" font
)))))
492 ;; Create standard fontset from 16 dots fonts which are the most widely
493 ;; installed fonts. Fonts for Chinese-GB, Korean, and Chinese-CNS are
494 ;; specified here because FAMILY of those fonts are not "fixed" in
496 (defvar standard-fontset-spec
497 (purecopy "-*-fixed-medium-r-normal-*-16-*-*-*-*-*-fontset-standard,
498 chinese-gb2312:-*-medium-r-normal-*-16-*-gb2312*-*,
499 korean-ksc5601:-*-medium-r-normal-*-16-*-ksc5601*-*,
500 chinese-cns11643-1:-*-medium-r-normal-*-16-*-cns11643*-1,
501 chinese-cns11643-2:-*-medium-r-normal-*-16-*-cns11643*-2,
502 chinese-cns11643-3:-*-medium-r-normal-*-16-*-cns11643*-3,
503 chinese-cns11643-4:-*-medium-r-normal-*-16-*-cns11643*-4,
504 chinese-cns11643-5:-*-medium-r-normal-*-16-*-cns11643*-5,
505 chinese-cns11643-6:-*-medium-r-normal-*-16-*-cns11643*-6,
506 chinese-cns11643-7:-*-medium-r-normal-*-16-*-cns11643*-7")
507 "String of fontset spec of the standard fontset.
508 You have the biggest chance to display international characters
509 with correct glyphs by using the standard fontset.
510 See the documentation of `create-fontset-from-fontset-spec' for the format.")
512 ;; Create fontsets from X resources of the name `fontset-N (class
513 ;; Fontset-N)' where N is integer 0, 1, ...
514 ;; The values of the resources the string of the same format as
515 ;; `standard-fontset-spec'.
517 (defun create-fontset-from-x-resource ()
520 (while (setq fontset-spec
(x-get-resource (format "fontset-%d" idx
)
521 (format "Fontset-%d" idx
)))
522 (create-fontset-from-fontset-spec fontset-spec t
'noerror
)
523 (setq idx
(1+ idx
)))))
528 ;;; fontset.el ends here