FontSB.c 136 KB
Newer Older
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 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884
/* 
 * FontSB.c
 *
 * (c) Copyright 1991-1994 Adobe Systems Incorporated.
 * All rights reserved.
 * 
 * Permission to use, copy, modify, distribute, and sublicense this software
 * and its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notices appear in all copies and that
 * both those copyright notices and this permission notice appear in
 * supporting documentation and that the name of Adobe Systems Incorporated
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  No trademark license
 * to use the Adobe trademarks is hereby granted.  If the Adobe trademark
 * "Display PostScript"(tm) is used to describe this software, its
 * functionality or for any other purpose, such use shall be limited to a
 * statement that this software works in conjunction with the Display
 * PostScript system.  Proper trademark attribution to reflect Adobe's
 * ownership of the trademark shall be given whenever any such reference to
 * the Display PostScript system is made.
 * 
 * ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
 * ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 * ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON- INFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO EVENT SHALL ADOBE BE LIABLE
 * TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  ADOBE WILL NOT
 * PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
 * 
 * Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
 * Incorporated which may be registered in certain jurisdictions
 * 
 * Author:  Adobe Systems Incorporated
 */
/* $XFree86$ */

#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <Xm/Xm.h>

/* There are no words to describe how I feel about having to do this */

#if XmVersion > 1001		
#include <Xm/ManagerP.h>
#else
#include <Xm/XmP.h>
#endif

#include <Xm/Form.h>
#include <Xm/List.h>
#include <Xm/Label.h>
#include <Xm/LabelG.h>
#include <Xm/PushB.h>
#include <Xm/PanedW.h>
#include <Xm/PushBG.h>
#include <Xm/SeparatoG.h>
#include <Xm/TextF.h>
#include <Xm/RowColumn.h>
#include <Xm/DrawingA.h>
#include <Xm/MessageB.h>
#include <DPS/dpsXclient.h>
#include "dpsXcommonI.h"
#include <DPS/dpsXcommon.h>
#include <DPS/dpsXshare.h>
#include <DPS/PSres.h>
#include <DPS/FontSBP.h>
#include "FSBwraps.h"
#include "FontSBI.h"
#include <DPS/FontSample.h>
#include <DPS/FontCreato.h>
#include <pwd.h>

#define PATH_BUF_SIZE 1024

/* Turn a string into a compound string */
#define UnsharedCS(str) XmStringCreate(str, XmSTRING_DEFAULT_CHARSET)
#define CS(str, w) _FSBCreateSharedCS(str, w)
static XmString CSempty;

/* Create a canonical representation of a string, and as a side effect
   make sure the string is in permanent storage.  This implementation may
   not work under all Xlibs */

#define Canonical(str) XrmQuarkToString(XrmStringToQuark(str))

static float defaultSizeList[] = {
#ifndef DEFAULT_SIZE_LIST
    8, 10, 12, 14, 16, 18, 24, 36, 48, 72
#else
	DEFAULT_SIZE_LIST
#endif /* DEFAULT_SIZE_LIST */
};

#ifndef DEFAULT_SIZE_LIST_COUNT
#define DEFAULT_SIZE_LIST_COUNT 10
#endif /* DEFAULT_SIZE_LIST_COUNT */

#ifndef DEFAULT_SIZE
static float default_size = 12.0;
#else
static float default_size = DEFAULT_SIZE
#endif /* DEFAULT_SIZE */

#ifndef DEFAULT_RESOURCE_PATH
#define DEFAULT_RESOURCE_PATH NULL
#endif /* DEFAULT_RESOURCE_PATH */

#ifndef DEFAULT_MAX_PENDING
#define DEFAULT_MAX_PENDING 10
#endif /* DEFAULT_MAX_PENDING */

#define Offset(field) XtOffsetOf(FontSelectionBoxRec, fsb.field)

static XtResource resources[] = {
    {XtNcontext, XtCContext, XtRDPSContext, sizeof(DPSContext),
	Offset(context), XtRDPSContext, (XtPointer) NULL},
    {XtNpreviewString, XtCPreviewString, XtRString, sizeof(String),
	Offset(preview_string), XtRString, (XtPointer) NULL},
    {XtNsizes, XtCSizes, XtRFloatList, sizeof(float*),
	Offset(sizes), XtRImmediate, (XtPointer) defaultSizeList},
    {XtNsizeCount, XtCSizeCount, XtRInt, sizeof(int),
	Offset(size_count), XtRImmediate, (XtPointer) DEFAULT_SIZE_LIST_COUNT},
    {XtNdefaultResourcePath, XtCDefaultResourcePath, XtRString, sizeof(String),
	Offset(default_resource_path), XtRImmediate,
	(XtPointer) DEFAULT_RESOURCE_PATH},
    {XtNresourcePathOverride, XtCResourcePathOverride,
	XtRString, sizeof(String),
	Offset(resource_path_override), XtRString, (XtPointer) NULL},
    {XtNuseFontName, XtCUseFontName, XtRBoolean, sizeof(Boolean),
	Offset(use_font_name), XtRImmediate, (XtPointer) True},
    {XtNfontName, XtCFontName, XtRString, sizeof(String),
	Offset(font_name), XtRString, (XtPointer) NULL},
    {XtNfontFamily, XtCFontFamily, XtRString, sizeof(String),
	Offset(font_family), XtRString, (XtPointer) NULL},
    {XtNfontFace, XtCFontFace, XtRString, sizeof(String),
	Offset(font_face), XtRString, (XtPointer) NULL},
    {XtNfontBlend, XtCFontBlend, XtRString, sizeof(String),
	Offset(font_blend), XtRString, (XtPointer) NULL},
    {XtNfontSize, XtCFontSize, XtRFloat, sizeof(String),
	Offset(font_size), XtRFloat, (XtPointer) &default_size},
    {XtNfontNameMultiple, XtCFontNameMultiple, XtRBoolean, sizeof(Boolean),
	Offset(font_name_multiple), XtRImmediate, (XtPointer) False},
    {XtNfontFamilyMultiple, XtCFontFamilyMultiple, XtRBoolean, sizeof(Boolean),
	Offset(font_family_multiple), XtRImmediate, (XtPointer) False},
    {XtNfontFaceMultiple, XtCFontFaceMultiple, XtRBoolean, sizeof(Boolean),
	Offset(font_face_multiple), XtRImmediate, (XtPointer) False},
    {XtNfontSizeMultiple, XtCFontSizeMultiple, XtRBoolean, sizeof(Boolean),
	Offset(font_size_multiple), XtRImmediate, (XtPointer) False},
    {XtNgetServerFonts, XtCGetServerFonts, XtRBoolean, sizeof(Boolean),
	Offset(get_server_fonts), XtRImmediate, (XtPointer) True},
    {XtNgetAFM, XtCGetAFM, XtRBoolean, sizeof(Boolean),
	Offset(get_afm), XtRImmediate, (XtPointer) False},
    {XtNautoPreview, XtCAutoPreview, XtRBoolean, sizeof(Boolean),
	Offset(auto_preview), XtRImmediate, (XtPointer) True},
    {XtNpreviewOnChange, XtCPreviewOnChange, XtRBoolean, sizeof(Boolean),
	Offset(preview_on_change), XtRImmediate, (XtPointer) True},
    {XtNundefUnusedFonts, XtCUndefUnusedFonts, XtRBoolean, sizeof(Boolean),
	Offset(undef_unused_fonts), XtRImmediate, (XtPointer) True},
    {XtNmaxPendingDeletes, XtCMaxPendingDeletes, XtRCardinal, sizeof(Cardinal),
	Offset(max_pending_deletes), XtRImmediate,
	(XtPointer) DEFAULT_MAX_PENDING},
    {XtNmakeFontsShared, XtCMakeFontsShared, XtRBoolean, sizeof(Boolean),
	Offset(make_fonts_shared), XtRImmediate, (XtPointer) True},
    {XtNshowSampler, XtCShowSampler, XtRBoolean, sizeof(Boolean),
	Offset(show_sampler), XtRImmediate, (XtPointer) False},
    {XtNshowSamplerButton, XtCShowSamplerButton, XtRBoolean, sizeof(Boolean),
	Offset(show_sampler_button), XtRImmediate, (XtPointer) True},
    {XtNtypographicSort, XtCTypographicSort, XtRBoolean, sizeof(Boolean),
	Offset(typographic_sort), XtRImmediate, (XtPointer) True},

    {XtNokCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(ok_callback), XtRCallback, (XtPointer) NULL},
    {XtNapplyCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(apply_callback), XtRCallback, (XtPointer) NULL},
    {XtNresetCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(reset_callback), XtRCallback, (XtPointer) NULL},
    {XtNcancelCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(cancel_callback), XtRCallback, (XtPointer) NULL},
    {XtNvalidateCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(validate_callback), XtRCallback, (XtPointer) NULL},
    {XtNfaceSelectCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(face_select_callback), XtRCallback, (XtPointer) NULL},
    {XtNcreateSamplerCallback, XtCCallback, XtRCallback,
	sizeof(XtCallbackList), Offset(create_sampler_callback),
	XtRCallback, (XtPointer) NULL},
    {XtNcreateCreatorCallback, XtCCallback, XtRCallback,
	sizeof(XtCallbackList), Offset(create_creator_callback),
	XtRCallback, (XtPointer) NULL},
    {XtNvalueChangedCallback, XtCCallback, XtRCallback, sizeof(XtCallbackList),
	Offset(value_changed_callback), XtRCallback, (XtPointer) NULL},

    {XtNpaneChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(pane_child), XtRWidget, (XtPointer) NULL},
    {XtNpreviewChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(preview_child), XtRWidget, (XtPointer) NULL},
    {XtNpanelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(panel_child), XtRWidget, (XtPointer) NULL},
    {XtNfamilyLabelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(family_label_child), XtRWidget, (XtPointer) NULL},
    {XtNfamilyMultipleLabelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(family_multiple_label_child), XtRWidget, (XtPointer) NULL},
    {XtNfamilyScrolledListChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(family_scrolled_list_child), XtRWidget, (XtPointer) NULL},
    {XtNfaceLabelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(face_label_child), XtRWidget, (XtPointer) NULL},
    {XtNfaceMultipleLabelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(face_multiple_label_child), XtRWidget, (XtPointer) NULL},
    {XtNfaceScrolledListChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(face_scrolled_list_child), XtRWidget, (XtPointer) NULL},
    {XtNsizeLabelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(size_label_child), XtRWidget, (XtPointer) NULL},
    {XtNsizeTextFieldChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(size_text_field_child), XtRWidget, (XtPointer) NULL},
    {XtNsizeOptionMenuChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(size_option_menu_child), XtRWidget, (XtPointer) NULL},
    {XtNsizeMultipleLabelChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(size_multiple_label_child), XtRWidget, (XtPointer) NULL},
    {XtNpreviewButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(preview_button_child), XtRWidget, (XtPointer) NULL},
    {XtNsamplerButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(sampler_button_child), XtRWidget, (XtPointer) NULL},
    {XtNseparatorChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(separator_child), XtRWidget, (XtPointer) NULL},
    {XtNokButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(ok_button_child), XtRWidget, (XtPointer) NULL},
    {XtNapplyButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(apply_button_child), XtRWidget, (XtPointer) NULL},
    {XtNresetButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(reset_button_child), XtRWidget, (XtPointer) NULL},
    {XtNcancelButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(cancel_button_child), XtRWidget, (XtPointer) NULL},
    {XtNmultipleMasterButtonChild, XtCReadOnly, XtRWidget, sizeof(Widget),
	Offset(multiple_master_button_child), XtRWidget, (XtPointer) NULL}
};

/* Forward declarations */

static Boolean ChangeBlends(Widget w, String base_name, String blend_name, FSBBlendAction action, int *axis_values, float *axis_percents);
static Boolean DownloadFontName(Widget w, String name);
static Boolean MatchFontFace(Widget w, String old_face, String new_family, String *new_face);
static Boolean SetValues(Widget old, Widget req, Widget new, ArgList args, Cardinal *num_args);
static Boolean Verify(FontSelectionBoxWidget fsb, FSBValidateCallbackRec *cb, String afm, Boolean doIt);
static String FindAFM(Widget w, String name);
static String FindFontFile(Widget w, String name);
static XtGeometryResult GeometryManager(Widget w, XtWidgetGeometry *desired, XtWidgetGeometry *allowed);
static void ChangeManaged(Widget w);
static void ClassInitialize(void);
static void ClassPartInitialize(WidgetClass widget_class);
static void Destroy(Widget widget);
static void DisplayFontFamilies(FontSelectionBoxWidget fsb);
static void FontFamilyFaceBlendToName(Widget w, String family, String face, String blend, String *font_name);
static void FontFamilyFaceToName(Widget w, String family, String face, String *font_name);
static void FontNameToFamilyFace(Widget w, String font_name, String *family, String *face);
static void FontNameToFamilyFaceBlend(Widget w, String font_name, String *family, String *face, String *blend);
static void FreeFontRec(FontRec *f);
static void GetBlendInfo(Widget w, String name, int *num_axes_return, int *num_designs_return, String **axis_names_return, float **blend_positions_return, int **blend_map_count_return, int **blend_design_coords_return, float **blend_normalized_coords_return);
static void GetBlendList(Widget w, String name, int *count_return, String **blend_return, String **font_name_return, float **axis_values_return);
static void GetFaceList(Widget w, String family, int *count, String **face_list, String **font_list);
static void GetFamilyList(Widget w, int *count, String **list);
static void GetTextDimensions(Widget w, String text, String font, double size, double x, double y, float *dx, float *dy, float *left, float *right, float *top, float *bottom);
static void Initialize(Widget request, Widget new, ArgList args, Cardinal *num_args);
static void ReadBlends(FontSelectionBoxWidget fsb);
static void RefreshFontList(Widget w);
static void Resize(Widget widget);
static void SetFontFamilyFace(Widget w, String family, String face, Bool family_multiple, Bool face_multiple);
static void SetFontFamilyFaceBlend(Widget w, String family, String face, String blend, Bool family_multiple, Bool face_multiple);
static void SetFontName(Widget w, String name, Bool name_multiple);
static void SetFontSize(Widget w, double size, Bool size_multiple);
static void SetUpCurrentSelections(FontSelectionBoxWidget fsb);
static void UndefUnusedFonts(Widget w);
static void WriteBlends(FontSelectionBoxWidget fsb);

FontSelectionBoxClassRec fontSelectionBoxClassRec = {
    /* Core class part */
  {
    /* superclass	     */	(WidgetClass) &xmManagerClassRec,
    /* class_name	     */ "FontSelectionBox",
    /* widget_size	     */ sizeof(FontSelectionBoxRec),
    /* class_initialize      */ ClassInitialize,
    /* class_part_initialize */ ClassPartInitialize,
    /* class_inited          */	False,
    /* initialize	     */	Initialize,
    /* initialize_hook       */	NULL,
    /* realize		     */	XtInheritRealize,
    /* actions		     */	NULL,
    /* num_actions	     */	0,
    /* resources	     */	resources,
    /* num_resources	     */	XtNumber(resources),
    /* xrm_class	     */	NULLQUARK,
    /* compress_motion	     */	True,
    /* compress_exposure     */	XtExposeCompressMultiple,
    /* compress_enterleave   */	True,
    /* visible_interest	     */	False,
    /* destroy		     */	Destroy,
    /* resize		     */	Resize,
    /* expose		     */	NULL,
    /* set_values	     */	SetValues,
    /* set_values_hook       */	NULL,			
    /* set_values_almost     */	XtInheritSetValuesAlmost,  
    /* get_values_hook       */	NULL,			
    /* accept_focus	     */	NULL,
    /* version		     */	XtVersion,
    /* callback offsets      */	NULL,
    /* tm_table              */	NULL,
    /* query_geometry	     */	XtInheritQueryGeometry,
    /* display_accelerator   */	NULL,
    /* extension	     */	NULL,
  },
   /* Composite class part */
  {
    /* geometry_manager	     */	GeometryManager,
    /* change_managed	     */	ChangeManaged,
    /* insert_child	     */	XtInheritInsertChild,
    /* delete_child	     */	XtInheritDeleteChild,
    /* extension	     */	NULL,
  },
   /* Constraint class part */
  {
    /* resources	     */ NULL,
    /* num_resources	     */ 0,
    /* constraint_size	     */ 0,
    /* initialize	     */ NULL,
    /* destroy		     */ NULL,
    /* set_values	     */ NULL,
    /* extension	     */ NULL,
  },
   /* Manager class part */
  {
    /* translations	     */ XtInheritTranslations,
    /* syn_resources	     */ NULL,
    /* num_syn_resources     */ 0,
    /* syn_constraint_resources */ NULL,
    /* num_syn_constraint_resources */ 0,
    /* parent_process	     */ XmInheritParentProcess,
    /* extension	     */ NULL,
  },
   /* FontSelectionBox class part */
  {
    /* set_font_name	     */	SetFontName,
    /* set_font_family_face  */	SetFontFamilyFace,
    /* set_font_size	     */	SetFontSize,
    /* refresh_font_list     */ RefreshFontList,
    /* get_family_list	     */	GetFamilyList,
    /* get_face_list	     */	GetFaceList,
    /* undef_unused_fonts    */ UndefUnusedFonts,
    /* download_font_name    */ DownloadFontName,
    /* match_font_face	     */ MatchFontFace,
    /* font_name_to_family_face */ FontNameToFamilyFace,
    /* font_family_face_to_name */ FontFamilyFaceToName,
    /* find_afm		     */ FindAFM,
    /* find_font_file	     */ FindFontFile,
    /* get_text_dimensions   */ GetTextDimensions,			
    /* set_font_family_face_blend     */ SetFontFamilyFaceBlend,
    /* font_name_to_family_face_blend */ FontNameToFamilyFaceBlend,
    /* font_family_face_blend_to_name */ FontFamilyFaceBlendToName,
    /* get_blend_list        */ GetBlendList,
    /* get_blend_info        */ GetBlendInfo,
    /* change_blends	     */ ChangeBlends,
    /* extension	     */	NULL,
  }
};

WidgetClass fontSelectionBoxWidgetClass =
	(WidgetClass) &fontSelectionBoxClassRec;

/* ARGSUSED */

static Boolean CvtStringToFloatList(
    Display *dpy,
    XrmValuePtr args,
    Cardinal *num_args,
    XrmValuePtr from,
    XrmValuePtr to,
    XtPointer *data)
{
    register int i, count = 1;
    register char *ch, *start = from->addr;
    static float *list;
    char save;

    if (*num_args != 0) {	/* Check for correct number */
	XtAppErrorMsg(XtDisplayToApplicationContext(dpy),
	       "cvtStringToFloatList", "wrongParameters",
	       "XtToolkitError",
	       "String to integer list conversion needs no extra arguments",
	       (String *) NULL, (Cardinal *) NULL);
    }

    if (to->addr != NULL && to->size < sizeof(int *)) {
	to->size = sizeof(int *);
	return False;
    }
    if (start == NULL || *start == '\0') list = NULL;
    else {
	for (ch = start; *ch != '\0'; ch++) {    /* Count floats */
	    if (!isdigit(*ch) && *ch != '.' && *ch != ',') {
		XtDisplayStringConversionWarning(dpy, from->addr, "FloatList");
		return False;
	    }
	    if (*ch == ',') count++;
	}
	list = (float *) XtCalloc(count+1, sizeof(float));

	for (i = 0; i < count; i++) {
	    for (ch = start; *ch != ',' && *ch != '\0'; ch++) {}
	    save = *ch;
	    *ch = '\0';
	    list[i] = atof(start);
	    *ch = save;
	    start = ch + 1;
	}
    }
    if (to->addr == NULL) to->addr = (caddr_t) &list;
    else *(float **) to->addr = list;
    to->size = sizeof(int *);
    return True;
}

