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 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091
|
/* Copyright (C) 2005-2012 by George Williams and Alexey Kryukov */
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stemdb.h"
#include "autohint.h"
#include "dumppfa.h"
#include "fontforge.h"
#include "edgelist2.h"
#include "splineoverlap.h"
#include "splineutil.h"
#include "splineutil2.h"
#include <math.h>
#include <utype.h>
#define GLYPH_DATA_DEBUG 0
#define PI 3.14159265358979323846264338327
/* A diagonal end is like the top or bottom of a slash. Should we add a vertical stem at the end? */
/* A diagonal corner is like the bottom of circumflex. Should we add a horizontal stem? */
int hint_diagonal_ends = 0,
hint_diagonal_intersections = 0,
hint_bounding_boxes = 1,
detect_diagonal_stems = 0;
float stem_slope_error = .05061454830783555773, /* 2.9 degrees */
stub_slope_error = .317649923862967983; /* 18.2 degrees */
static double dist_error_hv = 3.5;
static double dist_error_diag = 5.5;
/* It's easy to get horizontal/vertical lines aligned properly */
/* it is more difficult to get diagonal ones done */
/* The "A" glyph in Apple's Times.dfont(Roman) is off by 6 in one spot */
static double dist_error_curve = 22;
/* The maximum possible distance between the edge of an active zone for */
/* a curved spline segment and the spline itself */
struct st {
Spline *s;
double st, lt;
};
static int GetBlueFuzz(SplineFont *sf) {
char *str, *end;
if ( sf == NULL || sf->private == NULL ||
(str=PSDictHasEntry( sf->private,"BlueFuzz" )) == NULL || !isdigit( str[0] ))
return 1;
return strtod( str, &end );
}
static int IsUnitHV( BasePoint *unit,int strict ) {
double angle = atan2( unit->y,unit->x );
double deviation = ( strict ) ? stem_slope_error : stub_slope_error;
if ( fabs( angle ) >= PI/2 - deviation && fabs( angle ) <= PI/2 + deviation )
return( 2 );
else if ( fabs( angle ) <= deviation || fabs( angle ) >= PI - deviation )
return( 1 );
return( 0 );
}
static int UnitCloserToHV( BasePoint *u1,BasePoint *u2 ) {
double adiff1, adiff2;
adiff1 = fabs( atan2( u1->y,u1->x ));
adiff2 = fabs( atan2( u2->y,u2->x ));
if ( adiff1 > PI*.25 && adiff1 < PI*.75 )
adiff1 = fabs( adiff1 - PI*.5 );
else if ( adiff1 >= PI*.75 )
adiff1 = PI - adiff1;
if ( adiff2 > PI*.25 && adiff2 < PI*.75 )
adiff2 = fabs( adiff2 - PI*.5 );
else if ( adiff2 >= PI*.75 )
adiff2 = PI - adiff2;
if ( adiff1 < adiff2 )
return( 1 );
else if ( adiff1 > adiff2 )
return( -1 );
else
return( 0 );
}
static double GetUnitAngle( BasePoint *u1,BasePoint *u2 ) {
double dx, dy;
dy = u1->x*u2->y - u1->y*u2->x;
dx = u1->x*u2->x + u1->y*u2->y;
return( atan2( dy,dx ));
}
static int UnitsOrthogonal( BasePoint *u1,BasePoint *u2,int strict ) {
double angle, deviation = ( strict ) ? stem_slope_error : stub_slope_error;
angle = GetUnitAngle( u1,u2 );
return( fabs( angle ) >= PI/2 - deviation && fabs( angle ) <= PI/2 + deviation );
}
int UnitsParallel( BasePoint *u1,BasePoint *u2,int strict ) {
double angle, deviation = ( strict ) ? stem_slope_error : stub_slope_error;
angle = GetUnitAngle( u1,u2 );
return( fabs( angle ) <= deviation || fabs( angle ) >= PI - deviation );
}
static int IsInflectionPoint( struct glyphdata *gd,struct pointdata *pd ) {
SplinePoint *sp = pd->sp;
double CURVATURE_THRESHOLD = 1e-9;
struct spline *prev, *next;
double in, out;
if ( sp->prev == NULL || sp->next == NULL || !pd->colinear )
return( false );
/* point of a single-point contour can't be an inflection point. */
if ( sp->prev->from == sp )
return( false );
prev = sp->prev;
in = 0;
while ( prev != NULL && fabs(in) < CURVATURE_THRESHOLD ) {
in = SplineCurvature( prev,1 );
if ( fabs( in ) < CURVATURE_THRESHOLD ) in = SplineCurvature( prev, 0 );
if ( fabs( in ) < CURVATURE_THRESHOLD ) prev = prev->from->prev;
if ( gd->points[prev->to->ptindex].colinear )
break;
}
next = sp->next;
out = 0;
while ( next != NULL && fabs( out ) < CURVATURE_THRESHOLD ) {
out = SplineCurvature( next,0 );
if ( fabs( out ) < CURVATURE_THRESHOLD ) out = SplineCurvature( next, 1 );
if ( fabs( out ) < CURVATURE_THRESHOLD ) next = next->to->next;
if ( gd->points[next->from->ptindex].colinear )
break;
}
if ( in==0 || out==0 || ( prev != sp->prev && next != sp->next ))
return( false );
in/=fabs(in);
out/=fabs(out);
return ( in*out < 0 );
}
static int SplineFigureOpticalSlope(Spline *s,int start_at_from,BasePoint *dir) {
/* Sometimes splines have tiny control points, and to the eye the slope */
/* of the spline has nothing to do with that specified by the cps. */
/* So see if the spline is straightish and figure the slope based on */
/* some average direction */
/* dir is a input output parameter. */
/* it should be initialized to the unit vector determined by the appropriate cp */
/* if the function returns true, it will be set to a unit vector in the average direction */
BasePoint pos, *base, average_dir, normal;
double t, len, incr, off;
double dx, dy, ax, ay, d, a;
/* The vector is already nearly vertical/horizontal, no need to modify*/
if ( IsUnitHV( dir,true ))
return( false );
if ( start_at_from ) {
incr = -.1;
base = &s->from->me;
} else {
incr = .1;
base = &s->to->me;
}
t = .5-incr;
memset(&average_dir,0,sizeof(average_dir));
while ( t>0 && t<1.0 ) {
pos.x = ((s->splines[0].a*t+s->splines[0].b)*t+s->splines[0].c)*t+s->splines[0].d;
pos.y = ((s->splines[1].a*t+s->splines[1].b)*t+s->splines[1].c)*t+s->splines[1].d;
average_dir.x += (pos.x-base->x); average_dir.y += (pos.y-base->y);
t += incr;
}
len = sqrt( pow( average_dir.x,2 ) + pow( average_dir.y,2 ));
if ( len==0 )
return( false );
average_dir.x /= len; average_dir.y /= len;
normal.x = average_dir.y; normal.y = - average_dir.x;
t = .5-incr;
while ( t>0 && t<1.0 ) {
pos.x = ((s->splines[0].a*t+s->splines[0].b)*t+s->splines[0].c)*t+s->splines[0].d;
pos.y = ((s->splines[1].a*t+s->splines[1].b)*t+s->splines[1].c)*t+s->splines[1].d;
off = (pos.x-base->x)*normal.x + (pos.y-base->y)*normal.y;
if ( off<-dist_error_hv || off>dist_error_hv )
return( false );
t += incr;
}
if ( UnitsParallel( dir,&normal,true )) {
/* prefer the direction which is closer to horizontal/vertical */
if ( (dx=dir->x)<0 ) dx = -dx;
if ( (dy=dir->y)<0 ) dy = -dy;
d = (dx<dy) ? dx : dy;
if ( (ax=average_dir.x)<0 ) ax = -ax;
if ( (ay=average_dir.y)<0 ) ay = -ay;
a = (ax<ay) ? ax : ay;
if ( d<a )
return( false );
}
*dir = average_dir;
return( true );
}
static int IsSplinePeak( struct glyphdata *gd,struct pointdata *pd,int outer,int is_x,int flags );
static void PointInit( struct glyphdata *gd,SplinePoint *sp, SplineSet *ss ) {
struct pointdata *pd, *prevpd=NULL, *nextpd=NULL;
double len, same;
int hv;
if ( sp->ptindex >= gd->pcnt )
return;
pd = &gd->points[sp->ptindex];
pd->sp = sp;
pd->ss = ss;
pd->x_extr = pd->y_extr = 0;
pd->base = sp->me;
pd->ttfindex = sp->ttfindex;
pd->nextcnt = pd->prevcnt = 0;
pd->nextstems = pd->prevstems = NULL;
pd->next_is_l = pd->prev_is_l = NULL;
if ( !sp->nonextcp && gd->order2 && sp->nextcpindex < gd->realcnt ) {
nextpd = &gd->points[sp->nextcpindex];
nextpd->ss = ss;
nextpd->x_extr = nextpd->y_extr = 0;
nextpd->base = sp->nextcp;
nextpd->ttfindex = sp->nextcpindex;
}
if ( !sp->noprevcp && gd->order2 && sp->prev != NULL &&
sp->prev->from->nextcpindex < gd->realcnt ) {
nextpd = &gd->points[sp->prev->from->nextcpindex];
nextpd->ss = ss;
nextpd->x_extr = nextpd->y_extr = 0;
nextpd->base = sp->prevcp;
nextpd->ttfindex = sp->prev->from->nextcpindex;
}
if ( sp->next==NULL ) {
pd->nextunit.x = ss->first->me.x - sp->me.x;
pd->nextunit.y = ss->first->me.y - sp->me.y;
pd->nextlinear = true;
} else if ( sp->next->knownlinear ) {
pd->nextunit.x = sp->next->to->me.x - sp->me.x;
pd->nextunit.y = sp->next->to->me.y - sp->me.y;
pd->nextlinear = true;
} else if ( sp->nonextcp ) {
pd->nextunit.x = sp->next->to->prevcp.x - sp->me.x;
pd->nextunit.y = sp->next->to->prevcp.y - sp->me.y;
} else {
pd->nextunit.x = sp->nextcp.x - sp->me.x;
pd->nextunit.y = sp->nextcp.y - sp->me.y;
}
len = sqrt( pow( pd->nextunit.x,2 ) + pow( pd->nextunit.y,2 ));
if ( len==0 )
pd->nextzero = true;
else {
pd->nextlen = len;
pd->nextunit.x /= len;
pd->nextunit.y /= len;
if ( sp->next!=NULL && !sp->next->knownlinear )
SplineFigureOpticalSlope(sp->next,true,&pd->nextunit);
hv = IsUnitHV( &pd->nextunit,true );
if ( hv == 2 ) {
pd->nextunit.x = 0; pd->nextunit.y = pd->nextunit.y>0 ? 1 : -1;
} else if ( hv == 1 ) {
pd->nextunit.y = 0; pd->nextunit.x = pd->nextunit.x>0 ? 1 : -1;
}
if ( pd->nextunit.y==0 ) pd->next_hor = true;
else if ( pd->nextunit.x==0 ) pd->next_ver = true;
if ( nextpd != NULL ) {
nextpd->prevunit.x = -pd->nextunit.x;
nextpd->prevunit.y = -pd->nextunit.y;
}
}
if ( sp->prev==NULL ) {
pd->prevunit.x = ss->last->me.x - sp->me.x;
pd->prevunit.y = ss->last->me.y - sp->me.y;
pd->prevlinear = true;
} else if ( sp->prev->knownlinear ) {
pd->prevunit.x = sp->prev->from->me.x - sp->me.x;
pd->prevunit.y = sp->prev->from->me.y - sp->me.y;
pd->prevlinear = true;
} else if ( sp->noprevcp ) {
pd->prevunit.x = sp->prev->from->nextcp.x - sp->me.x;
pd->prevunit.y = sp->prev->from->nextcp.y - sp->me.y;
} else {
pd->prevunit.x = sp->prevcp.x - sp->me.x;
pd->prevunit.y = sp->prevcp.y - sp->me.y;
}
len = sqrt( pow( pd->prevunit.x,2 ) + pow( pd->prevunit.y,2 ));
if ( len==0 )
pd->prevzero = true;
else {
pd->prevlen = len;
pd->prevunit.x /= len;
pd->prevunit.y /= len;
if ( sp->prev!=NULL && !sp->prev->knownlinear )
SplineFigureOpticalSlope(sp->prev,false,&pd->prevunit);
hv = IsUnitHV( &pd->prevunit,true );
if ( hv == 2 ) {
pd->prevunit.x = 0; pd->prevunit.y = pd->prevunit.y>0 ? 1 : -1;
} else if ( hv == 1 ) {
pd->prevunit.y = 0; pd->prevunit.x = pd->prevunit.x>0 ? 1 : -1;
}
if ( pd->prevunit.y==0 ) pd->prev_hor = true;
else if ( pd->prevunit.x==0 ) pd->prev_ver = true;
if ( prevpd != NULL ) {
prevpd->nextunit.x = -pd->prevunit.x;
prevpd->nextunit.y = -pd->prevunit.y;
}
}
{
same = pd->prevunit.x*pd->nextunit.x + pd->prevunit.y*pd->nextunit.y;
if ( same<-.95 )
pd->colinear = true;
}
if (( pd->prev_hor || pd->next_hor ) && pd->colinear ) {
if ( IsSplinePeak( gd,pd,false,false,1 )) pd->y_extr = 1;
else if ( IsSplinePeak( gd,pd,true,false,1 )) pd->y_extr = 2;
} else if (( pd->prev_ver || pd->next_ver ) && pd->colinear ) {
if ( IsSplinePeak( gd,pd,true,true,1 )) pd->x_extr = 1;
else if ( IsSplinePeak( gd,pd,false,true,1 )) pd->x_extr = 2;
} else {
if (( pd->nextunit.y < 0 && pd->prevunit.y < 0 ) || ( pd->nextunit.y > 0 && pd->prevunit.y > 0 )) {
if ( IsSplinePeak( gd,pd,false,false,2 )) pd->y_corner = 1;
else if ( IsSplinePeak( gd,pd,true,false,2 )) pd->y_corner = 2;
}
if (( pd->nextunit.x < 0 && pd->prevunit.x < 0 ) || ( pd->nextunit.x > 0 && pd->prevunit.x > 0 )) {
if ( IsSplinePeak( gd,pd,true,true,2 )) pd->x_corner = 1;
else if ( IsSplinePeak( gd,pd,false,true,2 )) pd->x_corner = 2;
}
}
if ( hint_diagonal_intersections ) {
if (( pd->y_corner || pd->y_extr ) &&
RealNear( pd->nextunit.x,-pd->prevunit.x ) &&
RealNear( pd->nextunit.y,pd->prevunit.y ) && !pd->nextzero)
pd->symetrical_h = true;
else if (( pd->x_corner || pd->x_extr ) &&
RealNear( pd->nextunit.y,-pd->prevunit.y ) &&
RealNear( pd->nextunit.x,pd->prevunit.x ) && !pd->nextzero)
pd->symetrical_v = true;
}
}
static int BBoxIntersectsLine(Spline *s,Spline *line) {
double t,x,y;
DBounds b;
b.minx = b.maxx = s->from->me.x;
b.miny = b.maxy = s->from->me.y;
if ( s->to->me.x<b.minx ) b.minx = s->to->me.x;
else if ( s->to->me.x>b.maxx ) b.maxx = s->to->me.x;
if ( s->to->me.y<b.miny ) b.miny = s->to->me.y;
else if ( s->to->me.y>b.maxy ) b.maxy = s->to->me.y;
if ( s->to->prevcp.x<b.minx ) b.minx = s->to->prevcp.x;
else if ( s->to->prevcp.x>b.maxx ) b.maxx = s->to->prevcp.x;
if ( s->to->prevcp.y<b.miny ) b.miny = s->to->prevcp.y;
else if ( s->to->prevcp.y>b.maxy ) b.maxy = s->to->prevcp.y;
if ( s->from->nextcp.x<b.minx ) b.minx = s->from->nextcp.x;
else if ( s->from->nextcp.x>b.maxx ) b.maxx = s->from->nextcp.x;
if ( s->from->nextcp.y<b.miny ) b.miny = s->from->nextcp.y;
else if ( s->from->nextcp.y>b.maxy ) b.maxy = s->from->nextcp.y;
if ( line->splines[0].c!=0 ) {
t = (b.minx-line->splines[0].d)/line->splines[0].c;
y = line->splines[1].c*t+line->splines[1].d;
if ( y>=b.miny && y<=b.maxy )
return( true );
t = (b.maxx-line->splines[0].d)/line->splines[0].c;
y = line->splines[1].c*t+line->splines[1].d;
if ( y>=b.miny && y<=b.maxy )
return( true );
}
if ( line->splines[1].c!=0 ) {
t = (b.miny-line->splines[1].d)/line->splines[1].c;
x = line->splines[0].c*t+line->splines[0].d;
if ( x>=b.minx && x<=b.maxx )
return( true );
t = (b.maxy-line->splines[1].d)/line->splines[1].c;
x = line->splines[0].c*t+line->splines[0].d;
if ( x>=b.minx && x<=b.maxx )
return( true );
}
return( false );
}
static int stcmp(const void *_p1, const void *_p2) {
const struct st *stpt1 = _p1, *stpt2 = _p2;
if ( stpt1->lt>stpt2->lt )
return( 1 );
else if ( stpt1->lt<stpt2->lt )
return( -1 );
return( 0 );
}
static int line_pt_cmp( const void *_p1, const void *_p2 ) {
struct pointdata * const *pd1 = _p1, * const *pd2 = _p2;
struct linedata *line;
double ppos1=0,ppos2=0;
if ( (*pd1)->prevline != NULL &&
( (*pd1)->prevline == (*pd2)->prevline || (*pd1)->prevline == (*pd2)->nextline ))
line = (*pd1)->prevline;
else if ( (*pd1)->nextline != NULL &&
( (*pd1)->nextline == (*pd2)->prevline || (*pd1)->nextline == (*pd2)->nextline ))
line = (*pd1)->nextline;
else
return( 0 );
ppos1 = ( (*pd1)->sp->me.x - line->online.x ) * line->unit.x +
( (*pd1)->sp->me.y - line->online.y ) * line->unit.y;
ppos2 = ( (*pd2)->sp->me.x - line->online.x ) * line->unit.x +
( (*pd2)->sp->me.y - line->online.y ) * line->unit.y;
if ( ppos1>ppos2 )
return( 1 );
else if ( ppos1<ppos2 )
return( -1 );
else
return( 0 );
}
static int segment_cmp(const void *_s1, const void *_s2) {
const struct segment *s1 = _s1, *s2 = _s2;
if ( s1->start<s2->start )
return( -1 );
else if ( s1->start>s2->start )
return( 1 );
return( 0 );
}
static int proj_cmp(const void *_p1, const void *_p2) {
struct pointdata * const *p1 = _p1, * const *p2 = _p2;
if ( (*p1)->projection<(*p2)->projection )
return( -1 );
else if ( (*p1)->projection>(*p2)->projection )
return( 1 );
return( 0 );
}
static void AssignStemToPoint( struct pointdata *pd,struct stemdata *stem,int is_next, int left ) {
struct stemdata ***stems;
int i, *stemcnt, **is_l;
stems = ( is_next ) ? &pd->nextstems : &pd->prevstems;
stemcnt = ( is_next ) ? &pd->nextcnt : &pd->prevcnt;
is_l = ( is_next ) ? &pd->next_is_l : &pd->prev_is_l;
for ( i=0; i<*stemcnt; i++ ) {
if ((*stems)[i] == stem )
return;
}
*stems = realloc( *stems,( *stemcnt+1 )*sizeof( struct stemdata *));
*is_l = realloc( *is_l, ( *stemcnt+1 )*sizeof( int ));
(*stems)[*stemcnt] = stem;
(*is_l )[*stemcnt] = left;
(*stemcnt)++;
}
int IsStemAssignedToPoint( struct pointdata *pd,struct stemdata *stem,int is_next ) {
struct stemdata **stems;
int i, stemcnt;
stems = ( is_next ) ? pd->nextstems : pd->prevstems;
stemcnt = ( is_next ) ? pd->nextcnt : pd->prevcnt;
for ( i=0; i<stemcnt; i++ ) {
if ( stems[i] == stem )
return( i );
}
return( -1 );
}
static int GetValidPointDataIndex( struct glyphdata *gd,SplinePoint *sp,
struct stemdata *stem ) {
struct pointdata *tpd;
if ( sp == NULL )
return( -1 );
if ( sp->ttfindex < gd->realcnt )
return( sp->ttfindex );
if ( !sp->nonextcp && sp->nextcpindex < gd->realcnt ) {
tpd = &gd->points[sp->nextcpindex];
if ( IsStemAssignedToPoint( tpd,stem,false ) != -1 )
return( sp->nextcpindex );
}
if ( !sp->noprevcp && sp->prev != NULL &&
sp->prev->from->nextcpindex < gd->realcnt ) {
tpd = &gd->points[sp->prev->from->nextcpindex];
if ( IsStemAssignedToPoint( tpd,stem,true ) != -1 )
return( sp->prev->from->nextcpindex );
}
return( -1 );
}
static int LineType(struct st *st,int i, int cnt,Spline *line) {
SplinePoint *sp;
BasePoint nextcp, prevcp, here;
double dn, dp;
if ( st[i].st>.01 && st[i].st<.99 )
return( 0 ); /* Not near an end-point, just a normal line */
if ( i+1>=cnt )
return( 0 ); /* No following spline */
if ( st[i+1].st>.01 && st[i+1].st<.99 )
return( 0 ); /* Following spline not near an end-point, can't */
/* match to this one, just a normal line */
if ( st[i].st<.5 && st[i+1].st>.5 ) {
if ( st[i+1].s->to->next!=st[i].s )
return( 0 );
sp = st[i].s->from;
} else if ( st[i].st>.5 && st[i+1].st<.5 ) {
if ( st[i].s->to->next!=st[i+1].s )
return( 0 );
sp = st[i].s->to;
} else
return( 0 );
if ( !sp->nonextcp )
nextcp = sp->nextcp;
else
nextcp = sp->next->to->me;
if ( !sp->noprevcp )
prevcp = sp->prevcp;
else
prevcp = sp->prev->from->me;
here.x = line->splines[0].c*(st[i].st+st[i+1].st)/2 + line->splines[0].d;
here.y = line->splines[1].c*(st[i].st+st[i+1].st)/2 + line->splines[1].d;
nextcp.x -= here.x; nextcp.y -= here.y;
prevcp.x -= here.x; prevcp.y -= here.y;
dn = nextcp.x*line->splines[1].c - nextcp.y*line->splines[0].c;
dp = prevcp.x*line->splines[1].c - prevcp.y*line->splines[0].c;
if ( dn*dp<0 ) /* splines away move on opposite sides of the line */
return( 1 ); /* Treat this line and the next as one */
/* We assume that a rounding error gave us one erroneous intersection (or we went directly through the endpoint) */
else
return( 2 ); /* Ignore both this line and the next */
/* Intersects both in a normal fashion */
}
static int MonotonicOrder(Spline **sspace,Spline *line,struct st *stspace) {
Spline *s;
int i,j,k,cnt;
BasePoint pts[9];
extended lts[10], sts[10];
for ( i=j=0; (s=sspace[j])!=NULL; ++j ) {
if ( BBoxIntersectsLine(s,line) ) {
/* Lines parallel to the direction we are testing just get in the */
/* way and don't add any useful info */
if ( s->islinear &&
RealNear(line->splines[0].c*s->splines[1].c,
line->splines[1].c*s->splines[0].c))
continue;
if ( SplinesIntersect(line,s,pts,lts,sts)<=0 )
continue;
for ( k=0; sts[k]!=-1; ++k ) {
if ( sts[k]>=0 && sts[k]<=1 ) {
stspace[i].s = s;
stspace[i].lt = lts[k];
stspace[i++].st = sts[k];
}
}
}
}
stspace[i].s = NULL;
cnt = i;
qsort(stspace,cnt,sizeof(struct st),stcmp);
return( cnt );
}
static Spline *MonotonicFindAlong(Spline *line,struct st *stspace,int cnt,
Spline *findme, double *other_t) {
Spline *s;
int i;
int eo; /* I do horizontal/vertical by winding number */
/* But figuring winding number with respect to a */
/* diagonal line is hard. So I use even-odd */
/* instead. */
eo = 0;
for ( i=0; i<cnt; ++i ) {
s = stspace[i].s;
if ( s==findme ) {
if ( (eo&1) && i>0 ) {
*other_t = stspace[i-1].st;
return( stspace[i-1].s );
} else if ( !(eo&1) && i+1<cnt ) {
*other_t = stspace[i+1].st;
return( stspace[i+1].s );
}
fprintf( stderr, "MonotonicFindAlong: Ran out of intersections.\n" );
return( NULL );
}
if ( i+1<cnt && stspace[i+1].s==findme )
++eo;
else switch ( LineType(stspace,i,cnt,line) ) {
case 0: /* Normal spline */
++eo;
break;
case 1: /* Intersects at end-point & next entry is other side */
++eo; /* And the two sides continue in approximately the */
++i; /* same direction */
break;
case 2: /* Intersects at end-point & next entry is other side */
++i; /* And the two sides go in opposite directions */
break;
default:
break;
}
}
fprintf( stderr, "MonotonicFindAlong: Never found our spline.\n" );
return( NULL );
}
static int MonotonicFindStemBounds(Spline *line,struct st *stspace,int cnt,
double fudge,struct stemdata *stem ) {
int i,j;
int eo; /* I do horizontal/vertical by winding number */
/* But figuring winding number with respect to a */
/* diagonal line is hard. So I use even-odd */
/* instead. */
double pos, npos;
double lmin = ( stem->lmin < -fudge ) ? stem->lmin : -fudge;
double lmax = ( stem->lmax > fudge ) ? stem->lmax : fudge;
double rmin = ( stem->rmin < -fudge ) ? stem->rmin : -fudge;
double rmax = ( stem->rmax > fudge ) ? stem->rmax : fudge;
lmin -= .0001; lmax += .0001; rmin -= .0001; rmax += .0001;
eo = 0;
for ( i=0; i<cnt; ++i ) {
pos = (line->splines[0].c*stspace[i].lt + line->splines[0].d - stem->left.x)*stem->l_to_r.x +
(line->splines[1].c*stspace[i].lt + line->splines[1].d - stem->left.y)*stem->l_to_r.y;
npos = 1e4;
if ( i+1<cnt )
npos = (line->splines[0].c*stspace[i+1].lt + line->splines[0].d - stem->left.x)*stem->l_to_r.x +
(line->splines[1].c*stspace[i+1].lt + line->splines[1].d - stem->left.y)*stem->l_to_r.y;
if ( pos>=lmin && pos<=lmax ) {
if ( (eo&1) && i>0 )
j = i-1;
else if ( !(eo&1) && i+1<cnt )
j = i+1;
else
return( false );
pos = (line->splines[0].c*stspace[j].lt + line->splines[0].d - stem->right.x)*stem->l_to_r.x +
(line->splines[1].c*stspace[j].lt + line->splines[1].d - stem->right.y)*stem->l_to_r.y;
if ( pos >= rmin && pos <= rmax )
return( true );
}
if ( i+1 < cnt && npos >= lmin && npos <= lmax )
++eo;
else switch ( LineType(stspace,i,cnt,line) ) {
case 0: /* Normal spline */
++eo;
break;
case 1: /* Intersects at end-point & next entry is other side */
++eo; /* And the two sides continue in approximately the */
++i; /* same direction */
break;
case 2: /* Intersects at end-point & next entry is other side */
++i; /* And the two sides go in opposite directions */
break;
default:
break;
}
}
return( false );
}
static int MatchWinding(struct monotonic ** space,int i,int nw,int winding,int which,int idx) {
struct monotonic *m;
int j,cnt=0;
if (( nw<0 && winding>0 ) || (nw>0 && winding<0)) {
winding = nw;
for ( j=i-1; j>=0; --j ) {
m = space[j];
winding += ((&m->xup)[which] ? 1 : -1 );
if ( winding==0 ) {
if ( cnt == idx )
return( j );
cnt++;
}
}
} else {
winding = nw;
for ( j=i+1; space[j]!=NULL; ++j ) {
m = space[j];
winding += ((&m->xup)[which] ? 1 : -1 );
if ( winding==0 ) {
if ( cnt == idx )
return( j );
cnt++;
}
}
}
return( -1 );
}
static int FindMatchingHVEdge( struct glyphdata *gd,struct pointdata *pd,
int is_next,Spline **edges,double *other_t,double *dist ) {
double test, t, start, end;
int which;
Spline *s;
Monotonic *m;
int winding, nw, i, j, ret=0;
struct monotonic **space;
BasePoint *dir, d, hv;
/* Things are difficult if we go exactly through the point. Move off */
/* to the side a tiny bit and hope that doesn't matter */
if ( is_next==2 ) {
/* Consider the case of the bottom of the circumflex (or a chevron) */
/* Think of it as a flattend breve. It is symetrical and we want to */
/* note the vertical distance between the two points that define */
/* the bottom, so treat them as a funky stem */
/* \ \ / / */
/* \ \ / / */
/* \ \ / / */
/* \ + / */
/* \ / */
/* + */
hv.x = pd->symetrical_h ? 1.0 : 0.0;
hv.y = pd->symetrical_v ? 1.0 : 0.0;
dir = &hv;
t = .001;
s = pd->sp->next; /* Could just as easily be prev */
} else if ( is_next ) {
s = pd->sp->next;
t = .001;
dir = &pd->nextunit;
} else {
s = pd->sp->prev;
t = .999;
dir = &pd->prevunit;
}
if (( d.x = dir->x )<0 ) d.x = -d.x;
if (( d.y = dir->y )<0 ) d.y = -d.y;
which = d.x<d.y; /* closer to vertical */
if ( s==NULL ) /* Somehow we got an open contour? */
return( 0 );
test = ((s->splines[which].a*t+s->splines[which].b)*t+s->splines[which].c)*t+s->splines[which].d;
MonotonicFindAt(gd->ms,which,test,space = gd->space);
winding = 0;
for ( i=0; space[i]!=NULL; ++i ) {
m = space[i];
nw = ((&m->xup)[which] ? 1 : -1 );
if ( m->s == s && t>=m->tstart && t<=m->tend ) {
start = m->other;
break;
}
winding += nw;
}
if ( space[i]==NULL ) {
fprintf( stderr, "FindMatchinHVEdge didn't\n" );
return( 0 );
}
j = MatchWinding(space,i,nw,winding,which,0);
if ( j!=-1 ) {
other_t[0] = space[j]->t;
end = space[j]->other;
dist[0] = end - start;
if ( dist[0] < 0 ) dist[0] = -dist[0];
edges[0] = space[j]->s;
ret++;
}
if ( ret > 0 && is_next != 2 && ( pd->x_extr == 1 || pd->y_extr == 1 )) {
j = MatchWinding(space,i,nw,winding,which,1);
if ( j!=-1 ) {
other_t[ret] = space[j]->t;
end = space[j]->other;
dist[ret] = end - start;
if ( dist[ret] < 0 ) dist[ret] = -dist[ret];
edges[ret] = space[j]->s;
ret++;
}
}
return( ret );
}
static BasePoint PerturbAlongSpline( Spline *s,BasePoint *bp,double t ) {
BasePoint perturbed;
for (;;) {
perturbed.x = ((s->splines[0].a*t+s->splines[0].b)*t+s->splines[0].c)*t+s->splines[0].d;
perturbed.y = ((s->splines[1].a*t+s->splines[1].b)*t+s->splines[1].c)*t+s->splines[1].d;
if ( !RealWithin( perturbed.x,bp->x,.01 ) || !RealWithin( perturbed.y,bp->y,.01 ))
break;
if ( t<.5 ) {
t *= 2;
if ( t>.5 )
break;
} else {
t = 1- 2*(1-t);
if ( t<.5 )
break;
}
}
return( perturbed );
}
static void MakeVirtualLine(struct glyphdata *gd,BasePoint *perturbed,
BasePoint *dir,Spline *myline,SplinePoint *end1, SplinePoint *end2) {
BasePoint norm, absnorm;
SplineSet *spl;
Spline *s, *first;
double t1, t2;
int i, cnt;
if ( gd->stspace==NULL ) {
for ( i=0; i<2; ++i ) {
cnt = 0;
for ( spl=gd->sc->layers[gd->layer].splines; spl!=NULL; spl=spl->next ) {
first = NULL;
if ( spl->first->prev!=NULL ) {
for ( s=spl->first->next; s!=first; s=s->to->next ) {
if ( first==NULL ) first = s;
if ( i )
gd->sspace[cnt] = s;
++cnt;
}
}
}
if ( !i ) {
gd->scnt = cnt;
gd->sspace = malloc((cnt+1)*sizeof(Spline *));
} else
gd->sspace[cnt] = NULL;
}
gd->stspace = malloc((3*cnt+2)*sizeof(struct st));
SplineCharFindBounds(gd->sc,&gd->size);
gd->size.minx -= 10; gd->size.miny -= 10;
gd->size.maxx += 10; gd->size.maxy += 10;
}
norm.x = -dir->y;
norm.y = dir->x;
absnorm = norm;
if ( absnorm.x<0 ) absnorm.x = -absnorm.x;
if ( absnorm.y<0 ) absnorm.y = -absnorm.y;
memset(myline,0,sizeof(*myline));
memset(end1,0,sizeof(*end1));
memset(end2,0,sizeof(*end2));
myline->knownlinear = myline->islinear = true;
if ( absnorm.x > absnorm.y ) {
/* Greater change in x than in y */
t1 = (gd->size.minx-perturbed->x)/norm.x;
t2 = (gd->size.maxx-perturbed->x)/norm.x;
myline->splines[0].d = gd->size.minx;
myline->splines[0].c = gd->size.maxx-gd->size.minx;
myline->splines[1].d = perturbed->y+t1*norm.y;
myline->splines[1].c = (t2-t1)*norm.y;
} else {
t1 = (gd->size.miny-perturbed->y)/norm.y;
t2 = (gd->size.maxy-perturbed->y)/norm.y;
myline->splines[1].d = gd->size.miny;
myline->splines[1].c = gd->size.maxy-gd->size.miny;
myline->splines[0].d = perturbed->x+t1*norm.x;
myline->splines[0].c = (t2-t1)*norm.x;
}
end1->me.x = myline->splines[0].d;
end2->me.x = myline->splines[0].d + myline->splines[0].c;
end1->me.y = myline->splines[1].d;
end2->me.y = myline->splines[1].d + myline->splines[1].c;
end1->nextcp = end1->prevcp = end1->me;
end2->nextcp = end2->prevcp = end2->me;
end1->nonextcp = end1->noprevcp = end2->nonextcp = end2->noprevcp = true;
end1->next = myline; end2->prev = myline;
myline->from = end1; myline->to = end2;
}
static int FindMatchingEdge( struct glyphdata *gd, struct pointdata *pd,
int is_next,Spline **edges ) {
BasePoint *dir, vert, perturbed, diff;
Spline myline;
SplinePoint end1, end2;
double *other_t = is_next==2 ? &pd->both_e_t : is_next ? pd->next_e_t : pd->prev_e_t;
double *dist = is_next ? pd->next_dist : pd->prev_dist;
double t ;
Spline *s;
int cnt;
dist[0] = 0; dist[1] = 0;
if (( is_next && ( pd->next_hor || pd->next_ver )) ||
( !is_next && ( pd->prev_hor || pd->prev_ver )) ||
is_next == 2 )
return( FindMatchingHVEdge(gd,pd,is_next,edges,other_t,dist));
if ( is_next ) {
dir = &pd->nextunit;
t = .001;
s = pd->sp->next;
} else {
dir = &pd->prevunit;
t = .999;
s = pd->sp->prev;
}
/* For spline segments which have slope close enough to the font's italic */
/* slant look for an opposite edge along the horizontal direction, rather */
/* than along the normal for the point's next/previous unit. This allows */
/* us e. g. to detect serifs in italic fonts */
if ( gd->has_slant ) {
if ( UnitsParallel( dir,&gd->slant_unit,true )) {
vert.x = 0; vert.y = 1;
dir = |
}
}
if ( s==NULL || ( gd->only_hv && !IsUnitHV( dir,false )))
return( 0 );
diff.x = s->to->me.x-s->from->me.x; diff.y = s->to->me.y-s->from->me.y;
if ( diff.x<.03 && diff.x>-.03 && diff.y<.03 && diff.y>-.03 )
return( 0 );
/* Don't base the line on the current point, we run into rounding errors */
/* where lines that should intersect it don't. Instead perturb it a tiny*/
/* bit in the direction along the spline */
perturbed = PerturbAlongSpline( s,&pd->sp->me,t );
MakeVirtualLine(gd,&perturbed,dir,&myline,&end1,&end2);
/* prev_e_t = next_e_t = both_e_t =. This is where these guys are set */
cnt = MonotonicOrder(gd->sspace,&myline,gd->stspace);
edges[0] = MonotonicFindAlong(&myline,gd->stspace,cnt,s,other_t);
return( edges[0] != NULL );
}
static int StillStem(struct glyphdata *gd,double fudge,BasePoint *pos,struct stemdata *stem ) {
Spline myline;
SplinePoint end1, end2;
int cnt, ret;
MakeVirtualLine( gd,pos,&stem->unit,&myline,&end1,&end2 );
cnt = MonotonicOrder( gd->sspace,&myline,gd->stspace );
ret = MonotonicFindStemBounds( &myline,gd->stspace,cnt,fudge,stem );
return( ret );
}
static int CornerCorrectSide( struct pointdata *pd,int x_dir,int is_l ) {
int corner = ( x_dir ) ? pd->x_corner : pd->y_corner;
int start = (( x_dir && is_l ) || ( !x_dir && !is_l ));
double unit_p, unit_n;
unit_p = (&pd->prevunit.x)[!x_dir];
unit_n = (&pd->nextunit.x)[!x_dir];
return( ( start && (
( corner == 1 && unit_p > 0 && unit_n > 0 ) ||
( corner == 2 && unit_p < 0 && unit_n < 0 ))) ||
( !start && (
( corner == 1 && unit_p < 0 && unit_n < 0 ) ||
( corner == 2 && unit_p > 0 && unit_n > 0 ))));
}
static int IsCorrectSide( struct glyphdata *gd,struct pointdata *pd,
int is_next,int is_l,BasePoint *dir ) {
Spline *sbase, myline;
SplinePoint *sp = pd->sp, end1, end2;
BasePoint perturbed;
int i, hv, is_x, ret = false, winding = 0, cnt, eo;
double t, test;
struct monotonic **space, *m;
hv = IsUnitHV( dir,true );
if (( hv == 2 && pd->x_corner ) || ( hv == 1 && pd->y_corner ))
return( CornerCorrectSide( pd,( hv == 2 ),is_l ));
sbase = ( is_next ) ? sp->next : sp->prev;
t = ( is_next ) ? 0.001 : 0.999;
perturbed = PerturbAlongSpline( sbase,&sp->me,t );
if ( hv ) {
is_x = ( hv == 2 );
test = ( is_x ) ? perturbed.y : perturbed.x;
MonotonicFindAt( gd->ms,is_x,test,space = gd->space );
for ( i=0; space[i]!=NULL; ++i ) {
m = space[i];
winding = ((&m->xup)[is_x] ? 1 : -1 );
if ( m->s == sbase )
break;
}
if ( space[i]!=NULL )
ret = (( is_l && winding == 1 ) || ( !is_l && winding == -1 ));
} else {
MakeVirtualLine( gd,&perturbed,dir,&myline,&end1,&end2 );
cnt = MonotonicOrder( gd->sspace,&myline,gd->stspace );
eo = -1;
is_x = fabs( dir->y ) > fabs( dir->x );
/* If a diagonal stem is more vertical than horizontal, then our */
/* virtual line will go from left to right. It will first intersect */
/* the left side of the stem, if the stem also points north-east. */
/* In any other case the virtual line will first intersect the right */
/* side. */
i = ( is_x && dir->y > 0 ) ? 0 : cnt-1;
while ( i >= 0 && i <= cnt-1 ) {
eo = ( eo != 1 ) ? 1 : 0;
if ( gd->stspace[i].s == sbase )
break;
if ( is_x && dir->y > 0 ) i++;
else i--;
}
ret = ( is_l == eo );
}
return( ret );
}
/* In TrueType I want to make sure that everything on a diagonal line remains */
/* on the same line. Hence we compute the line. Also we are interested in */
/* points that are on the intersection of two lines */
static struct linedata *BuildLine(struct glyphdata *gd,struct pointdata *pd,int is_next ) {
int i;
BasePoint *dir, *base, *start, *end;
struct pointdata **pspace = gd->pspace, *pd2;
int pcnt=0, is_l, hv;
double dist_error;
struct linedata *line;
double off, firstoff, lastoff, lmin=0, lmax=0;
dir = is_next ? &pd->nextunit : &pd->prevunit;
is_l = IsCorrectSide( gd,pd,is_next,true,dir );
dist_error = ( IsUnitHV( dir,true )) ? dist_error_hv : dist_error_diag ; /* Diagonals are harder to align */
if ( dir->x==0 && dir->y==0 )
return( NULL );
base = &pd->sp->me;
for ( i= (pd - gd->points)+1; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
pd2 = &gd->points[i];
off = ( pd2->sp->me.x - base->x )*dir->y -
( pd2->sp->me.y - base->y )*dir->x;
if ( off <= lmax - 2*dist_error || off >= lmin + 2*dist_error )
continue;
if ( off < 0 && off < lmin ) lmin = off;
else if ( off > 0 && off > lmax ) lmax = off;
if ((( UnitsParallel( dir,&pd2->nextunit,true ) && pd2->nextline==NULL ) &&
IsCorrectSide( gd,pd2,true,is_l,dir )) ||
(( UnitsParallel( dir,&pd2->prevunit,true ) && pd2->prevline==NULL ) &&
IsCorrectSide( gd,pd2,false,is_l,dir )))
pspace[pcnt++] = pd2;
}
if ( pcnt==0 )
return( NULL );
if ( pcnt==1 ) {
/* if the line consists of just these two points, only count it as */
/* a true line if the two immediately follow each other */
if (( pd->sp->next->to != pspace[0]->sp || !pd->sp->next->knownlinear ) &&
( pd->sp->prev->from != pspace[0]->sp || !pd->sp->prev->knownlinear ))
return( NULL );
}
line = &gd->lines[gd->linecnt++];
line->pcnt = pcnt+1;
line->points = malloc((pcnt+1)*sizeof(struct pointdata *));
line->points[0] = pd;
line->unit = *dir;
line->is_left = is_l;
if ( dir->x < 0 || dir->y == -1 ) {
line->unit.x = -line->unit.x;
line->unit.y = -line->unit.y;
}
line->online = *base;
if ( is_next ) {
pd->nextline = line;
if ( pd->colinear ) pd->prevline = line;
} else {
pd->prevline = line;
if ( pd->colinear ) pd->nextline = line;
}
for ( i=0; i<pcnt; ++i ) {
if ( UnitsParallel( dir,&pspace[i]->nextunit,true ) && pspace[i]->nextline==NULL ) {
pspace[i]->nextline = line;
if ( pspace[i]->colinear )
pspace[i]->prevline = line;
}
if ( UnitsParallel( dir,&pspace[i]->prevunit,true ) && pspace[i]->prevline==NULL ) {
pspace[i]->prevline = line;
if ( pspace[i]->colinear )
pspace[i]->nextline = line;
}
line->points[i+1] = pspace[i];
}
qsort( line->points,line->pcnt,sizeof( struct pointdata * ),line_pt_cmp );
start = &line->points[0]->sp->me;
end = &line->points[pcnt]->sp->me;
/* Now recalculate the line unit vector basing on its starting and */
/* terminal points */
line->unit.x = ( end->x - start->x );
line->unit.y = ( end->y - start->y );
line->length = sqrt( pow( line->unit.x,2 ) + pow( line->unit.y,2 ));
line->unit.x /= line->length;
line->unit.y /= line->length;
hv = IsUnitHV( &line->unit,true );
if ( hv == 2 ) {
line->unit.x = 0; line->unit.y = 1;
} else if ( hv == 1 ) {
line->unit.x = 1; line->unit.y = 0;
} else if ( gd->has_slant && UnitsParallel( &line->unit,&gd->slant_unit,true )) {
firstoff = ( start->x - base->x )*gd->slant_unit.y -
( start->y - base->y )*gd->slant_unit.x;
lastoff = ( end->x - base->x )*gd->slant_unit.y -
( end->y - base->y )*gd->slant_unit.x;
if ( fabs( firstoff ) < 2*dist_error && fabs( lastoff ) < 2*dist_error )
line->unit = gd->slant_unit;
}
return( line );
}
static BasePoint MiddleUnit( BasePoint *unit1, BasePoint *unit2 ) {
BasePoint u1, u2, ret;
double hyp;
int hv;
u1 = *unit1; u2 = *unit2;
if ( u1.x*u2.x + u1.y*u2.y < 0 ) {
u2.x = -u2.x; u2.y = -u2.y;
}
ret.x = ( u1.x + u2.x )/2;
ret.y = ( u1.y + u2.y )/2;
hyp = sqrt( pow( ret.x,2 ) + pow( ret.y,2 ));
ret.x /= hyp;
ret.y /= hyp;
hv = IsUnitHV( &ret,true );
if ( hv ) {
ret.x = ( hv == 1 ) ? 1 : 0;
ret.y = ( hv == 1 ) ? 0 : 1;
}
return( ret );
}
static uint8 IsStubOrIntersection( struct glyphdata *gd, BasePoint *dir1,
struct pointdata *pd1, struct pointdata *pd2, int is_next1, int is_next2 ) {
int i;
int exc=0;
double dist, off, ext, norm1, norm2, opp, angle;
double mid_err = ( stem_slope_error + stub_slope_error )/2;
SplinePoint *sp1, *sp2, *nsp;
BasePoint hvdir, *dir2, *odir1, *odir2;
struct pointdata *npd;
struct linedata *line;
sp1 = pd1->sp; sp2 = pd2->sp;
dir2 = ( is_next2 ) ? &pd2->nextunit : &pd2->prevunit;
hvdir.x = ( int ) rint( dir1->x );
hvdir.y = ( int ) rint( dir1->y );
line = is_next2 ? pd2->nextline : pd2->prevline;
if ( !IsUnitHV( dir2,true ) && line != NULL )
dir2 = &line->unit;
odir1 = ( is_next1 ) ? &pd1->prevunit : &pd1->nextunit;
odir2 = ( is_next2 ) ? &pd2->prevunit : &pd2->nextunit;
angle = fabs( GetUnitAngle( dir1,dir2 ));
if ( angle > (double)stub_slope_error*1.5 && angle < PI - (double)stub_slope_error*1.5 )
return( 0 );
/* First check if it is a slightly slanted line or a curve which joins */
/* a straight line under an angle close to 90 degrees. There are many */
/* glyphs where circles or curved features are intersected by or */
/* connected to vertical or horizontal straight stems (the most obvious */
/* cases are Greek Psi and Cyrillic Yu), and usually it is highly desired to */
/* mark such an intersection with a hint */
norm1 = ( sp1->me.x - sp2->me.x ) * odir2->x +
( sp1->me.y - sp2->me.y ) * odir2->y;
norm2 = ( sp2->me.x - sp1->me.x ) * odir1->x +
( sp2->me.y - sp1->me.y ) * odir1->y;
/* if this is a real stub or intersection, then vectors on both sides */
/* of out going-to-be stem should point in the same direction. So */
/* the following value should be positive */
opp = dir1->x * dir2->x + dir1->y * dir2->y;
if (( angle <= mid_err || angle >= PI - mid_err ) &&
opp > 0 && norm1 < 0 && norm2 < 0 && UnitsParallel( odir1,odir2,true ) &&
( UnitsOrthogonal( dir1,odir1,false ) || UnitsOrthogonal( dir2,odir1,false )))
return( 2 );
if (( angle <= mid_err || angle >= PI - mid_err ) &&
opp > 0 && (( norm1 < 0 && pd1->colinear &&
IsUnitHV( dir1,true ) && UnitsOrthogonal( dir1,odir2,false )) ||
( norm2 < 0 && pd2->colinear &&
IsUnitHV( dir2,true ) && UnitsOrthogonal( dir2,odir1,false ))))
return( 4 );
/* Now check if our 2 points form a serif termination or a feature stub */
/* The check is pretty dumb: it returns 'true' if all the following */
/* conditions are met: */
/* - both the points belong to the same contour; */
/* - there are no more than 3 other points between them; */
/* - anyone of those intermediate points is positioned by such a way */
/* that it falls inside the stem formed by our 2 base point and */
/* the vector we are checking and its distance from the first point */
/* along that vector is not larger than the stem width; */
/* - none of the intermediate points is parallel to the vector direction */
/* (otherwise we should have checked against that point instead) */
if ( !UnitsParallel( dir1,&hvdir,false ))
return( 0 );
dist = ( sp1->me.x - sp2->me.x ) * dir1->y -
( sp1->me.y - sp2->me.y ) * dir1->x;
nsp = sp1;
for ( i=0; i<4; i++ ) {
if (( is_next1 && nsp->prev == NULL ) || ( !is_next1 && nsp->next == NULL ))
return( 0 );
nsp = ( is_next1 ) ? nsp->prev->from : nsp->next->to;
if ( ( i>0 && nsp == sp1 ) || nsp == sp2 )
break;
npd = &gd->points[nsp->ptindex];
if (UnitsParallel( &npd->nextunit,&hvdir,false ) ||
UnitsParallel( &npd->prevunit,&hvdir,false ))
break;
ext = ( sp1->me.x - nsp->me.x ) * hvdir.x +
( sp1->me.y - nsp->me.y ) * hvdir.y;
if ( ext < 0 ) ext = -ext;
if (( dist > 0 && ext > dist ) || ( dist < 0 && ext < dist ))
break;
off = ( sp1->me.x - nsp->me.x ) * hvdir.y -
( sp1->me.y - nsp->me.y ) * hvdir.x;
if (( dist > 0 && ( off <= 0 || off >= dist )) ||
( dist < 0 && ( off >= 0 || off <= dist )))
exc++;
}
if ( nsp == sp2 && exc == 0 )
return( 1 );
return( 0 );
}
/* We normalize all stem unit vectors so that they point between 90 and 270 */
/* degrees, as this range is optimal for sorting diagonal stems. This means */
/* that vertical stems will normally point top to bottom, but for diagonal */
/* stems (even if their angle is actually very close to vertical) the opposite */
/* direction is also possible. Sometimes we "normalize" such stems converting */
/* them to vertical. In such a case we have to swap their edges too. */
static void SwapEdges( struct glyphdata *gd,struct stemdata *stem ) {
BasePoint tpos;
struct pointdata *tpd;
struct linedata *tl;
struct stem_chunk *chunk;
double toff;
int i, j, temp;
tpos = stem->left; stem->left = stem->right; stem->right = tpos;
toff = stem->lmin; stem->lmin = stem->rmax; stem->rmax = toff;
toff = stem->rmin; stem->rmin = stem->lmax; stem->lmax = toff;
tl = stem->leftline; stem->leftline = stem->rightline; stem->rightline = tl;
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
tpd = chunk->l; chunk->l = chunk->r; chunk->r = tpd;
temp = chunk->lpotential; chunk->lpotential = chunk->rpotential; chunk->rpotential = temp;
temp = chunk->lnext; chunk->lnext = chunk->rnext; chunk->rnext = temp;
temp = chunk->ltick; chunk->ltick = chunk->rtick; chunk->rtick = temp;
tpd = chunk->l;
if ( tpd != NULL ) {
for ( j=0; j<tpd->nextcnt; j++ )
if ( tpd->nextstems[j] == stem )
tpd->next_is_l[j] = true;
for ( j=0; j<tpd->prevcnt; j++ )
if ( tpd->prevstems[j] == stem )
tpd->prev_is_l[j] = true;
}
tpd = chunk->r;
if ( tpd != NULL ) {
for ( j=0; j<tpd->nextcnt; j++ )
if ( tpd->nextstems[j] == stem )
tpd->next_is_l[j] = false;
for ( j=0; j<tpd->prevcnt; j++ )
if ( tpd->prevstems[j] == stem )
tpd->prev_is_l[j] = false;
}
}
/* In case of a quadratic contour invert assignments to stem sides */
/* also for off-curve points */
if ( gd->order2 ) {
for ( i=0; i<gd->realcnt; i++ ) if ( gd->points[i].sp == NULL ) {
tpd = &gd->points[i];
for ( j=0; j<tpd->nextcnt; j++ )
if ( tpd->nextstems[j] == stem )
tpd->next_is_l[j] = !tpd->next_is_l[j];
for ( j=0; j<tpd->prevcnt; j++ )
if ( tpd->prevstems[j] == stem )
tpd->prev_is_l[j] = !tpd->prev_is_l[j];
}
}
}
static int StemFitsHV( struct stemdata *stem,int is_x,uint8 mask ) {
int i,cnt;
double loff,roff;
double lmin=0,lmax=0,rmin=0,rmax=0;
struct stem_chunk *chunk;
cnt = stem->chunk_cnt;
for ( i=0 ; i<stem->chunk_cnt; i++ ) {
if( stem->chunks[i].stub & mask )
break;
}
if ( i == stem->chunk_cnt )
return( false );
if ( stem->chunk_cnt == 1 )
return( true );
for ( i=0;i<cnt;i++ ) {
chunk = &stem->chunks[i];
if ( chunk->l != NULL ) {
loff = ( chunk->l->sp->me.x - stem->left.x ) * !is_x -
( chunk->l->sp->me.y - stem->left.y ) * is_x;
if ( loff < lmin ) lmin = loff;
else if ( loff > lmax ) lmax = loff;
}
if ( chunk->r != NULL ) {
roff = ( chunk->r->sp->me.x - stem->right.x ) * !is_x -
( chunk->r->sp->me.y - stem->right.y ) * is_x;
if ( roff < rmin ) rmin = roff;
else if ( roff > rmax ) rmax = roff;
}
}
if ((( lmax - lmin ) < 2*dist_error_hv ) && (( rmax - rmin ) < 2*dist_error_hv ))
return( true );
return( false );
}
static int LineFitsHV( struct linedata *line ) {
int i,cnt,is_x,hv;
double off,min=0,max=0;
struct pointdata *pd;
cnt = line->pcnt;
hv = IsUnitHV( &line->unit,true );
if ( hv )
return( true );
hv = IsUnitHV( &line->unit,false );
if ( !hv )
return( false );
is_x = ( hv == 1 ) ? 1 : 0;
for ( i=0;i<cnt;i++ ) {
pd = line->points[i];
off = ( pd->base.x - line->online.x ) * !is_x -
( pd->base.y - line->online.y ) * is_x;
if ( off < min ) min = off;
else if ( off > max ) max = off;
}
if (( max - min ) < 2*dist_error_hv )
return( true );
return( false );
}
static int OnStem( struct stemdata *stem,BasePoint *test,int left ) {
double dist_error, off;
BasePoint *dir = &stem->unit;
double max=0, min=0;
/* Diagonals are harder to align */
dist_error = IsUnitHV( dir,true ) ? dist_error_hv : dist_error_diag;
if ( !stem->positioned ) dist_error = dist_error * 2;
if ( dist_error > stem->width/2 ) dist_error = stem->width/2;
if ( left ) {
off = (test->x - stem->left.x)*dir->y - (test->y - stem->left.y)*dir->x;
max = stem->lmax; min = stem->lmin;
} else {
off = (test->x - stem->right.x)*dir->y - (test->y - stem->right.y)*dir->x;
max = stem->rmax; min = stem->rmin;
}
if ( off > ( max - dist_error ) && off < ( min + dist_error ) )
return( true );
return( false );
}
static int BothOnStem( struct stemdata *stem,BasePoint *test1,BasePoint *test2,
int force_hv,int strict,int cove ) {
double dist_error, off1, off2;
BasePoint dir = stem->unit;
int hv, hv_strict;
double lmax=0, lmin=0, rmax=0, rmin=0;
hv = ( force_hv ) ? IsUnitHV( &dir,false ) : IsUnitHV( &dir,true );
hv_strict = ( force_hv ) ? IsUnitHV( &dir,true ) : hv;
if ( force_hv ) {
if ( force_hv != hv )
return( false );
if ( !hv_strict && !StemFitsHV( stem,( hv == 1 ),7 ))
return( false );
if ( !hv_strict ) {
dir.x = ( force_hv == 2 ) ? 0 : 1;
dir.y = ( force_hv == 2 ) ? 1 : 0;
}
}
/* Diagonals are harder to align */
dist_error = ( hv ) ? dist_error_hv : dist_error_diag;
if ( !strict ) {
dist_error = dist_error * 2;
lmax = stem->lmax; lmin = stem->lmin;
rmax = stem->rmax; rmin = stem->rmin;
}
if ( dist_error > stem->width/2 ) dist_error = stem->width/2;
off1 = (test1->x-stem->left.x)*dir.y - (test1->y-stem->left.y)*dir.x;
off2 = (test2->x-stem->right.x)*dir.y - (test2->y-stem->right.y)*dir.x;
if (off1 > ( lmax - dist_error ) && off1 < ( lmin + dist_error ) &&
off2 > ( rmax - dist_error ) && off2 < ( rmin + dist_error )) {
/* For some reasons in my patch from Feb 24 2008 I prohibited snapping */
/* to stems point pairs which together form a bend, if at least */
/* one point from the pair doesn't have exactly the same position as */
/* the stem edge. Unfortunately I don't remember why I did this, but */
/* this behavior has at least one obviously negative effect: it */
/* prevents building a stem from chunks which describe an ark */
/* intersected by some straight lines, even if the intersections lie */
/* closely enough to the ark extremum. So don't apply this test */
/* at least if the force_hv flag is on (which means either the */
/* chunk or the stem itself is not exactly horizontal/vertical) */
if ( !cove || force_hv || off1 == 0 || off2 == 0 )
return( true );
}
off2 = (test2->x-stem->left.x)*dir.y - (test2->y-stem->left.y)*dir.x;
off1 = (test1->x-stem->right.x)*dir.y - (test1->y-stem->right.y)*dir.x;
if (off2 > ( lmax - dist_error ) && off2 < ( lmin + dist_error ) &&
off1 > ( rmax - dist_error ) && off1 < ( rmin + dist_error )) {
if ( !cove || force_hv || off1 == 0 || off2 == 0 )
return( true );
}
return( false );
}
static int RecalcStemOffsets( struct stemdata *stem,BasePoint *dir,int left,int right ) {
double off, err;
double lmin=0, lmax=0, rmin=0, rmax=0;
struct stem_chunk *chunk;
int i;
if ( !left && !right )
return( false );
err = ( IsUnitHV( dir,true )) ? dist_error_hv : dist_error_diag;
if ( stem->chunk_cnt > 1 ) for ( i=0; i<stem->chunk_cnt; i++ ) {
chunk = &stem->chunks[i];
if ( left && chunk->l != NULL ) {
off = ( chunk->l->sp->me.x - stem->left.x )*dir->y -
( chunk->l->sp->me.y - stem->left.y )*dir->x;
if ( off < lmin ) lmin = off;
else if ( off > lmax ) lmax = off;
}
if ( right && chunk->r != NULL ) {
off = ( chunk->r->sp->me.x - stem->right.x )*dir->y +
( chunk->r->sp->me.y - stem->right.y )*dir->x;
if ( off < rmin ) rmin = off;
else if ( off > rmax ) rmax = off;
}
}
if ( lmax - lmin < 2*err && rmax - rmin < 2*err ) {
stem->lmin = lmin; stem->lmax = lmax;
stem->rmin = rmin; stem->rmax = rmax;
return( true );
}
return( false );
}
static void SetStemUnit( struct stemdata *stem,BasePoint dir ) {
double width;
width = ( stem->right.x - stem->left.x ) * dir.y -
( stem->right.y - stem->left.y ) * dir.x;
if ( width < 0 ) {
width = -width;
dir.x = -dir.x;
dir.y = -dir.y;
}
stem->unit = dir;
stem->width = width;
/* Guess at which normal we want */
stem->l_to_r.x = dir.y; stem->l_to_r.y = -dir.x;
/* If we guessed wrong, use the other */
if (( stem->right.x-stem->left.x )*stem->l_to_r.x +
( stem->right.y-stem->left.y )*stem->l_to_r.y < 0 ) {
stem->l_to_r.x = -stem->l_to_r.x;
stem->l_to_r.y = -stem->l_to_r.y;
}
/* Recalculate left/right offsets relatively to new vectors */
RecalcStemOffsets( stem,&dir,true,true );
}
static struct stem_chunk *AddToStem( struct glyphdata *gd,struct stemdata *stem,
struct pointdata *pd1,struct pointdata *pd2,int is_next1, int is_next2, int cheat ) {
int is_potential1 = false, is_potential2 = true;
struct stem_chunk *chunk=NULL;
BasePoint *dir = &stem->unit;
BasePoint *test;
int lincr = 1, rincr = 1;
double off, dist_error;
double loff = 0, roff = 0;
double min = 0, max = 0;
int i, in, ip, cpidx;
struct pointdata *pd, *npd, *ppd;
if ( cheat || stem->positioned ) is_potential2 = false;
/* Diagonals are harder to align */
dist_error = IsUnitHV( dir,true ) ? 2*dist_error_hv : 2*dist_error_diag;
if ( dist_error > stem->width/2 ) dist_error = stem->width/2;
max = stem->lmax;
min = stem->lmin;
/* The following swaps "left" and "right" points in case we have */
/* started checking relatively to a wrong edge */
if ( pd1 != NULL ) {
test = &pd1->base;
off = ( test->x - stem->left.x )*dir->y -
( test->y - stem->left.y )*dir->x;
if (( !stem->ghost &&
( off < ( max - dist_error ) || off > ( min + dist_error ))) ||
( RealNear( stem->unit.x, 1) && stem->ghost && stem->width == 21 ) ||
( RealNear( stem->unit.x,0 ) && stem->ghost && stem->width == 20 )) {
pd = pd1; pd1 = pd2; pd2 = pd;
in = is_next1; is_next1 = is_next2; is_next2 = in;
ip = is_potential1; is_potential1 = is_potential2; is_potential2 = ip;
}
}
if ( pd1 == NULL ) lincr = 0;
if ( pd2 == NULL ) rincr = 0;
/* Now run through existing stem chunks and see if the chunk we are */
/* going to add doesn't duplicate an existing one.*/
for ( i=stem->chunk_cnt-1; i>=0; --i ) {
chunk = &stem->chunks[i];
if ( chunk->l == pd1 ) lincr = 0;
if ( chunk->r == pd2 ) rincr = 0;
if (( chunk->l == pd1 || pd1 == NULL ) && ( chunk->r == pd2 || pd2 == NULL )) {
if ( !is_potential1 ) chunk->lpotential = false;
if ( !is_potential2 ) chunk->rpotential = false;
break;
} else if (( chunk->l == pd1 && chunk->r == NULL ) || ( chunk->r == pd2 && chunk->l == NULL )) {
if ( chunk->l == NULL ) {
chunk->l = pd1;
chunk->lpotential = is_potential1;
chunk->lnext = is_next1;
chunk->ltick = lincr;
} else if ( chunk->r == NULL ) {
chunk->r = pd2;
chunk->rpotential = is_potential2;
chunk->rnext = is_next2;
chunk->rtick = rincr;
}
break;
}
}
if ( i<0 ) {
stem->chunks = realloc(stem->chunks,(stem->chunk_cnt+1)*sizeof(struct stem_chunk));
chunk = &stem->chunks[stem->chunk_cnt++];
chunk->parent = stem;
chunk->l = pd1; chunk->lpotential = is_potential1;
chunk->r = pd2; chunk->rpotential = is_potential2;
chunk->ltick = lincr; chunk->rtick = rincr;
chunk->lnext = is_next1;
chunk->rnext = is_next2;
chunk->stemcheat = cheat;
chunk->stub = chunk->is_ball = false;
chunk->l_e_idx = chunk->r_e_idx = 0;
}
if ( pd1!=NULL ) {
loff = ( pd1->base.x - stem->left.x ) * stem->l_to_r.x +
( pd1->base.y - stem->left.y ) * stem->l_to_r.y;
if ( is_next1==1 || is_next1==2 || pd1->colinear ) {
AssignStemToPoint( pd1,stem,true,true );
/* For quadratic layers assign the stem not only to */
/* spline points, but to their control points as well */
/* (this may be important for TTF instructions */
if ( gd->order2 && !pd1->sp->nonextcp && pd1->sp->nextcpindex < gd->realcnt ) {
cpidx = pd1->sp->nextcpindex;
npd = &gd->points[cpidx];
if ( OnStem( stem,&npd->base,true ))
AssignStemToPoint( npd,stem,false,true );
}
}
if ( is_next1==0 || is_next1==2 || pd1->colinear ) {
AssignStemToPoint( pd1,stem,false,true );
if ( gd->order2 && !pd1->sp->noprevcp && pd1->sp->prev != NULL &&
pd1->sp->prev->from->nextcpindex < gd->realcnt ) {
cpidx = pd1->sp->prev->from->nextcpindex;
ppd = &gd->points[cpidx];
if ( OnStem( stem,&ppd->base,true ))
AssignStemToPoint( ppd,stem,true,true );
}
}
}
if ( pd2!=NULL ) {
roff = ( pd2->base.x - stem->right.x ) * stem->l_to_r.x +
( pd2->base.y - stem->right.y ) * stem->l_to_r.y;
if ( is_next2==1 || is_next2==2 || pd2->colinear ) {
AssignStemToPoint( pd2,stem,true,false );
if ( gd->order2 && !pd2->sp->nonextcp && pd2->sp->nextcpindex < gd->realcnt ) {
cpidx = pd2->sp->nextcpindex;
npd = &gd->points[cpidx];
if ( OnStem( stem,&npd->base,false ))
AssignStemToPoint( npd,stem,false,false );
}
}
if ( is_next2==0 || is_next2==2 || pd2->colinear ) {
AssignStemToPoint( pd2,stem,false,false );
if ( gd->order2 && !pd2->sp->noprevcp && pd2->sp->prev != NULL &&
pd2->sp->prev->from->nextcpindex < gd->realcnt ) {
cpidx = pd2->sp->prev->from->nextcpindex;
ppd = &gd->points[cpidx];
if ( OnStem( stem,&ppd->base,false ))
AssignStemToPoint( ppd,stem,true,false );
}
}
}
if ( loff < stem->lmin ) stem->lmin = loff;
else if ( loff > stem->lmax ) stem->lmax = loff;
if ( roff < stem->rmin ) stem->rmin = roff;
else if ( roff > stem->rmax ) stem->rmax = roff;
stem->lpcnt += lincr; stem->rpcnt += rincr;
return( chunk );
}
static struct stemdata *FindStem( struct glyphdata *gd,struct pointdata *pd,
struct pointdata *pd2,BasePoint *dir,int is_next2,int de ) {
int i, cove, test_left, hv, stemcnt;
struct stemdata *stem;
SplinePoint *match;
BasePoint newdir;
match = pd2->sp;
stemcnt = ( is_next2 ) ? pd2->nextcnt : pd2->prevcnt;
for ( i=0; i<stemcnt; i++ ) {
stem = ( is_next2 ) ? pd2->nextstems[i] : pd2->prevstems[i];
test_left = ( is_next2 ) ? !pd2->next_is_l[i] : !pd2->prev_is_l[i];
if (UnitsParallel( &stem->unit,dir,true ) &&
OnStem( stem,&pd->sp->me,test_left ))
return( stem );
}
cove = ( dir->x == 0 && pd->x_extr + pd2->x_extr == 3 ) ||
( dir->y == 0 && pd->y_extr + pd2->y_extr == 3 );
/* First pass to check for strict matches */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
/* Ghost hints and BBox hits are usually generated after all other */
/* hint types, but we can get them here in case we are generating */
/* glyph data for a predefined hint layout. In this case they should */
/* be excluded from the following tests */
if ( stem->ghost || stem->bbox )
continue;
if ( UnitsParallel( &stem->unit,dir,true ) &&
BothOnStem( stem,&pd->sp->me,&pd2->sp->me,false,true,cove )) {
return( stem );
}
}
/* One more pass. At this stage larger deviations are allowed */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->ghost || stem->bbox )
continue;
if ( UnitsParallel( &stem->unit,dir,true ) &&
BothOnStem( stem,&pd->sp->me,&pd2->sp->me,false,false,cove )) {
return( stem );
}
}
if ( de )
return( NULL );
hv = IsUnitHV( dir,false );
if ( !hv )
return( NULL );
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->ghost || stem->bbox )
continue;
if ( hv && BothOnStem( stem,&pd->base,&pd2->base,hv,false,cove )) {
newdir.x = ( hv == 2 ) ? 0 : 1;
newdir.y = ( hv == 2 ) ? 1 : 0;
if ( hv == 2 && stem->unit.y < 0 )
SwapEdges( gd,stem );
if ( stem->unit.x != newdir.x )
SetStemUnit( stem,newdir );
return( stem );
}
}
return( NULL );
}
static struct stemdata *NewStem( struct glyphdata *gd,BasePoint *dir,
BasePoint *pos1, BasePoint *pos2 ) {
struct stemdata * stem = &gd->stems[gd->stemcnt++];
double width;
stem->unit = *dir;
if ( dir->x < 0 || dir->y == -1 ) {
stem->unit.x = -stem->unit.x;
stem->unit.y = -stem->unit.y;
}
width = ( pos2->x - pos1->x ) * stem->unit.y -
( pos2->y - pos1->y ) * stem->unit.x;
if ( width > 0 ) {
stem->left = *pos1;
stem->right = *pos2;
stem->width = width;
} else {
stem->left = *pos2;
stem->right = *pos1;
stem->width = -width;
}
/* Guess at which normal we want */
stem->l_to_r.x = dir->y; stem->l_to_r.y = -dir->x;
/* If we guessed wrong, use the other */
if (( stem->right.x-stem->left.x )*stem->l_to_r.x +
( stem->right.y-stem->left.y )*stem->l_to_r.y < 0 ) {
stem->l_to_r.x = -stem->l_to_r.x;
stem->l_to_r.y = -stem->l_to_r.y;
}
stem->leftidx = stem->rightidx = -1;
stem->leftline = stem->rightline = NULL;
stem->lmin = stem->lmax = 0;
stem->rmin = stem->rmax = 0;
stem->ldone = stem->rdone = false;
stem->lpcnt = stem->rpcnt = 0;
stem->chunks = NULL;
stem->chunk_cnt = 0;
stem->ghost = stem->bbox = false;
stem->positioned = false;
stem->blue = -1;
return( stem );
}
static int ParallelToDir( struct pointdata *pd,int checknext,BasePoint *dir,
BasePoint *opposite,SplinePoint *basesp,uint8 is_stub ) {
BasePoint n, o, *base = &basesp->me;
SplinePoint *sp;
double angle, mid_err = ( stem_slope_error + stub_slope_error )/2;
sp = pd->sp;
n = ( checknext ) ? pd->nextunit : pd->prevunit;
angle = fabs( GetUnitAngle( dir,&n ));
if (( !is_stub && angle > stem_slope_error && angle < PI - stem_slope_error ) ||
( is_stub & 1 && angle > stub_slope_error*1.5 && angle < PI - stub_slope_error*1.5 ) ||
( is_stub & 6 && angle > mid_err && angle < PI - mid_err ))
return( false );
/* Now sp must be on the same side of the spline as opposite */
o.x = opposite->x-base->x; o.y = opposite->y-base->y;
n.x = sp->me.x-base->x; n.y = sp->me.y-base->y;
if ( ( o.x*dir->y - o.y*dir->x )*( n.x*dir->y - n.y*dir->x ) < 0 )
return( false );
return( true );
}
static int NearlyParallel( BasePoint *dir,Spline *other, double t ) {
BasePoint odir;
double olen;
odir.x = (3*other->splines[0].a*t+2*other->splines[0].b)*t+other->splines[0].c;
odir.y = (3*other->splines[1].a*t+2*other->splines[1].b)*t+other->splines[1].c;
olen = sqrt( pow( odir.x,2 ) + pow( odir.y,2 ));
if ( olen==0 )
return( false );
odir.x /= olen; odir.y /= olen;
return( UnitsParallel( dir,&odir,false ));
}
static double NormalDist( BasePoint *to, BasePoint *from, BasePoint *perp ) {
double len = (to->x-from->x)*perp->y - (to->y-from->y)*perp->x;
if ( len<0 ) len = -len;
return( len );
}
static struct stemdata *FindOrMakeHVStem( struct glyphdata *gd,
struct pointdata *pd,struct pointdata *pd2,int is_h,int require_existing ) {
int i,cove = false;
struct stemdata *stem;
BasePoint dir;
dir.x = ( is_h ) ? 1 : 0;
dir.y = ( is_h ) ? 0 : 1;
if ( pd2 != NULL )
cove = ( dir.x == 0 && pd->x_extr + pd2->x_extr == 3 ) ||
( dir.y == 0 && pd->y_extr + pd2->y_extr == 3 );
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( IsUnitHV( &stem->unit,true ) &&
( pd2 != NULL && BothOnStem( stem,&pd->sp->me,&pd2->sp->me,false,false,cove )))
break;
}
if ( i==gd->stemcnt ) stem=NULL;
if ( stem == NULL && pd2 != NULL && !require_existing )
stem = NewStem( gd,&dir,&pd->sp->me,&pd2->sp->me );
return( stem );
}
static int IsDiagonalEnd( struct glyphdata *gd,
struct pointdata *pd1,struct pointdata *pd2,int is_next,int require_existing ) {
/* suppose we have something like */
/* *--* */
/* \ \ */
/* \ \ */
/* Then let's create a vertical stem between the two points */
/* (and a horizontal stem if the thing is rotated 90 degrees) */
double width, length1, length2, dist1, dist2;
BasePoint *pt1, *pt2, *dir1, *dir2, *prevdir1, *prevdir2;
SplinePoint *prevsp1, *prevsp2;
struct pointdata *prevpd1, *prevpd2;
int hv;
if ( pd1->colinear || pd2->colinear )
return( false );
pt1 = &pd1->sp->me; pt2 = &pd2->sp->me;
/* Both key points of a diagonal end stem should have nearly the same */
/* coordinate by x or y (otherwise we can't determine by which axis */
/* it should be hinted) */
if ( pt1->x >= pt2->x - dist_error_hv && pt1->x <= pt2->x + dist_error_hv ) {
width = pd1->sp->me.y - pd2->sp->me.y;
hv = 1;
} else if ( pt1->y >= pt2->y - dist_error_hv && pt1->y <= pt2->y + dist_error_hv ) {
width = pd1->sp->me.x - pd2->sp->me.x;
hv = 2;
} else
return( false );
dir1 = ( is_next ) ? &pd1->nextunit : &pd1->prevunit;
dir2 = ( is_next ) ? &pd2->prevunit : &pd2->nextunit;
if ( IsUnitHV( dir1,true )) /* Must be diagonal */
return( false );
prevsp1 = ( is_next ) ? pd1->sp->next->to : pd1->sp->prev->from;
prevsp2 = ( is_next ) ? pd2->sp->prev->from : pd2->sp->next->to;
prevpd1 = &gd->points[prevsp1->ptindex];
prevpd2 = &gd->points[prevsp2->ptindex];
prevdir1 = ( is_next ) ? &prevpd1->prevunit : &prevpd1->nextunit;
prevdir2 = ( is_next ) ? &prevpd2->nextunit : &prevpd2->prevunit;
/* Ensure we have got a real diagonal, i. e. its sides are parallel */
if ( !UnitsParallel( dir1,dir2,true ) || !UnitsParallel( prevdir1,prevdir2,true ))
return( false );
/* Diagonal width should be smaller than its length */
length1 = pow(( prevsp1->me.x - pt1->x ),2 ) + pow(( prevsp1->me.y - pt1->y ),2 );
length2 = pow(( prevsp2->me.x - pt2->x ),2 ) + pow(( prevsp2->me.y - pt2->y ),2 );
if ( length2 < length1 ) length1 = length2;
if ( pow( width,2 ) > length1 )
return( false );
/* Finally exclude too short diagonals where the distance between key */
/* points of one edge at the direction orthogonal to the unit vector */
/* of the stem we are about to add is smaller than normal HV stem */
/* fudge. Such diagonals may be later turned into HV stems, and we will */
/* result into getting two coincident hints */
dist1 = ( hv == 1 ) ? prevsp1->me.y - pt1->y : prevsp1->me.x - pt1->x;
dist2 = ( hv == 1 ) ? prevsp2->me.y - pt2->y : prevsp2->me.x - pt2->x;
if ( dist1 < 0 ) dist1 = -dist1;
if ( dist2 < 0 ) dist2 = -dist2;
if ( dist1 < 2*dist_error_hv && dist2 < 2*dist_error_hv )
return( false );
return( hv );
}
static struct stemdata *TestStem( struct glyphdata *gd,struct pointdata *pd,
BasePoint *dir,SplinePoint *match,int is_next,int is_next2,int require_existing,uint8 is_stub,int eidx ) {
struct pointdata *pd2;
struct stemdata *stem, *destem;
struct stem_chunk *chunk;
struct linedata *otherline;
double width;
struct linedata *line, *line2;
BasePoint *mdir, middle;
int de=false, hv, l_changed;
width = ( match->me.x - pd->sp->me.x )*dir->y -
( match->me.y - pd->sp->me.y )*dir->x;
if ( width < 0 ) width = -width;
if ( width < .5 )
return( NULL ); /* Zero width stems aren't interesting */
if (( is_next && pd->sp->next->to==match ) || ( !is_next && pd->sp->prev->from==match ))
return( NULL ); /* Don't want a stem between two splines that intersect */
pd2 = &gd->points[match->ptindex];
line = is_next ? pd->nextline : pd->prevline;
mdir = is_next2 ? &pd2->nextunit : &pd2->prevunit;
line2 = is_next2 ? pd2->nextline : pd2->prevline;
if ( !IsUnitHV( mdir,true ) && line2 != NULL )
mdir = &line2->unit;
if ( mdir->x==0 && mdir->y==0 )
return( NULL ); /* cannot determine the opposite point's direction */
if ( !UnitsParallel( mdir,dir,true ) && !is_stub )
return( NULL ); /* Cannot make a stem if edges are not parallel (unless it is a serif) */
if ( is_stub & 1 && !IsUnitHV( dir,true )) {
/* For serifs we prefer the vector which is closer to horizontal/vertical */
middle = MiddleUnit( dir,mdir );
if ( UnitCloserToHV( &middle,dir ) == 1 && UnitCloserToHV( &middle,mdir ) == 1 )
dir = &middle;
else if ( UnitCloserToHV( mdir,dir ) == 1 )
dir = mdir;
if ( !IsUnitHV( dir,true ) &&
( hint_diagonal_ends || require_existing ))
de = IsDiagonalEnd( gd,pd,pd2,is_next,require_existing );
}
stem = FindStem( gd,pd,pd2,dir,is_next2,de );
destem = NULL;
if ( de )
destem = FindOrMakeHVStem( gd,pd,pd2,( de == 1 ),require_existing );
if ( stem == NULL && !require_existing )
stem = NewStem( gd,dir,&pd->sp->me,&match->me );
if ( stem != NULL ) {
chunk = AddToStem( gd,stem,pd,pd2,is_next,is_next2,false );
if ( chunk != NULL ) {
chunk->stub = is_stub;
chunk->l_e_idx = chunk->r_e_idx = eidx;
}
if ( chunk != NULL && gd->linecnt > 0 ) {
hv = IsUnitHV( &stem->unit,true );
/* For HV stems allow assigning a line to a stem edge only */
/* if that line also has an exactly HV vector */
if ( line != NULL && (( !hv &&
UnitsParallel( &stem->unit,&line->unit,true ) &&
RecalcStemOffsets( stem,&line->unit,true,true )) ||
( hv && line->unit.x == stem->unit.x && line->unit.y == stem->unit.y ))) {
otherline = NULL; l_changed = false;
if (( stem->leftline == NULL ||
stem->leftline->length < line->length ) && chunk->l == pd ) {
stem->leftline = line;
l_changed = true;
otherline = stem->rightline;
} else if (( stem->rightline == NULL ||
stem->rightline->length < line->length ) && chunk->r == pd ) {
stem->rightline = line;
l_changed = true;
otherline = stem->leftline;
}
/* If lines are attached to both sides of a diagonal stem, */
/* then prefer the longer line */
if ( !hv && l_changed && !stem->positioned &&
( otherline == NULL || ( otherline->length < line->length )))
SetStemUnit( stem,line->unit );
}
if ( line2 != NULL && (( !hv &&
UnitsParallel( &stem->unit,&line2->unit,true ) &&
RecalcStemOffsets( stem,&line2->unit,true,true )) ||
( hv && line2->unit.x == stem->unit.x && line2->unit.y == stem->unit.y ))) {
otherline = NULL; l_changed = false;
if (( stem->leftline == NULL ||
stem->leftline->length < line2->length ) && chunk->l == pd2 ) {
stem->leftline = line2;
l_changed = true;
otherline = stem->rightline;
} else if (( stem->rightline == NULL ||
stem->rightline->length < line2->length ) && chunk->r == pd2 ) {
stem->rightline = line2;
l_changed = true;
otherline = stem->leftline;
}
if ( !hv && l_changed && !stem->positioned &&
( otherline == NULL || ( otherline->length < line2->length )))
SetStemUnit( stem,line2->unit );
}
}
}
if ( destem != NULL )
AddToStem( gd,destem,pd,pd2,is_next,!is_next,1 );
return( stem );
}
static double FindSameSlope(Spline *s,BasePoint *dir,double close_to) {
double a, b, c, desc;
double t1, t2;
double d1, d2;
if ( s==NULL )
return( -1e4 );
a = dir->x*s->splines[1].a*3 - dir->y*s->splines[0].a*3;
b = dir->x*s->splines[1].b*2 - dir->y*s->splines[0].b*2;
c = dir->x*s->splines[1].c - dir->y*s->splines[0].c ;
if ( a!=0 ) {
desc = b*b - 4*a*c;
if ( desc<0 )
return( -1e4 );
desc = sqrt(desc);
t1 = (-b+desc)/(2*a);
t2 = (-b-desc)/(2*a);
if ( (d1=t1-close_to)<0 ) d1 = -d1;
if ( (d2=t2-close_to)<0 ) d2 = -d2;
if ( d2<d1 && t2>=-.001 && t2<=1.001 )
t1 = t2;
} else if ( b!=0 )
t1 = -c/b;
else
return( -1e4 );
return( t1 );
}
/* This function is used when generating stem data for preexisting */
/* stem hints. If we already know the desired hint position, then we */
/* can safely assign to this hint any points which meet other conditions */
/* but have no corresponding position at the opposite edge. */
static int HalfStemNoOpposite( struct glyphdata *gd,struct pointdata *pd,
struct stemdata *stem,BasePoint *dir,int is_next ) {
int i, ret=0, allowleft, allowright, hv, corner;
struct stemdata *tstem;
for ( i=0; i<gd->stemcnt; ++i ) {
tstem = &gd->stems[i];
if ( tstem->bbox || !tstem->positioned || tstem == stem )
continue;
allowleft = ( !tstem->ghost || tstem->width == 20 );
allowright = ( !tstem->ghost || tstem->width == 21 );
hv = IsUnitHV( &tstem->unit,true );
corner = (( pd->x_corner && hv == 2 ) || ( pd->y_corner && hv == 1 ));
if ( UnitsParallel( &tstem->unit,dir,true ) || tstem->ghost || corner ) {
if ( OnStem( tstem,&pd->sp->me,true ) && allowleft ) {
if ( IsCorrectSide( gd,pd,is_next,true,&tstem->unit )) {
AddToStem( gd,tstem,pd,NULL,is_next,false,false );
ret++;
}
} else if ( OnStem( tstem,&pd->sp->me,false ) && allowright ) {
if ( IsCorrectSide( gd,pd,is_next,false,&tstem->unit )) {
AddToStem( gd,tstem,NULL,pd,false,is_next,false );
ret++;
}
}
}
}
return( ret );
}
static struct stemdata *HalfStem( struct glyphdata *gd,struct pointdata *pd,
BasePoint *dir,Spline *other,double other_t,int is_next,int eidx ) {
/* Find the spot on other where the slope is the same as dir */
double t1;
double width;
BasePoint match;
struct stemdata *stem = NULL, *tstem;
struct pointdata *pd2 = NULL, *tpd;
int i;
t1 = FindSameSlope( other,dir,other_t );
if ( t1==-1e4 )
return( NULL );
if ( t1<0 && other->from->prev!=NULL && gd->points[other->from->ptindex].colinear ) {
other = other->from->prev;
t1 = FindSameSlope(other,dir,1.0);
} else if ( t1>1 && other->to->next!=NULL && gd->points[other->to->ptindex].colinear ) {
other = other->to->next;
t1 = FindSameSlope(other,dir,0.0);
}
if ( t1<-.001 || t1>1.001 )
return( NULL );
/* Ok. the opposite edge has the right slope at t1 */
/* Now see if we can make a one sided stem out of these two */
match.x = ((other->splines[0].a*t1+other->splines[0].b)*t1+other->splines[0].c)*t1+other->splines[0].d;
match.y = ((other->splines[1].a*t1+other->splines[1].b)*t1+other->splines[1].c)*t1+other->splines[1].d;
width = (match.x-pd->sp->me.x)*dir->y - (match.y-pd->sp->me.y)*dir->x;
/* offset = (match.x-pd->sp->me.x)*dir->x + (match.y-pd->sp->me.y)*dir->y;*/
if ( width<.5 && width>-.5 )
return( NULL ); /* Zero width stems aren't interesting */
if ( isnan(t1))
IError( "NaN value in HalfStem" );
if ( is_next ) {
pd->nextedges[eidx] = other;
pd->next_e_t[eidx] = t1;
} else {
pd->prevedges[eidx] = other;
pd->prev_e_t[eidx] = t1;
}
/* In my experience the only case where this function may be useful */
/* is when it occasionally finds a real spline point which for some */
/* reasons has been neglected by other tests and yet forms a valid */
/* pair for the first point. So run through points and see if we */
/* have actually got just a position on spline midway between to points, */
/* or it is a normal point allowing to make a normal stem chunk */
for ( i=0; i<gd->pcnt; ++i ) {
tpd = &gd->points[i];
if ( tpd->sp != NULL && tpd->sp->me.x == match.x && tpd->sp->me.y == match.y ) {
pd2 = tpd;
break;
}
}
for ( i=0; i<gd->stemcnt; ++i ) {
tstem = &gd->stems[i];
if ( UnitsParallel( &tstem->unit,dir,true ) &&
BothOnStem( tstem,&pd->base,&match,false,false,false )) {
stem = tstem;
break;
}
}
if ( stem == NULL )
stem = NewStem(gd,dir,&pd->sp->me,&match);
AddToStem( gd,stem,pd,pd2,is_next,false,false );
return( stem );
}
static int ConnectsAcross( struct glyphdata *gd,SplinePoint *sp,
int is_next,Spline *findme,int eidx ) {
struct pointdata *pd = &gd->points[sp->ptindex];
Spline *other, *test;
BasePoint dir;
other = ( is_next ) ? pd->nextedges[eidx] : pd->prevedges[eidx];
if ( other==findme )
return( true );
if ( other==NULL )
return( false );
dir.x = ( is_next ) ? -pd->nextunit.x : pd->prevunit.x;
dir.y = ( is_next ) ? -pd->nextunit.y : pd->prevunit.y;
test = other->to->next;
while ( test!=NULL && test != other &&
gd->points[test->from->ptindex].nextunit.x * dir.x +
gd->points[test->from->ptindex].nextunit.y * dir.y > 0 ) {
if ( test==findme )
return( true );
test = test->to->next;
}
dir.x = ( is_next ) ? pd->nextunit.x : -pd->prevunit.x;
dir.y = ( is_next ) ? pd->nextunit.y : -pd->prevunit.y;
test = other->from->prev;
while ( test!=NULL && test != other &&
gd->points[test->to->ptindex].prevunit.x * dir.x +
gd->points[test->to->ptindex].prevunit.y * dir.y > 0 ) {
if ( test==findme )
return( true );
test = test->from->prev;
}
return( false );
}
static int ConnectsAcrossToStem( struct glyphdata *gd,struct pointdata *pd,
int is_next,struct stemdata *target,int is_l,int eidx ) {
Spline *other, *test;
BasePoint dir;
struct pointdata *tpd;
int ecnt, stemidx;
ecnt = ( is_next ) ? pd->next_e_cnt : pd->prev_e_cnt;
if ( ecnt < eidx + 1 )
return( false );
other = ( is_next ) ? pd->nextedges[eidx] : pd->prevedges[eidx];
test = other;
dir.x = ( is_next ) ? pd->nextunit.x : -pd->prevunit.x;
dir.y = ( is_next ) ? pd->nextunit.y : -pd->prevunit.y;
do {
tpd = &gd->points[test->to->ptindex];
stemidx = IsStemAssignedToPoint( tpd,target,false );
if ( stemidx != -1 && tpd->prev_is_l[stemidx] == !is_l &&
IsSplinePeak( gd,tpd,rint( target->unit.y ),rint( target->unit.y ),7 ))
return( true );
test = test->to->next;
} while ( test!=NULL && test != other && stemidx == -1 &&
( tpd->prevunit.x * dir.x + tpd->prevunit.y * dir.y >= 0 ));
test = other;
dir.x = ( is_next ) ? -pd->nextunit.x : pd->prevunit.x;
dir.y = ( is_next ) ? -pd->nextunit.y : pd->prevunit.y;
do {
tpd = &gd->points[test->from->ptindex];
stemidx = IsStemAssignedToPoint( tpd,target,true );
if ( stemidx != -1 && tpd->next_is_l[stemidx] == !is_l &&
IsSplinePeak( gd,tpd,rint( target->unit.y ),rint( target->unit.y ),7 ))
return( true );
test = test->from->prev;
} while ( test!=NULL && test != other && stemidx == -1 &&
( tpd->nextunit.x * dir.x + tpd->nextunit.y * dir.y >= 0 ));
return( false );
}
static double RecalcT( Spline *base,SplinePoint *from, SplinePoint *to, double curt ) {
double baselen, fromlen, tolen, ret;
Spline *cur;
baselen = SplineLength( base );
fromlen = baselen * curt;
tolen = baselen * ( 1 - curt );
cur = base->from->prev;
while ( cur != NULL && cur->to != from ) {
fromlen += SplineLength( cur );
cur = cur->from->prev;
}
cur = base->to->next;
while ( cur!= NULL && cur->from != to ) {
tolen += SplineLength( cur );
cur = cur->to->next;
}
ret = fromlen/( fromlen + tolen );
return( ret );
}
static int BuildStem( struct glyphdata *gd,struct pointdata *pd,int is_next,
int require_existing,int has_existing,int eidx ) {
BasePoint *dir;
Spline *other, *cur;
double t;
double tod, fromd, dist;
SplinePoint *testpt, *topt, *frompt;
struct linedata *line;
struct pointdata *testpd, *topd, *frompd;
int tp, fp, t_needs_recalc=false, ret=0;
uint8 tstub=0, fstub=0;
BasePoint opposite;
struct stemdata *stem=NULL;
if ( is_next ) {
dir = &pd->nextunit;
other = pd->nextedges[eidx];
cur = pd->sp->next;
t = pd->next_e_t[eidx];
dist = pd->next_dist[eidx];
} else {
dir = &pd->prevunit;
other = pd->prevedges[eidx];
cur = pd->sp->prev;
t = pd->prev_e_t[eidx];
dist = pd->prev_dist[eidx];
}
topt = other->to; frompt = other->from;
topd = &gd->points[topt->ptindex];
frompd = &gd->points[frompt->ptindex];
line = is_next ? pd->nextline : pd->prevline;
if ( !IsUnitHV( dir,true ) && line != NULL)
dir = &line->unit;
if ( other==NULL )
return( 0 );
opposite.x = ((other->splines[0].a*t+other->splines[0].b)*t+other->splines[0].c)*t+other->splines[0].d;
opposite.y = ((other->splines[1].a*t+other->splines[1].b)*t+other->splines[1].c)*t+other->splines[1].d;
if ( eidx == 0 ) tstub = IsStubOrIntersection( gd,dir,pd,topd,is_next,false );
if ( eidx == 0 ) fstub = IsStubOrIntersection( gd,dir,pd,frompd,is_next,true );
tp = ParallelToDir( topd,false,dir,&opposite,pd->sp,tstub );
fp = ParallelToDir( frompd,true,dir,&opposite,pd->sp,fstub );
/* if none of the opposite points is parallel to the needed vector, then */
/* give it one more chance by skipping those points and looking at the next */
/* and previous one. This can be useful in situations where the opposite */
/* edge cannot be correctly detected just because there are too many points */
/* on the spline (which is a very common situation for poorly designed */
/* fonts or fonts with quadratic splines). */
/* But do that only for colinear spline segments and ensure that there are */
/* no bends between two splines. */
if ( !tp && ( !fp || t > 0.5 ) &&
topd->colinear && &other->to->next != NULL ) {
testpt = topt->next->to;
testpd = &gd->points[testpt->ptindex];
BasePoint *initdir = &topd->prevunit;
while ( !tp && topd->colinear && pd->sp != testpt && other->from != testpt && (
testpd->prevunit.x * initdir->x +
testpd->prevunit.y * initdir->y > 0 )) {
topt = testpt; topd = testpd;
tp = ParallelToDir( topd,false,dir,&opposite,pd->sp,false );
testpt = topt->next->to;
testpd = &gd->points[testpt->ptindex];
}
if ( tp ) t_needs_recalc = true;
}
if ( !fp && ( !fp || t < 0.5 ) &&
frompd->colinear && &other->from->prev != NULL ) {
testpt = frompt->prev->from;
testpd = &gd->points[testpt->ptindex];
BasePoint *initdir = &frompd->prevunit;
while ( !fp && frompd->colinear && pd->sp != testpt && other->to != testpt && (
testpd->prevunit.x * initdir->x +
testpd->prevunit.y * initdir->y > 0 )) {
frompt = testpt; frompd = testpd;
fp = ParallelToDir( frompd,true,dir,&opposite,pd->sp,false );
testpt = frompt->prev->from;
testpd = &gd->points[testpt->ptindex];
}
if ( fp ) t_needs_recalc = true;
}
if ( t_needs_recalc )
t = RecalcT( other,frompt,topt,t );
if ( !tp && !fp ) {
if ( has_existing )
ret = HalfStemNoOpposite( gd,pd,NULL,dir,is_next );
return( ret );
}
/* We have several conflicting metrics for getting the "better" stem */
/* Generally we prefer the stem with the smaller width (but not always. See tilde) */
/* Generally we prefer the stem formed by the point closer to the intersection */
tod = (1-t)*NormalDist( &topt->me,&pd->sp->me,dir );
fromd = t*NormalDist( &frompt->me,&pd->sp->me,dir );
if ( tp && (( tod<fromd ) ||
( !fp && ( tod<2*fromd || dist < topd->prev_dist[eidx] ||
ConnectsAcross( gd,frompt,true,cur,eidx ) || NearlyParallel( dir,other,t ))))) {
stem = TestStem( gd,pd,dir,topt,is_next,false,require_existing,tstub,eidx );
}
if ( stem == NULL && fp && (( fromd<tod ) ||
( !tp && ( fromd<2*tod || dist < frompd->next_dist[eidx] ||
ConnectsAcross( gd,topt,false,cur,eidx ) || NearlyParallel( dir,other,t ))))) {
stem = TestStem( gd,pd,dir,frompt,is_next,true,require_existing,fstub,eidx );
}
if ( eidx == 0 && stem == NULL && !require_existing && cur!=NULL &&
!other->knownlinear && !cur->knownlinear )
stem = HalfStem( gd,pd,dir,other,t,is_next,eidx );
if ( stem != NULL ) ret = 1;
if ( has_existing )
ret += HalfStemNoOpposite( gd,pd,stem,dir,is_next );
return( ret );
}
static void AssignLinePointsToStems( struct glyphdata *gd ) {
struct pointdata *pd;
struct stemdata *stem;
struct linedata *line;
struct stem_chunk *chunk;
int i, j, stem_hv, line_hv, needs_hv=false;
for ( i=0; i<gd->stemcnt; ++i ) if ( !gd->stems[i].toobig ) {
stem = &gd->stems[i];
stem_hv = IsUnitHV( &stem->unit,true );
needs_hv = ( stem_hv || ( stem->chunk_cnt == 1 &&
stem->chunks[0].stub && IsUnitHV( &stem->unit,false )));
if ( stem->leftline != NULL ) {
line = stem->leftline;
line_hv = ( needs_hv && LineFitsHV( line ));
if ( needs_hv && !line_hv )
stem->leftline = NULL;
else {
for ( j=0; j<line->pcnt; j++ ) {
pd = line->points[j];
if ( pd->prevline == line && OnStem( stem,&pd->base,true ) &&
IsStemAssignedToPoint( pd,stem,false ) == -1) {
chunk = AddToStem( gd,stem,pd,NULL,false,false,false );
chunk->lpotential = true;
} if ( pd->nextline == line && OnStem( stem,&pd->base,true ) &&
IsStemAssignedToPoint( pd,stem,true ) == -1 ) {
chunk = AddToStem( gd,stem,pd,NULL,true,false,false );
chunk->lpotential = true;
}
}
}
}
if ( stem->rightline != NULL ) {
line = stem->rightline;
line_hv = ( needs_hv && LineFitsHV( line ));
if ( needs_hv && !line_hv )
stem->rightline = NULL;
else {
for ( j=0; j<line->pcnt; j++ ) {
pd = line->points[j];
if ( pd->prevline == line && OnStem( stem,&pd->base,false ) &&
IsStemAssignedToPoint( pd,stem,false ) == -1 ) {
chunk = AddToStem( gd,stem,NULL,pd,false,false,false );
chunk->rpotential = true;
} if ( pd->nextline == line && OnStem( stem,&pd->base,false ) &&
IsStemAssignedToPoint( pd,stem,true ) == -1 ) {
chunk = AddToStem( gd,stem,NULL,pd,false,true,false );
chunk->rpotential = true;
}
}
}
}
}
}
static struct stemdata *DiagonalCornerStem( struct glyphdata *gd,
struct pointdata *pd,int require_existing ) {
Spline *other = pd->bothedge;
struct pointdata *pfrom = NULL, *pto = NULL, *pd2 = NULL, *pd3=NULL;
double width, length;
struct stemdata *stem;
struct stem_chunk *chunk;
pfrom = &gd->points[other->from->ptindex];
pto = &gd->points[other->to->ptindex];
if ( pd->symetrical_h && pto->symetrical_h && pd->both_e_t>.9 )
pd2 = pto;
else if ( pd->symetrical_h && pfrom->symetrical_h && pd->both_e_t<.1 )
pd2 = pfrom;
else if ( pd->symetrical_v && pto->symetrical_v && pd->both_e_t>.9 )
pd2 = pto;
else if ( pd->symetrical_v && pfrom->symetrical_v && pd->both_e_t<.1 )
pd2 = pfrom;
else if ( pd->symetrical_h && other->islinear && other->splines[1].c==0 ) {
pd2 = pfrom;
pd3 = pto;
} else if ( pd->symetrical_v && other->islinear && other->splines[0].c==0 ) {
pd2 = pfrom;
pd3 = pto;
} else
return( NULL );
if ( pd->symetrical_v )
width = (pd->sp->me.x-pd2->sp->me.x);
else
width = (pd->sp->me.y-pd2->sp->me.y);
length = (pd->sp->next->to->me.x-pd->sp->me.x)*(pd->sp->next->to->me.x-pd->sp->me.x) +
(pd->sp->next->to->me.y-pd->sp->me.y)*(pd->sp->next->to->me.y-pd->sp->me.y);
if ( width*width>length )
return( NULL );
stem = FindOrMakeHVStem(gd,pd,pd2,pd->symetrical_h,require_existing);
if ( pd3 == NULL && stem != NULL )
chunk = AddToStem( gd,stem,pd,pd2,2,2,2 );
else if ( stem != NULL ) {
chunk = AddToStem( gd,stem,pd,pd2,2,2,3 );
chunk = AddToStem( gd,stem,pd,pd3,2,2,3 );
}
return( stem );
}
static int chunk_cmp( const void *_p1, const void *_p2 ) {
const struct stem_chunk *ch1 = _p1, *ch2 = _p2;
struct stemdata *stem;
double loff1=0,roff1=0,loff2=0,roff2=0;
stem = ch1->parent;
if ( stem==NULL )
return( 0 );
if ( ch1->l != NULL )
loff1 = ( ch1->l->sp->me.x - stem->left.x ) * stem->unit.x +
( ch1->l->sp->me.y - stem->left.y ) * stem->unit.y;
if ( ch1->r != NULL )
roff1 = ( ch1->r->sp->me.x - stem->right.x ) * stem->unit.x +
( ch1->r->sp->me.y - stem->right.y ) * stem->unit.y;
if ( ch2->l != NULL )
loff2 = ( ch2->l->sp->me.x - stem->left.x ) * stem->unit.x +
( ch2->l->sp->me.y - stem->left.y ) * stem->unit.y;
if ( ch2->r != NULL )
roff2 = ( ch2->r->sp->me.x - stem->right.x ) * stem->unit.x +
( ch2->r->sp->me.y - stem->right.y ) * stem->unit.y;
if ( loff1>loff2 )
return( 1 );
else if ( loff1<loff2 )
return( -1 );
else {
if ( roff1>roff2 )
return( 1 );
else if ( roff1<roff2 )
return( -1 );
else
return( 0 );
}
}
static int stem_cmp( const void *_p1, const void *_p2 ) {
struct stemdata * const *st1 = _p1, * const *st2 = _p2;
double start1, end1, start2, end2;
if ( fabs( (*st1)->unit.x ) > fabs( (*st1)->unit.y )) {
start1 = (*st1)->right.y; end1 = (*st1)->left.y;
start2 = (*st2)->right.y; end2 = (*st2)->left.y;
} else {
start1 = (*st1)->left.x; end1 = (*st1)->right.x;
start2 = (*st2)->left.x; end2 = (*st2)->right.x;
}
if ( start1 > start2 )
return( 1 );
else if ( start1 < start2 )
return( -1 );
else {
if ( end1 > end2 )
return( 1 );
else if ( end1 < end2 )
return( -1 );
else
return( 0 );
}
}
static void FixupT( struct pointdata *pd,int stemidx,int isnext, int eidx ) {
/* When we calculated "next/prev_e_t" we deliberately did not use pd1->me */
/* (because things get hard at intersections) so our t is only an approx-*/
/* imation. We can do a lot better now */
Spline *s;
Spline myline;
SplinePoint end1, end2;
double width,t,sign, len, dot;
BasePoint pts[9];
extended lts[10], sts[10];
BasePoint diff;
struct stemdata *stem ;
if ( pd == NULL || stemidx == -1 )
return;
stem = ( isnext ) ? pd->nextstems[stemidx] : pd->prevstems[stemidx];
width = ( stem->right.x - stem->left.x )*stem->unit.y -
( stem->right.y-stem->left.y )*stem->unit.x;
s = ( isnext ) ? pd->nextedges[eidx] : pd->prevedges[eidx];
if ( s==NULL )
return;
diff.x = s->to->me.x-s->from->me.x;
diff.y = s->to->me.y-s->from->me.y;
if ( diff.x<.001 && diff.x>-.001 && diff.y<.001 && diff.y>-.001 )
return; /* Zero length splines give us NaNs */
len = sqrt( pow( diff.x,2 ) + pow( diff.y,2 ));
dot = ( diff.x*stem->unit.x + diff.y*stem->unit.y )/len;
if ( dot < .0004 && dot > -.0004 )
return; /* It's orthogonal to our stem */
if (( stem->unit.x==1 || stem->unit.x==-1 ) && s->knownlinear )
t = (pd->sp->me.x-s->from->me.x)/(s->to->me.x-s->from->me.x);
else if (( stem->unit.y==1 || stem->unit.y==-1 ) && s->knownlinear )
t = (pd->sp->me.y-s->from->me.y)/(s->to->me.y-s->from->me.y);
else {
memset(&myline,0,sizeof(myline));
memset(&end1,0,sizeof(end1));
memset(&end2,0,sizeof(end2));
sign = (( isnext && pd->next_is_l[stemidx] ) || ( !isnext && pd->prev_is_l[stemidx] )) ? 1 : -1;
myline.knownlinear = myline.islinear = true;
end1.me = pd->sp->me;
end2.me.x = pd->sp->me.x+1.1*sign*width*stem->l_to_r.x;
end2.me.y = pd->sp->me.y+1.1*sign*width*stem->l_to_r.y;
end1.nextcp = end1.prevcp = end1.me;
end2.nextcp = end2.prevcp = end2.me;
end1.nonextcp = end1.noprevcp = end2.nonextcp = end2.noprevcp = true;
end1.next = &myline; end2.prev = &myline;
myline.from = &end1; myline.to = &end2;
myline.splines[0].d = end1.me.x;
myline.splines[0].c = end2.me.x-end1.me.x;
myline.splines[1].d = end1.me.y;
myline.splines[1].c = end2.me.y-end1.me.y;
if ( SplinesIntersect(&myline,s,pts,lts,sts)<=0 )
return;
t = sts[0];
}
if ( isnan(t))
IError( "NaN value in FixupT" );
if ( isnext )
pd->next_e_t[eidx] = t;
else
pd->prev_e_t[eidx] = t;
}
/* flags: 1 -- accept curved extrema, 2 -- accept angles, */
/* 4 -- analyze segments (not just single points) */
static int IsSplinePeak( struct glyphdata *gd,struct pointdata *pd,
int outer,int is_x,int flags ) {
double base, next, prev, nextctl, prevctl, unit_p, unit_n;
Spline *s, *snext, *sprev;
struct monotonic **space, *m;
int wprev, wnext, i, desired;
SplinePoint *sp = pd->sp;
base = ((real *) &sp->me.x)[!is_x];
nextctl = sp->nonextcp ? base : ((real *) &sp->nextcp.x)[!is_x];
prevctl = sp->noprevcp ? base : ((real *) &sp->prevcp.x)[!is_x];
next = prev = base;
snext = sp->next; sprev = sp->prev;
if ( snext->to == NULL || sprev->from == NULL )
return( false );
if (!( flags & 2) && ( sp->nonextcp || sp->noprevcp ))
return( false );
else if (!( flags & 1 ) && ( pd->colinear ))
return( false );
if ( flags & 4 ) {
while ( snext->to->next != NULL && snext->to != sp && next == base ) {
next = ((real *) &snext->to->me.x)[!is_x];
snext = snext->to->next;
}
while ( sprev->from->prev != NULL && sprev->from != sp && prev == base ) {
prev = ((real *) &sprev->from->me.x)[!is_x];
sprev = sprev->from->prev;
}
} else {
next = ((real *) &snext->to->me.x)[!is_x];
prev = ((real *) &sprev->from->me.x)[!is_x];
}
if ( prev<base && next<base && nextctl<=base && prevctl<=base )
desired = ( outer ) ? -1 : 1;
else if ( prev>base && next>base && prevctl>=base && nextctl>=base )
desired = ( outer ) ? 1 : -1;
else
return( false );
MonotonicFindAt( gd->ms,is_x,((real *) &sp->me.x)[is_x],space = gd->space );
wprev = wnext = 0;
for ( i=0; space[i]!=NULL; ++i ) {
m = space[i];
s = m->s;
if ( s->from == sp )
wnext = ((&m->xup)[is_x] ? 1 : -1 );
else if ( s->to == sp )
wprev = ((&m->xup)[is_x] ? 1 : -1 );
}
if ( wnext != 0 && wprev != 0 && wnext != wprev ) {
unit_p = (&pd->prevunit.x)[!is_x];
unit_n = (&pd->nextunit.x)[!is_x];
if ( unit_p < unit_n && (
( outer && wprev == 1 ) || ( !outer && wprev == -1 )))
return( desired );
else if ( unit_p > unit_n && (
( outer && wnext == 1 ) || ( !outer && wnext == -1 )))
return( desired );
} else {
if ( wnext == desired || wprev == desired )
return( desired );
}
return( false );
}
static struct pointdata *FindClosestOpposite(
struct stemdata *stem,struct stem_chunk **chunk,SplinePoint *sp,int *next ) {
struct pointdata *pd, *ret=NULL;
struct stem_chunk *testchunk;
double test, proj=1e4;
int i, is_l;
for ( i=0; i<stem->chunk_cnt; ++i ) {
testchunk = &stem->chunks[i];
pd = NULL;
if ( testchunk->l != NULL && testchunk->l->sp==sp ) {
pd = testchunk->r;
is_l = false;
} else if ( testchunk->r != NULL && testchunk->r->sp==sp ) {
pd = testchunk->l;
is_l = true;
}
if ( pd != NULL ) {
test = ( pd->sp->me.x-sp->me.x ) * stem->unit.x +
( pd->sp->me.y-sp->me.y ) * stem->unit.y;
if ( test < 0 ) test = -test;
if ( test < proj ) {
ret = pd;
proj = test;
*chunk = testchunk;
}
}
}
if ( ret != NULL )
*next = ( is_l ) ? (*chunk)->lnext : (*chunk)->rnext;
return( ret );
}
static int ValueChunk( struct glyphdata *gd,struct vchunk *vchunks,
int chcnt,int idx,int l_base ) {
int peak1=0, peak2=0, val=0;
int i, is_x, base_next, opp_next;
struct pointdata *base, *opp, *frompd, *topd;
struct stem_chunk *chunk = vchunks[idx].chunk, *tchunk;
struct stemdata *stem = chunk->parent;
double norm, dist;
Spline *sbase, *sopp, *other;
/* If a stem was already present before generating glyph data, */
/* then it should always be preferred in case of a conflict */
if ( stem->positioned || chunk->stemcheat ) val++;
if ( l_base ) {
base = chunk->l; opp = chunk->r;
base_next = chunk->lnext; opp_next = chunk->rnext;
} else {
base = chunk->r; opp = chunk->l;
base_next = chunk->rnext; opp_next = chunk->lnext;
}
sbase = ( base_next ) ? base->sp->next : base->sp->prev;
sopp = ( opp_next ) ? opp->sp->next : opp->sp->prev;
other = ( opp_next ) ? opp->nextedges[0] : opp->prevedges[0];
/* If there are 2 conflicting chunks belonging to different stems but */
/* based on the same point, then we have to decide which stem is "better" */
/* for that point. We compare stems (or rather chunks) by assigning a */
/* value to each of them and then prefer the stem whose value is positive. */
/* A chunk gets a +1 value bonus in the following cases: */
/* - The stem is vertical/horizontal and splines are curved in the same */
/* direction at both sides of the chunk; */
/* - A stem has both its width and the distance between the opposite points */
/* smaller than another stem; */
/* - The common side of two stems is a straight line formed by two points */
/* and the opposite point can be projected to line segment between those */
/* two points. */
if ( IsUnitHV( &stem->unit,true ) && !sbase->knownlinear ) {
is_x = (int) rint( stem->unit.y );
peak1 = ( is_x ) ? base->x_extr : base->y_extr;
peak2 = ( is_x ) ? opp->x_extr : opp->y_extr;
dist = ( base->base.x - opp->base.x )*stem->unit.x +
( base->base.y - opp->base.y )*stem->unit.y;
/* Are there any stems attached to the same base point which */
/* are narrower than the distance between two points forming the */
/* given chunk? */
for ( i=0; i<chcnt; i++ ) {
tchunk = vchunks[i].chunk;
if ( tchunk == NULL || tchunk == chunk || chunk->l == NULL || chunk->r == NULL )
continue;
norm = tchunk->parent->width;
if ( norm < fabs( dist ))
break;
}
/* If both points are curved in the same direction, then check also */
/* the "line of sight" between those points (if there are interventing */
/* splines, then it is not a real feature bend)*/
if ( i == chcnt && peak1 + peak2 == 3 && ConnectsAcross( gd,base->sp,opp_next,sopp,0 ))
val++;
}
frompd = &gd->points[sbase->from->ptindex];
topd = &gd->points[sbase->to->ptindex];
if (IsStemAssignedToPoint( frompd,stem,true ) != -1 &&
IsStemAssignedToPoint( topd,stem,false ) != -1 )
if ( other == sbase ) val++;
dist = vchunks[idx].dist;
for ( i=0; i<chcnt; i++ ) {
tchunk = vchunks[i].chunk;
if ( tchunk == NULL || tchunk == chunk ||
( vchunks[idx].parallel && !vchunks[i].parallel ))
continue;
if ( vchunks[i].dist <= dist || tchunk->parent->width <= stem->width )
break;
}
if ( i==chcnt ) val++;
/* If just one of the checked chunks has both its sides parallel */
/* to the stem direction, then we consider it is always worth to be output. */
/* This check was introduced to avoid situations where a stem marking */
/* a feature termination can be preferred to another stem which controls */
/* the main part of the same feature */
if ( vchunks[idx].parallel ) {
for ( i=0; i<chcnt; i++ ) {
if ( vchunks[i].chunk == NULL || vchunks[i].chunk == chunk )
continue;
if ( vchunks[i].parallel )
break;
}
if ( i == chcnt ) val++;
}
return( val );
}
static void CheckPotential( struct glyphdata *gd,struct pointdata *pd,int is_next ) {
int i, j, is_l, next1, stemcnt, val;
int val_cnt=0;
BasePoint *lunit, *runit;
struct stemdata **stems;
struct vchunk *vchunks;
struct stem_chunk *cur;
stemcnt = ( is_next ) ? pd->nextcnt : pd->prevcnt;
stems = ( is_next ) ? pd->nextstems : pd->prevstems;
vchunks = calloc( stemcnt,sizeof( VChunk ));
for ( i=0; i<stemcnt; i++ ) {
is_l = ( is_next ) ? pd->next_is_l[i] : pd->prev_is_l[i];
FindClosestOpposite( stems[i],&vchunks[i].chunk,pd->sp,&next1 );
if ( vchunks[i].chunk == NULL )
continue;
cur = vchunks[i].chunk;
if ( vchunks[i].value > 0 ) val_cnt++;
vchunks[i].dist = pow( cur->l->base.x - cur->r->base.x,2 ) +
pow( cur->l->base.y - cur->r->base.y,2 );
lunit = ( cur->lnext ) ? &cur->l->nextunit : &cur->l->prevunit;
runit = ( cur->rnext ) ? &cur->r->nextunit : &cur->r->prevunit;
vchunks[i].parallel = UnitsParallel( lunit,&stems[i]->unit,2 ) &&
UnitsParallel( runit,&stems[i]->unit,2 );
}
for ( i=0; i<stemcnt; i++ ) if ( vchunks[i].chunk != NULL ) {
vchunks[i].value = ValueChunk( gd,vchunks,stemcnt,i,is_l );
if ( vchunks[i].value ) val_cnt++;
}
/* If we was unable to figure out any reasons for which at least */
/* one of the checked chunks should really be output, then keep */
/* all the 'potential' flags as they are and do nothing */
if ( val_cnt > 0 ) {
for ( i=0; i<stemcnt; i++ ) if ( vchunks[i].chunk != NULL ) {
is_l = ( is_next ) ? pd->next_is_l[i] : pd->prev_is_l[i];
val = vchunks[i].value;
for ( j=0; j<stems[i]->chunk_cnt; j++ ) {
cur = &stems[i]->chunks[j];
if ( is_l && cur->l == pd ) {
if ( val > 0 ) cur->lpotential = false;
else cur->lpotential = true;
} else if ( !is_l && cur->r == pd ) {
if ( val > 0 ) cur->rpotential = false;
else cur->rpotential = true;
}
}
}
}
free( vchunks );
}
static int StemIsActiveAt( struct glyphdata *gd,struct stemdata *stem,double stempos ) {
BasePoint pos,cpos,mpos;
int which;
double test;
double lmin, lmax, rmin, rmax, loff, roff, minoff, maxoff;
struct monotonic **space, *m;
int winding, nw, closest, i, j;
pos.x = stem->left.x + stempos*stem->unit.x;
pos.y = stem->left.y + stempos*stem->unit.y;
if ( IsUnitHV( &stem->unit,true )) {
which = stem->unit.x==0;
MonotonicFindAt(gd->ms,which,((real *) &pos.x)[which],space = gd->space);
test = ((real *) &pos.x)[!which];
lmin = ( stem->lmax - 2*dist_error_hv < -dist_error_hv ) ?
stem->lmax - 2*dist_error_hv : -dist_error_hv;
lmax = ( stem->lmin + 2*dist_error_hv > dist_error_hv ) ?
stem->lmin + 2*dist_error_hv : dist_error_hv;
rmin = ( stem->rmax - 2*dist_error_hv < -dist_error_hv ) ?
stem->rmax - 2*dist_error_hv : -dist_error_hv;
rmax = ( stem->rmin + 2*dist_error_hv > dist_error_hv ) ?
stem->rmin + 2*dist_error_hv : dist_error_hv;
minoff = test + ( lmin * stem->unit.y - lmax * stem->unit.x );
maxoff = test + ( lmax * stem->unit.y - lmin * stem->unit.x );
winding = 0; closest = -1;
for ( i=0; space[i]!=NULL; ++i ) {
m = space[i];
nw = ((&m->xup)[which] ? 1 : -1 );
if ( m->other >= minoff && m->other <= maxoff && nw == 1 ) {
closest = i;
break;
} else if ( m->other > maxoff )
break;
winding += nw;
}
if ( closest < 0 )
return( false );
cpos.x = ( which ) ? m->other : pos.x ;
cpos.y = ( which ) ? pos.y : m->other ;
loff = ( cpos.x - stem->left.x ) * stem->unit.y -
( cpos.y - stem->left.y ) * stem->unit.x;
if ( loff > lmax || loff < lmin )
return( false );
j = MatchWinding(space,i,nw,winding,which,0);
if ( j==-1 )
return( false );
m = space[j];
mpos.x = ( which ) ? m->other : pos.x ;
mpos.y = ( which ) ? pos.y : m->other ;
roff = ( mpos.x - stem->right.x ) * stem->unit.y -
( mpos.y - stem->right.y ) * stem->unit.x;
if ( roff >= rmin && roff <= rmax )
return( true );
return( false );
} else {
return( StillStem( gd,dist_error_diag,&pos,stem ));
}
}
/* This function is used to check the distance between a hint's edge */
/* and a spline and determine the extet where this hint can be */
/* considered "active". */
static int WalkSpline( struct glyphdata *gd, struct pointdata *pd,int gonext,
struct stemdata *stem,int is_l,int force_ac,BasePoint *res ) {
int i, curved;
double off, dist, min, max;
double incr, err;
double t, ratio, width;
Spline *s;
BasePoint *base, *nunit, pos, good;
SplinePoint *sp, *nsp;
struct pointdata *npd;
err = ( IsUnitHV( &stem->unit,true )) ? dist_error_hv : dist_error_diag;
width = stem->width;
ratio = gd->emsize/( 6 * width );
if ( err > width/2) err = width/2;
sp = pd->sp;
base = ( is_l ) ? &stem->left : &stem->right;
min = ( is_l ) ? stem->lmax - 2*err : stem->rmax - 2*err;
max = ( is_l ) ? stem->lmin + 2*err : stem->rmin + 2*err;
s = ( gonext ) ? sp->next : sp->prev;
nsp = ( gonext ) ? s->to : s->from;
npd = &gd->points[nsp->ptindex];
nunit = ( gonext ) ? &npd->prevunit : &npd->nextunit;
good = sp->me;
off = ( nsp->me.x - base->x )*stem->l_to_r.x +
( nsp->me.y - base->y )*stem->l_to_r.y;
/* Some splines have tiny control points and are almost flat */
/* think of them as lines then rather than treating them as curves */
/* figure out how long they remain within a few orthoganal units of */
/* the point */
/* We used to check the distance between a control point and a spline */
/* and consider the segment "flat" if this distance is smaller than */
/* the normal allowed "error" value. However this method doesn't produce */
/* consistent results if the spline is not long enough (as usual for */
/* fonts with quadratic splines). So now we consider a spline "flat" */
/* only if it never deviates too far from the hint's edge and both */
/* its terminal points are snappable to the same hint */
curved = ( IsStemAssignedToPoint( npd,stem,gonext ) == -1 &&
( off < min || off > max || !UnitsParallel( &stem->unit,nunit,true )));
/* If a spline does deviate from the edge too far to consider it flat, */
/* then we calculate the extent where the spline and the edge are still */
/* close enough to consider the hint active at this zone. If the hint is */
/* still active at the end of the spline, we can check some subsequent splines */
/* too. This method produces better effect than any "magic" manipulations */
/* with control point coordinates, because it takes into account just the */
/* spline configuration rather than point positions */
if ( curved ) {
max = err = dist_error_curve;
min = -dist_error_curve;
/* The following statement forces our code to detect an active zone */
/* even if all checks actually fail. This makes sense for stems */
/* marking arks and bends */
if ( force_ac )
good = ( gonext ) ? sp->nextcp : sp->prevcp;
/* If a spline is closer to the opposite stem edge than to the current edge, then we */
/* can no longer consider the stem active at this point */
if ( err > width/2 ) err = width/2;
t = ( gonext ) ? 0.9999 : 0.0001;
for ( ; ; s = ( gonext ) ? s->to->next : s->from->prev ) {
pos.x = ((s->splines[0].a*t+s->splines[0].b)*t+s->splines[0].c)*t+s->splines[0].d;
pos.y = ((s->splines[1].a*t+s->splines[1].b)*t+s->splines[1].c)*t+s->splines[1].d;
off = ( pos.x - base->x )*stem->l_to_r.x +
( pos.y - base->y )*stem->l_to_r.y;
dist = ( pos.x - sp->me.x )*stem->unit.x +
( pos.y - sp->me.y )*stem->unit.y;
nsp = ( gonext ) ? s->to : s->from;
npd = &gd->points[nsp->ptindex];
if (fabs( off ) < max && fabs( dist ) <= ( width + width * ratio ) &&
nsp != sp && npd->colinear && !npd->x_extr && !npd->y_extr &&
StillStem( gd,err,&pos,stem ))
good = pos;
else
break;
}
}
t = .5;
incr = ( gonext ) ? .25 : -.25;
for ( i=0; i<6; ++i ) {
pos.x = ((s->splines[0].a*t+s->splines[0].b)*t+s->splines[0].c)*t+s->splines[0].d;
pos.y = ((s->splines[1].a*t+s->splines[1].b)*t+s->splines[1].c)*t+s->splines[1].d;
off = ( pos.x - base->x )*stem->l_to_r.x +
( pos.y - base->y )*stem->l_to_r.y;
dist = ( pos.x - sp->me.x )*stem->unit.x +
( pos.y - sp->me.y )*stem->unit.y;
/* Don't check StillStem for non-curved segments, as they are subject */
/* to further projection-related tests anyway */
if ( off > min && off < max && ( !curved ||
( fabs( dist ) < ( width + width * ratio ) &&
StillStem( gd,err,&pos,stem )))) {
good = pos;
t += incr;
} else
t -= incr;
incr/=2;
}
*res = good;
return( curved );
}
static int AdjustForImperfectSlopeMatch( SplinePoint *sp,BasePoint *pos,
BasePoint *newpos,struct stemdata *stem,int is_l ) {
double poff, err, min, max;
BasePoint *base;
base = ( is_l ) ? &stem->left : &stem->right;
err = ( IsUnitHV( &stem->unit,true )) ? dist_error_hv : dist_error_diag;
min = ( is_l ) ? stem->lmax - 2*err : stem->rmax - 2*err;
max = ( is_l ) ? stem->lmin + 2*err : stem->rmin + 2*err;
/* Possible if the stem unit has been attached to a line. It is */
/* hard to prevent this */
if ( min > max ) {
min = stem->lmin; max = stem->lmax;
}
poff = ( pos->x - base->x )*stem->l_to_r.x +
( pos->y - base->y )*stem->l_to_r.y;
if ( poff > min && poff < max ) {
*newpos = *pos;
return( false );
} else if ( poff <= min )
err = fabs( min );
else if ( poff >= max )
err = fabs( max );
newpos->x = sp->me.x + err*( pos->x - sp->me.x )/fabs( poff );
newpos->y = sp->me.y + err*( pos->y - sp->me.y )/fabs( poff );
return( true );
}
static int AddLineSegment( struct stemdata *stem,struct segment *space,int cnt,
int is_l,struct pointdata *pd,int base_next,struct glyphdata *gd ) {
double s, e, t, dot;
BasePoint stemp, etemp;
BasePoint *start, *end, *par_unit;
int same_dir, corner = 0, par;
int scurved = false, ecurved = false, c, hv;
SplinePoint *sp, *psp, *nsp;
double b;
uint8 extr;
if ( pd==NULL || (sp = pd->sp)==NULL || sp->ticked ||
sp->next==NULL || sp->prev==NULL )
return( cnt );
end = &sp->me;
start = &sp->me;
par_unit = ( base_next ) ? &pd->nextunit : &pd->prevunit;
/* Do the spline and the stem unit point in the same direction ? */
dot = ( stem->unit.x * par_unit->x ) +
( stem->unit.y * par_unit->y );
same_dir = (( dot > 0 && base_next ) || ( dot < 0 && !base_next ));
if ( stem->unit.x == 1 ) corner = pd->y_corner;
else if ( stem->unit.y == 1 ) corner = pd->x_corner;
dot = ( stem->unit.x * pd->nextunit.x ) +
( stem->unit.y * pd->nextunit.y );
/* We used to apply normal checks only if the point's unit vector pointing */
/* in the direction we are going to check is nearly parallel to the stem unit. */
/* But this is not the best method, because a spline, "parallel" to our */
/* stem, may actually have filled space at a wrong side. On the other hand, */
/* sometimes it makes sense to calculate active space even for splines */
/* connected to our base point under an angle which is too large to consider */
/* the direction "parallel". So now we check the units' direction first */
/* and then (just for straight splines) also their parallelity. */
if (( dot > 0 && same_dir ) || ( dot < 0 && !same_dir )) {
/* If the segment sp-start doesn't have exactly the right slope, then */
/* we can only use that bit of it which is less than a standard error */
par = UnitsParallel( &stem->unit,&pd->nextunit,0 );
if ( !sp->next->knownlinear ) {
ecurved = WalkSpline( gd,pd,true,stem,is_l,par,&etemp );
/* Can merge, but treat as curved relatively to projections */
if ( !ecurved ) ecurved = 2;
end = &etemp;
} else if ( par || corner ) {
nsp = sp->next->to;
ecurved = AdjustForImperfectSlopeMatch( sp,&nsp->me,&etemp,stem,is_l );
end = &etemp;
}
}
dot = ( stem->unit.x * pd->prevunit.x ) +
( stem->unit.y * pd->prevunit.y );
if (( dot < 0 && same_dir ) || ( dot > 0 && !same_dir )) {
par = UnitsParallel( &stem->unit,&pd->prevunit,0 );
if ( !sp->prev->knownlinear ) {
scurved = WalkSpline( gd,pd,false,stem,is_l,par,&stemp );
if ( !scurved ) scurved = 2;
start = &stemp;
} else if ( par || corner ) {
psp = sp->prev->from;
scurved = AdjustForImperfectSlopeMatch( sp,&psp->me,&stemp,stem,is_l );
start = &stemp;
}
}
sp->ticked = true;
s = (start->x-stem->left.x)*stem->unit.x +
(start->y-stem->left.y)*stem->unit.y;
e = ( end->x-stem->left.x)*stem->unit.x +
( end->y-stem->left.y)*stem->unit.y;
b = (sp->me.x-stem->left.x)*stem->unit.x +
(sp->me.y-stem->left.y)*stem->unit.y;
if ( s == e )
return( cnt );
if ( s > e ) {
t = s; c = scurved;
s = e; e = t;
scurved = ecurved; ecurved = c;
}
space[cnt].start = s;
space[cnt].end = e;
space[cnt].sbase = space[cnt].ebase = b;
space[cnt].scurved = scurved;
space[cnt].ecurved = ecurved;
hv = IsUnitHV( &stem->unit,true );
if ( hv ) {
/* For vertical/horizontal stems we assign a special meaning to */
/* the 'curved' field. It will be non-zero if the key point of */
/* this segment is positioned on a prominent curve: */
/* 1 if the inner side of that curve is inside of the contour */
/* and 2 otherwise. */
/* Later, if we get a pair of "inner" and "outer" curves, then */
/* we are probably dealing with a feature's bend which should be */
/* necessarily marked with a hint. Checks we apply for this type */
/* of curved segments should be less strict than in other cases. */
extr = ( hv == 1 ) ? pd->y_extr : pd->x_extr;
space[cnt].curved = extr;
} else {
/* For diagonal stems we consider a segment "curved" if both its */
/* start and end are curved. Curved segments usually cannot be */
/* merged (unless scurved or ecurved is equal to 2) and are not */
/* checked for "projections". */
space[cnt].curved = scurved && ecurved;
}
return( cnt+1 );
}
static int InActive(double projection,struct segment *segments, int cnt) {
int i;
for ( i=0; i<cnt; ++i ) {
if ( projection>=segments[i].start && projection<=segments[i].end )
return( true );
}
return( false );
}
static int MergeSegments(struct segment *space, int cnt) {
int i,j;
double middle;
for ( i=j=0; i<cnt; ++i, ++j ) {
if ( i!=j )
space[j] = space[i];
while ( i+1<cnt && space[i+1].start<space[j].end ) {
if ( space[i+1].end >= space[j].end ) {
/* If there are 2 overlapping segments and neither the */
/* end of the first segment nor the start of the second */
/* one are curved we can merge them. Otherwise we have */
/* to preserve them both, but modify their start/end properties */
/* so that the overlap is removed */
if ( space[j].ecurved != 1 && space[i+1].scurved != 1 ) {
space[j].end = space[i+1].end;
space[j].ebase = space[i+1].ebase;
space[j].ecurved = space[i+1].ecurved;
space[j].curved = false;
} else if ( space[j].ecurved != 1 && space[i+1].scurved == 1 ) {
space[i+1].start = space[j].end;
--i;
} else if ( space[j].ecurved == 1 && space[i+1].scurved != 1 ) {
space[j].end = space[i+1].start;
--i;
} else {
middle = (space[j].end + space[i+1].start)/2;
space[j].end = space[i+1].start = middle;
--i;
}
}
++i;
}
}
return( j );
}
static int MergeSegmentsFinal( struct segment *space, int cnt ) {
int i,j;
for ( i=j=0; i<cnt; ++i, ++j ) {
if ( i!=j )
space[j] = space[i];
while ( i+1<cnt && space[i+1].start<=space[j].end ) {
if ( space[i+1].end>space[j].end ) {
space[j].end = space[i+1].end;
space[j].ebase = space[i+1].ebase;
space[j].ecurved = space[i+1].ecurved;
space[j].curved = false;
}
++i;
}
}
return( j );
}
static void FigureStemActive( struct glyphdata *gd, struct stemdata *stem ) {
int i, j, pcnt=0;
struct pointdata *pd, **pspace = gd->pspace;
struct stem_chunk *chunk;
struct segment *lspace = gd->lspace, *rspace = gd->rspace;
struct segment *bothspace = gd->bothspace, *activespace = gd->activespace;
int lcnt, rcnt, bcnt, bpos, acnt, cove, startset, endset;
double middle, width, len, clen, gap, lseg, rseg;
double err, lmin, rmax, loff, roff, last, s, e, sbase, ebase;
double proj, proj2, proj3, orig_proj, ptemp;
width = stem->width;
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL )
gd->points[i].sp->ticked = false;
lcnt = rcnt = 0;
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( chunk->stemcheat )
continue;
lcnt = AddLineSegment( stem,lspace,lcnt,true ,chunk->l,chunk->lnext,gd );
rcnt = AddLineSegment( stem,rspace,rcnt,false,chunk->r,chunk->rnext,gd );
}
bcnt = 0;
if ( lcnt!=0 && rcnt!=0 ) {
/* For curved segments we can extend left and right active segments */
/* a bit to ensure that they do overlap and thus can be marked with an */
/* active zone */
if ( rcnt == lcnt && stem->chunk_cnt == lcnt ) {
for ( i=0; i<lcnt; i++ ) {
/* If it's a feature bend, then our tests should be more liberal */
cove = (( rspace[i].curved + lspace[i].curved ) == 3 );
gap = 0;
if ( lspace[i].start>rspace[i].end && lspace[i].scurved && rspace[i].ecurved )
gap = lspace[i].start-rspace[i].end;
else if ( rspace[i].start>lspace[i].end && rspace[i].scurved && lspace[i].ecurved )
gap = rspace[i].start-lspace[i].end;
else if ( !cove )
continue;
lseg = lspace[i].end - lspace[i].start;
rseg = rspace[i].end - rspace[i].start;
if (( cove && gap < (lseg > rseg ? lseg : rseg )) ||
( gap < ( lseg + rseg )/2 && !stem->chunks[i].stub )) {
if ( lspace[i].ebase<rspace[i].start )
rspace[i].start = lspace[i].ebase;
else if ( lspace[i].sbase>rspace[i].end )
rspace[i].end = lspace[i].sbase;
if ( rspace[i].ebase<lspace[i].start )
lspace[i].start = rspace[i].ebase;
else if ( rspace[i].sbase>lspace[i].end )
lspace[i].end = rspace[i].sbase;
}
}
}
qsort(lspace,lcnt,sizeof(struct segment),segment_cmp);
qsort(rspace,rcnt,sizeof(struct segment),segment_cmp);
lcnt = MergeSegments( lspace,lcnt );
rcnt = MergeSegments( rspace,rcnt );
for ( i=j=bcnt=0; i<lcnt && j<rcnt; ++i ) {
while ( j<rcnt && rspace[j].end<=lspace[i].start )
++j;
while ( j<rcnt && rspace[j].start<=lspace[i].end ) {
cove = (( rspace[j].curved + lspace[i].curved ) == 3 );
s = ( rspace[j].start > lspace[i].start ) ?
rspace[j].start : lspace[i].start;
e = ( rspace[j].end < lspace[i].end ) ?
rspace[j].end : lspace[i].end;
sbase = ( rspace[j].start > lspace[i].start ) ?
lspace[i].sbase : rspace[j].sbase;
ebase = ( rspace[j].end < lspace[i].end ) ?
lspace[i].ebase : rspace[j].ebase;
middle = ( lspace[i].start + rspace[j].start )/2;
bothspace[bcnt].start = ( cove && middle < s ) ? middle : s;
if ( rspace[j].start > lspace[i].start )
bothspace[bcnt].scurved = ( rspace[j].scurved || sbase < s ) ?
rspace[j].scurved : lspace[i].scurved;
else
bothspace[bcnt].scurved = ( lspace[i].scurved || sbase < s ) ?
lspace[i].scurved : rspace[j].scurved;
middle = ( lspace[i].end + rspace[j].end )/2;
bothspace[bcnt].end = ( cove && middle > e ) ? middle : e;
if ( rspace[j].end < lspace[i].end )
bothspace[bcnt].ecurved = ( rspace[j].ecurved || ebase > e ) ?
rspace[j].ecurved : lspace[i].ecurved;
else
bothspace[bcnt].ecurved = ( lspace[i].ecurved || ebase > e ) ?
lspace[i].ecurved : rspace[j].ecurved;
sbase = ( rspace[j].sbase > lspace[i].sbase ) ?
rspace[j].sbase : lspace[i].sbase;
ebase = ( rspace[j].ebase < lspace[i].ebase ) ?
rspace[j].ebase : lspace[i].ebase;
if ( sbase > bothspace[bcnt].end )
sbase = ebase = bothspace[bcnt].end;
else if ( ebase < bothspace[bcnt].start )
sbase = ebase = bothspace[bcnt].start;
else if ( ebase < sbase )
ebase = sbase = ( ebase + sbase )/2;
bothspace[bcnt].sbase = sbase;
bothspace[bcnt].ebase = ebase;
bothspace[bcnt++].curved = rspace[j].curved || lspace[i].curved;
if ( rspace[j].end>lspace[i].end )
break;
++j;
}
}
}
#if GLYPH_DATA_DEBUG
fprintf( stderr, "Active zones for stem l=%.2f,%.2f r=%.2f,%.2f dir=%.2f,%.2f:\n",
stem->left.x,stem->left.y,stem->right.x,stem->right.y,stem->unit.x,stem->unit.y );
for ( i=0; i<lcnt; i++ ) {
fprintf( stderr, "\tleft space curved=%d\n",lspace[i].curved );
fprintf( stderr, "\t\tstart=%.2f,base=%.2f,curved=%d\n",
lspace[i].start,lspace[i].sbase,lspace[i].scurved );
fprintf( stderr, "\t\tend=%.2f,base=%.2f,curved=%d\n",
lspace[i].end,lspace[i].ebase,lspace[i].ecurved );
}
for ( i=0; i<rcnt; i++ ) {
fprintf( stderr, "\tright space curved=%d\n",rspace[i].curved );
fprintf( stderr, "\t\tstart=%.2f,base=%.2f,curved=%d\n",
rspace[i].start,rspace[i].sbase,rspace[i].scurved );
fprintf( stderr, "\t\tend=%.2f,base=%.2f,curved=%d\n",
rspace[i].end,rspace[i].ebase,rspace[i].ecurved );
}
for ( i=0; i<bcnt; i++ ) {
fprintf( stderr, "\tboth space\n" );
fprintf( stderr, "\t\tstart=%.2f,base=%.2f,curved=%d\n",
bothspace[i].start,bothspace[i].sbase,bothspace[i].scurved );
fprintf( stderr, "\t\tend=%.2f,base=%.2f,curved=%d\n",
bothspace[i].end,bothspace[i].ebase,bothspace[i].ecurved );
}
fprintf( stderr,"\n" );
#endif
err = ( stem->unit.x == 0 || stem->unit.y == 0 ) ?
dist_error_hv : dist_error_diag;
lmin = ( stem->lmin < -err ) ? stem->lmin : -err;
rmax = ( stem->rmax > err ) ? stem->rmax : err;
acnt = 0;
if ( bcnt!=0 ) {
for ( i=0; i<gd->pcnt; ++i ) if ( (pd = &gd->points[i])->sp!=NULL ) {
/* Let's say we have a stem. And then inside that stem we have */
/* another rectangle. So our first stem isn't really a stem any */
/* more (because we hit another edge first), yet it's still reasonable*/
/* to align the original stem */
/* Now suppose the rectangle is rotated a bit so we can't make */
/* a stem from it. What do we do here? */
loff = ( pd->sp->me.x - stem->left.x ) * stem->unit.y -
( pd->sp->me.y - stem->left.y ) * stem->unit.x;
roff = ( pd->sp->me.x - stem->right.x ) * stem->unit.y -
( pd->sp->me.y - stem->right.y ) * stem->unit.x;
if ( loff >= lmin && roff <= rmax ) {
pd->projection = (pd->sp->me.x - stem->left.x)*stem->unit.x +
(pd->sp->me.y - stem->left.y)*stem->unit.y;
if ( InActive(pd->projection,bothspace,bcnt) )
pspace[pcnt++] = pd;
}
}
qsort(pspace,pcnt,sizeof(struct pointdata *),proj_cmp);
bpos = i = 0;
while ( bpos<bcnt ) {
if ( bothspace[bpos].curved || pcnt==0 ) {
activespace[acnt++] = bothspace[bpos++];
} else {
last = bothspace[bpos].start;
startset = false; endset = false;
if ( bothspace[bpos].scurved ||
StemIsActiveAt( gd,stem,bothspace[bpos].start+0.0015 )) {
activespace[acnt].scurved = bothspace[bpos].scurved;
activespace[acnt].start = bothspace[bpos].start;
startset = true;
}
/* If the stem is preceded by a curved segment, then skip */
/* the first point position and start from the next one. */
/* (Otherwise StemIsActiveAt() may consider the stem is */
/* "inactive" at the fragment between the start of the active */
/* space and the first point actually belonging to this stem) */
if ( bothspace[bpos].scurved ) {
while ( pcnt>i && pspace[i]->projection < bothspace[bpos].sbase ) i++;
if ( pcnt > i && pspace[i]->projection >= bothspace[bpos].sbase ) {
last = activespace[acnt].end = pspace[i]->projection;
activespace[acnt].ecurved = false;
activespace[acnt].curved = false;
endset=true;
}
}
while ( i<pcnt && (
( !bothspace[bpos].ecurved && pspace[i]->projection<bothspace[bpos].end ) ||
( bothspace[bpos].ecurved && pspace[i]->projection<=bothspace[bpos].ebase ))) {
if ( last==activespace[acnt].start && pspace[i]->projection >= last ) {
if ( !StemIsActiveAt( gd,stem,last+(( 1.001*pspace[i]->projection-last )/2.001 ))) {
last = activespace[acnt].start = pspace[i]->projection;
activespace[acnt].scurved = false;
startset = true; endset = false;
} else {
last = activespace[acnt].end = pspace[i]->projection;
activespace[acnt].ecurved = false;
activespace[acnt].curved = false;
endset = true;
}
} else if (( last==activespace[acnt].end || !startset )
&& pspace[i]->projection >= last) {
if ( !StemIsActiveAt( gd,stem,last+(( 1.001*pspace[i]->projection-last )/2.001 )) ||
!startset ) {
if ( startset ) acnt++;
last = activespace[acnt].start = pspace[i]->projection;
activespace[acnt].scurved = false;
startset = true; endset = false;
} else {
last = activespace[acnt].end = pspace[i]->projection;
activespace[acnt].ecurved = false;
activespace[acnt].curved = false;
endset = true;
}
}
++i;
}
if (( bothspace[bpos].ecurved ||
StemIsActiveAt( gd,stem,bothspace[bpos].end-0.0015 )) &&
startset ) {
activespace[acnt].end = bothspace[bpos].end;
activespace[acnt].ecurved = bothspace[bpos].ecurved;
activespace[acnt].curved = bothspace[bpos].curved;
endset = true;
}
++bpos;
if ( endset ) ++acnt;
}
}
}
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
/* stemcheat 1 -- diagonal edge stem;
* 2 -- diagonal corner stem with a sharp top;
* 3 -- diagonal corner stem with a flat top;
* 4 -- bounding box hint */
if ( chunk->stemcheat==3 && chunk->l!=NULL && chunk->r!=NULL &&
i+1<stem->chunk_cnt &&
stem->chunks[i+1].stemcheat==3 &&
( chunk->l==stem->chunks[i+1].l ||
chunk->r==stem->chunks[i+1].r )) {
SplinePoint *sp = chunk->l==stem->chunks[i+1].l ?
chunk->l->sp : chunk->r->sp;
proj = (sp->me.x - stem->left.x) *stem->unit.x +
(sp->me.y - stem->left.y) *stem->unit.y;
SplinePoint *sp2 = chunk->l==stem->chunks[i+1].l ?
chunk->r->sp : chunk->l->sp;
SplinePoint *sp3 = chunk->l==stem->chunks[i+1].l ?
stem->chunks[i+1].r->sp : stem->chunks[i+1].l->sp;
proj2 = (sp2->me.x - stem->left.x) *stem->unit.x +
(sp2->me.y - stem->left.y) *stem->unit.y;
proj3 = (sp3->me.x - stem->left.x) *stem->unit.x +
(sp3->me.y - stem->left.y) *stem->unit.y;
if ( proj2>proj3 ) {
ptemp = proj2; proj2 = proj3; proj3 = ptemp;
}
if ( (proj3-proj2) < width ) {
activespace[acnt ].curved = true;
proj2 -= width/2;
proj3 += width/2;
} else {
activespace[acnt ].curved = false;
}
activespace[acnt].start = proj2;
activespace[acnt].end = proj3;
activespace[acnt].sbase = activespace[acnt].ebase = proj;
acnt++;
++i;
} else if ( chunk->stemcheat && chunk->l!=NULL && chunk->r!=NULL ) {
SplinePoint *sp = chunk->l->sp;
proj = ( sp->me.x - stem->left.x ) * stem->unit.x +
( sp->me.y - stem->left.y ) * stem->unit.y;
orig_proj = proj;
SplinePoint *other = chunk->lnext ? sp->next->to : sp->prev->from;
len = (other->me.x - sp->me.x) * stem->unit.x +
(other->me.y - sp->me.y) * stem->unit.y;
if ( chunk->stemcheat == 2 )
proj -= width/2;
else if ( len<0 )
proj -= width;
activespace[acnt].curved = true;
activespace[acnt].start = proj;
activespace[acnt].end = proj+width;
activespace[acnt].sbase = activespace[acnt].ebase = orig_proj;
acnt++;
}
}
if ( acnt!=0 ) {
stem->activecnt = MergeSegmentsFinal( activespace,acnt );
stem->active = malloc(acnt*sizeof(struct segment));
memcpy(stem->active,activespace,acnt*sizeof(struct segment));
}
len = clen = 0;
for ( i=0; i<acnt; ++i ) {
if ( stem->active[i].curved )
clen += stem->active[i].end-stem->active[i].start;
else
len += stem->active[i].end-stem->active[i].start;
}
stem->len = len; stem->clen = len+clen;
}
static void GDStemsFixupIntersects(struct glyphdata *gd) {
int i, j, stemidx;
struct stemdata *stem;
struct stem_chunk *chunk;
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
for ( j=0; j<stem->chunk_cnt; ++j ) {
chunk = &stem->chunks[j];
if ( chunk->l!=NULL ) {
stemidx = IsStemAssignedToPoint( chunk->l,stem,true );
FixupT( chunk->l,stemidx,true,chunk->l_e_idx );
stemidx = IsStemAssignedToPoint( chunk->l,stem,false );
FixupT( chunk->l,stemidx,false,chunk->l_e_idx );
}
if ( chunk->r!=NULL ) {
stemidx = IsStemAssignedToPoint( chunk->r,stem,true );
FixupT( chunk->r,stemidx,true,chunk->r_e_idx );
stemidx = IsStemAssignedToPoint( chunk->r,stem,false );
FixupT( chunk->r,stemidx,false,chunk->r_e_idx );
}
}
}
}
static int StemsWouldConflict( struct stemdata *stem1,struct stemdata *stem2 ) {
double loff, roff, s1, s2, e1, e2;
int acnt1, acnt2;
if ( stem1 == stem2 || !UnitsParallel( &stem1->unit,&stem2->unit,true ))
return( false );
loff = ( stem2->left.x - stem1->left.x ) * stem1->unit.y -
( stem2->left.y - stem1->left.y ) * stem1->unit.x;
roff = ( stem2->right.x - stem1->right.x ) * stem1->unit.y -
( stem2->right.y - stem1->right.y ) * stem1->unit.x;
loff = fabs( loff ); roff = fabs( roff );
if ( loff > stem1->width || roff > stem1->width )
return( false );
acnt1 = stem1->activecnt;
acnt2 = stem2->activecnt;
if ( acnt1 == 0 || acnt2 == 0 )
return( false );
s1 = stem1->active[0].start; e1 = stem1->active[acnt1-1].end;
s2 = stem2->active[0].start; e2 = stem2->active[acnt2-1].end;
loff = ( stem2->left.x - stem1->left.x ) * stem1->unit.x +
( stem2->left.y - stem1->left.y ) * stem1->unit.y;
if (( s2+loff >= s1 && s2+loff <= e1 ) || ( e2+loff >= s1 && e2+loff <= e1 ) ||
( s2+loff <= s1 && e2+loff >= e1 ) || ( e2+loff <= s1 && s2+loff >= e1 ))
return( true );
return( false );
}
/* Convert diagonal stems generated for stubs and intersections to horizontal */
/* or vertical, if they have just one chunk. This should be done before calculating */
/* active zones, as they are calculated against each stem's unit vector */
static void GDNormalizeStubs( struct glyphdata *gd ) {
int i, j, hv;
struct stemdata *stem;
struct stem_chunk *chunk;
BasePoint newdir;
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->positioned )
continue;
if ( !IsUnitHV( &stem->unit,true )) {
hv = IsUnitHV( &stem->unit,false );
if ( hv && StemFitsHV( stem,( hv == 1 ),3 )) {
if ( hv == 2 && stem->unit.y < 0 )
SwapEdges( gd,stem );
newdir.x = fabs( rint( stem->unit.x ));
newdir.y = fabs( rint( stem->unit.y ));
SetStemUnit( stem,newdir );
for ( j=0; j<stem->chunk_cnt && stem->leftidx == -1 && stem->rightidx == -1; j++ ) {
chunk = &stem->chunks[j];
if ( stem->leftidx == -1 && chunk->l != NULL )
stem->leftidx = GetValidPointDataIndex( gd,chunk->l->sp,stem );
if ( stem->rightidx == -1 && chunk->r != NULL )
stem->rightidx = GetValidPointDataIndex( gd,chunk->r->sp,stem );
}
}
}
}
}
static void GDFindUnlikelyStems( struct glyphdata *gd ) {
double width, minl, ratio;
int i, j, k, stem_cnt, ls_cnt, rs_cnt, ltick, rtick;
struct pointdata *lpd, *rpd;
Spline *ls, *rs;
SplinePoint *lsp, *rsp;
BasePoint *lunit, *runit, *slunit, *srunit, *sunit;
struct stemdata *stem, *stem1, *tstem;
struct stemdata **tstems, **lstems, **rstems;
struct stem_chunk *chunk;
GDStemsFixupIntersects( gd );
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
/* If stem had been already present in the spline char before we */
/* started generating glyph data, then it should never be */
/* considered "too big" */
if ( stem->positioned )
continue;
/* If a stem has straight edges, and it is wider than tall */
/* then it is unlikely to be a real stem */
width = stem->width;
ratio = IsUnitHV( &stem->unit,true ) ? gd->emsize/( 6 * width ) : -0.25;
stem->toobig = ( stem->clen + stem->clen * ratio < width );
}
/* One more check for curved stems. If a stem has just one active */
/* segment, this segment is curved and the stem has no conflicts, */
/* then select the active segment length which allows us to consider */
/* this stem suitable for PS output by such a way, that stems connecting */
/* the opposite sides of a circle are always accepted */
for ( i=0; i<gd->stemcnt; ++i ) if ( gd->stems[i].toobig ) {
stem = &gd->stems[i];
width = stem->width;
if ( IsUnitHV( &stem->unit,true ) && stem->activecnt == 1 &&
stem->active[0].curved && width/2 > dist_error_curve ) {
for ( j=0; j<gd->stemcnt; ++j) {
stem1 = &gd->stems[j];
if ( !stem1->toobig && StemsWouldConflict( stem,stem1 ))
break;
}
if ( j == gd->stemcnt ) {
minl = sqrt( pow( width/2,2 ) - pow( width/2 - dist_error_curve,2 ));
if ( stem->clen >= minl ) stem->toobig = false;
}
}
}
/* And finally a check for stubs and feature terminations. We don't */
/* want such things to be controlled by any special hints, if there */
/* is already a hint controlling the middle of the same feature */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->positioned )
continue;
if ( stem->chunk_cnt == 1 && stem->chunks[0].stub & 3 ) {
chunk = &stem->chunks[0];
slunit = chunk->lnext ? &chunk->l->nextunit : &chunk->l->prevunit;
srunit = chunk->rnext ? &chunk->r->nextunit : &chunk->r->prevunit;
/* This test is valid only for features which are not exactly horizontal/ */
/* vertical. But we can't check this using the stem unit, as it may have */
/* already beeen reset to HV. So we use the units of this stem's base points */
/* instead. */
if ( IsUnitHV( slunit,true ) && IsUnitHV( srunit,true ))
continue;
if ( UnitCloserToHV( srunit,slunit ) > 0 ) sunit = srunit;
else sunit = slunit;
lpd = chunk->l; lsp = lpd->sp; lstems = tstems = NULL;
ls_cnt = 0;
do {
stem_cnt = (( chunk->lnext && lpd == chunk->l ) ||
( !chunk->lnext && lpd != chunk->l )) ? lpd->nextcnt : lpd->prevcnt;
for ( j=0; j<stem_cnt; j++ ) {
tstems= (( chunk->lnext && lpd == chunk->l ) ||
( !chunk->lnext && lpd != chunk->l )) ? lpd->nextstems : lpd->prevstems;
tstem = tstems[j];
if ( tstem != stem ) {
lstems = tstems;
ls_cnt = stem_cnt;
break;
}
}
if( lstems != NULL )
break;
ls = ( chunk->lnext ) ? lsp->next : lsp->prev;
if ( ls == NULL )
break;
lsp = ( chunk->lnext ) ? ls->to : ls->from;
lpd = &gd->points[lsp->ptindex];
lunit = ( chunk->lnext ) ? &lpd->prevunit : &lpd->nextunit;
} while ( lpd != chunk->l && lpd != chunk->r &&
UnitsParallel( lunit,sunit,false ));
rpd = chunk->r; rsp = rpd->sp; rstems = tstems = NULL;
rs_cnt = 0;
do {
stem_cnt = (( chunk->rnext && rpd == chunk->r ) ||
( !chunk->rnext && rpd != chunk->r )) ? rpd->nextcnt : rpd->prevcnt;
for ( j=0; j<stem_cnt; j++ ) {
tstems= (( chunk->rnext && rpd == chunk->r ) ||
( !chunk->rnext && rpd != chunk->r )) ? rpd->nextstems : rpd->prevstems;
tstem = tstems[j];
if ( tstem != stem ) {
rstems = tstems;
rs_cnt = stem_cnt;
break;
}
}
if( rstems != NULL )
break;
rs = ( chunk->rnext ) ? rsp->next : rsp->prev;
if ( rs == NULL )
break;
rsp = ( chunk->rnext ) ? rs->to : rs->from;
rpd = &gd->points[rsp->ptindex];
runit = ( chunk->rnext ) ? &rpd->prevunit : &rpd->nextunit;
} while ( rpd != chunk->r && rpd != chunk->l &&
UnitsParallel( runit,sunit,false ));
if ( lstems != NULL && rstems !=NULL ) {
for ( j=0; j<ls_cnt && !stem->toobig; j++ ) {
for ( k=0; k<rs_cnt && !stem->toobig; k++ ) {
if ( lstems[j] == rstems[k] && IsUnitHV( &lstems[j]->unit,true )) {
stem->toobig = true;
}
}
}
}
}
/* One more check for intersections between a curved segment and a */
/* straight feature. Imagine a curve intersected by two bars, like in a Euro */
/* glyph. Very probably we will get two chunks, one controlling the uppest */
/* two points of intersection, and another the lowest two, and most probably */
/* these two chunks will get merged into a single stem (so this stem will */
/* even get an exactly vertical vector). Yet we don't need this stem because */
/* there is already a stem controlling the middle of the curve (between two */
/* bars).*/
else if ( stem->chunk_cnt == 2 &&
(( stem->chunks[0].stub & 7 && stem->chunks[1].stub & 6 ) ||
( stem->chunks[0].stub & 6 && stem->chunks[1].stub & 7 ))) {
for ( j=0; j<gd->stemcnt; ++j) {
stem1 = &gd->stems[j];
if ( !stem1->toobig && StemsWouldConflict( stem,stem1 ))
break;
}
if ( j < gd->stemcnt )
stem->toobig = true;
}
}
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( IsUnitHV( &stem->unit,true ))
continue;
/* If a diagonal stem doesn't have at least 2 points assigned to */
/* each edge, then we probably can't instruct it. However we don't */
/* disable stems which have just one point on each side, if those */
/* points are inflection points, as such stems may be useful for */
/* metafont routines */
if ( stem->lpcnt < 2 || stem->rpcnt < 2 ) {
lpd = rpd = NULL;
for ( j=0; j<stem->chunk_cnt && lpd == NULL && rpd == NULL; j++ ) {
chunk = &stem->chunks[j];
if ( chunk->l != NULL ) lpd = chunk->l;
if ( chunk->r != NULL ) rpd = chunk->r;
}
if (lpd == NULL || rpd == NULL ||
!IsInflectionPoint( gd,lpd ) || !IsInflectionPoint( gd,rpd ) || stem->clen < stem->width )
stem->toobig = 2;
} else if ( stem->activecnt >= stem->chunk_cnt )
stem->toobig = 2;
}
/* When using preexisting stem data, occasionally we can get two slightly */
/* different stems (one predefined, another recently detected) with nearly */
/* parallel vectors, sharing some points at both sides. Attempting to instruct */
/* them both would lead to very odd effects. So we must disable one */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( !stem->positioned || IsUnitHV( &stem->unit,true ))
continue;
for ( j=0; j<gd->stemcnt; ++j ) {
tstem = &gd->stems[j];
if ( tstem == stem || tstem->toobig || !UnitsParallel( &stem->unit,&tstem->unit,false ))
continue;
ltick = false; rtick = false;
for ( k=0; k<stem->chunk_cnt && ( !ltick || !rtick ); k++ ) {
chunk = &stem->chunks[k];
if ( chunk->l != NULL &&
IsStemAssignedToPoint( chunk->l,stem ,chunk->lnext ) != -1 &&
IsStemAssignedToPoint( chunk->l,tstem,chunk->lnext ) != -1 )
ltick = true;
if ( chunk->r != NULL &&
IsStemAssignedToPoint( chunk->r,stem ,chunk->rnext ) != -1 &&
IsStemAssignedToPoint( chunk->r,tstem,chunk->rnext ) != -1 )
rtick = true;
}
if ( ltick && rtick ) tstem->toobig = 2;
}
}
}
static int StemPointOnDiag( struct glyphdata *gd,struct stemdata *stem,
struct pointdata *pd ) {
struct stemdata *tstem;
int i, is_next, stemcnt;
if ( gd->only_hv || pd->colinear )
return( false );
is_next = IsStemAssignedToPoint( pd,stem,false ) != -1;
stemcnt = ( is_next ) ? pd->nextcnt : pd->prevcnt;
for ( i=0; i<stemcnt; i++ ) {
tstem = ( is_next ) ? pd->nextstems[i] : pd->prevstems[i];
if ( !IsUnitHV( &tstem->unit,true ) &&
tstem->lpcnt >= 2 && tstem->rpcnt >=2 )
return( true );
}
return( false );
}
static void FindRefPointsExisting( struct glyphdata *gd,struct stemdata *stem ) {
int i;
int pos, lbase, rbase, is_x;
struct stem_chunk *chunk;
struct pointdata *pd;
is_x = (int) rint( stem->unit.y );
lbase = ((real *) &stem->left.x)[!is_x];
rbase = ((real *) &stem->right.x)[!is_x];
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( chunk->ltick ) {
pd = chunk->l;
pos = ((real *) &pd->sp->me.x)[!is_x];
if ( pos == lbase ) {
pd->value++;
if ( pd->sp->ptindex < gd->realcnt )
pd->value++;
if ( StemPointOnDiag( gd,stem,pd ))
pd->value++;
}
}
if ( chunk->rtick ) {
pd = chunk->r;
pos = ((real *) &pd->sp->me.x)[!is_x];
if ( pos == rbase ) {
pd->value++;
if ( pd->sp->ptindex < gd->realcnt )
pd->value++;
if ( StemPointOnDiag( gd,stem,pd ))
pd->value++;
}
}
}
}
static void FindRefPointsNew( struct glyphdata *gd,struct stemdata *stem ) {
int i, j;
int pos, lpos, rpos, testpos, is_x;
int lval, rval;
struct stem_chunk *chunk;
struct pointdata *lmost1, *lmost2, *rmost1, *rmost2;
double llen, prevllen, rlen, prevrlen;
SplinePoint *sp, *tsp;
uint8 *lextr, *rextr;
is_x = (int) rint( stem->unit.y );
lpos = ((real *) &stem->left.x)[!is_x];
rpos = ((real *) &stem->right.x)[!is_x];
lmost1 = rmost1 = lmost2 = rmost2 = NULL;
llen = prevllen = rlen = prevrlen = 0;
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( chunk->ltick ) {
sp = chunk->l->sp;
pos = ((real *) &sp->me.x)[!is_x];
lval = 0;
for ( j=0; j<i; j++ ) if ( stem->chunks[j].ltick ) {
tsp = stem->chunks[j].l->sp;
testpos = ((real *) &tsp->me.x)[!is_x];
if ( pos == testpos ) {
lval = stem->chunks[j].l->value;
stem->chunks[j].l->value++;
/* An additional bonus for points which form together */
/* a longer stem segment */
if ( sp->next->to == tsp || sp->prev->from == tsp ) {
llen = fabs(( sp->me.x - tsp->me.x )*stem->unit.x +
( sp->me.y - tsp->me.y )*stem->unit.y );
if ( llen > prevllen ) {
lmost1 = stem->chunks[j].l;
lmost2 = chunk->l;
prevllen = llen;
}
}
}
}
chunk->l->value = lval+1;
if ( lval == 0 &&
( stem->lmin - ( pos - lpos ) > -dist_error_hv ) &&
( stem->lmax - ( pos - lpos ) < dist_error_hv ))
chunk->l->value++;
}
if ( chunk->rtick ) {
sp = chunk->r->sp;
pos = ((real *) &sp->me.x)[!is_x];
rval = 0;
for ( j=0; j<i; j++ ) if ( stem->chunks[j].rtick ) {
tsp = stem->chunks[j].r->sp;
testpos = ((real *) &tsp->me.x)[!is_x];
if ( pos == testpos ) {
rval = stem->chunks[j].r->value;
stem->chunks[j].r->value++;
if ( sp->next->to == tsp || sp->prev->from == tsp ) {
rlen = fabs(( sp->me.x - tsp->me.x )*stem->unit.x +
( sp->me.y - tsp->me.y )*stem->unit.y );
if ( rlen > prevrlen ) {
rmost1 = stem->chunks[j].r;
rmost2 = chunk->r;
prevrlen = rlen;
}
}
}
}
chunk->r->value = rval+1;
if ( rval == 0 &&
( stem->rmin - ( pos - rpos ) > -dist_error_hv ) &&
( stem->rmax - ( pos - rpos ) < dist_error_hv ))
chunk->r->value++;
}
}
if ( lmost1 != NULL && lmost2 != NULL ) {
lmost1->value++; lmost2->value++;
}
if ( rmost1 != NULL && rmost2 != NULL ) {
rmost1->value++; rmost2->value++;
}
/* Extrema points get an additional value bonus. This should */
/* prevent us from preferring wrong points for stems controlling */
/* curved segments */
/* Third pass to assign bonuses to extrema points (especially */
/* to those extrema which are opposed to another extremum point) */
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( chunk->ltick ) {
lextr = ( is_x ) ? &chunk->l->x_extr : &chunk->l->y_extr;
if ( *lextr ) chunk->l->value++;
}
if ( chunk->rtick ) {
rextr = ( is_x ) ? &chunk->r->x_extr : &chunk->r->y_extr;
if ( *rextr ) chunk->r->value++;
}
if ( chunk->ltick && chunk->rtick ) {
lextr = ( is_x ) ? &chunk->l->x_extr : &chunk->l->y_extr;
rextr = ( is_x ) ? &chunk->r->x_extr : &chunk->r->y_extr;
if ( *lextr && *rextr ) {
chunk->l->value++;
chunk->r->value++;
}
}
}
}
static void NormalizeStem( struct glyphdata *gd,struct stemdata *stem ) {
int i;
int is_x, lval, rval, val, lset, rset, best;
double loff=0, roff=0;
BasePoint lold, rold;
SplinePoint *lbest, *rbest;
struct stem_chunk *chunk;
/* First sort the stem chunks by their coordinates */
if ( IsUnitHV( &stem->unit,true )) {
qsort( stem->chunks,stem->chunk_cnt,sizeof( struct stem_chunk ),chunk_cmp );
is_x = (int) rint( stem->unit.y );
/* For HV stems we have to check all chunks once more in order */
/* to figure out "left" and "right" positions most typical */
/* for this stem. We perform this by assigning a value to */
/* left and right side of this chunk. */
/* First pass to determine some point properties necessary */
/* for subsequent operations */
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( chunk->ltick )
/* reset the point's "value" to zero */
chunk->l->value = 0;
if ( chunk->rtick )
chunk->r->value = 0;
}
/* Second pass to check which positions relative to stem edges are */
/* most common for this stem. Each position which repeats */
/* more than once gets a plus 1 value bonus */
if ( stem->positioned ) FindRefPointsExisting( gd,stem );
else FindRefPointsNew( gd,stem );
best = -1; val = 0;
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
lval = ( chunk->l != NULL ) ? chunk->l->value : 0;
rval = ( chunk->r != NULL ) ? chunk->r->value : 0;
if ((( chunk->l != NULL && chunk->l->value > 0 &&
GetValidPointDataIndex( gd,chunk->l->sp,stem ) != -1 ) ||
( stem->ghost && stem->width == 21 )) &&
(( chunk->r != NULL && chunk->r->value > 0 &&
GetValidPointDataIndex( gd,chunk->r->sp,stem ) != -1 ) ||
( stem->ghost && stem->width == 20 )) && lval + rval > val ) {
best = i;
val = lval + rval;
}
}
if ( best > -1 ) {
if ( !stem->ghost || stem->width == 20 ) {
lold = stem->left;
lbest = stem->chunks[best].l->sp;
stem->left = lbest->me;
stem->leftidx = GetValidPointDataIndex( gd,lbest,stem );
/* Now assign "left" and "right" properties of the stem */
/* to point coordinates taken from the most "typical" chunk */
/* of this stem. We also have to recalculate stem width and */
/* left/right offset values */
loff = ( stem->left.x - lold.x ) * stem->unit.y -
( stem->left.y - lold.y ) * stem->unit.x;
stem->lmin -= loff; stem->lmax -= loff;
}
if ( !stem->ghost || stem->width == 21 ) {
rold = stem->right;
rbest = stem->chunks[best].r->sp;
stem->right = rbest->me;
stem->rightidx = GetValidPointDataIndex( gd,rbest,stem );
roff = ( stem->right.x - rold.x ) * stem->unit.y -
( stem->right.y - rold.y ) * stem->unit.x;
stem->rmin -= roff; stem->rmax -= roff;
}
if ( !stem->ghost )
stem->width = ( stem->right.x - stem->left.x ) * stem->unit.y -
( stem->right.y - stem->left.y ) * stem->unit.x;
} else {
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( chunk->l != NULL && ( !stem->ghost || stem->width == 20 )) {
stem->leftidx = GetValidPointDataIndex( gd,chunk->l->sp,stem );
}
if ( chunk->r != NULL && ( !stem->ghost || stem->width == 21 )) {
stem->rightidx = GetValidPointDataIndex( gd,chunk->r->sp,stem );
}
}
}
} else {
qsort( stem->chunks,stem->chunk_cnt,sizeof( struct stem_chunk ),chunk_cmp );
lset = false; rset = false;
/* Search for a pair of points whose vectors are really parallel. */
/* This check is necessary because a diagonal stem can start from */
/* a feature termination, and our checks for such terminations */
/* are more "liberal" than in other cases. However we don't want */
/* considering such a pair of points basic for this stem */
for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
BasePoint *lu, *ru;
if ( chunk->l != NULL && chunk->r != NULL ) {
lu = chunk->lnext ? &chunk->l->nextunit : &chunk->l->prevunit;
ru = chunk->rnext ? &chunk->r->nextunit : &chunk->r->prevunit;
if ( UnitsParallel( lu,ru,true )) {
loff = ( chunk->l->sp->me.x - stem->left.x )*stem->l_to_r.x +
( chunk->l->sp->me.y - stem->left.y )*stem->l_to_r.y;
roff = ( chunk->r->sp->me.x - stem->right.x )*stem->l_to_r.x +
( chunk->r->sp->me.y - stem->right.y )*stem->l_to_r.y;
stem->left = chunk->l->sp->me;
stem->right = chunk->r->sp->me;
RecalcStemOffsets( stem,&stem->unit,loff != 0,roff != 0 );
break;
}
}
}
/* If the above check fails, just select the first point (relatively) */
/* to the stem direction both at the left and the right edge */
if ( i == stem->chunk_cnt ) for ( i=0; i<stem->chunk_cnt; ++i ) {
chunk = &stem->chunks[i];
if ( !lset && chunk->l != NULL ) {
loff = ( chunk->l->sp->me.x - stem->left.x )*stem->l_to_r.x +
( chunk->l->sp->me.y - stem->left.y )*stem->l_to_r.y;
stem->left = chunk->l->sp->me;
lset = true;
}
if ( !rset && chunk->r != NULL ) {
roff = ( chunk->r->sp->me.x - stem->right.x )*stem->l_to_r.x +
( chunk->r->sp->me.y - stem->right.y )*stem->l_to_r.y;
stem->right = chunk->r->sp->me;
rset = true;
}
if ( lset && rset ) {
RecalcStemOffsets( stem,&stem->unit,loff != 0,roff != 0 );
break;
}
}
}
}
static void AssignPointsToBBoxHint( struct glyphdata *gd,DBounds *bounds,
struct stemdata *stem,int is_v ) {
double min, max, test, left, right;
double dist, prevdist;
int i, j;
int lcnt=0, rcnt=0, closest;
BasePoint dir;
SplinePoint **lpoints, **rpoints;
struct pointdata *pd, *pd1, *pd2;
lpoints = calloc( gd->pcnt,sizeof( SplinePoint *));
rpoints = calloc( gd->pcnt,sizeof( SplinePoint *));
dir.x = !is_v; dir.y = is_v;
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
pd = &gd->points[i];
min = ( is_v ) ? bounds->minx : bounds->miny;
max = ( is_v ) ? bounds->maxx : bounds->maxy;
test = ( is_v ) ? pd->base.x : pd->base.y;
if ( test >= min && test < min + dist_error_hv && (
IsCorrectSide( gd,pd,true,is_v,&dir ) || IsCorrectSide( gd,pd,false,is_v,&dir )))
lpoints[lcnt++] = pd->sp;
else if ( test > max - dist_error_hv && test <= max && (
IsCorrectSide( gd,pd,true,!is_v,&dir ) || IsCorrectSide( gd,pd,false,!is_v,&dir )))
rpoints[rcnt++] = pd->sp;
}
if ( lcnt > 0 && rcnt > 0 ) {
if ( stem == NULL ) {
stem = NewStem( gd,&dir,&lpoints[0]->me,&rpoints[0]->me );
stem->bbox = true;
stem->len = stem->width;
stem->leftidx = GetValidPointDataIndex( gd,lpoints[0],stem );
stem->rightidx = GetValidPointDataIndex( gd,rpoints[0],stem );
}
for ( i=0; i<lcnt; ++i ) {
closest = -1;
dist = 1e4; prevdist = 1e4;
for ( j=0; j<rcnt; ++j ) {
left = ( is_v ) ? lpoints[i]->me.y : lpoints[i]->me.x;
right = ( is_v ) ? rpoints[j]->me.y : rpoints[j]->me.x;
dist = fabs( left - right );
if ( dist < prevdist ) {
closest = j;
prevdist = dist;
}
}
pd1 = &gd->points[lpoints[i]->ptindex];
pd2 = &gd->points[rpoints[closest]->ptindex];
AddToStem( gd,stem,pd1,pd2,false,true,4 );
}
qsort( stem->chunks,stem->chunk_cnt,sizeof( struct stem_chunk ),chunk_cmp );
}
free( lpoints );
free( rpoints );
}
static void CheckForBoundingBoxHints( struct glyphdata *gd ) {
/* Adobe seems to add hints at the bounding boxes of glyphs with no hints */
int i, hv;
int hcnt=0, vcnt=0;
double cw, ch;
struct stemdata *stem, *hstem=NULL,*vstem=NULL;
DBounds bounds;
SplineCharFindBounds( gd->sc,&bounds );
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
hv = IsUnitHV( &stem->unit,true );
if ( !hv )
continue;
if ( stem->toobig ) {
if ( stem->left.x == bounds.minx && stem->right.x == bounds.maxx )
vstem = stem;
else if ( stem->right.y == bounds.miny && stem->left.y == bounds.maxy )
hstem = stem;
continue;
}
if ( hv == 1 ) {
if ( stem->bbox ) hstem = stem;
else ++hcnt;
} else if ( hv == 2 ) {
if ( stem->bbox ) vstem = stem;
else ++vcnt;
}
}
if ( hcnt!=0 && vcnt!=0 &&
( hstem == NULL || !hstem->positioned ) &&
( vstem == NULL || !vstem->positioned ))
return;
ch = bounds.maxy - bounds.miny;
cw = bounds.maxx - bounds.minx;
if ( ch > 0 && (( hstem != NULL && hstem->positioned ) ||
( hcnt == 0 && ch < gd->emsize/3 ))) {
if ( hstem != NULL && hstem->toobig ) hstem->toobig = false;
AssignPointsToBBoxHint( gd,&bounds,hstem,false );
if ( hstem != NULL ) NormalizeStem( gd,hstem );
}
if ( cw > 0 && (( vstem != NULL && vstem->positioned ) ||
( vcnt == 0 && cw < gd->emsize/3 ))) {
if ( vstem != NULL && vstem->toobig ) vstem->toobig = false;
AssignPointsToBBoxHint( gd,&bounds,vstem,true );
if ( vstem != NULL ) NormalizeStem( gd,vstem );
}
}
static struct stemdata *FindOrMakeGhostStem( struct glyphdata *gd,
SplinePoint *sp,int blue,double width ) {
int i, j, hasl, hasr;
struct stemdata *stem=NULL, *tstem;
struct stem_chunk *chunk;
BasePoint dir,left,right;
double min, max;
dir.x = 1; dir.y = 0;
for ( i=0; i<gd->stemcnt; ++i ) {
tstem = &gd->stems[i];
if ( tstem->blue == blue && tstem->ghost && tstem->width == width ) {
stem = tstem;
break;
/* If the stem controlling this blue zone is not for a ghost hint, */
/* then we check if it has both left and right points, to ensure that */
/* we don't occasionally assign an additional point to a stem which */
/* has already been rejected in favor of another stem */
} else if ( tstem->blue == blue && !tstem->ghost && !tstem->toobig ) {
min = ( width == 20 ) ? tstem->left.y - tstem->lmin - 2*dist_error_hv :
tstem->right.y - tstem->rmin - 2*dist_error_hv;
max = ( width == 20 ) ? tstem->left.y - tstem->lmax + 2*dist_error_hv :
tstem->right.y - tstem->rmax + 2*dist_error_hv;
if ( sp->me.y <= min || sp->me.y >= max )
continue;
hasl = false; hasr = false; j = 0;
while ( j < tstem->chunk_cnt && ( !hasl || !hasr )) {
chunk = &tstem->chunks[j];
if ( chunk->l != NULL && !chunk->lpotential )
hasl = true;
if ( chunk->r != NULL && !chunk->rpotential )
hasr = true;
j++;
}
if ( hasl && hasr ) {
stem = tstem;
break;
}
}
}
if ( stem == NULL ) {
left.x = right.x = sp->me.x;
left.y = ( width == 21 ) ? sp->me.y + 21 : sp->me.y;
right.y = ( width == 21 ) ? sp->me.y : sp->me.y - 20;
stem = NewStem( gd,&dir,&left,&right );
stem->ghost = true;
stem->width = width;
stem->blue = blue;
}
return( stem );
}
static int AddGhostSegment( struct pointdata *pd,int cnt,double base,struct segment *space ) {
double s, e, temp, pos, spos, epos;
SplinePoint *sp, *nsp, *nsp2, *psp, *psp2;
sp = nsp = psp = pd->sp;
pos = pd->sp->me.y;
/* First check if there are points on the same line lying further */
/* in the desired direction */
if (( sp->next != NULL ) && ( sp->next->to->me.y == pos ))
nsp = sp->next->to;
if (( sp->prev != NULL ) && ( sp->prev->from->me.y == pos ))
psp = sp->prev->from;
if ( psp != sp ) {
s = psp->me.x;
} else if ( psp->noprevcp ) {
psp2 = psp->prev->from;
if ( psp2->me.y != psp->me.y ) {
s = ( psp->me.x - psp2->me.x )/( psp->me.y - psp2->me.y )*20.0;
if ( s < 0 ) s = -s;
if ( psp2->me.x<psp->me.x )
s = ( psp->me.x-psp2->me.x < s ) ? psp2->me.x : psp->me.x-s;
else
s = ( psp2->me.x-psp->me.x < s ) ? psp2->me.x : psp->me.x+s;
} else
s = psp->me.x;
} else {
s = ( pd->sp->me.x + psp->prevcp.x )/2;
}
if ( nsp != sp ) {
e = nsp->me.x;
} else if ( nsp->nonextcp ) {
nsp2 = nsp->next->to;
if ( nsp2->me.y != nsp->me.y ) {
e = ( nsp->me.x - nsp2->me.x )/( nsp->me.y - nsp2->me.y )*20.0;
if ( e < 0 ) e = -e;
if ( nsp2->me.x<nsp->me.x )
e = ( nsp->me.x-nsp2->me.x < e ) ? nsp2->me.x : nsp->me.x-e;
else
e = ( nsp2->me.x-nsp->me.x < e ) ? nsp2->me.x : nsp->me.x+e;
} else
e = nsp->me.x;
} else {
e = ( pd->sp->me.x + nsp->nextcp.x )/2;
}
spos = psp->me.x; epos = nsp->me.x;
if ( s>e ) {
temp = s; s = e; e = temp;
temp = spos; spos = epos; epos = temp;
}
space[cnt].start = s - base;
space[cnt].end = e - base;
space[cnt].sbase = spos - base;
space[cnt].ebase = epos - base;
space[cnt].ecurved = space[cnt].scurved = space[cnt].curved = ( false );
return( cnt+1 );
}
static void FigureGhostActive( struct glyphdata *gd,struct stemdata *stem ) {
int acnt, i;
real len = 0;
struct segment *activespace = gd->activespace;
struct pointdata *valid;
if ( !stem->ghost )
return;
acnt = 0;
for ( i=0; i<stem->chunk_cnt; ++i ) {
valid = ( stem->chunks[i].l != NULL) ?
stem->chunks[i].l : stem->chunks[i].r;
acnt = AddGhostSegment( valid,acnt,stem->left.x,activespace );
}
qsort(activespace,acnt,sizeof(struct segment),segment_cmp);
acnt = MergeSegments( activespace,acnt );
stem->activecnt = acnt;
if ( acnt!=0 ) {
stem->active = malloc(acnt*sizeof(struct segment));
memcpy( stem->active,activespace,acnt*sizeof( struct segment ));
}
for ( i=0; i<acnt; ++i ) {
len += stem->active[i].end-stem->active[i].start;
}
stem->clen = stem->len = len;
}
static void CheckForGhostHints( struct glyphdata *gd ) {
/* PostScript doesn't allow a hint to stretch from one alignment zone to */
/* another. (Alignment zones are the things in bluevalues). */
/* Oops, I got this wrong. PS doesn't allow a hint to start in a bottom */
/* zone and stretch to a top zone. Everything in OtherBlues is a bottom */
/* zone. The baseline entry in BlueValues is also a bottom zone. Every- */
/* thing else in BlueValues is a top-zone. */
/* This means */
/* that we can't define a horizontal stem hint which stretches from */
/* the baseline to the top of a capital I, or the x-height of lower i */
/* If we find any such hints we must remove them, and replace them with */
/* ghost hints. The bottom hint has height -21, and the top -20 */
BlueData *bd = &gd->bd;
struct stemdata *stem;
struct stem_chunk *chunk;
struct pointdata *pd;
real base;
int i, j, leftfound, rightfound, has_h, peak, fuzz;
fuzz = gd->fuzz;
/* look for any stems stretching from one zone to another and remove them */
/* (I used to turn them into ghost hints here, but that didn't work (for */
/* example on "E" where we don't need any ghosts from the big stem because*/
/* the narrow stems provide the hints that PS needs */
/* However, there are counter-examples. in Garamond-Pro the "T" character */
/* has a horizontal stem at the top which stretches between two adjacent */
/* bluezones. Removing it is wrong. Um... Thanks Adobe */
/* I misunderstood. Both of these were top-zones */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( IsUnitHV( &stem->unit,true ) != 1)
continue;
leftfound = rightfound = -1;
for ( j=0; j<bd->bluecnt; ++j ) {
if ( stem->left.y>=bd->blues[j][0]-fuzz && stem->left.y<=bd->blues[j][1]+fuzz )
leftfound = j;
else if ( stem->right.y>=bd->blues[j][0]-fuzz && stem->right.y<=bd->blues[j][1]+fuzz )
rightfound = j;
}
/* Assign value 2 to indicate this stem should be ignored also for TTF instrs */
if ( leftfound !=-1 && rightfound !=-1 &&
( stem->left.y > 0 && stem->right.y <= 0 ))
stem->toobig = 2;
/* Otherwise mark the stem as controlling a specific blue zone */
else if ( leftfound != -1 && ( rightfound == -1 || stem->left.y > 0 ))
stem->blue = leftfound;
else if ( rightfound != -1 && ( leftfound == -1 || stem->right.y <= 0 ))
stem->blue = rightfound;
}
/* Now look and see if we can find any edges which lie in */
/* these zones. Edges which are not currently in hints */
/* Use the winding number to determine top or bottom */
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
has_h = false;
for ( j=0; j<gd->points[i].prevcnt; j++ ) {
stem = gd->points[i].prevstems[j];
if ( !stem->toobig && IsUnitHV( &stem->unit,true ) == 1 ) {
has_h = true;
break;
}
}
for ( j=0; j<gd->points[i].nextcnt; j++ ) {
stem = gd->points[i].nextstems[j];
if ( !stem->toobig && IsUnitHV( &stem->unit,true ) == 1 ) {
has_h = true;
break;
}
}
if ( has_h )
continue;
pd = &gd->points[i];
base = pd->sp->me.y;
for ( j=0; j<bd->bluecnt; ++j ) {
if ( base>=bd->blues[j][0]-fuzz && base<=bd->blues[j][1]+fuzz ) {
peak = IsSplinePeak( gd,pd,false,false,7 );
if ( peak > 0 ) {
stem = FindOrMakeGhostStem( gd,pd->sp,j,20 );
chunk = AddToStem( gd,stem,pd,NULL,2,false,false );
} else if ( peak < 0 ) {
stem = FindOrMakeGhostStem( gd,pd->sp,j,21 );
chunk = AddToStem( gd,stem,NULL,pd,2,false,false );
}
}
}
}
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( !stem->ghost )
continue;
NormalizeStem( gd,stem );
FigureGhostActive( gd,stem );
}
}
static void MarkDStemCorner( struct glyphdata *gd,struct pointdata *pd ) {
int x_dir = pd->x_corner;
int hv, is_l, i, peak, has_stem = false;
struct stemdata *stem;
BasePoint left,right,unit;
for ( i=0; i<pd->prevcnt && !has_stem; i++ ) {
stem = pd->prevstems[i];
hv = IsUnitHV( &stem->unit,true );
if ( !stem->toobig && (
( x_dir && hv == 1 ) ||
( !x_dir && hv == 2 )))
has_stem = true;
}
for ( i=0; i<pd->nextcnt && !has_stem; i++ ) {
stem = pd->nextstems[i];
hv = IsUnitHV( &stem->unit,true );
if ( !stem->toobig && (
( x_dir && hv == 1 ) ||
( !x_dir && hv == 2 )))
has_stem = true;
}
if ( has_stem )
return;
peak = IsSplinePeak( gd,pd,x_dir,x_dir,2 );
unit.x = !x_dir; unit.y = x_dir;
if ( peak > 0 ) {
left.x = x_dir ? pd->sp->me.x + 21 : pd->sp->me.x;
right.x = x_dir ? pd->sp->me.x : pd->sp->me.x;
left.y = x_dir ? pd->sp->me.y : pd->sp->me.y;
right.y = x_dir ? pd->sp->me.y : pd->sp->me.y - 20;
} else if ( peak < 0 ) {
left.x = x_dir ? pd->sp->me.x : pd->sp->me.x;
right.x = x_dir ? pd->sp->me.x - 20 : pd->sp->me.x;
left.y = x_dir ? pd->sp->me.y : pd->sp->me.y + 21;
right.y = x_dir ? pd->sp->me.y : pd->sp->me.y;
}
is_l = IsCorrectSide( gd,pd,true,true,&unit );
for ( i=0; i<gd->stemcnt; i++ ) {
stem = &gd->stems[i];
if (!stem->toobig && UnitsParallel( &unit,&stem->unit,true ) &&
OnStem( stem,&pd->sp->me,is_l ))
break;
}
if ( i == gd->stemcnt ) {
stem = NewStem( gd,&unit,&left,&right );
stem->ghost = 2;
}
AddToStem( gd,stem,pd,NULL,2,false,false );
}
static void MarkDStemCorners( struct glyphdata *gd ) {
struct stemdata *stem;
struct stem_chunk *schunk, *echunk;
int i;
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->toobig || IsUnitHV( &stem->unit,true ))
continue;
schunk = &stem->chunks[0];
echunk = &stem->chunks[stem->chunk_cnt - 1];
if ( schunk->l != NULL && schunk->r != NULL &&
fabs( schunk->l->base.x - schunk->r->base.x ) > dist_error_hv &&
fabs( schunk->l->base.y - schunk->r->base.y ) > dist_error_hv && (
( schunk->l->x_corner == 1 && schunk->r->y_corner == 1 ) ||
( schunk->l->y_corner == 1 && schunk->r->x_corner == 1 ))) {
MarkDStemCorner( gd,schunk->l );
MarkDStemCorner( gd,schunk->r );
}
if ( echunk->l != NULL && echunk->r != NULL &&
fabs( echunk->l->base.x - echunk->r->base.x ) > dist_error_hv &&
fabs( echunk->l->base.y - echunk->r->base.y ) > dist_error_hv && (
( echunk->l->x_corner == 1 && echunk->r->y_corner == 1 ) ||
( echunk->l->y_corner == 1 && echunk->r->x_corner == 1 ))) {
MarkDStemCorner( gd,echunk->l );
MarkDStemCorner( gd,echunk->r );
}
}
}
#if GLYPH_DATA_DEBUG
static void DumpGlyphData( struct glyphdata *gd ) {
int i, j;
struct stemdata *stem;
struct linedata *line;
struct stem_chunk *chunk;
if ( gd->linecnt > 0 )
fprintf( stderr, "\nDumping line data for %s\n",gd->sc->name );
for ( i=0; i<gd->linecnt; ++i ) {
line = &gd->lines[i];
fprintf( stderr, "line vector=%.4f,%.4f base=%.2f,%.2f length=%.4f\n",
line->unit.x,line->unit.y,line->online.x,line->online.y,line->length );
for( j=0; j<line->pcnt;++j ) {
fprintf( stderr, "\tpoint num=%d, x=%.2f, y=%.2f, prev=%d, next=%d\n",
line->points[j]->sp->ttfindex, line->points[j]->sp->me.x,
line->points[j]->sp->me.y,
line->points[j]->prevline==line, line->points[j]->nextline==line );
}
fprintf( stderr, "\n" );
}
if ( gd->stemcnt > 0 )
fprintf( stderr, "\nDumping stem data for %s\n",gd->sc->name );
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
fprintf( stderr, "stem l=%.2f,%.2f idx=%d r=%.2f,%.2f idx=%d vector=%.4f,%.4f\n\twidth=%.2f chunk_cnt=%d len=%.4f clen=%.4f ghost=%d blue=%d toobig=%d\n\tlmin=%.2f,lmax=%.2f,rmin=%.2f,rmax=%.2f,lpcnt=%d,rpcnt=%d\n",
stem->left.x,stem->left.y,stem->leftidx,
stem->right.x,stem->right.y,stem->rightidx,
stem->unit.x,stem->unit.y,stem->width,
stem->chunk_cnt,stem->len,stem->clen,stem->ghost,stem->blue,stem->toobig,
stem->lmin,stem->lmax,stem->rmin,stem->rmax,stem->lpcnt,stem->rpcnt );
for ( j=0; j<stem->chunk_cnt; ++j ) {
chunk = &stem->chunks[j];
if ( chunk->l!=NULL && chunk->r!=NULL )
fprintf (stderr, "\tchunk l=%.2f,%.2f potential=%d r=%.2f,%.2f potential=%d stub=%d\n",
chunk->l->sp->me.x, chunk->l->sp->me.y, chunk->lpotential,
chunk->r->sp->me.x, chunk->r->sp->me.y, chunk->rpotential, chunk->stub );
else if ( chunk->l!=NULL )
fprintf (stderr, "\tchunk l=%.2f,%.2f potential=%d\n",
chunk->l->sp->me.x, chunk->l->sp->me.y, chunk->lpotential);
else if ( chunk->r!=NULL )
fprintf (stderr, "\tchunk r=%.2f,%.2f potential=%d\n",
chunk->r->sp->me.x, chunk->r->sp->me.y, chunk->rpotential);
}
fprintf( stderr, "\n" );
}
if ( gd->hbundle != NULL || gd->vbundle != NULL )
fprintf( stderr, "\nDumping HV stem bundles for %s\n",gd->sc->name );
if ( gd->hbundle != NULL ) for ( i=0; i<gd->hbundle->cnt; i++ ) {
stem = gd->hbundle->stemlist[i];
fprintf( stderr, "H stem l=%.2f,%.2f r=%.2f,%.2f slave=%d\n",
stem->left.x,stem->left.y,stem->right.x,stem->right.y,stem->master!=NULL );
if ( stem->dep_cnt > 0 ) for ( j=0; j<stem->dep_cnt; j++ ) {
fprintf( stderr, "\tslave l=%.2f,%.2f r=%.2f,%.2f mode=%c left=%d\n",
stem->dependent[j].stem->left.x,stem->dependent[j].stem->left.y,
stem->dependent[j].stem->right.x,stem->dependent[j].stem->right.y,
stem->dependent[j].dep_type,stem->dependent[j].lbase );
}
if ( stem->serif_cnt > 0 ) for ( j=0; j<stem->serif_cnt; j++ ) {
fprintf( stderr, "\tserif l=%.2f,%.2f r=%.2f,%.2f ball=%d left=%d\n",
stem->serifs[j].stem->left.x,stem->serifs[j].stem->left.y,
stem->serifs[j].stem->right.x,stem->serifs[j].stem->right.y,
stem->serifs[j].is_ball,stem->serifs[j].lbase );
}
}
fprintf( stderr, "\n" );
if ( gd->vbundle != NULL ) for ( i=0; i<gd->vbundle->cnt; i++ ) {
stem = gd->vbundle->stemlist[i];
fprintf( stderr, "V stem l=%.2f,%.2f r=%.2f,%.2f slave=%d\n",
stem->left.x,stem->left.y,stem->right.x,stem->right.y,stem->master!=NULL );
if ( stem->dep_cnt > 0 ) for ( j=0; j<stem->dep_cnt; j++ ) {
fprintf( stderr, "\tslave l=%.2f,%.2f r=%.2f,%.2f mode=%c left=%d\n",
stem->dependent[j].stem->left.x,stem->dependent[j].stem->left.y,
stem->dependent[j].stem->right.x,stem->dependent[j].stem->right.y,
stem->dependent[j].dep_type,stem->dependent[j].lbase );
}
if ( stem->serif_cnt > 0 ) for ( j=0; j<stem->serif_cnt; j++ ) {
fprintf( stderr, "\tserif l=%.2f,%.2f r=%.2f,%.2f ball=%d left=%d\n",
stem->serifs[j].stem->left.x,stem->serifs[j].stem->left.y,
stem->serifs[j].stem->right.x,stem->serifs[j].stem->right.y,
stem->serifs[j].is_ball,stem->serifs[j].lbase );
}
if ( stem->prev_c_m != NULL ) {
fprintf( stderr,"\tprev counter master: l=%.2f r=%.2f\n",
stem->prev_c_m->left.x,stem->prev_c_m->right.x );
}
if ( stem->next_c_m != NULL ) {
fprintf( stderr,"\tnext counter master: l=%.2f r=%.2f\n",
stem->next_c_m->left.x,stem->next_c_m->right.x );
}
}
fprintf( stderr, "\n" );
if ( gd->ibundle != NULL ) for ( i=0; i<gd->ibundle->cnt; i++ ) {
stem = gd->ibundle->stemlist[i];
fprintf( stderr, "I stem l=%.2f,%.2f r=%.2f,%.2f slave=%d\n",
stem->left.x,stem->left.y,stem->right.x,stem->right.y,stem->master!=NULL );
if ( stem->dep_cnt > 0 ) for ( j=0; j<stem->dep_cnt; j++ ) {
fprintf( stderr, "\tslave l=%.2f,%.2f r=%.2f,%.2f mode=%c left=%d\n",
stem->dependent[j].stem->left.x,stem->dependent[j].stem->left.y,
stem->dependent[j].stem->right.x,stem->dependent[j].stem->right.y,
stem->dependent[j].dep_type,stem->dependent[j].lbase );
}
if ( stem->serif_cnt > 0 ) for ( j=0; j<stem->serif_cnt; j++ ) {
fprintf( stderr, "\tserif l=%.2f,%.2f r=%.2f,%.2f ball=%d left=%d\n",
stem->serifs[j].stem->left.x,stem->serifs[j].stem->left.y,
stem->serifs[j].stem->right.x,stem->serifs[j].stem->right.y,
stem->serifs[j].is_ball,stem->serifs[j].lbase );
}
}
fprintf( stderr, "\n" );
}
#endif
static void AssignPointsToStems( struct glyphdata *gd,int startnum,DBounds *bounds ) {
int i;
struct pointdata *pd;
struct stemdata *stem = NULL;
BasePoint dir;
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
pd = &gd->points[i];
if ( pd->prev_e_cnt > 0 )
BuildStem( gd,pd,false,true,true,0 );
else
HalfStemNoOpposite( gd,pd,stem,&pd->prevunit,false );
if ( pd->next_e_cnt > 0 )
BuildStem( gd,pd,true,true,true,0 );
else
HalfStemNoOpposite( gd,pd,stem,&pd->nextunit,true );
if ( pd->x_corner ) {
if ( pd->bothedge!=NULL )
stem = DiagonalCornerStem( gd,pd,true );
dir.x = 0; dir.y = 1;
HalfStemNoOpposite( gd,pd,stem,&dir,2 );
} else if ( pd->y_corner ) {
if ( pd->bothedge!=NULL )
stem = DiagonalCornerStem( gd,pd,true );
dir.x = 1; dir.y = 0;
HalfStemNoOpposite( gd,pd,stem,&dir,2 );
}
}
gd->lspace = malloc(gd->pcnt*sizeof(struct segment));
gd->rspace = malloc(gd->pcnt*sizeof(struct segment));
gd->bothspace = malloc(3*gd->pcnt*sizeof(struct segment));
gd->activespace = malloc(3*gd->pcnt*sizeof(struct segment));
#if GLYPH_DATA_DEBUG
fprintf( stderr,"Going to calculate stem active zones for %s\n",gd->sc->name );
#endif
for ( i=startnum; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
NormalizeStem( gd,stem );
if ( gd->stems[i].ghost )
FigureGhostActive( gd,stem );
else if ( gd->stems[i].bbox )
AssignPointsToBBoxHint( gd,bounds,stem,( stem->unit.y == 1 ));
else
FigureStemActive( gd,&gd->stems[i] );
}
#if GLYPH_DATA_DEBUG
DumpGlyphData( gd );
#endif
free(gd->lspace); gd->lspace = NULL;
free(gd->rspace); gd->rspace = NULL;
free(gd->bothspace); gd->bothspace = NULL;
free(gd->activespace); gd->activespace = NULL;
}
static void _DStemInfoToStemData( struct glyphdata *gd,DStemInfo *dsi,int *startcnt ) {
struct stemdata *stem;
if ( gd->stems == NULL ) {
gd->stems = calloc( 2*gd->pcnt,sizeof( struct stemdata ));
gd->stemcnt = 0;
}
*startcnt = gd->stemcnt;
while ( dsi != NULL ) {
stem = NewStem( gd,&dsi->unit,&dsi->left,&dsi->right );
stem->positioned = true;
dsi = dsi->next;
}
}
struct glyphdata *DStemInfoToStemData( struct glyphdata *gd,DStemInfo *dsi ) {
int startcnt;
if ( dsi == NULL )
return( gd );
_DStemInfoToStemData( gd,dsi,&startcnt );
AssignPointsToStems( gd,startcnt,NULL );
return( gd );
}
static void _StemInfoToStemData( struct glyphdata *gd,StemInfo *si,DBounds *bounds,int is_v,int *startcnt ) {
struct stemdata *stem;
BasePoint dir,left,right;
dir.x = !is_v; dir.y = is_v;
if ( gd->stems == NULL ) {
gd->stems = calloc( 2*gd->pcnt,sizeof( struct stemdata ));
gd->stemcnt = 0;
}
*startcnt = gd->stemcnt;
while ( si != NULL ) {
left.x = ( is_v ) ? si->start : 0;
left.y = ( is_v ) ? 0 : si->start + si->width;
right.x = ( is_v ) ? si->start + si->width : 0;
right.y = ( is_v ) ? 0 : si->start;
stem = NewStem( gd,&dir,&left,&right );
stem->ghost = si->ghost;
if (( is_v &&
left.x >= bounds->minx && left.x < bounds->minx + dist_error_hv &&
right.x > bounds->maxx - dist_error_hv && right.x <= bounds->maxx ) ||
( !is_v &&
right.y >= bounds->miny && right.y < bounds->miny + dist_error_hv &&
left.y > bounds->maxy - dist_error_hv && left.y <= bounds->maxy ))
stem->bbox = true;
stem->positioned = true;
si = si->next;
}
}
struct glyphdata *StemInfoToStemData( struct glyphdata *gd,StemInfo *si,int is_v ) {
DBounds bounds;
int startcnt;
if ( si == NULL )
return( gd );
SplineCharFindBounds( gd->sc,&bounds );
_StemInfoToStemData( gd,si,&bounds,is_v,&startcnt );
AssignPointsToStems( gd,startcnt,&bounds );
return( gd );
}
static int ValidConflictingStem( struct stemdata *stem1,struct stemdata *stem2 ) {
int x_dir = fabs( stem1->unit.y ) > fabs( stem1->unit.x );
double s1, e1, s2, e2, temp;
s1 = (&stem1->left.x)[!x_dir] -
((&stem1->left.x)[x_dir] * (&stem1->unit.x)[!x_dir] )/(&stem1->unit.x)[x_dir];
e1 = (&stem1->right.x)[!x_dir] -
((&stem1->right.x)[x_dir] * (&stem1->unit.x)[!x_dir] )/(&stem1->unit.x)[x_dir];
s2 = (&stem2->left.x)[!x_dir] -
((&stem2->left.x)[x_dir] * (&stem2->unit.x)[!x_dir] )/(&stem2->unit.x)[x_dir];
e2 = (&stem2->right.x)[!x_dir] -
((&stem2->right.x)[x_dir] * (&stem2->unit.x)[!x_dir] )/(&stem2->unit.x)[x_dir];
if ( s1 > e1 ) {
temp = s1; s1 = e1; e1 = temp;
}
if ( s2 > e2 ) {
temp = s2; s2 = e2; e2 = temp;
}
/* If stems don't overlap, then there is no conflict here */
if ( s2 >= e1 || s1 >= e2 )
return( false );
/* Stems which have no points assigned cannot be valid masters for */
/* other stems (however there is a notable exception for ghost hints) */
if (( stem1->lpcnt > 0 || stem1->rpcnt > 0 ) &&
stem2->lpcnt == 0 && stem2->rpcnt == 0 && !stem2->ghost )
return( false );
/* Bounding box stems are always preferred */
if ( stem1->bbox && !stem2->bbox )
return( false );
/* Stems associated with blue zones always preferred to any other stems */
if ( stem1->blue >=0 && stem2->blue < 0 )
return( false );
/* Don't attempt to handle together stems, linked to different zones */
if ( stem1->blue >=0 && stem2->blue >= 0 && stem1->blue != stem2->blue )
return( false );
/* If both stems are associated with a blue zone, but one of them is for */
/* a ghost hint, then that stem is preferred */
if ( stem1->ghost && !stem2->ghost )
return( false );
return( true );
}
static int HasDependentStem( struct stemdata *master,struct stemdata *slave ) {
int i;
struct stemdata *tstem;
if ( slave->master != NULL && master->dep_cnt > 0 ) {
for ( i=0; i<master->dep_cnt; i++ ) {
tstem = master->dependent[i].stem;
if ( tstem == slave || HasDependentStem( tstem,slave ))
return( true );
}
}
return( false );
}
static int PreferEndDep( struct stemdata *stem,
struct stemdata *smaster,struct stemdata *emaster,char s_type,char e_type ) {
int hv = IsUnitHV( &stem->unit,true );
double sdist, edist;
if ( !hv )
return( false );
if (( s_type == 'a' && e_type != 'a' ) || ( s_type == 'm' && e_type == 'i' ))
return( false );
else if (( e_type == 'a' && s_type != 'a' ) || ( e_type == 'm' && s_type == 'i' ))
return( true );
if ( s_type == 'm' && s_type == e_type ) {
sdist = ( hv==1 ) ?
fabs( smaster->right.y - stem->right.y ) :
fabs( smaster->left.x - stem->left.x );
edist = ( hv==1 ) ?
fabs( emaster->left.y - stem->left.y ) :
fabs( emaster->right.x - stem->right.x );
return( edist < sdist );
} else
return( emaster->clen > smaster->clen );
}
static void LookForMasterHVStem( struct stemdata *stem,BlueData *bd ) {
struct stemdata *tstem, *smaster=NULL, *emaster=NULL;
struct stembundle *bundle = stem->bundle;
double start, end, tstart, tend;
double ssdist, sedist, esdist, eedist;
double smin, smax, emin, emax, tsmin, tsmax, temin, temax;
int is_x, i, link_to_s, stype, etype, allow_s, allow_e;
is_x = ( bundle->unit.x == 1 );
if ( is_x ) {
start = stem->right.y; end = stem->left.y;
smin = start - stem->rmin - 2*dist_error_hv;
smax = start - stem->rmax + 2*dist_error_hv;
emin = end - stem->lmin - 2*dist_error_hv;
emax = end - stem->lmax + 2* dist_error_hv;
} else {
start = stem->left.x; end = stem->right.x;
smin = start + stem->lmax - 2*dist_error_hv;
smax = start + stem->lmin + 2*dist_error_hv;
emin = end + stem->rmax - 2*dist_error_hv;
emax = end + stem->rmin + 2*dist_error_hv;
}
start = ( is_x ) ? stem->right.y : stem->left.x;
end = ( is_x ) ? stem->left.y : stem->right.x;
stype = etype = '\0';
for ( i=0; i<bundle->cnt; i++ ) {
tstem = bundle->stemlist[i];
if ( is_x ) {
tstart = tstem->right.y; tend = tstem->left.y;
tsmin = tstart - tstem->rmin - 2*dist_error_hv;
tsmax = tstart - tstem->rmax + 2*dist_error_hv;
temin = tend - tstem->lmin - 2*dist_error_hv;
temax = tend - tstem->lmax + 2* dist_error_hv;
} else {
tstart = tstem->left.x; tend = tstem->right.x;
tsmin = tstart + tstem->lmax - 2*dist_error_hv;
tsmax = tstart + tstem->lmin + 2*dist_error_hv;
temin = tend + tstem->rmax - 2*dist_error_hv;
temax = tend + tstem->rmin + 2*dist_error_hv;
}
tstart = ( is_x ) ? tstem->right.y : tstem->left.x;
tend = ( is_x ) ? tstem->left.y : tstem->right.x;
/* In this loop we are looking if the given stem has conflicts with */
/* other stems and if anyone of those conflicting stems should */
/* take precedence over it */
if ( stem == tstem || tend < start || tstart > end ||
!ValidConflictingStem( stem,tstem ) || HasDependentStem( stem,tstem ))
continue;
/* Usually in case of conflicts we prefer the stem with longer active */
/* zones. However a stem linked to a blue zone is always preferred to */
/* a stem which is not, and ghost hints are preferred to any other */
/* stems */
if ( stem->clen > tstem->clen && ValidConflictingStem( tstem,stem ))
continue;
stem->confl_cnt++;
/* If the master stem is for a ghost hint or both the stems are */
/* linked to the same blue zone, then we can link only to the edge */
/* which fall into the blue zone */
allow_s = ( !tstem->ghost || tstem->width == 21 ) &&
( stem->blue == -1 || stem->blue != tstem->blue || bd->blues[stem->blue][0] < 0 );
allow_e = ( !tstem->ghost || tstem->width == 20 ) &&
( stem->blue == -1 || stem->blue != tstem->blue || bd->blues[stem->blue][0] > 0 );
/* Assume there are two stems which have (almost) coincident left edges. */
/* The hinting technique for this case is to merge all points found on */
/* those coincident edges together, position them, and then link to the */
/* opposite edges. */
/* However we don't allow merging if both stems can be snapped to a blue */
/* zone, unless their edges are _exactly_ coincident, as shifting features */
/* relatively to each other instead of snapping them to the same zone would */
/* obviously be wrong */
if ( allow_s && tstart > smin && tstart < smax && start > tsmin && start < tsmax &&
( stem->blue == -1 || RealNear( tstart,start ))) {
if ( smaster == NULL || stype != 'a' || smaster->clen < tstem->clen ) {
smaster = tstem;
stype = 'a';
}
/* The same case for right edges */
} else if ( allow_e && tend > emin && tend < emax && end > temin && end < temax &&
( stem->blue == -1 || RealNear( tend,end ))) {
if ( emaster == NULL || etype != 'a' || emaster->clen < tstem->clen ) {
emaster = tstem;
etype = 'a';
}
/* Nested stems. I first planned to handle them by positioning the */
/* narrower stem first, and then linking its edges to the opposed edges */
/* of the nesting stem. But this works well only in those cases where */
/* maintaining the dependent stem width is not important. So now the */
/* situations where a narrower or a wider stem can be preferred */
/* (because it has longer active zones) are equally possible. In the */
/* first case I link to the master stem just one edge of the secondary */
/* stem, just like with overlapping stems */
} else if ( tstart > start && tend < end ) {
if ( allow_s && ( smaster == NULL || stype == 'i' ||
( stype == 'm' && smaster->clen < tstem->clen ))) {
smaster = tstem;
stype = 'm';
}
if ( allow_e && ( emaster == NULL || etype == 'i' ||
( etype == 'm' && emaster->clen < tstem->clen ))) {
emaster = tstem;
etype = 'm';
}
/* However if we have to prefer the nesting stem, we do as with */
/* overlapping stems which require interpolations, i. e. interpolate */
/* one edge and link to another */
} else if ( tstart < start && tend > end ) {
link_to_s = ( allow_s && ( start - tstart < tend - end ));
if ( link_to_s && ( smaster == NULL ||
( stype == 'i' && smaster->clen < tstem->clen ))) {
smaster = tstem;
stype = 'i';
} else if ( !link_to_s && ( emaster == NULL ||
( etype == 'i' && emaster->clen < tstem->clen ))) {
emaster = tstem;
etype = 'i';
}
/* Overlapping stems. Here we first check all 4 distances between */
/* 4 stem edges. If the closest distance is between left or right */
/* edges, then the normal technique (in TrueType) is linking them */
/* with MDRP without maintaining a minimum distance. Otherwise */
/* we interpolate an edge of the "slave" stem between already */
/* positioned edges of the "master" stem, and then gridfit it */
} else if (( tstart < start && start < tend && tend < end ) ||
( start < tstart && tstart < end && end < tend )) {
ssdist = fabs( start - tstart );
sedist = fabs( start - tend );
esdist = fabs( end - tstart );
eedist = fabs( end - tend );
if ( allow_s && ( !allow_e ||
( stem->width < tstem->width/3 && ssdist < eedist ) ||
( ssdist <= eedist && ssdist <= sedist && ssdist <= esdist )) &&
( smaster == NULL || ( stype == 'i' ||
( stype == 'm' && smaster->clen < tstem->clen )))) {
smaster = tstem;
stype = 'm';
} else if ( allow_e && ( !allow_s ||
( stem->width < tstem->width/3 && eedist < ssdist ) ||
( eedist <= ssdist && eedist <= sedist && eedist <= esdist )) &&
( emaster == NULL || ( etype == 'i' ||
( etype == 'm' && emaster->clen < tstem->clen )))) {
emaster = tstem;
etype = 'm';
} else if ( allow_s && allow_e && ( smaster == NULL ||
( stype == 'i' && smaster->clen < tstem->clen )) &&
sedist <= esdist && sedist <= ssdist && sedist <= eedist ) {
smaster = tstem;
stype = 'i';
} else if ( allow_s && allow_e && ( emaster == NULL ||
( etype == 'i' && emaster->clen < tstem->clen )) &&
esdist <= sedist && esdist <= ssdist && esdist <= eedist ) {
emaster = tstem;
etype = 'i';
}
}
}
if ( smaster != NULL && emaster != NULL ) {
if ( PreferEndDep( stem,smaster,emaster,stype,etype ))
smaster = NULL;
else
emaster = NULL;
}
if ( smaster != NULL ) {
stem->master = smaster;
if ( smaster->dependent == NULL )
smaster->dependent = calloc( bundle->cnt*2,sizeof( struct dependent_stem ));
smaster->dependent[smaster->dep_cnt].stem = stem;
smaster->dependent[smaster->dep_cnt].dep_type = stype;
smaster->dependent[smaster->dep_cnt++].lbase = !is_x;
} else if ( emaster != NULL ) {
stem->master = emaster;
if ( emaster->dependent == NULL )
emaster->dependent = calloc( bundle->cnt*2,sizeof( struct dependent_stem ));
emaster->dependent[emaster->dep_cnt ].stem = stem;
emaster->dependent[emaster->dep_cnt ].dep_type = etype;
emaster->dependent[emaster->dep_cnt++].lbase = is_x;
}
}
/* If a stem has been considered depending from another stem which in */
/* its turn has its own "master", and the first stem doesn't conflict */
/* with the "master" of the stem it overlaps (or any other stems), then */
/* this dependency is unneeded and processing it in the autoinstructor */
/* can even lead to undesired effects. Unfortunately we can't prevent */
/* detecting such dependecies in LookForMasterHVStem(), because we */
/* need to know the whole stem hierarchy first. So look for undesired */
/* dependencies and clean them now */
static void ClearUnneededDeps( struct stemdata *stem ) {
struct stemdata *master;
int i, j;
if ( stem->confl_cnt == 1 &&
( master = stem->master ) != NULL && master->master != NULL ) {
stem->master = NULL;
for ( i=j=0; i<master->dep_cnt; i++ ) {
if ( j<i )
memcpy( &master->dependent[i-1],&master->dependent[i],
sizeof( struct dependent_stem ));
if ( master->dependent[i].stem != stem ) j++;
}
(master->dep_cnt)--;
}
}
static void GDBundleStems( struct glyphdata *gd, int maxtoobig, int needs_deps ) {
struct stemdata *stem, *tstem;
int i, j, k, hv, hasl, hasr, stem_cnt;
struct pointdata *lpd, *rpd;
double dmove;
DBounds bounds;
/* Some checks for undesired stems which we couldn't do earlier */
/* First filter out HV stems which have only "potential" points */
/* on their left or right edge. Such stems aren't supposed to be */
/* used for PS hinting, so we mark them as "too big" */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
hasl = false; hasr = false;
if ( IsUnitHV( &stem->unit,true ) &&
!stem->toobig && !stem->ghost && !stem->positioned ) {
for ( j=0; j<stem->chunk_cnt && ( !hasl || !hasr ); ++j ) {
if ( stem->chunks[j].l!=NULL && !stem->chunks[j].lpotential )
hasl = true;
if ( stem->chunks[j].r!=NULL && !stem->chunks[j].rpotential )
hasr = true;
}
if ( !hasl || !hasr )
stem->toobig = true;
}
}
/* Filter out HV stems which have both their edges controlled by */
/* other, narrower HV stems */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
hv = IsUnitHV( &stem->unit,true );
if ( IsUnitHV( &stem->unit,true )) {
hasl = hasr = false;
for ( j=0; j<stem->chunk_cnt; ++j ) {
lpd = stem->chunks[j].l;
rpd = stem->chunks[j].r;
if ( lpd != NULL ) {
stem_cnt = ( stem->chunks[j].lnext ) ? lpd->nextcnt : lpd->prevcnt;
for ( k=0; k<stem_cnt; k++ ) {
tstem = ( stem->chunks[j].lnext ) ?
lpd->nextstems[k] : lpd->prevstems[k];
/* Used to test tstem->toobig <= stem->toobig, but got into troubles with */
/* a weird terminal stem preventing a ball terminal from being properly detected, */
/* because both the stems initially have toobig == 1. */
/* See the "f" from Heuristica-Italic */
if ( tstem != stem &&
!tstem->toobig && tstem->positioned >= stem->positioned &&
tstem->width < stem->width && hv == IsUnitHV( &tstem->unit,true )) {
hasl = true;
break;
}
}
}
if ( rpd != NULL ) {
stem_cnt = ( stem->chunks[j].rnext ) ? rpd->nextcnt : rpd->prevcnt;
for ( k=0; k<stem_cnt; k++ ) {
tstem = ( stem->chunks[j].rnext ) ?
rpd->nextstems[k] : rpd->prevstems[k];
if ( tstem != stem &&
!tstem->toobig && tstem->positioned >= stem->positioned &&
tstem->width < stem->width && hv == IsUnitHV( &tstem->unit,true )) {
hasr = true;
break;
}
}
}
if ( hasl && hasr ) {
stem->toobig = 2;
break;
}
}
}
}
gd->hbundle = calloc( 1,sizeof( struct stembundle ));
gd->hbundle->stemlist = calloc( gd->stemcnt,sizeof( struct stemdata *));
gd->hbundle->unit.x = 1; gd->hbundle->unit.y = 0;
gd->hbundle->l_to_r.x = 0; gd->hbundle->l_to_r.y = -1;
gd->vbundle = calloc( 1,sizeof( struct stembundle ));
gd->vbundle->stemlist = calloc( gd->stemcnt,sizeof( struct stemdata *));
gd->vbundle->unit.x = 0; gd->vbundle->unit.y = 1;
gd->vbundle->l_to_r.x = 1; gd->vbundle->l_to_r.y = 0;
if ( gd->has_slant && !gd->only_hv ) {
SplineCharFindBounds( gd->sc,&bounds );
gd->ibundle = calloc( 1,sizeof( struct stembundle ));
gd->ibundle->stemlist = calloc( gd->stemcnt,sizeof( struct stemdata *));
gd->ibundle->unit.x = gd->slant_unit.x;
gd->ibundle->unit.y = gd->slant_unit.y;
gd->ibundle->l_to_r.x = -gd->ibundle->unit.y;
gd->ibundle->l_to_r.y = gd->ibundle->unit.x;
}
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->toobig > maxtoobig )
continue;
hv = IsUnitHV( &stem->unit,true );
if ( hv == 1 ) {
gd->hbundle->stemlist[(gd->hbundle->cnt)++] = stem;
stem->bundle = gd->hbundle;
} else if ( hv == 2 ) {
gd->vbundle->stemlist[(gd->vbundle->cnt)++] = stem;
stem->bundle = gd->vbundle;
} else if ( gd->has_slant && !gd->only_hv &&
RealNear( stem->unit.x,gd->slant_unit.x ) &&
RealNear( stem->unit.y,gd->slant_unit.y )) {
/* Move base point coordinates to the baseline to simplify */
/* stem ordering and positioning relatively to each other */
stem->left.x -= (( stem->left.y - bounds.miny ) * stem->unit.x )/stem->unit.y;
stem->right.x -= (( stem->right.y - bounds.miny ) * stem->unit.x )/stem->unit.y;
dmove = ( stem->left.y - bounds.miny ) / stem->unit.y;
stem->left.y = stem->right.y = bounds.miny;
for ( j=0; j<stem->activecnt; j++ ) {
stem->active[j].start += dmove;
stem->active[j].end += dmove;
}
gd->ibundle->stemlist[(gd->ibundle->cnt)++] = stem;
stem->bundle = gd->ibundle;
stem->italic = true;
}
}
qsort( gd->hbundle->stemlist,gd->hbundle->cnt,sizeof( struct stemdata *),stem_cmp );
qsort( gd->vbundle->stemlist,gd->vbundle->cnt,sizeof( struct stemdata *),stem_cmp );
if ( gd->has_slant && !gd->only_hv )
qsort( gd->ibundle->stemlist,gd->ibundle->cnt,sizeof( struct stemdata *),stem_cmp );
if ( !needs_deps )
return;
for ( i=0; i<gd->hbundle->cnt; i++ )
LookForMasterHVStem( gd->hbundle->stemlist[i],&gd->bd );
for ( i=0; i<gd->hbundle->cnt; i++ )
ClearUnneededDeps( gd->hbundle->stemlist[i] );
for ( i=0; i<gd->vbundle->cnt; i++ )
LookForMasterHVStem( gd->vbundle->stemlist[i],&gd->bd );
for ( i=0; i<gd->vbundle->cnt; i++ )
ClearUnneededDeps( gd->vbundle->stemlist[i] );
}
static void AddSerifOrBall( struct glyphdata *gd,
struct stemdata *master,struct stemdata *slave,int lbase,int is_ball ) {
struct dependent_serif *tserif;
struct pointdata *spd, *bpd=NULL;
double width, min, max;
int i, j, refidx, scnt, next;
if ( lbase ) {
width = fabs(
( slave->right.x - master->left.x ) * master->unit.y -
( slave->right.y - master->left.y ) * master->unit.x );
max = width + slave->rmin + 2*dist_error_hv;
min = width + slave->rmax - 2*dist_error_hv;
} else {
width = fabs(
( master->right.x - slave->left.x ) * master->unit.y -
( master->right.y - slave->left.y ) * master->unit.x );
max = width - slave->lmax + 2*dist_error_hv;
min = width - slave->lmin - 2*dist_error_hv;
}
scnt = master->serif_cnt;
for ( i=0; i<scnt; i++ ) {
tserif = &master->serifs[i];
if ( tserif->stem == slave && tserif->lbase == lbase )
break;
else if ( tserif->width > min && tserif->width < max && tserif->lbase == lbase ) {
for ( j=0; j<slave->chunk_cnt; j++ ) {
spd = ( lbase ) ? slave->chunks[j].r : slave->chunks[j].l;
next = ( lbase ) ? slave->chunks[j].rnext : slave->chunks[j].lnext;
if ( spd != NULL && IsStemAssignedToPoint( spd,tserif->stem,next ) == -1 )
AddToStem( gd,tserif->stem,spd,NULL,next,false,false );
}
break;
}
}
if ( i<master->serif_cnt )
return;
refidx = ( lbase ) ? master->leftidx : master->rightidx;
if ( refidx != -1 ) bpd = &gd->points[refidx];
master->serifs = realloc(
master->serifs,( scnt+1 )*sizeof( struct dependent_serif ));
master->serifs[scnt].stem = slave;
master->serifs[scnt].width = width;
master->serifs[scnt].lbase = lbase;
master->serifs[scnt].is_ball = is_ball;
master->serif_cnt++;
/* Mark the dependent stem as related with a bundle, although it */
/* is not listed in that bundle itself */
slave->bundle = master->bundle;
}
static int IsBall( struct glyphdata *gd,
struct pointdata *pd,struct stemdata *master,int lbase ) {
double max, min, dot, coord;
BasePoint *lbp, *rbp, *dir;
Spline *test;
struct pointdata *nbase, *pbase, *tpd;
struct stem_chunk *chunk;
int i, is_x, peak_passed;
if ( pd == NULL || ( pd->x_extr != 1 && pd->y_extr != 1 ))
return( false );
is_x = ( IsUnitHV( &master->unit,true ) == 1 );
lbp = ( lbase ) ? &master->left : &pd->base;
rbp = ( lbase ) ? &pd->base : &master->right;
min = ( is_x ) ? rbp->y : lbp->x;
max = ( is_x ) ? lbp->y : rbp->x;
peak_passed = false;
nbase = pbase = NULL;
test = pd->sp->next;
dir = &pd->nextunit;
if ( test != NULL ) do {
tpd = &gd->points[test->to->ptindex];
if ( IsStemAssignedToPoint( tpd,master,true ) != -1 ) {
nbase = tpd;
break;
}
coord = ( is_x ) ? tpd->base.y : tpd->base.x;
dot = tpd->nextunit.x * dir->x + tpd->nextunit.y * dir->y;
if ( dot == 0 && !peak_passed ) {
dir = &tpd->nextunit;
dot = 1.0;
peak_passed = true;
}
test = test->to->next;
} while ( test != NULL && test != pd->sp->next && dot > 0 &&
coord >= min && coord <= max );
peak_passed = false;
test = pd->sp->prev;
dir = &pd->prevunit;
if ( test != NULL ) do {
tpd = &gd->points[test->from->ptindex];
if ( IsStemAssignedToPoint( tpd,master,false ) != -1 ) {
pbase = tpd;
break;
}
coord = ( is_x ) ? tpd->base.y : tpd->base.x;
dot = tpd->prevunit.x * dir->x + tpd->prevunit.y * dir->y;
if ( dot == 0 && !peak_passed ) {
dir = &tpd->prevunit;
dot = 1.0;
peak_passed = true;
}
test = test->from->prev;
} while ( test != NULL && test != pd->sp->prev && dot > 0 &&
coord >= min && coord <= max );
if ( nbase != NULL && pbase != NULL ) {
for ( i=0; i<master->chunk_cnt; i++ ) {
chunk = &master->chunks[i];
if (( chunk->l == nbase && chunk->r == pbase ) ||
( chunk->l == pbase && chunk->r == nbase ))
return( true );
}
}
return( false );
}
static void GetSerifData( struct glyphdata *gd,struct stemdata *stem ) {
int i, j, is_x, stem_cnt;
int snext, enext, eidx, allow_s, allow_e, s_ball, e_ball;
struct stem_chunk *chunk;
struct stemdata *tstem, *smaster=NULL, *emaster=NULL;
struct pointdata *spd, *epd;
struct stembundle *bundle;
double start, end, tstart, tend, smend, emstart;
is_x = ( IsUnitHV( &stem->unit,true ) == 1 );
bundle = ( is_x ) ? gd->hbundle : gd->vbundle;
start = ( is_x ) ? stem->right.y : stem->left.x;
end = ( is_x ) ? stem->left.y : stem->right.x;
allow_s = allow_e = true;
s_ball = e_ball = 0;
for ( i=0; i<stem->chunk_cnt && ( allow_s == true || allow_e == true ); i++ ) {
chunk = &stem->chunks[i];
spd = ( is_x ) ? chunk->r : chunk->l;
snext = ( is_x ) ? chunk->rnext : chunk->lnext;
epd = ( is_x ) ? chunk->l : chunk->r;
enext = ( is_x ) ? chunk->lnext : chunk->rnext;
if ( spd != NULL && allow_e ) {
stem_cnt = ( snext ) ? spd->nextcnt : spd->prevcnt;
for ( j=0; j<stem_cnt; j++ ) {
tstem = ( snext ) ? spd->nextstems[j] : spd->prevstems[j];
if (RealNear( tstem->unit.x,stem->unit.x ) && RealNear( tstem->unit.y,stem->unit.y ) &&
!tstem->toobig ) {
chunk->is_ball = e_ball = IsBall( gd,epd,tstem,!is_x );
if ( e_ball ) {
chunk->ball_m = tstem;
emaster = tstem;
emstart = ( is_x ) ? tstem->right.y : tstem->left.x;
}
allow_s = false;
}
}
}
if ( epd != NULL && allow_s ) {
stem_cnt = ( enext ) ? epd->nextcnt : epd->prevcnt;
for ( j=0; j<stem_cnt; j++ ) {
tstem = ( enext ) ? epd->nextstems[j] : epd->prevstems[j];
if (tstem->unit.x == stem->unit.x && tstem->unit.y == stem->unit.y &&
!tstem->toobig ) {
chunk->is_ball = s_ball = IsBall( gd,spd,tstem,is_x );
if ( s_ball ) {
chunk->ball_m = tstem;
smaster = tstem;
smend = ( is_x ) ? tstem->left.y : tstem->right.x;
}
allow_e = false;
}
}
}
}
for ( i=0; i<bundle->cnt; i++ ) {
tstem = bundle->stemlist[i];
if (tstem->unit.x != stem->unit.x || tstem->unit.y != stem->unit.y ||
tstem->toobig || tstem->width >= stem->width )
continue;
tstart = ( is_x ) ? tstem->right.y : tstem->left.x;
tend = ( is_x ) ? tstem->left.y : tstem->right.x;
if ( tstart >= start && tend <= end ) {
if ( allow_s && tstart > start ) {
for ( j=0; j<tstem->chunk_cnt && smaster != tstem; j++ ) {
if ( is_x ) {
spd = tstem->chunks[j].l;
snext = tstem->chunks[j].lnext;
eidx = tstem->chunks[j].l_e_idx;
} else {
spd = tstem->chunks[j].r;
snext = tstem->chunks[j].rnext;
eidx = tstem->chunks[j].r_e_idx;
}
if ( spd != NULL && ConnectsAcrossToStem( gd,spd,snext,stem,is_x,eidx ) &&
( smaster == NULL || smend - start > tend - start )) {
smaster = tstem;
smend = tend;
}
}
}
if ( allow_e && tend < end ) {
for ( j=0; j<tstem->chunk_cnt && emaster != tstem; j++ ) {
if ( is_x ) {
epd = tstem->chunks[j].r;
enext = tstem->chunks[j].rnext;
eidx = tstem->chunks[j].r_e_idx;
} else {
epd = tstem->chunks[j].l;
enext = tstem->chunks[j].lnext;
eidx = tstem->chunks[j].l_e_idx;
}
if ( epd != NULL && ConnectsAcrossToStem( gd,epd,enext,stem,!is_x,eidx ) &&
( emaster == NULL || end - emstart > end - tstart )) {
emaster = tstem;
emstart = tstart;
}
}
}
}
}
if ( smaster != NULL )
AddSerifOrBall( gd,smaster,stem,is_x,s_ball );
if ( emaster != NULL )
AddSerifOrBall( gd,emaster,stem,!is_x,e_ball );
}
static double ActiveOverlap( struct stemdata *stem1,struct stemdata *stem2 ) {
int is_x, i, j = 0;
double base1, base2, s1, e1, s2, e2, s, e, len = 0;
is_x = ( IsUnitHV( &stem1->unit,true ) == 2 );
base1 = ( &stem1->left.x )[is_x];
base2 = ( &stem2->left.x )[is_x];
for ( i=0; i<stem1->activecnt; i++ ) {
s1 = base1 + stem1->active[i].start;
e1 = base1 + stem1->active[i].end;
for ( ; j<stem2->activecnt; j++ ) {
s2 = base2 + stem2->active[j].start;
e2 = base2 + stem2->active[j].end;
if ( s2 > e1 )
break;
if ( e2 < s1 )
continue;
s = s2 < s1 ? s1 : s2;
e = e2 > e1 ? e1 : e2;
if ( e<s )
continue; /* Shouldn't happen */
len += e - s;
}
}
return( len );
}
static int StemPairsSimilar( struct stemdata *s1, struct stemdata *s2,
struct stemdata *ts1, struct stemdata *ts2 ) {
int normal, reversed, ret = 0;
double olen1, olen2;
/* Stem widths in the second pair should be nearly the same as */
/* stem widths in the first pair */
normal = ( ts1->width >= s1->width - dist_error_hv &&
ts1->width <= s1->width + dist_error_hv &&
ts2->width >= s2->width - dist_error_hv &&
ts2->width <= s2->width + dist_error_hv );
reversed = (ts1->width >= s2->width - dist_error_hv &&
ts1->width <= s2->width + dist_error_hv &&
ts2->width >= s1->width - dist_error_hv &&
ts2->width <= s1->width + dist_error_hv );
if ( !normal && !reversed )
return( false );
if ( normal ) {
olen1 = ActiveOverlap( s1, ts1 );
olen2 = ActiveOverlap( s2, ts2 );
ret = olen1 > s1->clen/3 && olen1 > ts1->clen/3 &&
olen2 > s2->clen/3 && olen2 > ts2->clen/3;
} else if ( reversed ) {
olen1 = ActiveOverlap( s1, ts2 );
olen2 = ActiveOverlap( s2, ts1 );
ret = olen1 > s1->clen/3 && olen1 > ts2->clen/3 &&
olen2 > s2->clen/3 && olen2 > ts1->clen/3;
}
return( ret );
}
static void FindCounterGroups( struct glyphdata *gd,int is_v ) {
struct stembundle *bundle = is_v ? gd->vbundle : gd->hbundle;
struct stemdata *curm, *prevm, *cur, *prev;
int i, j;
double mdist, dist;
prevm = NULL;
for ( i=0; i<bundle->cnt; i++ ) {
curm = prev = bundle->stemlist[i];
if ( curm->master != NULL )
continue;
if ( prevm == NULL || curm->prev_c_m != NULL ) {
prevm = curm;
continue;
}
mdist = is_v ? curm->left.x - prevm->right.x : curm->right.y - prevm->left.y;
for ( j=i+1; j<bundle->cnt; j++ ) {
cur = bundle->stemlist[j];
if ( cur->master != NULL )
continue;
if ( cur->prev_c_m != NULL ) {
prev = cur;
continue;
}
dist = is_v ? cur->left.x - prev->right.x : cur->right.y - prev->left.y;
if ( mdist > dist - dist_error_hv && mdist < dist + dist_error_hv &&
StemPairsSimilar( prevm,curm,prev,cur )) {
prev->next_c_m = prevm;
cur->prev_c_m = curm;
}
prev = cur;
}
prevm = curm;
}
}
/* Normally we use the DetectDiagonalStems flag (set via the Preferences dialog) to determine */
/* if diagonal stems should be generated. However, sometimes it makes sense to reduce the */
/* processing time, deliberately turning the diagonal stem detection off: in particular we */
/* don't need any diagonal stems if we only want to assign points to some preexisting HV */
/* hints. For thisreason the only_hv argument still can be passed to this function. */
struct glyphdata *GlyphDataInit( SplineChar *sc,int layer,double em_size,int only_hv ) {
struct glyphdata *gd;
struct pointdata *pd;
int i;
SplineSet *ss;
SplinePoint *sp;
Monotonic *m;
int cnt;
double iangle;
if ( layer<0 || layer>=sc->layer_cnt )
return( NULL );
/* We only hint one layer at a time */
/* We shan't try to hint references yet */
if ( sc->layers[layer].splines==NULL )
return( NULL );
gd = calloc( 1,sizeof( struct glyphdata ));
gd->only_hv = only_hv;
gd->layer = layer;
gd->sc = sc;
gd->sf = sc->parent;
gd->emsize = em_size;
gd->order2 = ( sc->parent != NULL ) ? sc->parent->layers[layer].order2 : false;
gd->fuzz = GetBlueFuzz( sc->parent );
dist_error_hv = .0035*gd->emsize;
dist_error_diag = .0065*gd->emsize;
dist_error_curve = .022*gd->emsize;
if ( sc->parent != NULL && sc->parent->italicangle ) {
iangle = ( 90 + sc->parent->italicangle );
gd->has_slant = true;
gd->slant_unit.x = cos( iangle * ( PI/180 ));
gd->slant_unit.y = sin( iangle * ( PI/180 ));
} else {
gd->has_slant = false;
gd->slant_unit.x = 0; gd->slant_unit.y = 1;
}
/* SSToMContours can clean up the splinesets (remove 0 length splines) */
/* so it must be called BEFORE everything else (even though logically */
/* that doesn't make much sense). Otherwise we might have a pointer */
/* to something since freed */
gd->ms = SSsToMContours(sc->layers[layer].splines,over_remove); /* second argument is meaningless here */
gd->realcnt = gd->pcnt = SCNumberPoints( sc, layer );
for ( i=0, ss=sc->layers[layer].splines; ss!=NULL; ss=ss->next, ++i );
gd->ccnt = i;
gd->contourends = malloc((i+1)*sizeof(int));
for ( i=0, ss=sc->layers[layer].splines; ss!=NULL; ss=ss->next, ++i ) {
SplinePoint *last;
if ( ss->first->prev!=NULL )
last = ss->first->prev->from;
else
last = ss->last;
if ( last->ttfindex==0xffff )
gd->contourends[i] = last->nextcpindex;
else
gd->contourends[i] = last->ttfindex;
}
gd->contourends[i] = -1;
/* Create temporary point numbers for the implied points. We need this */
/* for metafont if nothing else */
for ( ss= sc->layers[layer].splines; ss!=NULL; ss = ss->next ) {
for ( sp = ss->first; ; ) {
if ( sp->ttfindex < gd->realcnt )
sp->ptindex = sp->ttfindex;
else if ( sp->ttfindex == 0xffff )
sp->ptindex = gd->pcnt++;
if ( sp->next==NULL )
break;
sp = sp->next->to;
if ( sp==ss->first )
break;
}
}
gd->norefpcnt = gd->pcnt;
/* And for 0xfffe points such as those used in glyphs with order2 glyphs */
/* with references. */
for ( ss = sc->layers[layer].splines; ss!=NULL; ss = ss->next ) {
for ( sp = ss->first; ; ) {
if ( sp->ttfindex == 0xfffe )
sp->ptindex = gd->pcnt++;
if ( sp->next==NULL )
break;
sp = sp->next->to;
if ( sp==ss->first )
break;
}
}
gd->pspace = malloc( gd->pcnt*sizeof( struct pointdata *));
/*gd->ms = SSsToMContours(sc->layers[layer].splines,over_remove);*/ /* second argument is meaningless here */
for ( m=gd->ms, cnt=0; m!=NULL; m=m->linked, ++cnt );
gd->space = malloc((cnt+2)*sizeof(Monotonic*));
gd->mcnt = cnt;
gd->points = calloc(gd->pcnt,sizeof(struct pointdata));
for ( ss=sc->layers[layer].splines; ss!=NULL; ss=ss->next ) if ( ss->first->prev!=NULL ) {
for ( sp=ss->first; ; ) {
PointInit( gd,sp,ss );
if ( sp->next==NULL )
break;
sp = sp->next->to;
if ( sp==ss->first )
break;
}
}
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
pd = &gd->points[i];
if ( !pd->nextzero )
pd->next_e_cnt = FindMatchingEdge(gd,pd,true,pd->nextedges);
if ( !pd->prevzero )
pd->prev_e_cnt = FindMatchingEdge(gd,pd,false,pd->prevedges);
if (( pd->symetrical_h || pd->symetrical_v ) && ( pd->x_corner || pd->y_corner))
FindMatchingEdge(gd,pd,2,&pd->bothedge);
}
return( gd );
}
struct glyphdata *GlyphDataBuild( SplineChar *sc,int layer, BlueData *bd,int use_existing ) {
struct glyphdata *gd;
struct pointdata *pd;
struct stemdata *stem;
BasePoint dir;
struct stem_chunk *chunk;
int i, j, only_hv, startcnt, stemcnt, ecnt, hv, has_h, has_v;
double em_size;
DBounds bounds;
only_hv = ( !detect_diagonal_stems && ( !use_existing || sc->dstem == NULL ));
em_size = ( sc->parent != NULL ) ? sc->parent->ascent + sc->parent->descent : 1000;
gd = GlyphDataInit( sc,layer,em_size,only_hv );
if ( gd == NULL )
return( gd );
/* Get the alignment zones */
if ( bd == NULL )
QuickBlues( gd->sf,gd->layer,&gd->bd );
else
memcpy( &gd->bd,bd,sizeof( BlueData ));
/* There will never be more lines than there are points (counting next/prev as separate) */
gd->lines = malloc( 2*gd->pcnt*sizeof( struct linedata ));
gd->linecnt = 0;
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
pd = &gd->points[i];
if (( !gd->only_hv || pd->next_hor || pd->next_ver ) && pd->nextline==NULL ) {
pd->nextline = BuildLine(gd,pd,true);
if ( pd->colinear )
pd->prevline = pd->nextline;
}
if (( !gd->only_hv || pd->prev_hor || pd->prev_ver ) && pd->prevline==NULL ) {
pd->prevline = BuildLine(gd,pd,false);
if ( pd->colinear && pd->nextline == NULL )
pd->nextline = pd->prevline;
}
}
/* There will never be more stems than there are points (counting next/prev as separate) */
gd->stems = calloc( 2*gd->pcnt,sizeof( struct stemdata ));
gd->stemcnt = 0; /* None used so far */
if ( use_existing ) {
SplineCharFindBounds( gd->sc,&bounds );
if ( sc->vstem != NULL )
_StemInfoToStemData( gd,sc->vstem,&bounds,true,&startcnt );
if ( sc->hstem != NULL )
_StemInfoToStemData( gd,sc->hstem,&bounds,false,&startcnt );
if ( sc->dstem != NULL )
_DStemInfoToStemData( gd,sc->dstem,&startcnt );
}
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp!=NULL ) {
pd = &gd->points[i];
if ( pd->prev_e_cnt > 0 ) {
ecnt = BuildStem( gd,pd,false,false,use_existing,0 );
if ( ecnt == 0 && pd->prev_e_cnt > 1 )
BuildStem( gd,pd,false,false,false,1 );
}
if ( pd->next_e_cnt > 0 ) {
ecnt = BuildStem( gd,pd,true,false,use_existing,0 );
if ( ecnt == 0 && pd->next_e_cnt > 1 )
BuildStem( gd,pd,true,false,false,1 );
}
if ( pd->bothedge!=NULL ) {
DiagonalCornerStem( gd,pd,false );
}
/* Snap corner extrema to preexisting hints if they have not */
/* already been. This is currently done only when preparing */
/* glyph data for the autoinstructor */
if ( use_existing && ( pd->x_corner || pd->y_corner )) {
has_h = has_v = false;
for ( j=0; j<pd->prevcnt && (( pd->x_corner && !has_v ) || ( pd->y_corner && !has_h )); j++ ) {
hv = IsUnitHV( &pd->prevstems[j]->unit,true );
if ( hv == 1 ) has_h = true;
else if ( hv == 2 ) has_v = true;
}
for ( j=0; j<pd->nextcnt && (( pd->x_corner && !has_v ) || ( pd->y_corner && !has_h )); j++ ) {
hv = IsUnitHV( &pd->nextstems[j]->unit,true );
if ( hv == 1 ) has_h = true;
else if ( hv == 2 ) has_v = true;
}
if ( pd->x_corner && !has_v ) {
dir.x = 0; dir.y = 1;
HalfStemNoOpposite( gd,pd,NULL,&dir,2 );
} else if ( pd->y_corner && !has_h ) {
dir.x = 1; dir.y = 0;
HalfStemNoOpposite( gd,pd,NULL,&dir,2 );
}
}
}
AssignLinePointsToStems( gd );
/* Normalize stems before calculating active zones (as otherwise */
/* we don't know exact positions of stem edges */
for ( i=0; i<gd->stemcnt; ++i )
NormalizeStem( gd,&gd->stems[i] );
GDNormalizeStubs( gd );
/* Figure out active zones at the first order (as they are needed to */
/* determine which stems are undesired and they don't depend from */
/* the "potential" state of left/right points in chunks */
gd->lspace = malloc(gd->pcnt*sizeof(struct segment));
gd->rspace = malloc(gd->pcnt*sizeof(struct segment));
gd->bothspace = malloc(3*gd->pcnt*sizeof(struct segment));
gd->activespace = malloc(3*gd->pcnt*sizeof(struct segment));
#if GLYPH_DATA_DEBUG
fprintf( stderr,"Going to calculate stem active zones for %s\n",gd->sc->name );
#endif
for ( i=0; i<gd->stemcnt; ++i )
FigureStemActive( gd,&gd->stems[i] );
/* Check this before resolving stem conflicts, as otherwise we can */
/* occasionally prefer a stem which should be excluded from the list */
/* for some other reasons */
GDFindUnlikelyStems( gd );
/* we were cautious about assigning points to stems, go back now and see */
/* if there are any low-quality matches which remain unassigned, and if */
/* so then assign them to the stem they almost fit on. */
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
for ( j=0; j<stem->chunk_cnt; ++j ) {
chunk = &stem->chunks[j];
if ( chunk->l!=NULL && chunk->lpotential ) {
stemcnt = ( chunk->lnext ) ? chunk->l->nextcnt : chunk->l->prevcnt;
if ( stemcnt == 1 ) chunk->lpotential = false;
}
if ( chunk->r!=NULL && chunk->rpotential ) {
stemcnt = ( chunk->rnext ) ? chunk->r->nextcnt : chunk->r->prevcnt;
if ( stemcnt == 1 ) chunk->rpotential = false;
}
}
}
/* If there are multiple stems, find the one which is closest to this point */
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp != NULL ) {
pd = &gd->points[i];
if ( pd->prevcnt > 1 ) CheckPotential( gd,pd,false );
if ( pd->nextcnt > 1 ) CheckPotential( gd,pd,true );
}
if ( hint_bounding_boxes )
CheckForBoundingBoxHints( gd );
CheckForGhostHints( gd );
if ( use_existing )
MarkDStemCorners( gd );
GDBundleStems( gd,0,use_existing );
if ( use_existing ) {
for ( i=0; i<gd->stemcnt; ++i ) {
stem = &gd->stems[i];
if ( stem->toobig == 1 && IsUnitHV( &stem->unit,true ))
GetSerifData( gd,stem );
}
FindCounterGroups( gd,true );
}
#if GLYPH_DATA_DEBUG
DumpGlyphData( gd );
#endif
free(gd->lspace); gd->lspace = NULL;
free(gd->rspace); gd->rspace = NULL;
free(gd->bothspace); gd->bothspace = NULL;
free(gd->activespace); gd->activespace = NULL;
return( gd );
}
void GlyphDataFree(struct glyphdata *gd) {
int i;
if ( gd == NULL )
return;
FreeMonotonics( gd->ms ); gd->ms = NULL;
free( gd->space ); gd->space = NULL;
free( gd->sspace ); gd->sspace = NULL;
free( gd->stspace ); gd->stspace = NULL;
free( gd->pspace ); gd->pspace = NULL;
/* Clean up temporary point numbers */
for ( i=0; i<gd->pcnt; ++i ) if ( gd->points[i].sp != NULL )
gd->points[i].sp->ptindex = 0;
if ( gd->hbundle != NULL ) {
free( gd->hbundle->stemlist );
free( gd->hbundle );
}
if ( gd->vbundle != NULL ) {
free( gd->vbundle->stemlist );
free( gd->vbundle );
}
if ( gd->ibundle != NULL ) {
free( gd->ibundle->stemlist );
free( gd->ibundle );
}
for ( i=0; i<gd->linecnt; ++i )
free( gd->lines[i].points );
for ( i=0; i<gd->stemcnt; ++i ) {
free( gd->stems[i].chunks );
free( gd->stems[i].dependent );
free( gd->stems[i].serifs );
free( gd->stems[i].active );
}
for ( i=0; i<gd->pcnt; ++i ) {
free( gd->points[i].nextstems );
free( gd->points[i].next_is_l );
free( gd->points[i].prevstems );
free( gd->points[i].prev_is_l );
}
free( gd->lines );
free( gd->stems );
free( gd->contourends );
free( gd->points );
free( gd );
}
|