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
|
{
Reads an SVG Document
License: The same modified LGPL as the Free Pascal RTL
See the file COPYING.modifiedLGPL for more details
AUTHORS: Felipe Monteiro de Carvalho
SVG Coordinates vs FPVectorial coordinates:
SVG by default has [0, 0] at the top-left and coordinates grow downwards and to the right
Text is drawn upwards (towards negative Y)
* Example of a supported SVG image:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with fpVectorial (http://wiki.lazarus.freepascal.org/fpvectorial) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
width="100mm"
height="100mm"
id="svg2"
version="1.1"
sodipodi:docname="New document 1">
<g id="layer1">
<path
style="fill:none;stroke:#000000;stroke-width:10px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 0,283.486888731396 l 106.307583274274,-35.4358610914245 "
id="path0" />
<path
style="fill:none;stroke:#000000;stroke-width:10px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 0,354.358610914245 l 354.358610914245,0 l 0,-354.358610914245 l -354.358610914245,0 l 0,354.358610914245 "
id="path1" />
<path
style="fill:none;stroke:#000000;stroke-width:10px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 0,354.358610914245 l 35.4358610914245,-35.4358610914245 c 0,-35.4358610914246 35.4358610914245,-35.4358610914246 35.4358610914245,0 l 35.4358610914245,35.4358610914245 "
id="path2" />
</g>
</svg>
}
unit svgvectorialreader;
{$mode objfpc}{$H+}
{$define SVG_MERGE_LAYER_STYLES}
{$define FPVECTORIAL_SVG_SPLIT_PATHS}
interface
uses
Classes, SysUtils, math, contnrs,
fpimage, fpcanvas, laz2_xmlread, laz2_dom, fgl,
// image data formats
fpreadpng,
fpvectorial, fpvutils, lazutf8, TypInfo;
type
TDoubleArray = array of Double;
TSVGTokenType = (
// moves
sttMoveTo, sttRelativeMoveTo,
// Close Path
sttClosePath,
// lines
sttLineTo, sttRelativeLineTo,
sttHorzLineTo, sttRelativeHorzLineTo, sttVertLineTo, sttRelativeVertLineTo,
// cubic beziers
sttBezierTo, sttRelativeBezierTo, sttSmoothBezierTo, sttRelativeSmoothBezierTo,
// quadratic beziers
sttQuadraticBezierTo, sttRelativeQuadraticBezierTo,
// Elliptic curves
sttEllipticArcTo, sttRelativeEllipticArcTo,
// numbers
sttFloatValue);
TSVGToken = class
TokenType: TSVGTokenType;
Value: Float;
StrValue: string; // filled only by TokenizeFunctions
end;
TSVGTokenList = specialize TFPGList<TSVGToken>;
{ TSVGObjectStack }
TSVGObjectStack = class(TObjectStack)
public
function GetList: TList;
end;
{ TSVGTextSpanStyle }
TSVGTextSpanStyle = class(TvStyle)
public
PositionSet: Boolean;
X, Y: Double;
function GenerateDebugTree(ADestRoutine: TvDebugAddItemProc; APageItem: Pointer): Pointer; override;
end;
TvSVGVectorialReader = class;
{ TSVG_CSS_Style }
TSVG_CSS_Style = class(TvStyle)
public
CSSName, CSSData: string;
function MatchesClass(AClassName: string): Boolean;
procedure ParseCSSData(AReader: TvSVGVectorialReader);
end;
{ TSVGPathTokenizer }
TSVGPathTokenizer = class
protected
Tokens: TSVGTokenList;
public
FPointSeparator, FCommaSeparator: TFormatSettings;
ExtraDebugStr: string;
constructor Create;
destructor Destroy; override;
procedure AddToken(AStr: string);
procedure ClearTokens;
procedure TokenizePathString(AStr: string);
procedure TokenizeFunctions(AStr: string);
function DebugOutTokensAsString: string;
end;
TSVGCoordinateKind = (sckUnknown, sckX, sckY, sckXDelta, sckYDelta, sckXSize, sckYSize);
TSVGUnit = (suPX, suMM, suPT {Points});
{ TvSVGPathList }
TvSVGPathList = class(TFPObjectList)
public
// parsing temporary info
IsFirstPathMove: Boolean;
LastPathClosed: Boolean;
CurTokenIndex: Integer;
CurX, CurY: Double;
Data: TvVectorialPage;
Doc: TvVectorialDocument;
// Path support for multiple polygons
LastPathStart: T2DPoint;
function GetPath(AIndex: Integer): TPath;
end;
{ TvSVGVectorialReader }
TvSVGVectorialReader = class(TvCustomVectorialReader)
private
FPointSeparator: TFormatSettings;
FSVGPathTokenizer: TSVGPathTokenizer;
FLayerStylesKeys, FLayerStylesValues: TFPList; // of TStringList;
// View box adjustment
ViewBoxAdjustment: Boolean;
ViewBox_Left, ViewBox_Top, ViewBox_Width, ViewBox_Height, Page_Width, Page_Height: Double;
// Defs section
FBrushDefs: TFPList; // of TvEntityWithPenAndBrush;
FCSSDefs: TFPList; // of TSVG_CSS_Style;
// debug symbols
FPathNumber: Integer;
// BrushDefs functions
function FindBrushDef_WithName(AName: string): TvEntityWithPenAndBrush;
//
function ReadSVGColor(AValue: string): TFPColor;
function ReadSVGGradientColorStyle(AValue: String): TFPColor;
function ReadSVGStyle(AData: TvVectorialPage; AValue: string;
ADestEntity: TvEntityWithPen; ADestStyle: TvStyle = nil;
AUseFillAsPen: Boolean = False): TvSetPenBrushAndFontElements;
function ReadSVGStyleToStyleLists(AValue: string; AStyleKeys, AStyleValues: TStringList): TvSetPenBrushAndFontElements;
function ReadSVGPenStyleWithKeyAndValue(AKey, AValue: string; ADestEntity: TvEntityWithPen; ADestStyle: TvStyle = nil): TvSetPenBrushAndFontElements;
function ReadSVGBrushStyleWithKeyAndValue(AKey, AValue: string; ADestEntity: TvEntityWithPenAndBrush; ADestStyle: TvStyle = nil): TvSetPenBrushAndFontElements;
function ReadSVGFontStyleWithKeyAndValue(AKey, AValue: string; ADestEntity: TvEntityWithPenBrushAndFont; ADestStyle: TvStyle = nil): TvSetPenBrushAndFontElements;
procedure ReadSVGGeneralStyleWithKeyAndValue(AData: TvVectorialPage;
AKey, AValue: string; ADestEntity: TvEntity);
function IsAttributeFromStyle(AStr: string): Boolean;
procedure ApplyLayerStyles(AData: TvVectorialPage; ADestEntity: TvEntity);
function ReadSpaceSeparatedFloats(AInput: string; AOtherSeparators: string): TDoubleArray;
procedure ReadSVGTransformationMatrix(AMatrix: string; out AA, AB, AC, AD, AE, AF: Double);
procedure ApplyCSSClass(AData: TvVectorialPage; AValue: string;
ADestEntity: TvEntityWithPen);
function IsEntityStyleField(AFieldName: string): Boolean;
function ReadEntityStyleField(AData: TvVectorialPage; AFieldName, AFieldValue: string; ADestEntity: TvEntityWithPen; ADestStyle: TvStyle = nil;
AUseFillAsPen: Boolean = False): TvSetPenBrushAndFontElements;
//
function GetTextContentFromNode(ANode: TDOMNode): string;
procedure ReadDefs_LinearGradient(ADest: TvEntityWithPenAndBrush; ANode: TDOMNode; AData: TvVectorialPage);
procedure ReadDefs_CSS(ANode: TDOMNode);
procedure ReadDefsFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument);
//
function ReadEntityFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadCircleFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadEllipseFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadFrameFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadFrameTextFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadImageFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
procedure ReadLayerFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument);
function ReadLineFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadPathFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadPathFromString(AStr: string; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvSVGPathList;
procedure ReadNextPathCommand(ACurTokenType: TSVGTokenType; APaths: TvSVGPathList; var CurX, CurY: Double);
procedure ReadPointsFromString(AStr: string; AData: TvVectorialPage; ADoc: TvVectorialDocument; AClosePath: Boolean);
function ReadPolyFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadRectFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadSymbolFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadTextFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
function ReadUseFromNode(ANode: TDOMNode; AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
//
procedure StringToPenPattern(const AStr: String; var APen: TvPen);
function StringWithUnitToFloat(AStr: string; ACoordKind: TSVGCoordinateKind = sckUnknown;
ADefaultUnit: TSVGUnit = suPX; ATargetUnit: TSVGUnit = suPX): Double;
function StringFloatZeroToOneToWord(AStr: string): Word;
function StringWithPercentToFloat(AStr: String): Double;
procedure ConvertSVGCoordinatesToFPVCoordinates(
const AData: TvVectorialPage;
const ASrcX, ASrcY: Double; var ADestX, ADestY: Double;
ADoViewBoxAdjust: Boolean = True);
procedure ConvertSVGDeltaToFPVDelta(
const AData: TvVectorialPage;
const ASrcX, ASrcY: Double; var ADestX, ADestY: Double;
ADoViewBoxAdjust: Boolean = True);
procedure ConvertSVGSizeToFPVSize(
const AData: TvVectorialPage;
const ASrcX, ASrcY: Double; var ADestX, ADestY: Double;
ADoViewBoxAdjust: Boolean = True);
procedure AutoDetectDocSize(var ALeft, ATop, ARight, ABottom: Double; ABaseNode: TDOMNode);
function SVGColorValueStrToWord(AStr: string): Word;
public
{ General reading methods }
constructor Create; override;
Destructor Destroy; override;
procedure ReadFromStream(AStream: TStream; AData: TvVectorialDocument); override;
procedure ReadFromXML(Doc: TXMLDocument; AData: TvVectorialDocument); override;
class function ReadSpaceSeparatedStrings(AInput: string; AOtherSeparators: string): TStringList;
end;
implementation
const
// SVG requires hardcoding a DPI value
// The Opera Browser and Inkscape use 90 DPI, so we follow that
// 1 Inch = 25.4 milimiters
// 90 inches per pixel = (1 / 90) * 25.4 = 0.2822
// FLOAT_MILIMETERS_PER_PIXEL = 0.3528; // DPI 72 = 1 / 72 inches per pixel
FLOAT_MILLIMETERS_PER_PIXEL = 1; //0.2822; // DPI 90 = 1 / 90 inches per pixel => Actually I changed the value! Because otherwise it looks ugly!
FLOAT_PIXELS_PER_MILIMETER = 1 / FLOAT_MILLIMETERS_PER_PIXEL; // DPI 90 = 1 / 90 inches per pixel
FLOAT_POINTS_PER_PIXEL = 0.75; // For conversion
FLOAT_PIXEL_PER_POINT = 1 / FLOAT_POINTS_PER_PIXEL; // For conversion
{ TSVG_CSS_Style }
function TSVG_CSS_Style.MatchesClass(AClassName: string): Boolean;
var
lNameModified: string;
begin
lNameModified := '.' + AClassName;
Result := (lNameModified = CSSName);
end;
{
<style type="text/css">
<![CDATA[
.strr3 {stroke:#C7C7A2;stroke-width:0.793701}
.str1 {stroke:#8C8C60;stroke-width:1.70079}
.strr2 {stroke:#636347;stroke-width:2.26772}
.str0 {stroke:#2B2A29;stroke-width:3.9685}
.fil1 {fill:none}
.fil0 {fill:#EEEED4}
]]>
}
procedure TSVG_CSS_Style.ParseCSSData(AReader: TvSVGVectorialReader);
begin
SetElements += AReader.ReadSVGStyle(nil, CSSData, nil, Self, False);
end;
{ TSVGTextSpanStyle }
function TSVGTextSpanStyle.GenerateDebugTree(ADestRoutine: TvDebugAddItemProc;
APageItem: Pointer): Pointer;
begin
FExtraDebugStr := '';
if PositionSet then
FExtraDebugStr := FExtraDebugStr + Format(' X=%f Y=%f', [X, Y]);
Result:=inherited GenerateDebugTree(ADestRoutine, APageItem);
end;
{ TSVGObjectStack }
function TSVGObjectStack.GetList: TList;
begin
Result := List;
end;
{ TSVGPathTokenizer }
constructor TSVGPathTokenizer.Create;
begin
inherited Create;
FPointSeparator := DefaultFormatSettings;
FPointSeparator.DecimalSeparator := '.';
FPointSeparator.ThousandSeparator := '#';// disable the thousand separator
Tokens := TSVGTokenList.Create;
end;
destructor TSVGPathTokenizer.Destroy;
begin
ClearTokens;
Tokens.Free;
inherited Destroy;
end;
procedure TSVGPathTokenizer.AddToken(AStr: string);
var
lToken: TSVGToken;
lStr: string;
begin
// lToken := TSVGToken.Create;
lStr := Trim(AStr);
if lStr = '' then Exit;
lToken := TSVGToken.Create;
// Moves
if lStr[1] = 'M' then lToken.TokenType := sttMoveTo
else if lStr[1] = 'm' then lToken.TokenType := sttRelativeMoveTo
// Close Path
else if lStr[1] = 'Z' then lToken.TokenType := sttClosePath
else if lStr[1] = 'z' then lToken.TokenType := sttClosePath
// Lines
else if lStr[1] = 'L' then lToken.TokenType := sttLineTo
else if lStr[1] = 'l' then lToken.TokenType := sttRelativeLineTo
else if lStr[1] = 'H' then lToken.TokenType := sttHorzLineTo
else if lStr[1] = 'h' then lToken.TokenType := sttRelativeHorzLineTo
else if lStr[1] = 'V' then lToken.TokenType := sttVertLineTo
else if lStr[1] = 'v' then lToken.TokenType := sttRelativeVertLineTo
// cubic Bézier curve commands
else if lStr[1] = 'C' then lToken.TokenType := sttBezierTo
else if lStr[1] = 'c' then lToken.TokenType := sttRelativeBezierTo
else if lStr[1] = 'S' then lToken.TokenType := sttSmoothBezierTo
else if lStr[1] = 's' then lToken.TokenType := sttRelativeSmoothBezierTo
// quadratic beziers
else if lStr[1] = 'Q' then lToken.TokenType := sttQuadraticBezierTo
else if lStr[1] = 'q' then lToken.TokenType := sttRelativeQuadraticBezierTo
// Elliptic curves
else if lStr[1] = 'A' then lToken.TokenType := sttEllipticArcTo
else if lStr[1] = 'a' then lToken.TokenType := sttRelativeEllipticArcTo
else
begin
lToken.TokenType := sttFloatValue;
try
lToken.Value := StrToFloat(AStr, FPointSeparator);
except
on MyException: Exception do
begin
MyException.Message := MyException.Message + ExtraDebugStr;
raise MyException;
end;
end;
end;
// Sometimes we get a command glued to a value, for example M150
if (lToken.TokenType <> sttFloatValue) and (Length(lStr) > 1) then
begin
Tokens.Add(lToken);
lToken.TokenType := sttFloatValue;
lStr := Copy(AStr, 2, Length(AStr));
lToken.Value := StrToFloat(lStr, FPointSeparator);
end;
Tokens.Add(lToken);
end;
procedure TSVGPathTokenizer.ClearTokens;
var
i: Integer;
begin
for i := Tokens.Count-1 downto 0 do
Tokens[i].Free;
Tokens.Clear;
end;
procedure TSVGPathTokenizer.TokenizePathString(AStr: string);
const
Str_Space: Char = ' ';
Str_Comma: Char = ',';
Str_Plus: Char = '+';
Str_Minus: Char = '-';
ListOfCommandLetters: set of Char = ['a'..'d', 'f'..'z', 'A'..'D', 'F'..'Z'];
var
i: Integer;
lTmpStr: string = '';
lState: Integer;
lFirstTmpStrChar, lCurChar, lPrevChar: Char;
begin
lState := 0;
i := 1;
while i <= Length(AStr) do
begin
case lState of
0: // Adding to the tmp string
begin
if i > 0 then lPrevChar := AStr[i-1];
lCurChar := AStr[i];
if lCurChar = Str_Space then
begin
lState := 1;
AddToken(lTmpStr);
lTmpStr := '';
end
else if lCurChar = Str_Comma then
begin
AddToken(lTmpStr);
lTmpStr := '';
end
else if (lCurChar in [Str_Plus, Str_Minus]) and (lPrevChar in ['0'..'9']) then
begin
AddToken(lTmpStr);
lTmpStr := lCurChar;
end
// Check for a break, from letter to number
// Note: But don't forget that we need to support 3.799e-4 !!
// So e is not a valid letter for commands here
// Note 2: Letters don't need space between them as in "zm"
else if (lCurChar in ListOfCommandLetters) then
begin
if Length(lTmpStr) > 0 then
begin
AddToken(lTmpStr);
end;
AddToken(lCurChar);
lTmpStr := '';
end
else
begin
lTmpStr := lTmpStr + lCurChar;
end;
Inc(i);
end;
1: // Removing spaces
begin
if AStr[i] <> Str_Space then lState := 0
else Inc(i);
end;
end;
end;
// If there is a token still to be added, add it now
if (lState = 0) and (lTmpStr <> '') then AddToken(lTmpStr);
end;
procedure TSVGPathTokenizer.TokenizeFunctions(AStr: string);
const
Str_Space: Char = ' ';
Str_Start_Params: Char = '(';
Str_End_Params: Char = ')';
ListOfCommandLetters: set of Char = ['a'..'d', 'f'..'z', 'A'..'D', 'F'..'Z'];
var
i: Integer;
lTmpStr: string = '';
lState: Integer;
lFirstTmpStrChar, lCurChar: Char;
lToken: TSVGToken;
begin
lState := 0;
i := 1;
while i <= Length(AStr) do
begin
case lState of
0: // Adding to the tmp string
begin
lCurChar := AStr[i];
if lCurChar in [Str_Start_Params, Str_End_Params] then
begin
lState := 1;
// Add the token
lToken := TSVGToken.Create;
lToken.StrValue := lTmpStr;
Tokens.Add(lToken);
//
lTmpStr := '';
end
else
begin
lTmpStr := lTmpStr + lCurChar;
end;
Inc(i);
end;
1: // Removing spaces
begin
if AStr[i] <> Str_Space then lState := 0
else Inc(i);
end;
end;
end;
// If there is a token still to be added, add it now
if (lState = 0) and (lTmpStr <> '') then AddToken(lTmpStr);
end;
function TSVGPathTokenizer.DebugOutTokensAsString: string;
var
i: Integer;
begin
Result := '';
for i := 0 to Tokens.Count-1 do
Result := Result + GetEnumName(TypeInfo(TSVGTokenType), integer(Tokens.Items[i].TokenType))
+ Format('(%f) ', [Tokens.Items[i].Value]);
end;
{ TvSVGPathList }
function TvSVGPathList.GetPath(AIndex: Integer): TPath;
begin
Result := TPath(Self.Items[AIndex]);
end;
{ TvSVGVectorialReader }
function TvSVGVectorialReader.FindBrushDef_WithName(AName: string): TvEntityWithPenAndBrush;
var
i: Integer;
begin
Result := nil;
for i := 0 to FBrushDefs.Count-1 do
begin
Result := TvEntityWithPenAndBrush(FBrushDefs.Items[i]);
if Result.Name = AName then
begin
Exit;
end;
end;
Result := nil;
end;
function TvSVGVectorialReader.ReadSVGColor(AValue: string): TFPColor;
var
lValue, lStr: string;
lStrings: TStringList;
i: Integer;
HasAlphaChannel: Boolean = False;
begin
Result := colBlack;
lValue := Trim(LowerCase(AValue));
// Support for rgb(255,255,0)
if (Length(lValue) > 3) and (Copy(lValue, 0, 3) = 'rgb') then
begin
lStrings := TStringList.Create;
try
HasAlphaChannel := Copy(lValue, 0, 4) = 'rgba';
if HasAlphaChannel then lStr := Copy(lValue, 6, Length(lValue)-6)
else lStr := Copy(lValue, 5, Length(lValue)-5);
lStrings.Delimiter := ',';
lStrings.StrictDelimiter := True;
lStrings.DelimitedText := lStr;
if lStrings.Count in [3, 4] then
begin
Result.Red := SVGColorValueStrToWord(lStrings.Strings[0]);
Result.Green := SVGColorValueStrToWord(lStrings.Strings[1]);
Result.Blue := SVGColorValueStrToWord(lStrings.Strings[2]);
if (lStrings.Count = 4) and HasAlphaChannel then
Result.alpha := StringFloatZeroToOneToWord(lStrings.Strings[3]);
end
else
raise Exception.Create(Format('[TvSVGVectorialReader.ReadSVGColor] An unexpected number of channels was found: %d', [lStrings.Count]));
finally
lStrings.Free;
end;
Exit;
end;
// Support for RGB hex
// ex: #0000ff
// Another wierd valid variant: #000
if (Length(lValue) > 1) and (lValue[1] = '#') then
begin
lStr := Copy(lValue, 2, 2);
Result.Red := StrToInt('$'+lStr)*$101;
lStr := Copy(lValue, 4, 2);
if lStr = '' then Result.Green := 0
else Result.Green := StrToInt('$'+lStr)*$101;
lStr := Copy(lValue, 6, 2);
if lStr = '' then Result.Blue := 0
else Result.Blue := StrToInt('$'+lStr)*$101;
Exit;
end;
// Support for named colors
// List here: http://www.december.com/html/spec/colorsvghex.html
case lValue[1] of
'a': case lValue of
'aliceblue' : Result := FPColor($F0F0, $F8F8, $FFFF);
'antiquewhite' : Result := FPColor($FAFA, $EBEB, $D7D7);
'aqua' : Result := colCyan;
'aquamarine' : Result := FPColor($7F7F, $FFFF, $D4D4);
'azure' : Result := FPColor($F0F0, $FFFF, $FFFF);
end;
'b': case lValue of
'beige' : Result := FPColor($F5F5, $F5F5, $DCDC);
'bisque' : Result := FPColor($FFFF, $E4E4, $C4C4);
'black' : Result := colBlack;
'blanchedalmond' : Result := FPColor($FFFF, $EBEB, $CDCD);
'blue' : Result := colBlue;
'blueviolet' : Result := FPColor($8A8A, $2B2B, $E2E2);
'brown' : Result := FPColor($A5A5, $2A2A, $2A2A);
'burlywood' : Result := FPColor($DEDE, $B8B8, $8787);
end;
'c': case lValue of
'cadetblue' : Result := FPColor($5F5F, $9E9E, $A0A0);
'chartreuse' : Result := FPColor($7F7F, $FFFF, $0000);
'chocolate' : Result := FPColor($D2D2, $6969, $1E1E);
'coral' : Result := FPColor($FFFF, $7F7F, $5050);
'cornflowerblue' : Result := FPColor($6464, $9595, $EDED);
'cornsilk' : Result := FPColor($FFFF, $F8F8, $DCDC);
'crimson' : Result := FPColor($DCDC, $1414, $3C3C);
'cyan' : Result := colCyan;
end;
'd': case lValue of
'darkblue' : Result.Blue := $8B8B;
'darkcyan' : Result := FPColor($0000, $8B8B, $8B8B);
'darkgoldenrod' : Result := FPColor($B8B8, $8686, $0B0B);
'darkgray',
'darkgrey' : Result := FPColor($A9A9, $A9A9, $A9A9);
'darkgreen' : Result.Green := $6464;
'darkkhaki' : Result := FPColor($BDBD, $B7B7, $6B6B);
'darkmagenta' : Result := FPColor($8B8B, $0000, $8B8B);
'darkolivegreen' : Result := FPColor($5555, $6B6B, $2F2F);
'darkorange' : Result := FPColor($FFFF, $8C8C, $0000);
'darkorchid' : Result := FPColor($9999, $3232, $CCCC);
'darkred' : Result.Red := $8B8B;
'darksalmon' : Result := FPColor($E9E9, $9696, $7A7A);
'darkseagreen' : Result := FPColor($8F8F, $BCBC, $8F8F);
'darkslateblue' : Result := FPColor($4848, $3D3D, $8B8B);
'darkslategray',
'darkslategrey' : Result := FPColor($2F2F, $4F4F, $4F4F);
'darkturquoise' : Result := FPColor($0000, $CECE, $D1D1);
'darkviolet' : Result := FPColor($9494, $0000, $D3D3);
'deeppink' : Result := FPColor($FFFF, $1414, $9393);
'deepskyblue' : Result := FPColor($0000, $BFBF, $FFFF);
'dimgray',
'dimgrey' : Result := FPColor($6969, $6969, $6969);
'dodgerblue' : Result := FPColor($1E1E, $9090, $FFFF);
end;
'f': case lValue of
'firebrick' : Result := FPColor($B2B2, $2222, $2222);
'floralwhite' : Result := FPColor($FFFF, $FAFA, $F0F0);
'forestgreen' : Result := FPColor($2222, $8B8B, $2222);
'fuchsia' : Result := colFuchsia;
end;
'g': case lValue of
'gainsboro' : Result := FPColor($DCDC, $DCDC, $DCDC);
'ghostwhite' : Result := FPColor($F8F8, $F8F8, $FFFF);
'gold' : Result := FPColor($FFFF, $D7D7, $0000);
'goldenrod' : Result := FPColor($DADA, $A5A5, $2020);
'gray', 'grey' : Result := colGray;
'green' : Result.Green := $8080;
'greenyellow' : Result := FPColor($ADAD, $FFFF, $2F2F);
end;
'h': case lValue of
'honeydew' : Result := FPColor($F0F0, $FFFF, $F0F0);
'hotpink' : Result := FPColor($FFFF, $6969, $B4B4);
end;
'i': case lValue of
'indianred' : Result := FPColor($CDCD, $5C5C, $5C5C);
'indigo' : Result := FPColor($4B4B, $0000, $8282);
'ivory' : Result := FPColor($FFFF, $FFFF, $F0F0);
end;
'k': case lValue of
'khaki' : Result := FPColor($F0F0, $E6E6, $8C8C);
end;
'l': case lValue of
'lavender' : Result := FPColor($E6E6, $E6E6, $FAFA);
'lavenderblush' : Result := FPColor($FFFF, $F0F0, $F5F5);
'lawngreen' : Result := FPColor($7C7C, $FCFE, $0000);
'lemonchiffon' : Result := FPColor($FFFF, $FAFA, $CDCD);
'lightblue' : Result := FPColor($ADAD, $D8D8, $E6E6);
'lightcoral' : Result := FPColor($F0F0, $8080, $8080);
'lightcyan' : Result := FPColor($E0E0, $FFFF, $FFFF);
'lightgoldenrodyellow': Result := FPColor($FAFA, $FAFA, $D2D2);
'lightgray',
'lightgrey' : Result := FPColor($D3D3, $D3D3, $D3D3);
'lightgreen' : Result := FPColor($9090, $EEEE, $9090);
'lightpink' : Result := FPColor($FFFF, $B6B6, $C1C1);
'lightsalmon' : Result := FPColor($FFFF, $A0A0, $7A7A);
'lightseagreen' : Result := FPColor($2020, $B2B2, $AAAA);
'lightskyblue' : Result := FPColor($8787, $CECE, $FAFA);
'lightslategray',
'lightslategrey' : Result := FPColor($7777, $8888, $9999);
'lightsteelblue' : Result := FPColor($B0B0, $C4C4, $DEDE);
'lightyellow' : Result := FPColor($FFFF, $FEFE, $0000);
'lime' : Result := colGreen;
'limegreen' : Result := FPColor($3232, $CDCD, $3232);
'linen' : Result := FPColor($FAFA, $F0F0, $E6E6);
end;
'm': case lValue of
'magenta' : Result := colMagenta;
'maroon' : Result.Red := $8080;
'mediumaquamarine' : Result := FPColor($6666, $CDCD, $AAAA);
'mediumblue' : Result.Blue := $CDCD;
'mediumorchid' : Result := FPColor($BABA, $5555, $D3D3);
'mediumpurple' : Result := FPColor($9393, $7070, $DBDB);
'mediumseagreen' : Result := FPColor($3C3C, $CBCB, $7171);
'mediumslateblue' : Result := FPColor($7B7B, $6868, $EEEE);
'mediumspringgreen' : Result := FPColor($0000, $FAFA, $9A9A);
'mediumturquoise' : Result := FPColor($4848, $D1D1, $CCCC);
'mediumvioletred' : Result := FPColor($C7C7, $1515, $8585);
'midnightblue' : Result := FPColor($1919, $1919, $7070);
'mintcream' : Result := FPColor($F5F5, $FFFF, $FAFA);
'mistyrose' : Result := FPColor($FFFF, $E4E4, $E1E1);
'moccasin' : Result := FPColor($FFFF, $E4E4, $B5B5);
end;
'n': case lValue of
'navajowhite' : Result := FPColor($FFFF, $DEDE, $ADAD);
'navy' : Result.Blue := $8080;
end;
'o': case lValue of
'oldlace' : Result := FPColor($FDFD, $F5F5, $E6E6);
'olive' : Result := colOlive;
'olivedrab' : Result := FPColor($6B6B, $8E8E, $2323);
'orange' : Result := FPColor($FFFF, $A5A5, $0000);
'orangered' : Result := FPColor($FFFF, $4545, $0000);
'orchid' : Result := FPColor($DADA, $7070, $D6D6);
end;
'p': case lValue of
'palegreen' : Result := FPColor($9898, $FBFB, $9898);
'palegoldenrod' : Result := FPColor($EEEE, $E8E8, $AAAA);
'paleturquoise' : Result := FPColor($AFAF, $EEEE, $EEEE);
'palevioletred' : Result := FPColor($DBDB, $7070, $9393);
'papayawhip' : Result := FPColor($FFFF, $EFEF, $D5D5);
'peachpuff' : Result := FPColor($FFFF, $DADA, $B9B9);
'peru' : Result := FPColor($CDCD, $8585, $3F3F);
'pink' : Result := FPColor($FFFF, $C0C0, $CBCB);
'plum' : Result := FPColor($DDDD, $A0A0, $DDDD);
'powderblue' : Result := FPColor($B0B0, $E0E0, $E6E6);
'purple' : Result := colPurple;
end;
'r': case lValue of
'red' : Result := colRed;
'rosybrown' : Result := FPColor($BCBC, $8F8F, $8F8F);
'royalblue' : Result := FPColor($4141, $6969, $E1E1);
end;
's': case lValue of
'saddlebrown' : Result := FPColor($8B8B, $4545, $1313);
'salmon' : Result := FPColor($FAFA, $8080, $7272);
'sandybrown' : Result := FPColor($F4F4, $A4A4, $6060);
'seagreen' : Result := FPColor($2E2E, $8B8B, $5757);
'seashell' : Result := FPColor($FFFF, $F5F5, $EEEE);
'sienna' : Result := FPColor($A0A0, $5252, $2D2D);
'silver' : Result := colSilver;
'skyblue' : Result := FPColor($8787, $CECE, $EBEB);
'slateblue' : Result := FPCOlor($6A6A, $5A5A, $CDCD);
'slategray',
'slategrey' : Result := FPColor($7070, $8080, $9090);
'snow' : Result := FPColor($FFFF, $FAFA, $FAFA);
'springgreen' : Result := FPColor($0000, $FFFF, $7F7F);
'steelblue' : Result := FPColor($4646, $8282, $B4B4);
end;
't': case lValue of
'tan' : Result := FPColor($D2D2, $B4B4, $8C8C);
'teal' : Result := FPColor($0000, $8080, $8080);
'thistle' : Result := FPColor($D8D8, $BFBF, $D8D8);
'tomato' : Result := FPColor($FFFF, $6363, $4747);
'turquoise' : Result := FPColor($4040, $E0E0, $D0D0);
end;
'v': case lValue of
'violet' : Result := FPColor($EEEE, $8282, $EEEE);
end;
'w': case lValue of
'wheat' : Result := FPColor($F5F5, $DEDE, $B3B3);
'white' : Result := colWhite;
'whitesmoke' : Result := FPColor($F5F5, $F5F5, $F5F5);
end;
'y': case lValue of
'yellow' : Result := colYellow;
'yellowgreen' : Result := FPColor($9A9A, $CDCD, $3232);
end;
end;
end;
// style="stop-color:rgb(255,255,10);stop-opacity:1.0"
function TvSVGVectorialReader.ReadSVGGradientColorStyle(AValue: String): TFPColor;
var
lStr, lStyleKeyStr, lStyleValueStr: String;
lStrings: TStringList;
i: Integer;
p: Integer;
begin
Result := colBlack;
if AValue = '' then Exit;
lStrings := TStringList.Create;
try
lStrings.Delimiter := ';';
lStrings.StrictDelimiter := True;
lStrings.DelimitedText := LowerCase(AValue);
for i := 0 to lStrings.Count-1 do
begin
lStr := lStrings.Strings[i];
p := Pos(':', lStr);
lStyleKeyStr := Trim(Copy(lStr, 1, p-1));
lStyleValueStr := Trim(Copy(lStr, p+1, MaxInt));
if lStyleKeyStr = 'stop-color' then
Result := ReadSVGColor(lStyleValueStr)
else if lStyleKeyStr = 'stop-opacity' then
Result.Alpha := Round(StrToFloat(lStyleValueStr, FPointSeparator)*$FFFF);
end;
finally
lStrings.Free;
end;
end;
// style="fill:none;stroke:black;stroke-width:3"
function TvSVGVectorialReader.ReadSVGStyle(AData: TvVectorialPage; AValue: string;
ADestEntity: TvEntityWithPen; ADestStyle: TvStyle = nil;
AUseFillAsPen: Boolean = False): TvSetPenBrushAndFontElements;
var
lStr, lStyleKeyStr, lStyleValueStr: String;
lStrings: TStringList;
i: Integer;
begin
Result := [];
if AValue = '' then Exit;
// Now split using ";" separator
lStrings := TStringList.Create;
try
lStrings.Delimiter := ';';
lStrings.StrictDelimiter := True;
lStrings.DelimitedText := AValue;
for i := 0 to lStrings.Count-1 do
begin
lStr := lStrings.Strings[i];
SeparateStringInTwo(lStr, ':', lStyleKeyStr, lStyleValueStr);
lStyleKeyStr := LowerCase(Trim(lStyleKeyStr));
lStyleValueStr := Trim(lStyleValueStr);
if ADestEntity <> nil then
begin
ReadSVGPenStyleWithKeyAndValue(lStyleKeyStr, lStyleValueStr, ADestEntity);
ReadSVGGeneralStyleWithKeyAndValue(AData, lStyleKeyStr, lStyleValueStr, ADestEntity);
if AUseFillAsPen and (lStyleKeyStr = 'fill') then
Result := Result + ReadSVGPenStyleWithKeyAndValue('stroke', lStyleValueStr, ADestEntity)
else if ADestEntity is TvText then
Result := Result + ReadSVGFontStyleWithKeyAndValue(lStyleKeyStr, lStyleValueStr, ADestEntity as TvText)
else if ADestEntity is TvEntityWithPenAndBrush then
Result := Result + ReadSVGBrushStyleWithKeyAndValue(lStyleKeyStr, lStyleValueStr, ADestEntity as TvEntityWithPenAndBrush);
end;
if ADestStyle <> nil then
begin
Result += ReadSVGPenStyleWithKeyAndValue(lStyleKeyStr, lStyleValueStr, nil, ADestStyle);
//Result += ReadSVGGeneralStyleWithKeyAndValue(AData, lStyleKeyStr, lStyleValueStr, nil, ADestStyle);
if AUseFillAsPen and (lStyleKeyStr = 'fill') then
Result += ReadSVGPenStyleWithKeyAndValue('stroke', lStyleValueStr, nil, ADestStyle);
Result += ReadSVGFontStyleWithKeyAndValue(lStyleKeyStr, lStyleValueStr, nil, ADestStyle);
Result += ReadSVGBrushStyleWithKeyAndValue(lStyleKeyStr, lStyleValueStr, nil, ADestStyle);
end;
end;
finally
lStrings.Free;
end;
end;
// style="fill:none;stroke:black;stroke-width:3"
function TvSVGVectorialReader.ReadSVGStyleToStyleLists(AValue: string;
AStyleKeys, AStyleValues: TStringList): TvSetPenBrushAndFontElements;
var
lStr, lStyleKeyStr, lStyleValueStr: String;
lStrings: TStringList;
i: Integer;
begin
Result := [];
if AValue = '' then Exit;
// Now split using ";" separator
lStrings := TStringList.Create;
try
lStrings.Delimiter := ';';
lStrings.DelimitedText := LowerCase(AValue);
for i := 0 to lStrings.Count-1 do
begin
lStr := lStrings.Strings[i];
SeparateStringInTwo(lStr, ':', lStyleKeyStr, lStyleValueStr);
AStyleKeys.Add(lStyleKeyStr);
AStyleValues.Add(lStyleValueStr);
end;
finally
lStrings.Free;
end;
end;
function TvSVGVectorialReader.ReadSVGPenStyleWithKeyAndValue(AKey,
AValue: string; ADestEntity: TvEntityWithPen; ADestStyle: TvStyle = nil): TvSetPenBrushAndFontElements;
var
OldAlpha: Word;
lValueInt: Int64;
begin
Result := [];
if AKey = 'stroke' then
begin
// We store and restore the old alpha to support the "-opacity" element
if ADestEntity <> nil then
begin
OldAlpha := ADestEntity.Pen.Color.Alpha;
if ADestEntity.Pen.Style = psClear then ADestEntity.Pen.Style := psSolid;
end;
if ADestStyle <> nil then
begin
OldAlpha := ADestStyle.Pen.Color.Alpha;
if ADestStyle.Pen.Style = psClear then ADestStyle.Pen.Style := psSolid;
end;
if AValue = 'none' then
begin
if ADestEntity <> nil then ADestEntity.Pen.Style := fpcanvas.psClear;
if ADestStyle <> nil then ADestStyle.Pen.Style := fpcanvas.psClear;
end
else
begin
if ADestEntity <> nil then
begin
ADestEntity.Pen.Color := ReadSVGColor(AValue);
ADestEntity.Pen.Color.Alpha := OldAlpha;
end;
if ADestStyle <> nil then
begin
ADestStyle.Pen.Color := ReadSVGColor(AValue);
ADestStyle.Pen.Color.Alpha := OldAlpha;
end;
end;
Result := Result + [spbfPenColor, spbfPenStyle];
end
else if AKey = 'stroke-width' then
begin
lValueInt := Round(StringWithUnitToFloat(AValue, sckXSize));
if ADestEntity <> nil then ADestEntity.Pen.Width := lValueInt;
if ADestStyle <> nil then ADestStyle.Pen.Width := lValueInt;
Result := Result + [spbfPenWidth];
end
else if AKey = 'stroke-opacity' then
begin
lValueInt := Round(StrToFloat(AValue, FPointSeparator)*$FFFF);
if ADestEntity <> nil then ADestEntity.Pen.Color.Alpha := lValueInt;
if ADestStyle <> nil then ADestStyle.Pen.Color.Alpha := lValueInt;
end
else if AKey = 'stroke-linecap' then
begin
{case LowerCase(AValue) of
'butt':
'round':
'square': ADestEntity.Pen;
end;}
end
else if AKey = 'stroke-dasharray' then
StringToPenPattern(AValue, ADestEntity.Pen);
end;
function TvSVGVectorialReader.ReadSVGBrushStyleWithKeyAndValue(AKey,
AValue: string; ADestEntity: TvEntityWithPenAndBrush; ADestStyle: TvStyle = nil): TvSetPenBrushAndFontElements;
var
OldAlpha: Word;
Len: Integer;
lDefName: String;
lCurBrush: TvEntityWithPenAndBrush;
lDestBrush: PvBrush;
begin
Result := [];
if ADestEntity <> nil then
begin
lDestBrush := @ADestEntity.Brush;
end
else if ADestStyle <> nil then
begin
lDestBrush := @ADestStyle;
end;
if not ((ADestEntity <> nil) xor (ADestStyle <> nil)) then
raise Exception.Create('[TvSVGVectorialReader.ReadSVGBrushStyleWithKeyAndValue] Invalid arguments');
if AKey = 'fill' then
begin
// Support for fill="url(#grad2)"
lDefName := Trim(AValue);
if Copy(lDefName, 0, 3) = 'url' then
begin
lDefName := StringReplace(AValue, 'url(#', '', []);
lDefName := StringReplace(lDefName, ')', '', []);
if lDefName = '' then Exit;
lCurBrush := FindBrushDef_WithName(lDefName);
if lCurBrush <> nil then
begin
lDestBrush^ := lCurBrush.Brush;
Exit;
end;
Exit;
end;
// We store and restore the old alpha to support the "-opacity" element
OldAlpha := lDestBrush^.Color.Alpha;
if lDestBrush^.Style = bsClear then lDestBrush^.Style := bsSolid;
if AValue = 'none' then
begin
lDestBrush^.Style := fpcanvas.bsClear;
end
else
begin
lDestBrush^.Color := ReadSVGColor(AValue);
lDestBrush^.Color.Alpha := OldAlpha;
end;
Result := Result + [spbfBrushColor, spbfBrushStyle];
end
else if AKey = 'fill-rule' then
begin
if ADestEntity <> nil then
begin
if AValue = 'evenodd' then
ADestEntity.WindingRule := vcmEvenOddRule
else if AValue = 'nonzero' then
ADestEntity.WindingRule := vcmNonzeroWindingRule; // to do: "inherit" missing here
end;
end
else if AKey = 'fill-opacity' then
begin
lDestBrush^.Color.Alpha := StringFloatZeroToOneToWord(AValue);
if lDestBrush^.Color.Alpha = 0 then
lDestBrush^.Style := bsClear;
end
// For linear gradient => stop-color:rgb(255,255,0);stop-opacity:1
else if AKey = 'stop-color' then
begin
Len := Length(lDestBrush^.Gradient_colors);
SetLength(lDestBrush^.Gradient_colors, Len+1);
lDestBrush^.Gradient_colors[Len].Color := ReadSVGColor(AValue);
end;
end;
function TvSVGVectorialReader.ReadSVGFontStyleWithKeyAndValue(AKey,
AValue: string; ADestEntity: TvEntityWithPenBrushAndFont; ADestStyle: TvStyle = nil): TvSetPenBrushAndFontElements;
var
lLowerValue: String;
p: Integer;
fntName: String;
begin
Result := [];
lLowerValue := LowerCase(AValue);
// SVG text uses "fill" to indicate the pen color of the text, very unintuitive as
// "fill" is usually for brush in other elements
if AKey = 'fill' then
begin
if ADestEntity <> nil then ADestEntity.Font.Color := ReadSVGColor(AValue);
if ADestStyle <> nil then ADestStyle.Font.Color := ReadSVGColor(AValue);
Result := Result + [spbfFontColor];
end
// But sometimes SVG also uses stroke! Oh no...
else if AKey = 'stroke' then
begin
if ADestEntity <> nil then ADestEntity.Font.Color := ReadSVGColor(AValue);
if lLowerValue <> 'none' then // sometimes we get a fill value, but a stroke=none after it...
begin
if ADestStyle <> nil then ADestStyle.Font.Color := ReadSVGColor(AValue);
Result := Result + [spbfFontColor];
end;
Result := Result + [spbfFontColor];
end
else if AKey = 'fill-opacity' then
begin
if ADestEntity <> nil then ADestEntity.Font.Color.Alpha := StrToInt(AValue)*$101;
if ADestStyle <> nil then ADestStyle.Font.Color.Alpha := StrToInt(AValue)*$101;
Result := Result + [spbfFontColor];
end
else if AKey = 'font-size' then
begin
if ADestEntity <> nil then ADestEntity.Font.Size := Round(StringWithUnitToFloat(AValue, sckXSize, suPT, suPT));
if ADestStyle <> nil then ADestStyle.Font.Size := Round(StringWithUnitToFloat(AValue, sckXSize, suPT, suPT));
Result := Result + [spbfFontSize];
end
else if AKey = 'font-family' then
begin
// Extract the font name
// To do: Check if font name exists in system. Use replacement fonts which
// may follow after the comma.
p := pos(',', AValue);
if p = 0 then
fntName := AValue else
fntName := trim(Copy(AValue, 1, p-1));
if ADestEntity <> nil then ADestEntity.Font.Name := fntName;
if ADestStyle <> nil then ADestStyle.Font.Name := fntName;
Result := Result + [spbfFontName];
end
else if AKey = 'font-weight' then
begin
case lLowerValue of
'bold', '700', '800', '900':
begin
if ADestEntity <> nil then ADestEntity.Font.Bold := True;
if ADestStyle <> nil then ADestStyle.Font.Bold := True;
end;
else
if ADestEntity <> nil then ADestEntity.Font.Bold := False;
if ADestStyle <> nil then ADestStyle.Font.Bold := False;
end;
Result := Result + [spbfFontBold];
end
// Other text attributes, non-font ones
else if AKey = 'text-anchor' then
begin
// Adjust according to the text-anchor, if necessary
case lLowerValue of
'start':
begin
if ADestEntity <> nil then ADestEntity.TextAnchor := vtaStart;
if ADestStyle <> nil then ADestStyle.TextAnchor := vtaStart;
end;
'middle':
begin
if ADestEntity <> nil then ADestEntity.TextAnchor := vtaMiddle;
if ADestStyle <> nil then ADestStyle.TextAnchor := vtaMiddle;
end;
'end':
begin
if ADestEntity <> nil then ADestEntity.TextAnchor := vtaEnd;
if ADestStyle <> nil then ADestStyle.TextAnchor := vtaEnd;
end;
end;
Result := Result + [spbfTextAnchor];
end;
if ADestStyle <> nil then
ADestStyle.SetElements := ADestStyle.SetElements + Result;
end;
procedure TvSVGVectorialReader.ReadSVGGeneralStyleWithKeyAndValue(AData: TvVectorialPage;
AKey, AValue: string; ADestEntity: TvEntity);
var
// transform
MA, MB, MC, MD, ME, MF: Double;
lMTranslateX, lMTranslateY, lMScaleX, lMScaleY, lMSkewX, lMSkewY, lMRotate: Double;
lTokenizer: TSVGPathTokenizer;
i: Integer;
lFunctionName, lParamStr: string;
var
lMatrixElements: array of Double;
begin
// Examples:
// transform="matrix(0.860815 0 -0 1.07602 339.302 489.171)"
// transform="scale(0.24) translate(0, 35)"
// transform="rotate(90)"
if AKey = 'transform' then
begin
lTokenizer := TSVGPathTokenizer.Create;
try
lTokenizer.TokenizeFunctions(AValue);
i := 0;
while i < lTokenizer.Tokens.Count-1 do
begin
lFunctionName := Trim(lTokenizer.Tokens.Items[i].StrValue);
lParamStr := lTokenizer.Tokens.Items[i+1].StrValue;
lMatrixElements := ReadSpaceSeparatedFloats(lParamStr, ',');
if lFunctionName = 'matrix' then
begin
ReadSVGTransformationMatrix(lParamStr, MA, MB, MC, MD, ME, MF);
ConvertTransformationMatrixToOperations(MA, MB, MC, MD, ME, MF,
lMTranslateX, lMTranslateY, lMScaleX, lMScaleY, lMSkewX, lMSkewY, lMRotate);
ConvertSVGDeltaToFPVDelta(nil,
lMTranslateX, lMTranslateY,
lMTranslateX, lMTranslateY);
ADestEntity.Move(lMTranslateX, lMTranslateY);
ADestEntity.Scale(lMScaleX, lMScaleY);
end
else if lFunctionName = 'scale' then
begin
;
end
else if lFunctionName = 'translate' then
begin
ConvertSVGDeltaToFPVDelta(nil,
lMatrixElements[0], lMatrixElements[1],
lMatrixElements[0], lMatrixElements[1]);
ADestEntity.Move(lMatrixElements[0], lMatrixElements[1]);
end
else if lFunctionName = 'rotate' then
begin
lMRotate := -DegToRad(lMatrixElements[0]);
// "-" because of orientation of svg coordinate system
lMTranslateX := 0;
lMTranslateY := 0;
if Length(lMatrixElements) > 1 then
lMTranslateX := lMatrixElements[1];
if Length(lMatrixElements) > 2 then
lMTranslateY := lMatrixElements[2];
ConvertSVGCoordinatesToFPVCoordinates(AData,
lMTranslateX, lMTranslateY, lMTranslateX, lMTranslateY);
ADestEntity.Rotate(lMRotate, Make3DPoint(lMTranslateX, lMTranslateY));
end;
Inc(i, 2);
end;
finally
lTokenizer.Free;
end;
end;
end;
function TvSVGVectorialReader.IsAttributeFromStyle(AStr: string): Boolean;
begin
Result :=
// pen
(AStr = 'stroke') or (AStr = 'stroke-width') or
(AStr = 'stroke-dasharray') or (AStr = 'stroke-opacity') or
(AStr = 'stroke-linecap') or
// general
(AStr = 'transform') or
// brush
(AStr = 'fill') or (AStr = 'fill-opacity') or
// font
(AStr = 'font-size') or (AStr = 'font-family') or
(AStr = 'font-weight') or (AStr = 'text-anchor');
end;
procedure TvSVGVectorialReader.ApplyLayerStyles(AData: TvVectorialPage;
ADestEntity: TvEntity);
var
lStringsKeys, lStringsValues: TStringList;
i, j: Integer;
lCurKey, lCurValue: String;
begin
for i := 0 to FLayerStylesKeys.Count-1 do
begin
lStringsKeys := TStringList(FLayerStylesKeys.Items[i]);
lStringsValues := TStringList(FLayerStylesValues.Items[i]);
for j := 0 to lStringsKeys.Count-1 do
begin
lCurKey := lStringsKeys.Strings[j];
lCurValue := lStringsValues.Strings[j];
if ADestEntity is TvEntityWithPen then
ReadSVGPenStyleWithKeyAndValue(lCurKey, lCurValue, ADestEntity as TvEntityWithPen);
// Unfortunately SVG uses 'fill' for the text color =/ so we need to hack
// our way out of this ambiguity with the brush fill
if (ADestEntity is TvEntityWithPenAndBrush) and (not (ADestEntity is TvText)) then
ReadSVGBrushStyleWithKeyAndValue(lCurKey, lCurValue, ADestEntity as TvEntityWithPenAndBrush);
if ADestEntity is TvEntityWithPenBrushAndFont then
ReadSVGFontStyleWithKeyAndValue(lCurKey, lCurValue, ADestEntity as TvEntityWithPenBrushAndFont);
// transform
ReadSVGGeneralStyleWithKeyAndValue(AData, lCurKey, lCurValue, ADestEntity);
end;
end;
end;
function TvSVGVectorialReader.ReadSpaceSeparatedFloats(AInput: string;
AOtherSeparators: string): TDoubleArray;
var
lStrings: TStringList;
lInputStr: string;
lMatrixElements: array of Double;
i: Integer;
begin
lStrings := TStringList.Create;
try
lStrings.Delimiter := ' ';
// now other separator too
lInputStr := AInput;
for i := 1 to Length(AOtherSeparators) do
begin
lInputStr := StringReplace(lInputStr, AOtherSeparators[i], ' ', [rfReplaceAll]);
end;
//
lStrings.DelimitedText := lInputStr;
SetLength(lMatrixElements, lStrings.Count);
for i := 0 to lStrings.Count-1 do
begin
lMatrixElements[i] := StringWithUnitToFloat(lStrings.Strings[i]);
end;
Result := lMatrixElements;
finally
lStrings.Free;
end;
end;
class function TvSVGVectorialReader.ReadSpaceSeparatedStrings(AInput: string;
AOtherSeparators: string): TStringList;
var
i: Integer;
lInputStr: String;
begin
Result := TStringList.Create;
Result.Delimiter := ' ';
// now other separator too
lInputStr := AInput;
for i := 1 to Length(AOtherSeparators) do
begin
lInputStr := StringReplace(lInputStr, AOtherSeparators[i], ' ', [rfReplaceAll]);
end;
//
Result.DelimitedText := lInputStr;
end;
// transform="matrix(0.860815 0 -0 1.07602 354.095 482.177)"=>matrix(a, b, c, d, e, f)
// Looks like that some Apps separate the matrix elements with spaces
// But Inkscape uses commas! transform="matrix(1.2,0,0,1.2,9.48633,-2.25781)"
// See http://apike.ca/prog_svg_transform.html
procedure TvSVGVectorialReader.ReadSVGTransformationMatrix(
AMatrix: string; out AA, AB, AC, AD, AE, AF: Double);
var
lMatrixElements: array of Double;
begin
lMatrixElements := ReadSpaceSeparatedFloats(AMatrix, ',');
AA := lMatrixElements[0];
AB := lMatrixElements[1];
AC := lMatrixElements[2];
AD := lMatrixElements[3];
AE := lMatrixElements[4];
AF := lMatrixElements[5];
end;
procedure TvSVGVectorialReader.ApplyCSSClass(AData: TvVectorialPage;
AValue: string; ADestEntity: TvEntityWithPen);
var
i, j: Integer;
lCurStyle: TSVG_CSS_Style;
lAllClasses: T10Strings;
begin
lAllClasses := SeparateString(AValue, ' ');
for i := 0 to High(lAllClasses)-1 do
begin
if lAllClasses[i] = '' then Break;
for j := 0 to FCSSDefs.Count-1 do
begin
lCurStyle := TSVG_CSS_Style(FCSSDefs.Items[j]);
if lCurStyle.MatchesClass(lAllClasses[i]) then
begin
lCurStyle.ApplyIntoEntity(ADestEntity);
Break;
end;
end;
end;
end;
function TvSVGVectorialReader.IsEntityStyleField(AFieldName: string): Boolean;
begin
Result := (AFieldName = 'style') or (AFieldName = 'class') or
IsAttributeFromStyle(AFieldName);
end;
function TvSVGVectorialReader.ReadEntityStyleField(AData: TvVectorialPage;
AFieldName, AFieldValue: string; ADestEntity: TvEntityWithPen;
ADestStyle: TvStyle; AUseFillAsPen: Boolean): TvSetPenBrushAndFontElements;
begin
case AFieldName of
'style': Result := ReadSVGStyle(AData, AFieldValue, ADestEntity, ADestStyle,
AUseFillAsPen);
'class': ApplyCSSClass(AData, AFieldValue, ADestEntity);
else
ReadSVGPenStyleWithKeyAndValue(AFieldName, AFieldValue, ADestEntity);
if ADestEntity is TvEntityWithPenAndBrush then
ReadSVGBrushStyleWithKeyAndValue(AFieldName, AFieldValue, TvEntityWithPenAndBrush(ADestEntity));
ReadSVGGeneralStyleWithKeyAndValue(AData, AFieldName, AFieldValue, ADestEntity);
end;
end;
function TvSVGVectorialReader.GetTextContentFromNode(ANode: TDOMNode): string;
var
i: Integer;
begin
Result := '';
for i := 0 to ANode.ChildNodes.Count-1 do
begin
if ANode.ChildNodes.Item[i] is TDOMText then
Result := ANode.ChildNodes.Item[i].NodeValue;
end;
end;
// <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
procedure TvSVGVectorialReader.ReadDefs_LinearGradient(ADest: TvEntityWithPenAndBrush;
ANode: TDOMNode; AData: TvVectorialPage);
var
lAttrName, lAttrValue, lNodeName: DOMString;
i, len: Integer;
lCurSubNode: TDOMNode;
lBrushEntity, lCurBrush: TvEntityWithPenAndBrush;
lGradientColor: TvGradientColor;
x1, y1, x2, y2: Double;
begin
x1 := 0;
x2 := 0;
y1 := 0;
y2 := 0;
for i := 0 to ANode.Attributes.Length - 1 do
begin
lAttrName := lowercase(ANode.Attributes.Item[i].NodeName);
lAttrValue := ANode.Attributes.Item[i].NodeValue;
if lAttrName = 'id' then
ADest.Name := lAttrValue
else
if lAttrName = 'x1' then
begin
if lAttrValue[Length(lAttrValue)] = '%' then
Include(ADest.Brush.Gradient_flags, gfRelStartX);
x1 := StringWithPercentToFloat(lAttrValue);
end else
if lAttrName = 'x2' then
begin
if lAttrValue[Length(lAttrValue)] = '%' then
Include(ADest.Brush.Gradient_flags, gfRelEndX);
x2 := StringWithPercentToFloat(lAttrValue);
end else
if lAttrName = 'y1' then
begin
if lAttrValue[Length(lAttrValue)] = '%' then
Include(ADest.Brush.Gradient_flags, gfRelStartY);
y1 := StringWithPercentToFloat(lAttrValue);
end else
if lAttrName = 'y2' then
begin
if lAttrValue[Length(lAttrValue)] = '%' then
Include(ADest.Brush.Gradient_flags, gfRelEndY);
y2 := StringWithPercentToFloat(lAttrValue);
end else
if lAttrName = 'gradientunits' then
begin
lAttrValue := LowerCase(lAttrValue);
if lAttrValue = 'userspaceonuse' then
Include(ADest.Brush.Gradient_flags, gfRelToUserSpace)
else if lAttrValue = 'objectboundingbox' then
Exclude(ADest.Brush.Gradient_flags, gfRelToUserSpace);
end;
end;
ADest.Brush.Gradient_start.X := x1;
ADest.Brush.Gradient_end.X := x2;
ADest.Brush.Gradient_start.Y := y1;
ADest.Brush.Gradient_end.Y := y2;
ConvertSVGCoordinatesToFPVCoordinates(AData, x1, y1, x1, y1);
ConvertSVGCoordinatesToFPVCoordinates(AData, x2, y2, x2, y2);
if not (gfRelStartX in ADest.Brush.Gradient_flags) then
ADest.Brush.Gradient_start.X := x1;
if not (gfRelEndX in ADest.Brush.Gradient_flags) then
ADest.Brush.Gradient_end.X := x2;
if not (gfRelStartY in ADest.Brush.Gradient_flags) then
ADest.Brush.Gradient_start.Y := y1;
if not (gfRelEndY in ADest.Brush.Gradient_flags) then
ADest.Brush.Gradient_end.Y := y2;
if (ADest.Brush.Gradient_start.X = 0) and
(ADest.Brush.Gradient_start.Y = 0) and
(ADest.Brush.Gradient_end.X = 0) and
(ADest.Brush.Gradient_end.Y = 0) then
begin
ADest.Brush.Gradient_start.X := 0.0;
ADest.Brush.Gradient_start.Y := 0.0;
ADest.Brush.Gradient_end.X := 1.0;
ADest.Brush.Gradient_end.Y := 1.0;
end;
if ADest.Brush.Gradient_start.X = ADest.Brush.Gradient_end.X then
ADest.Brush.Kind := bkVerticalGradient
else if ADest.Brush.Gradient_start.Y = ADest.Brush.Gradient_end.Y then
ADest.Brush.Kind := bkHorizontalGradient
else
ADest.Brush.Kind := bkOtherLinearGradient;
// <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
// <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
lCurSubNode := ANode.FirstChild;
while Assigned(lCurSubNode) do
begin
lNodeName := LowerCase(lCurSubNode.NodeName);
if lNodeName = 'stop' then begin
for i := 0 to lCurSubNode.Attributes.Length - 1 do
begin
lAttrName := lCurSubNode.Attributes.Item[i].NodeName;
lAttrValue := lCurSubNode.Attributes.Item[i].NodeValue;
if lAttrName = 'offset' then
lGradientColor.Position := StringWithPercentToFloat(lAttrValue)
// use as fraction 0..1
else if lAttrName = 'style' then
lGradientColor.Color := ReadSVGGradientColorStyle(lAttrValue)
else if lAttrName = 'stop-color' then
lGradientColor.Color := ReadSVGColor(lAttrValue);
end;
len := Length(ADest.Brush.Gradient_colors);
SetLength(ADest.Brush.Gradient_colors, Len+1);
ADest.Brush.Gradient_colors[len] := lGradientColor;
end;
lCurSubNode := lCurSubNode.NextSibling;
end;
end;
procedure TvSVGVectorialReader.ReadDefs_CSS(ANode: TDOMNode);
var
lContent, lCurName, lCurData: string;
lCurChar: AnsiChar;
i, lParserState: Integer;
lCurStyle: TSVG_CSS_Style;
begin
lContent := Trim(ANode.TextContent);
lContent := StringReplace(lContent, '<![CDATA[', '', []);
lContent := StringReplace(lContent, ']]>', '', []);
lContent := Trim(lContent);
lParserState := 0;
for i := 1 to Length(lContent) do
begin
lCurChar := lContent[i];
case lParserState of
0: // filling class name
begin
if lCurChar = '{' then lParserState := 1
else lCurName += lCurChar;
end;
1: // filling class data
begin
if lCurChar = '}' then
begin
lCurStyle := TSVG_CSS_Style.Create;
lCurStyle.CSSName := Trim(lCurName);
lCurStyle.CSSData := Trim(lCurData);
FCSSDefs.Add(lCurStyle);
lCurStyle.ParseCSSData(Self);
lParserState := 0;
lCurName := '';
lCurData := '';
end
else lCurData += lCurChar;
end;
end;
end;
end;
procedure TvSVGVectorialReader.ReadDefsFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
lEntityName: DOMString;
lBlock: TvBlock;
lPreviousLayer: TvEntityWithSubEntities;
lAttrName, lAttrValue, lNodeName, lEntityValue: DOMString;
lLayerName: String;
i, len: Integer;
lCurNode, lCurSubNode: TDOMNode;
lBrushEntity, lCurBrush: TvEntityWithPenAndBrush;
lCurEntity: TvEntity;
lAttrValue_Double: Double;
begin
lCurNode := ANode.FirstChild;
while Assigned(lCurNode) do
begin
lEntityName := LowerCase(lCurNode.NodeName);
lEntityValue := lCurNode.NodeValue;
case lEntityName of
'radialgradient':
begin
lBrushEntity := TvEntityWithPenAndBrush.Create(nil);
// First copy everything we can from any xlink:href
for i := 0 to lCurNode.Attributes.Length - 1 do
begin
lAttrName := LowerCase(lCurNode.Attributes.Item[i].NodeName);
lAttrValue := lCurNode.Attributes.Item[i].NodeValue;
if lAttrName = 'xlink:href' then
begin
lAttrValue := StringReplace(Trim(lAttrValue), '#', '', []);
lCurBrush := FindBrushDef_WithName(lAttrValue);
if lCurBrush <> nil then
lBrushEntity.Brush := lCurBrush.Brush;
end;
end;
// Now linear gradient properties
ReadDefs_LinearGradient(lBrushEntity, lCurNode, AData);
// Now process our own properties
lBrushEntity.Brush.Kind := bkRadialGradient;
// <radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
// or
// <linearGradient id="linearGradient6038">
// <stop style="stop-color:#ffffff;stop-opacity:1;" offset="0" id="stop6040" />
// <stop style="stop-color:#000000;stop-opacity:0;" offset="1" id="stop6042" />
// </linearGradient>
// <radialGradient inkscape:collect="always" xlink:href="#linearGradient6038"
// id="radialGradient3333" gradientUnits="userSpaceOnUse"
// gradientTransform="matrix(0.05999054,1.120093,-1.0249935,0.05489716,995.9708,109.4759)"
// cx="406.62762" cy="567.12799" fx="406.62762" fy="567.12799" r="37.15749" />
for i := 0 to lCurNode.Attributes.Length - 1 do
begin
lAttrName := lCurNode.Attributes.Item[i].NodeName;
lAttrValue := lCurNode.Attributes.Item[i].NodeValue;
case lAttrName of
'cx', 'cy', 'fx', 'fy', 'r':
lAttrValue_Double := StringWithUnitToFloat(lAttrValue, sckUnknown, suMM, suMM);
end;
case lAttrName of
'id': lBrushEntity.Name := lAttrValue;
'cx': lBrushEntity.Brush.Gradient_cx := lAttrValue_Double;
'cy': lBrushEntity.Brush.Gradient_cy := lAttrValue_Double;
'r': lBrushEntity.Brush.Gradient_r := lAttrValue_Double;
'fx': lBrushEntity.Brush.Gradient_fx := lAttrValue_Double;
'fy': lBrushEntity.Brush.Gradient_fy := lAttrValue_Double;
end;
//lBrushEntity.Gradient_cx_Unit, Gradient_cy_Unit, Gradient_r_Unit, Gradient_fx_Unit, Gradient_fy_Unit
end;
FBrushDefs.Add(lBrushEntity);
end;
{
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
}
'lineargradient':
begin
lBrushEntity := TvEntityWithPenAndBrush.Create(nil);
ReadDefs_LinearGradient(lBrushEntity, lCurNode, AData);
FBrushDefs.Add(lBrushEntity);
end;
// Sometime entities are also put in the defs
'circle', 'ellipse', 'g', 'line', 'path',
'polygon', 'polyline', 'rect', 'text', 'use', 'symbol':
begin
lLayerName := '';
lBlock := TvBlock.Create(nil);
// pre-load attribute reader, to get the block name
for i := 0 to lCurNode.Attributes.Length - 1 do
begin
lAttrName := lCurNode.Attributes.Item[i].NodeName;
if lAttrName = 'id' then
begin
lLayerName := lCurNode.Attributes.Item[i].NodeValue;
lBlock.Name := lLayerName;
end;
end;
// Skip g without id, create instead items for his sub-items
if (lEntityName = 'g') and (lLayerName = '') then
begin
lCurSubNode := lCurNode.FirstChild;
while Assigned(lCurSubNode) do
begin
lCurEntity := ReadEntityFromNode(lCurSubNode, AData, ADoc);
if lCurEntity <> nil then
AData.AddEntity(lCurEntity);
lCurSubNode := lCurSubNode.NextSibling;
end;
lBlock.Free;
lCurNode := lCurNode.NextSibling;
Continue;
end;
lPreviousLayer := AData.GetCurrentLayer();
AData.AddEntity(lBlock);
AData.SetCurrentLayer(lBlock);
//
lCurEntity := ReadEntityFromNode(lCurNode, AData, ADoc);
if lCurEntity <> nil then
AData.AddEntity(lCurEntity);
//
AData.SetCurrentLayer(lPreviousLayer);
end;
// CSS styles
'style':
begin
for i := 0 to lCurNode.Attributes.Length - 1 do
begin
lAttrName := LowerCase(lCurNode.Attributes.Item[i].NodeName);
lAttrValue := lCurNode.Attributes.Item[i].NodeValue;
if (lAttrName = 'type') and (lAttrValue = 'text/css') then
begin
ReadDefs_CSS(lCurNode);
end;
end;
end;
end;
lCurNode := lCurNode.NextSibling;
end;
end;
function TvSVGVectorialReader.ReadEntityFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lEntityName: DOMString;
begin
Result := nil;
lEntityName := LowerCase(ANode.NodeName);
case lEntityName of
'circle': Result := ReadCircleFromNode(ANode, AData, ADoc);
'defs': ReadDefsFromNode(ANode, AData, ADoc);
'ellipse': Result := ReadEllipseFromNode(ANode, AData, ADoc);
'frame': Result := ReadFrameFromNode(ANode, AData, ADoc);
'g': ReadLayerFromNode(ANode, AData, ADoc);
'image': Result := ReadImageFromNode(ANode, AData, ADoc);
'line': Result := ReadLineFromNode(ANode, AData, ADoc);
'path': Result := ReadPathFromNode(ANode, AData, ADoc);
'polygon', 'polyline': Result := ReadPolyFromNode(ANode, AData, ADoc);
'rect': Result := ReadRectFromNode(ANode, AData, ADoc);
'symbol': Result := ReadSymbolFromNode(ANode, AData, ADoc);
'text': Result := ReadTextFromNode(ANode, AData, ADoc);
'use': Result := ReadUseFromNode(ANode, AData, ADoc);
end;
end;
function TvSVGVectorialReader.ReadCircleFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
cx, cy, cr, tmp: double;
lCircle: TvCircle;
i: Integer;
lNodeName, lNodeValue: DOMString;
begin
cx := 0.0;
cy := 0.0;
cr := 0.0;
lCircle := TvCircle.Create(nil);
// SVG entities start without any pen drawing, but with a black brush
lCircle.Pen.Style := psClear;
lCircle.Brush.Style := bsSolid;
lCircle.Brush.Color := colBlack;
// Apply the layer style
ApplyLayerStyles(AData, lCircle);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'cx' then
cx := StringWithUnitToFloat(lNodeValue) //, sckX, suPX, suMM)
else if lNodeName = 'cy' then
cy := StringWithUnitToFloat(lNodeValue) //, sckY, suPX, suMM)
else if lNodeName = 'r' then
cr := StringWithUnitToFloat(lNodeValue) //, sckXSize, suPX, suMM)
else if lNodeName = 'id' then
lCircle.Name := lNodeValue;
end;
ConvertSVGCoordinatesToFPVCoordinates(AData, cx, cy, cx, cy);
ConvertSVGSizeToFPVSize(AData, cr, cr, lCircle.Radius, tmp);
lCircle.X := lCircle.X + cx;
lCircle.Y := lCircle.Y + cy;
// Make sure that transformations are read after geometry and position
// of cirlce is known.
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'style' then
ReadSVGStyle(AData, lNodeValue, lCircle)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGPenStyleWithKeyAndValue(lNodeName, lNodeValue, lCircle);
ReadSVGBrushStyleWithKeyAndValue(lNodeName, lNodeValue, lCircle);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName, lNodeValue, lCircle);
end;
end;
Result := lCircle;
end;
function TvSVGVectorialReader.ReadEllipseFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
cx, cy, crx, cry: double;
lEllipse: TvEllipse;
i: Integer;
lNodeName, lNodeValue: DOMString;
begin
cx := 0.0;
cy := 0.0;
crx := 0.0;
cry := 0.0;
lEllipse := TvEllipse.Create(nil);
// SVG entities start without any pen drawing, but with a black brush
lEllipse.Pen.Style := psClear;
lEllipse.Brush.Style := bsSolid;
lEllipse.Brush.Color := colBlack;
// Apply the layer style
ApplyLayerStyles(AData, lEllipse);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'cx' then
cx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'cy' then
cy := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'rx' then
crx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'ry' then
cry := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'id' then
lEllipse.Name := ANode.Attributes.Item[i].NodeValue;
end;
ConvertSVGCoordinatesToFPVCoordinates(AData, cx, cy, lEllipse.X, lEllipse.Y);
ConvertSVGSizeToFPVSize(AData, crx, cry, lEllipse.HorzHalfAxis, lEllipse.VertHalfAxis);
// Make sure that transformations are read after geometry and position
// of ellipse is known.
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'style' then
ReadSVGStyle(AData, lNodeValue, lEllipse)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGPenStyleWithKeyAndValue(lNodeName, lNodeValue, lEllipse);
ReadSVGBrushStyleWithKeyAndValue(lNodeName, lNodeValue, lEllipse);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName, lNodeValue, lEllipse);
end;
end;
Result := lEllipse;
end;
{
<draw:frame draw:style-name="gr5" draw:layer="layout" svg:width="9.024cm" svg:height="0.963cm"
draw:transform="rotate (-1.58737695468884) translate (2.3cm 1.197cm)">
<draw:text-box>
<text:p>Jump opposite arm and leg up</text:p>
</draw:text-box>
</draw:frame>
<draw:frame draw:style-name="gr5" draw:layer="layout" svg:width="15.07cm" svg:height="1.115cm" svg:x="2.6cm" svg:y="26.9cm">
<draw:text-box>
<text:p>
<text:span text:style-name="T1">Back muscle movement</text:span>
<text:span text:style-name="T2">opposite</text:span>
<text:span text:style-name="T3">arm</text:span>
and
<text:span text:style-name="T3">leg</text:span>
up
</text:p>
</draw:text-box>
</draw:frame>
}
function TvSVGVectorialReader.ReadFrameFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lTextStr: string = '';
lx, ly: double;
lText: TvText;
i: Integer;
lNodeName, lNodeValue, lSubNodeName, lSubNodeValue: DOMString;
lCurNode, lCurSubNode: TDOMNode;
begin
lx := 0.0;
ly := 0.0;
Result := nil;
lText := nil;//TvText.Create(nil);
// Apply the layer style
ApplyLayerStyles(AData, lText);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'svg:x' then
lx := lx + StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'svg:y' then
ly := ly + StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'draw:style-name' then
ReadSVGStyle(AData, lNodeValue, lText);
end;
// Get the text contents
lCurNode := Anode.FirstChild;
while lCurNode <> nil do
begin
lNodeName := LowerCase(lCurNode.NodeName);
if lNodeName <> 'draw:text-box' then Continue;
lCurSubNode := lCurNode.FirstChild;
while lCurSubNode <> nil do
begin
lSubNodeName := LowerCase(lCurSubNode.NodeName);
if lSubNodeName <> 'draw:text-box' then Continue;
lText := ReadFrameTextFromNode(lCurNode, AData, ADoc) as TvText;
Break;
lCurSubNode := lCurSubNode.NextSibling;
end;
if lText <> nil then Break;
lCurNode := lCurNode.NextSibling;
end;
if lText = nil then Exit;
// Set the coordinates
ConvertSVGCoordinatesToFPVCoordinates(
AData, lx, ly, lText.X, lText.Y);
// Finalization
Result := lText;
end;
{
<text:p>Jump opposite arm and leg up</text:p>
<text:p>
<text:span text:style-name="T1">Back muscle movement</text:span>
<text:span text:style-name="T2">opposite</text:span>
<text:span text:style-name="T3">arm</text:span>
and
<text:span text:style-name="T3">leg</text:span>
up
</text:p>
}
function TvSVGVectorialReader.ReadFrameTextFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lTextStr: string = '';
lx, ly: double;
lText: TvText;
//i: Integer;
//lNodeName, lNodeValue: DOMString;
//lCurNode: TDOMNode;
begin
lx := 0.0;
ly := 0.0;
lText := TvText.Create(nil);
// Apply the layer style
ApplyLayerStyles(AData, lText);
{// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'x' then
lx := lx + StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'y' then
ly := ly + StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'id' then
lText.Name := lNodeValue
else if lNodeName = 'style' then
ReadSVGStyle(lNodeValue, lText);
end;}
// The text contents are inside as a child text, not as a attribute
// ex: <text x="0" y="15" fill="red" transform="rotate(30 20,40)">I love SVG</text>
if Anode.FirstChild <> nil then
lTextStr := Anode.FirstChild.NodeValue;
// Add the first line
lText.Value.Add(lTextStr);
// Recover the position if there was a transformation matrix
//lx := lx + lText.X;
//ly := ly + lText.Y;
// Set the coordinates
ConvertSVGCoordinatesToFPVCoordinates(
AData, lx, ly, lText.X, lText.Y);
// Now add other lines, which appear as <tspan ...>another line</tspan>
// Example:
// <text x="10" y="20" style="fill:red;">Several lines:
// <tspan x="10" y="45">First line</tspan>
// <tspan x="10" y="70">Second line</tspan>
// </text>
// These other lines can be positioned, so they need to appear as independent TvText elements
{ lCurNode := Anode.FirstChild;
while lCurNode <> nil do
begin
lNodeName := LowerCase(lCurNode.NodeName);
if lNodeName <> 'tspan' then Continue;
ReadTextFromNode(lCurNode, AData, ADoc);
lCurNode := lCurNode.NextSibling;
end;}
// Finalization
Result := lText;
end;
// <image width="92.5" x="0" y="0" height="76.0429"
// xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKMAAACGCAYAA
// ACxDToF......" clip-path="url(#Clip0)" transform="matrix(1 0 0 1 0 0)"/>
function TvSVGVectorialReader.ReadImageFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lImage: TvRasterImage;
lx, ly, lw, lh: Double;
lTransformX, lTransformY, lTransformW, lTransformH: Double;
i: Integer;
lNodeName, lNodeValue: DOMString;
lImageDataParts: TStringList;
lImageDataBase64: string;
lImageData: array of Byte;
lImageDataStream: TMemoryStream;
lImageReader: TFPCustomImageReader;
begin
lImage := TvRasterImage.Create(nil);
lx := 0;
ly := 0;
lw := 0;
lh := 0;
lImage.Width := 1000;
lImage.Height := 1000;
// Apply the layer style
//ApplyLayerStyles(lEllipse);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'x' then
lx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'y' then
ly := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'width' then
lw := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'height' then
lh := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'xlink:href' then
begin
lImageDataParts := ReadSpaceSeparatedStrings(lNodeValue, ':;,');
try
if (lImageDataParts.Strings[0] = 'data') and
(lImageDataParts.Strings[1] = 'image/png') and
(lImageDataParts.Strings[2] = 'base64') then
begin
lImageReader := TFPReaderPNG.Create;
lImageDataStream := TMemoryStream.Create;
try
lImageDataBase64 := lImageDataParts.Strings[3];
DecodeBase64(lImageDataBase64, lImageDataStream);
lImageDataStream.Position := 0;
lImage.CreateRGB888Image(10, 10);
lImage.RasterImage.LoadFromStream(lImageDataStream, lImageReader);
finally
lImageDataStream.Free;
lImageReader.Free;
end;
end
else
raise Exception.Create('[TvSVGVectorialReader.ReadImageFromNode] Unimplemented image format');
finally
lImageDataParts.Free;
end;
end
else if lNodeName = 'id' then
lImage.Name := lNodeValue
{ else if lNodeName = 'style' then
ReadSVGStyle(lNodeValue, lImage)}
else if IsAttributeFromStyle(lNodeName) then
begin
//ReadSVGPenStyleWithKeyAndValue(lNodeName, lNodeValue, lImage);
//ReadSVGBrushStyleWithKeyAndValue(lNodeName, lNodeValue, lImage);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName, lNodeValue, lImage);
end;
end;
// Record the transform data
lTransformX := lImage.X;
lTransformY := lImage.Y;
lTransformW := lImage.Width / 1000;
lTransformH := lImage.Height / 1000;
ConvertSVGCoordinatesToFPVCoordinates(
AData, lx, ly, lImage.X, lImage.Y);
ConvertSVGDeltaToFPVDelta(
AData, lw, lh, lImage.Width, lImage.Height);
// And re-apply the transform data
lImage.X := lImage.X + lTransformX;
lImage.Y := lImage.Y + lTransformY;
lImage.Width := lImage.Width * lTransformW;
lImage.Height := lImage.Height * lTransformH;
// Strange hack: No idea why it works ... but helped wmtboc
lImage.X := lImage.X - lImage.Width;
lImage.Y := lImage.Y + lImage.Height / 2;
// Apply the layer style
ApplyLayerStyles(AData, lImage);
Result := lImage;
end;
procedure TvSVGVectorialReader.ReadLayerFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument);
var
lNodeName, lNodeValue: DOMString;
lLayerName: string = '';
lCurNode, lLayerNameNode: TDOMNode;
lLayer: TvLayer;
lParentLayer: TvEntityWithSubEntities;
i: Integer;
{$ifdef SVG_MERGE_LAYER_STYLES}
lLayerStyleKeys, lLayerStyleValues: TStringList;
lCurEntity: TvEntity;
{$endif}
begin
// Store the style of this layer in the list
{$ifdef SVG_MERGE_LAYER_STYLES}
lLayerStyleKeys := TStringList.Create;
lLayerStyleValues := TStringList.Create;
FLayerStylesKeys.Add(lLayerStyleKeys);
FLayerStylesValues.Add(lLayerStyleValues);
{$endif}
// first attribute reader, there is a second one
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'id' then
lLayerName := ANode.Attributes.Item[i].NodeValue;
end;
lParentLayer := AData.GetCurrentLayer();
lLayer := AData.AddLayerAndSetAsCurrent(lLayerName);
// attribute reading again after getting the object
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'style' then
begin
{$ifdef SVG_MERGE_LAYER_STYLES}
ReadSVGStyleToStyleLists(lNodeValue, lLayerStyleKeys, lLayerStyleValues);
{$else}
lLayer.SetPenBrushAndFontElements += ReadSVGStyle(lNodeValue, lLayer)
{$endif}
end
else if IsAttributeFromStyle(lNodeName) then
begin
{$ifdef SVG_MERGE_LAYER_STYLES}
lLayerStyleKeys.Add(lNodeName);
lLayerStyleValues.Add(lNodeValue);
{$else}
lLayer.SetPenBrushAndFontElements += ReadSVGPenStyleWithKeyAndValue(lNodeName, lNodeValue, lLayer);
lLayer.SetPenBrushAndFontElements += ReadSVGBrushStyleWithKeyAndValue(lNodeName, lNodeValue, lLayer);
lLayer.SetPenBrushAndFontElements += ReadSVGFontStyleWithKeyAndValue(lNodeName, lNodeValue, lLayer);
{$endif}
end;
end;
lCurNode := ANode.FirstChild;
while Assigned(lCurNode) do
begin
lCurEntity := ReadEntityFromNode(lCurNode, AData, ADoc);
if lCurEntity <> nil then
AData.AddEntity(lCurEntity);
lCurNode := lCurNode.NextSibling;
end;
{$ifdef SVG_MERGE_LAYER_STYLES}
// Now remove the style from this layer
FLayerStylesKeys.Remove(lLayerStyleKeys);
lLayerStyleKeys.Free;
FLayerStylesValues.Remove(lLayerStyleValues);
lLayerStyleValues.Free;
{$endif}
// Set the current layer to the parent node,
// or else items read next will be put as children of this layer
AData.SetCurrentLayer(lParentLayer);
end;
function TvSVGVectorialReader.ReadLineFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
x1, y1, x2, y2: double;
vx1, vy1, vx2, vy2: double;
i: Integer;
lNodeName: DOMString;
lPath: TPath;
lStyleStr: DOMString;
begin
x1 := 0.0;
y1 := 0.0;
x2 := 0.0;
y2 := 0.0;
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'x1' then
x1 := StringWithUnitToFloat(ANode.Attributes.Item[i].NodeValue)
else if lNodeName = 'y1' then
y1 := StringWithUnitToFloat(ANode.Attributes.Item[i].NodeValue)
else if lNodeName = 'x2' then
x2 := StringWithUnitToFloat(ANode.Attributes.Item[i].NodeValue)
else if lNodeName = 'y2' then
y2 := StringWithUnitToFloat(ANode.Attributes.Item[i].NodeValue);
end;
ConvertSVGCoordinatesToFPVCoordinates(
AData, x1, y1, vx1, vy1);
ConvertSVGCoordinatesToFPVCoordinates(
AData, x2, y2, vx2, vy2);
AData.StartPath();
AData.AddMoveToPath(vx1, vy1);
AData.AddLineToPath(vx2, vy2);
lPath := AData.EndPath(True);
// Add default SVG pen/brush
lPath.Pen.Style := psClear;
// Apply the layer style
ApplyLayerStyles(AData, lPath);
// Add the entity styles
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'style' then
ReadSVGStyle(AData, ANode.Attributes.Item[i].NodeValue, lPath)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGPenStyleWithKeyAndValue(lNodeName,
ANode.Attributes.Item[i].NodeValue, lPath);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName,
ANode.Attributes.Item[i].NodeValue, lPath);
end;
end;
//
Result := lPath;
end;
function TvSVGVectorialReader.ReadPathFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lNodeName, lDStr: WideString;
i, j: Integer;
lCurPath: TPath;
lPaths: TvSVGPathList;
begin
Result := nil;
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'd' then
lDStr := ANode.Attributes.Item[i].NodeValue;
end;
Inc(FPathNumber);
FSVGPathTokenizer.ExtraDebugStr := Format(' [TvSVGVectorialReader.ReadPathFromNode] path#(1-based)=%d', [FPathNumber]);
lPaths := ReadPathFromString(UTF8Encode(lDStr), AData, ADoc);
FSVGPathTokenizer.ExtraDebugStr := '';
for j := 0 to lPaths.Count-1 do
begin
lCurPath := lPaths.GetPath(j);
// Add default SVG pen/brush
lCurPath.Pen.Style := psClear;
lCurPath.Brush.Color := colBlack;
lCurPath.Brush.Style := bsClear;
// Apply the layer style
ApplyLayerStyles(AData, lCurPath);
// name
if lPaths.Count > 1 then
lCurPath.Name := Format('[%d]', [i]);
// Add the pen/brush/name
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'id' then
begin
if lPaths.Count = 1 then
lCurPath.Name := ANode.Attributes.Item[i].NodeValue;
end
else if lNodeName = 'style' then
ReadSVGStyle(AData, ANode.Attributes.Item[i].NodeValue, lCurPath)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGPenStyleWithKeyAndValue(lNodeName,
ANode.Attributes.Item[i].NodeValue, lCurPath);
ReadSVGBrushStyleWithKeyAndValue(lNodeName,
ANode.Attributes.Item[i].NodeValue, lCurPath);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName,
ANode.Attributes.Item[i].NodeValue, lCurPath);
end;
end;
end;
if lPaths.Count = 1 then
begin
Result := lPaths.GetPath(0);
end
else if lPaths.Count > 1 then
begin
Result := TvEntityWithSubEntities.Create(nil);
for j := 0 to lPaths.Count-1 do
begin
lCurPath := lPaths.GetPath(j);
TvEntityWithSubEntities(Result).AddEntity(lCurPath);
end;
// Add thename
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'id' then
TvEntityWithSubEntities(Result).Name := ANode.Attributes.Item[i].NodeValue;
end;
end;
end;
// Documentation: http://www.w3.org/TR/SVG/paths.html
function TvSVGVectorialReader.ReadPathFromString(AStr: string;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvSVGPathList;
var
lCurTokenType, lLastCommandToken: TSVGTokenType;
lTmpTokenType: TSVGTokenType;
begin
Result := TvSVGPathList.Create;
FSVGPathTokenizer.ClearTokens;
FSVGPathTokenizer.TokenizePathString(AStr);
//lDebugStr := FSVGPathTokenizer.DebugOutTokensAsString();
Result.IsFirstPathMove := true;
Result.Data := AData;
Result.Doc := ADoc;
lLastCommandToken := sttFloatValue;
AData.StartPath();
Result.CurTokenIndex := 0;
while Result.CurTokenIndex < FSVGPathTokenizer.Tokens.Count do
begin
Result.LastPathClosed := False;
lCurTokenType := FSVGPathTokenizer.Tokens.Items[Result.CurTokenIndex].TokenType;
if not (lCurTokenType = sttFloatValue) then
begin
lLastCommandToken := lCurTokenType;
ReadNextPathCommand(lCurTokenType, Result, Result.CurX, Result.CurY);
end
// In this case we are getting a command without a starting letter
// It is a copy of the last one, or something related to it
else
begin
lTmpTokenType := lLastCommandToken;
if lLastCommandToken = sttMoveTo then lTmpTokenType := sttLineTo;
if lLastCommandToken = sttRelativeMoveTo then lTmpTokenType := sttRelativeLineTo;
// For bezier I checked that a sttBezierTo upon repetition expects a sttBezierTo
Dec(Result.CurTokenIndex);// because there is no command token in this command
ReadNextPathCommand(lTmpTokenType, Result, Result.CurX, Result.CurY);
end;
end;
if not Result.LastPathClosed then
Result.Add(AData.EndPath(True));
end;
procedure TvSVGVectorialReader.ReadNextPathCommand(ACurTokenType: TSVGTokenType;
APaths: TvSVGPathList; var CurX, CurY: Double);
var
X, Y, X2, Y2, X3, Y3, XQ, YQ, Xnew, Ynew, cx, cy, phi, tmp: Double;
PathEndX, PathEndY: Double;
LargeArcFlag, SweepFlag, LeftmostEllipse, ClockwiseArc: Boolean;
lCurTokenType: TSVGTokenType;
lToken5Before, lToken7Before: TSVGTokenType;
lCorrectPreviousToken: Boolean;
lPrevRelative, lCurRelative: Boolean;
AData: TvVectorialPage;
i: Integer;
begin
lCurTokenType := ACurTokenType;
AData := APaths.Data;
i := APaths.CurTokenIndex;
// --------------
// Moves
// --------------
if lCurTokenType in [sttMoveTo, sttRelativeMoveTo] then
begin
// The point at which the polygon starts.
X := FSVGPathTokenizer.Tokens.Items[i+1].Value;
Y := FSVGPathTokenizer.Tokens.Items[i+2].Value;
// take care of relative or absolute
// Idiotism in SVG: If the path starts with a relative move to,
// the coordinates are absolute =o source: http://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands
if (lCurTokenType = sttRelativeMoveTo) and (i > 0) then
begin
ConvertSVGDeltaToFPVDelta(AData, X, Y, X, Y);
CurX := CurX + X;
CurY := CurY + Y;
end
else
begin
ConvertSVGCoordinatesToFPVCoordinates(AData, X, Y, X, Y);
CurX := X;
CurY := Y;
end;
APaths.Data.AddMoveToPath(CurX, CurY);
// Since there may be several subpolygons we must store the start point
// to close the subpolygon correctly later.
if APaths.IsFirstPathMove then
begin
APaths.LastPathStart.X := APaths.CurX;
APaths.LastPathStart.Y := APaths.CurY;
APaths.IsFirstPathMove := False;
APaths.LastPathClosed := False;
end;
Inc(APaths.CurTokenIndex, 3);
end
// --------------
// Close Path
// --------------
else if lCurTokenType = sttClosePath then
begin
// Repeat the first point of the subpolygon
PathEndX := APaths.LastPathStart.X;
PathEndY := APaths.LastPathStart.Y;
CurX := PathEndX;
CurY := PathEndY;
APaths.LastPathStart.X := 0;
APaths.LastPathStart.Y := 0;
APaths.Data.AddLineToPath(PathEndX, PathEndY);
{$IFDEF FPVECTORIAL_SVG_SPLIT_PATHS}
APaths.LastPathClosed := True;
APaths.IsFirstPathMove := True;
APaths.Add(AData.EndPath(True));
AData.StartPath();
{$ENDIF}
Inc(APaths.CurTokenIndex, 1);
end
// --------------
// Lines
// --------------
else if lCurTokenType in [sttLineTo, sttRelativeLineTo, sttHorzLineTo,
sttRelativeHorzLineTo, sttVertLineTo, sttRelativeVertLineTo] then
begin
if lCurTokenType in [sttLineTo, sttRelativeLineTo] then
begin
X := FSVGPathTokenizer.Tokens.Items[i+1].Value;
Y := FSVGPathTokenizer.Tokens.Items[i+2].Value;
if lCurTokenType = sttLineTo then
ConvertSVGCoordinatesToFPVCoordinates(APaths.Data, X,Y, CurX,CurY)
else
begin
ConvertSVGDeltaToFPVDelta(APaths.Data, X,Y, X,Y);
CurX := CurX + X;
CurY := CurY + Y;
end;
inc(APaths.CurTokenIndex, 3);
end
else if lCurTokenType in [sttHorzLineTo, sttVertLineTo] then
begin
tmp := FSVGPathTokenizer.Tokens.Items[i+1].Value;
ConvertSVGCoordinatesToFPVCoordinates(APaths.Data, tmp, tmp, X, Y);
if lCurTokenType = sttHorzLineTo then
CurX := X
else
CurY := Y;
inc(APaths.CurTokenIndex, 2);
end else
if lCurTokenType in [sttRelativeHorzLineTo, sttRelativeVertLineTo] then
begin
tmp := FSVGPathTokenizer.Tokens.Items[i+1].Value;
ConvertSVGDeltaToFPVDelta(APaths.Data, tmp, tmp, X, Y);
if lCurTokenType = sttRelativeHorzLineTo then
CurX := CurX + X
else
CurY := CurY + Y;
inc(APaths.CurTokenIndex, 2);
end;
APaths.Data.AddLineToPath(CurX, CurY);
end
// --------------
// Cubic Bezier
// --------------
else if lCurTokenType in [sttBezierTo, sttRelativeBezierTo,
sttSmoothBezierTo, sttRelativeSmoothBezierTo] then
begin
lPrevRelative := false;
if lCurTokenType in [sttBezierTo, sttRelativeBezierTo] then
begin
X2 := FSVGPathTokenizer.Tokens.Items[i+1].Value;
Y2 := FSVGPathTokenizer.Tokens.Items[i+2].Value;
X3 := FSVGPathTokenizer.Tokens.Items[i+3].Value;
Y3 := FSVGPathTokenizer.Tokens.Items[i+4].Value;
X := FSVGPathTokenizer.Tokens.Items[i+5].Value;
Y := FSVGPathTokenizer.Tokens.Items[i+6].Value;
end
else
begin
// Calculation found here: http://stackoverflow.com/questions/5287559/calculating-control-points-for-a-shorthand-smooth-svg-path-bezier-curve
// Description here: http://www.w3.org/TR/SVG/paths.html#PathDataCurveCommands
//
// M X0, Y0 C X1, Y1 X2, Y2 X3, Y3 S X4, Y4 X5, Y5
// Missing control points for S is:
// XR = 2*X3 - X2 and
// YR = 2*Y3 - Y2
if i >= 7 then
begin
lToken5Before := FSVGPathTokenizer.Tokens.Items[i-5].TokenType;
lToken7Before := FSVGPathTokenizer.Tokens.Items[i-7].TokenType;
lCorrectPreviousToken := lToken5Before in [sttSmoothBezierTo, sttRelativeSmoothBezierTo];
lCorrectPreviousToken := lCorrectPreviousToken or
(lToken7Before in [sttBezierTo, sttRelativeBezierTo]);
lPrevRelative := (lToken5Before = sttRelativeSmoothBezierTo) or (lToken7Before = sttRelativeBezierTo);
end;
if (i >= 7) and (lCorrectPreviousToken) then
begin
if (lCurTokenType = sttRelativeSmoothBezierTo) or lPrevRelative then
begin
X2 := FSVGPathTokenizer.Tokens.Items[i-2].Value - FSVGPathTokenizer.Tokens.Items[i-4].Value;
Y2 := FSVGPathTokenizer.Tokens.Items[i-1].Value - FSVGPathTokenizer.Tokens.Items[i-3].Value;
end
else
begin
X2 := 2*FSVGPathTokenizer.Tokens.Items[i-2].Value - FSVGPathTokenizer.Tokens.Items[i-4].Value;
Y2 := 2*FSVGPathTokenizer.Tokens.Items[i-1].Value - FSVGPathTokenizer.Tokens.Items[i-3].Value;
end;
end;
// Now the non-missing items
X3 := FSVGPathTokenizer.Tokens.Items[i+1].Value;
Y3 := FSVGPathTokenizer.Tokens.Items[i+2].Value;
X := FSVGPathTokenizer.Tokens.Items[i+3].Value;
Y := FSVGPathTokenizer.Tokens.Items[i+4].Value;
end;
// Careful that absolute coordinates require using ConvertSVGCoordinatesToFPVCoordinates
lCurRelative := lCurTokenType in [sttRelativeBezierTo, sttRelativeSmoothBezierTo];
if lPrevRelative then
begin
ConvertSVGDeltaToFPVDelta(AData, X2, Y2, X2, Y2);
if lCurRelative then
begin
ConvertSVGDeltaToFPVDelta(AData, X3, Y3, X3, Y3);
ConvertSVGDeltaToFPVDelta(AData, X, Y, X, Y);
end else
begin
ConvertSVGCoordinatesToFPVCoordinates(AData, X3, Y3, X3, Y3);
ConvertSVGCoordinatesToFPVCoordinates(AData, X, Y, X, Y);
end;
end else
begin
if lCurRelative then
begin
ConvertSVGDeltaToFPVDelta(AData, X2, Y2, X2, Y2);
ConvertSVGDeltaToFPVDelta(AData, X3, Y3, X3, Y3);
ConvertSVGDeltaToFPVDelta(AData, X, Y, X, Y);
end else
begin
ConvertSVGCoordinatesToFPVCoordinates(AData, X2, Y2, X2, Y2);
ConvertSVGCoordinatesToFPVCoordinates(AData, X3, Y3, X3, Y3);
ConvertSVGCoordinatesToFPVCoordinates(AData, X, Y, X, Y);
end;
end;
// Covers the case where there is no valid first control point in smooth bezier
// The code is here to be after the conversions
if (lCurTokenType in [sttSmoothBezierTo, sttRelativeSmoothBezierTo]) and
((i < 7) or (not lCorrectPreviousToken)) then
begin
if lCurTokenType = sttRelativeSmoothBezierTo then
begin
X2 := CurX;
Y2 := CurY;
end
else
begin
X2 := 0;
Y2 := 0;
end;
end;
// The final step
if lCurTokenType in [sttRelativeBezierTo, sttRelativeSmoothBezierTo] then
begin
AData.AddBezierToPath(X2 + CurX, Y2 + CurY, X3 + CurX, Y3 + CurY, X + CurX, Y + CurY);
CurX := CurX + X;
CurY := CurY + Y;
end
else
begin
if lPrevRelative then
AData.AddBezierToPath(X2 + CurX, Y2 + CurY, X3, Y3, X, Y)
else
AData.AddBezierToPath(X2, Y2, X3, Y3, X, Y);
CurX := X;
CurY := Y;
end;
if lCurTokenType in [sttBezierTo, sttRelativeBezierTo] then
Inc(APaths.CurTokenIndex, 7)
else
Inc(APaths.CurTokenIndex, 5);
end
// --------------
// Quadratic Bezier
// --------------
else if lCurTokenType in [sttQuadraticBezierTo, sttRelativeQuadraticBezierTo] then
begin
XQ := FSVGPathTokenizer.Tokens.Items[i+1].Value;
YQ := FSVGPathTokenizer.Tokens.Items[i+2].Value;
X := FSVGPathTokenizer.Tokens.Items[i+3].Value;
Y := FSVGPathTokenizer.Tokens.Items[i+4].Value;
// Careful that absolute coordinates require using ConvertSVGCoordinatesToFPVCoordinates
if lCurTokenType in [sttRelativeQuadraticBezierTo] then
begin
ConvertSVGDeltaToFPVDelta(APaths.Data, XQ, YQ, XQ, YQ);
ConvertSVGDeltaToFPVDelta(APaths.Data, X, Y, X, Y);
XQ := XQ + CurX;
YQ := YQ + CurY;
X := X + CurX;
Y := Y + CurY;
end
else
begin
ConvertSVGCoordinatesToFPVCoordinates(APaths.Data, XQ, YQ, XQ, YQ);
ConvertSVGCoordinatesToFPVCoordinates(APaths.Data, X, Y, X, Y);
end;
// Convert quadratic to cubic bezier
// CP1 = QP0 + 2/3 *(QP1-QP0)
// CP2 = QP2 + 2/3 *(QP1-QP2)
// See http://stackoverflow.com/questions/3162645/convert-a-quadratic-bezier-to-a-cubic
// Just CP1=CP2=QP1 does not work! See svg/w3c/path2.svg
X2 := CurX + (2/3) * (XQ-CurX);
Y2 := CurY + (2/3) * (YQ-CurY);
//
X3 := X + (2/3) * (XQ-X);
Y3 := Y + (2/3) * (YQ-Y);
APaths.Data.AddBezierToPath(X2, Y2, X3, Y3, X, Y);
CurX := X;
CurY := Y;
Inc(APaths.CurTokenIndex, 5);
end
// --------------
// Elliptical arcs
// See http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
// (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+
// --------------
else if lCurTokenType in [sttEllipticArcTo, sttRelativeEllipticArcTo] then
begin
X2 := FSVGPathTokenizer.Tokens.Items[i+1].Value; // RX
Y2 := FSVGPathTokenizer.Tokens.Items[i+2].Value; // RY
phi := FSVGPathTokenizer.Tokens.Items[i+3].Value; // RotationX
phi := DegToRad(phi); // degrees to radians conversion
LargeArcFlag := Round(FSVGPathTokenizer.Tokens.Items[i+4].Value) = 1;
SweepFlag := Round(FSVGPathTokenizer.Tokens.Items[i+5].Value) = 1;
X := FSVGPathTokenizer.Tokens.Items[i+6].Value; // X
Y := FSVGPathTokenizer.Tokens.Items[i+7].Value; // Y
{
if lCurTokenType = sttRelativeEllipticArcTo then
begin
Xnew := CurX + X;
Ynew := CurY + Y;
end else
begin
Xnew := CurX;
Ynew := CurY;
end;
CalcEllipseCenter(CurX, CurY, Xnew, Ynew, X2, Y2, phi, LargeArcFlag, SweepFlag, cx, cy, tmp);
ConvertSVGCoordinatesToFPVCoordinates(AData, cx, cy, cx, cy);
}
// non-coordinate values (radii)
ConvertSVGDeltaToFPVDelta(AData, X2, Y2, X2, Y2);
if X2 < 0 then X2 := -X2;
if Y2 < 0 then Y2 := -Y2;
// Careful that absolute coordinates require using ConvertSVGCoordinatesToFPVCoordinates
if lCurTokenType in [sttRelativeEllipticArcTo] then
begin
ConvertSVGDeltaToFPVDelta(AData, X, Y, X, Y);
Xnew := CurX + X;
Ynew := CurY + Y;
end else
begin
ConvertSVGCoordinatesToFPVCoordinates(AData, X, Y, X, Y);
Xnew := X;
Ynew := Y;
end;
// in svg the y axis increases downward, in fpv upward. Therefore, angles
// change their sign!
if vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags then
begin
phi := -phi;
SweepFlag := not SweepFlag; // i.e. "clockwise" turns into "counter-clockwise"!
end;
if CalcEllipseCenter(CurX, CurY, Xnew, Ynew, X2, Y2, phi, LargeArcFlag, SweepFlag, cx, cy, tmp) then
AData.AddEllipticalArcWithCenterToPath(X2*tmp, Y2*tmp, phi, Xnew, Ynew, cx, cy, SweepFlag)
else
// Use a straight segment in case of no solution existing for the ellipse center
AData.AddLineToPath(Xnew, Ynew);
CurX := Xnew;
CurY := Ynew;
{
// Convert SVG flags to fpvectorial flags
LeftmostEllipse := (LargeArcFlag and (not SweepFlag))
or ((not LargeArcFlag) and SweepFlag);
if (Y > CurY) or ((Y = CurY) and (X > CurX)) then
LeftMostEllipse := not LeftMostEllipse;
// if Y = CurY then "LeftMost" is to be understood as "TopMost"
ClockwiseArc := SweepFlag;
if lCurTokenType = sttRelativeEllipticArcTo then
begin
AData.AddEllipticalArcToPath(X2, Y2, X3, X + CurX, Y + CurY, LeftmostEllipse, ClockwiseArc);
CurX := CurX + X;
CurY := CurY + Y;
end
else
begin
AData.AddEllipticalArcToPath(X2, Y2, X3, X, Y, LeftmostEllipse, ClockwiseArc);
CurX := X;
CurY := Y;
end;
}
Inc(APaths.CurTokenIndex, 8);
end
else
begin
Inc(APaths.CurTokenIndex);
end;
end;
procedure TvSVGVectorialReader.ReadPointsFromString(AStr: string;
AData: TvVectorialPage; ADoc: TvVectorialDocument; AClosePath: Boolean);
var
i: Integer;
X, Y: Double;
FirstPtX, FirstPtY, CurX, CurY: Double;
begin
FSVGPathTokenizer.ClearTokens;
FSVGPathTokenizer.TokenizePathString(AStr);
CurX := 0;
CurY := 0;
if FSVGPathTokenizer.Tokens.Count <= 2 then
raise Exception.Create('[TvSVGVectorialReader.ReadPointsFromString] There are too few points in the element');
// The first point
CurX := FSVGPathTokenizer.Tokens.Items[0].Value;
CurY := FSVGPathTokenizer.Tokens.Items[1].Value;
ConvertSVGCoordinatesToFPVCoordinates(AData, CurX, CurY, CurX, CurY);
FirstPtX := CurX;
FirstPtY := CurY;
AData.AddMoveToPath(CurX, CurY);
// Now all other points
i := 2;
while i < FSVGPathTokenizer.Tokens.Count do
begin
X := FSVGPathTokenizer.Tokens.Items[i].Value;
Y := FSVGPathTokenizer.Tokens.Items[i+1].Value;
ConvertSVGCoordinatesToFPVCoordinates(AData, X, Y, CurX, CurY);
AData.AddLineToPath(CurX, CurY);
Inc(i, 2);
end;
// and if we want, close the path
if AClosePath then
AData.AddLineToPath(FirstPtX, FirstPtY);
end;
// polygon and polyline are very similar
function TvSVGVectorialReader.ReadPolyFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lPointsStr: string = '';
i: Integer;
lNodeName, lNodeValue: DOMString;
lPath: TPath;
lIsPolygon: Boolean = False;
begin
lIsPolygon := LowerCase(ANode.NodeName) = 'polygon';
// first get the points
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'points' then
lPointsStr := ANode.Attributes.Item[i].NodeValue;
end;
AData.StartPath();
ReadPointsFromString(lPointsStr, AData, ADoc, lIsPolygon);
lPath := AData.EndPath(True);
Result := lPath;
// Add default SVG pen/brush
lPath.Pen.Style := psClear;
lPath.Brush.Style := bsClear;
// Apply the layer style
ApplyLayerStyles(AData, lPath);
// now read the other attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'id' then
lPath.Name := lNodeValue
else if IsEntityStyleField(lNodeName) then
ReadEntityStyleField(AData, lNodeName, lNodeValue, lPath);
end;
end;
// <rect width="90" height="90" stroke="green" stroke-width="3" fill="yellow" filter="url(#f1)" />
function TvSVGVectorialReader.ReadRectFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lx, ly, cx, cy, lrx, lry: double;
lRect: TvRectangle;
i: Integer;
lNodeName: DOMString;
lNodeValue: String;
begin
lx := 0.0;
ly := 0.0;
cx := 0.0;
cy := 0.0;
lrx := 0.0;
lry := 0.0;
lRect := TvRectangle.Create(nil);
// SVG entities start without any pen drawing, but with a black brush
lRect.Pen.Style := psClear;
lRect.Brush.Style := bsSolid;
lRect.Brush.Color := colBlack;
// Apply the layer style
ApplyLayerStyles(AData, lRect);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'x' then
lx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'y' then
ly := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'rx' then
lrx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'ry' then
lry := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'width' then
cx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'height' then
cy := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'id' then
lRect.Name := lNodeValue;
end;
ConvertSVGCoordinatesToFPVCoordinates(AData, lx, ly, lRect.X, lRect.Y);
ConvertSVGSizeToFPVSize(AData, cx, cy, lRect.CX, lRect.CY);
ConvertSVGSizeToFPVSize(AData, lrx, lry, lRect.RX, lRect.RY);
lRect.RX := Abs(lRect.RX) * 2;
lRect.RY := Abs(lRect.RY) * 2;
// Make sure that transformations are read after geometry and position
// of rectangle is known.
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
if lNodeName = 'style' then
ReadSVGStyle(AData, lNodeValue, lRect)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGPenStyleWithKeyAndValue(lNodeName,
ANode.Attributes.Item[i].NodeValue, lRect);
ReadSVGBrushStyleWithKeyAndValue(lNodeName,
ANode.Attributes.Item[i].NodeValue, lRect);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName,
ANode.Attributes.Item[i].NodeValue, lRect);
end;
end;
Result := lRect;
end;
function TvSVGVectorialReader.ReadSymbolFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lBlock: TvBlock;
lLayerName: string;
lPreviousLayer: TvEntityWithSubEntities;
lCurEntity: TvEntity;
i: Integer;
lAttrName: DOMString;
lCurNode: TDOMNode;
begin
Result := nil;
lBlock := TvBlock.Create(nil);
// pre-load attribute reader, to get the block name
for i := 0 to ANode.Attributes.Length - 1 do
begin
lAttrName := ANode.Attributes.Item[i].NodeName;
if lAttrName = 'id' then
begin
lLayerName := ANode.Attributes.Item[i].NodeValue;
lBlock.Name := lLayerName;
end;
end;
lPreviousLayer := AData.GetCurrentLayer();
AData.AddEntity(lBlock);
AData.SetCurrentLayer(lBlock);
//
lCurNode := ANode.FirstChild;
while Assigned(lCurNode) do
begin
lCurEntity := ReadEntityFromNode(lCurNode, AData, ADoc);
if lCurEntity <> nil then
AData.AddEntity(lCurEntity);
lCurNode := lCurNode.NextSibling;
end;
//
AData.SetCurrentLayer(lPreviousLayer);
end;
{
<text x="0" y="15" fill="red" transform="rotate(30 20,40)">I love SVG</text>
Example with nested tspan:
<text class="TextShape">
<tspan class="TextParagraph" font-family="Times New Roman, serif" font-size="917px" font-weight="400">
<tspan class="TextPosition" x="3650" y="11251">
<tspan fill="rgb(0,0,0)" stroke="none">This </tspan>
<tspan fill="rgb(197,0,11)" stroke="none">size</tspan>
<tspan fill="rgb(0,0,0)" stroke="none"> is bigger.</tspan>
</tspan>
</tspan>
</text>
}
function TvSVGVectorialReader.ReadTextFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lx, ly: double;
lName: string;
lParagraph: TvParagraph;
lTextSpanStack: TSVGObjectStack; // of TSVGTextSpanStyle
lCurStyle: TSVGTextSpanStyle;
i: Integer;
lNodeName, lNodeValue, lXLink: DOMString;
lCurObject: TObject;
procedure ApplyStackStylesToText(ADest: TvText);
var
j: Integer;
begin
for j := 0 to lTextSpanStack.GetList().Count-1 do
begin
lCurStyle := TSVGTextSpanStyle(lTextSpanStack.GetList().Items[j]);
lCurStyle.ApplyIntoEntity(ADest);
if lCurStyle.PositionSet then
begin
ADest.X := ADest.X + lCurStyle.X;
ADest.Y := ADest.Y + lCurStyle.Y;
end;
end;
end;
procedure ReadTextSpans(ACurNode: TDOMNode);
var
j: Integer;
lCurNode: TDOMNode;
lTextStr: string;
lText: TvText;
lCText: TvCurvedText;
lInsertedEntity, lInsertedSubEntity: TvEntity;
begin
lCurNode := ACurNode.FirstChild;
while lCurNode <> nil do
begin
lNodeName := LowerCase(lCurNode.NodeName);
lNodeValue := lCurNode.NodeValue;
if lNodeName = 'tspan' then
begin
lCurStyle := TSVGTextSpanStyle.Create;
lTextSpanStack.Push(lCurStyle);
// read the attributes
for j := 0 to lCurNode.Attributes.Length - 1 do
begin
lNodeName := lCurNode.Attributes.Item[j].NodeName;
lNodeValue := lCurNode.Attributes.Item[j].NodeValue;
lNodeName := LowerCase(lNodeName);
if lNodeName = 'x' then
begin
lCurStyle.PositionSet := True;
lCurStyle.X := StringWithUnitToFloat(lNodeValue, sckX) - lParagraph.X;
end
else if lNodeName = 'y' then
begin
lCurStyle.PositionSet := True;
lCurStyle.Y := StringWithUnitToFloat(lNodeValue, sckY) - lParagraph.Y;
end
//else if lNodeName = 'id' then
// lText.Name := lNodeValue
//else if lNodeName = 'style' then
// ReadSVGStyle(lNodeValue, lParagraph)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGFontStyleWithKeyAndValue(lNodeName, lNodeValue, nil, lCurStyle);
//ReadSVGGeneralStyleWithKeyAndValue(lNodeName, lNodeValue, lText);
end;
end;
// Recursion
ReadTextSpans(lCurNode);
// Get rid of the current style
lCurObject := lTextSpanStack.Pop();
if lCurObject <> nil then lCurObject.Free;
end
else if lNodeName = 'textpath' then
begin
lTextStr := GetTextContentFromNode(lCurNode);
lTextStr := RemoveLineEndingsAndTrim(lTextStr);
lTextStr := Trim(lTextStr);
lCText := lParagraph.AddCurvedText(lTextStr);
lCText.Font.Size := 10;
lCText.Name := lName;
// read the attributes
for j := 0 to lCurNode.Attributes.Length - 1 do
begin
lNodeName := lCurNode.Attributes.Item[j].NodeName;
lNodeValue := lCurNode.Attributes.Item[j].NodeValue;
lNodeName := LowerCase(lNodeName);
if lNodeName = 'xlink:href' then
begin
lXLink := lNodeValue;
lXLink := Copy(lXLink, 2, Length(lXLink));
if lXLink = '' then Continue; // nothing to insert, so give up
lInsertedEntity := AData.FindEntityWithNameAndType(lXLink, TvEntity, True);
if lInsertedEntity = nil then Continue; // nothing to insert, give up!
if lInsertedEntity is TvBlock then
begin
lInsertedSubEntity := TvBlock(lInsertedEntity).GetFirstEntity();
if not (lInsertedSubEntity is TPath) then Continue;
// Add the curvature
lCText.Path := TPath(lInsertedSubEntity as TPath);
end;
end;
end;
// Apply the layer style
ApplyLayerStyles(AData, lCText);
// Apply the layer style
ApplyStackStylesToText(lCText);
end
else
if lNodeValue <> '' then
begin
lText := lParagraph.AddText(lNodeValue);
lText.Font.Size := 10;
lText.Name := lName;
// Apply the layer style
ApplyLayerStyles(AData, lText);
// Apply the layer style
ApplyStackStylesToText(lText);
end;
lCurNode := lCurNode.NextSibling;
end;
end;
begin
lx := 0.0;
ly := 0.0;
// The text contents are inside as a child text, not as a attribute
// ex: <text x="0" y="15" fill="red" transform="rotate(30 20,40)">I love SVG</text>
// For simple text, but much more complex structures appear if there are
// text spans
lParagraph := TvParagraph.Create(AData);
lParagraph.YPos_NeedsAdjustment_DelFirstLineBodyHeight := True;
lTextSpanStack := TSVGObjectStack.Create;
lCurStyle := TSVGTextSpanStyle.Create;
lTextSpanStack.Push(lCurStyle);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'x' then
lx := lx + StringWithUnitToFloat(lNodeValue, sckX, suPX, suMM)
else if lNodeName = 'y' then
ly := ly + StringWithUnitToFloat(lNodeValue, sckY, suPX, suMM)
else if lNodeName = 'id' then
begin
lName := lNodeValue;
lParagraph.Name := lName;
end
else if lNodeName = 'style' then
ReadSVGStyle(AData, lNodeValue, nil, lCurStyle)
else if IsAttributeFromStyle(lNodeName) then
begin
ReadSVGFontStyleWithKeyAndValue(lNodeName, lNodeValue, nil, lCurStyle);
// ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName, lNodeValue, lParagraph);
end;
end;
// Takes into account a possible transformation matrix
lx := lx + lParagraph.X;
ly := ly + lParagraph.Y;
// Set the coordinates -> Don't use ConvertSVGCoordinatesToFPVCoordinates
// because StringWithUnitToFloat(..sckX..) already makes all conversions necessary
lParagraph.X := lx;
lParagraph.Y := ly;
// Now add other lines, which appear as <tspan ...>another line</tspan>
// Example:
// <text x="10" y="20" style="fill:red;">Several lines:
// <tspan x="10" y="45">First line</tspan>
// <tspan x="10" y="70">Second line</tspan>
// </text>
// These other lines can be positioned, so they need to appear as independent TvText elements
ReadTextSpans(ANode);
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if IsAttributeFromStyle(lNodeName) then
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName, lNodeValue, lParagraph);
end;
// Finalization
lCurObject := lTextSpanStack.Pop();
if lCurObject <> nil then lCurObject.Free;
lTextSpanStack.Free;
Result := lParagraph;
end;
// <use xlink:href="#svgbar" transform="rotate(45)"/>
// It might use another entity or use something from Defs
function TvSVGVectorialReader.ReadUseFromNode(ANode: TDOMNode;
AData: TvVectorialPage; ADoc: TvVectorialDocument): TvEntity;
var
lInsert: TvInsert;
lXLink: DOMString = '';
lInsertedEntity: TvEntity;
i: Integer;
lx, ly: Double;
lNodeName, lNodeValue: DOMString;
begin
Result := nil;
lx := 0.0;
ly := 0.0;
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'xlink:href' then
begin
lXLink := lNodeValue;
lXLink := Copy(lXLink, 2, Length(lXLink));
end
else if lNodeName = 'x' then
lx := StringWithUnitToFloat(lNodeValue, sckX)
else if lNodeName = 'y' then
ly := StringWithUnitToFloat(lNodeValue, sckY);
end;
if lXLink = '' then Exit; // nothing to insert, so give up
lInsertedEntity := AData.FindEntityWithNameAndType(lXLink, TvEntity, True);
if lInsertedEntity = nil then Exit; // nothing to insert, give up!
lInsert := TvInsert.Create(nil);
lInsert.InsertEntity := lInsertedEntity;
lInsert.x := lx;
lInsert.y := ly;
// Apply the styles
// read the attributes
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'style' then
begin
ReadSVGStyle(AData, lNodeValue, lInsert);
end
else if IsAttributeFromStyle(lNodeName) then
begin
lInsert.SetElements += ReadSVGPenStyleWithKeyAndValue(lNodeName, lNodeValue, lInsert);
lInsert.SetElements += ReadSVGBrushStyleWithKeyAndValue(lNodeName, lNodeValue, lInsert);
lInsert.SetElements += ReadSVGFontStyleWithKeyAndValue(lNodeName, lNodeValue, lInsert);
ReadSVGGeneralStyleWithKeyAndValue(AData, lNodeName, lNodeValue, lInsert);
end;
end;
// We need to add this hack here, otherwise the Height is added twice
// to inserted items: Once in the Insert and yet another time in the
// coordinates of the inserted item!
if vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags then
lInsert.Y := lInsert.Y - AData.Height;
Result := lInsert;
end;
procedure TvSVGVectorialReader.StringToPenPattern(const AStr: String;
var APen: TvPen);
var
float_patt: TDoubleArray;
patt: array of LongWord;
i: Integer;
begin
if AStr = 'none' then
exit;
float_patt := ReadSpaceSeparatedFloats(AStr, ',');
if Length(float_patt) < 2 then
exit;
SetLength(patt, Length(float_patt));
for i:=0 to High(patt) do
begin
if float_patt[i] < 0 then
raise Exception.CreateFmt('Incorrect value in stroke-dasharray "%s"', [AStr]);
patt[i] := round(float_patt[i]);
end;
case Length(patt) of
2: if patt[1] = 5 then
case patt[0] of
3: begin APen.Style := psDot; exit; end; // stroke-dasharray: 3, 5
9: begin APen.Style := psDash; exit; end; // stroke-dasharray: 9, 5
end;
4: if (patt[0] = 9) and (patt[1] = 5) and (patt[2] = 3) and (patt[3] = 5) then
begin // stroke-dasharray: 9, 5, 3, 5
APen.Style := psDashDot;
exit;
end;
6: if (patt[0] = 9) and (patt[1] = 5) and (patt[2] = 3) and (patt[3] = 5) and
(patt[4] = 3) and (patt[5] = 5)
then begin // stroke-dasharray: 9, 5, 3, 5, 3, 5
APen.Style := psDashDotDot;
exit;
end;
end;
APen.Style := psPattern;
APen.Pattern := patt;
end;
function TvSVGVectorialReader.StringWithUnitToFloat(AStr: string;
ACoordKind: TSVGCoordinateKind = sckUnknown; ADefaultUnit: TSVGUnit = suPX;
ATargetUnit: TSVGUnit = suPX): Double;
var
UnitStr, ValueStr: string;
Len: Integer;
LastChar: Char;
ViewPortApplied: Boolean = False;
procedure DoViewBoxAdjust();
begin
if ViewBoxAdjustment then
begin
case ACoordKind of
sckX: Result := (Result - ViewBox_Left) * Page_Width / ViewBox_Width;
sckXDelta,
sckXSize: Result := Result * Page_Width / ViewBox_Width;
end;
if not (vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags) then
begin
case ACoordKind of
sckY: Result := (Result - ViewBox_Top) * Page_Height / ViewBox_Height;
sckYDelta,
sckYSize: Result := Result * Page_Height / ViewBox_Height;
end;
end
else
begin
case ACoordKind of
sckY: Result := Page_Height - (Result - ViewBox_Top) * Page_Height / ViewBox_Height;
sckYDelta: Result := - (Result - ViewBox_Top) * Page_Height / ViewBox_Height;
sckYSize: Result := Result * Page_Height / ViewBox_Height;
end;
end;
ViewPortApplied := True;
end
else
begin
if vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags then
begin
case ACoordKind of
sckY: Result := Page_Height - Result;
sckYDelta: Result := - Result;
end;
end;
end;
end;
procedure DoProcessMM_End();
begin
if ATargetUnit = suPX then
Result := Result / FLOAT_MILLIMETERS_PER_PIXEL;
DoViewBoxAdjust();
end;
procedure DoProcessPX();
begin
Result := StrToFloat(ValueStr, FPointSeparator);
case ATargetUnit of
suMM: Result := Result * FLOAT_MILLIMETERS_PER_PIXEL;
suPT: Result := Result * FLOAT_POINTS_PER_PIXEL;
end;
DoViewBoxAdjust();
end;
begin
if AStr = '' then Exit(0.0);
// Check the unit
Len := Length(AStr);
UnitStr := Copy(AStr, Len-1, 2);
LastChar := AStr[Len];
if UnitStr = 'mm' then
begin
ValueStr := Copy(AStr, 1, Len-2);
Result := StrToFloat(ValueStr, FPointSeparator);
DoProcessMM_End();
end
else if UnitStr = 'cm' then
begin
ValueStr := Copy(AStr, 1, Len-2);
Result := StrToFloat(ValueStr, FPointSeparator);
Result := Result * 10;
DoProcessMM_End();
end
else if UnitStr = 'in' then
begin
ValueStr := Copy(AStr, 1, Len-2);
Result := StrToFloat(ValueStr, FPointSeparator);
Result := Result * 25.4;
DoProcessMM_End();
end
else if UnitStr = 'px' then
begin
ValueStr := Copy(AStr, 1, Len-2);
DoProcessPX();
end
else if LastChar = '%' then
begin
ValueStr := Copy(AStr, 1, Len-1);
Result := StrToInt(ValueStr);
end
else if UnitStr = 'pt' then
begin
ValueStr := Copy(AStr, 1, Len-2);
Result := StrToFloat(ValueStr, FPointSeparator);
end
// Now process default values
else if ADefaultUnit = suMM then
begin
ValueStr := AStr;
Result := StrToFloat(ValueStr, FPointSeparator);
DoProcessMM_End();
end
else if ADefaultUnit = suPX then
begin
ValueStr := AStr;
DoProcessPX();
end
else // If there is no unit and no matching default, just use StrToFloat
begin
Result := StrToFloat(AStr, FPointSeparator);
DoViewBoxAdjust();
end;
end;
function TvSVGVectorialReader.StringFloatZeroToOneToWord(AStr: string): Word;
begin
Result := Round(StrToFloat(AStr, FPointSeparator) * $FFFF);
end;
{@@ Converts a number string to a floating-point number. If the string has a
% character at its end then it is removed, and the numerical value is
divided by 100. }
function TvSVGVectorialReader.StringWithPercentToFloat(AStr: String): Double;
begin
if AStr[Length(AStr)] = '%' then
begin
Delete(AStr, Length(AStr), 1);
Result := 0.01 * StrToFloat(trim(AStr), FPointSeparator);
end else
Result := StrToFloat(AStr, FPointSeparator);
end;
procedure TvSVGVectorialReader.ConvertSVGCoordinatesToFPVCoordinates(
const AData: TvVectorialPage; const ASrcX, ASrcY: Double;
var ADestX,ADestY: Double; ADoViewBoxAdjust: Boolean = True);
begin
ADestX := ASrcX * FLOAT_MILLIMETERS_PER_PIXEL;
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestX := (ASrcX - ViewBox_Left) * Page_Width / ViewBox_Width;
end;
if not (vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags) then
begin
ADestY := ASrcY * FLOAT_MILLIMETERS_PER_PIXEL;
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestY := (ASrcY - ViewBox_Top) * Page_Height / ViewBox_Height;
end;
end
else
begin
ADestY := AData.Height - ASrcY * FLOAT_MILLIMETERS_PER_PIXEL;
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestY := AData.Height - (ASrcY - ViewBox_Top) * Page_Height / ViewBox_Height;
end;
end;
end;
procedure TvSVGVectorialReader.ConvertSVGDeltaToFPVDelta(
const AData: TvVectorialPage; const ASrcX, ASrcY: Double; var ADestX,
ADestY: Double; ADoViewBoxAdjust: Boolean = True);
begin
ADestX := ASrcX * FLOAT_MILLIMETERS_PER_PIXEL;
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestX := ASrcX * Page_Width / ViewBox_Width;
end;
if not (vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags) then
begin
ADestY := ASrcY * FLOAT_MILLIMETERS_PER_PIXEL;
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestY := ASrcY * Page_Height / ViewBox_Height;
end;
end
else
begin
ADestY := -ASrcY * FLOAT_MILLIMETERS_PER_PIXEL; // fpc y coords grow upward, svg downward
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestY := -ASrcY * Page_Height / ViewBox_Height;
end;
end;
end;
procedure TvSVGVectorialReader.ConvertSVGSizeToFPVSize(
const AData: TvVectorialPage; const ASrcX, ASrcY: Double; var ADestX,
ADestY: Double; ADoViewBoxAdjust: Boolean = True);
begin
ADestX := ASrcX * FLOAT_MILLIMETERS_PER_PIXEL;
ADestY := ASrcY * FLOAT_MILLIMETERS_PER_PIXEL;
if ViewBoxAdjustment and ADoViewBoxAdjust then
begin
ADestX := ASrcX * Page_Width / ViewBox_Width;
ADestY := ASrcY * Page_Height / ViewBox_Height;
end;
end;
procedure TvSVGVectorialReader.AutoDetectDocSize(var ALeft, ATop, ARight, ABottom: Double;
ABaseNode: TDOMNode);
var
i: Integer;
lCurNode: TDOMNode;
lx, ly, lcx, lcy: Double;
lNodeName, lNodeValue: DomString;
begin
lx := 0;
ly := 0;
lcx := 0;
lcy := 0;
// read the attributes
if ABaseNode.Attributes <> nil then
begin
for i := 0 to ABaseNode.Attributes.Length - 1 do
begin
lNodeName := ABaseNode.Attributes.Item[i].NodeName;
lNodeValue := ABaseNode.Attributes.Item[i].NodeValue;
if (lNodeName = 'x') or (lNodeName = 'cx') then
lx := lx + StringWithUnitToFloat(lNodeValue)
else if (lNodeName = 'y') or (lNodeName = 'cy') then
ly := ly + StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'width' then
lcx := StringWithUnitToFloat(lNodeValue)
else if lNodeName = 'height' then
lcy := StringWithUnitToFloat(lNodeValue);
end;
end;
// Borders guessing
if lx < ALeft then ALeft := lx;
if ly < ATop then ATop := ly;
if lx + lcx > ARight then ARight := lx + lcx;
if ly + lcy > ABottom then ABottom := ly + lcy;
// iterate through children
lCurNode := ABaseNode.FirstChild;
while Assigned(lCurNode) do
begin
AutoDetectDocSize(ALeft, ATop, ARight, ABottom, lCurNode);
lCurNode := lCurNode.NextSibling;
end;
end;
// From 0..255 to 0..$FFFF
// From 0%..100% to 0..$FFFF
function TvSVGVectorialReader.SVGColorValueStrToWord(AStr: string): Word;
var
lStr: string;
begin
lStr := Trim(AStr);
if lStr[Length(lStr)] = '%' then
begin
lStr := Copy(lStr, 1, Length(lStr)-1);
Result := Round(StrToFloat(lStr, FPointSeparator) * $FFFF / 100);
end
else Result := StrToInt(lStr) * $101;
end;
constructor TvSVGVectorialReader.Create;
begin
inherited Create;
FPointSeparator := DefaultFormatSettings;
FPointSeparator.DecimalSeparator := '.';
FPointSeparator.ThousandSeparator := '#';// disable the thousand separator
FSVGPathTokenizer := TSVGPathTokenizer.Create;
FLayerStylesKeys := TFPList.Create;
FLayerStylesValues := TFPList.Create;
FBrushDefs := TFPList.Create;
FCSSDefs := TFPList.Create;
end;
destructor TvSVGVectorialReader.Destroy;
var
i: Integer;
begin
FSVGPathTokenizer.Free;
FLayerStylesKeys.Free;
FLayerStylesValues.Free;
for i:=FBrushDefs.Count-1 downto 0 do TObject(FBrushDefs[i]).Free;
FBrushDefs.Free;
for i:=FCSSDefs.Count-1 downto 0 do TObject(FCSSDefs[i]).Free;
FCSSDefs.Free;
inherited Destroy;
end;
procedure TvSVGVectorialReader.ReadFromStream(AStream: TStream;
AData: TvVectorialDocument);
var
Doc: TXMLDocument = nil;
begin
try
// Read in xml file from the stream
ReadXMLFile(Doc, AStream);
ReadFromXML(Doc, AData);
finally
// finally, free the document
Doc.Free;
end;
end;
procedure TvSVGVectorialReader.ReadFromXML(Doc: TXMLDocument;
AData: TvVectorialDocument);
var
lCurNode: TDOMNode;
lPage: TvVectorialPage;
{$ifdef SVG_MERGE_LAYER_STYLES}
lLayerStyleKeys, lLayerStyleValues: TStringList;
{$endif}
lNodeName, lNodeValue: DOMString;
ANode: TDOMElement;
i: Integer;
lCurEntity: TvEntity;
lViewBox: TDoubleArray;
lStr: string;
lDocNeedsSizeAutoDetection: Boolean = True;
lx, ly, lx2, ly2: Double;
begin
FPathNumber := 0;
ViewBoxAdjustment := False;
// ----------------
// Read the properties of the <svg> tag
// ----------------
AData.Width := StringWithUnitToFloat(Doc.DocumentElement.GetAttribute('width'), sckX, suPX, suMM);
lStr := Doc.DocumentElement.GetAttribute('height');
if lStr <> '' then
begin
lDocNeedsSizeAutoDetection := False;
AData.Height := StringWithUnitToFloat(lStr, sckX, suPX, suMM);
end;
Page_Width := AData.Width;
Page_Height := AData.Height;
{$ifdef SVG_MERGE_LAYER_STYLES}
FLayerStylesKeys.Clear;
FLayerStylesValues.Clear;
lLayerStyleKeys := TStringList.Create;
lLayerStyleValues := TStringList.Create;
FLayerStylesKeys.Add(lLayerStyleKeys);
FLayerStylesValues.Add(lLayerStyleValues);
{$endif}
ANode := Doc.DocumentElement;
for i := 0 to ANode.Attributes.Length - 1 do
begin
lNodeName := ANode.Attributes.Item[i].NodeName;
lNodeValue := ANode.Attributes.Item[i].NodeValue;
if lNodeName = 'viewBox' then
begin
lViewBox := ReadSpaceSeparatedFloats(lNodeValue, '');
if lDocNeedsSizeAutoDetection then // Has only ViewBox
begin
lDocNeedsSizeAutoDetection := False;
AData.Width := lViewBox[2] - lViewBox[0];
AData.Height := lViewBox[3] - lViewBox[1];
ViewBox_Left := 0;
ViewBox_Top := 0;
ViewBox_Width := 0;
ViewBox_Height := 0;
end
else // Has both viewBox and width/height!
begin
ViewBox_Left := lViewBox[0];
ViewBox_Top := lViewBox[1];
ViewBox_Width := lViewBox[2];
ViewBox_Height := lViewBox[3];
ViewBoxAdjustment := True;
end;
end
else if lNodeName = 'style' then
begin
{$ifdef SVG_MERGE_LAYER_STYLES}
ReadSVGStyleToStyleLists(lNodeValue, lLayerStyleKeys, lLayerStyleValues);
{$endif}
end
else if IsAttributeFromStyle(lNodeName) then
begin
{$ifdef SVG_MERGE_LAYER_STYLES}
lLayerStyleKeys.Add(lNodeName);
lLayerStyleValues.Add(lNodeValue);
{$endif}
end;
end;
// Auto-detect the document size of necessary
if lDocNeedsSizeAutoDetection then
begin
lx := 0;
ly := 0;
lx2 := 0;
ly2 := 0;
lCurNode := Doc.DocumentElement.FirstChild;
while Assigned(lCurNode) do
begin
AutoDetectDocSize(lx, ly, lx2, ly2, lCurNode);
lCurNode := lCurNode.NextSibling;
end;
AData.Width := lx2 - lx;
AData.Height := ly2 - ly;
end;
// Make sure the latest page size is synchronized with auto-detected
// or ViewBox-only obtained size
Page_Width := AData.Width;
Page_Height := AData.Height;
// ----------------
// Now process the elements
// ----------------
lCurNode := Doc.DocumentElement.FirstChild;
lPage := AData.AddPage(not (vrfSVG_UseBottomLeftCoords in Settings.VecReaderFlags));
lPage.Width := AData.Width;
lPage.Height := AData.Height;
while Assigned(lCurNode) do
begin
lNodeName := lCurNode.NodeName;
if lNodeName = 'defs' then
begin
ReadDefsFromNode(lCurNode, lPage, AData);
end
else
begin
lCurEntity := ReadEntityFromNode(lCurNode, lPage, AData);
if lCurEntity <> nil then
lPage.AddEntity(lCurEntity);
end;
lCurNode := lCurNode.NextSibling;
end;
// ----------------
// Remove the memory of the styles
// ----------------
{$ifdef SVG_MERGE_LAYER_STYLES}
// Now remove the style from this layer
FLayerStylesKeys.Remove(lLayerStyleKeys);
lLayerStyleKeys.Free;
FLayerStylesValues.Remove(lLayerStyleValues);
lLayerStyleValues.Free;
{$endif}
end;
initialization
RegisterVectorialReader(TvSVGVectorialReader, vfSVG);
end.
|