/* ARGSUSED */

static void FloatListDestructor(
    XtAppContext app,
    XrmValuePtr to,
    XtPointer converter_data,
    XrmValuePtr args,
    Cardinal *num_args)
{
    float *list = (float *) to->addr;

    if (list == NULL) return;
    XtFree((XtPointer) list);
}

XmString _FSBCreateSharedCS(String str, Widget w)
{
    XrmValue src, dst;
    XmString result;

    src.addr = str;
    src.size = strlen(str);

    dst.addr = (caddr_t) &result;
    dst.size = sizeof(result);

    if (XtConvertAndStore(w, XtRString, &src, XmRXmString, &dst)) {
	return result;
    } else return NULL;
}
 
static Boolean ScanFloat(char *src, float *f, char **past)
{
    char buf[20], *ch;
    int countDecimals;

    ch = buf;
    countDecimals = 0;
    while (*src == '.' || isdigit(*src)) {
	if (*src == '.') {
	    if (countDecimals) return False;
	    else countDecimals++;
	}
	*ch++ = *src++;
    }
    if (ch == buf) return False;
    *ch++ = '\0';
    *f = atof(buf);
    *past = src;
    return True;
}

static Boolean ScanInt(char *src, int *i, char **past)
{
    char buf[20], *ch;

    ch = buf;
    while (isdigit(*src)) *ch++ = *src++;
    if (ch == buf) return False;
    *ch++ = '\0';
    *i = atoi(buf);
    *past = src;
    return True;
}

static void ClassInitialize(void)
{
    /* Register a converter for string to int list */

    XtSetTypeConverter(XtRString, XtRFloatList,
	    CvtStringToFloatList, (XtConvertArgList) NULL, 0,
	    XtCacheAll | XtCacheRefCount, FloatListDestructor);

    CSempty = UnsharedCS("");
}

static void ClassPartInitialize(WidgetClass widget_class)
{
    register FontSelectionBoxWidgetClass wc =
	    (FontSelectionBoxWidgetClass) widget_class;
    FontSelectionBoxWidgetClass super =
	    (FontSelectionBoxWidgetClass) wc->core_class.superclass;

    if (wc->fsb_class.set_font_name == InheritSetFontName) {
	wc->fsb_class.set_font_name = super->fsb_class.set_font_name;
    }
    if (wc->fsb_class.set_font_family_face == InheritSetFontFamilyFace) {
	wc->fsb_class.set_font_family_face =
		super->fsb_class.set_font_family_face;
    }
    if (wc->fsb_class.set_font_size == InheritSetFontSize) {
	wc->fsb_class.set_font_size = super->fsb_class.set_font_size;
    }
    if (wc->fsb_class.refresh_font_list == InheritRefreshFontList) {
	wc->fsb_class.refresh_font_list = super->fsb_class.refresh_font_list;
    }
    if (wc->fsb_class.get_family_list == InheritGetFamilyList) {
	wc->fsb_class.get_family_list = super->fsb_class.get_family_list;
    }
    if (wc->fsb_class.get_face_list == InheritGetFaceList) {
	wc->fsb_class.get_face_list = super->fsb_class.get_face_list;
    }
    if (wc->fsb_class.undef_unused_fonts == InheritUndefUnusedFonts) {
	wc->fsb_class.undef_unused_fonts = super->fsb_class.undef_unused_fonts;
    }
    if (wc->fsb_class.download_font_name == InheritDownloadFontName) {
	wc->fsb_class.download_font_name = super->fsb_class.download_font_name;
    }
    if (wc->fsb_class.match_font_face == InheritMatchFontFace) {
	wc->fsb_class.match_font_face = super->fsb_class.match_font_face;
    }
    if (wc->fsb_class.font_name_to_family_face ==
						InheritFontNameToFamilyFace) {
	wc->fsb_class.font_name_to_family_face =
		super->fsb_class.font_name_to_family_face;
    }
    if (wc->fsb_class.font_family_face_to_name ==
						InheritFontFamilyFaceToName) {
	wc->fsb_class.font_family_face_to_name =
		super->fsb_class.font_family_face_to_name;
    }
    if (wc->fsb_class.find_afm == InheritFindAFM) {
	wc->fsb_class.find_afm = super->fsb_class.find_afm;
    }
    if (wc->fsb_class.find_font_file == InheritFindFontFile) {
	wc->fsb_class.find_font_file = super->fsb_class.find_font_file;
    }
    if (wc->fsb_class.get_text_dimensions == InheritGetTextDimensions) {
	wc->fsb_class.get_text_dimensions =
		super->fsb_class.get_text_dimensions;
    }
    if (wc->fsb_class.set_font_family_face_blend ==
	InheritSetFontFamilyFaceBlend) {
	wc->fsb_class.set_font_family_face_blend =
		super->fsb_class.set_font_family_face_blend;
    }
    if (wc->fsb_class.font_name_to_family_face_blend ==
	InheritFontNameToFamilyFaceBlend) {
	wc->fsb_class.font_name_to_family_face_blend =
		super->fsb_class.font_name_to_family_face_blend;
    }
    if (wc->fsb_class.font_family_face_blend_to_name ==
	InheritFontFamilyFaceBlendToName) {
	wc->fsb_class.font_family_face_blend_to_name =
		super->fsb_class.font_family_face_blend_to_name;
    }
    if (wc->fsb_class.get_blend_list == InheritGetBlendList) {
	wc->fsb_class.get_blend_list =
		super->fsb_class.get_blend_list;
    }
    if (wc->fsb_class.get_blend_info == InheritGetBlendInfo) {
	wc->fsb_class.get_blend_info =
		super->fsb_class.get_blend_info;
    }
    if (wc->fsb_class.change_blends == InheritChangeBlends) {
	wc->fsb_class.change_blends =
		super->fsb_class.change_blends;
    }
}

static String bugFamilies[] = {
    "Berkeley", "CaslonFiveForty", "CaslonThree", "GaramondThree",
    "Music", "TimesTen", NULL
};

static String fixedFamilies[] = {
    "ITC Berkeley Oldstyle", "Caslon 540", "Caslon 3", "Garamond 3",
    "Sonata", "Times 10", NULL
};

static String missingFoundries[] = {
    "Berthold ", "ITC ", "Linotype ", NULL
};

static int missingFoundryLen[] = {
    9, 4, 9, 0
};

/* I wish we didn't have to do this! */

static void MungeFontNames(
    String name, String family, String fullname, String weight,
    String *familyReturn, String *fullnameReturn, String *faceReturn)
{
    register char *src, *dst, prev;
    char buf[256];
    int digits = 0;
    int i, diff;
    static Bool inited = False;
    static String FetteFrakturDfr, LinotextDfr;

    /* Don't make bugFamilies canonical; we'd have to make the initial
       family canonical to do anything with it and there's no point in that */

    if (!inited) {
	for (i = 0; fixedFamilies[i] != NULL; i++) {
	    fixedFamilies[i] = Canonical(fixedFamilies[i]);
	}
	FetteFrakturDfr = Canonical("FetteFraktur-Dfr");
	LinotextDfr = Canonical("Linotext-Dfr");
	inited = True;
    }

    /* Copy the fullname into buf, enforcing one space between words.
       Eliminate leading digits and spaces, ignore asterisks, if the
       full name ends with 5 digits strip them, and replace periods that
       aren't followed by a space with a space.  If leading digits are
       followed by " pt " skip that too. */

    dst = buf;
    prev = ' ';
    src = fullname; 
    while (isdigit(*src)) src++;
    while (*src == ' ' || *src == '\t') src++;
    if (strncmp(src, "pt ", 3) == 0) src += 3;
    else if (strncmp(src, "pt. ", 4) == 0) src += 4;

    while (*src != '\0') {
	if (*src == '*') {
	    src++;
	    continue;
	}

	if (*src == '.') {
	    if (*(src+1) != ' ') {
		prev = *dst++ = ' ';
	    } else prev = *dst++ = '.';
	    src++;
	    continue;
	}

	if (isdigit(*src)) digits++;
	else digits = 0;

	if (isupper(*src)) {
	    if (prev != ' ' && (islower(*(src+1)) || islower(prev))) {
		*dst++ = ' ';
		prev = *dst++ = *src++;
	    } else prev = *dst++ = *src++;

	} else if (*src == ' ' || *src == '\t') {
	    if (prev == ' ') {
		src++;
		continue;
	    }
	    prev = *dst++ = ' ';
	    src++;

	} else prev = *dst++ = *src++;
    }

    if (digits == 5) {
	dst -= 5;
    }
    if (dst > buf && *(dst-1) == ' ') dst--;

    *dst = '\0';

    /* Stupid Fette Fraktur */

    if (name == FetteFrakturDfr) {
	strcat(buf, " Black Dfr");
    } else if (name == LinotextDfr) {
	strcat(buf, " Dfr");
    }

    if (strncmp(fullname, "pt ", 3) == 0) {
	src = buf + 2;
	while (*++src != '\0') *(src-3) = *src;
	*(src-3) = '\0';
    }
    *fullnameReturn = XtNewString(buf);

    /* From here on fullname should not be used */

    /* Done with the full name; now onto the family */

    for (i = 0; bugFamilies[i] != NULL; i++) {
	diff = strcmp(family, bugFamilies[i]);
	if (diff < 0) break;
	if (diff == 0) {
	    *familyReturn = fixedFamilies[i];
	    goto FAMILY_DONE;
	}
    }

    /* Copy the family into buf, enforcing one space between words */

    dst = buf;
    prev = ' ';
    src = family; 

    while (*src != '\0') {
	if (isupper(*src)) {
	    if (prev != ' ' && (islower(*(src+1)) || islower(prev))) {
		*dst++ = ' ';
		prev = *dst++ = *src++;
	    } else prev = *dst++ = *src++;

	} else if (*src == ' ' || *src == '\t') {
	    if (prev == ' ') {
		src++;
		continue;
	    }
	    prev = *dst++ = ' ';
	    src++;

	} else prev = *dst++ = *src++;
    }

    if (dst > buf && *(dst-1) == ' ') dst--;
    *dst = '\0';

    /* Compensate for fonts with foundries in the full name but not the
       family name by adding to the family name */
 
    for (i = 0; missingFoundries[i] != NULL; i++) {
	diff = strncmp(*fullnameReturn, missingFoundries[i],
		       missingFoundryLen[i]);
	if (diff > 0) continue;
	if (diff == 0 && strncmp(buf, missingFoundries[i],
		       missingFoundryLen[i] != 0)) {
	    while (dst >= buf) {
		*(dst+missingFoundryLen[i]) = *dst;
		dst--;
	    }
	    strncpy(buf, missingFoundries[i], missingFoundryLen[i]);
	}
	break;
    }

    /* From here on dst no longer points to the end of the buffer */

    /* Stupid Helvetica Rounded! */

    if (strncmp(*fullnameReturn, "Helvetica Rounded ", 18) == 0) {
	strcat(buf, " Rounded");
    }

    *familyReturn = Canonical(buf);

    /* From here on family should not be used */

FAMILY_DONE:

    /* Now to find the face in all this */

    src = *fullnameReturn;
    dst = *familyReturn;
    while (*dst == *src && *dst != '\0') {
        src++;
        dst++;
    }
    if (*src == ' ') src++;

    if (*src != '\0') *faceReturn = Canonical(src);
    else if (*weight != '\0') {
	/* Handle Multiple Master fonts */
	if (strcmp(weight, "All") == 0) *faceReturn = Canonical("Roman");
	else {
	    if (islower(weight[0])) weight[0] = toupper(weight[0]);
	    *faceReturn = Canonical(weight);
	}
    } else *faceReturn = Canonical("Medium");
}

static String strip[] = {
    "Adobe ", "Bauer ", "Berthold ", "ITC ", "Linotype ",
    "New ", "Simoncini ", "Stempel ", NULL};

static int striplen[] = {6, 6, 9, 4, 9, 4, 10, 8, 0};

#define STEMPELINDEX 7

static Boolean CreateSortKey(String family, String key)
{
    char newkey[256];
    int len = strlen(family);
    register int i, diff;

    if (family[len-2] == 'P' && family[len-1] == 'i') {
	key[0] = 'P';
	key[1] = 'i';
	key[2] = ' ';
	strcpy(key+3, family);
	key[len] = '\0';
	return True;
    }

    for (i = 0; strip[i] != NULL; i++) {
	diff = strncmp(family, strip[i], striplen[i]);
	if (diff < 0) break;
	if (diff == 0) {
	    if (i == STEMPELINDEX) {
		if (strcmp(family, "Stempel Schneidler") == 0) break;
	    }
	    strcpy(key, family + striplen[i]);
	    key[len - striplen[i]] = ' ';
	    strcpy(key + len - striplen[i] + 1, strip[i]);
	    key[len] = '\0';
	    if (CreateSortKey(key, newkey)) strcpy(key, newkey);
	    return True;
	}
    }
    strcpy(key, family);
    return False;
}

#define SKIP_SPACE(buf) while (*buf == ' ' || *buf == '\t') buf++;

static int CountAxes(char *buf)
{
    int count = 0;

    while (*buf != '\0') {
	SKIP_SPACE(buf)
	if (*buf != '/') return 0;
	buf++;
	count++;
	if (*buf == ' ' || *buf == '\t' || *buf == '\0') return 0;
	while (*buf != ' ' && *buf != '\t' && *buf != '\0') buf++;
    }
    return count;
}

static Boolean ParseBlendPositions(
    char *buf,
    float *blendPos,
    int *axes, int *designs)
{
    int i, j = 0;
    float f;

    *designs = 0;

    while (*buf != '\0') {
	SKIP_SPACE(buf)
	if (*buf++ != '[') return True;

	/* Now there should be *axes positive floats, separated by space */
	SKIP_SPACE(buf)
	for (i = 0; i < *axes; i++) {
	    if (!ScanFloat(buf, &f, &buf)) return True;
	    blendPos[j++] = f;
	    SKIP_SPACE(buf)
	}
	if (*buf++ != ']') return True;
	(*designs)++;
    }
    return False;
}

static Boolean ParseBlendMap(
    char *buf,
    int *breakCount,
    int *blendBreak,
    float *blendBreakValue,
    int *axes)
{
    int i, j = 0;
    int n;
    float f;

    /* OK.  What we expect to see here is *axes arrays.  Each one contains at
       least 2 and no more than 12 subarrays, each of which contains 2 values,
       an int and a float */

    for (i = 0; i < *axes; i++) {

	breakCount[i] = 0;

	SKIP_SPACE(buf)
	if (*buf++ != '[') return True;
	SKIP_SPACE(buf)

	while (*buf == '[') {
	    buf++;
	    SKIP_SPACE(buf)
	    /* Now there should be an integer */
	    if (!ScanInt(buf, &n, &buf)) return True;
	    blendBreak[j] = n;

	    SKIP_SPACE(buf)

	    /* Now there should be a float */
	    if (!ScanFloat(buf, &f, &buf)) return True;
	    blendBreakValue[j++] = f;
	    SKIP_SPACE(buf)

	    /* Nothing more in the array */
	    if (*buf++ != ']') return True;
	    SKIP_SPACE(buf)

	    breakCount[i]++;
	    if (breakCount[i] == 12 && *buf != ']') return True;
	}
	if (*buf++ != ']') return True;
    }
    SKIP_SPACE(buf)
    if (*buf != '\0') return True;
    return False;
}

static Boolean ParseAxisNames(
    int axes,
    char *buf,
    char *names[])
{
    int i = 0;

    /* We expect to see axes names, each optionally preceded with a / and
       separated by space */

    while (*buf != '\0') {
	SKIP_SPACE(buf)
	if (*buf == '/') buf++;
	names[i] = buf;
	while (*buf !=  ' ' && *buf != '\t' && *buf != '\0') buf++;
	if (buf != names[i]) i++;
	if (*buf != '\0') *buf++ = '\0';
	if (i >= axes) return True;
    }
    return False;
}	
#undef SKIP_SPACE

static void GetPSFontInfo(
    FontSelectionBoxWidget fsb,
    char *name,
    int *axes,
    int *designs,
    char *axisNames,
    float *blendPos,
    int *breakCount,
    int *blendBreak,
    float *blendBreakValue)
{
    int entries;
    char **names, **data;

    entries = ListPSResourceFiles(fsb->fsb.resource_path_override,
				  fsb->fsb.default_resource_path,
				  "FontAxes", name,
				  &names, &data);
    if (entries < 1) {
	*axes = 0;
	return;
    }
    *axes = CountAxes(data[0]);
    if (*axes == 0) return;
    strcpy(axisNames, data[0]);

    entries = ListPSResourceFiles(fsb->fsb.resource_path_override,
				  fsb->fsb.default_resource_path,
				  "FontBlendMap", name,
				  &names, &data);
    if (entries < 1) {
	*axes = 0;
	return;
    }
    if (ParseBlendMap(data[0], breakCount,
		      blendBreak, blendBreakValue, axes)) {
	*axes = 0;
	return;
    }

    entries = ListPSResourceFiles(fsb->fsb.resource_path_override,
				  fsb->fsb.default_resource_path,
				  "FontBlendPositions", name,
				  &names, &data);
    if (entries < 1) {
	*axes = 0;
	return;
    }
    if (ParseBlendPositions(data[0], blendPos, axes, designs)) {
	*axes = 0;
	return;
    }
}

