This file is indexed.

/usr/lib/ruby/2.3.0/tk/font.rb is in ruby2.3-tcltk 2.3.0-5ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
# frozen_string_literal: false
#
#  tk/font.rb - the class to treat fonts on Ruby/Tk
#
#                               by  Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
require 'tk'

class TkFont
  include Tk
  extend TkCore

  TkCommandNames = ['font'.freeze].freeze

  (Tk_FontID = ["@font".freeze, TkUtil.untrust("00000")]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }

  Tk_FontNameTBL = TkCore::INTERP.create_table
  Tk_FontUseTBL  = TkCore::INTERP.create_table

  TkCore::INTERP.init_ip_env{
    Tk_FontNameTBL.mutex.synchronize{ Tk_FontNameTBL.clear }
    Tk_FontUseTBL.mutex.synchronize{ Tk_FontUseTBL.clear }
  }

  # option_type : default => string
  OptionType = Hash.new(?s)
  OptionType['size'] = ?n
  OptionType['pointadjust'] = ?n
  OptionType['underline'] = ?b
  OptionType['overstrike'] = ?b

  # metric_type : default => num_or_str
  MetricType = Hash.new(?n)
  MetricType['fixed'] = ?b

  # system font names
  SYSTEM_FONT_NAMES = []
  def SYSTEM_FONT_NAMES.add(font_names)
    (@mutex ||= Mutex.new).synchronize{
      self.replace(self | font_names.map{|name| name.to_s})
    }
  end
  def SYSTEM_FONT_NAMES.include?(name)
    (@mutex ||= Mutex.new).synchronize{
      super(name.to_s)
    }
  end

  # set default font
  case Tk::TK_VERSION
  when /^4\..*/
    DEFAULT_LATIN_FONT_NAME = 'a14'.freeze
    DEFAULT_KANJI_FONT_NAME = 'k14'.freeze

  when /^8\.[0-4]/
    if JAPANIZED_TK
      begin
        fontnames = tk_call('font', 'names')
        case fontnames
        when /defaultgui/
          # Tcl/Tk-JP for Windows
          ltn = 'defaultgui'
          knj = 'defaultgui'
        when /Mincho:Helvetica-Bold-12/
          # Tcl/Tk-JP for UNIX/X
          ltn, knj = tk_split_simplelist(tk_call('font', 'configure',
                                                 'Mincho:Helvetica-Bold-12',
                                                 '-compound'))
        else
          # unknown Tcl/Tk-JP
          #platform = tk_call('set', 'tcl_platform(platform)')
          platform = Tk::PLATFORM['platform']
          case platform
          when 'unix'
            ltn = {'family'=>'Helvetica'.freeze,
                   'size'=>-12, 'weight'=>'bold'.freeze}
            #knj = 'k14'
            #knj = '-misc-fixed-medium-r-normal--14-*-*-*-c-*-jisx0208.1983-0'
            knj = '-*-fixed-bold-r-normal--12-*-*-*-c-*-jisx0208.1983-0'
          when 'windows'
            ltn = {'family'=>'MS Sans Serif'.freeze, 'size'=>8}
            knj = 'mincho'
          when 'macintosh'
            ltn = 'system'
            knj = 'mincho'
          else # unknown
            ltn = 'Helvetica'
            knj = 'mincho'
          end
        end
      rescue
        ltn = 'Helvetica'
        knj = 'mincho'
      end

    else # not JAPANIZED_TK
      begin
        #platform = tk_call('set', 'tcl_platform(platform)')
        platform = Tk::PLATFORM['platform']
        case platform
        when 'unix'
          ltn = {'family'=>'Helvetica'.freeze,
                 'size'=>-12, 'weight'=>'bold'.freeze}
        when 'windows'
          ltn = {'family'=>'MS Sans Serif'.freeze, 'size'=>8}
        when 'macintosh'
          ltn = 'system'
        else # unknown
          ltn = 'Helvetica'
        end
      rescue
        ltn = 'Helvetica'
      end

      knj = ltn.dup
    end

    DEFAULT_LATIN_FONT_NAME = ltn.freeze
    DEFAULT_KANJI_FONT_NAME = knj.freeze

  when /^8\.[5-9]/, /^9\..*/
    if tk_call('font', 'names') =~ /\bTkDefaultFont\b/
      DEFAULT_LATIN_FONT_NAME = 'TkDefaultFont'.freeze
      DEFAULT_KANJI_FONT_NAME = 'TkDefaultFont'.freeze
    else
      DEFAULT_LATIN_FONT_NAME = 'Helvetica'.freeze
      DEFAULT_KANJI_FONT_NAME = 'mincho'.freeze
    end

  else # unknown version
    DEFAULT_LATIN_FONT_NAME = 'Helvetica'.freeze
    DEFAULT_KANJI_FONT_NAME = 'mincho'.freeze

  end

  if $DEBUG
    print "default latin font = "; p DEFAULT_LATIN_FONT_NAME
    print "default kanji font = "; p DEFAULT_KANJI_FONT_NAME
  end


  ###################################
  class DescendantFont
    def initialize(compound, type)
      unless compound.kind_of?(TkFont)
        fail ArgumentError, "a TkFont object is expected for the 1st argument"
      end

      @compound = compound
      case type
      when 'kanji', 'latin', 'ascii'
        @type = type
      when :kanji, :latin, :ascii
        @type = type.to_s
      else
        fail ArgumentError, "unknown type '#{type}'"
      end
    end

    def dup
      fail RuntimeError, "cannot dupulicate a descendant font"
    end
    def clone
      fail RuntimeError, "cannot clone a descendant font"
    end

    def to_eval
      @compound.__send__(@type + '_font_id')
    end
    def font
      @compound.__send__(@type + '_font_id')
    end
    alias font_id font
    alias name font
    alias to_s font

    def [](slot)
      @compound.__send__(@type + '_configinfo', slot)
    end
    def []=(slot, value)
      @compound.__send__(@type + '_configure', slot, value)
      value
    end

    def method_missing(id, *args)
      @compound.__send__(@type + '_' + id.id2name, *args)
    end
  end


  ###################################
  # class methods
  ###################################
  def TkFont.is_system_font?(fnt)
    # true  --> system font which is available on the current system
    # false --> not system font (or unknown system font)
    # nil   --> system font name, but not available on the current system
    fnt = fnt.to_s
    SYSTEM_FONT_NAMES.include?(fnt) && self.names.index(fnt) && true
  end

  def TkFont.actual(fnt, option=nil)
    fnt = '{}' if fnt == ''
    if fnt.kind_of?(TkFont)
     fnt.actual(option)
    else
      actual_core(fnt, nil, option)
    end
  end
  def TkFont.actual_hash(fnt, option=nil)
    Hash[TkFont.actual(fnt, option)]
  end

  def TkFont.actual_displayof(fnt, win, option=nil)
    fnt = '{}' if fnt == ''
    if fnt.kind_of?(TkFont)
     fnt.actual_displayof(win, option)
    else
      win = '.' unless win
      actual_core(fnt, win, option)
    end
  end
  def TkFont.actual_hash_displayof(fnt, option=nil)
    Hash[TkFont.actual_displayof(fnt, option)]
  end

  def TkFont.configure(fnt, slot, value=None)
    if fnt.kind_of?(TkFont)
      fnt.configure(fnt, slot, value)
    else
      configure_core(fnt, slot, value)
    end
    fnt
  end

  def TkFont.configinfo(fnt, slot=nil)
    if fnt.kind_of?(TkFont)
      fnt.configinfo(fnt, slot)
    else
      configinfo_core(fnt, slot)
    end
  end

  def TkFont.current_configinfo(fnt, slot=nil)
    if fnt.kind_of?(TkFont)
      fnt.current_configinfo(fnt, slot)
    else
      current_configinfo_core(fnt, slot)
    end
  end

  def TkFont.measure(fnt, text)
    fnt = '{}' if fnt == ''
    if fnt.kind_of?(TkFont)
      fnt.measure(text)
    else
      measure_core(fnt, nil, text)
    end
  end

  def TkFont.measure_displayof(fnt, win, text)
    fnt = '{}' if fnt == ''
    if fnt.kind_of?(TkFont)
      fnt.measure_displayof(win, text)
    else
      win = '.' unless win
      measure_core(fnt, win, text)
    end
  end

  def TkFont.metrics(fnt, option=nil)
    fnt = '{}' if fnt == ''
    if fnt.kind_of?(TkFont)
      fnt.metrics(option)
    else
      metrics_core(fnt, nil, option)
    end
  end
  def TkFont.metrics_hash(fnt, option=nil)
    if option
      val = TkFont.metrics(fnt, option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[TkFont.metrics(fnt)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def TkFont.metrics_displayof(fnt, win, option=nil)
    fnt = '{}' if fnt == ''
    if fnt.kind_of?(TkFont)
      font.metrics_displayof(win, option=nil)
    else
      win = '.' unless win
      metrics_core(fnt, win, option)
    end
  end
  def TkFont.metrics_hash_displayof(fnt, win, option=nil)
    if option
      val = TkFont.metrics_displayof(fnt, win, option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[TkFont.metrics_displayof(fnt, win, option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def TkFont.families(win=nil)
    case (Tk::TK_VERSION)
    when /^4\..*/
      ['fixed']

    when /^8\..*/
      if win
        tk_split_simplelist(tk_call('font', 'families', '-displayof', win))
      else
        tk_split_simplelist(tk_call('font', 'families'))
      end
    end
  end

  def TkFont.names
    case (Tk::TK_VERSION)
    when /^4\..*/
      r = ['fixed']
      r += ['a14', 'k14'] if JAPANIZED_TK
      Tk_FontNameTBL.mutex.synchronize{
        Tk_FontNameTBL.each_value{|obj| r.push(obj)}
      }
      #r | []
      r.uniq

    when /^8\..*/
      tk_split_simplelist(tk_call('font', 'names'))

    end
  end

  def TkFont.create_copy(font)
    fail 'source-font must be a TkFont object' unless font.kind_of? TkFont
    if TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      keys = {}
      font.configinfo.each{|key,value| keys[key] = value }
      TkFont.new(font.latin_font_id, font.kanji_font_id, keys)
    else # ! TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      TkFont.new(font.latin_font_id, font.kanji_font_id, font.configinfo)
    end
  end

  def TkFont.get_obj(name)
    name = name.to_s
    if name =~ /^(@font[0-9]+)(|c|l|k)$/
      Tk_FontNameTBL.mutex.synchronize{
        Tk_FontNameTBL[$1]
      }
    else
      Tk_FontNameTBL.mutex.synchronize{
        Tk_FontNameTBL[name]
      }
    end
  end

  def TkFont.init_widget_font(pathname, *args)
    win, tag, key = pathname.split(';')
    key = 'font' if key == nil || key == ''
    path = [win, tag, key].join(';')

    case (Tk::TK_VERSION)
    when /^4\..*/
      regexp = /^-(|kanji)#{key} /

      conf_list = tk_split_simplelist(tk_call(*args)).
        find_all{|prop| prop =~ regexp}.
        collect{|prop| tk_split_simplelist(prop)}

      if conf_list.size == 0
        raise RuntimeError, "the widget may not support 'font' option"
      end

      args << {}

      ltn_key = "-#{key}"
      knj_key = "-kanji#{key}"

      ltn_info = conf_list.find{|conf| conf[0] == ltn_key}
      ltn = ltn_info[-1]
      ltn = nil if ltn == [] || ltn == ""

      knj_info = conf_list.find{|conf| conf[0] == knj_key}
      knj = knj_info[-1]
      knj = nil if knj == [] || knj == ""

      TkFont.new(ltn, knj).call_font_configure([path, key], *args)

    when /^8\.[0-4]/
      regexp = /^-#{key} /

      conf_list = tk_split_simplelist(tk_call(*args)).
        find_all{|prop| prop =~ regexp}.
        collect{|prop| tk_split_simplelist(prop)}

      if conf_list.size == 0
        raise RuntimeError, "the widget may not support 'font' option"
      end

      args << {}

      optkey = "-#{key}"

      info = conf_list.find{|conf| conf[0] == optkey}
      fnt = info[-1]
      fnt = nil if fnt == [] || fnt == ""

      unless fnt
        # create dummy
        # TkFont.new(nil, nil).call_font_configure([path, key], *args)
        dummy_fnt = TkFont.allocate
        dummy_fnt.instance_eval{ init_dummy_fontobj() }
        dummy_fnt
      else
        begin
          compound = tk_split_simplelist(
              Hash[*tk_split_simplelist(tk_call('font', 'configure',
                                                fnt))].collect{|k,v|
                [k[1..-1], v]
              }.assoc('compound')[1])
        rescue
          compound = []
        end
        if compound == []
          if TkFont.is_system_font?(fnt)
            TkNamedFont.new(fnt).call_font_configure([path, key], *args)
          else
            TkFont.new(fnt).call_font_configure([path, key], *args)
          end
        else
          TkFont.new(compound[0],
                     compound[1]).call_font_configure([path, key], *args)
        end
      end

    when /^8\.[5-9]/, /^9\..*/
      regexp = /^-#{key} /

      conf_list = tk_split_simplelist(tk_call(*args)).
        find_all{|prop| prop =~ regexp}.
        collect{|prop| tk_split_simplelist(prop)}

      if conf_list.size == 0
        raise RuntimeError, "the widget may not support 'font' option"
      end

      args << {}

      optkey = "-#{key}"

      info = conf_list.find{|conf| conf[0] == optkey}
      fnt = info[-1]
      fnt = nil if fnt == [] || fnt == ""

      unless fnt
        # create dummy
        # TkFont.new(nil, nil).call_font_configure([path, key], *args)
        dummy_fnt = TkFont.allocate
        dummy_fnt.instance_eval{ init_dummy_fontobj() }
        dummy_fnt
      else
        if TkFont.is_system_font?(fnt)
          TkNamedFont.new(fnt).call_font_configure([path, key], *args)
        else
          TkFont.new(fnt).call_font_configure([path, key], *args)
        end
      end
    end
  end

  def TkFont.used_on(path=nil)
    Tk_FontUseTBL.mutex.synchronize{
      if path
        Tk_FontUseTBL[path]
      else
        # Tk_FontUseTBL.values | []
        Tk_FontUseTBL.values.uniq
      end
    }
  end

  def TkFont.failsafe(font)
    begin
      if /^8\..*/ === Tk::TK_VERSION  && JAPANIZED_TK
        tk_call('font', 'failsafe', font)
      end
    rescue
    end
  end

  ###################################
  # instance methods
  ###################################
  private
  ###################################
  def init_dummy_fontobj
    Tk_FontID.mutex.synchronize{
      @id = Tk_FontID.join(TkCore::INTERP._ip_id_)
      Tk_FontID[1].succ!
    }
    Tk_FontNameTBL.mutex.synchronize{
      Tk_FontNameTBL[@id] = self
    }

    # @latin_desscendant = nil
    # @kanji_desscendant = nil
    @descendant = [nil, nil] # [latin, kanji]

    case (Tk::TK_VERSION)
    when /^4\..*/
      @latinfont = ""
      @kanjifont = ""
      if JAPANIZED_TK
        @compoundfont = [[@latinfont], [@kanjifont]]
        @fontslot = {'font'=>@latinfont, 'kanjifont'=>@kanjifont}
      else
        @compoundfont = @latinfont
        @fontslot = {'font'=>@latinfont}
      end
    else
      @latinfont = @id + 'l'
      @kanjifont = @id + 'k'
      @compoundfont = @id + 'c'

      if JAPANIZED_TK
        tk_call('font', 'create', @latinfont, '-charset', 'iso8859')
        tk_call('font', 'create', @kanjifont, '-charset', 'jisx0208.1983')
        tk_call('font', 'create', @compoundfont,
                '-compound', [@latinfont, @kanjifont])
      else
        tk_call('font', 'create', @latinfont)
        tk_call('font', 'create', @kanjifont)
        tk_call('font', 'create', @compoundfont)
      end

      @fontslot = {'font'=>@compoundfont}
    end

    self
  end

  def initialize(ltn=nil, knj=nil, keys=nil)
    ltn = '{}' if ltn == ''
    knj = '{}' if knj == ''

    Tk_FontID.mutex.synchronize{
      # @id = Tk_FontID.join('')
      @id = Tk_FontID.join(TkCore::INTERP._ip_id_)
      Tk_FontID[1].succ!
    }
    Tk_FontNameTBL.mutex.synchronize{
      Tk_FontNameTBL[@id] = self
    }

    # @latin_desscendant = nil
    # @kanji_desscendant = nil
    @descendant = [nil, nil] # [latin, kanji]

    # @latinfont = @id + 'l'
    # @kanjifont = @id + 'k'
    # @compoundfont = @id + 'c'
    # @fontslot = {}

    if knj.kind_of?(Hash) && !keys
      keys = knj
      knj = nil
    end

    # compound font check
    if Tk::TK_VERSION == '8.0' && JAPANIZED_TK
      begin
        compound = tk_split_simplelist(tk_call('font', 'configure',
                                               ltn, '-compound'))
        if knj == nil
          if compound != []
            ltn, knj = compound
          end
        else
          if compound != []
            ltn = compound[0]
          end
          compound = tk_split_simplelist(tk_call('font', 'configure',
                                                 knj, '-compound'))
          if compound != []
            knj = compound[1]
          end
        end
      rescue
      end
    end

    if ltn
      if JAPANIZED_TK && !knj
        if Tk::TK_VERSION =~ /^4..*/
          knj = DEFAULT_KANJI_FONT_NAME
        else
          knj = ltn
        end
      end
    else
      ltn = DEFAULT_LATIN_FONT_NAME
      knj = DEFAULT_KANJI_FONT_NAME if JAPANIZED_TK && !knj
    end

    create_compoundfont(ltn, knj, keys)
  end

  def initialize_copy(font)
    unless font.kind_of?(TkFont)
      fail TypeError, '"initialize_copy should take same class object'
    end
    if TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      keys = {}
      font.configinfo.each{|key,value| keys[key] = value }
      initialize(font.latin_font_id, font.kanji_font_id, keys)
    else # ! TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      initialize(font.latin_font_id, font.kanji_font_id, font.configinfo)
    end
  end

  def _get_font_info_from_hash(font)
    font = _symbolkey2str(font)
    foundry  = (info = font['foundry'] .to_s)?  info: '*'
    family   = (info = font['family']  .to_s)?  info: '*'
    weight   = (info = font['weight']  .to_s)?  info: '*'
    slant    = (info = font['slant']   .to_s)?  info: '*'
    swidth   = (info = font['swidth']  .to_s)?  info: '*'
    adstyle  = (info = font['adstyle'] .to_s)?  info: '*'
    pixels   = (info = font['pixels']  .to_s)?  info: '*'
    points   = (info = font['points']  .to_s)?  info: '*'
    resx     = (info = font['resx']    .to_s)?  info: '*'
    resy     = (info = font['resy']    .to_s)?  info: '*'
    space    = (info = font['space']   .to_s)?  info: '*'
    avgWidth = (info = font['avgWidth'].to_s)?  info: '*'
    charset  = (info = font['charset'] .to_s)?  info: '*'
    encoding = (info = font['encoding'].to_s)?  info: '*'

    [foundry, family, weight, slant, swidth, adstyle,
      pixels, points, resx, resy, space, avgWidth, charset, encoding]
  end

  def create_latinfont_tk4x(font)
    if font.kind_of? Hash
      @latinfont = '-' + _get_font_info_from_hash(font).join('-') + '-'

    elsif font.kind_of? Array
      finfo = {}
      finfo['family'] = font[0].to_s
      if font[1]
        fsize = font[1].to_s
        if fsize != '0' && fsize =~ /^(|\+|-)([0-9]+)$/
          if $1 == '-'
            finfo['pixels'] = $2
          else
            finfo['points'] = $2
          end
        else
          finfo['points'] = '13'
        end
      end
      font[2..-1].each{|style|
        case (style)
        when 'normal'
          finfo['weight'] = style
        when 'bold'
          finfo['weight'] = style
        when 'roman'
          finfo['slant'] = 'r'
        when 'italic'
          finfo['slant'] = 'i'
        end
      }

      @latinfont = '-' + _get_font_info_from_hash(finfo).join('-') + '-'

    elsif font.kind_of? TkFont
      @latinfont = font.latin_font

    else
      if font
        @latinfont = font
      else
        @latinfont = DEFAULT_LATIN_FONT_NAME
      end

    end
  end

  def create_kanjifont_tk4x(font)
    unless JAPANIZED_TK
      @kanjifont = ""
      return
    end

    if font.kind_of? Hash
      @kanjifont = '-' + _get_font_info_from_hash(font).join('-') + '-'

    elsif font.kind_of? Array
      finfo = {}
      finfo['family'] = font[0].to_s
      if font[1]
        fsize = font[1].to_s
        if fsize != '0' && fsize =~ /^(|\+|-)([0-9]+)$/
          if $1 == '-'
            finfo['pixels'] = $2
          else
            finfo['points'] = $2
          end
        else
          finfo['points'] = '13'
        end
      end
      font[2..-1].each{|style|
        case (style)
        when 'normal'
          finfo['weight'] = style
        when 'bold'
          finfo['weight'] = style
        when 'roman'
          finfo['slant'] = 'r'
        when 'italic'
          finfo['slant'] = 'i'
        end
      }

      @kanjifont = '-' + _get_font_info_from_hash(finfo).join('-') + '-'
    elsif font.kind_of? TkFont
      @kanjifont = font.kanji_font_id
    else
      if font
        @kanjifont = font
      else
        @kanjifont = DEFAULT_KANJI_FONT_NAME
      end
    end
  end

  def create_compoundfont_tk4x(ltn, knj, keys)
    create_latinfont(ltn)
    create_kanjifont(knj)

    if JAPANIZED_TK
      @compoundfont = [[@latinfont], [@kanjifont]]
      @fontslot = {'font'=>@latinfont, 'kanjifont'=>@kanjifont}
      # @fontslot.clear
      # @fontslot['font'] = @latinfont
      # @fontslot['kanjifont'] = @kanjifont
    else
      @compoundfont = @latinfont
      @fontslot = {'font'=>@latinfont}
      # @fontslot.clear
      # @fontslot['font'] = @latinfont
    end
  end

  def create_latinfont_tk8x(font)
    @latinfont = @id + 'l'

    if JAPANIZED_TK
      if font.kind_of? Hash
        if font[:charset] || font['charset']
          tk_call('font', 'create', @latinfont, *hash_kv(font))
        else
          tk_call('font', 'create', @latinfont,
                  '-charset', 'iso8859', *hash_kv(font))
        end
      elsif font.kind_of? Array
        tk_call('font', 'create', @latinfont, '-copy', array2tk_list(font))
        tk_call('font', 'configure', @latinfont, '-charset', 'iso8859')
      elsif font.kind_of? TkFont
        tk_call('font', 'create', @latinfont, '-copy', font.latin_font)
      elsif font
        tk_call('font', 'create', @latinfont, '-copy', font,
                '-charset', 'iso8859')
      else
        tk_call('font', 'create', @latinfont, '-charset', 'iso8859')
      end
    else
      if font.kind_of? Hash
        tk_call('font', 'create', @latinfont, *hash_kv(font))
      else
        keys = {}
        if font.kind_of? Array
          actual_core(array2tk_list(font)).each{|key,val| keys[key] = val}
        elsif font.kind_of? TkFont
          actual_core(font.latin_font).each{|key,val| keys[key] = val}
        elsif font
          actual_core(font).each{|key,val| keys[key] = val}
        end
        tk_call('font', 'create', @latinfont, *hash_kv(keys))
      end

      if font && @compoundfont
        keys = {}
        actual_core(@latinfont).each{|key,val| keys[key] = val}
        tk_call('font', 'configure', @compoundfont, *hash_kv(keys))
      end
    end
  end

  def create_kanjifont_tk8x(font)
    @kanjifont = @id + 'k'

    if JAPANIZED_TK
      if font.kind_of? Hash
        if font[:charset] || font['charset']
          tk_call('font', 'create', @kanjifont, *hash_kv(font))
        else
          tk_call('font', 'create', @kanjifont,
                  '-charset', 'jisx0208.1983', *hash_kv(font))
        end
      elsif font.kind_of? Array
        tk_call('font', 'create', @kanjifont, '-copy', array2tk_list(font))
        tk_call('font', 'configure', @kanjifont, '-charset', 'jisx0208.1983')
      elsif font.kind_of? TkFont
        tk_call('font', 'create', @kanjifont, '-copy', font.kanji_font_id)
      elsif font
        tk_call('font', 'create', @kanjifont, '-copy', font,
                '-charset', 'jisx0208.1983')
      else
        tk_call('font', 'create', @kanjifont, '-charset', 'jisx0208.1983')
      end
      # end of JAPANIZED_TK

    else
      if font.kind_of? Hash
        tk_call('font', 'create', @kanjifont, *hash_kv(font))
      else
        keys = {}
        if font.kind_of? Array
          actual_core(array2tk_list(font)).each{|key,val| keys[key] = val}
        elsif font.kind_of? TkFont
          actual_core(font.kanji_font_id).each{|key,val| keys[key] = val}
        elsif font
          actual_core(font).each{|key,val| keys[key] = val}
        end
        tk_call('font', 'create', @kanjifont, *hash_kv(keys))
      end

      if font && @compoundfont
        keys = {}
        actual_core(@kanjifont).each{|key,val| keys[key] = val}
        tk_call('font', 'configure', @compoundfont, *hash_kv(keys))
      end
    end
  end

  def create_compoundfont_tk8x(ltn, knj, keys)
    if knj
      create_latinfont(ltn)
      create_kanjifont(knj)
    else
      cfnt = ltn
      create_kanjifont(cfnt)
      create_latinfont(cfnt)
    end

    @compoundfont = @id + 'c'

    if JAPANIZED_TK
      unless keys
        keys = {}
      else
        keys = keys.dup
      end
      if (tk_call('font', 'configure', @latinfont, '-underline') == '1' &&
          tk_call('font', 'configure', @kanjifont, '-underline') == '1' &&
          !keys.key?('underline'))
        keys['underline'] = true
      end
      if (tk_call('font', 'configure', @latinfont, '-overstrike') == '1' &&
          tk_call('font', 'configure', @kanjifont, '-overstrike') == '1' &&
          !keys.key?('overstrike'))
        keys['overstrike'] = true
      end

      @fontslot = {'font'=>@compoundfont}
      # @fontslot['font'] = @compoundfont
      begin
        tk_call('font', 'create', @compoundfont,
                '-compound', [@latinfont, @kanjifont], *hash_kv(keys))
      rescue RuntimeError => e
        if ltn == knj
          if e.message =~ /kanji font .* specified/
            tk_call('font', 'delete', @latinfont)
            create_latinfont(DEFAULT_LATIN_FONT_NAME)
            opts = []
            Hash[*(tk_split_simplelist(tk_call('font', 'configure',
                                               @kanjifont)))].each{|k,v|
              case k
              when '-size', '-weight', '-slant', '-underline', '-overstrike'
                opts << k << v
              end
            }
            tk_call('font', 'configure', @latinfont, *opts)
            tk_call('font', 'create', @compoundfont,
                    '-compound', [@latinfont, @kanjifont], *hash_kv(keys))

          elsif e.message =~ /ascii font .* specified/
            tk_call('font', 'delete', @kanjifont)
            create_kanjifont(DEFAULT_KANJI_FONT_NAME)
            opts = []
            Hash[*(tk_split_simplelist(tk_call('font', 'configure',
                                               @latinfont)))].each{|k,v|
              case k
              when '-size', '-weight', '-slant', '-underline', '-overstrike'
                opts << k << v
              end
            }
            tk_call('font', 'configure', @kanjifont, *opts)
            tk_call('font', 'create', @compoundfont,
                    '-compound', [@latinfont, @kanjifont], *hash_kv(keys))

          else
            raise e
          end
        else
          raise e
        end
      end
    else
      tk_call('font', 'create', @compoundfont)

      latinkeys = {}
      begin
        actual_core(@latinfont).each{|key,val| latinkeys[key] = val}
      rescue
        latinkeys = {}
      end
      if latinkeys != {}
        tk_call('font', 'configure', @compoundfont, *hash_kv(latinkeys))
      end

      if knj
        compoundkeys = nil
        kanjikeys = {}
        begin
          actual_core(@kanjifont).each{|key,val| kanjikeys[key] = val}
        rescue
          kanjikeys = {}
        end
        if kanjikeys != {}
          tk_call('font', 'configure', @compoundfont, *hash_kv(kanjikeys))
        end
      end

      if cfnt
        if cfnt.kind_of?(Hash)
          compoundkeys = cfnt.dup
        else
          compoundkeys = {}
          actual_core(cfnt).each{|key,val| compoundkeys[key] = val}
        end
        compoundkeys.update(_symbolkey2str(keys))
        keys = compoundkeys
      end

      @fontslot = {'font'=>@compoundfont}
      # @fontslot['font'] = @compoundfont
      tk_call('font', 'configure', @compoundfont, *hash_kv(keys))
    end
  end

  ###################################
  public
  ###################################
  def inspect
    sprintf("#<%s:%0x:%s>", self.class.inspect, self.__id__, @compoundfont)
  end

  def method_missing(id, *args)
    name = id.id2name
    case args.length
    when 1
      if name[-1] == ?=
        configure name[0..-2], args[0]
        args[0]
      else
        configure name, args[0]
        self
      end
    when 0
      begin
        configinfo name
      rescue
        super(id, *args)
#        fail NameError, "undefined local variable or method `#{name}' for #{self.to_s}", error_at
      end
    else
      super(id, *args)
#      fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at
    end
  end

  def call_font_configure(path, *args)
    if path.kind_of?(Array)
      # [path, optkey]
      win, tag = path[0].split(';')
      optkey = path[1].to_s
    else
      win, tag, optkey = path.split(';')
    end

    fontslot = _symbolkey2str(@fontslot)
    if optkey && optkey != ""
      ltn = fontslot.delete('font')
      knj = fontslot.delete('kanjifont')
      fontslot[optkey] = ltn if ltn
      fontslot["kanji#{optkey}"] = knj if knj
    end

    keys = _symbolkey2str(args.pop).update(fontslot)
    args.concat(hash_kv(keys))
    begin
      tk_call(*args)
    rescue => e
      unless TkConfigMethod.__IGNORE_UNKNOWN_CONFIGURE_OPTION__
        fail e
      end
    end
    Tk_FontUseTBL.mutex.synchronize{
      Tk_FontUseTBL[[win, tag, optkey].join(';')] = self
    }
    self
  end

  def used
    ret = []
    table = nil
    Tk_FontUseTBL.mutex.synchronize{
      table = Tk_FontUseTBL.clone # to avoid deadlock
    }
    table.each{|key,value|
      next unless self == value
      if key.include?(';')
        win, tag, optkey = key.split(';')
        winobj = tk_tcl2ruby(win)
        #if winobj.kind_of? TkText
        if winobj.kind_of?(TkText) || winobj.kind_of?(Tk::Text)
          if optkey
            ret.push([winobj, winobj.tagid2obj(tag), optkey])
          else
            ret.push([winobj, winobj.tagid2obj(tag)])
          end
        #elsif winobj.kind_of? TkCanvas
        elsif winobj.kind_of?(TkCanvas) || winobj.kind_of?(Tk::Canvas)
          if (tagobj = TkcTag.id2obj(winobj, tag)).kind_of? TkcTag
            if optkey
              ret.push([winobj, tagobj, optkey])
            else
              ret.push([winobj, tagobj])
            end
          elsif (tagobj = TkcItem.id2obj(winobj, tag)).kind_of? TkcItem
            if optkey
              ret.push([winobj, tagobj, optkey])
            else
              ret.push([winobj, tagobj])
            end
          else
            if optkey
              ret.push([winobj, tag, optkey])
            else
              ret.push([winobj, tag])
            end
          end
        #elsif winobj.kind_of? TkMenu
        elsif winobj.kind_of?(TkMenu) || winobj.kind_of?(Tk::Menu)
          if optkey
            ret.push([winobj, tag, optkey])
          else
            ret.push([winobj, tag])
          end
        else
          if optkey
            ret.push([win, tag, optkey])
          else
            ret.push([win, tag])
          end
        end
      else
        ret.push(tk_tcl2ruby(key))
      end
    }
    ret
  end

  def id
    @id
  end

  def to_eval
    font
  end

  def font
    @compoundfont
  end
  alias font_id font
  alias name font
  alias to_s font

  def latin_font_id
    @latinfont
  end

  def latin_font
    # @latinfont
    if @descendant[0] # [0] -> latin
      @descendant[0]
    else
      @descendant[0] = DescendantFont.new(self, 'latin')
    end
=begin
    if @latin_descendant
      @latin_descendant
    else
      @latin_descendant = DescendantFont.new(self, 'latin')
    end
=end
  end
  alias latinfont latin_font

  def kanji_font_id
    @kanjifont
  end

  def kanji_font
    # @kanjifont
    if @descendant[1] # [1] -> kanji
      @descendant[1]
    else
      @descendant[1] = DescendantFont.new(self, 'kanji')
    end
=begin
    if @kanji_descendant
      @kanji_descendant
    else
      @kanji_descendant = DescendantFont.new(self, 'kanji')
    end
=end
  end
  alias kanjifont kanji_font

  def actual(option=nil)
    actual_core(@compoundfont, nil, option)
  end
  def actual_hash(option=nil)
    Hash[actual(option)]
  end

  def actual_displayof(win, option=nil)
    win = '.' unless win
    actual_core(@compoundfont, win, option)
  end
  def actual_hash_displayof(win, option=nil)
    Hash[actual_displayof(win, option)]
  end

  def latin_actual(option=nil)
    if @latinfont == nil
      actual_core(@compoundfont, nil, option) # use @compoundfont
    else
      actual_core(@latinfont, nil, option)
    end
  end
  def latin_actual_hash(option=nil)
    Hash[latin_actual(option)]
  end

  def latin_actual_displayof(win, option=nil)
    win = '.' unless win
    if @latinfont == nil
      actual_core(@compoundfont, win, option) # use @compoundfont
    else
      actual_core(@latinfont, win, option)
    end
  end
  def latin_actual_hash_displayof(win, option=nil)
    Hash[latin_actual_displayof(win, option)]
  end

  def kanji_actual(option=nil)
    #if JAPANIZED_TK
    if @kanjifont == nil
      actual_core(@compoundfont, nil, option) # use @compoundfont
    elsif @kanjifont != ""
      actual_core(@kanjifont, nil, option)
    else
      actual_core_tk4x(nil, nil, option)
    end
  end
  def kanji_actual_hash(option=nil)
    Hash[kanji_actual(option)]
  end

  def kanji_actual_displayof(win, option=nil)
    #if JAPANIZED_TK
    if @kanjifont == nil
      actual_core(@compoundfont, nil, option) # use @compoundfont
    elsif @kanjifont != ""
      win = '.' unless win
      actual_core(@kanjifont, win, option)
    else
      actual_core_tk4x(nil, win, option)
    end
  end
  def kanji_actual_hash_displayof(win, option=nil)
    Hash[kanji_actual_displayof(win, option)]
  end

  def [](slot)
    configinfo slot
  end

  def []=(slot, val)
    configure slot, val
    val
  end

  def configure(slot, value=None)
    configure_core(@compoundfont, slot, value)
    self
  end

  def configinfo(slot=nil)
    configinfo_core(@compoundfont, slot)
  end

  def current_configinfo(slot=nil)
    current_configinfo_core(@compoundfont, slot)
  end

  def delete
    delete_core
  end

  def latin_configure(slot, value=None)
    if JAPANIZED_TK
      configure_core(@latinfont, slot, value)
    else
      configure(slot, value)
    end
    self
  end

  def latin_configinfo(slot=nil)
    if JAPANIZED_TK
      configinfo_core(@latinfont, slot)
    else
      configinfo(slot)
    end
  end
  def latin_current_configinfo(slot=nil)
    Hash[latin_configinfo(slot)]
  end

  def kanji_configure(slot, value=None)
    #if JAPANIZED_TK
    if @kanjifont == nil
      configure_core(@compoundfont, slot, value) # use @compoundfont
    elsif @kanjifont != ""
      configure_core(@kanjifont, slot, value)
      configure('size'=>configinfo('size')) # to reflect new configuration
    else
      #""
      configure(slot, value)
    end
    self
  end

  def kanji_configinfo(slot=nil)
    #if JAPANIZED_TK
    if @kanjifont == nil
      configure_core(@compoundfont, slot) # use @compoundfont
    elsif @kanjifont != ""
      configinfo_core(@kanjifont, slot)
    else
      #[]
      configinfo(slot)
    end
  end
  def kanji_current_configinfo(slot=nil)
    Hash[kanji_configinfo(slot)]
  end

  def replace(ltn, knj=None)
    knj = ltn if knj == None
    latin_replace(ltn)
    kanji_replace(knj)
    self
  end

  def latin_replace(ltn)
    if @latinfont
      latin_replace_core(ltn)
      reset_pointadjust
    else
      # not compound font -> copy properties of ltn
      latinkeys = {}
      begin
        actual_core(ltn).each{|key,val| latinkeys[key] = val}
      rescue
        latinkeys = {}
      end
      begin
        tk_call('font', 'configure', @compoundfont, *hash_kv(latinkeys))
      rescue
        # not exist? (deleted?) -> create font
        tk_call('font', 'create', @compoundfont, *hash_kv(latinkeys))
      end
    end

    self
  end

  def kanji_replace(knj)
    return self unless @kanjifont  # ignore
    kanji_replace_core(knj)
    reset_pointadjust
    self
  end

  def measure(text)
    measure_core(@compoundfont, nil, text)
  end

  def measure_displayof(win, text)
    win = '.' unless win
    measure_core(@compoundfont, win, text)
  end

  def metrics(option=nil)
    metrics_core(@compoundfont, nil, option)
  end
  def metrics_hash(option=nil)
    if option
      val = metrics(option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[metrics(option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def metrics_displayof(win, option=nil)
    win = '.' unless win
    metrics_core(@compoundfont, win, option)
  end
  def metrics_hash_displayof(win, option=nil)
    if option
      val = metrics_displayof(win, option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[metrics_displayof(win, option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def latin_metrics(option=nil)
    if @latinfont == nil
      metrics_core(@compoundfont, nil, option) # use @compoundfont
    else
      metrics_core(@latinfont, nil, option)
    end
  end
  def latin_metrics_hash(option=nil)
    if option
      val = latin_metrics(option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[latin_metrics(option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def latin_metrics_displayof(win, option=nil)
    win = '.' unless win
    if @latinfont == nil
      metrics_core(@compoundfont, win, option) # use @compoundfont
    else
      metrics_core(@latinfont, win, option)
    end
  end
  def latin_metrics_hash_displayof(win, option=nil)
    if option
      val = latin_metrics_displayof(win, option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[latin_metrics_displayof(win, option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def kanji_metrics(option=nil)
    if @latinfont == nil
      metrics_core(@compoundfont, nil, option) # use @compoundfont
    elsif JAPANIZED_TK
      metrics_core(@kanjifont, nil, option)
    else
      metrics_core_tk4x(nil, nil, option)
    end
  end
  def kanji_metrics_hash(option=nil)
    if option
      val = kanji_metrics(option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[kanji_metrics(option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def kanji_metrics_displayof(win, option=nil)
    win = '.' unless win
    if @latinfont == nil
      metrics_core(@compoundfont, win, option) # use @compoundfont
    elsif JAPANIZED_TK
      metrics_core(@kanjifont, win, option)
    else
      metrics_core_tk4x(nil, win, option)
    end
  end
  def kanji_metrics_hash_displayof(win, option=nil)
    if option
      val = kanji_metrics_displayof(win, option)
      case TkFont::MetricsType[option.to_s]
      when ?n
        val = TkComm::num_or_str(val)
      when ?b
        val = TkComm::bool(val)
      else
        # do nothing
      end
      return val
    end

    h = Hash[kanji_metrics_displayof(win, option)]
    h.keys.each{|k|
      case TkFont::MetricsType[k.to_s]
      when ?n
        h[k] = TkComm::num_or_str(h[k])
      when ?b
        h[k] = TkComm::bool(h[k])
      else
        # do nothing
      end
    }
    h
  end

  def reset_pointadjust
    begin
      if /^8\..*/ === Tk::TK_VERSION  && JAPANIZED_TK
        configure('pointadjust' => latin_actual.assoc('size')[1].to_f /
                                      kanji_actual.assoc('size')[1].to_f )
      end
    rescue
    end
    self
  end

  ###################################
  # private alias
  ###################################
  case (Tk::TK_VERSION)
  when /^4\..*/
    alias create_latinfont        create_latinfont_tk4x
    alias create_kanjifont        create_kanjifont_tk4x
    alias create_compoundfont     create_compoundfont_tk4x

  when /^8\.[0-5]/
    alias create_latinfont        create_latinfont_tk8x
    alias create_kanjifont        create_kanjifont_tk8x
    alias create_compoundfont     create_compoundfont_tk8x

  else
    alias create_latinfont        create_latinfont_tk8x
    alias create_kanjifont        create_kanjifont_tk8x
    alias create_compoundfont     create_compoundfont_tk8x

  end

  ###################################
  # public alias
  ###################################
  alias ascii_font             latin_font
  alias asciifont              latinfont
  alias create_asciifont       create_latinfont
  alias ascii_actual           latin_actual
  alias ascii_actual_displayof latin_actual_displayof
  alias ascii_configure        latin_configure
  alias ascii_configinfo       latin_configinfo
  alias ascii_replace          latin_replace
  alias ascii_metrics          latin_metrics

  ###################################
=begin
  def dup
    TkFont.new(self)
  end
  def clone
    TkFont.new(self)
  end
=end
end

module TkFont::CoreMethods
  include Tk
  extend TkCore

  private

  def actual_core_tk4x(font, win=nil, option=nil)
    # dummy
    if option == 'pointadjust' || option == :pointadjust
        1.0
    elsif option
      case TkFont::OptionType[option.to_s]
      when ?n
        0
      when ?b
        false
      else
        ''
      end
    else
      [['family',''], ['size',0], ['weight',''], ['slant',''],
        ['underline',false], ['overstrike',false], ['charset',''],
        ['pointadjust',0]]
    end
  end

  def actual_core_tk8x(font, win=nil, option=nil)
    font = '{}' if font == ''

    if option == 'compound' || option == :compound
      ""
    elsif option
      if win
        val = tk_call('font', 'actual', font,
                      "-displayof", win, "-#{option}")
      else
        val = tk_call('font', 'actual', font, "-#{option}")
      end
      case TkFont::OptionType[option.to_s]
      when ?n
        num_or_str(val)
      when ?b
        bool(val)
      else
        val
      end
    else
      l = tk_split_simplelist(if win
                                 tk_call('font', 'actual', font,
                                                     "-displayof", win)
                              else
                                 tk_call('font', 'actual', font)
                              end)
      r = []
      while key=l.shift
        if key == '-compound'
          l.shift
        else
          key = key[1..-1]
          val = l.shift
          case TkFont::OptionType[key]
          when ?n
            r.push [key, num_or_str(val)]
          when ?b
            r.push [key, bool(val)]
          else
            r.push [key, val]
          end
        end
      end
      r
    end
  end

  def configure_core_tk4x(font, slot, value=None)
    #""
    self
  end

  def configinfo_core_tk4x(font, option=nil)
    # dummy
    if TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      if option == 'pointadjust' || option == :pointadjust
        1.0
      elsif option
        case TkFont::OptionType[option.to_s]
        when ?n
          0
        when ?b
          false
        else
          ''
        end
      else
        [['family',''], ['size',0], ['weight',''], ['slant',''],
          ['underline',false], ['overstrike',false], ['charset',''],
          ['pointadjust',1.0]]
      end
    else # ! TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      current_configinfo_core_tk4x(font, option)
    end
  end

  def current_configinfo_core_tk4x(font, option=nil)
    if option
      case TkFont::OptionType[option.to_s]
      when ?n
        0
      when ?b
        false
      else
        ''
      end
    else
      {'family'=>'', 'size'=>0, 'weight'=>'', 'slant'=>'',
        'underline'=>false, 'overstrike'=>false,
        'charset'=>false, 'pointadjust'=>1.0}
    end
  end

  def configure_core_tk8x(font, slot, value=None)
    if JAPANIZED_TK
      begin
        padjust = tk_call('font', 'configure', font, '-pointadjust')
      rescue
        padjust = nil
      end
    else
      padjust = nil
    end
    if slot.kind_of? Hash
      if JAPANIZED_TK && (slot.key?('family') || slot.key?(:family))
        slot = _symbolkey2str(slot)
        configure_core_tk8x(font, 'family', slot.delete('family'))
      end

      if ((slot.key?('size') || slot.key?(:size)) &&
          padjust && !slot.key?('pointadjust') && !slot.key?(:pointadjust))
        tk_call('font', 'configure', font,
                '-pointadjust', padjust, *hash_kv(slot))
      else
        tk_call('font', 'configure', font, *hash_kv(slot))
      end
    elsif (slot == 'size' || slot == :size) && padjust != nil
      tk_call('font', 'configure', font,
              "-#{slot}", value, '-pointadjust', padjust)
    elsif JAPANIZED_TK && (slot == 'family' || slot == :family)
      # coumpund font?
      begin
        compound = tk_split_simplelist(tk_call('font', 'configure',
                                               font, '-compound'))
      rescue
        tk_call('font', 'configure', font, '-family', value)
        return self
      end
      if compound == []
        tk_call('font', 'configure', font, '-family', value)
        return self
      end
      ltn, knj = compound

      lfnt = tk_call('font', 'create', '-copy', ltn)
      begin
        tk_call('font', 'configure', lfnt, '-family', value)
        latin_replace_core_tk8x(lfnt)
      rescue RuntimeError => e
        fail e if $DEBUG
      ensure
        tk_call('font', 'delete', lfnt) if lfnt != ''
      end

      kfnt = tk_call('font', 'create', '-copy', knj)
      begin
        tk_call('font', 'configure', kfnt, '-family', value)
        kanji_replace_core_tk8x(lfnt)
      rescue RuntimeError => e
        fail e if $DEBUG
      ensure
        tk_call('font', 'delete', kfnt) if kfnt != ''
      end

    else
      tk_call('font', 'configure', font, "-#{slot}", value)
    end
    self
  end

  def configinfo_core_tk8x(font, option=nil)
    if TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      if option == 'compound' || option == :compound
        ""
      elsif option
        val = tk_call('font', 'configure', font, "-#{option}")
        case TkFont::OptionType[option.to_s]
        when ?n
          num_or_str(val)
        when ?b
          bool(val)
        else
          val
        end
      else
        l = tk_split_simplelist(tk_call('font', 'configure', font))
        r = []
        while key=l.shift
          if key == '-compound'
            l.shift
          else
            key = key[1..-1]
            val = l.shift
            case TkFont::OptionType[key]
            when ?n
              r.push [key, num_or_str(val)]
            when ?b
              r.push [key, bool(val)]
            else
              r.push [key, val]
            end
          end
        end
        r
      end
    else # ! TkComm::GET_CONFIGINFOwoRES_AS_ARRAY
      current_configinfo_core_tk8x(font, option)
    end
  end

  def current_configinfo_core_tk8x(font, option=nil)
    if option == 'compound'
      ""
    elsif option
      val = tk_call('font', 'configure', font, "-#{option}")
      case TkFont::OptionType[option.to_s]
      when ?n
        num_or_str(val)
      when ?b
        bool(val)
      else
        val
      end
    else
      l = tk_split_simplelist(tk_call('font', 'configure', font))
      h = {}
      while key=l.shift
        if key == '-compound'
          l.shift
        else
          key = key[1..-1]
          val = l.shift
          case TkFont::OptionType[key]
          when ?n
            h[key] = num_or_str(val)
          when ?b
            h[key] = bool(val)
          else
            h[key] = val
          end
        end
      end
      h
    end
  end

  def delete_core_tk4x
    TkFont::Tk_FontNameTBL.mutex.synchronize{
      TkFont::Tk_FontNameTBL.delete(@id)
    }
    TkFont::Tk_FontUseTBL.mutex.synchronize{
      TkFont::Tk_FontUseTBL.delete_if{|key,value| value == self}
    }
  end

  def delete_core_tk8x
    begin
      tk_call('font', 'delete', @latinfont) if @latinfont
    rescue
    end
    begin
      tk_call('font', 'delete', @kanjifont) if @kanjifont
    rescue
    end
    begin
      tk_call('font', 'delete', @compoundfont) if @compoundfont
    rescue
    end
    TkFont::Tk_FontNameTBL.mutex.synchronize{
      TkFont::Tk_FontNameTBL.delete(@id)
    }
    TkFont::Tk_FontUseTBL.mutex.synchronize{
      TkFont::Tk_FontUseTBL.delete_if{|key,value| value == self}
    }
  end

  def latin_replace_core_tk4x(ltn)
    create_latinfont_tk4x(ltn)
    @compoundfont[0] = [@latinfont] if JAPANIZED_TK
    @fontslot['font'] = @latinfont
    table = nil
    TkFont::Tk_FontUseTBL.mutex.synchronize{
      table = TkFont::Tk_FontUseTBL.clone
    }
    table.each{|w, fobj|
      if self == fobj
        begin
          if w.include?(';')
            win, tag, optkey = w.split(';')
            optkey = 'font' if optkey == nil || optkey == ''
            winobj = tk_tcl2ruby(win)
#           winobj.tagfont_configure(tag, {'font'=>@latinfont})
            #if winobj.kind_of? TkText
            if winobj.kind_of?(TkText) || winobj.kind_of?(Tk::Text)
              tk_call(win, 'tag', 'configure', tag, "-#{optkey}", @latinfont)
            #elsif winobj.kind_of? TkCanvas
            elsif winobj.kind_of?(TkCanvas) || winobj.kind_of?(Tk::Canvas)
              tk_call(win, 'itemconfigure', tag, "-#{optkey}", @latinfont)
            #elsif winobj.kind_of? TkMenu
            elsif winobj.kind_of?(TkMenu) || winobj.kind_of?(Tk::Menu)
              tk_call(win, 'entryconfigure', tag, "-#{optkey}", @latinfont)
            else
              raise RuntimeError, "unknown widget type"
            end
          else
#           tk_tcl2ruby(w).font_configure('font'=>@latinfont)
            tk_call(w, 'configure', '-font', @latinfont)
          end
        rescue
          TkFont::Tk_FontUseTBL.mutex.synchronize{
            TkFont::Tk_FontUseTBL.delete(w)
          }
        end
      end
    }
    self
  end

  def kanji_replace_core_tk4x(knj)
    return self unless JAPANIZED_TK

    create_kanjifont_tk4x(knj)
    @compoundfont[1] = [@kanjifont]
    @fontslot['kanjifont'] = @kanjifont
    table = nil
    TkFont::Tk_FontUseTBL.mutex.synchronize{
      table = TkFont::Tk_FontUseTBL.clone
    }
    table.dup.each{|w, fobj|
      if self == fobj
        begin
          if w.include?(';')
            win, tag, optkey = w.split(';')
            optkey = 'kanjifont' unless optkey
            winobj = tk_tcl2ruby(win)
#           winobj.tagfont_configure(tag, {'kanjifont'=>@kanjifont})
            #if winobj.kind_of? TkText
            if winobj.kind_of?(TkText) || winobj.kind_of?(Tk::Text)
              tk_call(win, 'tag', 'configure', tag, "-#{optkey}", @kanjifont)
            #elsif winobj.kind_of? TkCanvas
            elsif winobj.kind_of?(TkCanvas) || winobj.kind_of?(Tk::Canvas)
              tk_call(win, 'itemconfigure', tag, "-#{optkey}", @kanjifont)
            #elsif winobj.kind_of? TkMenu
            elsif winobj.kind_of?(TkMenu) || winobj.kind_of?(Tk::Menu)
              tk_call(win, 'entryconfigure', tag, "-#{optkey}", @latinfont)
            else
              raise RuntimeError, "unknown widget type"
            end
          else
#           tk_tcl2ruby(w).font_configure('kanjifont'=>@kanjifont)
            tk_call(w, 'configure', '-kanjifont', @kanjifont)
          end
        rescue
          Tk_FontUseTBL.mutex.synchronize{
            TkFont::Tk_FontUseTBL.delete(w)
          }
        end
      end
    }
    self
  end

  def latin_replace_core_tk8x(ltn)
    ltn = '{}' if ltn == ''

    if JAPANIZED_TK
      begin
        tk_call('font', 'delete', '@font_tmp')
      rescue
      end
      begin
        fnt_bup = tk_call('font', 'create', '@font_tmp', '-copy', @latinfont)
      rescue
        #fnt_bup = ''
        fnt_bup = TkFont::DEFAULT_LATIN_FONT_NAME
      end
    end

    begin
      tk_call('font', 'delete', @latinfont)
    rescue
    end
    create_latinfont(ltn)

    if JAPANIZED_TK
      keys = self.configinfo
      tk_call('font', 'delete', @compoundfont)
      begin
        tk_call('font', 'create', @compoundfont,
                '-compound', [@latinfont, @kanjifont], *hash_kv(keys))
=begin
        latinkeys = {}
        begin
          actual_core(@latinfont).each{|key,val| latinkeys[key] = val}
        rescue
          latinkeys = {}
        end
        if latinkeys != {}
          tk_call('font', 'configure', @compoundfont, *hash_kv(latinkeys))
        end
=end
      rescue RuntimeError => e
        tk_call('font', 'delete', @latinfont)
        if fnt_bup && fnt_bup != ''
          tk_call('font', 'create', @latinfont, '-copy', fnt_bup)
          tk_call('font', 'create', @compoundfont,
                  '-compound', [@latinfont, @kanjifont], *hash_kv(keys))
          tk_call('font', 'delete', fnt_bup)
        else
          fail e
        end
      end

    else
      latinkeys = {}
      begin
        actual_core(@latinfont).each{|key,val| latinkeys[key] = val}
      rescue
        latinkeys = {}
      end
      begin
        tk_call('font', 'configure', @compoundfont, *hash_kv(latinkeys))
      rescue
        # not exist? (deleted?) -> create font
        tk_call('font', 'create', @compoundfont, *hash_kv(latinkeys))
      end
    end
    self
  end

  def kanji_replace_core_tk8x(knj)
    knj = '{}' if knj == ''

    if JAPANIZED_TK
      begin
        tk_call('font', 'delete', '@font_tmp')
      rescue
      end
      begin
        fnt_bup = tk_call('font', 'create', '@font_tmp', '-copy', @kanjifont)
      rescue
        #fnt_bup = ''
        fnt_bup = TkFont::DEFAULT_KANJI_FONT_NAME
      end
    end

    begin
      tk_call('font', 'delete', @kanjifont)
    rescue
    end
    create_kanjifont(knj)

    if JAPANIZED_TK
      keys = self.configinfo
      tk_call('font', 'delete', @compoundfont)
      begin
        tk_call('font', 'create', @compoundfont,
                '-compound', [@latinfont, @kanjifont], *hash_kv(keys))
      rescue RuntimeError => e
        tk_call('font', 'delete', @kanjifont)
        if fnt_bup && fnt_bup != ''
          tk_call('font', 'create', @kanjifont, '-copy', fnt_bup)
          tk_call('font', 'create', @compoundfont,
                  '-compound', [@latinfont, @kanjifont], *hash_kv(keys))
          tk_call('font', 'delete', fnt_bup)
        else
          fail e
        end
      end
    end
    self
  end

  def measure_core_tk4x(font, win, text)
    0
  end

  def measure_core_tk8x(font, win, text)
    font = '{}' if font == ''

    if win
      number(tk_call('font', 'measure', font,
                     '-displayof', win, text))
    else
      number(tk_call('font', 'measure', font, text))
    end
  end

  def metrics_core_tk4x(font, win, option=nil)
    # dummy
    if option
      ""
    else
      [['ascent',[]], ['descent',[]], ['linespace',[]], ['fixed',[]]]
    end
  end

  def metrics_core_tk8x(font, win, option=nil)
    font = '{}' if font == ''

    if option
      if win
        number(tk_call('font', 'metrics', font,
                       "-displayof", win, "-#{option}"))
      else
        number(tk_call('font', 'metrics', font, "-#{option}"))
      end
    else
      l = tk_split_list(if win
                          tk_call('font','metrics',font,"-displayof",win)
                        else
                          tk_call('font','metrics',font)
                        end)
      r = []
      while key=l.shift
        r.push [key[1..-1], l.shift.to_i]
=begin
        if key == '-fixed'  # boolean value
          r.push [key[1..-1], bool(l.shift)]
        else
          r.push [key[1..-1], l.shift.to_i]
        end
=end
      end
      r
    end
  end

  ###################################
  # private alias
  ###################################
  case (Tk::TK_VERSION)
  when /^4\..*/
    alias actual_core             actual_core_tk4x
    alias configure_core          configure_core_tk4x
    alias configinfo_core         configinfo_core_tk4x
    alias current_configinfo_core current_configinfo_core_tk4x
    alias delete_core             delete_core_tk4x
    alias latin_replace_core      latin_replace_core_tk4x
    alias kanji_replace_core      kanji_replace_core_tk4x
    alias measure_core            measure_core_tk4x
    alias metrics_core            metrics_core_tk4x

  when /^8\.[0-9]/
    alias actual_core             actual_core_tk8x
    alias configure_core          configure_core_tk8x
    alias configinfo_core         configinfo_core_tk8x
    alias current_configinfo_core current_configinfo_core_tk8x
    alias delete_core             delete_core_tk8x
    alias latin_replace_core      latin_replace_core_tk8x
    alias kanji_replace_core      kanji_replace_core_tk8x
    alias measure_core            measure_core_tk8x
    alias metrics_core            metrics_core_tk8x

  else
    alias actual_core             actual_core_tk8x
    alias configure_core          configure_core_tk8x
    alias configinfo_core         configinfo_core_tk8x
    alias current_configinfo_core current_configinfo_core_tk8x
    alias delete_core             delete_core_tk8x
    alias latin_replace_core      latin_replace_core_tk8x
    alias kanji_replace_core      kanji_replace_core_tk8x
    alias measure_core            measure_core_tk8x
    alias metrics_core            metrics_core_tk8x

  end
end

class TkFont
  include TkFont::CoreMethods
  extend  TkFont::CoreMethods
end

class TkNamedFont < TkFont
  # for built-in named fonts
  def TkNamedFont.find(name)
    name = name.to_s
    unless (obj = Tk_FontNameTBL[name])
      obj = self.new(name) if TkFont.is_system_font?(name)
    end
    obj
  end

  def TkNamedFont.new(name, keys=nil)
    name = name.to_s
    obj = nil
    Tk_FontNameTBL.mutex.synchronize{
      unless (obj = Tk_FontNameTBL[name])
        (obj = self.allocate).instance_eval{
          @id = @compoundfont = name.to_s
          @latinfont = nil
          @kanjifont = nil
          @descendant = [self, self] # [latin, kanji] : dummy
          Tk_FontNameTBL[@id] = self
        }
      end
    }
    obj.instance_eval{ initialize(name, keys) }
    obj
  end

  ###########################
  private
  ###########################
  def initialize(name, keys=nil)
    @id = @compoundfont = name.to_s

    # if not exist named font, create it.
    begin
      if keys
        tk_call('font', 'configure', @compoundfont, keys)
      else
        tk_call('font', 'configure', @compoundfont)
      end
    rescue
      # the named font doesn't exist -> create
      if keys
        tk_call('font', 'create', @compoundfont, keys)
      else
        tk_call('font', 'create', @compoundfont)
      end
    end
  end

  def create_latinfont(fnt)
    # ignore
  end
  def create_kanjifont(fnt)
    # ignore
  end
  def create_compoundfont(ltn, knj, keys)
    # ignore
  end

  ###########################
  public
  ###########################
  def latin_font_id
    @compoundfont
  end
  def kanji_font_id
    @compoundfont
  end
end

#######################################
# define system font names
#######################################
if Tk::TCL_MAJOR_VERSION > 8 ||
    (Tk::TCL_MAJOR_VERSION == 8 && Tk::TCL_MINOR_VERSION >= 5)
  # add standard fonts of Tcl/Tk 8.5+
  TkFont::SYSTEM_FONT_NAMES.add [
    'TkDefaultFont', 'TkTextFont', 'TkFixedFont', 'TkMenuFont',
    'TkHeadingFont', 'TkCaptionFont', 'TkSmallCaptionFont',
    'TkIconFont', 'TkTooltipFont'
  ]
end

# platform-specific fonts
#  -- windows
TkFont::SYSTEM_FONT_NAMES.add [
  'ansifixed', 'ansi', 'device', 'oemfixed', 'systemfixed', 'system'
]

#  --  macintosh, macosx
TkFont::SYSTEM_FONT_NAMES.add ['system', 'application']

if Tk::TCL_MAJOR_VERSION > 8 ||
    (Tk::TCL_MAJOR_VERSION == 8 && Tk::TCL_MINOR_VERSION >= 5)
  TkFont::SYSTEM_FONT_NAMES.add ['menu']
end

#  --  macosx (Aqua theme)
if Tk::TCL_MAJOR_VERSION > 8 ||
    (Tk::TCL_MAJOR_VERSION == 8 && Tk::TCL_MINOR_VERSION >= 5)
  TkFont::SYSTEM_FONT_NAMES.add [
    'systemSystemFont', 'systemEmphasizedSystemFont',
    'systemSmallSystemFont', 'systemSmallEmphasizedSystemFont',
    'systemApplicationFont', 'systemLabelFont', 'systemViewsFont',
    'systemMenuTitleFont', 'systemMenuItemFont', 'systemMenuItemMarkFont',
    'systemMenuItemCmdKeyFont', 'systemWindowTitleFont',
    'systemPushButtonFont', 'systemUtilityWindowTitleFont',
    'systemAlertHeaderFont', 'systemToolbarFont', 'systemMiniSystemFont',
    'systemDetailSystemFont', 'systemDetailEmphasizedSystemFont'
  ]
end

#######################################
# autoload
#######################################
class TkFont
  autoload :Chooser, 'tk/fontchooser'
end