summaryrefslogtreecommitdiff
path: root/lisp/international/fontset.el
blob: cdbaade73abb15791db0a6bb6c0705e9b972c8a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
;;; fontset.el --- commands for handling fontset

;; Copyright (C) 1997-2015 Free Software Foundation, Inc.
;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
;;   2005, 2006, 2007, 2008, 2009, 2010, 2011
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H14PRO021
;; Copyright (C) 2003, 2006
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H13PRO009

;; Keywords: mule, i18n, fontset

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

;; Setup font-encoding-alist for all known encodings.

(setq font-encoding-alist
      (mapcar (lambda (arg)
		(cons (purecopy (car arg)) (cdr arg)))
      '(("iso8859-1$" . iso-8859-1)
	("iso8859-2$" . iso-8859-2)
	("iso8859-3$" . iso-8859-3)
	("iso8859-4$" . iso-8859-4)
	("iso8859-5$" . iso-8859-5)
	("iso8859-6$" . iso-8859-6)
	("iso8859-7$" . iso-8859-7)
	("iso8859-8$" . iso-8859-8)
	("iso8859-9$" . iso-8859-9)
	("iso8859-10$" . iso-8859-10)
	("iso8859-11$" . iso-8859-11)
	("iso8859-13$" . iso-8859-13)
	("iso8859-14$" . iso-8859-14)
	("iso8859-15$" . iso-8859-15)
	("iso8859-16$" . iso-8859-16)
	("ascii-0$" . ascii)
	("gb2312.1980" . chinese-gb2312)
	("gbk" . chinese-gbk)
	("gb18030" . (unicode . nil))
	("jisx0208.1978" . japanese-jisx0208-1978)
	("jisx0208" . japanese-jisx0208)
	("jisx0201" . jisx0201)
	("jisx0212" . japanese-jisx0212)
	("ksx1001" . korean-ksc5601)
	("ksc5601.1987" . korean-ksc5601)
	("cns11643.1992.*1" . chinese-cns11643-1)
	("cns11643.1992.*2" . chinese-cns11643-2)
	("cns11643.1992.*3" . chinese-cns11643-3)
	("cns11643.1992.*4" . chinese-cns11643-4)
	("cns11643.1992.*5" . chinese-cns11643-5)
	("cns11643.1992.*6" . chinese-cns11643-6)
	("cns11643.1992.*7" . chinese-cns11643-7)
	("cns11643.92p1-0" . chinese-cns11643-1)
	("cns11643.92p2-0" . chinese-cns11643-2)
	("cns11643.92p3-0" . chinese-cns11643-3)
	("cns11643.92p4-0" . chinese-cns11643-4)
	("cns11643.92p5-0" . chinese-cns11643-5)
	("cns11643.92p6-0" . chinese-cns11643-6)
	("cns11643.92p7-0" . chinese-cns11643-7)
	("big5" . big5)
	("viscii" . viscii)
	("tis620" . tis620-2533)
	("microsoft-cp1251" . windows-1251)
	("koi8-r" . koi8-r)
	("jisx0213.2000-1" . japanese-jisx0213-1)
	("jisx0213.2000-2" . japanese-jisx0213-2)
	("jisx0213.2004-1" . japanese-jisx0213.2004-1)
	("iso10646-1$" . (unicode-bmp . nil))
	("iso10646.indian-1" . (unicode-bmp . nil))
	("unicode-bmp" . (unicode-bmp . nil))
	("abobe-symbol" . symbol)
	("sisheng_cwnn" . chinese-sisheng)
	("mulearabic-0" . arabic-digit)
	("mulearabic-1" . arabic-1-column)
	("mulearabic-2" . arabic-2-column)
	("muleipa" . ipa)
	("ethiopic-unicode" . (unicode-bmp . ethiopic))
	("is13194-devanagari" . indian-is13194)
	("Devanagari-CDAC" . devanagari-cdac)
 	("Sanskrit-CDAC" . sanskrit-cdac)
 	("Bengali-CDAC" . bengali-cdac)
 	("Assamese-CDAC" . assamese-cdac)
 	("Punjabi-CDAC" . punjabi-cdac)
 	("Gujarati-CDAC" . gujarati-cdac)
 	("Oriya-CDAC" . oriya-cdac)
 	("Tamil-CDAC" . tamil-cdac)
 	("Telugu-CDAC" . telugu-cdac)
 	("Kannada-CDAC" . kannada-cdac)
 	("Malayalam-CDAC" . malayalam-cdac)
	("Devanagari-Akruti" . devanagari-akruti)
	("Bengali-Akruti" . bengali-akruti)
	("Punjabi-Akruti" . punjabi-akruti)
	("Gujarati-Akruti" . gujarati-akruti)
	("Oriya-Akruti" . oriya-akruti)
	("Tamil-Akruti" . tamil-akruti)
	("Telugu-Akruti" . telugu-akruti)
	("Kannada-Akruti" . kannada-akruti)
	("Malayalam-Akruti" . malayalam-akruti)
	("muleindian-2" . indian-2-column)
	("muleindian-1" . indian-1-column)
	("mulelao-1" . mule-lao)
	("muletibetan-2" . tibetan)
	("muletibetan-0" . tibetan)
	("muletibetan-1" . tibetan-1-column))))

(defvar font-encoding-charset-alist)

(setq font-encoding-charset-alist
      '((latin-iso8859-1 . iso-8859-1)
	(latin-iso8859-2 . iso-8859-2)
	(latin-iso8859-3 . iso-8859-3)
	(latin-iso8859-4 . iso-8859-4)
	(latin-iso8859-9 . iso-8859-9)
	(latin-iso8859-10 . iso-8859-10)
	(latin-iso8859-13 . iso-8859-13)
	(latin-iso8859-14 . iso-8859-14)
	(latin-iso8859-15 . iso-8859-15)
	(latin-iso8859-16 . iso-8859-16)
	(cyrillic-iso8859-5 . iso-8859-5)
	(greek-iso8859-7 . iso-8859-7)
	(arabic-iso8859-6 . iso-8859-6)
	(thai-tis620 . tis620-2533)
	(latin-jisx0201 . jisx0201)
	(katakana-jisx0201 . jisx0201)
	(chinese-big5-1 . big5)
	(chinese-big5-2 . big5)
	(vietnamese-viscii-lower . viscii)
	(vietnamese-viscii-upper . viscii)
	(tibetan . unicode-bmp)))