static void AddFontRecord(
    FontSelectionBoxWidget fsb,
    int serverNum,
    String name, String family, String fullname, String weight,
    Boolean resident)
{
    FontFamilyRec *ff;
    FontRec *f;
    String familyReturn, fullnameReturn, faceReturn;
    char axisNameBuf[256];
    char *axisName[MAX_AXES];
    int blendedFont, undefineIt, brokenFont;
    int axes, designs, breakCount[MAX_AXES],
	    blendBreak[12 * MAX_AXES];
    float blendBreakValue[12 * MAX_AXES], blendPos[MAX_AXES * MAX_BLENDS];
    char key[256];
    int i, j, k, n;

    name = Canonical(name);

    /* First see if it's there already */

    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == name) {
		if (!f->resident && resident) f->resident = True;
		return;
	    }
	}
    }

    /* We believe that names gotten from PS resource files have been
       pre-munged, so no need to do it again */

    if (resident) {
	/* Have to get the info from the server */
	_DPSFGetFontInfo(fsb->fsb.context, serverNum, fsb->fsb.old_server,
			 family, fullname,
			 weight, &blendedFont, &undefineIt, &brokenFont);

	if (brokenFont) return;

	/* Deal with fonts that don't have useful information */

	if (family[0] == '\0') {
	    if (fullname[0] == '\0') {
		strcpy(family, name);
		strcpy(fullname, name);
	    } else strcpy(family, fullname);
	} else if (fullname[0] == '\0') strcpy(fullname, family);

	MungeFontNames(name, family, fullname, weight,
		       &familyReturn, &fullnameReturn, &faceReturn);
	if (blendedFont) {
	    _DPSFGetBlendedFontInfo(fsb->fsb.context, serverNum,
				    undefineIt, fsb->fsb.old_server,
				    &axes, &designs, axisNameBuf,
				    blendPos, breakCount, blendBreak,
				    blendBreakValue, &brokenFont);
	    if (brokenFont) axes = 0;
	} else axes = 0;

    } else {
	familyReturn = Canonical(family);
	fullnameReturn = XtNewString(fullname);
	faceReturn = Canonical(weight);
	GetPSFontInfo(fsb, name, &axes, &designs, axisNameBuf, blendPos,
		      breakCount, blendBreak, blendBreakValue);
    }

    /* We didn't get an exact match, go for family match */

    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	if (ff->family_name == familyReturn) break;
    }

    if (ff == NULL) {
	ff = (FontFamilyRec *) XtMalloc(sizeof(FontFamilyRec));
	ff->next = fsb->fsb.known_families;
	ff->family_name = familyReturn;
	ff->fonts = NULL;
	ff->font_count = 0;
	ff->blend_count = 0;
	if (fsb->fsb.typographic_sort) {
	    (void) CreateSortKey(familyReturn, key);
	    ff->sort_key = XtNewString(key);
	} else ff->sort_key = ff->family_name;
	fsb->fsb.known_families = ff;
	fsb->fsb.family_count++;
    }

    f = (FontRec *) XtMalloc(sizeof(FontRec));
    f->next = ff->fonts;
    f->font_name = name;
    f->full_name = fullnameReturn;
    f->resident = resident;
    f->temp_resident = False;
    f->in_font_creator = False;
    f->pending_delete_next = NULL;
    f->face_name = faceReturn;
    f->CS_face_name = CS(f->face_name, (Widget) fsb);
    f->blend_count = 0;

    if (axes != 0 && ParseAxisNames(axes, axisNameBuf, axisName)) {
	BlendDataRec *b;
	
	f->blend_data = b = XtNew(BlendDataRec);
	b->num_axes = axes;
	b->num_designs = designs;
	k = 0;

	for (i = 0; i < axes; i++) {
	    b->internal_points[i] = breakCount[i] - 2;
	    if (b->internal_points[i] <= 0) {
		b->internal_break[i] = NULL;
		b->internal_value[i] = NULL;
		b->internal_points[i] = 0;
	    } else {
		b->internal_break[i] = (int *)
			XtMalloc(b->internal_points[i] * sizeof(int));
		b->internal_value[i] = (float *)
			XtMalloc(b->internal_points[i] * sizeof(float));
	    }

	    n = 0;
	    for (j = 0; j < breakCount[i]; j++) {
		if (blendBreakValue[k] == 0.0) b->min[i] = blendBreak[k];
		else if (blendBreakValue[k] == 1.0) b->max[i] = blendBreak[k];
		else {
		    b->internal_break[i][n] = blendBreak[k];
		    b->internal_value[i][n++] = blendBreakValue[k];
		}
		k++;
	    }
	    b->name[i] = Canonical(axisName[i]);
	}

	b->design_positions =
		(float *) XtMalloc(axes * designs * sizeof(float));
	for (i = 0; i < axes * designs; i++) {
	    b->design_positions[i] = blendPos[i];
	}
	b->blends = NULL;
    } else f->blend_data = NULL;

    ff->fonts = f;
    ff->font_count++;
}

static void SortFontNames(FontFamilyRec *ff)
{
    FontRec *f, *highest, **prev, **highestPrev;
    FontRec *newFontList = NULL;

    while (ff->fonts != NULL) {
	prev = highestPrev = &ff->fonts;
	highest = ff->fonts;

	for (f = ff->fonts->next; f != NULL; f = f->next) {
	    prev = &(*prev)->next;
	    if (strcmp(f->face_name, highest->face_name) > 0) {
		highest = f;
		highestPrev = prev;
	    }
	}

	*highestPrev = highest->next;
	highest->next = newFontList;
	newFontList = highest;
    }
    ff->fonts = newFontList;
}

static void SortFontFamilies(FontSelectionBoxWidget fsb)
{
    FontFamilyRec *ff, *highest, **prev, **highestPrev;
    FontFamilyRec *newFamilyList = NULL;

    while (fsb->fsb.known_families != NULL) {
	prev = highestPrev = &fsb->fsb.known_families;
	highest = fsb->fsb.known_families;

	for (ff = fsb->fsb.known_families->next; ff != NULL; ff = ff->next) {
	    prev = &(*prev)->next;
	    if (strcmp(ff->sort_key, highest->sort_key) > 0) {
		highest = ff;
		highestPrev = prev;
	    }
	}

	*highestPrev = highest->next;
	highest->next = newFamilyList;
	newFamilyList = highest;
	SortFontNames(highest);
	if (fsb->fsb.typographic_sort) XtFree(highest->sort_key);
	highest->sort_key = NULL;
    }
    fsb->fsb.known_families = newFamilyList;
}

static void AddFamily(
    FontSelectionBoxWidget fsb,
    char *family, char *fonts, char *weight, char *fullname, char *name)
{
    int j;
    char *ch;

    ch = fonts;
    while (*ch != '\0') {
	j = 0;
	while (1) {
            if (*ch == '\\' && (*(ch+1) == '\\' || *(ch+1) == ',')) {
		ch++;
		weight[j++] = *ch++;
	    } else if (*ch == '\0' || *ch == ',') {
		weight[j] = '\0';
		break;
	    } else weight[j++] = *ch++;
	}
	if (*ch == ',') {
	    j = 0;
	    ch++;
	    while (1) {
		if (*ch == '\\' && (*(ch+1) == '\\' || *(ch+1) == ',')) {
		    ch++;
		    name[j++] = *ch++;
		} else if (*ch == '\0' || *ch == ',') {
		    name[j] = '\0';
		    break;
	        } else name[j++] = *ch++;
	    }
	    strcpy(fullname, family);
	    strcat(fullname, " ");
	    strcat(fullname, weight);
	    AddFontRecord(fsb, 0, name, family, fullname, weight, False);
	    if (*ch == ',') ch++;
	}
    }
}

static void GetFontNames(FontSelectionBoxWidget fsb)
{
    int i;
    char name[256], family[256], fullname[256], weight[256];
    char *buffer, *ch, *start;
    int fontCount, totalLength;
    char **loadableFamilies = NULL, **loadableFamilyFonts = NULL;
    
    fsb->fsb.family_count = 0;

    fontCount = ListPSResourceFiles(fsb->fsb.resource_path_override,
				    fsb->fsb.default_resource_path,
				    PSResFontFamily, NULL,
				    &loadableFamilies, &loadableFamilyFonts);
    for (i = 0; i < fontCount; i++) {
        AddFamily(fsb, loadableFamilies[i], loadableFamilyFonts[i],
		  weight, fullname, name);
    }

    XtFree((XtPointer) loadableFamilies);
    XtFree((XtPointer) loadableFamilyFonts);
    FreePSResourceStorage(False);

    if (fsb->fsb.get_server_fonts) {
	_DPSFEnumFonts(fsb->fsb.context, &fontCount, &totalLength);

	buffer = XtMalloc(totalLength);
	_DPSFGetAllFontNames(fsb->fsb.context, fontCount, totalLength, buffer);
	ch = start = buffer;
	for (i = 0; i < fontCount; i++) {
	    while (*ch != ' ') ch++;
	    *ch = '\0';
	    AddFontRecord(fsb, i, start, family, fullname, weight, True);
	    start = ch+1;
	}
	XtFree(buffer);
    }

    _DPSFFreeFontInfo(fsb->fsb.context);
    SortFontFamilies(fsb);
    ReadBlends(fsb);
}

static void SensitizeReset(FontSelectionBoxWidget fsb)
{
    XtSetSensitive(fsb->fsb.reset_button_child, True);
}

static void DesensitizeReset(FontSelectionBoxWidget fsb)
{
    XtSetSensitive(fsb->fsb.reset_button_child, False);
}

static void ManageFamilyMultiple(FontSelectionBoxWidget fsb)
{
    XtManageChild(fsb->fsb.family_multiple_label_child);

    XtVaSetValues(XtParent(fsb->fsb.family_scrolled_list_child),
		  XmNtopWidget, fsb->fsb.family_multiple_label_child, NULL);
}

static void ManageFaceMultiple(FontSelectionBoxWidget fsb)
{
    XtManageChild(fsb->fsb.face_multiple_label_child);

    XtVaSetValues(XtParent(fsb->fsb.face_scrolled_list_child),
		  XmNtopWidget, fsb->fsb.face_multiple_label_child, NULL);
}

static void ManageMultipleMaster(FontSelectionBoxWidget fsb)
{
    XtManageChild(fsb->fsb.multiple_master_button_child);

    XtVaSetValues(XtParent(fsb->fsb.face_scrolled_list_child),
		  XmNbottomWidget, fsb->fsb.multiple_master_button_child,
		  NULL);
}

static void ManageSizeMultiple(FontSelectionBoxWidget fsb)
{
    XtManageChild(fsb->fsb.size_multiple_label_child);
}

static void UnmanageFamilyMultiple(FontSelectionBoxWidget fsb)
{
    XtVaSetValues(XtParent(fsb->fsb.family_scrolled_list_child),
		  XmNtopWidget, fsb->fsb.family_label_child, NULL);

    XtUnmanageChild(fsb->fsb.family_multiple_label_child);
}

static void UnmanageFaceMultiple(FontSelectionBoxWidget fsb)
{
    XtVaSetValues(XtParent(fsb->fsb.face_scrolled_list_child),
		  XmNtopWidget, fsb->fsb.face_label_child, NULL);

    XtUnmanageChild(fsb->fsb.face_multiple_label_child);
}

static void UnmanageMultipleMaster(FontSelectionBoxWidget fsb)
{
    XtUnmanageChild(fsb->fsb.multiple_master_button_child);

    XtVaSetValues(XtParent(fsb->fsb.face_scrolled_list_child),
		  XmNbottomWidget, fsb->fsb.size_text_field_child, NULL);
}

static void UnmanageSizeMultiple(FontSelectionBoxWidget fsb)
{
    XtUnmanageChild(fsb->fsb.size_multiple_label_child);
}

/* Callbacks for subwidgets */

static Boolean DownloadFont(
    FontSelectionBoxWidget fsb,
    String name,
    DPSContext ctxt,
    Boolean make_shared)
{
    int count;
    char **names, **files;
    FILE *f;
#define BUFLEN 256
    char buf[BUFLEN];
    static char eobuf[] = "\n$Adobe$DPS$Lib$Dict /downloadSuccess true put\n\
stop\n\
Magic end of data line )))))))))) 99#2 2#99 <xyz> // 7gsad,32h4ghNmndFgj2\n";
    int currentShared, ok;

    /* Assume context is correct */

    count = ListPSResourceFiles(fsb->fsb.resource_path_override,
				fsb->fsb.default_resource_path,
				PSResFontOutline, name,
				&names, &files);
    if (count == 0) return False;

    f = fopen(files[0], "r");
    if (f == NULL) return False;

    /* A bug in 1006.9 and earlier servers prevents the more robust
       downloading method from working reliably. */

    if (fsb->fsb.old_server) {
	DPSPrintf(ctxt, "\ncurrentshared %s setshared\n",
		  (make_shared ? "true" : "false"));
	while (fgets(buf, BUFLEN, f) != NULL) {
	    DPSWritePostScript(ctxt, buf, strlen(buf));
	}
	DPSWritePostScript(ctxt, "\nsetshared\n", 11);
	ok = True;

    } else {
	_DPSFPrepareToDownload(ctxt, make_shared, &currentShared);
	DPSWriteData(ctxt, "\nexec\n", 6);

	while (fgets(buf, BUFLEN, f) != NULL) {
	    DPSWriteData(ctxt, buf, strlen(buf));
	}

	/* This marks the end of the data stream */
	DPSWriteData(ctxt, eobuf, strlen(eobuf));

	/* Check the results of the download by getting the error status */
	_DPSFFinishDownload(ctxt, currentShared, &ok);
    }

    fclose (f);
    free(names);
    free(files);
    return ok;

#undef BUFLEN
}

static void UndefSomeUnusedFonts(
    FontSelectionBoxWidget fsb,
    Boolean all)
{
    FontRec *f, *nextf, **start;
    int i;

    if (!all
     && (Cardinal)fsb->fsb.pending_delete_count < fsb->fsb.max_pending_deletes) {
	return;
    }

    if (all) start = &fsb->fsb.pending_delete_font;
    else {
	/* Skip to the end of the ones we're keeping */
	f = fsb->fsb.pending_delete_font;
	for (i = 1; f != NULL && (Cardinal)i < fsb->fsb.max_pending_deletes; i++) {
	    f = f->pending_delete_next;
	}
	if (f == NULL) return;
	start = &f->pending_delete_next;
    }

    for (f = *start; f != NULL; f = nextf) {
	nextf = f->pending_delete_next;
	if (f == fsb->fsb.currently_previewed) {
	    start = &f->pending_delete_next;
	    continue;
	}
	*start = nextf;
	if (!f->resident && !f->in_font_creator) {
	    _DPSFUndefineFont(fsb->fsb.context, f->font_name,
			      fsb->fsb.old_server);
	}
	f->temp_resident = False;
	fsb->fsb.pending_delete_count--;	    
	f->pending_delete_next = NULL;
    }
}

static void UndefUnusedFonts(Widget w)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;

    UndefSomeUnusedFonts(fsb, True);
}

Boolean _FSBDownloadFontIfNecessary(
    FontRec *f,
    FontSelectionBoxWidget fsb)
{
    Boolean shared;

    if (!f->resident && !f->temp_resident) {

	shared = fsb->fsb.make_fonts_shared && !fsb->fsb.undef_unused_fonts;
	if (!fsb->fsb.get_server_fonts) {
	    int resident;
	    /* This font might already be there, so check before downloading */
	    _DPSFIsFontResident(fsb->fsb.context, f->font_name, &resident);
	    if (resident) {
		f->resident = True;
		return True;
	    }
	}
	if (!DownloadFont(fsb, f->font_name, fsb->fsb.context, shared)) {
	    _FSBFlushFont(fsb, f);
	    return False;
	}
	if (shared) f->resident = True;
	else f->temp_resident = True;

	if (f->pending_delete_next == NULL && fsb->fsb.undef_unused_fonts) {
	    f->pending_delete_next = fsb->fsb.pending_delete_font;
	    fsb->fsb.pending_delete_font = f;
	    fsb->fsb.pending_delete_count++;
	    UndefSomeUnusedFonts(fsb, False);
	}
    }
    return True;
}

static void DoPreview(
    FontSelectionBoxWidget fsb,
    Boolean override)
{
    int i, n;
    int *selectList, selectCount;
    float size;
    FontFamilyRec *ff = fsb->fsb.known_families;
    FontRec *f;
    BlendRec *b;
    char *chSize, *fontName;
    Dimension height;
    Cardinal depth;
    int bogusFont;

    if (!XtIsRealized(fsb)) return;

    XtVaGetValues(fsb->fsb.preview_child, XmNheight, &height,
		  XmNdepth, &depth, NULL);

    if (fsb->fsb.gstate == 0) {
	XDPSSetContextParameters(fsb->fsb.context, XtScreen(fsb), depth,
				 XtWindow(fsb->fsb.preview_child), height,
				 (XDPSStandardColormap *) NULL,
				 (XDPSStandardColormap *) NULL,
				 XDPSContextScreenDepth | XDPSContextDrawable |
				 XDPSContextRGBMap | XDPSContextGrayMap);
	XDPSCaptureContextGState(fsb->fsb.context, &fsb->fsb.gstate);
    } else XDPSSetContextGState(fsb->fsb.context, fsb->fsb.gstate);

    _DPSFClearWindow(fsb->fsb.context);

    if (override) {
	if (fsb->fsb.current_family_multiple ||
	    fsb->fsb.current_face_multiple ||
	    fsb->fsb.current_size_multiple) return;
	f = fsb->fsb.currently_previewed;
	size = fsb->fsb.currently_previewed_size;
	b = fsb->fsb.currently_previewed_blend;
    }

    if (!override || f == NULL || size == 0.0) {
	if (!XmListGetSelectedPos(fsb->fsb.family_scrolled_list_child,
				  &selectList, &selectCount)) return;
	if (selectCount == 0 ||
	    *selectList < 1 || *selectList > fsb->fsb.family_count) return;

	for (i = 1; i < *selectList; i++) ff = ff->next;
    
	XtFree((XtPointer) selectList);

	if (!XmListGetSelectedPos(fsb->fsb.face_scrolled_list_child,
				  &selectList, &selectCount)) return;
	if (selectCount == 0 ||
	    *selectList < 1 ||
	    *selectList > ff->font_count + ff->blend_count) return;

	f = ff->fonts;
	n = 0;
	while (1) {
	    n += f->blend_count + 1;
	    if (n >= *selectList) {
		n -= f->blend_count;
		if (n == *selectList) b = NULL;
		else for (b = f->blend_data->blends;
			  n < *selectList - 1; b = b->next) n++;
		break;
	    }
	    f = f->next;
	}
    
	XtFree((XtPointer) selectList);

	XtVaGetValues(fsb->fsb.size_text_field_child,
		      XmNvalue, &chSize, NULL); 

	if (chSize == NULL || *chSize == '\0') return;
	size = atof(chSize);
    }

    if (size <= 0.0) return;

    fsb->fsb.currently_previewed = f;
    fsb->fsb.currently_previewed_blend = b;
    fsb->fsb.currently_previewed_size = size;

    if (!_FSBDownloadFontIfNecessary(f, fsb)) return;

    if (b == NULL) fontName = f->font_name;
    else fontName = b->font_name;

    if (fsb->fsb.preview_string == NULL) {
	_DPSFPreviewString(fsb->fsb.context, fontName, size,
			   f->full_name, height, &bogusFont);
    } else _DPSFPreviewString(fsb->fsb.context, fontName, size,
			      fsb->fsb.preview_string, height, &bogusFont);
    if (bogusFont) {
	_FSBBogusFont(fsb, f);
    }
}

static void DoValueChangedCallback(FontSelectionBoxWidget fsb)
{
    String afm = NULL;
    FSBValidateCallbackRec cb;

    if (fsb->fsb.get_afm) {
	if (fsb->fsb.currently_selected_face == NULL) afm = NULL;
	else afm = FindAFM((Widget) fsb,
			   fsb->fsb.currently_selected_face->font_name);
    }

    (void) Verify(fsb, &cb, afm, False);
    cb.reason = FSBValueChanged;

    XtCallCallbackList((Widget) fsb, fsb->fsb.value_changed_callback, &cb);
}

static void ValueChanged(FontSelectionBoxWidget fsb)
{
    if (fsb->fsb.auto_preview) DoPreview(fsb, False);
    DoValueChangedCallback(fsb);
}

