Sync to HEAD
[bpt/emacs.git] / lisp / international / fontset.el
CommitLineData
60370d40 1;;; fontset.el --- commands for handling fontset
4ed46869 2
fa526c4a 3;; Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
8f924df7 4;; Licensed to the Free Software Foundation.
4fbe2b13 5;; Copyright (C) 2001 Free Software Foundation, Inc.
8f924df7
KH
6;; Copyright (C) 2003
7;; National Institute of Advanced Industrial Science and Technology (AIST)
8;; Registration Number H13PRO009
4ed46869 9
8135c753 10;; Keywords: mule, i18n, fontset
4ed46869
KH
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
369314dc
KH
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
4ed46869 28
60370d40
PJ
29;;; Commentary:
30
4ed46869
KH
31;;; Code:
32
37a94e2f
KH
33;; Setup font-encoding-alist for all known encodings.
34
35(setq font-encoding-alist
36 '(("iso8859-1$" . iso-8859-1)
37 ("iso8859-2$" . iso-8859-2)
38 ("iso8859-3$" . iso-8859-3)
39 ("iso8859-4$" . iso-8859-4)
40 ("iso8859-5$" . iso-8859-5)
41 ("iso8859-6$" . iso-8859-6)
42 ("iso8859-7$" . iso-8859-7)
43 ("iso8859-8$" . iso-8859-8)
44 ("iso8859-9$" . iso-8859-9)
45 ("iso8859-10$" . iso-8859-10)
46 ("iso8859-11$" . iso-8859-11)
47 ("iso8859-13$" . iso-8859-13)
48 ("iso8859-14$" . iso-8859-14)
49 ("iso8859-15$" . iso-8859-15)
50 ("gb2312.1980" . chinese-gb2312)
51 ("jisx0208.1978" . japanese-jisx0208-1978)
52 ("jisx0208" . japanese-jisx0208)
53 ("jisx0201" . jisx0201)
54 ("jisx0212" . japanese-jisx0212)
55 ("ksc5601.1987" . korean-ksc5601)
56 ("cns11643.1992.*1" . chinese-cns11643-1)
57 ("cns11643.1992.*2" . chinese-cns11643-2)
58 ("cns11643.1992.*3" . chinese-cns11643-3)
59 ("cns11643.1992.*4" . chinese-cns11643-4)
60 ("cns11643.1992.*5" . chinese-cns11643-5)
61 ("cns11643.1992.*6" . chinese-cns11643-6)
62 ("cns11643.1992.*7" . chinese-cns11643-7)
eb62213c
KH
63 ("cns11643.92p1-0" . chinese-cns11643-1)
64 ("cns11643.92p2-0" . chinese-cns11643-2)
65 ("cns11643.92p3-0" . chinese-cns11643-3)
66 ("cns11643.92p4-0" . chinese-cns11643-4)
67 ("cns11643.92p5-0" . chinese-cns11643-5)
68 ("cns11643.92p6-0" . chinese-cns11643-6)
69 ("cns11643.92p7-0" . chinese-cns11643-7)
37a94e2f
KH
70 ("big5" . big5)
71 ("sisheng_cwnn" . chinese-sisheng)
72 ("viscii" . viscii)
73 ("tis620" . tis620-2533)
74 ("mulearabic-0" . arabic-digit)
75 ("mulearabic-1" . arabic-1-column)
76 ("mulearabic-2" . arabic-2-column)
77 ("muleipa" . ipa)
78 ("ethiopic-unicode" . ethiopic)
79 ("is13194-devanagari" . indian-is13194)
8f924df7
KH
80 ("Devanagari-CDAC" . devanagari-cdac)
81 ("Sanskrit-CDAC" . sanskrit-cdac)
82 ("Bengali-CDAC" . bengali-cdac)
83 ("Assamese-CDAC" . assamese-cdac)
84 ("Punjabi-CDAC" . punjabi-cdac)
85 ("Gujarati-CDAC" . gujarati-cdac)
86 ("Oriya-CDAC" . oriya-cdac)
87 ("Tamil-CDAC" . tamil-cdac)
88 ("Telugu-CDAC" . telugu-cdac)
89 ("Kannada-CDAC" . kannada-cdac)
90 ("Malayalam-CDAC" . malayalam-cdac)
91 ("Devanagari-Akruti" . devanagari-akruti)
92 ("Bengali-Akruti" . bengali-akruti)
93 ("Punjabi-Akruti" . punjabi-akruti)
94 ("Gujarati-Akruti" . gujarati-akruti)
95 ("Oriya-Akruti" . oriya-akruti)
96 ("Tamil-Akruti" . tamil-akruti)
97 ("Telugu-Akruti" . telugu-akruti)
98 ("Kannada-Akruti" . kannada-akruti)
99 ("Malayalam-Akruti" . malayalam-akruti)
37a94e2f
KH
100 ("muleindian-2" . indian-2-column)
101 ("muleindian-1" . indian-1-column)
102 ("mulelao-1" . mule-lao)
103 ("muletibetan-2" . tibetan)
104 ("muletibetan-1" . tibetan-1-column)
105 ("jisx0213.2000-1" . japanese-jisx0213-1)
106 ("jisx0213.2000-2" . japanese-jisx0213-2)
5d4fe8aa 107 ("jisx0213.2004-1" . japanese-jisx0213.2004-1)
37a94e2f 108 ("abobe-symbol" . symbol)
45a830cf 109 ("iso10646-1$" . (unicode-bmp . nil))
45a830cf 110 ("iso10646.indian-1" . (unicode-bmp . nil))))
37a94e2f
KH
111
112
2ad00186 113;; Set standard fontname specification of characters in the default
37a94e2f
KH
114;; fontset to find an appropriate font for each script/charset. The
115;; specification has the form ((SCRIPT FONT-SPEC ...) ...), where
116;; FONT-SPEC is:
117;; a vector [ FAMILY WEIGHT SLANT ADSTYLE REGISTRY ],
118;; or a cons (FAMILY . REGISTRY),
119;; or a string FONT-NAME.
8f924df7 120;;
37a94e2f
KH
121;; FAMILY, WEIGHT, SLANT, and ADSTYLE may be nil, in which case, the
122;; the corresponding name of default face is used. If REGISTRY
2ad00186
KH
123;; contains a character `-', the string before that is embedded in
124;; `CHARSET_REGISTRY' field, and the string after that is embedded in
125;; `CHARSET_ENCODING' field. If it does not contain `-', the whole
126;; string is embedded in `CHARSET_REGISTRY' field, and a wild card
37a94e2f
KH
127;; character `*' is embedded in `CHARSET_ENCODING' field.
128;;
129;; SCRIPT is a symbol that appears as an element of the char table
130;; `char-script-table'. SCRIPT may be a charset specifying the range
131;; of characters.
2ad00186 132
c963bbc2
KH
133(defun setup-default-fontset ()
134 "Setup the default fontset."
8f924df7
KH
135 (new-fontset
136 "fontset-default"
137 '(;; for each script
138 (latin (nil . "ISO8859-1")
139 (nil . "ISO8859-2")
140 (nil . "ISO8859-3")
141 (nil . "ISO8859-4")
142 (nil . "ISO8859-9")
143 (nil . "ISO8859-10")
144 (nil . "ISO8859-13")
145 (nil . "ISO8859-14")
146 (nil . "ISO8859-15")
147 (nil . "VISCII1.1-1"))
148
149 (thai (nil . "TIS620*")
150 (nil . "ISO8859-11"))
151
152 (devanagari (nil . "iso10646.indian-1"))
153
154 (lao (nil . "MuleLao-1"))
155
156 ;; both for script and charset.
157 (tibetan (nil . "muletibetan-2"))
158
159 ;; both for script and charset.
160 (ethiopic (nil . "ethiopic-unicode"))
161
162 (greek (nil . "ISO8859-7"))
163
164 (cyrillic (nil . "ISO8859-5"))
165
166 (arabic (nil . "MuleArabic-0")
167 (nil . "MuleArabic-1")
168 (nil . "MuleArabic-2")
169 (nil . "ISO8859-6"))
170
171 (hebrew (nil . "ISO8859-8"))
172
173 (kana (nil . "JISX0208*")
174 (nil . "GB2312.1980-0")
175 (nil . "KSC5601.1987*")
d1bd4b12
KH
176 (nil . "JISX0201*")
177 (nil . "JISX0213.2000-1")
178 (nil . "JISX0213.2004-1"))
8f924df7
KH
179
180 (bopomofo (nil . "sisheng_cwnn-0"))
181
182 (han (nil . "GB2312.1980-0")
183 (nil . "JISX0208*")
184 (nil . "JISX0212*")
185 (nil . "big5*")
186 (nil . "KSC5601.1987*")
187 (nil . "CNS11643.1992-1")
188 (nil . "CNS11643.1992-2")
189 (nil . "CNS11643.1992-3")
190 (nil . "CNS11643.1992-4")
191 (nil . "CNS11643.1992-5")
192 (nil . "CNS11643.1992-6")
193 (nil . "CNS11643.1992-7")
194 (nil . "gbk-0")
195 (nil . "JISX0213.2000-1")
a16abfed
KH
196 (nil . "JISX0213.2000-2")
197 (nil . "JISX0213.2004-1"))
8f924df7
KH
198
199 (cjk-misc (nil . "GB2312.1980-0")
200 (nil . "JISX0208*")
201 (nil . "JISX0212*")
202 (nil . "big5*")
203 (nil . "KSC5601.1987*")
204 (nil . "CNS11643.1992-1")
205 (nil . "CNS11643.1992-2")
206 (nil . "CNS11643.1992-3")
207 (nil . "CNS11643.1992-4")
208 (nil . "CNS11643.1992-5")
209 (nil . "CNS11643.1992-6")
210 (nil . "CNS11643.1992-7")
211 (nil . "gbk-0")
212 (nil . "JISX0213.2000-1")
213 (nil . "JISX0213.2000-2"))
214
215 (hangul (nil . "KSC5601.1987-0"))
216
217 ;; for each charset
218 (ascii (nil . "ISO8859-1"))
219 (arabic-digit ("*" . "MuleArabic-0"))
220 (arabic-1-column ("*" . "MuleArabic-1"))
221 (arabic-2-column ("*" . "MuleArabic-2"))
222 (indian-is13194 (nil . "is13194-devanagari"))
223 (indian-1-column ("*" . "muleindian-2"))
224 ;; Indian CDAC
225 (devanagari-cdac (nil . "Devanagari-CDAC"))
226 (sanskrit-cdac (nil . "Sanskrit-CDAC"))
227 (bengali-cdac (nil . "Bengali-CDAC"))
228 (assamese-cdac (nil . "Assamese-CDAC"))
229 (punjabi-cdac (nil . "Punjabi-CDAC"))
230 (gujarati-cdac (nil . "Gujarati-CDAC"))
231 (oriya-cdac (nil . "Oriya-CDAC"))
232 (tamil-cdac (nil . "Tamil-CDAC"))
233 (telugu-cdac (nil . "Telugu-CDAC"))
234 (kannada-cdac (nil . "Kannada-CDAC"))
235 (malayalam-cdac (nil . "Malayalam-CDAC"))
236 ;; Indian AKRUTI
237 (devanagari-akruti (nil . "Devanagari-Akruti"))
238 (bengali-akruti (nil . "Bengali-Akruti"))
239 (punjabi-akruti (nil . "Punjabi-Akruti"))
240 (gujarati-akruti (nil . "Gujarati-Akruti"))
241 (oriya-akruti (nil . "Oriya-Akruti"))
242 (tamil-akruti (nil . "Tamil-Akruti"))
243 (telugu-akruti (nil . "Telugu-Akruti"))
244 (kannada-akruti (nil . "Kannada-Akruti"))
245 (malayalam-akruti (nil . "Malayalam-Akruti"))
246 ;;(devanagari-glyph ("altsys-dv_ttsurekh" . "devanagari-cdac"))
247 ;;(malayalam-glyph ("altsys-ml_ttkarthika" . "malayalam-cdac"))
248 (ipa (nil . "MuleIPA-1"))
d1bd4b12
KH
249
250 ;; Fallback fonts
251 (nil (nil . "gb2312.1980")
252 (nil . "jisx0208")
253 (nil . "ksc5601.1987")
eb62213c
KH
254 (nil . "CNS11643.1992-1")
255 (nil . "CNS11643.1992-2")
256 (nil . "CNS11643.1992-3")
257 (nil . "CNS11643.1992-4")
258 (nil . "CNS11643.1992-5")
259 (nil . "CNS11643.1992-6")
260 (nil . "CNS11643.1992-7")
d1bd4b12
KH
261 (nil . "big5")
262 (nil . "jisx0213.2000-1")
263 (nil . "jisx0213.2004-1")
264 (nil . "jisx0212"))
8f924df7
KH
265 ))
266
267 ;; Append Unicode fonts.
268 ;; This may find fonts with more variants (bold, italic) but which
269 ;; don't cover many characters.
d1bd4b12 270 (set-fontset-font "fontset-default" nil
8f924df7
KH
271 '(nil . "iso10646-1") nil 'append)
272 ;; These may find fonts that cover many characters but with fewer
273 ;; variants.
d1bd4b12 274 (set-fontset-font "fontset-default" nil
8f924df7 275 '("gnu-unifont" . "iso10646-1") nil 'append)
d1bd4b12 276 (set-fontset-font "fontset-default" nil
8f924df7 277 '("mutt-clearlyu" . "iso10646-1") nil 'append))
06f76f0d 278
8135c753
DL
279;; These are the registered registries/encodings from
280;; ftp://ftp.x.org/pub/DOCS/registry 2001/06/01
281
282;; Name Reference
283;; ---- ---------
284;; "DEC" [27]
285;; registry prefix
286;; "DEC.CNS11643.1986-2" [53]
287;; CNS11643 2-plane using the encoding
288;; suggested in that standard
289;; "DEC.DTSCS.1990-2" [54]
290;; DEC Taiwan Supplemental Character Set
291;; "fujitsu.u90x01.1991-0" [87]
292;; "fujitsu.u90x03.1991-0" [87]
293;; "GB2312.1980-0" [39],[12]
294;; China (PRC) Hanzi, GL encoding
295;; "GB2312.1980-1" [39]
296;; (deprecated)
297;; China (PRC) Hanzi, GR encoding
298;; "HP-Arabic8" [36]
299;; HPARABIC8 8-bit character set
300;; "HP-East8" [36]
301;; HPEAST8 8-bit character set
302;; "HP-Greek8" [36]
303;; HPGREEK8 8-bit character set
304;; "HP-Hebrew8" [36]
305;; HPHEBREW8 8-bit character set
306;; "HP-Japanese15" [36]
307;; HPJAPAN15 15-bit characer set,
308;; modified from industry defacto
309;; standard Shift-JIS
310;; "HP-Kana8" [36]
311;; HPKANA8 8-bit character set
312;; "HP-Korean15" [36]
313;; HPKOREAN15 15-bit character set
314;; "HP-Roman8" [36]
315;; HPROMAN8 8-bit character set
316;; "HP-SChinese15" [36]
317;; HPSCHINA15 15-bit character set for
318;; support of Simplified Chinese
319;; "HP-TChinese15" [36]
320;; HPTCHINA15 15-bit character set for
321;; support of Traditional Chinese
322;; "HP-Turkish8" [36]
323;; HPTURKISH8 8-bit character set
324;; "IPSYS" [59]
325;; registry prefix
326;; "IPSYS.IE-1" [59]
327;; "ISO2022"<REG>"-"<ENC> [44]
328;; "ISO646.1991-IRV" [107]
329;; ISO 646 International Reference Version
330;; "ISO8859-1" [15],[12]
331;; ISO Latin alphabet No. 1
332;; "ISO8859-2" [15],[12]
333;; ISO Latin alphabet No. 2
334;; "ISO8859-3" [15],[12]
335;; ISO Latin alphabet No. 3
336;; "ISO8859-4" [15],[12]
337;; ISO Latin alphabet No. 4
338;; "ISO8859-5" [15],[12]
339;; ISO Latin/Cyrillic alphabet
340;; "ISO8859-6" [15],[12]
341;; ISO Latin/Arabic alphabet
342;; "ISO8859-7" [15],[12]
343;; ISO Latin/Greek alphabet
344;; "ISO8859-8" [15],[12]
345;; ISO Latin/Hebrew alphabet
346;; "ISO8859-9" [15],[12]
347;; ISO Latin alphabet No. 5
348;; "ISO8859-10" [15],[12]
349;; ISO Latin alphabet No. 6
350;; "ISO8859-13" [15],[12]
351;; ISO Latin alphabet No. 7
352;; "ISO8859-14" [15],[12]
353;; ISO Latin alphabet No. 8
354;; "ISO8859-15" [15],[12]
355;; ISO Latin alphabet No. 9
356;; "FCD8859-15" [7]
357;; (deprecated)
358;; ISO Latin alphabet No. 9, Final Committee Draft
359;; "ISO10646-1" [133]
360;; Unicode Universal Multiple-Octet Coded Character Set
361;; "ISO10646-MES" [133]
362;; (deprecated)
363;; Unicode Minimum European Subset
364;; "JISX0201.1976-0" [38],[12]
365;; 8-Bit Alphanumeric-Katakana Code
366;; "JISX0208.1983-0" [40],[12]
367;; Japanese Graphic Character Set,
368;; GL encoding
369;; "JISX0208.1990-0" [71]
370;; Japanese Graphic Character Set,
371;; GL encoding
372;; "JISX0208.1983-1" [40]
373;; (deprecated)
374;; Japanese Graphic Character Set,
375;; GR encoding
376;; "JISX0212.1990-0" [72]
377;; Supplementary Japanese Graphic Character Set,
378;; GL encoding
379;; "KOI8-R" [119]
380;; Cyrillic alphabet
381;; "KSC5601.1987-0" [41],[12]
382;; Korean Graphic Character Set,
383;; GL encoding
384;; "KSC5601.1987-1" [41]
385;; (deprecated)
386;; Korean Graphic Character Set,
387;; GR encoding
388;; "omron_CNS11643-0" [45]
389;; "omron_CNS11643-1" [45]
390;; "omron_BIG5-0" [45]
391;; "omron_BIG5-1" [45]
392;; "wn.tamil.1993" [103]
4ed46869 393
06f76f0d 394(defun set-font-encoding (pattern charset)
8135c753 395 "Set arguments in `font-encoding-alist' (which see)."
4ed46869
KH
396 (let ((slot (assoc pattern font-encoding-alist)))
397 (if slot
06f76f0d 398 (setcdr slot charset)
4ed46869 399 (setq font-encoding-alist
06f76f0d 400 (cons (cons pattern charset) font-encoding-alist)))))
12385722 401
4ed46869
KH
402;; Setting for suppressing XLoadQueryFont on big fonts.
403(setq x-pixel-size-width-font-regexp
404 "gb2312\\|jisx0208\\|ksc5601\\|cns11643\\|big5")
405
6eca8d93 406;; These fonts require vertical centering.
0c4f97d0 407(setq vertical-centering-font-regexp
6eca8d93 408 "gb2312\\|jisx0208\\|jisx0212\\|ksc5601\\|cns11643\\|big5")
0c4f97d0 409
e46947a5
KH
410;; CDAC fonts are actually smaller than their design sizes.
411(setq face-font-rescale-alist
412 '(("-cdac$" . 1.3)))
413
06f76f0d
KH
414(defvar x-font-name-charset-alist nil
415 "This variable has no meaning now. Just kept for backward compatibility.")
6fb8f8bd 416
4ed46869
KH
417;;; XLFD (X Logical Font Description) format handler.
418
419;; Define XLFD's field index numbers. ; field name
06f76f0d
KH
420(defconst xlfd-regexp-family-subnum 0) ; FOUNDRY and FAMILY
421(defconst xlfd-regexp-weight-subnum 1) ; WEIGHT_NAME
422(defconst xlfd-regexp-slant-subnum 2) ; SLANT
423(defconst xlfd-regexp-swidth-subnum 3) ; SETWIDTH_NAME
424(defconst xlfd-regexp-adstyle-subnum 4) ; ADD_STYLE_NAME
425(defconst xlfd-regexp-pixelsize-subnum 5) ; PIXEL_SIZE
426(defconst xlfd-regexp-pointsize-subnum 6) ; POINT_SIZE
427(defconst xlfd-regexp-resx-subnum 7) ; RESOLUTION_X
428(defconst xlfd-regexp-resy-subnum 8) ; RESOLUTION_Y
429(defconst xlfd-regexp-spacing-subnum 8) ; SPACING
430(defconst xlfd-regexp-avgwidth-subnum 10) ; AVERAGE_WIDTH
431(defconst xlfd-regexp-registry-subnum 11) ; REGISTRY and ENCODING
4ed46869
KH
432
433;; Regular expression matching against a fontname which conforms to
434;; XLFD (X Logical Font Description). All fields in XLFD should be
435;; not be omitted (but can be a wild card) to be matched.
436(defconst xlfd-tight-regexp
437 "^\
06f76f0d 438-\\([^-]*-[^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
4ed46869 439-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
06f76f0d
KH
440-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*-[^-]*\\)$")
441
442;; Regular expression matching against a fontname which conforms to
443;; XLFD (X Logical Font Description). All fields in XLFD from FOUNDRY
8135c753 444;; to ADSTYLE, REGISTRY, and ENCODING should be not be omitted (but
06f76f0d
KH
445;; can be a wild card) to be matched.
446(defconst xlfd-style-regexp
447 "^\
448-\\([^-]*-[^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-.*\
449-\\([^-]*-[^-]*\\)$")
4ed46869
KH
450
451;; List of field numbers of XLFD whose values are numeric.
452(defconst xlfd-regexp-numeric-subnums
06f76f0d
KH
453 (list xlfd-regexp-pixelsize-subnum ;5
454 xlfd-regexp-pointsize-subnum ;6
455 xlfd-regexp-resx-subnum ;7
456 xlfd-regexp-resy-subnum ;8
457 xlfd-regexp-avgwidth-subnum ;10
4ed46869
KH
458 ))
459
460(defun x-decompose-font-name (pattern)
92438d6e 461 "Decompose PATTERN into XLFD fields and return a vector of the fields.
06f76f0d 462The length of the vector is 12.
92438d6e
KH
463The FOUNDRY and FAMILY fields are concatinated and stored in the first
464element of the vector.
465The REGISTRY and ENCODING fields are concatinated and stored in the last
466element of the vector.
467
468Return nil if PATTERN doesn't conform to XLFD."
469 (if (string-match xlfd-tight-regexp pattern)
470 (let ((xlfd-fields (make-vector 12 nil)))
471 (dotimes (i 12)
472 (aset xlfd-fields i (match-string (1+ i) pattern)))
473 (dotimes (i 12)
474 (if (string-match "^[*-]+$" (aref xlfd-fields i))
475 (aset xlfd-fields i nil)))
476 xlfd-fields)))
441038a6 477
bb98ead9 478(defun x-compose-font-name (fields &optional reduce)
8135c753 479 "Compose X fontname from FIELDS.
06f76f0d 480FIELDS is a vector of XLFD fields, the length 12.
441038a6 481If a field is nil, wild-card letter `*' is embedded.
6eca8d93
KH
482Optional argument REDUCE is always ignored. It exists just for
483backward compatibility."
484 (concat "-" (mapconcat (lambda (x) (or x "*")) fields "-")))
bcb68cff 485
4fbe2b13
GM
486(defun x-must-resolve-font-name (xlfd-fields)
487 "Like `x-resolve-font-name', but always return a font name.
488XLFD-FIELDS is a vector of XLFD (X Logical Font Description) fields.
489If no font matching XLFD-FIELDS is available, successively replace
490parts of the font name pattern with \"*\" until some font is found.
491Value is name of that font."
492 (let ((ascii-font nil) (index 0))
06f76f0d 493 (while (and (null ascii-font) (<= index xlfd-regexp-registry-subnum))
4fbe2b13
GM
494 (let ((pattern (x-compose-font-name xlfd-fields)))
495 (condition-case nil
496 (setq ascii-font (x-resolve-font-name pattern))
497 (error
498 (message "Warning: no fonts matching `%s' available" pattern)
499 (aset xlfd-fields index "*")
500 (setq index (1+ index))))))
501 (unless ascii-font
37aad8f5 502 (error "No fonts found"))
4fbe2b13
GM
503 ascii-font))
504
505
4ed46869 506(defun x-complement-fontset-spec (xlfd-fields fontlist)
92438d6e 507 "Complement elements of FONTLIST based on XLFD-FIELDS.
4ed46869 508XLFD-FIELDS is a vector of XLFD (X Logical Font Description) fields.
92438d6e
KH
509FONTLIST is an alist of script names vs the corresponding font names.
510
511The font names are complemented as below.
512
513If a font name matches `xlfd-style-regexp', each field of wild card is
514replaced by the corresponding fields in XLFD-FIELDS."
515 (let ((default-spec (vector (aref xlfd-fields xlfd-regexp-family-subnum)
516 (aref xlfd-fields xlfd-regexp-weight-subnum)
517 (aref xlfd-fields xlfd-regexp-slant-subnum)
518 (aref xlfd-fields xlfd-regexp-swidth-subnum)
519 (aref xlfd-fields xlfd-regexp-adstyle-subnum)
520 (aref xlfd-fields xlfd-regexp-registry-subnum))))
06f76f0d 521 (dolist (elt fontlist)
37a94e2f 522 (let ((name (cadr elt))
06f76f0d 523 font-spec)
dcda246b
KH
524 (when (or (string-match xlfd-style-regexp name)
525 (and (setq name (car (x-list-fonts name nil nil 1)))
526 (string-match xlfd-style-regexp name)))
06f76f0d
KH
527 (setq font-spec (make-vector 6 nil))
528 (dotimes (i 6)
529 (aset font-spec i (match-string (1+ i) name)))
3662d9e7 530 (dotimes (i 5)
06f76f0d
KH
531 (if (string-match "^[*-]+$" (aref font-spec i))
532 (aset font-spec i (aref default-spec i))))
37a94e2f 533 (setcar (cdr elt) font-spec))))
a1506d29 534
6eca8d93 535 fontlist))
4ed46869 536
35d4066a
KH
537(defun fontset-name-p (fontset)
538 "Return non-nil if FONTSET is valid as fontset name.
539A valid fontset name should conform to XLFD (X Logical Font Description)
540with \"fontset\" in `<CHARSET_REGISTRY> field."
541 (and (string-match xlfd-tight-regexp fontset)
542 (string= (match-string (1+ xlfd-regexp-registry-subnum) fontset)
543 "fontset")))
544
4ed46869 545(defun generate-fontset-menu ()
8135c753
DL
546 "Return list to be appended to `x-fixed-font-alist'.
547Done when `mouse-set-font' is called."
d58b6a31
KH
548 (let (l)
549 (dolist (fontset (fontset-list))
550 (or (string-match "fontset-default$" fontset)
551 (push (list (fontset-plain-name fontset) fontset) l)))
80ae5f3a 552 (cons "Fontset"
d58b6a31 553 (sort l #'(lambda (x y) (string< (car x) (car y)))))))
4ed46869
KH
554
555(defun fontset-plain-name (fontset)
556 "Return a plain and descriptive name of FONTSET."
494ec9bc
KH
557 (if (not (setq fontset (query-fontset fontset)))
558 (error "Invalid fontset: %s" fontset))
4ed46869
KH
559 (let ((xlfd-fields (x-decompose-font-name fontset)))
560 (if xlfd-fields
87997c2c
KH
561 (let ((family (aref xlfd-fields xlfd-regexp-family-subnum))
562 (weight (aref xlfd-fields xlfd-regexp-weight-subnum))
4ed46869
KH
563 (slant (aref xlfd-fields xlfd-regexp-slant-subnum))
564 (swidth (aref xlfd-fields xlfd-regexp-swidth-subnum))
565 (size (aref xlfd-fields xlfd-regexp-pixelsize-subnum))
06f76f0d 566 (nickname (aref xlfd-fields xlfd-regexp-registry-subnum))
4ed46869 567 name)
06f76f0d 568 (if (not (string-match "^fontset-\\(.*\\)$" nickname))
87997c2c
KH
569 (setq nickname family)
570 (setq nickname (match-string 1 nickname)))
571 (if (and size (> (string-to-int size) 0))
572 (setq name (format "%s: %s-dot" nickname size))
573 (setq name nickname))
574 (and weight
575 (cond ((string-match "^medium$" weight)
576 (setq name (concat name " " "medium")))
577 ((string-match "^bold$\\|^demibold$" weight)
578 (setq name (concat name " " weight)))))
579 (and slant
580 (cond ((string-match "^i$" slant)
581 (setq name (concat name " " "italic")))
582 ((string-match "^o$" slant)
583 (setq name (concat name " " "slant")))
584 ((string-match "^ri$" slant)
585 (setq name (concat name " " "reverse italic")))
586 ((string-match "^ro$" slant)
587 (setq name (concat name " " "reverse slant")))))
588 name)
4ed46869
KH
589 fontset)))
590
37a94e2f 591(defvar charset-script-alist
8a020f4c 592 '((ascii . latin)
37a94e2f
KH
593 (latin-iso8859-1 . latin)
594 (latin-iso8859-2 . latin)
595 (latin-iso8859-3 . latin)
596 (latin-iso8859-4 . latin)
597 (latin-iso8859-9 . latin)
598 (latin-iso8859-10 . latin)
599 (latin-iso8859-13 . latin)
600 (latin-iso8859-14 . latin)
601 (latin-iso8859-15 . latin)
602 (latin-iso8859-16 . latin)
603 (latin-jisx0201 . latin)
604 (thai-tis620 . thai)
605 (cyrillic-iso8859-5 . cyrillic)
606 (arabic-iso8859-6 . arabic)
607 (greek-iso8859-7 . latin)
608 (hebrew-iso8859-8 . latin)
8a020f4c 609 (katakana-jisx0201 . kana)
37a94e2f
KH
610 (chinese-gb2312 . han)
611 (chinese-big5-1 . han)
612 (chinese-big5-2 . han)
613 (chinese-cns11643-1 . han)
614 (chinese-cns11643-2 . han)
615 (chinese-cns11643-3 . han)
616 (chinese-cns11643-4 . han)
617 (chinese-cns11643-5 . han)
618 (chinese-cns11643-6 . han)
619 (chinese-cns11643-7 . han)
620 (japanese-jisx0208 . han)
621 (japanese-jisx0208-1978 . han)
622 (japanese-jisx0212 . han)
623 (japanese-jisx0213-1 . han)
624 (japanese-jisx0213-2 . han)
8a020f4c 625 (korean-ksc5601 . hangul)
37a94e2f
KH
626 (chinese-sisheng . bopomofo)
627 (vietnamese-viscii-lower . latin)
628 (vietnamese-viscii-upper . latin)
629 (arabic-digit . arabic)
630 (arabic-1-column . arabic)
631 (arabic-2-column . arabic)
632 (indian-is13194 . devanagari)
633 (indian-glyph . devanagari)
634 (indian-1-column . devanagari)
635 (indian-2-column . devanagari)
636 (tibetan-1-column . tibetan))
637 "Alist of charsets vs the corresponding most appropriate scripts.
638
639This alist is used by the function `create-fontset-from-fontset-spec'
640to map charsets to scripts.")
c963bbc2 641
6fb8f8bd 642(defun create-fontset-from-fontset-spec (fontset-spec
bb98ead9 643 &optional style-variant noerror)
4ed46869
KH
644 "Create a fontset from fontset specification string FONTSET-SPEC.
645FONTSET-SPEC is a string of the format:
37a94e2f 646 FONTSET-NAME,SCRIPT-NAME0:FONT-NAME0,SCRIPT-NAME1:FONT-NAME1, ...
494ec9bc 647Any number of SPACE, TAB, and NEWLINE can be put before and after commas.
bb98ead9 648
92438d6e
KH
649When a frame uses the fontset as the `font' parameter, the frame's
650default font name is derived from FONTSET-NAME by substituting
651\"iso8859-1\" for the tail part \"fontset-XXX\". But, if SCRIPT-NAMEn
652is \"ascii\", use the corresponding FONT-NAMEn as the default font
653name.
bb98ead9 654
92438d6e
KH
655Optional 2nd and 3rd arguments are ignored. They exist just for
656backward compatibility.
1f50fef9 657
37a94e2f
KH
658It returns a name of the created fontset.
659
660For backward compatibility, SCRIPT-NAME may be a charset name, in
661which case, the corresponding script is decided by the variable
662`charset-script-alist' (which see)."
92438d6e 663 (or (string-match "^[^,]+" fontset-spec)
494ec9bc
KH
664 (error "Invalid fontset spec: %s" fontset-spec))
665 (let ((idx (match-end 0))
666 (name (match-string 0 fontset-spec))
92438d6e
KH
667 xlfd-fields script fontlist)
668 (setq xlfd-fields (x-decompose-font-name name))
669 (or xlfd-fields
670 (error "Fontset name \"%s\" not conforming to XLFD" name))
671
672 ;; At first, extract pairs of charset and fontname from FONTSET-SPEC.
673 (while (string-match "[, \t\n]*\\([^:]+\\):\\([^,]+\\)" fontset-spec idx)
674 (setq idx (match-end 0))
675 (setq script (intern (match-string 1 fontset-spec)))
676 (if (or (eq script 'ascii)
677 (memq script (char-table-extra-slot char-script-table 0))
678 (setq script (cdr (assq script charset-script-alist))))
679 (setq fontlist (cons (list script (match-string 2 fontset-spec))
680 fontlist))))
681
682 ;; Complement FONTLIST.
683 (setq fontlist (x-complement-fontset-spec xlfd-fields fontlist))
684
685 ;; Create a fontset.
686 (new-fontset name fontlist)))
1f50fef9
KH
687
688(defun create-fontset-from-ascii-font (font &optional resolved-font
689 fontset-name)
690 "Create a fontset from an ASCII font FONT.
691
692Optional 1st arg RESOLVED-FONT is a resolved name of FONT. If
37aad8f5 693omitted, `x-resolve-font-name' is called to get the resolved name. At
8135c753 694this time, if FONT is not available, an error is signaled.
1f50fef9
KH
695
696Optional 2nd arg FONTSET-NAME is a string to be used in
697`<CHARSET_ENCODING>' fields of a new fontset name. If it is omitted,
698an appropriate name is generated automatically.
699
1f50fef9 700It returns a name of the created fontset."
6eca8d93 701 (setq font (downcase font))
37a94e2f
KH
702 (setq resolved-font
703 (downcase (or resolved-font (x-resolve-font-name font))))
06f76f0d
KH
704 (let ((xlfd (x-decompose-font-name resolved-font))
705 fontset)
6eca8d93
KH
706 (if fontset-name
707 (setq fontset-name (downcase fontset-name))
87997c2c
KH
708 (if (query-fontset "fontset-startup")
709 (setq fontset-name
710 (subst-char-in-string
711 ?- ?_ (aref xlfd xlfd-regexp-registry-subnum) t))
712 (setq fontset-name "startup")))
06f76f0d
KH
713 (aset xlfd xlfd-regexp-registry-subnum
714 (format "fontset-%s" fontset-name))
1f50fef9
KH
715 (setq fontset (x-compose-font-name xlfd))
716 (or (query-fontset fontset)
6eca8d93
KH
717 (create-fontset-from-fontset-spec (concat fontset ", ascii:" font)))))
718
4ed46869 719\f
acfb10b8 720;; Create standard fontset from 16 dots fonts which are the most widely
80d4ea92
KH
721;; installed fonts. Fonts for Chinese-GB, Korean, and Chinese-CNS are
722;; specified here because FAMILY of those fonts are not "fixed" in
723;; many cases.
acfb10b8 724(defvar standard-fontset-spec
06f76f0d 725 (purecopy "-*-fixed-medium-r-normal-*-16-*-*-*-*-*-fontset-standard")
acfb10b8
KH
726 "String of fontset spec of the standard fontset.
727You have the biggest chance to display international characters
728with correct glyphs by using the standard fontset.
4ed46869
KH
729See the documentation of `create-fontset-from-fontset-spec' for the format.")
730
37a94e2f 731
4ed46869
KH
732;; Create fontsets from X resources of the name `fontset-N (class
733;; Fontset-N)' where N is integer 0, 1, ...
734;; The values of the resources the string of the same format as
acfb10b8 735;; `standard-fontset-spec'.
4ed46869
KH
736
737(defun create-fontset-from-x-resource ()
738 (let ((idx 0)
739 fontset-spec)
cdd675ad
DL
740 (while (setq fontset-spec (x-get-resource (format "fontset-%d" idx)
741 (format "Fontset-%d" idx)))
a5695549 742 (create-fontset-from-fontset-spec fontset-spec t 'noerror)
4ed46869
KH
743 (setq idx (1+ idx)))))
744
4ed46869
KH
745;;
746(provide 'fontset)
747
6b61353c 748;;; arch-tag: bb53e629-0234-403c-950e-551e61554849
4ed46869 749;;; fontset.el ends here