(setq script-representative-chars
      '((latin ?A ?Z ?a ?z #x00C0 #x0100 #x0180 #x1e00)
	(phonetic #x250 #x283)
	(greek #x3A9)
	(coptic #x3E2)
	(cyrillic #x42F)
	(armenian #x531)
	(hebrew #x5D0)
	(vai #xA500)
	(arabic #x628)
	(syriac #x710)
	(thaana #x78C)
	(devanagari #x915)
	(bengali #x995)
	(gurmukhi #xA15)
	(gujarati #xA95)
	(oriya #xB15)
	(tamil #xB95)
	(telugu #xC15)
	(kannada #xC95)
	(malayalam #xD15)
	(sinhala #xD95)
	(thai #xE17)
	(lao #xEA5)
	(tibetan #xF40)
	(burmese #x1000)
	(georgian #x10D3)
	(ethiopic #x1208)
	(cherokee #x13B6)
	(canadian-aboriginal #x14C0)
	(ogham #x168F)
	(runic #x16A0)
	(khmer #x1780)
	(mongolian #x1826)
	(symbol . [#x201C #x2200 #x2500])
	(braille #x2800)
	(ideographic-description #x2FF0)
	(cjk-misc #x300E)
	(kana #x304B)
	(bopomofo #x3105)
	(kanbun #x319D)
	(han #x5B57)
	(yi #xA288)
	(cham #xAA00)
	(tai-viet #xAA80)
	(hangul #xAC00)
	(linear-b #x10000)
	(aegean-number #x10100)
	(ancient-greek-number #x10140)
	(ancient-symbol #x10190)
	(phaistos-disc #x101D0)
	(lycian #x10280)
	(carian #x102A0)
	(old-italic #x10300)
	(ugaritic #x10380)
	(old-permic #x10350)
	(old-persian #x103A0)
	(deseret #x10400)
	(shavian #x10450)
	(osmanya #x10480)
	(elbasan #x10500)
	(caucasian-albanian #x10530)
	(linear-a #x10600)
	(cypriot-syllabary #x10800)
	(palmyrene #x10860)
	(nabataean #x10880)
	(phoenician #x10900)
	(lydian #x10920)
	(kharoshthi #x10A00)
	(manichaean #x10AC0)
	(mahajani #x11150)
	(sinhala-archaic-number #x111E1)
	(khojki #x11200)
	(khudawadi #x112B0)
	(grantha #x11305)
	(tirhuta #x11481)
	(siddham #x11580)
	(modi #x11600)
	(takri #x11680)
	(warang-citi #x118A1)
	(pau-cin-hau #x11AC0)
	(cuneiform #x12000)
	(cuneiform-numbers-and-punctuation #x12400)
	(mro #x16A40)
	(bassa-vah #x16AD0)
	(pahawh-hmong #x16B11)
	(duployan-shorthand #x1BC20)
	(byzantine-musical-symbol #x1D000)
	(musical-symbol #x1D100)
	(ancient-greek-musical-notation #x1D200)
	(tai-xuan-jing-symbol #x1D300)
	(counting-rod-numeral #x1D360)
	(mende-kikakui #x1E810)
	(mahjong-tile #x1F000)
	(domino-tile #x1F030)))

(defvar otf-script-alist)

;; The below was synchronized with the latest Jan 3, 2013 version of
;; https://www.microsoft.com/typography/otspec/scripttags.htm.
(setq otf-script-alist
      '((arab . arabic)
	(armi . aramaic)
	(armn . armenian)
	(avst . avestan)
	(bali . balinese)
	(bamu . bamum)
	(batk . batak)
	(bng2 . bengali)
	(beng . bengali)
	(bopo . bopomofo)
	(brai . braille)
	(brah . brahmi)
	(bugi . buginese)
	(buhd . buhid)
	(byzm . byzantine-musical-symbol)
	(cans . canadian-aboriginal)
	(cari . carian)
	(cakm . chakma)
	(cham . cham)
	(cher . cherokee)
	(copt . coptic)
	(xsux . cuneiform)
	(cprt . cypriot)
	(cyrl . cyrillic)
	(dsrt . deseret)
	(deva . devanagari)
	(dev2 . devanagari)
	(egyp . egyptian)
	(ethi . ethiopic)
	(geor . georgian)
	(glag . glagolitic)
	(goth . gothic)
	(grek . greek)
	(gujr . gujarati)
	(gjr2 . gujarati)
	(guru . gurmukhi)
	(gur2 . gurmukhi)
	(hani . han)
	(hang . hangul)
	(jamo . hangul)
	(hano . hanunoo)
	(hebr . hebrew)
	(phli . inscriptional-pahlavi)
	(prti . inscriptional-parthian)
	(java . javanese)
	(kthi . kaithi)
	(kana . kana)	; Hiragana
	(knda . kannada)
	(knd2 . kannada)
	(kali . kayah-li)
	(khar . kharoshthi)
	(khmr . khmer)
	(lao\  . lao)
	(latn . latin)
	(lepc . lepcha)
	(limb . limbu)
	(linb . linear_b)
        (lisu . lisu)
        (lyci . lycian)
        (lydi . lydian)
	(mlym . malayalam)
	(mlm2 . malayalam)
	(mand . mandaic)
	(math . mathematical)
	(mtei . meetei-mayek)
	(merc . meroitic)
	(mero . meroitic)
	(mong . mongolian)
	(musc . musical-symbol)
	(mym2 . burmese)
	(mymr . burmese)
	(nko\  . nko)
	(ogam . ogham)
	(olck . ol-chiki)
	(ital . old_italic)
	(xpeo . old_persian)
	(sarb . old-south-arabian)
	(orkh . old-turkic)
	(orya . oriya)
	(ory2 . oriya)
	(osma . osmanya)
	(phag . phags-pa)
	(phnx . phoenician)
	(rjng . rejang)
	(runr . runic)
	(samr . samaritan)
	(saur . saurashtra)
	(shrd . sharada)
	(shaw . shavian)
	(sinh . sinhala)
	(sora . sora-sompeng)
	(sund . sundanese)
	(sylo . syloti_nagri)
	(syrc . syriac)
	(tglg . tagalog)
	(tagb . tagbanwa)
	(tale . tai_le)
	(talu . tai-lue)
	(lana . tai-tham)
	(tavt . tai-viet)
	(takr . takri)
	(taml . tamil)
	(tml2 . tamil)
	(telu . telugu)
	(tel2 . telugu)
	(thaa . thaana)
	(thai . thai)
	(tibt . tibetan)
	(tfng . tifinagh)
	(ugar . ugaritic)
	(vai\  . vai)
	(yi\ \   . yi)))

;; Set standard fontname specification of characters in the default
;; fontset to find an appropriate font for each script/charset.  The
;; specification has the form ((SCRIPT FONT-SPEC ...) ...), where
;; FONT-SPEC is:
;;	a cons (FAMILY . REGISTRY),
;;	or a string FONT-NAME,
;;	or an object created by `font-spec'.
;;
;; FAMILY may be nil, in which case, the corresponding name of
;; default face is used.  If REGISTRY contains a character `-', the
;; string before that is embedded in `CHARSET_REGISTRY' field, and the
;; string after that is embedded in `CHARSET_ENCODING' field.  If it
;; does not contain `-', the whole string is embedded in
;; `CHARSET_REGISTRY' field, and a wild card character `*' is embedded
;; in `CHARSET_ENCODING' field.
;;
;; SCRIPT is a symbol that appears as an element of the char table
;; `char-script-table'.  SCRIPT may be a charset specifying the range
;; of characters.

(declare-function new-fontset "fontset.c" (name fontlist))
(declare-function set-fontset-font "fontset.c"
		  (name target font-spec &optional frame add))

(eval-when-compile

;; Build data to initialize the default fontset at compile time to
;; avoid loading charsets that won't be necessary at runtime.

;; The value is (CJK-REGISTRY-VECTOR TARGET-SPEC ...), where
;; CJK-REGISTRY-VECTOR is ["JISX0208.1983-0" "GB2312.1980-0" ...],
;; TARGET-SPEC is (TARGET . BITMASK) or (TARGET SPEC ...),
;; TARGET is CHAR or (FROM-CHAR . TO-CHAR),
;; BITMASK is a bitmask of indices to CJK-REGISTRY-VECTOR,
;; SPEC is a list of arguments to font-spec.

(defmacro build-default-fontset-data ()
  (let* (;;       CHARSET-REGISTRY  CHARSET            FROM-CODE TO-CODE
	 (cjk '(("JISX0208.1983-0" japanese-jisx0208  #x2121    #x287E)
		("GB2312.1980-0"   chinese-gb2312     #x2121    #x297E)
		("BIG5-0"          big5               #xA140    #xA3FE)
		("CNS11643.1992-1" chinese-cns11643-1 #x2121    #x427E)
		("KSC5601.1987-0"  korean-ksc5601     #x2121    #x2C7E)))
	 (scripts '((tibetan
		     (:registry "iso10646-1" :otf (tibt nil (ccmp blws abvs)))
		     (:family "mtib" :registry "iso10646-1")
		     (:registry "muletibetan-2"))
		    (ethiopic
		     (:registry "iso10646-1" :script ethiopic)
		     (:registry "ethiopic-unicode"))
		    (phonetic
		     (:registry "iso10646-1" :script phonetic)
		     (:registry "MuleIPA-1")
		     (:registry "iso10646-1"))))
	 (cjk-table (make-char-table nil))
	 (script-coverage
	  #'(lambda (script)
	      (let ((coverage))
		(map-char-table
		 #'(lambda (range val)
		     (when (eq val script)
		       (if (consp range)
			   (setq range (cons (car range) (cdr range))))
		       (push range coverage)))
		 char-script-table)
		coverage)))
	 (data (list (vconcat (mapcar 'car cjk))))
	 (i 0))
    (dolist (elt cjk)
      (let ((mask (lsh 1 i)))
	(map-charset-chars
	 #'(lambda (range _arg)
	     (let ((from (car range)) (to (cdr range)))
	       (if (< to #x110000)
		   (while (<= from to)
		     (or (memq (aref char-script-table from)
			       '(kana hangul han cjk-misc))
			 (aset cjk-table from
			       (logior (or (aref cjk-table from) 0) mask)))
		     (setq from (1+ from))))))
	 (nth 1 elt) nil (nth 2 elt) (nth 3 elt)))
      (setq i (1+ i)))
    (map-char-table
     #'(lambda (range val)
	 (if (consp range)
	     (setq range (cons (car range) (cdr range))))
	 (push (cons range val) data))
     cjk-table)
    (dolist (script scripts)
      (dolist (range (funcall script-coverage (car script)))
	(push (cons range (cdr script)) data)))
    `(quote ,(nreverse data))))
)

(defun setup-default-fontset ()
  "Setup the default fontset."
  (new-fontset
   "fontset-default"
   `(;; for each script
     (latin (nil . "ISO8859-1")
	    (nil . "ISO8859-2")
	    (nil . "ISO8859-3")
	    (nil . "ISO8859-4")
	    (nil . "ISO8859-9")
	    (nil . "ISO8859-10")
	    (nil . "ISO8859-13")
	    (nil . "ISO8859-14")
	    (nil . "ISO8859-15")
	    (nil . "ISO8859-16")
	    (nil . "VISCII1.1-1")
	    ,(font-spec :registry "iso10646-1" :script 'latin))

     (thai  ,(font-spec :registry "iso10646-1" :otf '(thai nil nil (mark)))
	    ,(font-spec :registry "iso10646-1" :script 'thai)
	    (nil . "TIS620*")
	    (nil . "ISO8859-11"))

     (devanagari ,(font-spec :registry "iso10646-1" :otf '(dev2 nil (rphf)))
                 ,(font-spec :registry "iso10646-1" :otf '(deva nil (rphf)))
		 (nil . "iso10646.indian-1"))
     (bengali ,(font-spec :registry "iso10646-1" :otf '(bng2 nil (rphf)))
              ,(font-spec :registry "iso10646-1" :otf '(beng nil (rphf))))
     (gurmukhi ,(font-spec :registry "iso10646-1" :otf '(gur2 nil (blwf)))
               ,(font-spec :registry "iso10646-1" :otf '(guru nil (blwf))))
     (gujarati ,(font-spec :registry "iso10646-1" :otf '(gjr2 nil (rphf)))
               ,(font-spec :registry "iso10646-1" :otf '(gujr nil (rphf))))
     (oriya ,(font-spec :registry "iso10646-1" :otf '(ory2 nil (rphf)))
            ,(font-spec :registry "iso10646-1" :otf '(orya nil (rphf))))
     (tamil ,(font-spec :registry "iso10646-1" :otf '(tml2 nil (akhn)))
            ,(font-spec :registry "iso10646-1" :otf '(taml nil (akhn))))
     (telugu ,(font-spec :registry "iso10646-1" :otf '(tel2 nil (blwf)))
             ,(font-spec :registry "iso10646-1" :otf '(telu nil (blwf))))
     (kannada ,(font-spec :registry "iso10646-1" :otf '(knd2 nil (rphf)))
              ,(font-spec :registry "iso10646-1" :otf '(knda nil (rphf))))
     (sinhala ,(font-spec :registry "iso10646-1" :otf '(sinh nil (akhn))))
     (malayalam ,(font-spec :registry "iso10646-1" :otf '(mlm2 nil (akhn)))
                ,(font-spec :registry "iso10646-1" :otf '(mlym nil (akhn))))

     (burmese ,(font-spec :registry "iso10646-1" :otf '(mym2 nil nil))
              ,(font-spec :registry "iso10646-1" :otf '(mymr nil nil))
	      ,(font-spec :registry "iso10646-1" :script 'burmese))

     (lao ,(font-spec :registry "iso10646-1" :otf '(lao\  nil nil (mark)))
	  ,(font-spec :registry "iso10646-1" :script 'lao)
	  (nil . "MuleLao-1"))

     (tai-viet ("TaiViet" . "iso10646-1"))

     (greek ,(font-spec :registry "iso10646-1" :script 'greek)
	    (nil . "ISO8859-7"))

     (cyrillic ,(font-spec :registry "iso10646-1" :script 'cyrillic)
	       (nil . "ISO8859-5")
	       (nil . "microsoft-cp1251")
	       (nil . "koi8-r"))

     (arabic ,(font-spec :registry "iso10646-1"
			 :otf '(arab nil (init medi fina liga)))
	     (nil . "MuleArabic-0")
	     (nil . "MuleArabic-1")
	     (nil . "MuleArabic-2")
	     (nil . "ISO8859-6"))

     (hebrew ,(font-spec :registry "iso10646-1" :script 'hebrew)
	     (nil . "ISO8859-8"))

     (khmer ,(font-spec :registry "iso10646-1" :otf '(khmr nil (pres))))

     (kana (nil . "JISX0208*")
	   (nil . "GB2312.1980-0")
	   (nil . "KSC5601.1987*")
	   (nil . "JISX0201*")
	   (nil . "JISX0213.2000-1")
	   (nil . "JISX0213.2004-1")
	   ,(font-spec :registry "iso10646-1" :script 'kana))

     (bopomofo ,(font-spec :registry "iso10646-1" :script 'bopomofo)
	       (nil . "sisheng_cwnn-0"))

     (han (nil . "GB2312.1980-0")
	  (nil . "JISX0208*")
	  (nil . "JISX0212*")
	  (nil . "big5*")
	  (nil . "KSC5601.1987*")
	  (nil . "CNS11643.1992-1")
	  (nil . "CNS11643.1992-2")
	  (nil . "CNS11643.1992-3")
	  (nil . "CNS11643.1992-4")
	  (nil . "CNS11643.1992-5")
	  (nil . "CNS11643.1992-6")
	  (nil . "CNS11643.1992-7")
	  (nil . "gbk-0")
	  (nil . "gb18030")
	  (nil . "JISX0213.2000-1")
	  (nil . "JISX0213.2000-2")
	  (nil . "JISX0213.2004-1")
	  ,(font-spec :registry "iso10646-1" :lang 'ja)
	  ,(font-spec :registry "iso10646-1" :lang 'zh))

     (cjk-misc (nil . "GB2312.1980-0")
	       (nil . "JISX0208*")
	       (nil . "JISX0212*")
	       (nil . "big5*")
	       (nil . "KSC5601.1987*")
	       (nil . "CNS11643.1992-1")
	       (nil . "CNS11643.1992-2")
	       (nil . "CNS11643.1992-3")
	       (nil . "CNS11643.1992-4")
	       (nil . "CNS11643.1992-5")
	       (nil . "CNS11643.1992-6")
	       (nil . "CNS11643.1992-7")
	       (nil . "gbk-0")
	       (nil . "gb18030")
	       (nil . "JISX0213.2000-1")
	       (nil . "JISX0213.2000-2")
	       ,(font-spec :registry "iso10646-1" :lang 'ja)
	       ,(font-spec :registry "iso10646-1" :lang 'zh))

     (hangul (nil . "KSC5601.1987-0")
	     ,(font-spec :registry "iso10646-1" :lang 'ko))

     ;; for each charset
     (ascii (nil . "ISO8859-1"))
     (arabic-digit ("*" . "MuleArabic-0"))
     (arabic-1-column ("*" . "MuleArabic-1"))
     (arabic-2-column ("*" . "MuleArabic-2"))
     (indian-is13194 (nil . "is13194-devanagari"))
     (indian-1-column ("*" . "muleindian-2"))
     ;; Indian CDAC
     (devanagari-cdac (nil . "Devanagari-CDAC"))
     (sanskrit-cdac (nil . "Sanskrit-CDAC"))
     (bengali-cdac (nil . "Bengali-CDAC"))
     (assamese-cdac (nil . "Assamese-CDAC"))
     (punjabi-cdac (nil . "Punjabi-CDAC"))
     (gujarati-cdac (nil . "Gujarati-CDAC"))
     (oriya-cdac (nil . "Oriya-CDAC"))
     (tamil-cdac (nil . "Tamil-CDAC"))
     (telugu-cdac (nil . "Telugu-CDAC"))
     (kannada-cdac (nil . "Kannada-CDAC"))
     (malayalam-cdac (nil . "Malayalam-CDAC"))
     ;; Indian AKRUTI
     (devanagari-akruti (nil . "Devanagari-Akruti"))
     (bengali-akruti (nil . "Bengali-Akruti"))
     (punjabi-akruti (nil . "Punjabi-Akruti"))
     (gujarati-akruti (nil . "Gujarati-Akruti"))
     (oriya-akruti (nil . "Oriya-Akruti"))
     (tamil-akruti (nil . "Tamil-Akruti"))
     (telugu-akruti (nil . "Telugu-Akruti"))
     (kannada-akruti (nil . "Kannada-Akruti"))
     (malayalam-akruti (nil . "Malayalam-Akruti"))

     ;; Fallback fonts
     (nil (nil . "gb2312.1980")
	  (nil . "gbk-0")
	  (nil . "gb18030")
	  (nil . "jisx0208")
	  (nil . "ksc5601.1987")
	  (nil . "CNS11643.1992-1")
	  (nil . "CNS11643.1992-2")
	  (nil . "CNS11643.1992-3")
	  (nil . "CNS11643.1992-4")
	  (nil . "CNS11643.1992-5")
	  (nil . "CNS11643.1992-6")
	  (nil . "CNS11643.1992-7")
	  (nil . "big5")
	  (nil . "jisx0213.2000-1")
	  (nil . "jisx0213.2004-1")
	  (nil . "jisx0212"))
     ))

  ;; For simple scripts
  (dolist (script '(phonetic
		    armenian
		    syriac
		    thaana
		    georgian
		    cherokee
		    canadian-aboriginal
		    ogham
		    runic
		    symbol
		    braille
		    yi
		    aegean-number
		    ancient-greek-number
		    ancient-symbol
		    phaistos-disc
		    lycian
		    carian
		    old-italic
		    ugaritic
		    old-persian
		    deseret
		    shavian
		    osmanya
		    cypriot-syllabary
		    phoenician
		    lydian
		    kharoshthi
		    cuneiform
		    cuneiform-numbers-and-punctuation
		    byzantine-musical-symbol
		    musical-symbol
		    ancient-greek-musical-notation
		    tai-xuan-jing-symbol
		    counting-rod-numeral
		    mahjong-tile
		    domino-tile))
    (set-fontset-font "fontset-default"
		      script (font-spec :registry "iso10646-1" :script script)
		      nil 'append))

  ;; Special settings for `MATHEMATICAL (U+1D400..U+1D7FF)'.
  (dolist (math-subgroup '((#x1D400 #x1D433 mathematical-bold)
			   (#x1D434 #x1D467 mathematical-italic)
			   (#x1D468 #x1D49B mathematical-bold-italic)
			   (#x1D49C #x1D4CF mathematical-script)
			   (#x1D4D0 #x1D503 mathematical-bold-script)
			   (#x1D504 #x1D537 mathematical-fraktur)
			   (#x1D538 #x1D56B mathematical-double-struck)
			   (#x1D56C #x1D59F mathematical-bold-fraktur)
			   (#x1D5A0 #x1D5D3 mathematical-sans-serif)
			   (#x1D5D4 #x1D607 mathematical-sans-serif-bold)
			   (#x1D608 #x1D63B mathematical-sans-serif-italic)
			   (#x1D63C #x1D66F mathematical-sans-serif-bold-italic)
			   (#x1D670 #x1D6A3 mathematical-monospace)
			   (#x1D6A4 #x1D6A5 mathematical-italic)
			   (#x1D6A8 #x1D6E1 mathematical-bold)
			   (#x1D6E2 #x1D71B mathematical-italic)
			   (#x1D71C #x1D755 mathematical-bold-italic)
			   (#x1D756 #x1D78F mathematical-sans-serif-bold)
			   (#x1D790 #x1D7C9 mathematical-sans-serif-bold-italic)
			   (#x1D7CA #x1D7D7 mathematical-bold)
			   (#x1D7D8 #x1D7E1 mathematical-double-struck)
			   (#x1D7E2 #x1D7EB mathematical-sans-serif)
			   (#x1D7EC #x1D7F5 mathematical-sans-serif-bold)
			   (#x1D7F6 #x1D7FF mathematical-monospace)))
    (let ((slot (assq (nth 2 math-subgroup) script-representative-chars)))
      (if slot
	  (if (vectorp (cdr slot))
	      (setcdr slot (vconcat (cdr slot) (vector (car math-subgroup))))
	    (setcdr slot (vector (cadr slot) (car math-subgroup))))
	(setq slot (list (nth 2 math-subgroup) (car math-subgroup)))
	(nconc script-representative-chars (list slot))))
    (set-fontset-font
     "fontset-default"
     (cons (car math-subgroup) (nth 1 math-subgroup))
     (font-spec :registry "iso10646-1" :script (nth 2 math-subgroup))))

  ;; Special setup for various symbols and punctuation characters
  ;; covered well by Symbola, excluding those covered well by popular
  ;; Unicode fonts.  We exclude the latter because users don't like us
  ;; invading on their font setups where they have good support from
  ;; other fonts.
  (dolist (symbol-subgroup
           '((#x2000 . #x2012)	;; General Punctuation
             (#x2015 . #x2017)
             #x201F
             (#x2023 . #x202F)
             (#x2031 . #x2038)
             (#x203B . #x206F)
             (#x2070 . #x209F)	;; Superscripts and Subscripts
             (#x20B6 . #x20CF)	;; Currency Symbols
             (#x2100 . #x2121)	;; Letterlike Symbols
             (#x2123 . #x214F)
             (#x2150 . #x215A)	;; Number Forms
             (#x215F . #x218F)
             (#x2194 . #x21FF)	;; Arrows
             (#x2200 . #x2211)	;; Mathematical Operators
             (#x2213 . #x2247)
             (#x2249 . #x225F)
             (#x2261 . #x2263)
             (#x2266 . #x22FF)
             (#x2300 . #x2301)	;; Miscellaneous Technical
             (#x2303 . #x230F)
             (#x2311 . #x231F)
             (#x2322 . #x23FF)
             (#x2400 . #x243F)	;; Control Pictures
             (#x2440 . #x245F)	;; Optical Char Recognition
             (#x2460 . #x24FF)	;; Enclosed Alphanumerics
             (#x25A0 . #x25FF)	;; Geometric Shapes
             (#x2600 . #x265F)	;; Miscellaneous Symbols
             (#x2661 . #x2662)
             #x2664
             (#x2667 . #x2669)
             (#x266C . #x26FF)
             (#x2700 . #x27bF)	;; Dingbats
             (#x27C0 . #x27EF)	;; Misc Mathematical Symbols-A
             (#x27F0 . #x27FF)	;; Supplemental Arrows-A
             (#x2900 . #x297F)	;; Supplemental Arrows-B
             (#x2980 . #x29FF)	;; Misc Mathematical Symbols-B
             (#x2A00 . #x2AFF)	;; Suppl. Math Operators
             (#x2B00 . #x2BFF)	;; Misc Symbols and Arrows
             (#x2E00 . #x2E7F)	;; Supplemental Punctuation
             (#x4DC0 . #x4DFF)	;; Yijing Hexagram Symbols
             (#xFE10 . #xFE1F)	;; Vertical Forms
             (#x10100 . #x1013F)	;; Aegean Numbers
             (#x102E0 . #x102FF)	;; Coptic Epact Numbers
             (#x1D000 . #x1D0FF)	;; Byzantine Musical Symbols
             (#x1D200 . #x1D24F)	;; Ancient Greek Musical Notation
             (#x1F0A0 . #x1F0FF)	;; Playing Cards
             (#x1F100 . #x1F1FF)	;; Enclosed Alphanumeric Suppl
             (#x1F300 . #x1F5FF)	;; Misc Symbols and Pictographs
             (#x1F600 . #x1F64F)	;; Emoticons
             (#x1F650 . #x1F67F)	;; Ornamental Dingbats
             (#x1F680 . #x1F6FF)	;; Transport and Map Symbols
             (#x1F700 . #x1F77F)	;; Alchemical Symbols
             (#x1F780 . #x1F7FF)	;; Geometric Shapes Extended
             (#x1F800 . #x1F8FF)))	;; Supplemental Arrows-C
    (set-fontset-font "fontset-default" symbol-subgroup "Symbola" nil 'prepend))
  ;; Box Drawing and Block Elements
  (set-fontset-font "fontset-default" '(#x2500 . #x259F)
                    "FreeMono" nil 'prepend)

  ;; Since standard-fontset-spec on X uses fixed-medium font, which
  ;; gets mapped to a iso8859-1 variant, we would like to prefer its
  ;; iso10646-1 variant for symbols, where the coverage is known to be
  ;; good.
  (dolist (symbol-subgroup
			 '((#x2000 . #x206F)   ;; General Punctuation
			   (#x2070 . #x209F)   ;; Superscripts and Subscripts
			   (#x20A0 . #x20CF)   ;; Currency Symbols
			   (#x2150 . #x218F)   ;; Number Forms
			   (#x2190 . #x21FF)   ;; Arrows
			   (#x2200 . #x22FF)   ;; Mathematical Operators
			   (#x2300 . #x23FF)   ;; Miscellaneous Technical
			   (#x2400 . #x243F)   ;; Control Pictures
			   (#x2440 . #x245F)   ;; Optical Char Recognition
			   (#x2460 . #x24FF)   ;; Enclosed Alphanumerics
                           (#x2500 . #x257F)   ;; Box Drawing
                           (#x2580 . #x259F)   ;; Block Elements
			   (#x25A0 . #x25FF)   ;; Geometric Shapes
			   (#x2600 . #x2689)   ;; Miscellaneous Symbols
			   (#x2700 . #x27bF)   ;; Dingbats
			   (#x27F5 . #x27FF))) ;; Supplemental Arrows-A
    (set-fontset-font "fontset-default" symbol-subgroup
                      "-*-fixed-medium-*-*-*-*-*-*-*-*-*-iso10646-1"
                      nil 'prepend))

  ;; Append CJK fonts for characters other than han, kana, cjk-misc.
  ;; Append fonts for scripts whose name is also a charset name.
  (let* ((data (build-default-fontset-data))
	 (registries (car data)))
    (dolist (target-spec (cdr data))
      (let ((target (car target-spec))
	    (spec (cdr target-spec)))
	(if (integerp spec)
	    (dotimes (i (length registries))
	      (if (> (logand spec (lsh 1 i)) 0)
		  (set-fontset-font "fontset-default" target
				    (cons nil (aref registries i))
				    nil 'append)))
	(dolist (args spec)
	  (set-fontset-font "fontset-default" target
			    (apply 'font-spec args) nil 'append))))))

  ;; Append Unicode fonts.
  ;; This may find fonts with more variants (bold, italic) but which
  ;; don't cover many characters.
  (set-fontset-font "fontset-default" nil
		    '(nil . "iso10646-1") nil 'prepend)
  ;; These may find fonts that cover many characters but with fewer
  ;; variants.
  (set-fontset-font "fontset-default" nil
		    '("gnu-unifont" . "iso10646-1") nil 'prepend)
  (set-fontset-font "fontset-default" nil
		    '("mutt-clearlyu" . "iso10646-1") nil 'prepend)
  (set-fontset-font "fontset-default" '(#x20000 . #x2FFFF)
		    '(nil . "unicode-sip"))

  (set-fontset-font "fontset-default" '(#xE000 . #xF8FF)
		    '(nil . "iso10646-1"))
  ;; Don't try the fallback fonts even if no suitable font was found
  ;; by the above font-spec.
  (set-fontset-font "fontset-default" '(#xE000 . #xF8FF) nil nil 'append))

(defun create-default-fontset ()
  "Create the default fontset.
Internal use only.  Should be called at startup time."
  (condition-case err
      (setup-default-fontset)
    (error (display-warning
	    'initialization
	    (format "Creation of the default fontsets failed: %s" err)
	    :error))))

;; These are the registered registries/encodings from
;; ftp://ftp.x.org/pub/DOCS/registry 2001/06/01

;; Name                                            Reference
;; ----                                            ---------
;; "DEC"                                           [27]
;;         registry prefix
;; "DEC.CNS11643.1986-2"                           [53]
;;         CNS11643 2-plane using the encoding
;;         suggested in that standard
;; "DEC.DTSCS.1990-2"                              [54]
;;         DEC Taiwan Supplemental Character Set
;; "fujitsu.u90x01.1991-0"                         [87]
;; "fujitsu.u90x03.1991-0"                         [87]
;; "GB2312.1980-0"                                 [39],[12]
;;         China (PRC) Hanzi, GL encoding
;; "GB2312.1980-1"                                 [39]
;;         (deprecated)
;;         China (PRC) Hanzi, GR encoding
;; "HP-Arabic8"                                    [36]
;;         HPARABIC8 8-bit character set
;; "HP-East8"                                      [36]
;;         HPEAST8 8-bit character set
;; "HP-Greek8"                                     [36]
;;         HPGREEK8 8-bit character set
;; "HP-Hebrew8"                                    [36]
;;         HPHEBREW8 8-bit character set
;; "HP-Japanese15"                                 [36]
;;         HPJAPAN15 15-bit character set,
;;         modified from industry de facto
;;         standard Shift-JIS
;; "HP-Kana8"                                      [36]
;;         HPKANA8 8-bit character set
;; "HP-Korean15"                                   [36]
;;         HPKOREAN15 15-bit character set
;; "HP-Roman8"                                     [36]
;;         HPROMAN8 8-bit character set
;; "HP-SChinese15"                                 [36]
;;         HPSCHINA15 15-bit character set for
;;         support of Simplified Chinese
;; "HP-TChinese15"                                 [36]
;;         HPTCHINA15 15-bit character set for
;;         support of Traditional Chinese
;; "HP-Turkish8"                                   [36]
;;         HPTURKISH8 8-bit character set
;; "IPSYS"                                         [59]
;;         registry prefix
;; "IPSYS.IE-1"                                    [59]
;; "ISO2022"<REG>"-"<ENC>                          [44]
;; "ISO646.1991-IRV"                               [107]
;;         ISO 646 International Reference Version
;; "ISO8859-1"                                     [15],[12]
;;         ISO Latin alphabet No. 1
;; "ISO8859-2"                                     [15],[12]
;;         ISO Latin alphabet No. 2
;; "ISO8859-3"                                     [15],[12]
;;         ISO Latin alphabet No. 3
;; "ISO8859-4"                                     [15],[12]
;;         ISO Latin alphabet No. 4
;; "ISO8859-5"                                     [15],[12]
;;         ISO Latin/Cyrillic alphabet
;; "ISO8859-6"                                     [15],[12]
;;         ISO Latin/Arabic alphabet
;; "ISO8859-7"                                     [15],[12]
;;         ISO Latin/Greek alphabet
;; "ISO8859-8"                                     [15],[12]
;;         ISO Latin/Hebrew alphabet
;; "ISO8859-9"                                     [15],[12]
;;         ISO Latin alphabet No. 5
;; "ISO8859-10"                                    [15],[12]
;;         ISO Latin alphabet No. 6
;; "ISO8859-13"                                    [15],[12]
;;         ISO Latin alphabet No. 7
;; "ISO8859-14"                                    [15],[12]
;;         ISO Latin alphabet No. 8
;; "ISO8859-15"                                    [15],[12]
;;         ISO Latin alphabet No. 9
;; "FCD8859-15"                                    [7]
;;         (deprecated)
;;         ISO Latin alphabet No. 9, Final Committee Draft
;; "ISO10646-1"                                    [133]
;;         Unicode Universal Multiple-Octet Coded Character Set
;; "ISO10646-MES"                                  [133]
;;         (deprecated)
;;         Unicode Minimum European Subset
;; "JISX0201.1976-0"                               [38],[12]
;;         8-Bit Alphanumeric-Katakana Code
;; "JISX0208.1983-0"                               [40],[12]
;;         Japanese Graphic Character Set,
;;         GL encoding
;; "JISX0208.1990-0"                               [71]
;;         Japanese Graphic Character Set,
;;         GL encoding
;; "JISX0208.1983-1"                               [40]
;;         (deprecated)
;;         Japanese Graphic Character Set,
;;         GR encoding
;; "JISX0212.1990-0"                               [72]
;;         Supplementary Japanese Graphic Character Set,
;;         GL encoding
;; "KOI8-R"                                        [119]
;;         Cyrillic alphabet
;; "KSC5601.1987-0"                                [41],[12]
;;         Korean Graphic Character Set,
;;         GL encoding
;; "KSC5601.1987-1"                                [41]
;;         (deprecated)
;;         Korean Graphic Character Set,
;;         GR encoding
;; "omron_CNS11643-0"                              [45]
;; "omron_CNS11643-1"                              [45]
;; "omron_BIG5-0"                                  [45]
;; "omron_BIG5-1"                                  [45]
;; "wn.tamil.1993"                                 [103]

(defun set-font-encoding (pattern charset)
  "Set arguments in `font-encoding-alist' (which see)."
  (let ((slot (assoc pattern font-encoding-alist)))
    (if slot
	(setcdr slot charset)
      (setq font-encoding-alist
	    (cons (cons pattern charset) font-encoding-alist)))))

(defvar x-pixel-size-width-font-regexp)
(defvar vertical-centering-font-regexp)

;; Setting for suppressing XLoadQueryFont on big fonts.
(setq x-pixel-size-width-font-regexp
      (purecopy "gb2312\\|gbk\\|gb18030\\|jisx0208\\|ksc5601\\|cns11643\\|big5"))

;; These fonts require vertical centering.
(setq vertical-centering-font-regexp
      (purecopy "gb2312\\|gbk\\|gb18030\\|jisx0208\\|jisx0212\\|ksc5601\\|cns11643\\|big5"))
(put 'vertical-centering-font-regexp 'standard-value
     (list vertical-centering-font-regexp))

;; CDAC fonts are actually smaller than their design sizes.
(setq face-font-rescale-alist
      (list (cons (purecopy "-cdac$")  1.3)))

(defvar x-font-name-charset-alist nil
  "This variable has no meaning now.  Just kept for backward compatibility.")

;;; XLFD (X Logical Font Description) format handler.

;; Define XLFD's field index numbers.		; field name
(defconst xlfd-regexp-family-subnum 0)		; FOUNDRY and FAMILY
(defconst xlfd-regexp-weight-subnum 1)		; WEIGHT_NAME
(defconst xlfd-regexp-slant-subnum 2)		; SLANT
(defconst xlfd-regexp-swidth-subnum 3)		; SETWIDTH_NAME
(defconst xlfd-regexp-adstyle-subnum 4)		; ADD_STYLE_NAME
(defconst xlfd-regexp-pixelsize-subnum 5)	; PIXEL_SIZE
(defconst xlfd-regexp-pointsize-subnum 6)	; POINT_SIZE
(defconst xlfd-regexp-resx-subnum 7)		; RESOLUTION_X
(defconst xlfd-regexp-resy-subnum 8)		; RESOLUTION_Y
(defconst xlfd-regexp-spacing-subnum 8)		; SPACING
(defconst xlfd-regexp-avgwidth-subnum 10)	; AVERAGE_WIDTH
(defconst xlfd-regexp-registry-subnum 11)	; REGISTRY and ENCODING

;; Regular expression matching against a fontname which conforms to
;; XLFD (X Logical Font Description).  All fields in XLFD should be
;; not be omitted (but can be a wild card) to be matched.
(defconst xlfd-tight-regexp
  "^\
-\\([^-]*-[^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)\
-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*-[^-]*\\)$")

;; Regular expression matching against a fontname which conforms to
;; XLFD (X Logical Font Description).  All fields in XLFD from FOUNDRY
;; to ADSTYLE, REGISTRY, and ENCODING should be not be omitted (but
;; can be a wild card) to be matched.
(defconst xlfd-style-regexp
  "^\
-\\([^-]*-[^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-\\([^-]*\\)-.*\
-\\([^-]*-[^-]*\\)$")

;; List of field numbers of XLFD whose values are numeric.
(defconst xlfd-regexp-numeric-subnums
  (list xlfd-regexp-pixelsize-subnum	;5
	xlfd-regexp-pointsize-subnum	;6
	xlfd-regexp-resx-subnum		;7
	xlfd-regexp-resy-subnum		;8
	xlfd-regexp-avgwidth-subnum	;10
	))

(defun x-decompose-font-name (pattern)
  "Decompose PATTERN into XLFD fields and return a vector of the fields.
The length of the vector is 12.
The FOUNDRY and FAMILY fields are concatenated and stored in the first
element of the vector.
The REGISTRY and ENCODING fields are concatenated and stored in the last
element of the vector.

Return nil if PATTERN doesn't conform to XLFD."
  (if (string-match xlfd-tight-regexp pattern)
      (let ((xlfd-fields (make-vector 12 nil)))
	(dotimes (i 12)
	  (aset xlfd-fields i (match-string (1+ i) pattern)))
	(dotimes (i 12)
	  (if (string-match "^[*-]+$" (aref xlfd-fields i))
	      (aset xlfd-fields i nil)))
	xlfd-fields)))

(defun x-compose-font-name (fields &optional _reduce)
  "Compose X fontname from FIELDS.
FIELDS is a vector of XLFD fields, of length 12.
If a field is nil, wild-card letter `*' is embedded.
Optional argument REDUCE exists just for backward compatibility,
and is always ignored."
  (concat "-" (mapconcat (lambda (x) (or x "*")) fields "-")))


(defun x-must-resolve-font-name (xlfd-fields)
  "Like `x-resolve-font-name', but always return a font name.
XLFD-FIELDS is a vector of XLFD (X Logical Font Description) fields.
If no font matching XLFD-FIELDS is available, successively replace
parts of the font name pattern with \"*\" until some font is found.
Value is name of that font."
  (let ((ascii-font nil) (index 0))
    (while (and (null ascii-font) (<= index xlfd-regexp-registry-subnum))
      (let ((pattern (x-compose-font-name xlfd-fields)))
	(condition-case nil
	    (setq ascii-font (x-resolve-font-name pattern))
	  (error
	   (message "Warning: no fonts matching `%s' available" pattern)
	   (aset xlfd-fields index "*")
	   (setq index (1+ index))))))
    (unless ascii-font
      (error "No fonts found"))
    ascii-font))


(defun x-complement-fontset-spec (default-spec fontlist)
  "Complement elements of FONTLIST based on DEFAULT-SPEC.
DEFAULT-SPEC is a font-spec object providing default font properties.
FONTLIST is an alist of script names vs the corresponding font names.

The font names are parsed and unspecified font properties are
given from DEFAULT-SPEC."
  (let ((prop-list '(:foundry :family :weight :slant :width :adstyle :size)))
    (dolist (elt fontlist)
      (let ((spec (font-spec :name (cadr elt))))
	(dolist (prop prop-list)
	  (let ((val (font-get spec prop)))
	    (or val
		(font-put spec prop (font-get default-spec prop)))))
	(setcar (cdr elt) spec)))
    fontlist))

(defun fontset-name-p (fontset)
  "Return non-nil if FONTSET is valid as fontset name.
A valid fontset name should conform to XLFD (X Logical Font Description)
with \"fontset\" in `<CHARSET_REGISTRY>' field."
  (and (string-match xlfd-tight-regexp fontset)
       (string= (match-string (1+ xlfd-regexp-registry-subnum) fontset)
		"fontset")))

(declare-function fontset-list "fontset.c" ())

(defun generate-fontset-menu ()
  "Return list to be appended to `x-fixed-font-alist'.
Done when `mouse-set-font' is called."
  (let (l)
    (dolist (fontset (fontset-list))
      (or (string-match "fontset-default$" fontset)
	  (string-match "fontset-auto[0-9]+$" fontset)
	  (push (list (fontset-plain-name fontset) fontset) l)))
    (cons "Fontset"
	  (sort l #'(lambda (x y) (string< (car x) (car y)))))))

(declare-function query-fontset "fontset.c" (pattern &optional regexpp))

(defun fontset-plain-name (fontset)
  "Return a plain and descriptive name of FONTSET."
  (if (not (setq fontset (query-fontset fontset)))
      (error "Invalid fontset: %s" fontset))
  (let ((xlfd-fields (x-decompose-font-name fontset)))
    (if xlfd-fields
	(let ((family (aref xlfd-fields xlfd-regexp-family-subnum))
	      (weight (aref xlfd-fields xlfd-regexp-weight-subnum))
	      (slant  (aref xlfd-fields xlfd-regexp-slant-subnum))
	      ;(swidth (aref xlfd-fields xlfd-regexp-swidth-subnum))
	      (size   (aref xlfd-fields xlfd-regexp-pixelsize-subnum))
	      (nickname (aref xlfd-fields xlfd-regexp-registry-subnum))
	      name)
	  (if (not (string-match "^fontset-\\(.*\\)$" nickname))
	      (setq nickname family)
	    (setq nickname (match-string 1 nickname)))
	  (if (and size (> (string-to-number size) 0))
	      (setq name (format "%s: %s-dot" nickname size))
	    (setq name nickname))
	  (and weight
	       (cond ((string-match "^medium$" weight)
		      (setq name (concat name " " "medium")))
		     ((string-match "^bold$\\|^demibold$" weight)
		      (setq name (concat name " " weight)))))
	  (and slant
	       (cond ((string-match "^i$" slant)
		      (setq name (concat name " " "italic")))
		     ((string-match "^o$" slant)
		      (setq name (concat name " " "slant")))
		     ((string-match "^ri$" slant)
		      (setq name (concat name " " "reverse italic")))
		     ((string-match "^ro$" slant)
		      (setq name (concat name " " "reverse slant")))))
	  name)
      fontset)))

(defvar charset-script-alist
  '((ascii . latin)
    (latin-iso8859-1 . latin)
    (latin-iso8859-2 . latin)
    (latin-iso8859-3 . latin)
    (latin-iso8859-4 . latin)
    (latin-iso8859-9 . latin)
    (latin-iso8859-10 . latin)
    (latin-iso8859-13 . latin)
    (latin-iso8859-14 . latin)
    (latin-iso8859-15 . latin)
    (latin-iso8859-16 . latin)
    (latin-jisx0201 . latin)
    (thai-tis620 . thai)
    (cyrillic-iso8859-5 . cyrillic)
    (arabic-iso8859-6 . arabic)
    (greek-iso8859-7 . latin)
    (hebrew-iso8859-8 . latin)
    (katakana-jisx0201 . kana)
    (chinese-gb2312 . han)
    (chinese-gbk . han)
    (gb18030-2-byte . han)
    (gb18030-4-byte-bmp . han)
    (gb18030-4-byte-ext-1 . han)
    (gb18030-4-byte-ext-2 . han)
    (gb18030-4-byte-smp . han)
    (chinese-big5-1 . han)
    (chinese-big5-2 . han)
    (chinese-cns11643-1 . han)
    (chinese-cns11643-2 . han)
    (chinese-cns11643-3 . han)
    (chinese-cns11643-4 . han)
    (chinese-cns11643-5 . han)
    (chinese-cns11643-6 . han)
    (chinese-cns11643-7 . han)
    (japanese-jisx0208 . han)
    (japanese-jisx0208-1978 . han)
    (japanese-jisx0212 . han)
    (japanese-jisx0213-1 . han)
    (japanese-jisx0213-2 . han)
    (korean-ksc5601 . hangul)
    (chinese-sisheng . bopomofo)
    (vietnamese-viscii-lower . latin)
    (vietnamese-viscii-upper . latin)
    (arabic-digit . arabic)
    (arabic-1-column . arabic)
    (arabic-2-column . arabic)
    (indian-is13194 . devanagari)
    (indian-glyph . devanagari)
    (indian-1-column . devanagari)
    (indian-2-column . devanagari)
    (tibetan-1-column . tibetan))
  "Alist of charsets vs the corresponding most appropriate scripts.

This alist is used by the function `create-fontset-from-fontset-spec'
to map charsets to scripts.")

(defun create-fontset-from-fontset-spec (fontset-spec
					 &optional _style-variant _noerror)
  "Create a fontset from fontset specification string FONTSET-SPEC.
FONTSET-SPEC is a string of the format:
	FONTSET-NAME,SCRIPT-NAME0:FONT-NAME0,SCRIPT-NAME1:FONT-NAME1, ...
Any number of SPACE, TAB, and NEWLINE can be put before and after commas.

When a frame uses the fontset as the `font' parameter, the frame's
default font name is derived from FONTSET-NAME by substituting
\"iso8859-1\" for the tail part \"fontset-XXX\".  But, if SCRIPT-NAMEn
is \"ascii\", use the corresponding FONT-NAMEn as the default font
name.

Optional 2nd and 3rd arguments exist just for backward compatibility,
and are ignored.

It returns a name of the created fontset.

For backward compatibility, SCRIPT-NAME may be a charset name, in
which case, the corresponding script is decided by the variable
`charset-script-alist' (which see)."
  (or (string-match "^[^,]+" fontset-spec)
      (error "Invalid fontset spec: %s" fontset-spec))
  (let ((idx (match-end 0))
	(name (match-string 0 fontset-spec))
	default-spec target script fontlist)
    (or (string-match xlfd-tight-regexp name)
	(error "Fontset name \"%s\" not conforming to XLFD" name))
    (setq default-spec (font-spec :name name))
    ;; At first, extract pairs of charset and fontname from FONTSET-SPEC.
    (while (string-match "[, \t\n]*\\([^:]+\\):[ \t]*\\([^,]+\\)"
			 fontset-spec idx)
      (setq idx (match-end 0))
      (setq target (intern (match-string 1 fontset-spec)))
      (cond ((or (eq target 'ascii)
		 (memq target (char-table-extra-slot char-script-table 0)))
	     (push (list target (match-string 2 fontset-spec)) fontlist))
	    ((setq script (cdr (assq target charset-script-alist)))
	     (push (list script (match-string 2 fontset-spec)) fontlist))
	    ((charsetp target)
	     (push (list target (match-string 2 fontset-spec)) fontlist))))

    ;; Complement FONTLIST.
    (setq fontlist (x-complement-fontset-spec default-spec fontlist))

    ;; Create a fontset.
    (new-fontset name (nreverse fontlist))))

(defun create-fontset-from-ascii-font (font &optional resolved-font
					    fontset-name)
  "Create a fontset from an ASCII font FONT.

Optional 2nd arg RESOLVED-FONT is a resolved name of FONT.
If omitted, `x-resolve-font-name' is called to get the resolved name.
At this time, if FONT is not available, an error is signaled.

Optional 3rd arg FONTSET-NAME is a string to be used in
`<CHARSET_ENCODING>' fields of a new fontset name.  If it is omitted,
an appropriate name is generated automatically.

It returns a name of the created fontset."
  (setq font (downcase font))
  (setq resolved-font
	(downcase (or resolved-font (x-resolve-font-name font))))
  (let ((xlfd (x-decompose-font-name resolved-font))
	fontset)
    (if fontset-name
	(setq fontset-name (downcase fontset-name))
      (if (query-fontset "fontset-startup")
	  (setq fontset-name
		(subst-char-in-string
		 ?- ?_ (aref xlfd xlfd-regexp-registry-subnum) t))
	(setq fontset-name "startup")))
    (aset xlfd xlfd-regexp-registry-subnum
	  (format "fontset-%s" fontset-name))
    (setq fontset (x-compose-font-name xlfd))
    (or (query-fontset fontset)
	(create-fontset-from-fontset-spec (concat fontset ", ascii:" font)))))


;; Create standard fontset from 16 dots fonts which are the most widely
;; installed fonts.  Fonts for Chinese-GB, Korean, and Chinese-CNS are
;; specified here because FAMILY of those fonts are not "fixed" in
;; many cases.
(defvar standard-fontset-spec
  (purecopy "-*-fixed-medium-r-normal-*-16-*-*-*-*-*-fontset-standard")
  "String of fontset spec of the standard fontset.
You have the biggest chance to display international characters
with correct glyphs by using the standard fontset.
See the documentation of `create-fontset-from-fontset-spec' for the format.")


;; Create fontsets from X resources of the name `fontset-N (class
;; Fontset-N)' where N is integer 0, 1, ...
;; The values of the resources the string of the same format as
;; `standard-fontset-spec'.

(declare-function x-get-resource "frame.c"
		  (attribute class &optional component subclass))

(defun create-fontset-from-x-resource ()
  (let ((idx 0)
	fontset-spec)
    (while (setq fontset-spec (x-get-resource (format "fontset-%d" idx)
					      (format "Fontset-%d" idx)))
      (condition-case nil
	  (create-fontset-from-fontset-spec fontset-spec t)
	(error (display-warning
		'initialization
		(format "Fontset-%d: invalid specification in X resource" idx)
		:warning)))
      (setq idx (1+ idx)))))

;;
(provide 'fontset)

;;; fontset.el ends here