/* ARGSUSED */

static void PreviewText(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;
    XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) callData;

    if (!fsb->fsb.preview_fixed) {
	XSetWindowAttributes att;
	att.bit_gravity = ForgetGravity;
	XChangeWindowAttributes(XtDisplay(fsb),
				XtWindow(fsb->fsb.preview_child),
				CWBitGravity, &att);
	fsb->fsb.preview_fixed = True;
    }

    if (cb != NULL && cb->event->type == Expose &&
	cb->event->xexpose.count != 0) return;

    DoPreview(fsb, True);
}

/* ARGSUSED */

static void PreviewCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    DoPreview(fsb, False);
}

/* ARGSUSED */

static void DismissSamplerCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    fsb->fsb.show_sampler = False;
}

static void ShowSampler(FontSelectionBoxWidget fsb)
{
    int i;
    Arg args[2];
    Widget s;

    if (fsb->fsb.sampler == NULL) {
	FSBCreateSamplerCallbackRec cs;

	cs.sampler_shell = NULL;

	XtCallCallbackList((Widget) fsb, fsb->fsb.create_sampler_callback,
			   (XtPointer) &cs);

	if (cs.sampler_shell == NULL || cs.sampler == NULL) {
	    fsb->fsb.sampler =
		    XtCreatePopupShell("samplerShell",
				       transientShellWidgetClass,	
				       (Widget) fsb, (ArgList) NULL, 0);
	    i = 0;
	    XtSetArg(args[i], XtNfontSelectionBox, fsb);		i++;
	    s = XtCreateManagedWidget("sampler", fontSamplerWidgetClass,
				      fsb->fsb.sampler, args, i);
	    XtAddCallback(s, XtNdismissCallback,
			  DismissSamplerCallback, (XtPointer) fsb);
	} else {
	    fsb->fsb.sampler = cs.sampler_shell;
	    XtAddCallback(cs.sampler, XtNdismissCallback,
			  DismissSamplerCallback, (XtPointer) fsb);
	}
    }
    XtPopup(fsb->fsb.sampler, XtGrabNone);
    XRaiseWindow(XtDisplay(fsb->fsb.sampler), XtWindow(fsb->fsb.sampler));
    fsb->fsb.show_sampler = True;
}

static void ShowCreator(FontSelectionBoxWidget fsb)
{
    int i;
    Arg args[2];
    FSBCreateCreatorCallbackRec cc;

    if (fsb->fsb.creator == NULL) {

	cc.creator_shell = NULL;

	XtCallCallbackList((Widget) fsb, fsb->fsb.create_creator_callback,
			   (XtPointer) &cc);

	if (cc.creator_shell == NULL || cc.creator == NULL) {
	    cc.creator_shell =
		    XtCreatePopupShell("creatorShell",
				       transientShellWidgetClass,	
				       (Widget) fsb, (ArgList) NULL, 0);
	    i = 0;
	    XtSetArg(args[i], XtNfontSelectionBox, fsb);		i++;
	    cc.creator =
		    XtCreateManagedWidget("creator", fontCreatorWidgetClass,
					  cc.creator_shell, args, i);
	}
	fsb->fsb.creator_shell = cc.creator_shell;
	fsb->fsb.creator = cc.creator;
    }

    XtPopup(fsb->fsb.creator_shell, XtGrabNone);
    XRaiseWindow(XtDisplay(fsb->fsb.creator_shell),
		 XtWindow(fsb->fsb.creator_shell)); 

    _FSBSetCreatorFamily(fsb->fsb.creator, fsb->fsb.currently_selected_family);
}

/* ARGSUSED */

static void ShowCreatorCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    ShowCreator(fsb);
}

/* ARGSUSED */

static void ShowSamplerCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    ShowSampler(fsb);
}

/* ARGSUSED */

static void PreviewDoubleClick(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    DoPreview(fsb, False);
}

/* ARGSUSED */

static void ResizePreview(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    Dimension height;
    Cardinal depth;
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    if (!XtIsRealized(widget) || fsb->fsb.gstate == 0) return;

    XtVaGetValues(widget, XmNheight, &height, XmNdepth, &depth, NULL);

    XDPSSetContextGState(fsb->fsb.context, fsb->fsb.gstate);

    XDPSSetContextParameters(fsb->fsb.context, XtScreen(widget), depth,
			     XtWindow(widget), height,
			     (XDPSStandardColormap *) NULL,
			     (XDPSStandardColormap *) NULL,
			     XDPSContextScreenDepth | XDPSContextDrawable);

    _DPSFReclip(fsb->fsb.context);

    XDPSUpdateContextGState(fsb->fsb.context, fsb->fsb.gstate);
}

static String FindAFMRecursive(
    Widget w,
    String name,
    Boolean recur)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    int count;
    char **names, **files;
    String ret, ch;

    if (name == NULL) return NULL;

    count = ListPSResourceFiles(fsb->fsb.resource_path_override,
				fsb->fsb.default_resource_path,
				PSResFontAFM,
				name,
				&names, &files);
    
    if (count == 0 && recur) {
	for (ch = name; *ch != '_' && *ch != '\0'; ch++) {}
	if (*ch == '\0') return NULL;
	*ch = '\0';
	ret = FindAFMRecursive(w, name, False);
	*ch = '_';
	return ret;
    }

    if (count == 0) return NULL;
    ret = files[0];
    free(names);
    free(files);
    return ret;
}

static String FindAFM(Widget w, String name)
{
    return FindAFMRecursive(w, name, True);
}

static String FindFontFileRecursive(
    Widget w,
    String name,
    Boolean recur)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    int count;
    char **names, **files;
    String ret, ch;

    if (name == NULL) return NULL;

    count = ListPSResourceFiles(fsb->fsb.resource_path_override,
				fsb->fsb.default_resource_path,
				PSResFontOutline,
				name,
				&names, &files);
    
    if (count == 0 && recur) {
	for (ch = name; *ch != '_' && *ch != '\0'; ch++) {}
	if (*ch == '\0') return NULL;
	*ch = '\0';
	ret = FindFontFileRecursive(w, name, False);
	*ch = '_';
	return ret;
    }

    if (count == 0) return NULL;
    ret = files[0];
    free(names);
    free(files);
    return ret;
}

static String FindFontFile(Widget w, String name)
{
    return FindFontFileRecursive(w, name, True);
}

static Boolean Verify(
    FontSelectionBoxWidget fsb,
    FSBValidateCallbackRec *cb,
    String afm,
    Boolean doIt)
{
    char *chSize;
    int i;

    if (fsb->fsb.current_family_multiple) {
	cb->family = NULL;
	cb->family_selection = FSBMultiple;
    } else if (fsb->fsb.currently_selected_family == NULL) {
	cb->family = NULL;
	cb->family_selection = FSBNone;
    } else {
	cb->family = fsb->fsb.currently_selected_family->family_name;
	cb->family_selection = FSBOne;
    }

    if (fsb->fsb.current_face_multiple) {
	cb->face = NULL;
	cb->face_selection = FSBMultiple;
    } else if (fsb->fsb.currently_selected_face == NULL) {
	cb->face = NULL;
	cb->face_selection = FSBNone;
    } else {
	cb->face = fsb->fsb.currently_selected_face->face_name;
	cb->face_selection = FSBOne;
    }
	
    if (cb->family_selection == FSBMultiple ||
	cb->face_selection == FSBMultiple) {
	cb->name = NULL;
	cb->name_selection = FSBMultiple;
    } else if (fsb->fsb.currently_selected_face == NULL) {
	cb->name = NULL;
	cb->name_selection = FSBNone;
    } else {
	if (fsb->fsb.currently_selected_blend != NULL) {
	    cb->name = fsb->fsb.currently_selected_blend->font_name;
	} else cb->name = fsb->fsb.currently_selected_face->font_name;
	cb->name_selection = FSBOne;
    }

    if (fsb->fsb.current_size_multiple) {
	cb->size = 0.0;
	cb->size_selection = FSBMultiple;
    } else {
	XtVaGetValues(fsb->fsb.size_text_field_child, XmNvalue, &chSize, NULL);

	if (chSize == NULL || *chSize == '\0') {
	    cb->size = 0.0;
	    cb->size_selection = FSBNone;
	} else {
	    cb->size = atof(chSize);
	    cb->size_selection = FSBOne;
	}
    }

    cb->afm_filename = afm;
    cb->afm_present = (afm != NULL);
    cb->doit = True;

    if (fsb->fsb.currently_selected_blend == NULL) {
	cb->blend = cb->base_name = NULL;
	for (i = 0; i < MAX_AXES; i++) cb->axis_percent[i] = 0.0;
    } else {
	cb->blend = fsb->fsb.currently_selected_blend->blend_name;
	cb->base_name = fsb->fsb.currently_selected_face->font_name;
	for (i = 0; i < MAX_AXES; i++) {
	    cb->axis_percent[i] = fsb->fsb.currently_selected_blend->data[i];
	}
    }

    if (doIt) XtCallCallbackList((Widget) fsb, fsb->fsb.validate_callback, cb);
    return cb->doit;
}

static Boolean VerifyAndCallback(
    FontSelectionBoxWidget fsb,
    FSBCallbackReason reason,
    XtCallbackList callback)
{
    String afm = NULL;
    FSBValidateCallbackRec cb;
    FontRec *fsave, *face;

    if (fsb->fsb.get_afm) {
	if (fsb->fsb.currently_selected_face == NULL) afm = NULL;
	else afm = FindAFM((Widget) fsb,
			   fsb->fsb.currently_selected_face->font_name);
    }

    DoPreview(fsb, False);

    cb.reason = reason;
    if (!Verify(fsb, &cb, afm, True)) return False;

    fsb->fsb.font_family_multiple = fsb->fsb.current_family_multiple;
    if (!fsb->fsb.font_family_multiple &&
	fsb->fsb.currently_selected_family != NULL) {
	fsb->fsb.font_family =
		fsb->fsb.currently_selected_family->family_name;
    } else fsb->fsb.font_family = NULL;

    fsb->fsb.font_face_multiple = fsb->fsb.current_face_multiple;
    if (!fsb->fsb.font_face_multiple &&
	fsb->fsb.currently_selected_face != NULL) {
	fsb->fsb.font_face = fsb->fsb.currently_selected_face->face_name;
    } else fsb->fsb.font_face = NULL;

    fsb->fsb.font_name_multiple =
	    fsb->fsb.font_family_multiple || fsb->fsb.font_face_multiple;
    if (!fsb->fsb.font_name_multiple &&
	fsb->fsb.currently_selected_face != NULL) {
	fsb->fsb.font_name = fsb->fsb.currently_selected_face->font_name;
    } else fsb->fsb.font_name = NULL;

    fsb->fsb.font_size_multiple = fsb->fsb.current_size_multiple;
    if (!fsb->fsb.font_size_multiple) {
	fsb->fsb.font_size = cb.size;
    }

    if (fsb->fsb.currently_selected_blend != NULL) {
	fsb->fsb.font_blend = fsb->fsb.currently_selected_blend->blend_name;
    } else fsb->fsb.font_blend = NULL;

    if (fsb->fsb.undef_unused_fonts) {
	fsave = fsb->fsb.currently_previewed;
	if (fsb->fsb.make_fonts_shared) {
	    fsb->fsb.currently_previewed = NULL;
	}
	UndefUnusedFonts((Widget)fsb);
	fsb->fsb.currently_previewed = fsave;
	face = fsb->fsb.currently_selected_face;
	if (face != NULL && !face->resident) {
	    face->resident = True;
	    if (fsb->fsb.make_fonts_shared) {
		(void) DownloadFont(fsb, face->font_name,
				    fsb->fsb.context, True);
		/* If making it shared, be sure to synchronize with
		   the caller who might be using a different context */
		DPSWaitContext(fsb->fsb.context);
	    }
	}
    }

    XtCallCallbackList((Widget) fsb, callback, &cb);
    return True;
}

/* ARGSUSED */

static void OKCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    if (!VerifyAndCallback(fsb, FSBOK, fsb->fsb.ok_callback)) return;
    if (XtIsShell(XtParent(fsb))) XtPopdown(XtParent(fsb));
    WriteBlends(fsb);
    DesensitizeReset(fsb);
    if (fsb->fsb.show_sampler) XtPopdown(fsb->fsb.sampler);
}

/* ARGSUSED */

static void ApplyCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    (void) VerifyAndCallback(fsb, FSBApply, fsb->fsb.apply_callback);
    WriteBlends(fsb);
    DesensitizeReset(fsb);
}

static void ResetFSB(
    FontSelectionBoxWidget fsb,
    FSBCallbackReason reason)
{
    FSBCallbackRec cb;
    int i;

    fsb->fsb.currently_previewed = NULL;
    fsb->fsb.currently_previewed_size = fsb->fsb.currently_selected_size = 0.0;
    SetUpCurrentSelections(fsb);
    if (fsb->fsb.undef_unused_fonts) UndefUnusedFonts((Widget)fsb);

    cb.reason = reason;
    if (fsb->fsb.font_family_multiple) {
	cb.family = NULL;
	cb.family_selection = FSBMultiple;
    } else if (fsb->fsb.font_family == NULL) {
	cb.family = NULL;
	cb.family_selection = FSBNone;
    } else {
	cb.family = fsb->fsb.font_family;
	cb.family_selection = FSBOne;
    }

    if (fsb->fsb.font_face_multiple) {
	cb.face = NULL;
	cb.face_selection = FSBMultiple;
    } else if (fsb->fsb.font_face == NULL) {
	cb.face = NULL;
	cb.face_selection = FSBNone;
    } else {
	cb.face = fsb->fsb.font_face;
	cb.face_selection = FSBOne;
    }
	
    if (cb.family_selection == FSBMultiple ||
	cb.face_selection == FSBMultiple) {
	cb.name = NULL;
	cb.name_selection = FSBMultiple;
    } else if (fsb->fsb.font_face == NULL) {
	cb.name = NULL;
	cb.name_selection = FSBNone;
    } else {
	cb.name = fsb->fsb.font_name;
	cb.name_selection = FSBOne;
    }

    if (fsb->fsb.font_size_multiple) {
	cb.size = 0.0;
	cb.size_selection = FSBMultiple;
    } else {
	cb.size = fsb->fsb.font_size;
	cb.size_selection = FSBOne;
    }

    cb.afm_filename = NULL;
    cb.afm_present = False;

    cb.blend = fsb->fsb.font_blend;
    if (cb.blend == NULL || fsb->fsb.currently_selected_blend == NULL) {
	cb.base_name = NULL;
	for (i = 0; i < MAX_AXES; i++) cb.axis_percent[i] = 0;
    } else {
	cb.base_name = fsb->fsb.currently_selected_face->font_name;
	for (i = 0; i < MAX_AXES; i++) {
	    cb.axis_percent[i] = fsb->fsb.currently_selected_blend->data[i];
	}
    }

    if (reason == FSBReset) {
	XtCallCallbackList((Widget) fsb, fsb->fsb.reset_callback, &cb);
    } else XtCallCallbackList((Widget) fsb, fsb->fsb.cancel_callback, &cb);
}

/* ARGSUSED */

static void ResetCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    ResetFSB(fsb, FSBReset);
    DesensitizeReset(fsb);
}

/* ARGSUSED */

static void CancelCallback(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    ResetFSB(fsb, FSBCancel);
    if (XtIsShell(XtParent(fsb))) XtPopdown(XtParent(fsb));
    DesensitizeReset(fsb);
    if (fsb->fsb.show_sampler) XtPopdown(fsb->fsb.sampler);
}

/* There's a problem; sometimes the change has already been made in the field,
   and sometimes it hasn't.  The times when it has seem to correspond to
   making changes with the size option menu, so we use this disgusting
   global flag to notice when this happens.  We also use this to tell whether
   or not the change is coming from internal to the widget or as a result
   of user interaction.  */

static Boolean changingSize = False;

/* ARGSUSED */

static void SizeSelect(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    String value;
    Widget option;
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;
    char *ch;

    XtVaGetValues(widget, XmNvalue, &value, NULL);

    if (value == NULL) option = fsb->fsb.other_size;
    else {
	if (value[0] != '\0' && fsb->fsb.current_size_multiple) {
	    fsb->fsb.current_size_multiple = False;
	    UnmanageSizeMultiple(fsb);
	}
	for (ch = value; *ch != '\0'; ch++) if (*ch == '.') *ch = '-';

	option = XtNameToWidget(fsb->fsb.size_menu, value);
	if (option == NULL) option = fsb->fsb.other_size;
    }

    XtVaSetValues(fsb->fsb.size_option_menu_child,
		  XmNmenuHistory, option, NULL);

    if (value != NULL && value[0] != '\0') {
	fsb->fsb.currently_selected_size = atof(value);
    } else fsb->fsb.currently_selected_size = 0.0;

    if (!changingSize) SensitizeReset(fsb);
    fsb->fsb.current_size_multiple = False;
}

/* ARGSUSED */

static void TextVerify(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    int i;
    XmTextVerifyPtr v = (XmTextVerifyPtr) callData;
    char ch, *cp;
    int decimalPoints = 0;
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    if (changingSize) return;	/* We know what we're doing; allow it */

    /* Should probably look at format field, but seems to contain garbage */

    if (v->text->length == 0) return;

    if (v->text->length == 1) {
	ch = v->text->ptr[0];
	if (ch == 'p' || ch == 'P') {
	    XtCallCallbacks(fsb->fsb.preview_button_child,
			    XmNactivateCallback, NULL);
	    v->doit = False;
	    return;
	}
    }
	
    for (i = 0; i < v->text->length; i++) {
	ch = v->text->ptr[i];
	if (ch == '.') decimalPoints++;
	else if (!isdigit(ch)) {
	    v->doit = False;
	    return;
	}
    }

    if (decimalPoints > 1) {
	v->doit = False;
	return;
    }

    XtVaGetValues(widget, XmNvalue, &cp, NULL);

    for (/**/; *cp != '\0'; cp++) {
	if (*cp == '.') decimalPoints++;
    }

    if (decimalPoints > 1) v->doit = False;
}

/* ARGSUSED */

static void SetSize(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    char buf[20];
    char *ch;
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;

    if (fsb->fsb.current_size_multiple) {
	fsb->fsb.current_size_multiple = False;
	UnmanageSizeMultiple(fsb);
    }

    strcpy(buf, XtName(widget));
    for (ch = buf; *ch != '\0'; ch++) if (*ch == '-') *ch++ = '.';

    changingSize = True;
    XtVaSetValues(fsb->fsb.size_text_field_child, XmNvalue, buf, NULL);
    changingSize = False;

    SensitizeReset(fsb);
    ValueChanged(fsb);
}

/* This makes sure the selected item is visible */

static void ListSelectPos(
    Widget w,
    int pos,
    Boolean notify)
{
    int topPos, items, visible;

    XmListSelectPos(w, pos, notify);
    
    XtVaGetValues(w, XmNtopItemPosition, &topPos,
		  XmNvisibleItemCount, &visible, XmNitemCount, &items, NULL);

    if (pos >= topPos && pos < topPos + visible) return;
    topPos = pos - (visible-1)/2;
    if (topPos + visible > items) topPos = items - visible + 1;
    if (topPos < 1) topPos = 1;

    XtVaSetValues(w, XmNtopItemPosition, topPos, NULL);
}

/* The following function Copyright 1987, 1988 by Digital Equipment
Corporation, Maynard, Massachusetts, and the Massachusetts Institute of
Technology, Cambridge, Massachusetts. */

static String GetRootDirName(String buf)
{
#ifndef X_NOT_POSIX
     uid_t uid;
#else
     int uid;
     extern int getuid();
#ifndef SYSV386
     extern struct passwd *getpwuid(), *getpwnam();
#endif
#endif
     struct passwd *pw;
     static char *ptr = NULL;

     if (ptr == NULL) {
        if (!(ptr = getenv("HOME"))) {
            if ((ptr = getenv("USER")) != NULL) pw = getpwnam(ptr);
            else {
                uid = getuid();
                pw = getpwuid(uid);
            }
            if (pw) ptr = pw->pw_dir;
            else {
                ptr = NULL;
                *buf = '\0';
            }
        }
     }

     if (ptr)
        (void) strcpy(buf, ptr);

     buf += strlen(buf);
     *buf = '/';
     buf++;
     *buf = '\0';
     return buf;
}

static void WriteBlendLine(
    FILE *f,
    String family, String face, String blend, String name,
    int axes,
    float *p)
{
    register char *ch;
    int i;

    ch = family;
    while (*ch != '\0') {
	if (*ch == ',' || *ch == '\\') (void) putc('\\', f);
	(void) putc(*ch++, f);
    }
    putc(',', f);
    ch = face;
    while (*ch != '\0') {
	if (*ch == ',' || *ch == '\\') (void) putc('\\', f);
	(void) putc(*ch++, f);
    }
    putc(',', f);
    ch = blend;
    while (*ch != '\0') {
	if (*ch == ',' || *ch == '\\') (void) putc('\\', f);
	(void) putc(*ch++, f);
    }
    (void) putc(',', f);
    ch = name;
    while (*ch != '\0') {
	if (*ch == ',' || *ch == '\\') (void) putc('\\', f);
	(void) putc(*ch++, f);
    }
    for (i = 0; i < axes; i++) fprintf(f, ",%f", p[i]);
    (void) putc('\n', f);
}

static void WriteBlends(FontSelectionBoxWidget fsb)
{
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;
    String blendEnv;
    char homeDir[PATH_BUF_SIZE];
    FILE *blendFile = NULL;
    char fileName[PATH_BUF_SIZE];

    if (!fsb->fsb.blends_changed) return;

    blendEnv = getenv("DPSFONTRC");

    if (blendEnv != NULL) blendFile = fopen(blendEnv, "w");

    if (blendFile == NULL) {
	(void) GetRootDirName(homeDir);
	sprintf(fileName, "%s/.dpsfontrc", homeDir);
	blendFile = fopen(fileName, "w");

	if (blendFile == NULL) return;
    }

    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->blend_data != NULL) {
		for (b = f->blend_data->blends; b != NULL; b = b->next) {
		    WriteBlendLine(blendFile, ff->family_name, f->face_name,
				   b->blend_name, b->font_name,
				   f->blend_data->num_axes, b->data);
		}
	    }
	}
    }

    fclose(blendFile);
    fsb->fsb.blends_changed = False;
}

static Boolean ParseBlendLine(
    String buf, String family, String face, String blend, String name,
    float *p)
{
    char *src, *dst;
    int i;
    float f;

    src = buf;
    dst = family;
    while (*src != ',' && *src != '\0') {
	if (*src == '\\') src++;
	if (*src == '\0') return False;
	*dst++ = *src++;
    }
    if (*src == '\0') return False;
    *dst = '\0';
    src++;
    dst = face;
    while (*src != ',' && *src != '\0') {
	if (*src == '\\') src++;
	if (*src == '\0') return False;
	*dst++ = *src++;
    }
    if (*src == '\0') return False;
    *dst = '\0';
    src++;
    dst = blend;
    while (*src != ',' && *src != '\0') {
	if (*src == '\\') src++;
	if (*src == '\0') return False;
	*dst++ = *src++;
    }
    if (*src == '\0') return False;
    *dst = '\0';
    src++;
    dst = name;
    while (*src != ',' && *src != '\0') {
	if (*src == '\\') src++;
	if (*src == '\0') return False;
	*dst++ = *src++;
    }
    if (*src == '\0') return False;
    *dst = '\0';
    for (i = 0; i < MAX_AXES; i++) {
	src++;
	if (!ScanFloat(src, &f, &src)) {
	    for (/**/; i < MAX_AXES; i++) p[i] = 0;
	    return True;;
	}
	else p[i] = f;
    }
    return True;
}

static void ReadBlends(FontSelectionBoxWidget fsb)
{
    String blendEnv;
    char homeDir[PATH_BUF_SIZE];
    FILE *blendFile = NULL;
    char fileName[PATH_BUF_SIZE];
#define BUF 256
    char buf[BUF+1], family[BUF+1], face[BUF+1], blend[BUF+1], name[BUF+1];
    char *cfamily, *cface;
    float p[MAX_AXES];
    FontRec *f;
    FontFamilyRec *ff = 0;
    BlendRec *b, *newb, **lastb;
    char *spaceBlend;	    
    char *lastFamily = NULL;
    int cmp, i;

    blendEnv = getenv("DPSFONTRC");

    if (blendEnv != NULL) blendFile = fopen(blendEnv, "r");

    if (blendFile == NULL) {
	(void) GetRootDirName(homeDir);
	sprintf(fileName, "%s/.dpsfontrc", homeDir);
	blendFile = fopen(fileName, "r");

	if (blendFile == NULL) return;
    }

    while (1) {
	if (fgets(buf, BUF, blendFile) == NULL) {
	    fclose(blendFile);
	    return;
	}
	if (ParseBlendLine(buf, family, face, blend, name, p)) {
	    cfamily = Canonical(family);
	    if (cfamily != lastFamily) {
		for (ff = fsb->fsb.known_families;
		     ff != NULL && ff->family_name != cfamily;
		     ff = ff->next) {}
	    }
	    if (ff == NULL) continue;
	    lastFamily = cfamily;
	    cface = Canonical(face);
	    for (f = ff->fonts; f != NULL && f->face_name != cface;
		 f = f->next) {}
	    /* If the blend data is NULL, we have a blend line for a font
	       that we don't believe is a MM font.  Ignore it */
	    if (f != NULL && f->blend_data != NULL) {
		lastb = &f->blend_data->blends;
		cmp = -1;
		for (b = f->blend_data->blends; b != NULL; b = b->next) {
		    cmp = strcmp(blend, b->blend_name);
		    if (cmp < 0) break;
		    lastb = &b->next;
		}
		if (cmp != 0) {
		    newb = XtNew(BlendRec);
		    newb->blend_name = Canonical(blend);
		    newb->CS_blend_name = CS(newb->blend_name, (Widget) fsb);

		    spaceBlend = (char *) XtMalloc(strlen(blend) + 4);
		    spaceBlend[0] = spaceBlend[1] = spaceBlend[2] = ' ';
		    strcpy(spaceBlend+3, blend);
		    newb->CS_space_blend_name = CS(spaceBlend, (Widget) fsb);
		    XtFree((XtPointer) spaceBlend);

		    for (i = 0; i < MAX_AXES; i++) newb->data[i] = p[i];
		    newb->font_name = Canonical(name);

		    f->blend_count++;
		    ff->blend_count++;

		    newb->next = b;
		    *lastb = newb;
		}
	    }
	}
    }
}

static void SetUpFaceList(
    FontSelectionBoxWidget fsb,
    FontFamilyRec *ff)
{
    FontRec *f;
    BlendRec *b;
    XmString *CSfaces;
    Boolean multiple = False;
    int i;

    for (f = ff->fonts; f != NULL; f = f->next) {
	if (f->blend_data != NULL) {
	    multiple = True;
	    break;
	}
    }
    if (multiple) ManageMultipleMaster(fsb);
    else UnmanageMultipleMaster(fsb);

    CSfaces = (XmString *) XtCalloc(ff->font_count + ff->blend_count,
				    sizeof(XmString));

    i = 0;
    for (f = ff->fonts; f != NULL; f = f->next) {
	CSfaces[i++] = f->CS_face_name;
	if (f->blend_data != NULL) {
	    for (b = f->blend_data->blends; b != NULL; b = b->next) {
		CSfaces[i++] = b->CS_space_blend_name;
	    }
	}
    }

    XtVaSetValues(fsb->fsb.face_scrolled_list_child,
		  XmNitemCount, ff->font_count + ff->blend_count,
		  XmNitems, CSfaces, NULL);
    XtFree((XtPointer) CSfaces);
}

/* ARGSUSED */

static void DeleteMessage(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    XtDestroyWidget(widget);
}

static void FlushFont(
    FontSelectionBoxWidget fsb,
    FontRec *font)
{
    FontRec *f = 0, *f1;
    FontFamilyRec *ff, *ff1;
    Boolean previewedFamily = False;

    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f == font) goto FOUND_BOGUS;
	}
    }

FOUND_BOGUS:
    if (f != NULL) {
	for (f1 = ff->fonts; f1 != NULL; f1 = f1->next) {
	    if (f1 == fsb->fsb.currently_previewed) {
		previewedFamily = True;
		break;
	    }
	}

	if (ff->fonts == f) {
	    ff->fonts = f->next;
	} else {
	    for (f1 = ff->fonts; f1 != NULL && f1->next != f; f1 = f1->next) {}
	    if (f1 != NULL) f1->next = f->next;
	}

	ff->font_count--;
	ff->blend_count -= f->blend_count;

	if (f == fsb->fsb.currently_selected_face) {
	    fsb->fsb.currently_selected_face = NULL;
	    fsb->fsb.currently_selected_blend = NULL;
	}

	if (previewedFamily) SetUpFaceList(fsb, ff);

	if (f == fsb->fsb.currently_previewed) {
	    fsb->fsb.currently_previewed = NULL;
	    fsb->fsb.currently_previewed_blend = NULL;
	    ValueChanged(fsb);
	}

	/* We do not free the FontRec or FontFamilyRec.  In the long
	   run we don't expect to leak much storage this way, since we
	   shouldn't have many bogus fonts, and invalidating every 
	   reference here, in the sampler, and in the creator isn't
	   worth the small storage waste. */

	if (ff->fonts == NULL) {
	    if (fsb->fsb.known_families == ff) {
		fsb->fsb.known_families = ff->next;
	    } else {
		for (ff1 = fsb->fsb.known_families;
		     ff1 != NULL && ff1->next != ff; ff1 = ff1->next) {}
		if (ff1 != NULL) ff1->next = ff->next;
	    }

	    fsb->fsb.family_count--;

	    if (ff == fsb->fsb.currently_selected_family) {
		fsb->fsb.currently_selected_family = NULL;
	    }

	    DisplayFontFamilies(fsb);
	}
    }
}

void _FSBFlushFont(
    FontSelectionBoxWidget fsb,
    FontRec *font)
{
    if (font == fsb->fsb.currently_previewed) _FSBBogusFont(fsb, font);
    else FlushFont(fsb, font);
}

void _FSBBogusFont(
    FontSelectionBoxWidget fsb,
    FontRec *font)
{
    Widget message, w;

    message = XmCreateInformationDialog((Widget) fsb, "invalidFontMessage",
					(ArgList) NULL, 0);
    w =	XmMessageBoxGetChild(message, XmDIALOG_CANCEL_BUTTON);
    XtUnmanageChild(w);
    w =	XmMessageBoxGetChild(message, XmDIALOG_HELP_BUTTON);
    XtUnmanageChild(w);
    XtAddCallback(message, XmNokCallback, DeleteMessage, (XtPointer) NULL);
    
    XtManageChild(message);

    /* Now get this blasted thing out of here */
    FlushFont(fsb, font);
}

void _FSBSetUpFaceList(
    FontSelectionBoxWidget fsb,
    Bool redisplay)
{
    FontRec *f;
    BlendRec *b;
    int i;

    SetUpFaceList(fsb, fsb->fsb.currently_selected_family);
    
    f = fsb->fsb.currently_selected_family->fonts;
    i = 1;
    while (f != NULL) {
	if (f == fsb->fsb.currently_selected_face) {
	    if (f->blend_data != NULL) {
		b = f->blend_data->blends;
		if (fsb->fsb.currently_selected_blend != NULL) {
		    i++;
		    while (b != NULL &&
			   b != fsb->fsb.currently_selected_blend) {
			i++;
			b = b->next;
		    }
		}
	    }
	    break;
	} else {
	    i += f->blend_count+1;
	    f = f->next;
	}
    }

    ListSelectPos(fsb->fsb.face_scrolled_list_child, i, False);
    if (redisplay) ValueChanged(fsb);
    fsb->fsb.blends_changed = True;
}

static String categories[][6] = {
    {"Regular", "Roman", "Medium", "Book", "Light", NULL},
    {"Italic", "Slanted", "Oblique", NULL},
    {"Demi", "Semibold", "Heavy", "Bold", NULL},
    {NULL},
};

#define NORMALINDEX 0][0
#define ITALICINDEX 1][0
#define BOLDINDEX 2][3
#define DEMIINDEX 2][0
#define LIGHTINDEX 0][4
#define BOOKINDEX 0][3

static String extraNormalFaces[] = {"Demi", "Semibold", NULL};

static int MatchFaceName(
    FSBFaceSelectCallbackRec *rec,
    Boolean *gaveUp)
{
    int i, j, k, face;
#define PIECEMAX 10
    String pieces[PIECEMAX];
    int numPieces;
    int pass;
    char *ch, *start, *compare;
    char save;
    static Boolean categoriesInited = False;
    static char *canonicalBold, *canonicalLight, *canonicalBook;

    *gaveUp = False;

    if (!categoriesInited) {
	for (i = 0; categories[i][0] != NULL; i++) {
	    for (j = 0; categories[i][j] != NULL; j++) {
		categories[i][j] = Canonical(categories[i][j]);
	    }
	}
	for (i = 0; extraNormalFaces[i] != NULL; i++) {
	    extraNormalFaces[i] = Canonical(extraNormalFaces[i]);
	}
	canonicalBold = categories[BOLDINDEX];
	canonicalLight = categories[LIGHTINDEX];
	canonicalBook = categories[BOOKINDEX];
	categoriesInited = True;
    }

    if (rec->current_face == NULL || rec->current_face[0] == '\0') {
	goto GIVE_UP;
    }

    /* First check for an exact match */

    for (i = 0; i < rec->num_available_faces; i++) {
	if (rec->available_faces[i] == rec->current_face) return i;
    }

    /* Try some category matching.  We make two passes; in the first pass
       we remove "Bold" from the "Demi" family and "Light" and "Book" from
       the "Regular" family; in the second pass we include them.  We ignore
       leading digits in the face name.  */

    categories[BOLDINDEX] = categories[LIGHTINDEX] =
	    categories[BOOKINDEX] = NULL;

    i = 0;
    ch = rec->current_face;
    while (*ch == ' ' || isdigit(*ch)) ch++;
    start = ch;

    while (1) {
	while (*ch != ' ' && *ch != '\0') ch++;
	save = *ch;
	*ch = '\0';
	compare = Canonical(start);
	for (j = 0; categories[j][0] != NULL; j++) {
	    for (k = 0; categories[j][k] != NULL; k++) {
		if (compare == categories[j][k]) {
		    pieces[i++] = categories[j][0];
		    goto FOUND_PIECE;
		}
	    }
	}
	pieces[i++] = compare;	/* A unique piece */
FOUND_PIECE:
	*ch = save;
	while (*ch == ' ') ch++;
	if (*ch == '\0') break;
	if (i >= PIECEMAX) goto GIVE_UP;
	start = ch;
    }
    numPieces = i;
    if (numPieces == 0) goto GIVE_UP;

    /* Special case starting with the italic category */

    if (pieces[0] == categories[ITALICINDEX] && numPieces < PIECEMAX-1) {
	for (i = numPieces; i > 0; i--) pieces[i] = pieces[i-1];
	pieces[0] = categories[NORMALINDEX];
	numPieces++;
    }

    for (pass = 0; pass < 2; pass++) {
	if (pass == 1) {
	    categories[BOLDINDEX] = canonicalBold;
	    categories[LIGHTINDEX] = canonicalLight;
	    categories[BOOKINDEX] = canonicalBook;
	    for (i = 0; i < numPieces; i++) {
		if (pieces[i] == canonicalBold) {
		    pieces[i] = categories[DEMIINDEX];
		} else if (pieces[i] == canonicalLight) {
		    pieces[i] = categories[NORMALINDEX];
		}  else if (pieces[i] == canonicalBook) {
		    pieces[i] = categories[NORMALINDEX];
		}
	    }
	}

	/* Now match against each face */

	for (face = 0; face < rec->num_available_faces; face++) {
	    i = 0;
	    ch = rec->available_faces[face];
	    while (*ch == ' ' || isdigit(*ch)) ch++;
	    start = ch;

	    while (1) {
		while (*ch != ' ' && *ch != '\0') ch++;
		save = *ch;
		*ch = '\0';
		compare = Canonical(start);
		for (j = 0; categories[j][0] != NULL; j++) {
		    for (k = 0; categories[j][k] != NULL; k++) {
			if (compare == categories[j][k]) {
			    compare = categories[j][0];
			    goto MATCH;
			}
		    }
		}
    MATCH:
		/* Special case matching the italic category again */

		if (i == 0 && compare == categories[ITALICINDEX] &&
		    pieces[0] == categories[NORMALINDEX] &&
		    numPieces > 1 &&
		    pieces[1] == categories[ITALICINDEX]) i = 1;

		if (pieces[i] != compare) {
		    *ch = save;
		    goto NEXT_FACE;
		} else i++;

		*ch = save;
		while (*ch == ' ') ch++;
		if (*ch == '\0') break;
		if (i >= numPieces) goto NEXT_FACE;
		start = ch;
	    }
	    if (i == numPieces) return face;	/* Found a match! */
    NEXT_FACE:		
	    ;
	}
    }

    /* Couldn't find a match.  Look for a "normal face".  Make sure "Light"
       and "Book" are installed. Again, ignore leading spaces.  */
GIVE_UP:
    *gaveUp = True;
    categories[LIGHTINDEX] = canonicalLight;
    categories[BOOKINDEX] = canonicalBook;

    for (i = 0; categories[0][i] != NULL; i++) {
	for (face = 0; face < rec->num_available_faces; face++) {
	    compare = rec->available_faces[face];
	    while (*compare == ' ' || isdigit(*compare)) compare++;
	    if (compare != rec->available_faces[face]) {
		compare = Canonical(compare);
	    }
	    if (categories[0][i] == compare) return face;
	}
    }

    for (i = 0; extraNormalFaces[i] != NULL; i++) {
	for (face = 0; face < rec->num_available_faces; face++) {
	    compare = rec->available_faces[face];
	    while (*compare == ' ' || isdigit(*compare)) compare++;
	    if (compare != rec->available_faces[face]) {
		compare = Canonical(compare);
	    }
	    if (extraNormalFaces[i] == compare) return face;
	}
    }
    
    /* Oh, well.  Use the first one */
    return 0;
}

static void GetInitialFace(
    FontSelectionBoxWidget fsb,
    FontFamilyRec *ff)
{
    FSBFaceSelectCallbackRec rec;
    String *faces;
    int i, j;
    FontRec *f;
    Boolean junk;

    faces = (String *) XtMalloc(ff->font_count * sizeof(String));
    i = 0;
    for (f = ff->fonts; f != NULL; f = f->next) faces[i++] = f->face_name;

    rec.available_faces = faces;
    rec.num_available_faces = ff->font_count;

    if (fsb->fsb.currently_selected_face != NULL) {
	rec.current_face = fsb->fsb.currently_selected_face->face_name;
    } else rec.current_face = fsb->fsb.font_face;

    rec.new_face = NULL;

    XtCallCallbackList((Widget) fsb, fsb->fsb.face_select_callback, &rec);
    if (rec.new_face != NULL) {
	for (i = 0; i < ff->font_count; i++) {
	    if (rec.new_face == faces[i]) break;
	}
    }
    if (rec.new_face == NULL || i >= ff->font_count) {
	i = MatchFaceName(&rec, &junk);
    }
    XtFree((XtPointer) faces);

    j = 0;
    for (f = ff->fonts; i != 0; f= f->next) {
	j += f->blend_count + 1;
	i--;
    }
    
    ListSelectPos(fsb->fsb.face_scrolled_list_child, j+1, False);
    fsb->fsb.currently_selected_face = f;
    fsb->fsb.currently_selected_blend = NULL;
}

/* ARGSUSED */

static void FamilySelect(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    XmListCallbackStruct *listCB = (XmListCallbackStruct *) callData;
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;
    FontFamilyRec *ff = fsb->fsb.known_families;
    int i;

    if (fsb->fsb.current_family_multiple) {
	fsb->fsb.current_family_multiple = False;
	UnmanageFamilyMultiple(fsb);
    }

    /* List uses 1-based addressing!! */
    for (i = 1; i < listCB->item_position; i++) ff = ff->next;

    fsb->fsb.currently_selected_family = ff;

    SensitizeReset(fsb);
    SetUpFaceList(fsb, ff);
    if (!fsb->fsb.current_face_multiple) GetInitialFace(fsb, ff);
    ValueChanged(fsb);
}

/* ARGSUSED */

static void FaceSelect(
    Widget widget,
    XtPointer clientData, XtPointer callData)
{
    XmListCallbackStruct *listCB = (XmListCallbackStruct *) callData;
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) clientData;
    FontRec *f;
    BlendRec *b;
    int n;

    if (fsb->fsb.currently_selected_family == NULL) return;
    f = fsb->fsb.currently_selected_family->fonts;

    if (fsb->fsb.current_face_multiple) {
	fsb->fsb.current_face_multiple = False;
	UnmanageFaceMultiple(fsb);
    }

    /* List uses 1-based addressing!! */
    n = 0;
    while (1) {
	n += f->blend_count + 1;
	if (n >= listCB->item_position) {
	    n -= f->blend_count;
	    if (n == listCB->item_position) b = NULL;
	    else for (b = f->blend_data->blends; n < listCB->item_position - 1;
		      b = b->next) n++;
	    break;
	}
	f = f->next;
    }

    fsb->fsb.currently_selected_face = f;
    fsb->fsb.currently_selected_blend = b;

    SensitizeReset(fsb);
    ValueChanged(fsb);
}

static void CreateSizeMenu(
    FontSelectionBoxWidget fsb,
    Boolean destroyOldChildren)
{
    Arg args[20];
    int i, j;
    Widget *sizes;
    char buf[20];
    Widget *children;
    Cardinal num_children;
    XmString csName;
    char *ch;

    if (destroyOldChildren) {
	XtVaGetValues(fsb->fsb.size_menu, XtNchildren, &children,
		      XtNnumChildren, &num_children, NULL);

	/* Don't destroy first child ("other") */
	for (j = 1; (Cardinal)j < num_children; j++) XtDestroyWidget(children[j]);

	sizes = (Widget *) XtMalloc((fsb->fsb.size_count+1) * sizeof(Widget));
	sizes[0] = children[0];
    } else {
	i = 0;
	sizes = (Widget *) XtMalloc((fsb->fsb.size_count+1) * sizeof(Widget));
	fsb->fsb.other_size = sizes[0] =
		XtCreateManagedWidget("other", xmPushButtonGadgetClass,
				  fsb->fsb.size_menu, args, i);
    }

    for (j = 0; j < fsb->fsb.size_count; j++) {
	(void) sprintf(buf, "%g", fsb->fsb.sizes[j]);
	csName = UnsharedCS(buf);
	for (ch = buf; *ch != '\0'; ch++) if (*ch == '.') *ch = '-';
	i = 0;
	XtSetArg(args[i], XmNlabelString, csName);			i++;
	sizes[j+1] =
		XmCreatePushButtonGadget(fsb->fsb.size_menu, buf, args, i);
	XmStringFree(csName);
	XtAddCallback(sizes[j+1], XmNactivateCallback,
		      SetSize, (XtPointer) fsb);
    }
    XtManageChildren(sizes, j+1);
    XtFree((char *) sizes);
}

static void CreateChildren(FontSelectionBoxWidget fsb)
{
    Arg args[20];
    int i;
    Widget form;

    i = 0;
    fsb->fsb.pane_child =
	    XtCreateManagedWidget("pane", xmPanedWindowWidgetClass,
				  (Widget) fsb, args, i);

    i = 0;
    fsb->fsb.preview_child =
	    XtCreateManagedWidget("preview", xmDrawingAreaWidgetClass,
				  fsb->fsb.pane_child, args, i);
    XtAddCallback(fsb->fsb.preview_child, XmNexposeCallback,
		  PreviewText, (XtPointer) fsb);
    XtAddCallback(fsb->fsb.preview_child, XmNresizeCallback,
		  ResizePreview, (XtPointer) fsb);

    i = 0;	
    form = XtCreateManagedWidget("panel", xmFormWidgetClass,
				 fsb->fsb.pane_child, args, i);
    fsb->fsb.panel_child = form;

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.ok_button_child =
	    XtCreateManagedWidget("okButton", xmPushButtonWidgetClass,
				  form, args, i);
    XtAddCallback(fsb->fsb.ok_button_child, XmNactivateCallback,
		  OKCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNleftWidget,fsb->fsb.ok_button_child );		i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.apply_button_child =
	    XtCreateManagedWidget("applyButton", xmPushButtonWidgetClass,
				  form, args, i);
    XtAddCallback(fsb->fsb.apply_button_child, XmNactivateCallback,
		  ApplyCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNleftWidget,fsb->fsb.apply_button_child );	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.reset_button_child =
	    XtCreateManagedWidget("resetButton", xmPushButtonWidgetClass,
				  form, args, i);
    XtAddCallback(fsb->fsb.reset_button_child, XmNactivateCallback,
		  ResetCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNleftWidget,fsb->fsb.reset_button_child );	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.cancel_button_child =
	    XtCreateManagedWidget("cancelButton", xmPushButtonWidgetClass,
				  form, args, i);
    XtAddCallback(fsb->fsb.cancel_button_child, XmNactivateCallback,
		  CancelCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.ok_button_child);	i++;
    fsb->fsb.separator_child =
	    XtCreateManagedWidget("separator", xmSeparatorGadgetClass,
				  form, args, i);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.separator_child);	i++;
    fsb->fsb.size_label_child =
	    XtCreateManagedWidget("sizeLabel", xmLabelWidgetClass,
				  form, args, i);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNleftWidget, fsb->fsb.size_label_child);	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET);	i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.size_label_child);	i++;
    fsb->fsb.size_text_field_child =
	    XtCreateManagedWidget("sizeTextField", xmTextFieldWidgetClass,
				  form, args, i);
    XtAddCallback(fsb->fsb.size_text_field_child, XmNvalueChangedCallback,
		  SizeSelect, (XtPointer) fsb);
    XtAddCallback(fsb->fsb.size_text_field_child, XmNmodifyVerifyCallback,
		  TextVerify, (XtPointer) fsb);

    i = 0;
    fsb->fsb.size_menu = XmCreatePulldownMenu(form, "sizeMenu", args, i);

    CreateSizeMenu(fsb, False);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNleftWidget, fsb->fsb.size_text_field_child);	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET);	i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.size_label_child);	i++;
    XtSetArg(args[i], XmNsubMenuId, fsb->fsb.size_menu);		i++;
    fsb->fsb.size_option_menu_child =
	    XmCreateOptionMenu(form, "sizeOptionMenu", args, i);
    XtManageChild(fsb->fsb.size_option_menu_child);

    i = 0;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNleftWidget, fsb->fsb.size_option_menu_child);	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.separator_child);	i++;
    fsb->fsb.size_multiple_label_child =
	    XtCreateWidget("sizeMultipleLabel", xmLabelWidgetClass,
			   form, args, i);

    i = 0;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET);	i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.size_label_child);	i++;
    fsb->fsb.preview_button_child =
	    XtCreateManagedWidget("previewButton", xmPushButtonWidgetClass,
				  form, args, i);
    XtAddCallback(fsb->fsb.preview_button_child, XmNactivateCallback,
		  PreviewCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNrightWidget, fsb->fsb.preview_button_child);	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET);	i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.preview_button_child);	i++;
    fsb->fsb.sampler_button_child =
	    XtCreateWidget("samplerButton", xmPushButtonWidgetClass,
				  form, args, i);
    if (fsb->fsb.show_sampler_button) {
	XtManageChild(fsb->fsb.sampler_button_child);
    }
    XtAddCallback(fsb->fsb.sampler_button_child, XmNactivateCallback,
		  ShowSamplerCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNtopAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNrightPosition, 50);				i++;
    fsb->fsb.family_label_child =
	    XtCreateManagedWidget("familyLabel", xmLabelGadgetClass,
				  form, args, i);

    i = 0;
    XtSetArg(args[i], XmNtopAttachment, XmATTACH_FORM);			i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNleftPosition, 50);				i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.face_label_child =
	    XtCreateManagedWidget("faceLabel", xmLabelGadgetClass,
				  form, args, i);

    /* The next two must be widgets in order to be reversed in color */

    i = 0;
    XtSetArg(args[i], XmNtopAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNtopWidget, fsb->fsb.family_label_child);	i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNrightPosition, 50);				i++;
    fsb->fsb.family_multiple_label_child =
	    XtCreateWidget("familyMultipleLabel", xmLabelWidgetClass,
			   form, args, i);

    i = 0;
    XtSetArg(args[i], XmNtopAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNtopWidget, fsb->fsb.face_label_child);		i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNleftPosition, 50);				i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.face_multiple_label_child =
	    XtCreateWidget("faceMultipleLabel", xmLabelWidgetClass,
			   form, args, i);

    i = 0; 
    XtSetArg(args[i], XmNitemCount, 1);					i++;
    XtSetArg(args[i], XmNitems, &CSempty);				i++;
    fsb->fsb.family_scrolled_list_child =
	    XmCreateScrolledList(form, "familyScrolledList", args, i);
    XtAddCallback(fsb->fsb.family_scrolled_list_child,
		  XmNbrowseSelectionCallback, FamilySelect, (XtPointer) fsb);
    XtAddCallback(fsb->fsb.family_scrolled_list_child,
		  XmNdefaultActionCallback,
		  PreviewDoubleClick, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNtopAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNtopWidget, fsb->fsb.family_label_child);	i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.size_text_field_child);	i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_FORM);		i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNrightPosition, 50);				i++;
    XtSetValues(XtParent(fsb->fsb.family_scrolled_list_child), args, i);
    XtManageChild(fsb->fsb.family_scrolled_list_child);

    i = 0;
    XtSetArg(args[i], XmNitemCount, 1);					i++;
    XtSetArg(args[i], XmNitems, &CSempty);				i++;
    fsb->fsb.face_scrolled_list_child =
	    XmCreateScrolledList(form, "faceScrolledList", args, i);
    XtAddCallback(fsb->fsb.face_scrolled_list_child,
		  XmNbrowseSelectionCallback, FaceSelect, (XtPointer) fsb);
    XtAddCallback(fsb->fsb.face_scrolled_list_child,
		  XmNdefaultActionCallback, PreviewDoubleClick,
		  (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNtopAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNtopWidget, fsb->fsb.face_label_child);		i++;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.size_text_field_child);	i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNleftPosition, 50);				i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_FORM);		i++;
    XtSetValues(XtParent(fsb->fsb.face_scrolled_list_child), args, i);
    XtManageChild(fsb->fsb.face_scrolled_list_child);

    i = 0;
    XtSetArg(args[i], XmNbottomAttachment, XmATTACH_WIDGET);		i++;
    XtSetArg(args[i], XmNbottomWidget, fsb->fsb.size_text_field_child);	i++;
    XtSetArg(args[i], XmNleftAttachment, XmATTACH_POSITION);		i++;
    XtSetArg(args[i], XmNleftPosition, 50);				i++;
    XtSetArg(args[i], XmNrightAttachment, XmATTACH_FORM);		i++;
    fsb->fsb.multiple_master_button_child =
	    XtCreateWidget("multipleMasterButton", xmPushButtonWidgetClass,
			   form, args, i);
    XtAddCallback(fsb->fsb.multiple_master_button_child, XmNactivateCallback,
		  ShowCreatorCallback, (XtPointer) fsb);

    i = 0;
    XtSetArg(args[i], XmNdefaultButton, fsb->fsb.ok_button_child);	i++;
    XtSetValues(form, args, i);
}

static void DisplayFontFamilies(FontSelectionBoxWidget fsb)
{
    FontFamilyRec *ff;
    XmString *CSlist, *str;
    
    CSlist = (XmString *) XtMalloc(fsb->fsb.family_count * sizeof(XmString));
    str = CSlist;
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	*str++ = UnsharedCS(ff->family_name);
    }
    
    XtVaSetValues(fsb->fsb.family_scrolled_list_child,
		  XmNitemCount, fsb->fsb.family_count,
		  XmNitems, CSlist, NULL);		

    /* The list makes a copy, so we can delete the list */
    XtFree((char *) CSlist);
}

static void SetUpCurrentFontFromName(FontSelectionBoxWidget fsb)
{
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;
    int i, j;

    fsb->fsb.currently_selected_face = NULL;
    fsb->fsb.currently_selected_family = NULL;
    fsb->fsb.currently_selected_blend = NULL;

    if (fsb->fsb.font_name_multiple || fsb->fsb.font_name == NULL) {
	fsb->fsb.font_name = NULL;
	fsb->fsb.font_family = NULL;
	fsb->fsb.font_blend = NULL;
	fsb->fsb.font_face = NULL;
	if (fsb->fsb.font_name_multiple) {
	    fsb->fsb.current_family_multiple = True;
	    fsb->fsb.current_face_multiple = True;
	    ManageFamilyMultiple(fsb);
	    ManageFaceMultiple(fsb);
	}
	XmListDeselectAllItems(fsb->fsb.family_scrolled_list_child);
	XmListDeselectAllItems(fsb->fsb.face_scrolled_list_child);
	XmListDeleteAllItems(fsb->fsb.face_scrolled_list_child);
	XmListAddItem(fsb->fsb.face_scrolled_list_child, CSempty, 1);
	return;
    }

    if (!fsb->fsb.font_name_multiple) {
	fsb->fsb.current_family_multiple = False;
	fsb->fsb.current_face_multiple = False;
	UnmanageFamilyMultiple(fsb);
	UnmanageFaceMultiple(fsb);
    }

    fsb->fsb.font_name = Canonical(fsb->fsb.font_name);
    i = 1;
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	j = 1;
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == fsb->fsb.font_name) {
		fsb->fsb.font_family = ff->family_name;
		fsb->fsb.font_face = f->face_name;
		SetUpFaceList(fsb, ff);
		ListSelectPos(fsb->fsb.family_scrolled_list_child, i, False);
		ListSelectPos(fsb->fsb.face_scrolled_list_child, j, False);
		fsb->fsb.currently_selected_face = f;
		fsb->fsb.currently_selected_family = ff;
		fsb->fsb.currently_selected_blend = NULL;
		return;
	    }
	    j++;
	    if (f->blend_data != NULL && f->blend_data->blends != NULL) {
		for (b = f->blend_data->blends; b != NULL; b = b->next) {
		    if (b->font_name == fsb->fsb.font_name) {
			fsb->fsb.font_family = ff->family_name;
			fsb->fsb.font_face = f->face_name;
			SetUpFaceList(fsb, ff);
			ListSelectPos(fsb->fsb.family_scrolled_list_child, i,
				      False);
			ListSelectPos(fsb->fsb.face_scrolled_list_child, j,
				      False);
			fsb->fsb.currently_selected_face = f;
			fsb->fsb.currently_selected_family = ff;
			fsb->fsb.currently_selected_blend = b;
			return;
		    }
		    j++;
		}
	    }

	}
	i++;
    }
 
   /* Didn't find it! */
    fsb->fsb.font_name = NULL;
    fsb->fsb.font_family = NULL;
    fsb->fsb.font_face = NULL;
    fsb->fsb.font_blend = NULL;
    XmListDeselectAllItems(fsb->fsb.family_scrolled_list_child);
    XmListDeselectAllItems(fsb->fsb.face_scrolled_list_child);
    XmListDeleteAllItems(fsb->fsb.face_scrolled_list_child);
    XmListAddItem(fsb->fsb.face_scrolled_list_child, CSempty, 1);
}

static void SetUpCurrentFontFromFamilyFace(FontSelectionBoxWidget fsb)
{
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;
    int i;

    fsb->fsb.currently_selected_face = NULL;
    fsb->fsb.currently_selected_family = NULL;
    fsb->fsb.currently_selected_blend = NULL;

    if (fsb->fsb.font_family_multiple) {
	fsb->fsb.font_family = NULL;
	fsb->fsb.current_family_multiple = True;
	ManageFamilyMultiple(fsb);
    } else {
	fsb->fsb.current_family_multiple = False;
	UnmanageFamilyMultiple(fsb);
    }

    if (fsb->fsb.font_face_multiple) {
	fsb->fsb.font_face = NULL;
	fsb->fsb.current_face_multiple = True;
	ManageFaceMultiple(fsb);
    } else {
	fsb->fsb.current_face_multiple = False;
	UnmanageFaceMultiple(fsb);
    }

    fsb->fsb.font_name_multiple =
	    fsb->fsb.font_family_multiple || fsb->fsb.font_face_multiple;

    if (fsb->fsb.font_family != NULL) {
	fsb->fsb.font_family = Canonical(fsb->fsb.font_family);
	i = 1;
	for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	    if (fsb->fsb.font_family == ff->family_name) {
		ListSelectPos(fsb->fsb.family_scrolled_list_child, i, False);
		fsb->fsb.currently_selected_family = ff;
		SetUpFaceList(fsb, ff);
		break;
	    }
	    i++;
	}
	if (ff == NULL) fsb->fsb.font_family = NULL;
    }   
	
    if (fsb->fsb.font_family == NULL) {
	fsb->fsb.font_face = NULL;
	fsb->fsb.font_blend = NULL;
	fsb->fsb.font_name = NULL;
	XmListDeselectAllItems(fsb->fsb.family_scrolled_list_child);
	XmListDeselectAllItems(fsb->fsb.face_scrolled_list_child);
	XmListDeleteAllItems(fsb->fsb.face_scrolled_list_child);
	XmListAddItem(fsb->fsb.face_scrolled_list_child, CSempty, 1);
	return;
    }

    if (fsb->fsb.font_face != NULL) {
	fsb->fsb.font_face = Canonical(fsb->fsb.font_face);

	i = 1;
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (fsb->fsb.font_face == f->face_name) {
		fsb->fsb.currently_selected_face = f;
		if (fsb->fsb.font_blend != NULL) {
		    fsb->fsb.font_blend = Canonical(fsb->fsb.font_blend);
		    for (b = f->blend_data->blends; b != NULL; b = b->next) {
			i++;
			if (b->blend_name == fsb->fsb.font_blend) {
			    fsb->fsb.currently_selected_blend = b;
			    break;
			}
		    }
		    if (b == NULL) {
			fsb->fsb.font_blend = NULL;
			i -= f->blend_count;
		    }
		}
		ListSelectPos(fsb->fsb.face_scrolled_list_child, i, False);
		break;
	    }
	    i += f->blend_count + 1;
	}
	if (f == NULL) fsb->fsb.font_face = NULL;
    } else {
	f = NULL;
	XmListDeselectAllItems(fsb->fsb.face_scrolled_list_child);
    }

    if (f == NULL && !fsb->fsb.font_face_multiple) GetInitialFace(fsb, ff);
}

static void SetUpCurrentFont(FontSelectionBoxWidget fsb)
{
    if (fsb->fsb.use_font_name) SetUpCurrentFontFromName(fsb);
    else SetUpCurrentFontFromFamilyFace(fsb);
}

static void SetUpCurrentSize(FontSelectionBoxWidget fsb)
{
    char buf[20];

    if (fsb->fsb.font_size_multiple) {
	changingSize = True;
	XtVaSetValues(fsb->fsb.size_text_field_child, XmNvalue, "", NULL);
	changingSize = False;
	fsb->fsb.current_size_multiple = True;
	ManageSizeMultiple(fsb);
	return;
    } else UnmanageSizeMultiple(fsb);
    
    if (fsb->fsb.currently_selected_size == 0.0) {
	sprintf(buf, "%g", fsb->fsb.font_size);
    } else sprintf(buf, "%g", fsb->fsb.currently_selected_size);

    changingSize = True;
    XtVaSetValues(fsb->fsb.size_text_field_child, XmNvalue, buf, NULL);
    changingSize = False;
}

static void SetUpCurrentSelections(FontSelectionBoxWidget fsb)
{
    SetUpCurrentFont(fsb);
    SetUpCurrentSize(fsb);
    if (fsb->fsb.preview_on_change) DoPreview(fsb, False);
    DoValueChangedCallback(fsb);
}

/* ARGSUSED */

static void Initialize(
    Widget request, Widget new,
    ArgList args,
    Cardinal *num_args)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) new;
    Bool inited;
    char version[20];

    /* Verify size list */

    if (fsb->fsb.size_count > 0 && fsb->fsb.sizes == NULL) {
	XtAppWarningMsg(XtWidgetToApplicationContext(new),
			"initializeFontBox", "sizeMismatch",
			"FontSelectionBoxError",
			"Size count specified but no sizes present",
			(String *) NULL, (Cardinal *) NULL);
	fsb->fsb.size_count = 0;
    }

    if (fsb->fsb.size_count < 0) {
	XtAppWarningMsg(XtWidgetToApplicationContext(new),
			"initializeFontBox", "negativeSize",
			"FontSelectionBoxError",
			"Size count should not be negative",
			(String *) NULL, (Cardinal *) NULL);
	fsb->fsb.size_count = 0;
    }

    if (fsb->fsb.max_pending_deletes <= 0) {
	XtAppWarningMsg(XtWidgetToApplicationContext(new),
			"initializeFontBox", "nonPositivePendingDelete",
			"FontSelectionBoxError",
			"Pending delete max must be positive",
			(String *) NULL, (Cardinal *) NULL);
	fsb->fsb.max_pending_deletes = 1;
    }

    /* Copy strings.  SetUpCurrentSelection will copy the font strings */

    if (fsb->fsb.preview_string != NULL) {
	fsb->fsb.preview_string = XtNewString(fsb->fsb.preview_string);
    }
    if (fsb->fsb.default_resource_path != NULL) {
	fsb->fsb.default_resource_path =
		XtNewString(fsb->fsb.default_resource_path);
    }
    if (fsb->fsb.resource_path_override != NULL) {
	fsb->fsb.resource_path_override =
		XtNewString(fsb->fsb.resource_path_override);
    }

    /* Get the context */

    if (fsb->fsb.context == NULL) {
	fsb->fsb.context = XDPSGetSharedContext(XtDisplay(fsb));
    }

    if (_XDPSTestComponentInitialized(fsb->fsb.context,
				      dps_init_bit_fsb, &inited) ==
	dps_status_unregistered_context) {
	XDPSRegisterContext(fsb->fsb.context, False);
    }

    if (!inited) {
	(void) _XDPSSetComponentInitialized(fsb->fsb.context,
					    dps_init_bit_fsb);
	_DPSFDefineFontEnumFunctions(fsb->fsb.context);
    }

    DPSversion(fsb->fsb.context, 20, version);
    fsb->fsb.old_server = (atof(version) < 1007);

    /* Initialize non-resource fields */

    fsb->fsb.gstate = 0;
    fsb->fsb.sampler = fsb->fsb.creator = NULL;
    fsb->fsb.known_families = NULL;
    fsb->fsb.family_count = 0;
    fsb->fsb.currently_previewed = NULL;
    fsb->fsb.currently_selected_face = NULL;
    fsb->fsb.currently_selected_family = NULL;
    fsb->fsb.currently_previewed_blend = NULL;
    fsb->fsb.currently_selected_blend = NULL;
    fsb->fsb.currently_previewed_size = 0.0;
    fsb->fsb.currently_selected_size = 0.0;
    fsb->fsb.pending_delete_count = 0;
    fsb->fsb.pending_delete_font = NULL;
    fsb->fsb.preview_fixed = False;
    fsb->fsb.current_family_multiple = False;
    fsb->fsb.current_face_multiple = False;
    fsb->fsb.current_size_multiple = False;
    fsb->fsb.blends_changed = False;

    GetFontNames(fsb);
    CreateChildren(fsb);

    DisplayFontFamilies(fsb);
    SetUpCurrentSelections(fsb);
    DesensitizeReset(fsb);
    if (fsb->fsb.show_sampler) ShowSampler(fsb);
}

static void FreeFontRec(FontRec *f)
{
    BlendDataRec *bd;
    BlendRec *b, *next_b;

    if (f->blend_data != NULL) {
	bd = f->blend_data;
	for (b = bd->blends; b != NULL; b = next_b) {
	    next_b = b->next;
	    XtFree((char *) b);
	}
	XtFree((char *) bd->internal_break);
	XtFree((char *) bd->internal_value);
	XtFree((char *) bd->design_positions);
	XtFree((char *) bd);
    }
    XtFree(f->full_name);
}

static void FreeFontLists(
    FontSelectionBoxWidget fsb)
{
    FontFamilyRec *ff, *next_ff;
    FontRec *f, *next_f;

    /* font_name, face_name, family_name, and blend_name are canonical
       strings and so should not be freed.  The face and blend compound
       strings were gotten from converters and so should likewise remain. */

    for (ff = fsb->fsb.known_families; ff != NULL; ff = next_ff) {
	for (f = ff->fonts; f != NULL; f = next_f) {
	    FreeFontRec(f);
	    next_f = f->next;
	    XtFree((char *) f);
	}
	next_ff = ff->next;
	XtFree((char *) ff);
    }
    fsb->fsb.known_families = NULL;
}

static void Destroy(Widget widget)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) widget;

    /* Lots of stuff to destroy! */

    if (fsb->fsb.gstate != 0) XDPSFreeContextGState(fsb->fsb.context,
						    fsb->fsb.gstate);
    if (fsb->fsb.preview_string != NULL) XtFree(fsb->fsb.preview_string);
    if (fsb->fsb.default_resource_path != NULL) {
	XtFree(fsb->fsb.default_resource_path);
    }
    if (fsb->fsb.resource_path_override != NULL) {
	XtFree(fsb->fsb.resource_path_override);
    }

    FreeFontLists(fsb);
}

static void Resize(Widget widget)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) widget;

    XtResizeWidget(fsb->fsb.pane_child, fsb->core.width, fsb->core.height, 0);
}

/* ARGSUSED */

static Boolean SetValues(
    Widget old, Widget req, Widget new,
    ArgList args,
    Cardinal *num_args)
{
    FontSelectionBoxWidget oldfsb = (FontSelectionBoxWidget) old;
    FontSelectionBoxWidget newfsb = (FontSelectionBoxWidget) new;
    Boolean refreshLists = False, setSelection = False, do_preview = False;
    Bool inited;

#define NE(field) newfsb->fsb.field != oldfsb->fsb.field
#define DONT_CHANGE(field) \
    if (NE(field)) newfsb->fsb.field = oldfsb->fsb.field;

    DONT_CHANGE(typographic_sort);
    DONT_CHANGE(pane_child);
    DONT_CHANGE(preview_child);
    DONT_CHANGE(panel_child);
    DONT_CHANGE(family_label_child);
    DONT_CHANGE(family_multiple_label_child);
    DONT_CHANGE(family_scrolled_list_child);
    DONT_CHANGE(face_label_child);
    DONT_CHANGE(face_multiple_label_child);
    DONT_CHANGE(face_scrolled_list_child);
    DONT_CHANGE(size_label_child);
    DONT_CHANGE(size_text_field_child);
    DONT_CHANGE(size_option_menu_child);
    DONT_CHANGE(preview_button_child);
    DONT_CHANGE(sampler_button_child);
    DONT_CHANGE(separator_child);
    DONT_CHANGE(ok_button_child);
    DONT_CHANGE(apply_button_child);
    DONT_CHANGE(reset_button_child);
    DONT_CHANGE(cancel_button_child);
#undef DONT_CHANGE

    if (newfsb->fsb.size_count > 0 && newfsb->fsb.sizes == NULL) {
	XtAppWarningMsg(XtWidgetToApplicationContext(new),
			"setValuesFontBox", "sizeMismatch",
			"FontSelectionBoxError",
			"Size count specified but no sizes present",
			(String *) NULL, (Cardinal *) NULL);
	newfsb->fsb.size_count = 0;
    }

    if (newfsb->fsb.size_count < 0) {
	XtAppWarningMsg(XtWidgetToApplicationContext(new),
			"setValuesFontBox", "negativeSize",
			"FontSelectionBoxError",
			"Size count should not be negative",
			(String *) NULL, (Cardinal *) NULL);
	newfsb->fsb.size_count = 0;
    }

    if (newfsb->fsb.max_pending_deletes <= 0) {
	XtAppWarningMsg(XtWidgetToApplicationContext(new),
			"setValuesFontBox", "nonPositivePendingDelete",
			"FontSelectionBoxError",
			"Pending delete max must be positive",
			(String *) NULL, (Cardinal *) NULL);
	newfsb->fsb.max_pending_deletes = 1;
    }

    if (NE(preview_string)) {
	XtFree(oldfsb->fsb.preview_string);
	newfsb->fsb.preview_string = XtNewString(newfsb->fsb.preview_string);
	do_preview = True;
    }

    if (NE(default_resource_path)) {
	XtFree(oldfsb->fsb.default_resource_path);
	newfsb->fsb.default_resource_path =
		XtNewString(newfsb->fsb.default_resource_path);
	refreshLists = True;
    }

    if (NE(resource_path_override)) {
	XtFree(oldfsb->fsb.resource_path_override);
	newfsb->fsb.resource_path_override =
		XtNewString(newfsb->fsb.resource_path_override);
	refreshLists = True;
    }

    if (newfsb->fsb.undef_unused_fonts) UndefSomeUnusedFonts(newfsb, False);

    if (NE(context)) {
	if (newfsb->fsb.context == NULL) {
	    newfsb->fsb.context = XDPSGetSharedContext(XtDisplay(newfsb));
	} 
	if (_XDPSTestComponentInitialized(newfsb->fsb.context,
					  dps_init_bit_fsb, &inited) ==
	    dps_status_unregistered_context) {
	    XDPSRegisterContext(newfsb->fsb.context, False);
	}
	if (!inited) {
	    (void) _XDPSSetComponentInitialized(newfsb->fsb.context,
						dps_init_bit_fsb);
	    _DPSFDefineFontEnumFunctions(newfsb->fsb.context);
	}
    }	

    if (refreshLists) {
	UndefUnusedFonts((Widget)newfsb);
	newfsb->fsb.pending_delete_font = NULL;
	newfsb->fsb.pending_delete_count = 0;
	FreeFontLists(newfsb);
	GetFontNames(newfsb);
	DisplayFontFamilies(newfsb);
	setSelection = True;
    }

    if (NE(sizes)) {
	CreateSizeMenu(newfsb, True);
	setSelection = True;
    }

    if (NE(show_sampler)) {
	if (newfsb->fsb.show_sampler) ShowSampler(newfsb);
	else XtPopdown(newfsb->fsb.sampler);
    }

    if (NE(show_sampler_button)) {
	if (newfsb->fsb.show_sampler_button) {
	    XtManageChild(newfsb->fsb.sampler_button_child);
	} else XtUnmanageChild(newfsb->fsb.sampler_button_child);
    }

    if (NE(font_size)) newfsb->fsb.currently_selected_size = 0.0;

    if (NE(use_font_name) || NE(font_name) || NE(font_family) ||
	NE(font_face) || NE(font_size) || NE(font_name_multiple) ||
	NE(font_family_multiple) || NE(font_face_multiple) ||
	NE(font_size_multiple) || NE(font_blend)) setSelection = True;

    if (setSelection) SetUpCurrentSelections(newfsb);
    else if (do_preview && newfsb->fsb.preview_on_change) {
	DoPreview(newfsb, False);
    }

    if ((NE(font_name) || NE(font_size)) &&
	XtIsSensitive(newfsb->fsb.reset_button_child)) {

	if ((newfsb->fsb.font_size_multiple ||
	     newfsb->fsb.font_size == newfsb->fsb.currently_selected_size) &&
	    (newfsb->fsb.font_name_multiple ||
	     newfsb->fsb.currently_selected_face == NULL ||
	     newfsb->fsb.font_name ==
		    newfsb->fsb.currently_selected_face->font_name)) {
	    DesensitizeReset(newfsb);
	}
    }

    return False;
#undef NE
}

/* ARGSUSED */

static XtGeometryResult GeometryManager(
    Widget w,
    XtWidgetGeometry *desired, XtWidgetGeometry *allowed)
{
#define WANTS(flag) (desired->request_mode & flag)

    if (WANTS(XtCWQueryOnly)) return XtGeometryYes;

    if (WANTS(CWWidth)) w->core.width = desired->width;
    if (WANTS(CWHeight)) w->core.height = desired->height;
    if (WANTS(CWX)) w->core.x = desired->x;
    if (WANTS(CWY)) w->core.y = desired->y;
    if (WANTS(CWBorderWidth)) {
	w->core.border_width = desired->border_width;
    }

    return XtGeometryYes;
#undef WANTS
}

static void ChangeManaged(Widget w)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;

    w->core.width = fsb->composite.children[0]->core.width;
    w->core.height = fsb->composite.children[0]->core.height;
}

static void SetFontName(
    Widget w,
    String name,
    Bool name_multiple)
{
    XtVaSetValues(w, XtNfontName, name, XtNuseFontName, True,
		  XtNfontNameMultiple, name_multiple, NULL);
}

void FSBSetFontName(
    Widget w,
    String name,
    Bool name_multiple)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->fsb_class.set_font_name)
	    (w, name, name_multiple);
}

static void SetFontFamilyFace(
    Widget w,
    String family, String face,
    Bool family_multiple, Bool face_multiple)
{
    XtVaSetValues(w, XtNfontFamily, family, XtNfontFace, face,
		  XtNuseFontName, False,
		  XtNfontFamilyMultiple, family_multiple,
		  XtNfontFaceMultiple, face_multiple, NULL);
}

void FSBSetFontFamilyFace(
    Widget w,
    String family, String face,
    Bool family_multiple, Bool face_multiple)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.set_font_family_face)
	    (w, family, face, family_multiple, face_multiple);
}

static void SetFontSize(
    Widget w,
    double size,
    Bool size_multiple)
{
    int i;
    Arg args[2];

    union {
	int i;
	float f;
    } kludge;

    kludge.f = size;

    i = 0;
    if (sizeof(float) > sizeof(XtArgVal)) {
	XtSetArg(args[i], XtNfontSize, &kludge.f);			i++;
    } else XtSetArg(args[i], XtNfontSize, kludge.i);			i++;
    XtSetArg(args[i], XtNfontSizeMultiple, size_multiple);		i++;
    XtSetValues(w, args, i);
}

void FSBSetFontSize(
    Widget w,
    double size,
    Bool size_multiple)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->fsb_class.set_font_size)
	    (w, size, size_multiple);
}

static void RefreshFontList(Widget w)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;

    UndefUnusedFonts((Widget)fsb);
    fsb->fsb.pending_delete_font = NULL;
    fsb->fsb.pending_delete_count = 0;
    FreeFontLists(fsb);
    FreePSResourceStorage(True);
    GetFontNames(fsb);
    DisplayFontFamilies(fsb);
    SetUpCurrentSelections(fsb);
}

void FSBRefreshFontList(
    Widget w)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.refresh_font_list) (w);
}

static void GetFamilyList(
    Widget w,
    int *count,
    String **list)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    String *buf;
    FontFamilyRec *ff;

    *count = fsb->fsb.family_count;
    *list = buf = (String *) XtMalloc(*count * sizeof(String));
    
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	*buf++ = ff->family_name;
    }
}

void FSBGetFamilyList(
    Widget w,
    int *count,
    String **list)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.get_family_list) (w, count, list);
}

static void GetFaceList(
    Widget w,
    String family,
    int *count,
    String **face_list, String **font_list)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    String *buf1, *buf2;
    FontFamilyRec *ff;
    FontRec *f;
    
    family = Canonical(family);
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	if (ff->family_name == family) break;
    }

    if (ff == NULL) {
	*count = 0;
	*face_list = *font_list = NULL;
	return;
    }

    *count = ff->font_count;
    *face_list = buf1 = (String *) XtMalloc(*count * sizeof(String));
    *font_list = buf2 = (String *) XtMalloc(*count * sizeof(String));

    for (f = ff->fonts; f != NULL; f = f->next) {
	*buf1++ = f->face_name;
	*buf2++ = f->font_name;
    }
}

void FSBGetFaceList(
    Widget w,
    String family,
    int *count_return,
    String **face_list, String **font_list)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.get_face_list) (w, family, count_return,
					      face_list, font_list);
}

void FSBUndefineUnusedFonts(
    Widget w)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.undef_unused_fonts) (w);
}

static Boolean DownloadFontName(Widget w, String name)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    FontFamilyRec *ff;
    FontRec *f;
    Boolean ret;

    name = Canonical(name);
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == name) {
		if (!fsb->fsb.get_server_fonts) {
		    int resident;
		    _DPSFIsFontResident(fsb->fsb.context, f->font_name,
					&resident);
		    if (resident) f->resident = True;
		}
		if (f->resident) return True;
		else {
		    ret = DownloadFont(fsb, name, fsb->fsb.context,
				       fsb->fsb.make_fonts_shared);
		    if (fsb->fsb.make_fonts_shared && ret) f->resident = True;
		    return ret;
		}
	    }
	}
    }
 
    return DownloadFont(fsb, name, fsb->fsb.context,
			fsb->fsb.make_fonts_shared);
}

Boolean FSBDownloadFontName(
    Widget w,
    String name)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    if (name == NULL) return False;
    return (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.download_font_name) (w, name);
}

static Boolean MatchFontFace(
    Widget w,
    String old_face, String new_family,
    String *new_face)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    FSBFaceSelectCallbackRec rec;
    String *faces;
    int i;
    FontFamilyRec *ff;
    FontRec *f;
    Boolean retVal;

    new_family = Canonical(new_family);
    old_face = Canonical(old_face);
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	if (ff->family_name == new_family) break;
    }
    if (ff == NULL) {
	*new_face = NULL;
	return False;
    }

    faces = (String *) XtMalloc(ff->font_count * sizeof(String));
    i = 0;
    for (f = ff->fonts; f != NULL; f = f->next) faces[i++] = f->face_name;

    rec.available_faces = faces;
    rec.num_available_faces = ff->font_count;
    rec.current_face = old_face;
    rec.new_face = NULL;

    i = MatchFaceName(&rec, &retVal);
    *new_face = faces[i];
    XtFree((XtPointer) faces);
    return !retVal;
}

Boolean FSBMatchFontFace(
    Widget w,
    String old_face, String new_family,
    String *new_face)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    return (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.match_font_face) (w, old_face,
						new_family, new_face);
}

static void FontNameToFamilyFaceBlend(
    Widget w,
    String font_name,
    String *family, String *face, String *blend)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;

    font_name = Canonical(font_name);
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == font_name) {
		*family = ff->family_name;
		*face = f->face_name;
		*blend = NULL;
		return;
	    }
	    if (f->blend_data != NULL) {
		for (b = f->blend_data->blends; b != NULL; b = b->next) {
		    if (b->font_name == font_name) {
			*family = ff->family_name;
			*face = f->face_name;
			*blend = b->blend_name;
			return;
		    }
		}
	    }
	}
    }

    *family = NULL;
    *face = NULL;
    *blend = NULL;
}

static void FontNameToFamilyFace(
    Widget w,
    String font_name,
    String *family, String *face)
{
    String blend;

    FontNameToFamilyFaceBlend(w, font_name, family, face, &blend);
}

void FSBFontNameToFamilyFace(
    Widget w,
    String font_name,
    String *family, String *face)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.font_name_to_family_face) (w, font_name,
							 family, face);
}

static void FontFamilyFaceBlendToName(
    Widget w,
    String family, String face, String blend,
    String *font_name)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;

    family = Canonical(family);
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	if (ff->family_name == family) break;
    }
    if (ff == NULL) {
	*font_name = NULL;
	return;
    }

    face = Canonical(face);
    for (f = ff->fonts; f != NULL; f = f->next) {
	if (f->face_name == face) break;
    }
    if (f == NULL) {
	*font_name = NULL;
	return;
    }

    if (blend == NULL) {
	*font_name = f->font_name;
	return;
    }
    if (f->blend_data == NULL) {
	*font_name = NULL;
	return;
    }

    blend = Canonical(blend);
    for (b = f->blend_data->blends; b != NULL; b = b->next) {
	if (b->blend_name == blend) {
	    *font_name = b->font_name;
	    return;
	}
    }
    *font_name = NULL;
}

static void FontFamilyFaceToName(
    Widget w,
    String family, String face,
    String *font_name)
{
    FontFamilyFaceBlendToName(w, family, face, NULL, font_name);
}

void FSBFontFamilyFaceToName(
    Widget w,
    String family, String face,
    String *font_name)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass)
       XtClass(w))->fsb_class.font_family_face_to_name) (w, family, face,
							 font_name);
}

String FSBFindAFM(
    Widget w,
    String font_name)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    return (*((FontSelectionBoxWidgetClass) XtClass(w))->
	    fsb_class.find_afm) (w, font_name);
}

String FSBFindFontFile(
    Widget w,
    String font_name)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    return (*((FontSelectionBoxWidgetClass) XtClass(w))->
	    fsb_class.find_font_file) (w, font_name);
}

static void GetTextDimensions(
    Widget w,
    String text, String font,
    double size, double x, double y,
    float *dx, float *dy, float *left, float *right, float *top, float *bottom)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    int bogusFont;

    _DPSFGetTextDimensions(fsb->fsb.context, text, font, size, x, y,
			   dx, dy, left, right, top, bottom, &bogusFont);
}

void FSBGetTextDimensions(
    Widget w,
    String text, String font,
    double size, double x, double y,
    float *dx, float *dy, float *left, float *right, float *top, float *bottom)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->
	fsb_class.get_text_dimensions) (w, text, font, size, x, y,
					dx, dy, left, right, top, bottom);
}

static void SetFontFamilyFaceBlend(
    Widget w,
    String family,
    String face,
    String blend,
    Bool family_multiple,
    Bool face_multiple)
{
    XtVaSetValues(w, XtNfontFamily, family, XtNfontFace, face,
		  XtNfontBlend, blend, XtNuseFontName, False,
		  XtNfontFamilyMultiple, family_multiple,
		  XtNfontFaceMultiple, face_multiple, NULL);
}

void FSBSetFontFamilyFaceBlend(
    Widget w,
    String font_family,
    String font_face,
    String font_blend,
    Bool font_family_multiple,
    Bool font_face_multiple)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->
        fsb_class.set_font_family_face_blend) (w, font_family, font_face,
					       font_blend,
					       font_family_multiple,
					       font_face_multiple);
}

void FSBFontNameToFamilyFaceBlend(
    Widget w,
    String font_name,
    String *family,
    String *face,
    String *blend)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->
        fsb_class.font_name_to_family_face_blend) (w, font_name, family,
						   face, blend);
}
    
void FSBFontFamilyFaceBlendToName(
    Widget w,
    String family,
    String face,
    String blend,
    String *font_name)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->
        fsb_class.font_family_face_blend_to_name) (w, family, face,
						   blend, font_name);
}

static void GetBlendList(
    Widget w,
    String name,
    int *count_return,
    String **blend_return,
    String **font_name_return,
    float **axis_values_return)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    String *buf1, *buf2;
    float *buf3;
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;
    int i;

    name = Canonical(name);
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == name) break;
	}
    }

    if (ff == NULL || f == NULL || f->blend_data == NULL) {
	*count_return = 0;
	*blend_return = *font_name_return = NULL;
	*axis_values_return = NULL;
	return;
    }
	
    *count_return = f->blend_count;
    *blend_return = buf1 = (String *) XtMalloc(*count_return * sizeof(String));
    *font_name_return = buf2 =
	    (String *) XtMalloc(*count_return * sizeof(String));
    *axis_values_return = buf3 =
	    (float *) XtMalloc(*count_return * MAX_AXES * sizeof(float));


    for (b = f->blend_data->blends; b != NULL; b = b->next) {
	*buf1++ = b->blend_name;
	*buf2++ = b->font_name;
	for (i = 0; i < MAX_AXES; i++) *buf3++ = b->data[i];
    }
}

void FSBGetBlendList(
    Widget w,
    String name,
    int *count_return,
    String **blend_return,
    String **font_name_return,
    float **axis_values_return)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->
        fsb_class.get_blend_list) (w, name, count_return, blend_return,
				   font_name_return, axis_values_return);
}

static void GetBlendInfo(
    Widget w,
    String name,
    int *num_axes_return,
    int *num_designs_return,
    String **axis_names_return,
    float **blend_positions_return,
    int **blend_map_count_return,
    int **blend_design_coords_return,
    float **blend_normalized_coords_return)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    FontFamilyRec *ff;
    FontRec *f;
    BlendDataRec *bd;
    int i, j;
    float *fbuf;
    int *ibuf;
    String *sbuf;
    int coords;

    name = Canonical(name);
    if (fsb->fsb.currently_selected_face->font_name == name) {
	bd = fsb->fsb.currently_selected_face->blend_data;
    } else {

	for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	    for (f = ff->fonts; f != NULL; f = f->next) {
		if (f->font_name == name) goto FOUND_IT;
	     }
	}
	*num_axes_return = *num_designs_return = 0;
	*axis_names_return = NULL;
	*blend_positions_return = *blend_normalized_coords_return = NULL;
	*blend_map_count_return = *blend_design_coords_return = NULL;
	return;

FOUND_IT:
	bd = f->blend_data;
    }

    *num_axes_return = bd->num_axes;
    *num_designs_return = bd->num_designs;

    *axis_names_return = sbuf =
	    (String *) XtMalloc(bd->num_axes * sizeof(String));
    *blend_map_count_return = ibuf =
	    (int *) XtMalloc(bd->num_axes * sizeof(int));
    coords = 0;
    for (i = 0; i < bd->num_axes; i++) {
	*sbuf++ = bd->name[i];
	*ibuf++ = bd->internal_points[i] + 2;
	coords += bd->internal_points[i] + 2;
    }

    *blend_positions_return = fbuf =
	    (float *) XtMalloc(bd->num_axes * bd->num_designs * sizeof(float));
    for (i = 0; i < bd->num_axes * bd->num_designs; i++) {
	*fbuf++ = bd->design_positions[i];
    }

    *blend_design_coords_return = ibuf =
	    (int *) XtMalloc(coords * sizeof(int));
    *blend_normalized_coords_return = fbuf =
	    (float *) XtMalloc(coords * sizeof(float));

    for (i = 0; i < bd->num_axes; i++) {
	*ibuf++ = bd->min[i];
	*fbuf++ = 0.0;
	for (j = 0; j < bd->internal_points[i]; j++) {
	    *ibuf++ = bd->internal_break[i][j];
	    *fbuf++ = bd->internal_value[i][j];
	}
	*ibuf++ = bd->max[i];
	*fbuf++ = 1.0;
    }
}

void FSBGetBlendInfo(
    Widget w,
    String name,
    int *num_axes_return,
    int *num_designs_return,
    String **axis_names_return,
    float **blend_positions_return,
    int **blend_map_count_return,
    int **blend_design_coords_return,
    float **blend_normalized_coords_return)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    (*((FontSelectionBoxWidgetClass) XtClass(w))->
        fsb_class.get_blend_info) (w, name, num_axes_return,
				   num_designs_return, axis_names_return,
				   blend_positions_return,
				   blend_map_count_return,
				   blend_design_coords_return,
				   blend_normalized_coords_return);
}

static Boolean ChangeBlends(
    Widget w,
    String base_name,
    String blend_name,
    FSBBlendAction action,
    int *axis_values,
    float *axis_percents)
{
    FontSelectionBoxWidget fsb = (FontSelectionBoxWidget) w;
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b = NULL, *newb, **lastb;
    BlendDataRec *bd;
    String spaceBlend;
    int val[4];
    float pct[4];
    int i;

    base_name = Canonical(base_name);
    blend_name = Canonical(blend_name);

    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == base_name) {
		if ((bd = f->blend_data) == NULL) return False;

		for (b = f->blend_data->blends; b != NULL; b = b->next) {
		    if (b->blend_name == blend_name) break;
		}
		goto FOUND_BASE;
	    }
	}
    }
    return False;

FOUND_BASE:
    if (action != FSBDeleteBlend) { 
	if (axis_values != NULL) {
	    for (i = 0; i < bd->num_axes; i++) {
		val[i] = axis_values[i];
		pct[i] = _FSBNormalize(val[i], bd, i);
	    }
	    for (/**/; i < 4; i++) pct[i] = 0.0;
	} else {
	    if (axis_percents == NULL) return False;
	    for (i = 0; i < bd->num_axes; i++) {
		pct[i] = axis_percents[i];
		val[i] = _FSBUnnormalize(pct[i], bd, i);
	    }
	    for (/**/; i < 4; i++) pct[i] = 0.0;
	}
    }

    switch (action) {
	case FSBAddBlend:
	    if (b != NULL) return False;
	    newb = XtNew(BlendRec);
	    newb->blend_name = blend_name;
	    newb->CS_blend_name = CS(blend_name, (Widget) fsb);

	    spaceBlend = (char *) XtMalloc(strlen(blend_name) + 4);
	    spaceBlend[0] = spaceBlend[1] = spaceBlend[2] = ' ';
	    strcpy(spaceBlend+3, blend_name);
	    newb->CS_space_blend_name = CS(spaceBlend, (Widget) fsb);
	    XtFree((XtPointer) spaceBlend);

	    for (i = 0; i < MAX_AXES; i++) newb->data[i] = pct[i];
	    newb->font_name = _FSBGenFontName(base_name, val, bd);

	    f->blend_count++;
	    ff->blend_count++;

	    lastb = &bd->blends;
	    for (b = bd->blends; b != NULL; b = b->next) {
		if (strcmp(blend_name, b->blend_name) < 0) break;
		lastb = &b->next;
	    }

	    newb->next = b;
	    *lastb = newb;
	    break;

	case FSBReplaceBlend:
	    if (b == NULL) return False;

	    for (i = 0; i < MAX_AXES; i++) b->data[i] = pct[i];
	    b->font_name = _FSBGenFontName(base_name, val, bd);
	    if (b == fsb->fsb.currently_previewed_blend) DoPreview(fsb, False);

	    break;

	case FSBDeleteBlend:
	    if (b == NULL) return False;

	    if (bd->blends == b) {
		bd->blends = b->next;
	    } else {
		for (newb = bd->blends; newb->next != b; newb = newb->next) {}
		newb->next = b->next;
	    }

	    f->blend_count--;
	    ff->blend_count--;

	    /* Don't actually delete the blend record, in case it's displayed
	       in the sampler. */
	    break;
    }
    if (f->in_font_creator) _FSBSetCreatorFamily(fsb->fsb.creator, ff);
    if (ff == fsb->fsb.currently_selected_family) SetUpFaceList(fsb, ff);
    fsb->fsb.blends_changed = True;
    WriteBlends(fsb);
    return True;
}

Boolean FSBChangeBlends(
    Widget w,
    String base_name,
    String blend_name,
    FSBBlendAction action,
    int *axis_values,
    float *axis_percents)
{
    XtCheckSubclass(w, fontSelectionBoxWidgetClass, NULL);

    return (*((FontSelectionBoxWidgetClass) XtClass(w))->
	    fsb_class.change_blends) (w, base_name, blend_name, action,
				    axis_values, axis_percents);
}

void _FSBSetCurrentFont(
    FontSelectionBoxWidget fsb,
    String name)
{
    FontFamilyRec *ff;
    FontRec *f;
    BlendRec *b;
    int i, j;

    fsb->fsb.current_family_multiple = False;
    fsb->fsb.current_face_multiple = False;
    UnmanageFamilyMultiple(fsb);
    UnmanageFaceMultiple(fsb);

    name = Canonical(name);
    i = 1;
    for (ff = fsb->fsb.known_families; ff != NULL; ff = ff->next) {
	j = 1;
	for (f = ff->fonts; f != NULL; f = f->next) {
	    if (f->font_name == name) {
		b = NULL;
		goto FOUND_NAME;
	    }
	    j++;
	    if (f->blend_data != NULL && f->blend_data->blends != NULL) {
		for (b = f->blend_data->blends; b != NULL; b = b->next) {
		    if (b->font_name == name) {
			goto FOUND_NAME;
		    }
		    j++;
		}
	    }

	}
	i++;
    }
    return;
FOUND_NAME:
    SetUpFaceList(fsb, ff);
    ListSelectPos(fsb->fsb.family_scrolled_list_child, i, False);
    ListSelectPos(fsb->fsb.face_scrolled_list_child, j, False);
    fsb->fsb.currently_selected_face = f;
    fsb->fsb.currently_selected_family = ff;
    fsb->fsb.currently_selected_blend = b;
    SensitizeReset(fsb);
    DoPreview(fsb, False);
}

float _FSBNormalize(
    int val,
    BlendDataRec *bd,
    int i)
{
    int j;
    int lessBreak, moreBreak;
    float lessValue, moreValue;

    if (bd->internal_points[i] == 0) {
	return ((float) (val - bd->min[i])) /
		((float) (bd->max[i] - bd->min[i]));
    }

    /* Find the largest breakpoint less than val and the smallest one greater
       than it */

    lessBreak = bd->min[i];
    lessValue = 0.0;
    moreBreak = bd->max[i];
    moreValue = 1.0;

    for (j = 0; j < bd->internal_points[i]; j++) {
	if (bd->internal_break[i][j] > lessBreak &&
	    bd->internal_break[i][j] <= val) {
	    lessBreak = bd->internal_break[i][j];
	    lessValue = bd->internal_value[i][j];
	}
	if (bd->internal_break[i][j] < moreBreak &&
	    bd->internal_break[i][j] >= val) {
	    moreBreak = bd->internal_break[i][j];
	    moreValue = bd->internal_value[i][j];
	}
    }

    if (moreBreak == lessBreak) return moreValue;

    return lessValue + (moreValue - lessValue) *
	    ((float) (val - lessBreak)) / ((float) (moreBreak - lessBreak));
}

int _FSBUnnormalize(val, bd, i)
    float val;
    BlendDataRec *bd;
    int i;
{
    int j;
    int lessBreak, moreBreak;
    float lessValue, moreValue;

    if (bd->internal_points[i] == 0) {
	return val * (bd->max[i] - bd->min[i]) + bd->min[i] + 0.5;
    }

    /* Find the largest breakpoint less than val and the smallest one greater
       than it */

    lessBreak = bd->min[i];
    lessValue = 0.0;
    moreBreak = bd->max[i];
    moreValue = 1.0;

    for (j = 0; j < bd->internal_points[i]; j++) {
	if (bd->internal_value[i][j] > lessValue &&
	    bd->internal_value[i][j] <= val) {
	    lessBreak = bd->internal_break[i][j];
	    lessValue = bd->internal_value[i][j];
	}
	if (bd->internal_value[i][j] < moreBreak &&
	    bd->internal_value[i][j] >= val) {
	    moreBreak = bd->internal_break[i][j];
	    moreValue = bd->internal_value[i][j];
	}
    }

    if (moreBreak == lessBreak) return moreBreak;

    return ((float) (val - lessValue)) / ((float) (moreValue - lessValue)) *
	    (moreBreak - lessBreak) + lessBreak + 0.5;
}

String _FSBGenFontName(
    String name,
    int *val,
    BlendDataRec *bd)
{
    char nameBuf[256];
    int i;
    char *ch;

    strcpy(nameBuf, name);
    ch = nameBuf + strlen(nameBuf);

    for (i = 0; i < bd->num_axes; i++) {
	sprintf(ch, "_%d_%s", val[i], bd->name[i]);
	ch = ch + strlen(ch);
    }

    return Canonical(nameBuf);
}