1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427
|
<html>
<head>
<title>netrik hacker's manual: layout engine</title>
</head>
<body>
<h1 align="center">netrik hacker's manual<br />>========================<</h1>
<p>
[This file contains a description of the layouting module. See hacking.txt or
<a href="hacking.html">hacking.html</a> for an overview of the manual.]
</p>
<h2>0. Overview</h2>
<p>
The whole layouting is split up into several, fairly simple passes, which are
executed one after the other. See the
<a href="hacking.html#notes">notes in hacking.*</a> for a discussion of
this approach.
</p>
<p>
The first pass is the <a href="#parseSyntax">parse_syntax()</a>
function, which creates a <a href="#syntaxTree">Syntax Tree</a> of
the document. This tree contains all HTML elements and their content, but the
elements have no special meaning yet.
</p>
<p>
<a href="#dumpTree">dump_tree()</a> can be used to output the syntax
tree.
</p>
<p>
In the next pass (<a href="#parseElements">parse_elements()</a>),
all element and attribute names are looked up in tables and stored as enums to
facialiate further processing.
</p>
<p>
If not compiled with -DXHTML_ONLY, an additional pass is inserted after element
parsing: In <a href="#sgmlRework">sgml_rework()</a>, the
syntax tree is modified to fix the wrong element nesting caused by missing end
tags in SGML documents.
</p>
<p>
dump_tree() can be used again to dump all element and attribute types as found
in the lookup, and the possibly modified tree structure.
</p>
<p>
The third pass is the central processing step.
<a href="#parseStruct">parse_struct()</a> interprets the elements
and their attributes, and creates a <a href="#itemTree">Structure
Tree</a>, which contains all the items that will be visible on the
output page.
</p>
<p>
The fourth pass prepares the page for rendering. In
<a href="#preRender">pre_render()</a>, all items created in
parse_struct() are assigned actual sizes and positions in the output page. Also,
a structure <a href="#pageMap">"page_map[]"</a> is created, needed for
fast lookup what items are present in any given line.
</p>
<p>
All of the passes mentioned above are necessary to prepare the rendering, and
are executed from <a href="#layout">layout()</a>.
</p>
<p>
The actual rendering is done in render.c. However, this isn't done for the
whole page like the other layouting passes. Instead, every time some region of
the output page needs to be displayed,
<a href="#render">render()</a> is called to render exactly that
region.
</p>
<p>
Alternatively, The whole page can be dumped to the terminal line by line, using
<a href="#dump">dump()</a>.
</p>
<p>
The third function in render.c is <a href="#dumpItems">dump_items()</a>.
This is not really a rendering function; it only dumps the item tree, including
the (coloured) text.
</p>
<h2>1. layout.c</h2>
<p>
This file forms the framework for the layouting process. It contains functions
to load a file and prepare it for rendering, but also to free the memory used
by a document when it is no longer needed.
</p>
<a name="layout" id="layout">
<h3>layout()</h3>
<p>
layout() is given a URL of a file or web resource to load, and does all
actions necessary to be able to render the corresponding page.
</p>
<p>
Before starting any of the loading or layouting operations, a descriptor is
allocated where all the data structures created inside layout() will be stored.
</p>
<p>
The descriptor is a "struct Layout" pointer. It contains the following data:
</p>
<ul> <li>
A pointer to the input ressource descriptor ("input")
</li> <li>
An additional pointer "url" to the effective page URL, necessary to hold the
URL after the input resource descriptor is freed
</li> <li>
Pointers to all data structures necessary for the layouting ("syntax_tree",
"item_tree", "page_map[]")
</li> <li>
Pointers to the "<a href="hacking-links.html#linkList">link_list</a>" and "<a href="hacking-links.html#anchorList">anchor_list</a>" data structures
</li> </ul>
<p>
After allocating the descriptor, layout() first opens the resource with
<a href="hacking-load.html#initLoad">init_load()</a>. (Described in
hacking-load.*)
</p>
<p>
Afterwards, <a href="#parseSyntax">parse_syntax()</a>,
<a href="#parseElements">parse_elements()</a>,
<a href="#sgmlRework">sgml_rework()</a>,
<a href="#parseStruct">parse_struct()</a>, and
<a href="#preRender">pre_render()</a> are called in sequence.
These functions are responsible for preparing the page for rendering.
</p>
<p>
The file loading itself is done inside parse_syntax(), which uses the
<a href="hacking-load.html#load">load()</a> function from load.c (see
<a href="hacking-load.html">hacking-load.*</a>) to read a data block
every time the input buffer is empty. It processes the data in the buffer
character by character (keeping track of the current read position by
"input->buf_ptr"), and when it reaches the end it calls load() again to get the
next data block.
</p>
<p>
After parse_struct(), the syntax tree is no longer needed. It is freed by
<a href="#freeSyntax">free_syntax()</a>.
</p>
<p>
At this point also the "link_list" and "anchor_list" data structures are
created using
<a href="hacking-links.html#linkList">make_link_list()</a> and
<a href="hacking-links.html#anchorList">make_anchor_list()</a>. (See
<a href="hacking-links.html">hacking-links.*</a> )
</p>
</a> <!-- layout -->
<a name="freeLayout" id="freeLayout">
<h3>free_layout()</h3>
<p>
When a page is unloaded (usually before loading a new page), this function is
called to free all the data structures created by the layouting process to
allow rendering (Item tree, page map, link list, anchor list), i.e. all data
stored in the "Layout" descriptor, except for "input" and "syntax_tree", which
are already freed during the layouting process. (s.a.) The descriptor itself is
also freed.
</p>
</a> <!-- freeLayout -->
<a name="resize" id="resize">
<h3>resize()</h3>
<p>
The resize() function is somewhat similar to
<a href="#layout">layout()</a> -- it calls the same subfunctions
to create a combination of an item tree with assigned coordinates and a page
usage map. The difference is that resize() does not start from scratch, but
only repeats the steps necessary to adapt to a new screen width; the properties
determined by the document itself (i.e. the item tree) are kept.
</p>
<p>
So we actually just call pre_render() (see <a href="#preRender">7.
pre-render.c</a>) again. (Note that the minimal item sizes calculated
in <a href="calcWidth">calc_width()</a>could be kept also; for
simplicity, we just pre-render completely again anyways -- this shouldn't be
too big a loss, we believe. When implementing rendering of incompletely loaded
pages, we will have to create some mechanism to skip such unnecessary
re-calculations per item anyways.)
</p>
<p>
As resize() starts from a "ready" page, not from scratch, it has to free the
old data structures (page map) before creating new ones.
</p>
</a> <!-- resize -->
<a name="parseSyntax" id="parseSyntax">
<h2>2. parse-syntax.c</h2>
<p>
The first thing to be done when layouting is parsing the syntax of the input
file. This is done by parse_syntax(). This function creates a syntax tree. The
pointer to the head of this tree is returned to layout() and stored as
"layout->syntax_tree" there.
</p>
<h3>Syntax Tree</h3>
<p>
Every node of "syntax_tree" is a structure of the type "Element" (defined in
"syntax.h") and corresponds to one HTML element. (An element in an HTML
document is represented by an HTML-tag, and the corresponding end tag, if any.)
End tags do not create tree nodes, as they only close elements already stored.
</p>
<a name="testHtml" id="testHtml">
<p>
For the supplied "test/0.html":
</p>
<p><pre>
header text
<html><head> </head>
<body>
<h1> heading </h1>
<p>
first paragraph of text;
includes multiple spaces and newlines,
<em> emphasized text </em>and
<strong> strong text </strong>
</p>
<p>
<center>starting with an evil center tag,</center>
this very long second paragraph contains some special characters (including a simple space...):
&amp;; &lt;&gt;&quot;=/ plus a big gap&nbsp;&nbsp;&nbsp;and two unicode escapes
(decimal: &#161; and hexal: &#xbf;)
but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
(this anchor also is the only tag with parameters);
and finally a blank row <br /> (a single tag)
</p>
</body>
</html>
</pre></p>
</a> <!--testHtml-->
<p>
the syntax tree looks like this:
</p>
<a name="syntaxTree" id="syntaxTree">
<p><pre>
++>NULL
+
+---+
| ! |-. <++
+---+ | +
v +
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+ <+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| head |->| body |-. <++++++++++++++ +
+------+ +------+ | <+ + +
v + + + <+++++++++++++++++++++++++++++++++++++++++++++++++
+----++ +---+ <++++++++++++++++++++ +---+ <++++++++++++++++++++++++++++++++ +
| h1 |-. <+ ,->| p |---. <++ + ,->| p |-. <++++++++++++++++++ + + (back
+----+ | + | +---+ | + + | +---+ | <+ + + + to top)
v + | v + + | | + + + + ^
,--------. | ,------------------. ,----. | | + ,----------. ,---------. ,---------------. |
(" heading") | (" first...newlines,") (" and") | v + (" this...em") ("ded...row") (" (a single tag)") |
`--+---+-' | `-----+----+-------' +-+----+-+ | +--------+ `---+---+--' `--+----+-' `-----+---+-----' |
| ? |----' | em |-. <++ ,->| strong |-. <++ | | center |-. <++ ,->| a |-. <+ ,->| br |---------->| ? |--------'
+---+ +----+ | + | +--------+ | + | +--------+ | + | +---+ | + | +----+ +---+
v + | v + | v + | v + |
,----------------. | ,------------. | ,----------------. | ,---.+ |
(" emphasized text") | (" strong text") | (" starting...tag,") | ("bed") |
`--------+---+---' | `-----+---+--' | `--------+---+---' | +---+ |
+++> "parent" | ? |------' | ? |-----' | ? |------' | ? |--'
---> "list_next" +---+ +---+ +---+ +---+
</pre></p>
</a> <!--syntaxTree-->
<p>
(I'm really curious if anyone can read this ;-) )
</p>
<p>
The "Element" structure includes:
</p>
<ul> <li>
The pointers "list_next" and "parent" describe the tree structure.
"parent" points to the element (node) which contains this one in its content
(text) area. "list_next" points to the next element as it appears in the
input stream.
</li> <li>
The "closed" flag is a helper flag for <a href="#sgmlRework">sgml_rework()</a> and has
no meaning outside of it.
</li> <li>
The union "name" describes what kind of element this node represents. ("html",
"head" etc.) It can store the element name either as a pointer to a string
(as appears in the input stream), or as an enum number.
</li> <li>
"attr_count" stores the number of attributes of this element. (Attributes
are the parameters of an element, which appear inside the start tag, like:
href="foo" etc.)
</li> <li>
"attr" points to an array of "Attr" structures. Each of these structures
contains the data for one attribute; it consists of a union of type
"Attr_name", which, like "Element_name", stores the attribute name either as a
string or as an enum; and a union of type "Attr_value", which stores the
value of the attribute. (String or number.)
</li> <li>
<a name="elementText">
The "content" string stores the content. (The text between the tags.) Every
element stores the content between the previous tag and the start tag of this
element. Thus it does not store the content of the element itself, but part
of the content of the *parent* element. This simplifies processing a bit,
because this way no facility for storing content blocks divided by
sub-elements is needed -- the sub-elements store the content themselves. The
caveat is that a lot of dummy elements are needed to store the content if no
further sub-element follows them. This is quite a big inefficiency, as nearly
every real element also needs a dummy element to store its content. This
should change in the future -- if we won't drop the syntax-tree in its
present form at all... Which we will :-)
</a> <!--elementText-->
</li> </ul>
<h3>Initialization</h3>
<p>
Before starting parsing, we have to create the tree top. (We call it the global
element.) This is done by setting "cur_el" to NULL and calling add_element().
</p>
<a name="addElement" id="addElement">
<h4>add_element()</h4>
<p>
This function creates a new node and inserts it into the syntax tree; thus it
has to set the "parent" and "list_next" pointers too, and adjust some
pointes of other nodes to point to this one.
</p>
<p>
"parent" is set to "cur_el", as any new tag is created while parsing the
content area of its parent. "list_next" is set to NULL, as the new node is
always the last one in the list. "list_next" of "last_el" (the last node in the
list up to now) is set to point to the new node; this is omitted if "cur_el" is
NULL, indicating that there are no other nodes yet.
</p>
</a> <!--addElement-->
<a name="parsing" id="parsing">
<h3>Parsing</h3>
<p>
The parser itself works in a very simple way. It is some kind of state machine.
For every input character, one action is taken, selected by a dispatcher
depending on the current state (stored in "parse_mode") and the input char
itself. Several combinations (e.g. tag start) change the current state, thus
the following character(s) are parsed in a different mode. (Other actions are
taken.)
</p>
<p>
Sometimes a character that causes a mode switch has to be parsed in the new
mode itself. In this case the flag "recycle" is set after the mode change,
causing the dispatch to be repeated for the same char, but in the new mode.
</p>
<p>
Again, the parsing is not very efficient in the present implementation. (In
fact, it is by far the most time consuming part of the whole layouting.)
Especially the huge switch is quite slow. (Good compilers have a fairly
efficient implementation of the switch itself; however, it still causes many
unpredictable branches.) There are some possibilities to optimize this. The
bigger problem is that the inner loop is quite big, and may not fit into the
processor's instruction cache, thus making it terribly slow. Maybe splitting
the parsing into several simpler passes would help. However, we are planning to
switch to a completely different, (hopefully) much more efficient parser system
in the next major release...
</p>
<p>
The default parsing mode is "PM_CONTENT", which is the mode for parsing element
content. Any normal character encountered in this mode is simply added to
"text_buf" by "buf_add_char()". A ' ', '\t', '\n', '\r' or '\f' aren't stored;
we switch to "PM_BLANK" instead. Any following blank space is ignored. As soon
as a normal character occurs again, we store a single ' ' and swich back to
"PM_CONTENT".
</p>
<p><pre>
input:
first paragraph of text;
includes multiple spaces and newlines,
^
file position
text_buf: " first paragraph of text; includes mul"
</pre></p>
<h4><pre> Blocks</h4>
<p>
After a <pre> tag, the mode isn't switched back to "PM_CONTENT", but to
"PM_PRE". In this mode all blank space characters are stored to "text_buf"
as non-breakable spaces, except newlines which are stored directly.
</p>
<p>
The mode is ended and switched to "PM_CONTENT" again when a closing "</pre>"
tag is encountered.
</p>
<p>
PM_PRE is also (mis-)used for <textarea>: The content of a <textarea> is used
as the initial value; and this is plain text, so it has to be treated
literally, without messing with the blanks. Thus it can be handled similar to
<pre>, except that blanks are really stored directly, not even converted to
&nbsp; . The "textarea" flag indicates we are in a <textarea> not a real <pre>,
and this exception needs to be applied.
</p>
<p>
Note that this is quite a dirty hack, which may not work in all situations.
(<textarea> inside <pre>...) However, the new parser in 2.x will handle this
totally diffent anyway, so it's not worth more effort with the old parser.
</p>
<h4>References</h4>
<p>
An '&' indicates a character reference (unicode escape) or entity reference
(named escape), and starts the reference parsing mode "PM_AMP". On entering
this mode, the current write position in "text_buf" is saved to "amp_pos".
</p>
<p><pre>
input: [...] &lt; [...]
^
text_buf: "[...] &"
^^
text_buf_len
amp_pos
</pre></p>
<p>
There are several submodes in the PM_AMP family, keeping track of the reference
syntax -- this is necessary in SGML mode, as there is no other method to
reliably discover the end of the reference or a '&' character which isn't
actually a reference.
</p>
<p>
Nonetheless, all characters occuring in any of these submodes are added to
"text_buf"; actually evaluating the reference is done only when it's end is
encountered. The mode is then switched back to the previous parsing mode before
the reference occured (saved in "prev_mode_amp") -- references can occur in
content and in attribute values.
</p>
<p><pre>
input: [...] &lt; [...]
^
text_buf: "[...] &lt;"
^ ^
text_buf_len
amp_pos
</pre></p>
<p>
The text between the saved start position of the escape sequence and the
current positon is converted then, depending on the type of the reference.
</p>
<p>
If it's a symbolic (named) reference, the string is looked up in "ref_table[]",
which is a table of named characters, defined in facilities.c.
</p>
<p>
For numerical character references, the integer value is extracted, using
decimal or hexadecimal conversion, depending on whether the number starts with
'x'. (Note that we have different parser states for decimal and hexal; however,
testing for the 'x' instead of the parser state saves us one condition, as we
would have to test the 'x' anyways -- the parser treats every alphanumerical
sequence starting with a letter as a hex number.)
</p>
<p>
If a replacement char was found either in the table or by the number
conversion, the reference is removed from "text_buf" and the replacement char
is inserted instead.
</p>
<p><pre>
text_buf: "[...] <"
^
text_buf_len
</pre></p>
<p>
If no replacement was found, the string is left unchanged. Probably we will
mark unknown escapes with some visible attribute in the future.
</p>
<h4>Tags</h4>
<p>
A '<' starts tag parsing. There is a a whole bunch of tag parsing modes. The
one entered after the '<' is "PM_TAG_START", which indicates that the tag name
should follow next.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: "[...] but also an anchor em"
tree:
+
+
+---+
-->| p |-. <== cur_el
+---+ | <+
| +
| +
v +
+--------+
| center |-. <++
+--------+ | +
v +
,----------------.
(" starting...tag,")
`--------+---+---' <-- last_el
| ? |->NULL
+---+
</pre></p>
<h5>Start Tags</h5>
<p>
If the following character is an normal char, it's a start tag. (Or a single
tag, which is treated the same way for now.) "PM_TAG_NAME" is entered. A new
element node is created by
<a href="#addElement">add_element()</a>. Any content in front of
this new element, which was stored in "text_buf" up to now, is stored to the
new node's "<a href="#elementText">text</a>" field by "insert_buf".
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: ""
tree:
+
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + <== +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--' <--
| center |-. <++ ,->| |->NULL
+--------+ | + | +---+
v + |
,----------------. |
(" starting...tag,") |
`--------+---+---' |
| ? |------'
+---+
</pre></p>
<p>
Normal characters encounterd in "PM_TAG_NAME" mode (including the one that
started the mode) are stored to "text_buf".
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: "a"
</pre></p>
<p>
A blank space character ends "PM_TAG_NAME" and switches to "PM_TAG", which indicates that
attributes may follow. "text_buf" is stored as the element name.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: ""
tree:
+
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + <== +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--' <--
| center |-. <++ ,->| a |->NULL
+--------+ | + | +---+
v + |
,----------------. |
(" starting...tag,") |
`--------+---+---' |
| ? |------'
+---+
</pre></p>
<p>
A following normal char is the beginning of an attribute name, and switches to
"PM_ATTR_NAME". Characters encounterd in "PM_ATTR_NAME" mode are also stored to "text_buf".
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: "name"
</pre></p>
<p>
The attribute name ends with an '=' or a blank char. A new entry is created in the "attr[]"
array, and "text_buf" is stored as the attribute name. Mode is switched to
"PM_ATTR_NAME_END" first, which indicates that the attribue value should follow.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: ""
attr: name: data:
"name" ""
</pre></p>
<p>
If the attribute name was ended by an '=', mode is switched immediately to
"PM_ATTR_VALUE", otherwise as soon as an '=' is encountered. (After any amount
of whitespace.)
</p>
<p>
White space in "PM_ATTR_VALUE" mode (after the '=') is ignored too.
</p>
<p>
Next char must be either a '"' or a '\'', and switches to "PM_ATTR_DATA_QUOT"
or "PM_ATTR_DATA_APOS", respectively. In this modes characters are stored to
"text_buf" again.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: "anchor"
attr: name: data:
"name" ""
</pre></p>
<p>
A second '"' (or '\'', respectively) ends this mode. "text_buf" is stored as
the attribute value for the new "attr" entry.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: ""
attr: name: data:
"name" "anchor"
</pre></p>
<p>
Mode is swiched back to "PM_TAG". Now blank space may follow (which is
ignored), followed by another attribute.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
</pre></p>
<p>
In "PM_TAG" mode also a '>' may occur, ending tag parsing and switching back to
the mode before tag parsing had begun. (PM_CONTENT or PM_BLANK.) In this case,
"cur_el" is set to "last_el"; this means descending in the syntax tree to the
newly created node.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
text_buf: ""
attr: name: data:
"name" "anchor"
"href" ""
tree:
+
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--' <==
| center |-. <++ ,->| a |->NULL <--
+--------+ | + | +---+
v + |
,----------------. |
(" starting...tag,") |
`--------+---+---' |
| ? |------'
+---+
</pre></p>
<p>
A '>' may also occur in "PM_TAG_NAME" mode, meaning the element has no
attributes.
</p>
<p><pre>
input: <html> <head> [...]
^
</pre></p>
<p>
In this case creating the new node and storing the name, and descending into
the element are done in one step. (By "recycle".)
</p>
<h5>End Tags</h5>
<p>
If the first character after the '<' is a '/', the tag is an end tag, and we
switch to "PM_END_TAG_START", and then to "PM_END_TAG_NAME" on the first
letter.
</p>
<p>
If any text was pending in "text_buf" before the tag, we have to store it
somewhere. As an end tag normally does not create a new element node, we have
to create a dummy node for this. (Very inefficient, s.a.)
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
tree:
+
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--' <==
| center |-. <++ ,->| a |-. <+
+--------+ | + | +---+ | +
v + | v +
,----------------. | ,---.+
(" starting...tag,") | ("bed")
`--------+---+---' | +---+ <--
| ? |------' | ? |->NULL
+---+ +---+
</pre></p>
<p>
Normal chars in "PM_END_TAG_NAME" mode are stored to "text_buf", too.
"PM_END_TAG_NAME" can be ended immediately by a '>', or by blank space
(switching to "PM_END_TAG_SPACE") followed by '>'.
</p>
<p>
The tag name extracted to "text_buf" is compared against the element name of
the current element, to see if the end tag matches, and then abdannoned. The
element is closed by ascending to the parent.
</p>
<p><pre>
input: [...] but also an anchor em<a name="anchor" href="">bed</a>ded inside a word
^
tree:
+
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + <== +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--'
| center |-. <++ ,->| a |-. <+
+--------+ | + | +---+ | +
v + | v +
,----------------. | ,---.+
(" starting...tag,") | ("bed")
`--------+---+---' | +---+ <--
| ? |------' | ? |->NULL
+---+ +---+
</pre></p>
<h5>Single Tags</h5>
<p>
If a '/' appears instead of an attribute name in "PM_TAG" mode, "parse_mode" is
set to "PM_SINGLE_TAG", indicating an (XML) single tag.
</p>
<p><pre>
input: [...] and finally a blank row <br /> (a single tag)
^
</pre></p>
<p>
The '/' can also immediately follow the element name. (In "PM_TAG_NAME" mode.)
</p>
<p><pre>
input: <hr/>
^
</pre></p>
<p>
In this case, creating the node and switching to a single tag are done in one
step by "recycle".
</p>
<p>
In any case, a '>' has to follow, and switches back to normal mode just like in
a start tag, only it does not descend (set the new node as "cur_el") -- a
single tag has no content area; the content following a single tag still
belongs to the parent.
</p>
<p><pre>
input: [...] and finally a blank row <br /> (a single tag)
^
tree:
+
+---+ <++++++++++++++++++++++++++++++++
->| p |-. <++++++++++++++++++ +
+---+ | <+ + +
| + <== + +
| + ,----------. ,---------.
v + (" this...em") ("ded...row")
+--------+ `---+---+--' `--+----+-' <--
| center |-. <++ ,->| a |-. <+ ,->| br |->NULL
+--------+ | + | +---+ | + | +----+
v + | v + |
,----------------. | ,---.+ |
(" starting...tag,") | ("bed") |
`--------+---+---' | +---+ |
| ? |------' | ? |--'
+---+ +---+
</pre></p>
<h4>Comments</h4>
<p>
In "PM_TAG_START" mode (after the '<'), also an '!' can follow, indicating that
we have not any tag at all, but either a comment, a DOCTYPE declaration, or a
CDATA section. "parse_mode" is set to "PM_EXCLAM" in this case.
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
</pre></p>
<p>
If a '-' follows, it's a comment. Mode is switched to "PM_COMMENT_START".
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
</pre></p>
<p>
Now a second '-' has to follow, switching to "PM_COMMENT". In this mode any
characters but a '-' are simply ignored.
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
text_buf: "some text"
</pre></p>
<p>
A '-' switches to "PM_COMMENT_END1", which means that it *may* be the comment end.
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
</pre></p>
<p>
However, if it is followed by any other char than a second '-', mode is
switched back to "PM_COMMENT".
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
</pre></p>
<p>
A second '-' in "PM_COMMENT_END1" switches to "PM_COMMENT_END2", which means
that now the comment really ends.
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
</pre></p>
<p>
Now the '>' has to follow, and switches back to parsing mode before the
beginning of the comment ("prev_mode_tag").
</p>
<p><pre>
input: some text <!--a test-comment--> and more text
^
</pre></p>
<h4>DOCTYPE Declarations</h4>
<p>
If a normal char occurs in "PM_EXCLAM", we assume it is the "D" in "<!DOCTYPE".
</p>
<p><pre>
input: garbage <!DOCTYPE somedoc> more garbage
^
</pre></p>
<p>
We treat DOCTYPE declarations as comments. Any characters but '>' are ignored.
</p>
<p><pre>
input: garbage <!DOCTYPE somedoc> more garbage
^
text_buf: "garbage"
</pre></p>
<p>
A '>' returns to normal mode.
</p>
<p><pre>
input: garbage <!DOCTYPE somedoc> more garbage
^
</pre></p>
<p>
This isn't a very reliable detection, as according to the grammer, an unescaped
'>' may appear in some system literal inside the declaration. However, we
assume that this won't happen... (We would have to parse the whole declaration
otherwise.)
</p>
<h4>CDATA Sections</h4>
<p>
A '[' in "PM_EXCLAM" mode starts a CDATA section, indicated by
"PM_CDATA_START". If there is a pending blank ("prev_mode_tag" is "PM_BLANK"),
it has to be stored *before* the CDATA.
</p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
text_buf: "some text "
</pre></p>
<p>
Following normal chars (should) belong to the "CDATA" string, and are ignored.
</p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
text_buf: "some text "
</pre></p>
<p>
A second '[' in "PM_CDATA_START" mode switches to "PM_CDATA", indicating that
the actual data will follow.
</p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
</pre></p>
<p>
Any characters in "PM_CDATA" mode but '>' are stored directly to "text_buf".
</p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
text_buf: "some text a tricky ]"
</pre></p>
<p>
When a '>' occurs, the previous two chars (in "text_buf") are tested against
"]]". If they do not match, the '>' is simply stored just as any other
character.
</p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
text_buf: "some text a tricky ]>"
</pre></p>
<p>
If they match, the last two characters are removed from "text_buf" (they belong
to the CDATA terminator), and mode is switched back to "PM_CONTENT". (It
doesn't need to be switched back to the mode before the CDATA section, as any
pending blanks already have been stored, and a CDATA section can't start in
other modes than "PM_TEXT" or "PM_BLANK".)
</p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
text_buf: "some text a tricky ]> CDATA section]]"
</pre></p>
<p><pre>
input: some text <![CDATA[a tricky ]> CDATA section]]> and more text
^
text_buf: "some text a tricky ]> CDATA section"
</pre></p>
<h4>Processing Instructions</h4>
<p>
The '<' may also be followed by a '?', indicating a processing instruction.
Mode is switched from "PM_TAG_START" to "PM_INSTR".
</p>
<p><pre>
input: some text <?a fake? processing instruction??> more text
^
</pre></p>
<p>
Processing instructions are also treated as comments. Any chars but '?' are
ignored in "PM_INSTR".
</p>
<p><pre>
input: some text <?a fake? processing instruction??> more text
^
text_buf: "some text"
</pre></p>
<p>
A '?' switches to "PM_INSTR_END", indicating this *may* be the end of the
processing instruction.
</p>
<p><pre>
input: some text <?a fake? processing instruction??> more text
^
</pre></p>
<p>
If a normal char follows the '?', mode is switched back to "PM_INSTR".
</p>
<p><pre>
input: some text <?a fake? processing instruction??> more text
^
</pre></p>
<p>
If a second '?' follows, "PM_INSTR_END" is kept, as the first one isn't the end
of the processing instruction, but the new one could be.
</p>
<p><pre>
input: some text <?a fake? processing instruction??> more text
^
</pre></p>
<p>
A '>' in PM_INSTR_END really ends the processing instruction, and switches to
"prev_mode_tag".
</p>
<p><pre>
input: some text <?a fake? processing instruction??> more text
^
</pre></p>
<h3>SGML Mode</h3>
<p>
When compiled without the "-DXHTML_ONLY" option, a few cases more are possible.
</p>
<h4>Unclosed Tags</h4>
<p>
In SGML, not every element has to have an end tag.
</p>
<p>
When an end tag is encountered, we ascend in the syntax tree not only once, but
until an element is found that matches the end tag. Thus, all elements in
between are automatically closed.
</p>
<p><pre>
input:
<body>
<p>
some text
<hr>
</p>
^
+------+
| body |-.<+
+------+ | +
v +
+---+
| p |-.<+
+---+ | +
v +
,-------.
(some text)
`-+----+'
--> | hr |->NULL
==> +----+
</pre></p>
<p><pre>
input:
<body>
<p>
some text
<hr>
</p>
^
+------+
| body |-.<+ <==
+------+ | +
v +
+---+
| p |-.<+
+---+ | +
v +
,-------.
(some text)
`-+----+'
--> | hr |->NULL
+----+
</pre></p>
<h4>Unquoted Attribute Values</h4>
<p>
When a normal char occurs in "PM_ATTR_VALUE" mode, "PM_ATTR_DATA_NOQUOTE" is entered.
</p>
<p><pre>
input: <sometag someattribute=somevalue minimized third="nothing">
^
</pre></p>
<p>
This mode is just like "PM_ATTR_DATA_QUOT" or "PM_ATTR_DATA_APOS", only it
is ended by a blank or the tag end.
</p>
<p><pre>
input: <sometag someattribute=somevalue minimized third="nothing">
^
</pre></p>
<h4>Mimimized Attributes</h4>
<p>
In SGML, attributes without a value are possible. This is recognized when a
normal char or the tag end occurs in "PM_ATTR_NAME_END" mode instead of the
'='.
</p>
<p><pre>
input: <sometag someattribute=somevalue minimized third="nothing">
^
</pre></p>
<p>
The attribute is ended immediately. "text_buf[]" (which is empty in this case)
is stored just like at the end of an unquoted attribute value. Mode is set to
"PM_TAG", and the current character (the tag end or beginning of next
attribute) is processed in this mode.
</p>
<h4>SGML Comments</h4>
<p>
Comments also allow more complicated syntax. For one, blank space is possible
between the "--" ending the comment string and the '>' ending the declaration.
Thus, blank space in PM_COMMENT_END2 is ignored.
</p>
<p>
Moreover, another comment string may follow the end. Thus, a '-' in
PM_COMMENT_END2 switches back to PM_COMMENT_START, similary to the '-' after
the "<!".
</p>
<p><pre>
input: <!--comment start-- --second comment string in same declaration-- >
^
</pre></p>
<p>
Finally, SGML also allows empty declarations ("<!>"), which are also a kind of
comment. Thus a '>' in PM_EXCLAM switches immediately to PM_COMMENT_END2 and
recycles.
</p>
<p><pre>
input: <!>
^
</pre></p>
<h4>Unclosed Tags</h4>
<p>
In SGML, tags needn't be closed by '>', if the tag end can be deduced from the
context. In practise, this means that a tag can also be ended by a '<'
character, which may be the beginning of a following tag. Thus we have to
handle this in all situations where a '>' could also occur.
</p>
<h4>Unhandled Constructs</h4>
<p>
SGML also allows some constructs that aren't recognized by any browser we know
of. These include empty tags (<> and </>) and "net mode".
</p>
<p>
Netrik recognizes these constructs and prints a warning, but doesn't handle
them either -- there is no point in this, as autors couldn't use them anyways
due to lack of support in other browsers. Handling them correctly would
actually even break some pages, because it would behave different than all
other browsers.
</p>
<h4>Loose '&' and '<' Chars</h4>
<p>
If some illegal char occurs in a entity/character reference, it's not really a
reference, but an unescaped '&'. We keep the whole sequence literally and
switch back to "prev_mode_amp".
</p>
<p><pre>
input: x = a & b
^
text_buf: "x = a & "
</pre></p>
<p>
Similar for illegal characters in "PM_TAG_START" (and some other PM_TAG*
modes), which indicate an unescaped '<'. We store a '<' and switch back to
"prev_mode_tag".
</p>
<p><pre>
input: if(a < b)
^
text_buf: "< "
</pre></p>
</a> <!-- parsing -->
<h3>Finishing</h3>
<p>
Parsing is ended by EOF. This should only appear in "PM_CONTENT" or "PM_BLANK"
mode (not inside some tag, comment, CDATA section or chracter/entity
reference), and only if the current element is the global one (not while
parsing some element's content).
</p>
<p>
The "list_next" pointer of the last node is set to point back to the tree top.
This faciliates easier processing in the following steps.
</p>
<h3>Error Handling</h3>
<p>
When using -DXHTML_ONLY, every syntax error encountered causes netrik to print
an error message and immediately quit. (The XML standard requires this.)
</p>
<p>
Without -DXHTML_ONLY, netrik is more tolerant.
</p>
<h4>Workarounds</h4>
<p>
Netrik uses simple workarounds for some of the most common cases of broken
HTML.
</p>
<p>
Most notable is comment parsing: As SGML comments have a quite complicated
syntax, reasonable error handling is also quite complicated.
</p>
<p>
If someting else then '>' (end of comment declaration), '-' (beginning of
second comment string), or blank space follows in PM_COMMENT_END2 (after a
"--"), then the "--" was probably not intended to have any special meaning, but
simply to be part of the comment. Thus, mode is switched back to PM_COMMENT.
</p>
<p><pre>
input: <!-- some broken -- comment -->
^
</pre></p>
<p>
The same is done for unexpected characters in PM_COMMENT_RESTART mode, which is
most common for "---" inside a comment.
</p>
<p><pre>
input: <!-- some broken --- comment -->
^
</pre></p>
<p>
There is one exception to this, however: If a '>' follows in PM_COMMENT_START
mode, and it was preceeded not only by one '-' (the one which started
PM_COMMENT_START) but two or more, then the the '>' together with the last two
'-' was probably intended as as an XML-like "-->" comment end.
</p>
<p><pre>
input: <!--- anything --->
^
parse_mode: PM_COMMENT_START
dash_count: 3
</pre></p>
<p>
The "dash_count" variable keeps track of how many dashes have been encountered
in a row; it is incremented every time a '-' apprears in some of the comment
parsing modes, and is reset to 0 every time some other character is
encountered.
</p>
<p>
"dash_count" is also used in another situation: If a '>' follows in PM_COMMENT
or PM_COMMENT_END1, normally it is part of the comment. The '>' is ignored and
mode stays PM_COMMENT. (Or is switched back from PM_COMMENT_END1.)
</p>
<p><pre>
input: <!-- comment with > and -> in it -->
^
</pre></p>
<p>
However, if there were two or more dashes in front of the '>', this "-->"
combination was probably also intended as a comment end. A comment consisting
of a series of dashes is a typical example:
</p>
<p><pre>
input: <!------>
^
parse_mode=PM_COMMENT
dash_count=6
</pre></p>
<p>
However, only a little warning can be printed in this case -- this is valid
SGML, and *has* to be treated as part of the comment, even if it's probably not
what the page author intended! Printing an error and using a workaround would
mean deliberately to violate the standard in favour of broken pages, which is
probably not a very good idea...
</p>
<p>
There is another trick however, which contervails this in most situations: As
soon as any clear error is detected, a "broken" flag is set for the time of
this comment. If the above situation occurs afterwards, we treat it as an error
and abort the comment -- as we are sure that the comment has errors, there is
no point in continuing as if the comment was correct.
</p>
<p>
There are also a couple of specific workarounds for tags:
</p>
<p>
Spurious quotes inside the attribute value are quite common when the autor
forgets the opening quote but not the closing one. These have to be ignored. (A
warning is printed, but they aren't stored as part of the value or handled
otherwise.)
</p>
<p>
Very often we find illegal characters in unquoted attribute values. (According
to the standard, only name characters are allowed here.) These produce a
warning, but are otherwise handled like legal chars -- as long as there are not
ambigious. (A '<' is always an error for example, as it usually indicates
another tag start.)
</p>
<p>
Other unexpected characters in tags (e.g. "a<b =" or something the like) are
handled by immediatly aborting tag parsing and returning to normal mode. This
seems the surest bet, because such a situation usually indicates that the
construct wasn't intended as a tag at all, only looked similar by incident. By
bailing out as soon as possible, we try to limit the damage -- staying in tag
mode might produce more critical problems, like hiding or misinterpreting
considerable parts of the remaining document.
</p>
<p>
Note that even better would be storing the whole preceeding part of the
presumed tag literally as content. it would be much more complicated however;
we haven't bothered to implement this.
</p>
<p>
Unexpected characters in any other mode are simply ignored, hoping for the
best.
</p>
<h4>html_error()</h4>
<p>
Whenever some syntax error is detected (no matter whether workarounds are
available), html_error() is called, with several parameters describing the
error. This function is responsible for everything that needs to be done when
an error occurs.
</p>
<p>
Before taking any action, the requested error message is tested against an
array with all errors printed so far. Only if the message is new, the function
proceeds; otherwise, an "ignored"-counter is incremented, and the function
returns early.
</p>
<p>
Only now html_error() starts it's normal operation: First, it prints an the
message. The message text is passed from parse_syntax(), and used as the format
string for printf(). If the error message requires additional arguments, they
are passed at the end of the parameter list when calling html_error().
</p>
<p>
If the parsing mode requires that, html_error() quits immediately afterwards.
The mode is determined by the config variable "cfg.parser", which is an "enum
Parser_mode", with the possible values FUSSY_HTML, CLEAN_HTML, VALID_HTML,
BROKEN_HTML and IGNORE_BROKEN. The parser quits only in FUSSY_HTML mode, or
when -DXHTML_ONLY is enabled. If the input resource from which the page is
loaded is a pipe from wget (see
<a href="hacking-load.html">hacking-load.*</a>, the pipe is closed
before quitting to assure a cleaner exit.
</p>
<p>
In all other modes, an additonal message passed from parse_syntax() is printed
afterwards, informing in which way netrik will handle the error. (workaround,
ignore etc.)
</p>
<p>
Finally the error level passed from parse_syntax() is compared against the
highest error level up to know, and the new higest level is returned.
</p>
$$<a name="warn" id="warn">
<h4>Warning messages</h4>
<p>
parse_syntax keeps track of most severe syntax error that was found while
parsing the page in "err_level", which is of type "enum Syntax_error" and can
have the following values:
</p>
<ul> <li>
SE_NO: No errors were found
</li> <li>
SE_BREAK: The user issued an interrupt (SIGINT) while loading the document.
This isn't really an error, but can be handled very convenient this way...
</li> <li>
SE_DISCOURAGED: Some constructs were found that are strictly speaking valid
SGML, but explicitly discouraged in the HTML standard. These may be handled
differently by other browsers -- especially comments.
</li> <li>
SE_UNIMPLEMENTED: Also valid SGML and discouraged in HTML, but not handled
correctly by netrik nor any other popular browser.
</li> <li>
SE_WORKAROUND: Real errors were found, but workarounds could be applied that
work in most cases.
</li> <li>
SE_CRITICAL: Something went terribly wrong: We have an error situation which
we can not make out, and thus no useful workaround could be applied. The page
almost certainly will look broken, often with considerable parts or the
content missing. (e.g. a misinterpreted comment or missing closing quote)
</li> <li>
SE_FAIL: This isn't really a syntax error. It is not used inside
parse_syntax() itself; it's only set before returning when a file loading
error was detected, for the sake of the calling function.
</li> <li>
SE_NODATA: Similar to SE_FAIL. This is set if EOF is returned by load()
before *any* data has been read.
</li> </ul>
<p>
After the whole page is parsed, a warning message is printed if some error was
found. The message text depends on the error level. The error level is also
passed back to main(), which then waits for a keypress before starting the
pager, so the message will be seen.
</p>
<p>
In IGNORE_BROKEN mode the warning is suppressed, and "err_level" is reset. In
BROKEN_HTML mode, all but SE_CRITICAL errors are suppresed; and in VALID_HTML,
all but SE_CRITICAL and SE_WORKAROUND.
</p>
<p>
SE_BREAK is set if EOF is returned, but at the same time "input->user_break"
has been set, indicating that it's not really EOF, but transfer was interrupted
by the user. Other errors are supressed in this mode, as a user break during
loading might cause several syntax errors (unclosed elements etc.) with the
page itself being not to blame for.
</p>
<p>
SE_NODATA is set if EOF is returned by load() before any data has been read.
(This can be caused by failure to open the resource, but also by an empty
file/http response.) It's handled like a normal syntax error; the only
difference is that it can't be masked even by IGNORE_BROKEN. The syntax tree
consists only of the global element; it will be correctly rendered to an empty
page.
</p>
<p>
SE_FAIL isn't set during parsing. Before returning, parse_syntax() checks
whether "input->type" is RES_FAIL; if it is, an error message is printed, and
SE_FAIL is set (so main() knows an error occured). However, this test is only
necessary if SE_NODATA isn't set; otherwise, an error message has already been
printed and an error code would be returned anyways. As EOF is returned by
load() also when an error occured, SE_NODATA is already set for most errors;
SE_FAIL is only used if the error occurs after some data could be read.
</p>
</a> <!-- warn -->
<a name="freeSyntax" id="freeSyntax">
<h3>free_syntax()</h3>
<p>
This function is responsible for freeing the memory used by the syntax tree
when it is no longer needed.
</p>
<p>
The whole tree is traversed by "list_next", and the element nodes are freed one
by one.
</p>
<p>
As the "list_next" pointer is necesary to find the next node, but not longer
available after freeing the current node, it is saved in "next_el" before
freeing. At the the beginning of the next iteration this is copied to "cur_el".
</p>
<p>
Before freeing the element node itself, all dynamic data belonging to the node
has to be freed.
</p>
</a> <!--freeSyntax-->
</a> <!--parseSyntax-->
<a name="dumpTree" id="dumpTree">
<h2>3. dump-tree.c</h2>
<p>
dump_tree() is primarily used for dumping the syntax tree generated by
parse_sytax() for debugging purposes. The reason it resides in an own file is
that it could be easily modified to be a really useful function for dumping a
HTML document's structure. This may be implemented in the future, if someone
shows interest...
</p>
<p>
The implementation of dump_tree() is quite straightforward, as the function
only needs to print every node in the order it occured in the HTML file.
</p>
<p>
For every node, first the text is printed. (If "dump_content" is given.) The
reason it is printed in front of the node itself is because it's also the
content in front of the element in the original HTML file.
</p>
<p>
Next, the current tree depth is shown by a number of '|'. The current depth is
always stored in "depth".
</p>
<p>
Afterwards the element name is printed, and all attributes with their values.
Depending on "elements_parsed", either the raw values extracted from the
document are printed, or the transformed values generated by parse_elements().
</p>
<p>
The next node is reached by the "list_next" pointer of the current node. Before
going to the next node the tree depth of the new node needs to be calculated.
This is done by assuming that the next node is below the current one, and than
going up, until we find the parent of the new node. This idea is explained in
more detail in <a href="#parseStruct">6. parse-struct.c</a>.
</p>
</a> <!--dumpTree-->
<a name="parseElements" id="parseElements">
<h2>4. parse-elements.c</h2>
<p>
parse_elements() is responsible for making out the elements and attributes from the
syntax tree. (Extraced by parse_syntax().)
</p>
<p>
All elements but the first one (which is always ELEMENT_GLOBAL) are processed one
by one; the tree is traversed via "list_next". For every element, the name is
looked up in "element_table[]" by comparing to all entries, in a loop.
</p>
<a name="elementTable" id="elementTable">
<p>
"element_table[]" contains all names of ordinary elements, then the "?"
representing ELEMENT_NO, and finally "!" representing ELEMENT_GLOBAL. (It also contains
other properties of the elements; more on this in
<a href="#parseElementsProcessing">Processing</a> in
<a href="#parseStruct">6. parse-struct.c</a>.)
</p>
</a> <!--elementTable-->
<p>
The last two aren't checked against the element name.
</p>
<p>
As soon as a match is found, the loop is left and the entry number is stored to
"syntax_tree" in place of the string. The entry number is an "enum
Element_type", defined in syntax.h; it tells the element type in the following
processing passes.
</p>
<p>
If none of the ordinary entries matched, the entry nuber, which now is ELEMENT_NO
(as this one follows after the ordinary entries), is stored anyhow, indicating
that the element is unknown. If no element name was stored ("cur_el->name.str"
is NULL), indicating a dummy tag, ELEMENT_NO is set also.
</p>
<p>
After the element name, all attribute names are processed in a loop. They are
looked up in "attr_table[]" the same way the element name is.
</p>
<p>
The attribute value isn't processed at all yet.
</p>
</a> <!--parseElements-->
<a name="sgmlRework" id="sgmlRework">
<h2>5. sgml_rework()</h2>
<p>
Before the syntax tree is further processed, sgml_rework() (from sgml.c) is
applied. (Unless compiled with -DXHTML_ONLY.)
</p>
<p>
This function is responsible for fixing the problems arising from the fact that
SGML allows certain end tags to be left out; thus the syntax parser doesn't
recognize the elements' ends, and stores all following elements as children,
even if they should actually be at the same level. (e.g. the list items in a
list.) sgml_rework() goes over the complete (broken) tree, finds such
situations, and unnests the elements, thus creating a correct syntax tree.
</p>
<p>
It won't be covered in too much detail here, as this is only a temporary
solution; it will become obsolete with the planned new parser(s).
</p>
<p>
The recognition of the missing element ends is done by the "element_group" enum
in "<a href="#elementTable">element_table[]</a>". This Enum has the
values GROUP_SINGLE for single tag elements (elements which mustn't have any
content), GROUP_OBLIGATE for all elements where the end tag can't be left out,
and several others for various kinds of elements with optional end tag.
</p>
<p>
The whole tree is scanned element by element (using "list_next"), and each one
is tested to fullfill one of the offending conditions. Two things have to be
handled: Unclosed single tag elements, and unclosed optional end tag elements.
</p>
<p>
The second situation is more complicated. If the element is of some type with
optional end tag, it could terminate a previous (unclosed) element from the
same group; e.g. a <li> will terminate the previous <li>. It doesn't terminate
elements from other groups, though; a <td> inside a <tr> doesn't terminate the
<tr>, for example. Thus, the group of the current element needs to be tested
against the group of the parent (all elements following an unclosed one are
stored as its children by the parser!); if they are the same, the parent is
actually an element that should be terminated at the position where the child
starts, and the child should follow it, at the same depth. This means that the
child has to be "lifted" out of the parent. However, we don't do that
immediately; we only set the "closed" flag of the parent element for now, and
the lifting will be done later.
</p>
<p>
However, it's not enough to test only the immediate parent: The element may
follow some other unclosed element, and thus be a child of it, e.g. a <tr>
following a <td>, which is inside the previous <tr>. This also needs to be
recognized, and *both* the previous <tr> and the <td> have to be closed. Thus,
not only the immediate parent's group is compared, but all ancestors are
scanned. The scanning only stops on an element with obligate end tag -- as the
element's end is always known for these, nothing will be ever stored inside
that element that doesn't belong there, and nothing should be lifted out. (In
nested tables for example, the <tr>s and <td>s of the inner table shouldn't
mess with the ones of the outer table -- this is ensured by the scanning of the
inner table's rows and columns stopping at the inner <table> element.) The
"closed" flag is set for all the closed ancestors.
</p>
<p>
Some more handling is necessary due to the fact that an element node always
stores the content which appears *before* the element. When an element is
lifted, the content musn't be lifted also -- it appeared *before* the element,
and thus also before the previous element's end, so it has to stay where it is.
We have to create a new dummy tag inside the closed element therefore, taking
the place of the lifted element and storing its content.
</p>
<p>
However, this isn't done when the parent was already closed. This happens if
the parent is a single tag element. These elements end right where they start,
not at the beginning of the next element; the content also has to be lifted
out. (Nothing is allowed to stay inside a single tag element!)
</p>
<p>
The actual lifting is done after processing the element: If the parent is
closed, we have to "leave" it. (This is done by setting the "parent" to the
previous grandparent -- this way, the element is no longer a child of the old
parent, but a sibling.) Thus the element that closed it's parent is lifted
right afterwards; all following elements of the parent will be lifted also,
after being processed. Any preceeding elements (as well as the possibly created
new dummy) won't be lifted on the other hand, as they won't be processed
anymore.
</p>
<p>
Single tag elements are handled more or less the other way round: They are not
closed by some child (which turns out to actually be a sibling), but close
*themselfs*, as soon as they are encountered. This way all children will be
lifted out, no matter what.
</p>
<p>
No other processing is necessary for single tag elements, as they won't ever
terminate some other element.
</p>
</a> <!-- sgmlRework -->
<a name="parseStruct" id="parseStruct">
<h2>6. parse-struct.c</h2>
<p>
After the syntax tree was generated by parse_syntax(), we have to "understand"
it. This is done by parse_struct(), which is the central pass of the layouting
process. In this function the syntax tree, which contains a nearly 1:1
reproduction of the HTML file, is converted to an item tree, which contains a
representation of what will be actually shown as the output of the browser --
text blocks, blank rows, boxes grouping severel other items.
</p>
<h3>Structure Tree</h3>
<p>
For 0.html, we have to convert the syntax tree:
</p>
<p><pre>
++>NULL
+
+---+
| ! |-. <++
+---+ | +
v +
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+ <+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| head |->| body |-. <++++++++++++++ +
+------+ +------+ | <+ + +
v + + + <+++++++++++++++++++++++++++++++++++++++++++++++++
+----++ +---+ <++++++++++++++++++++ +---+ <++++++++++++++++++++++++++++++++ +
| h1 |-. <+ ,->| p |---. <++ + ,->| p |-. <++++++++++++++++++ + + (back
+----+ | + | +---+ | + + | +---+ | <+ + + + to top)
v + | v + + | | + + + + ^
,--------. | ,------------------. ,----. | | + ,----------. ,---------. ,---------------. |
(" heading") | (" first...newlines,") (" and") | v + (" this...em") ("ded...row") (" (a single tag)") |
`--+---+-' | `-----+----+-------' +-+----+-+ | +--------+ `---+---+--' `--+----+-' `-----+---+-----' |
| ? |----' | em |-. <++ ,->| strong |-. <++ | | center |-. <++ ,->| a |-. <+ ,->| br |---------->| ? |--------'
+---+ +----+ | + | +--------+ | + | +--------+ | + | +---+ | + | +----+ +---+
v + | v + | v + | v + |
,----------------. | ,------------. | ,----------------. | ,---.+ |
(" emphasized text") | (" strong text") | (" starting...tag,") | ("bed") |
`--------+---+---' | `-----+---+--' | `--------+---+---' | +---+ |
| ? |------' | ? |-----' | ? |------' | ? |--'
+---+ +---+ +---+ +---+
</pre></p>
<p>
to this structure tree:
</p>
<a name="itemTree" id="itemTree">
<p><pre>
***> "string" (back <+++++++++
xxx> "first_child" to first +
+++> "parent" item) +-----+
===> "next" ,->| box |-->NULL
---> "list_next" | +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x | +
x ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
x + + + + + + + + + |
v + + + + + + + + + |
+------+ +-------+ +------+ +-------+ +------+ +-------+ +------+ +------+ +------+ |
| text |-->| blank |-->| text |-->| blank |-->| text |-->| blank |-->| text |-->| text |-->| text |-'
+----*-+==>+-------+==>+----*-+==>+-------+==>+----*-+==>+-------+==>+----*-+==>+----*-+==>+----*-+==>NULL
x * x x * x x * x x * x * x *
v * v v * v v * v v * v * v * ,--------------.
NULL * NULL NULL * NULL NULL * NULL NULL * NULL * NULL **>("(a single tag)")
v v v v * `--------------'
,-----------. ,-------. ,------------. ,---------------. * ,----------------.
("header text") ("heading") ("first...text") ("starting...tag,") **>("this...blank row")
`-----------' `-------' `------------' `---------------' `----------------'
</pre></p>
</a> <!--itemTree-->
<p>
which, in turn, is a representation of this output page:
</p>
<a name="itemPage" id="itemPage">
<p><pre>
+-------------------------------------------------------------------------------------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| |
|+-------+ |
||heading| |
|+-------+ |
| |
|+-----------------------------------------------------------------------------------------------+|
||first paragraph of text; includes multiple spaces and newlines, emphasized text and strong text||
|+-----------------------------------------------------------------------------------------------+|
| |
|+---------------------------------+ |
||starting with an evil center tag,| |
|+---------------------------------+ |
|+-----------------------------------------------------------------------------------------------+|
||this very long second paragraph contains some special characters (including a simple space...):||
|| &; <>"=/ plus a big gap###and two unicode escapes (decimal: and hexal: ) but also an anchor||
|| embedded inside a word (this anchor also is the only tag with parameters); and finally a blank||
|| row ||
|+-----------------------------------------------------------------------------------------------+|
|+--------------+ |
||(a single tag)| |
|+--------------+ |
+-------------------------------------------------------------------------------------------------+
</pre></p>
</a> <!--itemPage-->
<p>
Note that there are no actual sizes or postions for the items, and no line
breaks inside the text items; this is all done in a later processing step
(pre-render.c). The item tree at this point only represents the structure of
the output page. (The line breaks in the fifth text block aren't really there;
we have inserted them in the figure because the text block is a bit too long to
put in a single line...)
</p>
<p>
The item tree looks complicated at first, but it's a quite trivial example when
taking a closer look. (This is becuase at the time of creating the 0.html file
used here, netrik wasn't able to do anything more complicated...) However, it
should be sufficient to get the idea...
</p>
<p>
Every node of the item tree consists of an "Item" structure. This structure is
declared in "items.h". It contains:
</p>
<ul> <li>
The pointers "list_next", "next", "parent", and "first_child" connect the
nodes inside the tree. "list_next" points to the next node in the order they
are generated. Note that in contrast to the element tree, in the item tree
any children are generated *before* the parent. "next" points to the next
item at the same tree depth and in the same branch, i.e. the next sibling.
"parent" points to the parent item, and "first_child" to the first sub-item.
More pointers are necessary than for the element tree, because the item tree
is traversed in several differnt ways while processing.
</li> <li>
The "center" flag indicates whether the item is centered. As not all items
use this, and the exact meaning varies between different item types, it may
be reasonable to move this to the item specific data. We'll decide on this as
soon as enought HTML facilities are implemented.
</li> <li>
"x_start", "x_end", "y_start" and "y_end" define a square area inside the
layouted page, in which the item is displayed. In some processing steps
"x_end" end "y_end" are also "abused" to store the minimal size of the item.
</li> <li>
"type" is an enum storing what kind of item this node represents. (Currently
ITEM_TEXT, ITEM_BOX, ITEM_FORM, ITEM_BLANK, ITEM_BLOCK_ANCHOR or
ITEM_INLINE_ANCHOR.)
</li> <li>
"data" is an union storing all data specific to different item types.
Currently this can be the pointer to a text string for text items, to an
(block or inline) anchor struct, or to a form paramters struct.
</li> </ul>
<h3>add_item()</h3>
<p>
New items are created by add_item(). This function allocates a new "Item"
structure, and sets some pointers. The item isn't inserted into the tree
directly; it's only inserted into a list of all items at the current tree
depth. This is a single linked list maintained by the "first_item" and
"last_item" pointers of "state" (there is one such list for each tree depth),
and the "next" pointers of the item structures. The only other pointers set are
"list_next". The "parent" and "first_child" pointers aren't set; this is done
later when the items are actually inserted into the tree, while ascending from
the current depth. (For the first item in every tree depth the "parent" is
explicitly set to NULL, indicating that there is no parent yet.)
</p>
<p>
This function is called directly to create box items, and from add_string() to
create text items. Under certain conditions it also creates a blank item before
the actual text item or box item; more on this later, under
<a href="#blankLines">Blank Lines</a>.
</p>
<p>
When called with the "virtual" flag, this function behaves slightly different:
No line break/blank line handling is done; the status remains unchanged. This
is for creating the <a href="#virtual">Virtual Boxes</a> used for
anchors.
</p>
<a name="string" id="string">
<h3>String</h3>
<p>
The actual text data of text items is stored in a different place. Every text
item points to a "String" structure (also declared in "items.h"). This
structure consists of a normal C-string containing the text itself, and an
array of "Div" structures, containing all attribute information. (Color etc.) A
"String" can consist of several divisions with different attributes. Every
"Div" structure stores the attributes for one such division, and the ending
position of the division inside the string. (More exactly: the position *after*
the end of the div -- which is the starting position of the next div.) The end
of the last division is also used to find out the string length. This is quite
inefficient...
</p>
<p>
The "String" structure also contains "line_table[]", which holds the positions
of all line breaks inside the string; more on this in <a
href="#preRender">7. pre-render.c</a>.
</p>
<p>
Finally, it contains an array of
"<a href="hacking-links.html#linkStruct">Link</a>" structures, which
describe all the links (and form elements) inside this text block. See
<a href="hacking-links.html">hacking-links.*</a> for this.
</p>
</a> <!-- string -->
<a name="parseElementsProcessing" id="parseElementsProcessing">
<h3>Processing</h3>
<p>
Many properties of the various element types are data-driven. This presently
includes the line break/blank line handling, elements creating a box around all
children, and elements whose content is not to be rendered. This properties are
stored to the same "<a href="#elementTable">element_table[]</a>" as
the name strings used by parse_elements(). In future probably more properties
will be data driven, making the code simpler, and also necessary for handling
style sheets, which allow changing of almost all formatting properties.
</p>
<p>
The item tree is generated while traversing the element tree. Processing of
each element is done in two steps: One step is done before entering an element
(descending in the element tree), and the second step is done after leaving the
element (ascending). Between those two steps, the same is done for all
sub-elements. You guess it: This is a recursive algorithm. Only we haven't
implemented it recursively, as mentioned in chapter 0. In this function the
pseudo-recursive implementation is most evident; we even have to use a
pseudo-stack.
</p>
<p>
The processing could also be split into a couple of much simpler passes using
some temporary data structures, e.g. one generating the "normal" items, one
generating blank items, one generating the strings, one "optimizing" the tree
(lifting items where possible). This would be much easier to understand, and
probably it would have been a good idea for the beginning. However, it would be
much less efficient; that's why we won't step back to such an implementation
after already having the present one.
</p>
<h4>Pre-processing</h4>
<p>
The first action in every iteration of the outer loop is to do the first
processing step (s.a.) for the current element ("cur_el") -- in every iteration
exactly one element is pre-processed.
</p>
<p><pre>
++>NULL
+
+---+
| ! |-. <++
+---+ | +
v +
,-----------.
("header text")
`-+------+--' <-- cur_el <== depth
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+
| head |->| body |-.
+------+ +------+ |
</pre></p>
<p>
First, we store any text from the current element node to the current open text
item by add_string().
</p>
<p><pre>
state[0]->first_item -->NULL (depth 0)
state[0]->last_item **>NULL
---
+------+ <-- first_item
| text |-->NULL <** last_item <== depth=1
+----*-+==>NULL
x *
v *
NULL *
v
,-----------.
("header text")
`-----------'
</pre></p>
<p>
The next thing is processing of line breaks and paragraph breaks, depending
what kind of element we have. More on this later, in <a
href="#textBlocks">Text Blocks</a> and <a
href="#blankLines">Blank Lines</a>.
</p>
<h4>Recursing</h4>
<p>
Then we recurse into the element (descend in the element tree).
</p>
<p><pre>
++>NULL
+
+---+
| ! |-. <++
+---+ | +
v +
,-----------.
("header text")
`-+------+--' <-- cur_el
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+
| head |->| body |-. <== depth
+------+ +------+ |
</pre></p>
<p><pre>
state[0]->first_item -->NULL (depth 0)
state[0]->last_item **>NULL
---
+------+ <-- state[1].first_item (depth 1)
| text |-->NULL <** state[1].last_item
+----*-+==>NULL
x *
v *
NULL *
v
,-----------.
("header text")
`-----------'
---
first_item -->NULL <== depth=2
last_item **>NULL
</pre></p>
<p>
This is done by push_state(). This function doubles the top of stack, and
returns a pointer to the newly created entry, which is used as the current
state. ("first_item" and "last_item" aren't copied, but set to NULL; "id_attr"
and "link_type" are set to -1.)
</p>
<p>
The stack stores all variables which are specific to every tree depth. Note
that the stack uses the depths from the element tree, not from the item tree.
The depths in the item tree are completely different, and change while
processing -- which is one of the most tricky parts about parse_struct().
</p>
<p>
Currently the data stored is:
</p>
<ul> <li>
Visibilty of element's content ("visible")
</li> <li>
Text attributes ("text_mode" and "high").
</li> <li>
The nesting depth "list_depth" of item lists. (Determines the indent of list
items.)
</li> <li>
The two pointers "first_item" and "last_item", necessary to maintain the list
of all items at a given depth.
</li> <li>
The type of the link or form control created by the element in the
"link_type" enum (a value of -1 indicates there is no link at all)
</li> <li>
The URL of links or value of form elements in the "link_value" string
</li> <li>
The "form_enabled" flag indicates whether a form element is to be submitted to the
server
</li> <li>
The name of a possible <select> element, bequeathed to its <option> elements
</li> <li>
The kind of the <select> element, also bequeathed
</li> <li>
"link_start" stores the position where a link or inline anchor beginns inside
the current string
</li> <li>
"link_item" stores the text item in which a link/anchor beginns
</li> <li>
For elements with an anchor, "id_attr" stores which attribute contains the
anchor id (or name)
</li> </ul>
<p>
After descending, first some generic processing is performed.
</p>
<p>
This includes setting "cur_state->visible" depending on the parent element
type's "visible" property.
</p>
<p>
Also, "link_start" is set to the current string end, so any text generated
inside this element will become part of the link or anchor, if the element
creates one. If there is no string item open, 0 is stored, so the link will
begin at the start of the string if a new string beginns inside the element.
</p>
<p>
Aferwards, some element type specific handling is done. Mostly this is
outputing of special element indicators. (This could be made data-driven, and
probably it will do so soon.)
</p>
<p>
For some element types, also values of the current state are modified; this
would be the argument passing in a real recursive implementation.
</p>
<h4>Ascending</h4>
<p>
The last step of every outer loop iteration is returning from recursion
(ascending in the element tree). But in contrast to descending, ascending isn't
done once per outer loop iteration. Instead, there is an inner loop, that
ascends as often as needed to reach the level of the next element -- this can
be zero, once or several times.
</p>
<p>
We know how long we need to ascend by starting at the level of the current
element and looking for the parent of the new element; as soon as we find it,
we know we needn't ascend any more. "depth" is adjusted every time we ascend,
and all actions for leaving the element (unrecursing) are taken. ("depth" is
always one below the depth of "new_el".)
</p>
<p>
If the next element is a child of the current one, no ascending is necessary.
We have descended one step in the pre-processing step of the current iteration,
and this is ok; we keep it. ("cur_el" is alredy the parent of "list_next".)
</p>
<p><pre>
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+
| head |->| body |-. <++++++++++++++
+------+ +------+ | <+ +
v + +
+----++ +---+
cur_el --> | h1 |-. <+ ,->| p |->
new_el xx> +----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' | <== depth
list_next **> | ? |----'
+---+
</pre></p>
<p>
If the next element is at the same level as the current one (single tags or
other elements with no sub-elements), we need to ascend exactly one time, to
get back to the level of the current element, after we have descended in the
pre-processing step.
</p>
<p><pre>
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+ <**
--> | head |->| body |-. <++++++++++++++
xx> +------+ +------+ | <+ +
v + +
+----++ +---+
| h1 |-. <+ ,->| p |-> <==
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
| ? |----'
+---+
</pre></p>
<p><pre>
,-----------.
("header text") <xx
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+ <**
--> | head |->| body |-. <++++++++++++++ <==
+------+ +------+ | <+ +
v + +
+----++ +---+
| h1 |-. <+ ,->| p |->
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
| ? |----'
+---+
</pre></p>
<p>
Of course it's a bit of overhead first to descend into an element, just to
ascend from it right after. But it saves a lot of code for special handling of
such childless elements. Many of the actions of both the first step and the
second step have to be done for them also -- putting them together and leaving
out the descending and ascending wouldn't save that much, while complicating
the code quite a lot. We may consider some way in the future if profiling shows
this would be rewarding.
</p>
<p>
If the next element is above the current one, we have to ascend more than once.
(Once to get back to the level of the current one, the others to ascend to the
new level.)
</p>
<p><pre>
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+
| head |->| body |-. <++++++++++++++
+------+ +------+ | <+ +
v + +
+----++ +---+
| h1 |-. <+ ,->| p |-> <**
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
--> | ? |----'
xx> +---+
<==
</pre></p>
<p><pre>
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+
| head |->| body |-. <++++++++++++++
+------+ +------+ | <+ +
v + +
+----++ +---+
xx> | h1 |-. <+ ,->| p |-> <**
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' | <==
--> | ? |----'
+---+
</pre></p>
<p><pre>
,-----------.
("header text")
`-+------+--'
| html |-. <++++++++
+------+ | <+ +
v + +
+------+ +------+ <xx
| head |->| body |-. <++++++++++++++
+------+ +------+ | <+ +
v + +
+----++ +---+
| h1 |-. <+ ,->| p |-> <** <==
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
--> | ? |----'
+---+
</pre></p>
<p>
In every ascending iteration, first we do some element type specific handling
again, mostly outputing element end indicators. Then we pop the previous state
from the stack.
</p>
<p><pre>
++>NULL
+
+ .--------------------------------.
+ v |
+---+ <xx |
| ! |-. <++ <** |
+---+ | + |
v + |
,-----------. |
("header text") | <==
`-+------+--' |
| html |-. <+++++++++++ |
+------+ | + |
| [...] [...]
[...] + |
| ,---------------. |
| (" (a single tag)") |
| `-----+---+-----' | <--
`--------->| ? |--------'
+---+
</pre></p>
<p><pre>
state[0]->first_item -->NULL (depth 0)
state[0]->last_item **>NULL
--- ,-- first_item
+------+ <--' +------+ +------+ <** last_item
| text |--[...]-->| text |-->| text |-->NULL <== depth=1
+----*-+==[...]==>+----*-+==>+----*-+==>NULL
x * x * x *
v * v * v * ,--------------.
NULL * NULL * NULL **>("(a single tag)")
v * `--------------'
,-----------. * ,----------------.
("header text") **>("this...blank row")
`-----------' `----------------'
</pre></p>
<p><pre>
++>NULL <xx
+
+ .--------------------------------.
+ v |
+---+ |
| ! |-. <++ <** | <==
+---+ | + |
v + |
,-----------. |
("header text") |
`-+------+--' |
| html |-. <+++++++++++ |
+------+ | + |
| [...] [...]
[...] + |
| ,---------------. |
| (" (a single tag)") |
| `-----+---+-----' | <--
`--------->| ? |--------'
+---+
</pre></p>
<p><pre>
first_item -->NULL <== depht=0
last_item **>NULL
--- ,-- state[1].first_item
+------+ <--' +------+ +------+ <** state[1].last_item (depth 1)
| text |--[...]-->| text |-->| text |-->NULL
+----*-+==[...]==>+----*-+==>+----*-+==>NULL
x * x * x *
v * v * v * ,--------------.
NULL * NULL * NULL **>("(a single tag)")
v * `--------------'
,-----------. * ,----------------.
("header text") **>("this...blank row")
`-----------' `----------------'
</pre></p>
<h4>Inserting into Item Tree</h4>
<p>
Afterwards, the probably most interesting part follows: The sub-items created
inside the element we are just leaving, are inserted into the item tree
properly.
</p>
<p>
If the element we are leaving enforces a box (looked up in "element_table[]"), a new
box item is created. (Box items are always created when leaving the element,
and thus *after* all items inside the box.)
</p>
<p><pre>
--> +-----+
**> ,->| box |-->NULL <== depht=0
| +-----+==>NULL
|
|
|
|
|
|
+------+ <-- +------+ +------+ | <** (depth 1)
| text |--[...]-->| text |-->| text |-'
+----*-+==[...]==>+----*-+==>+----*-+==>NULL
x * x * x *
v * v * v * ,--------------.
NULL * NULL * NULL **>("(a single tag)")
v * `--------------'
,-----------. * ,----------------.
("header text") **>("this...blank row")
`-----------' `----------------'
</pre></p>
<p>
The "parent" pointers of all immediate children are set to the new box item,
and "first_child" of the new item is set to the first of them.
</p>
<p><pre>
--> +-----+
**> ,->| box |-->NULL <== depht=0
| +-----+==>NULL
| x ^
xxxxxxxx[...]xxxxxxxxxxxxxxxxxxxxxxxxxxx +
x | +
x +++++[...]++++++++++++++++++++++++++++++
x + + + |
v + + + |
+------+ <-- +------+ +------+ | <** (depth 1)
| text |--[...]-->| text |-->| text |-'
+----*-+==[...]==>+----*-+==>+----*-+==>NULL
x * x * x *
v * v * v * ,--------------.
NULL * NULL * NULL **>("(a single tag)")
v * `--------------'
,-----------. * ,----------------.
("header text") **>("this...blank row")
`-----------' `----------------'
</pre></p>
<p>
If the element does not create a box, things are more tricky: We have to "lift"
all sub-elements to the new level. This is done by concatenating the list of
elements of the depth we are leaving to the list of elements of the depth we
are entering.
</p>
<p><pre>
state[0]->first_item -->NULL (depth 0)
state[0]->last_item **>NULL
---
+------+ <-- state[1].first_item (depth 1)
| text |-->NULL <** state[1].last_item
+----*-+==>NULL
x *
v *
NULL *
v
,-----------.
("header text")
`-----------'
---
state[2]->first_item -->NULL (depth 2)
state[2]->last_item **>NULL
--- ,-- state[3].first_item
+------+<-'+-------+ <** state[3].last_item (depth 3)
| text |-->| blank |-->NULL
+----*-+==>+-------+==>NULL
x * x
v * v
NULL * NULL
v
,-------.
("heading")
`-------'
---
+------+ <-- first_item
| text |-->NULL <** last_item <== depth=4
+----*-+==>NULL
x *
v *
NULL *
v
,------------.
("first...text")
`------------'
</pre></p>
<p><pre>
+------+ <+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
->| body |-. <++++++++++++++ +
+------+ | <+ + +
xx^ v + + +
+----++ +---+ <++++++++++++++++++++ +---+
==> | h1 |-. <+ ,->| p |---. <++ + **> ,->| p |->
depth=3 +----+ | + | +---+ | + + | +---+
v + | v + + |
,--------. | ,------------------. ,----. |
(" heading") | (" first...newlines,") (" and") |
`--+---+-' | `-----+----+-------' +-+----+-+ |
| ? |----' | em |-. <++ ,->| strong |-. <++ |
+---+ +----+ | + | +--------+ | + |
v + | v + |
,----------------. | ,------------. |
(" emphasized text") | (" strong text") |
`--------+---+---' | `-----+---+--' |
| ? |------' --> | ? |-----'
+---+ +---+
</pre></p>
<p><pre>
state[0]->first_item -->NULL (depth 0)
state[0]->last_item **>NULL
---
+------+ <-- state[1].first_item (depth 1)
| text |-->NULL <** state[1].last_item
+----*-+==>NULL
x *
v *
NULL *
v
,-----------.
("header text")
`-----------'
---
state[2]->first_item -->NULL (depth 2)
state[2]->last_item **>NULL
--- ,-- first_item
+------+<-'+-------+ +------+ <** last_item
| text |-->| blank |-->| text |-->NULL <== depth=3
+----*-+==>+-------+==>+----*-+==>NULL
x * x x *
v * v v *
NULL * NULL NULL *
v v
,-------. ,------------.
("heading") ("first...text")
`-------' `------------'
</pre></p>
<p>
In the second case, we need an additional step if the element we are leaving
was a <center> element (or some element with an align="center" attribute): In
this case all sub-elements have to be centered one by one. (Elements creating a
box are centered as a whole.)
</p>
<p>
Finally, we have to do some more line break/blank line handling.
</p>
</a> <!--parseElementsProcessing-->
<a name="textBlocks">
<h3>Text Blocks</h3>
<p>
One single text block can be created by several elements. Every series of text
parts not interrupted by elements requirering line breaks (or blank lines)
around them is stored to a single text item; it can even contain newline
characters created by <br> elements. This is done by calling add_string() every
time text data is encountered.
</p>
<p>
The color for the text added by some element is determined from the current
"state->text_mode" and "state->high". The normal color for the text mode is
looked up in "color_map[]", and than its bit 3 is negated if the text is
highlighted. This is more or less a hack; we'll have to replace this by some
serious attribute handling at some point...
</p>
<h4>add_string()</h4>
<p>
This function concatenates the new text to the "string" of "string_item", which
is a global variable pointing to the current open text item. If the new text
has other attributes than the last division of the "string" so far, a new
division is created for the new text; otherwise, the text is simply added to
the last division.
</p>
<p><pre>
text_item->string:
div[0].end div[1].end div[2].end
v v v
text: "[...]newlines, emphasized text and"
`~~~~~v~~~~~~'`~~~~~~~v~~~~~~'`~v'
div[1].color=MAGENTA div[2].color=WHITE
div[0].color=WHITE
</pre></p>
<p><pre>
+------------------------------------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| |
|+-------+ |
||heading| |
|+-------+ |
| |
|+----------------------------------+ |
||[...]newlines, emphasized text and| |
</pre></p>
<p><pre>
,------------------. ,----.
(" first...newlines,") (" and")
`-----+----+-------' +-+----+-+
| em |-. <++ ,->| strong |-. <++
+----+ | + | +--------+ | +
v + | v +
,----------------. | ,------------.
(" emphasized text") | (" strong text")
`--------+---+---' | `-----+---+--'
| ? |------' --> | ? |-->
+---+ +---+
</pre></p>
<p><pre>
text_item->string:
div[0].end div[1].end div[2].end div[3].end
v v v v
text: "[...]newlines, emphasized text and strong text"
`~~~~~v~~~~~~'`~~~~~~~v~~~~~~'`~v'`~~~~~v~~~~'
div[1].color=MAGENTA div[3].color=STRONG WHITE
div[0].color=WHITE div[2].color=WHITE
</pre></p>
<p><pre>
+------------------------------------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| |
|+-------+ |
||heading| |
|+-------+ |
| |
|+----------------------------------------------+|
||[...]newlines, emphasized text and strong text||
</pre></p>
<p>
A new division can also be enforced, by an additional call of add_text() with
NULL as text before adding the next text part. (This is necessary to prevent
multiple consecutive links from being merged into one div, as link highlighting
is done div-wise.)
</p>
<p>
Appending to an existing text item is only possible if the last created item
was a text item, and there was no breaking element. (Neither the last one, nor
the new one, nor anyone in between.) Otherwise, a new text item has to be
created, and a new "String" structure for it. This is the only place where new
text items are created.
</p>
<p><pre>
| |
|+---------------------------------+|
||starting with an evil center tag,||
|+---------------------------------+|
</pre></p>
<p><pre>
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--' <--
| center |-. <++ ,->| a |->
+--------+ | + | +---+
v + |
,----------------. |
(" starting...tag,") |
`--------+---+---' |
| ? |------'
+---+
</pre></p>
<p><pre>
| |
|+---------------------------------+|
||starting with an evil center tag,||
|+---------------------------------+|
|+---------------------+ |
||this very...anchor em| |
</pre></p>
<p>
If there is a space at the beginning of a string, it is discarded while
creating the string, for a text block always starts with a word.
</p>
</a> <!--textBlocks-->
<h3>Line Breaks</h3>
<p>
As soon as an element forcing a line break is either entered or left (there are
no elements creating a break only before or only behind it), "string_item" is
set to NULL, indicating that no more text can be added to the last text item,
and new text has to create a new one.
</p>
<p><pre>
+---+
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + +
| + ,----------.
v + (" this...em")
+--------+ <xx `---+---+--' <**
| center |-. <++ ,->| a |->
+--------+ | + | +---+
v + |
,----------------. |
(" starting...tag,") |
`--------+---+---' | <-- <==
| ? |------'
+---+
</pre></p>
<p><pre>
| |
|+---------------------------------+|
||starting with an evil center tag,||
</pre></p>
<p><pre>
+---+ <xx
->| p |-. <++++++++++++++++++
+---+ | <+ +
| + +
| + ,----------.
v + (" this...em")
+--------+ `---+---+--' <** <==
| center |-. <++ ,->| a |->
+--------+ | + | +---+
v + |
,----------------. |
(" starting...tag,") |
`--------+---+---' | <--
| ? |------'
+---+
</pre></p>
<p><pre>
| |
|+---------------------------------+|
||starting with an evil center tag,||
|+---------------------------------+|
</pre></p>
<a name="blankLines" id="blankLines">
<h3>Blank Lines</h3>
<p>
Probably the most tricky part is handling of blank lines. Similar to line
breaks, items needing blank lines have them before *and* after them. However,
when two such items meet, they have only *one* blank line between them.
Furthermore, a blank line is *never* inserted before the first or after the
last item inside a box. That's why blank lines cannot be simply inserted when
entering an element causing blank lines, or when leaving it. Instead, only
"requests" for blank lines are stored in "para_blank", and if certain
conditions are met, a blank line is inserted before the next item (inside
add_item()).
</p>
<p>
Actually, the blank line is inserted not in front of the new item, but after
the last item, at the same tree depth as that one. This is important, to ensure
that a blank line generated in front of a box is actually inserted *outside* of
the box, not inside it, as would be the case if it was inserted at the current
depth. The global "blank_depth" variable is responsible for this, and is set to
the current depth every time a blank line request is generated. The blank item
is then inserted directly to the "state" structure at "blank_depth" in
add_item(). This is surely very bad programming style ;-)
</p>
<p>
The blank line requests are managed by the global "para_blank" variable. A
value of 1 indicates that a blank line is needed, and will be generated by the
next add_item(). A value of 0 indicates that no blank line is needed in any
case; this situation can only occur when there are no items inside the current
box yet. A value of -1 indicates that a blank line *may* be necessary. That is
the case when we already have some items inside the current box, but the last
item does not need a blank line; a blank line needs to be inserted only when
the following item wants one.
</p>
<p>
When entering an element needing a blank line, a request is generated
("para_blank" set to 1 and "blank_depth" is stored) if "para_blank" was -1,
indicating that there are already items in the current box.
</p>
<p><pre>
+
+------+
->| body |-. <++++++++++++++
+------+ | <+ +
v + +
+----++ +---+
--> | h1 |-. <+ ,->| p |->
cur_ +----+ | + | +---+
tag v + |
,--------. |
(" heading") |
`--+---+-' |
| ? |----'
+---+
</pre></p>
<p><pre>
+------------------+
|+-----------+ |
||header text| |
|+-----------+ |
??? (para_blank=-1)
</pre></p>
<p><pre>
+
+------+
->| body |-. <++++++++++++++
+------+ | <+ +
v + +
+----++ +---+
--> | h1 |-. <+ ,->| p |->
==> +----+ | + | +---+
blank_ v + |
depth ,--------. |
(" heading") |
`--+---+-' |
**> | ? |----'
list_next +---+
(new "cur_el")
</pre></p>
<p><pre>
+------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| (para_blank=1) |
...
|+-------+ | (will be created later)
||heading| |
|+-------+ |
</pre></p>
<p>
If "para_blank" is already 1, the request is left unchanged; a blank line will
be inserted already.
</p>
<p><pre>
| |
|+-------+ |
||heading| |
|+-------+ |
| (para_blank=1) |
</pre></p>
<p><pre>
|
v
+----+ <== +---+ <--
| h1 |-. <+ ,->| p |---. <++
+----+ | + | +---+ | +
v + | v +
,--------. | ,------------------.
(" heading") | (" first...newlines,")
`--+---+-' | `-----+----+-------'
| ? |----' **> | em |-.
+---+ +----+ |
</pre></p>
<p><pre>
| |
|+-------+ |
||heading| |
|+-------+ |
| (para_blank=1) |
...
|+------------+ |
||first...text| |
|+------------+ |
</pre></p>
<p>
If "para_blank" is 0 nothing is done, even if the element would normally need a
blank line: No blank line is ever inserted in front of the first item inside a
box.
</p>
<p><pre>
+-----------+
(para_blank=0)
</pre></p>
<p><pre>
+---+
| ! |-. <+
+---+ | +
v +
+---++
--> | p |-. <+
+---+ | +
v +
,---------.
("some text")
`--+---+--'
**> | ? |->
+---+
</pre></p>
<p><pre>
+-----------+
...
|+---------+|
||some text||
|+---------+|
</pre></p>
<p>
When some item (text or box) is inserted by add_item(), "para_blank" is always
set to -1, as now there is at least one item in the current box.
</p>
<p><pre>
+------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| (para_blank=1) |
</pre></p>
<p><pre>
+
+------+
->| body |-. <++++++++++++++
+------+ | <+ +
v + +
+----++ +---+
| h1 |-. <+ ,->| p |->
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
--> | ? |----'
+---+
</pre></p>
<p><pre>
+------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| |
|+-------+ |
||heading| |
|+-------+ |
??? (para_blank=-1)
</pre></p>
<p>
When leaving an element needing a blank line, a request is generated also, and
will be handled in the next add_item(). Otherwise, the current state is kept.
</p>
<p><pre>
+
+------+
->| body |-. <++++++++++++++
+------+ | <+ +
v + +
+----++ +---+
xx> | h1 |-. <+ ,->| p |-> <**
new_el +----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
--> | ? |----'
+---+
</pre></p>
<p><pre>
+------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| |
|+-------+ |
||heading| |
|+-------+ |
??? (para_blank=-1)
</pre></p>
<p><pre>
+
+------+ <xx
->| body |-. <++++++++++++++
+------+ | <+ +
v + +
+----++ +---+
==> | h1 |-. <+ ,->| p |-> <**
+----+ | + | +---+
v + |
,--------. |
(" heading") |
`--+---+-' |
--> | ? |----'
+---+
</pre></p>
<p><pre>
+------------------+
|+-----------+ |
||header text| |
|+-----------+ |
| |
|+-------+ |
||heading| |
|+-------+ |
| (para_blank=1) |
</pre></p>
<p>
When entering an element generating a box, "para_blank" is reset to 0.
</p>
<p><pre>
| |
|+---------+ |
||some text| |
|+---------+ |
??? (para_blank=-1)
</pre></p>
<p><pre>
+--------+ +------+
->| center |-. ,->| form |-. <--
+--------+ | | +------+ |
v | |
,---------. | |
("some text") | v
`--+---+--' | +---+
| ? |-----' **> | p |->
+---+ +---+
</pre></p>
<p><pre>
| |
|+---------+ |
||some text| |
|+---------+ |
|+--------------+|
(para_blank=0)
</pre></p>
<p>
However, it's left unchanged if there is already a request, indicating that a
blank line will be inserted *in front* of the box by the next add_item().
</p>
<p><pre>
| |
|+---------+ |
||some text| |
|+---------+ |
| (para_blank=1) |
</pre></p>
<p><pre>
+---+ +------+
->| p |-. <== ,->| form |-. <--
+---+ | | +------+ |
v | |
,---------. | |
("some text") | v
`--+---+--' | +---+
| ? |-----' **> | p |->
+---+ +---+
</pre></p>
<p><pre>
| |
|+---------+ |
||some text| |
|+---------+ |
| (para_blank=1) |
|+--------------+|
</pre></p>
<p>
When leaving a box creating element, any request generated inside the box is
discarded. We never insert a blank line after the last item of a box.
</p>
<p><pre>
++>NULL
+ .--------------------------------.
+ v |
+---+ <xx |
| ! |-. <++ <** |
+---+ | + |
v + |
,-----------. |
("header text") |
`-+------+--' |
| html |-. <+++++++++++ |
+------+ | + |
| [...] [...]
[...] + |
| ,---------------. |
| (" (a single tag)") |
| `-----+---+-----' | <--
`--------->| ? |--------'
+---+
</pre></p>
<p><pre>
|+--------------+ |
||(a single tag)| |
|+--------------+ |
| (para_blank=1) |
</pre></p>
<p><pre>
++>NULL <xx
+ .--------------------------------.
+ v |
+---+ |
| ! |-. <++ <** |
+---+ | + |
v + |
,-----------. |
("header text") |
`-+------+--' |
| html |-. <+++++++++++ |
+------+ | + |
| [...] [...]
[...] + |
| ,---------------. |
| (" (a single tag)") |
| `-----+---+-----' | <--
`--------->| ? |--------'
+---+
</pre></p>
<p><pre>
|+--------------+ |
||(a single tag)| |
|+--------------+ |
+------------------+
??? (para_blank=-1)
</pre></p>
<p>
However, if the last request was generated *before* descending into the element
creating the box (only possible if the box is empty), it is kept, and the blank
is inserted when the box is added.
</p>
<p><pre>
| |
|+---------+ |
||some text| |
|+---------+ |
| (para_blank=1) |
|+--------------+|
</pre></p>
<p><pre>
+---+ +------+ <xx +---+
->| p |-. <== ,->| form |-. ,->| p |-> <**
+---+ | | +------+ | | +---+
v | | |
,---------. | | |
("some text") | v |
`--+---+--' | +------+ |
| ? |-----' --> | span |-'
+---+ +------+
</pre></p>
<p><pre>
| |
|+---------+ |
||some text| |
|+---------+ |
| |
|+--------------+|
|+--------------+|
</pre></p>
<h4><br> elements</h4>
<p>
<br> elements used to break text blocks as described above. This however turned
out to be a bug: some elements (e.g. <a>) can span over a <br>, and these were
handled incorrectly with that approach.
</p>
<p>
Now simply a '\n' is added to the text block when <br> is encountered, and then
correctly handled while <a href="#lineBreaking">Breaking String into
Lines</a>.
</p>
</a> <!-- blankLines -->
<a name="links" id="links">
<h3>Links</h3>
<p>
When a link element is encountered (any <a> element having a "href"),
"cur_state->link_type" is set (to FORM_NO), indicatating that the element
creates a link. The URL (from the "href" attribute) is saved in
"cur_state->link_value".
</p>
<p>
The link is then stored when leaving the link element. (After processing all
sub-elements.) The link start is set to "cur_state->link_start", which was the
string end position while entering the link element; the link end is set to the
current string end position. This way the link spans all text generated inside
the element.
</p>
<p>
There is some additional handlig necessary to workaround broken links, however.
(This used to be important to make forms work with SGML at all; now that full
SGML support is implemented, it probably only helps a few broken pages...)
</p>
<p>
When a link doesn't end in the same string as it started (checked by
"cur_state->link_item", which was the current string item when entering the
link element), it is stored in the starting string, not the current one.
However, if there was no active text item when the link started (indicating the
link starts at the beginning of a text block), we can't determine the starting
string. In this case, we store it in the current string -- normally, this is
the right thing to do; if the link spans multiple strings, on the other hand,
at least the last part ist stored this way.
</p>
<p>
Some magic is necessary to handle nested links: The link which is stored later
is the outer one, i.e. it starts *before* the previously stored inner link... To
handle this in a useful fashion, we also have to *store* it before the inner
link. Thus, instead of simply appending it at the end of the string's link list,
we have to shift all inner links one position behind, and put the new (outer)
link at the free position created in front of them.
</p>
<p>
Additionally, two hashes of the link URL and the text inside the link are
stored for each link; this is necessary to recognize the right link to
reactivate when a page is revisited but its content changed. See
<a href="hacking-page.html#reactivating">_Reactivating Link</a> in
hacking-page.* for details.
</p>
</a> <!-- links -->
<h3>Forms</h3>
<p>
Form elements are handled very similar, and by the same code. The only
difference is that "link_type" is set to some form type instead of FORM_NO, and
the initial "value" of the element is stored in the structure tree so it will
be submitted to the server, and (for some form control types) displayed on the
page via <a href="hacking-links.html#setForm">set_form()</a> (see
hacking-links.*).
</p>
<p>
Also, "form_enabled" is set for all elements that will be submitted to the
server. It is always set for elements that are submitted unconditionally
(text/password/hidden input fields), and set to the initial state for elements
that are submitted depending on their state (radio buttons, checkboxes,
<select> options). Submit buttons initially aren't enabled.
</p>
<p>
Some special handling is necessary for <select> options: They do not have an
own "name" attribute to store in "Link->name" like other form elements; the
"name" for all options is given in the <select> element instead. Thus it needs
to be passed to all the options by the state stack, in "select_name". The same
for the link type (FORM_OPTION or FORM_MULTIOPTION) of the option links: It
depends on the presence of the "multiple" attribute in the <select> element,
and is passed in "select_type" for that reason.
</p>
<a name="virtual" id="virtual">
<h3>Virtual Items</h3>
<p>
There are two kinds of "virtual" Items: ITEM_BLOCK_ANCHOR and
ITEM_INLINE_ANCHOR. As the names suggest, both types are presently used for
anchors. The difference is that block anchors are created by block elements
with an "id" attribute, and span one or more block elements, while inline
anchors are created inside text blocks by the classical "a" element or by any
inline element with an "id", and span only a text part.
</p>
<p>
"virtual" means that these items do not affect layouting like other items; they
only create some additional structure.
</p>
<h4>Block Anchors</h4>
<p>
As block anchors span multiple other elements, they have to act as boxes,
containing a series of virtual children. In contrast to real box items, they do
not get their size assigned by the parent, and do not assign size to their
virtual children. The children get their size assigned by the real parent
directly, and the size of the virtual box is determined afterwards, from the
outer bounds of all virtual children.
</p>
<p>
In the present implementation, virtual box items aren't inserted normally as
parents of their virtual children into the item tree. They are simply inserted
as normal items after their virtual children, at the same tree depth. The
virtual box is created only by special pointers, handled only in the necessary
places; the normal tree traversal functions do not know about them. This is a
hack; the idea was to integrate them into the existing system changing as
little as possible. Most probably this will be replaced by a clean
implementation using real parent/children relations in the future.
</p>
<p><pre>
virtual tree:
+-----+
,->| box |
| +-----+
| x ^
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
v + + + |
+------+ +--------------+ +------+ |
| text |-. ,->| block anchor |-->| text |-'
+------+=|======================|=>+--------------+==>+------+==>NULL
| | x ^
| xxxxxxxxxxxxxxxxxxxxxxxx +
| x +++++++++++++++++++++++++++
| v + + |
| +------+ +------+ |
`->| text |-->| text |-'
+------+ +------+==>NULL
</pre></p>
<p><pre>
real tree:
> virtual child +-----+
,->| box |
virtual box | +-----+
,----------^----------. | x ^
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
v + + + + + |
+------+ +------+ +------+ +--------------+ +------+ |
| text |-->| text |-->| text |-->| block anchor |-->| text |-'
+------+==>+------+==>+------+==>+--------------+==>+------+==>NULL
^
</pre></p>
<h4>Inline Anchors</h4>
<p>
As inline anchors are contained within text elements, they have to act as
children of text items. Text item however can't have children in the present
implementation; thus, inline anchors presently are also only virtual children.
</p>
<p>
Similar to the virtual boxes created by block anchors, these virtual children
are stored after their virtual parent, at the same tree depth. This is a hack
just as the virtual boxes, and will be replaced also.
</p>
<p>
Additionally to the virtual parent (and the name of the anchor), inline anchors
need to store the position of their start and end inside the parent string.
</p>
<p><pre>
virtual tree:
%%%> anchor_start,anchor_end +-----+
.->| box |
| +-----+
| x ^
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
v + + + |
+------+ +------+ +------+ |
| text |-. .->| text |-->| text |-'
+------+=|========================================|=>+------+==>+------+==>NULL
| | x ^ *
| | x + * ,--------------------.
| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + **>("anchor1 and anchor2.")
| x ++++++++++++++++++++++++++++++++++++++ `--------------------'
| x + + | ^ ^ ^ ^
| x + + | % % % %
| x + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %
| x + % + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
| v + % + % |
| +---------------+ +---------------+ |
`->| inline anchor |-->| inline anchor |-'
+---------------+==>+---------------+==>NULL
</pre></p>
<p><pre>
real tree:
> virtual parent +-----+
.->| box |
| +-----+
| x ^
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
v + + + + + |
+------+ +------+ +---------------+ +---------------+ +------+ |
| text |-->| text |-->| inline anchor |-->| inline anchor |-->| text |-'
+------+==>+------+==>+---------------+==>+---------------+==>+------+==>NULL
* ^ % %
%%%%%%%%%%%%%*%%%%%%%%%%%%%%%% %
% * %
% * %
% v %
% ,--------------------. %
% ("anchor1 and anchor2.") %
% `--------------------' %
% ^ ^ ^ ^ %
% % % % % %
%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
</pre></p>
<p>
Links are very similar to inline anchors, and probably will be stored the same
way as anchors in the future, once ITEM_INLINE is cleanly implemented. The
present different handling of links and anchors is by purpose -- to see which
method turns out to be better. (Presently, it looks much like the anchor-method
is better.)
</p>
</a> <!-- virutal -->
<h3>Finishing</h3>
<p>
After all elements of the syntax tree are traversed, the structure tree is
finalized by setting "next" of the tree top (the global item) to point back to
itself, and "parent" point to the first item.
</p>
<p>
parse_struct() returns a "struct Item" pointer to the tree top. This is passed
as argument to the following passes by main().
</p>
<p>
Is is a bit strange that "parent" of the tree top points to the first item.
However, it is important to have some simple way of accessing the first item,
as some of the following passes start with the fist item; but only the pointer
to the tree top is passed on the following passes. Maybe we should use some
function that finds the first item by following "first_child" instead -- as
this is done only once in every processing pass, it wouldn't be too big an
unefficiency.
</p>
</a> <!--parseStruct-->
<a name="freeItems" id="freeItems">
<h3>free_items()</h3>
<p>
This function is responsible for freeing the memory used by the item tree.
</p>
<p>
Just as <a href="#freeSyntax">free_syntax()</a>, the tree is
traversed by "list_next", and every node is freed, including all data belonging
to it.
</p>
<p>
For text items, the associated strings are freed also; for anchor items, the
anchor data is freed.
</p>
</a> <!--freeItems-->
<a name="preRender" id="preRender">
<h2>7. pre-render.c</h2>
<p>
So far, the item tree only represents the structure of the page, i.e. the
dependencies of the items. The Items have no actual sizes nor positions yet.
</p>
<p><pre>
# ################################################################################################
# +-----------
# |header text
# ***
# *
# +-------
# |heading
# ***
# *
# +-----------------------------------------------------------------------------------------------
# |first paragraph of text; includes multiple spaces and newlines, emphasized text and strong text
# ***
# *
# +---------------------------------
# |starting with an evil center tag,
# +-----------------------------------------------------------------------------------------------
# |this very long second paragraph contains some special characters (including a simple space...):
# | &; <>"=/ plus a big gap###and two unicode escapes (decimal: and hexal: ) but also an anchor
# | embedded inside a word (this anchor also is the only tag with parameters); and finally a blank
# | row
# +--------------
# |(a single tag)
</pre></p>
<p>
pre_render() assigns coordinates inside the layouted page to all items. It also
breaks the text blocks into lines.
</p>
<p>
This process is reversible (it doesn't alter any data stored before; it only
creates additional data), and thus resizing the output area of the viewer will
be possible without regenerating the item tree. (And without reloading the
file.)
</p>
<p>
The coordinates are stored to "x_start", "x_end", "y_start" and "y_end" of the
item structures. For every text item, the positions of all line breaks are
stored in "line_table[]". This is the only way line breaks are indicated; the
text string itself isn't altered. Thus the rendering function has to look at
this table while generating output. (<a href="#renderC">8. render.c</a>)
</p>
<p>
Besides of assigning the coordinates, pre_render() also generates the
"page_map[]". This is a structure telling which items occupy every line of the
output page. For every line, a table is generated, containing references to all
elements that show up in this line. It's described more thoroughly in
<a href="#createMap">create_map()</a>.
</p>
<p>
The processing is split into five smaller passes, executed successively by
pre_render(). It also could be done in one single, pseudo-recursive pass. Most
probably this would be more efficient; it would be harder to understand, too.
Maybe we will change this at some point...
</p>
<a name="calcWidth" id="calcWidth">
<h3>calc_width()</h3>
<p>
In the first pass, the minimal x-width of all items is calculated. This is done
by traversing the item tree by "list_next" (bottom to top). This ensures that
the sizes of all children are calculated before the parent. In every iteration
one item is processed. The actions taken depends on the type of the item.
</p>
<p>
For text items, the minimal width (stored in "x_end") is set to the width of
the longest word in the text block, as in other browsers. Note that we *can*
generate narrower text blocks; netrik can break words that do not fit on a
line, and probably also scrolling of items will be possible in the future.
(AFAIK no other browser does any of that, although it was recommended by the
W3C for years...)
</p>
<p><pre>
# ###############[...]
# +------+
# |header|
^ ^x_end=6
0
# ***
# *
# +-------
# |heading
[...]
</pre></p>
<p>
For blank lines, the minimal width is set to 0 -- blank lines do not need any
width; anchor items as well.
</p>
<p><pre>
# ###############[...]
# +------+
# |header|
# **
# **
^x_end=0
# +-------
# |heading
[...]
</pre></p>
<p>
Finding the longest word is quite easy. The whole text block is processed char
by char in a loop. For each char, the current word length "len" is incremented.
When the word ends (space or string end encountered), "len" is reset to 0.
"longest" keeps the current maximum, and is stored to "x_end" after the whole
text block was processed.
</p>
<p>
For box items, the minimal width is the one of the widest sub-item.
</p>
<p><pre>
# ##############
# +------+ #
# |header| #
# ** #
# ** #
# +-------+ #
# |heading| #
# ** #
# ** #
# +----------+ #
# |emphasized| #
# ** #
# ** #
# +--------+ #
# |starting| #
# +------------#
# |parameters);#
# +------+ #
# |single| #
^ ^x_end
0
</pre></p>
<p>
As all sub-items are processed before the parent, we already know the widths of
the sub-items when processing a box item. We simply go trough all immediate
children (start with "first_child" and go on by "next") and look for the
maximum.
</p>
</a> <!--calcWidth-->
<a name="assignWidth" id="assignWidth">
<h3>assign_width()</h3>
<p>
The second pass assigns the x-coordinates (x_start and x_end) to all items.
(Presently, this is trivial: all items have the same coordinates as their
parent, which is the global item...) For text items, the positions of line
breaks are also calculated.
</p>
<h4>Traversing Item Tree Top to Bottom</h4>
<p>
In this pass, the tree is traversed top to bottom, as the coordinates of the
sub-items depend on the coordinates of the parent. This is a bit more
complicated than traversing bottom to top, as there is no equivalent of the
"list_next" pointer for this.
</p>
<p>
If the current item has children, we proceed with the first child. (Descend.)
</p>
<p><pre>
<+++
+
+-----+
,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
cur_item xx> +-----+ +-----+ |
,->| box |-. ,->| box |-'
new cur_item | +-----+=|===========|=>+-----+==>NULL
| | x ^ | | x ^
xxxxxxxx|xxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++|+++++++++++++++++ | x ++++++++++++
v + | + | | v + |
+------+<-'+------+ | | +------+ |
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x x x
v v v
NULL NULL NULL
</pre></p>
<p>
If the current item has no children, we go to the "next" item.
</p>
<p><pre>
<+++
+
+-----+
,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
+-----+ +-----+ |
,->| box |-. ,->| box |-'
| +-----+=|===========|=>+-----+==>NULL
| x ^ | | x ^
xxxxxxxxxxxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++++++++++++++++++++ | x ++++++++++++
v + + | | v + |
+------+ +------+ | <-- | +------+ |
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x ^xx x x
v v v
NULL NULL NULL
</pre></p>
<p>
If there is no "next" item (we are already at the last item of this depth), we
have to ascend before we can go to the next item.
</p>
<p><pre>
<+++
+
+-----+
,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
+-----+ +-----+ |
--> ,->| box |-. ,->| box |-'
| +-----+=|===========|=>+-----+==>NULL
| x ^ | | x ^
xxxxxxxxxxxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++++++++++++++++++++ | x ++++++++++++
v + + | | v + |
+------+ +------+ | <xx | +------+ |
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x x x
v v v
NULL NULL NULL
</pre></p>
<p><pre>
<+++
+
+-----+
,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
+-----+ --> +-----+ |
,->| box |-. ,->| box |-'
| +-----+=|===========|=>+-----+==>NULL
| x ^ | | x ^
xxxxxxxxxxxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++++++++++++++++++++ | x ++++++++++++
v + + | | v + |
+------+ +------+ | <xx | +------+ |
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x x x
v v v
NULL NULL NULL
</pre></p>
<p>
If we still do not have a "next" item after ascending, we ascend again.
</p>
<p><pre>
<+++
+
+-----+
,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
+-----+ --> +-----+ |
,->| box |-. ,->| box |-'
| +-----+=|===========|=>+-----+==>NULL
| x ^ | | x ^
xxxxxxxxxxxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++++++++++++++++++++ | x ++++++++++++
v + + | | v + |
+------+ +------+ | | +------+ | <xx
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x x x
v v v
NULL NULL NULL
</pre></p>
<p><pre>
<+++
+
+-----+
--> ,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
+-----+ +-----+ |
,->| box |-. ,->| box |-'
| +-----+=|===========|=>+-----+==>NULL
| x ^ | | x ^
xxxxxxxxxxxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++++++++++++++++++++ | x ++++++++++++
v + + | | v + |
+------+ +------+ | | +------+ | <xx
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x x x
v v v
NULL NULL NULL
</pre></p>
<p>
After all items have been processed, we'll ascend until we get back to the top
item, which's "next" pointer points back to itself; Thus we stay at the tree
top after following "next", and the main loop is terminated.
</p>
<p><pre>
<+++
+
+-----+
--> ,->| box |-->NULL
| +-----+<==#
| x ^ #===#
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
x +++++++++++++++++++++++++++++++++++
v + + |
+-----+ +-----+ |
,->| box |-. ,->| box |-'
| +-----+=|===========|=>+-----+==>NULL
| x ^ | | x ^
xxxxxxxxxxxxxxxxxxxxxxxx + | xxxxxxxxxxxxx +
x +++++++++++++++++++++++ | x ++++++++++++
v + + | | v + |
+------+ +------+ | | +------+ | <xx
| text |-->| text |-' `->| text |-'
+------+==>+------+==>NULL +------+==>NULL
x x x
v v v
NULL NULL NULL
</pre></p>
<h4>Assigning x-Coordinates</h4>
<p>
When processing an item, not the coordinates of "cur_item" are assigned, but
the coordinates of its *sub-items*. "cur_item" has had its coodinates assigned
while its parent was processed. This is an exact reversal of <a
href="#calcWidth">calc_width()</a>: Instead of "cur_item" getting
its size from its sub-items, the sub-items get their size from "cur_item".
</p>
<p>
The coordinates for the global item are assigned before calling assign_width().
x-start is set to 0, x-end is set to the output width passed from main(). (The
screen width when cfg.term_width is set; otherwise, either a constant value (in
--dump mode) or the maximum of screen width and page width.) Play around with
them if you like. (If set to a narrow box, you can see the word breaking work.)
</p>
<p><pre>
##################################################################################
#------+ #
#header| #
#* #
#* #
[...]
#------------+ #
#parameters);| #
#------+ #
#single| #
^x_start=0 ^x_end=80
</pre></p>
<p>
For box (and form) items, the coordinates of all immediate children are simply
set to the ones of the box. (This will have to change at some point, but it's
ok for now...) It's done in a simple loop processing the children by
"first_child" and "next", just as in
<a href="#calcWidth">calc_width()</a>.
</p>
<p><pre>
##################################################################################
+--------------------------------------------------------------------------------+
|header text |
**********************************************************************************
* *
[...]
+--------------------------------------------------------------------------------+
|this very long second paragraph contains some special characters (including a |
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: |
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the|
|only tag with parameters); and finally a blank row |
+--------------------------------------------------------------------------------+
|(a single tag) |
</pre></p>
<p>
Blank items have no children. Nothing has to be done. The same for anchor items.
</p>
<p>
Text items have no children, too; but they need to be broken into lines. (This
could be done in a seperate pass...)
</p>
<a name="lineBreaking" id="lineBreaking">
<h4>Breaking String into Lines</h4>
<p>
The breaking into lines is a bit complicated, because netrik has the ability to
break words that do not fit on a single line. And: It does this smartly,
avoiding line breaks whenever possible.
</p>
<p>
The line breaking does not alter the string structure itself. The only output
is "line_table[]" (containing the positions of all line breaks inside the
string), and its length (The number of line breaks), which is stored as the
height of the text item in "y_end".
</p>
<p>
Before starting, some constants are calculated: The width of the text block is
the difference of x_end and x_start. The pointer to the start of the text
string is taken from the string structure and stored in "string_start".
</p>
<p>
The text block is processed word-wise. In each iteration of the outer loop one
word is processed. (There are some exceptions though for extremely long words;
this will be explained in a moment.)
</p>
<p>
First, "word_end" is retrieved. It points to the space, newline or string
terminator that ends the word to be processed. "next_word_start" is set to
"word_end+1", assuming that we will proceed with the next word (which starts
after the space); however, this may by altered later.
</p>
<p><pre>
line_table[0]
v
string: "I am a very simple and stupid example sentence."
^ ^ ^ ^^
s l w en
string_start line_start next_word_start
word_end
word_start
output:
x_start x_end
|<- width ->|
v 12 v
+------------+
|I am a very |
|simple and stupid example sentence.
^ ^ ^^
l w en
</pre></p>
<p>
Then we test if the whole word fits into the current open line (which's
starting position is stored in "line_start").
</p>
<p><pre>
v
string: "I am a very simple and stupid example sentence."
^ ^ ^ ^^^
s l w enb
line_start+width (line end/Break position)
|<- width ->|
(12)
+------------+
|I am a very |
|simple and stupid example sentence.
^ ^ ^^^
l w enb
e<b -> no wrap
</pre></p>
<p>
If it does, we do nothing, and simply proceed with the next word. "word_start"
is set to "next_word_start" before the next iteration.
</p>
<p><pre>
v
string: "I am a very simple and stupid example sentence."
^ ^ ^
s l n
^
w
</pre></p>
<p>
Otherwise, we have to generate a line wrap, and put the word on a new line.
</p>
<p><pre>
v
string: "I am a very simple and stupid example sentence."
^ ^ ^^ ^^
s l wb en
+------------+
|I am a very |
|simple and stupid example sentence.
^ ^^ ^^
l wb en
e>=b -> wrap
</pre></p>
<p>
Normally, we simply wrap the line at the current word, by setting "line_start"
to "word_start", and adding a line break at this position to "line_table[]".
</p>
<p><pre>
line_table[1]
v v
string: "I am a very simple and stupid example sentence."
^ ^ ^^
s w en
^
l
+------------+
|I am a very |
|simple and |
|stupid example sentence.
^ ^^
w en
^
l
</pre></p>
<p>
However, things are more complicated due to the ability of breaking too long
words. We have to decide whether we simply put the word on a new line as
described above, or if it's necessary/better to break it at the line end. And
this is the tricky part.
</p>
<p>
We can't just break all words, as it would cause many really unnecessary
breaks, which is really ugly and hard to read.
</p>
<p><pre>
+------------+
|Having aRea\|
|llyLongWord |
|followed by |
|anotherLong\|
|Word, I am |
|quite inter\| <--
|esting. |
+------------+
</pre></p>
<p>
Wrapping before every word that doesn't fit on the line end is also not
optimal:
</p>
<p><pre>
+------------+
|Having | <--
|aReallyLong\|
|Word | <--
|followed by |
|anotherLong\|
|Word, I am |
|quite |
|interesting.|
+------------+
</pre></p>
<p>
There is lots of space wasted, and it looks quite ugly.
</p>
<p>
A better solution is putting the beginning of a word on the current line always
as long as this doesn't introduce an additional (unnecessary) word break:
</p>
<p><pre>
+------------+
|Having aRea\| <--
|llyLongWord |
|followed by |
|anotherLong\|
|Word, I am |
|quite | <--
interesting. |
+------------+
</pre></p>
<p>
(The first arrow shows a case where the beginnig of the word was put on the
line end to better fill the space, the second one a case where this is not done
to avoid an unnecessary word break.)
</p>
<p>
The Problem here is to decide whether we can put the beginning of the word on
the line end without introducing an additional line break. For this, we
truncate the word, chopping off as many whole line widths ("width-1", because
of the break chars) from the end of the word as possible, and test if the
remaining part fits on the current line.
</p>
<p><pre>
v v v v
string: "I am a second example sentence with aVeryLongWord in me."
^ ^ ^ ^ ^^
l w t b en
trunc_word_end=word_end-(width-1)
|<- ->|
width-1 (11)
+------------+
|I am a |
|second |
|example |
|sentence |
|with a\ |
^ ^ ^ ^
l w t b
t<=b -> fill line
|VeryLongWord| (truncated part)
^
e
</pre></p>
<p>
Or:
</p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^ ^ ^ ^^
l w b t en
+------------+
|I am a third|
|example |
|sentence, |
|with anEvenAB\
^ ^ ^ ^
l w b t
t>b -> put word on new line
|itLongerWord|
^
e
</pre></p>
<p>
For a longer word, the truncation is done more than once:
</p>
<p><pre>
v v v
string: "I am another example sentence, with aTerriblyLongWordThatDoesn'tSeemToEnd in me."
^ ^ ^ ^ ^ ^ ^^
l w T b t t en
(final)
+------------+
|I am another|
|example |
|sentence, |
|with aTe\ |
^ ^ ^ ^
l w T b t<=b -> fill line
|rriblyLongW\|
^
t
|ordThatDoes\|
^
t
|n'tSeemToEnd in me.
^^
en
</pre></p>
<p>
or:
</p>
<p><pre>
v
string: "This example has again aTerriblyLongWordThatDoesn'tSeemToEnd in some other sentence."
^ ^ ^ ^ ^ ^ ^^
l w b T t t en
+------------+
|This example|
|has again aTe\
^ ^ ^ ^
l w b T
t>b -> don't fill
</pre></p>
<p>
This is done in a loop. We also could do it by a modulo division, but
evaluating a quite complicated expression is probably less efficient than a
simple loop, which is entered only in exceptional cases anyways. (Words wider
than a line are not very common, after all...) Also, it is easier to
understand, isn't it?...
</p>
<p>
Note that this only takes effect for words that actually need to be broken --
words shorter than the line width will never be truncated; the remaining part
will still not fit on the line, so the word is just put on the new line as
described above.
</p>
<p>
If we decide on the first way, we fill up the current line with the beginning
of the word. (The last char is kept empty for the word break indicator
displayed in the output.) The "line_start" is set accordingly, so that the rest
of the word will be put on the next line.
</p>
<p><pre>
v v v v v
string: "I am a second example sentence with aVeryLongWord in me."
^ ^^ ^^
w lb en
+------------+
|I am a |
|second |
|example |
|sentence |
|with aVeryL\|
|ongWord in me.
^ ^^
l en
</pre></p>
<p>
If the second way was chosen, things are quite easy: The line wrap is inserted
at the beginning of the word. Again, this is the same case as for short words
that aren't broken at all -- there is no extra handling for this.
</p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^^
w en
^
l
+------------+
|I am a third|
|example |
|sentence, |
|with |
|anEvenABitLongerWord in me."
^ ^^
l en
</pre></p>
<p>
Now we need to test if the remainder fits on the new line; if it does not, we
have to adjust "next_word_start" to make sure processing in the next loop
iteration will not continue with the next word, but with the part of the
current word that does not fit on the new line.
</p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^ ^^
l b en
+------------+
|I am a third|
|example |
|sentence, |
|with |
|anEvenABitLongerWord in me."
^ ^ ^^
l b en
e>=b -> more breaks necessary
</pre></p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^^
l nb n
(old value)
+------------+
|I am a third|
|example |
|sentence, |
|with |
|anEvenABitLongerWord in me."
^ ^^
l nb n
</pre></p>
<p>
This way we will scan for the word end again in the next iteration; but
starting with the part of the word that does not fit on the new line. (This is
unefficient of course, but this case is quite rare, and it's not worth adding
special code for handling this.)
</p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^ ^^
l w en
+------------+
|I am a third|
|example |
|sentence, |
|with |
|anEvenABitLongerWord in me."
^ ^^ ^^
l wb en
</pre></p>
<p>
As the new line is already filled up with the previous word part, a line break
will always be inserted just in front of the remainder, and the remainder will
be put into another line.
</p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^^ ^^
l wb en
+------------+
|I am a third|
|example |
|sentence, |
|with |
|anEvenABitLongerWord in me."
^ ^^ ^^
l wb en
e>b -> wrap
</pre></p>
<p><pre>
v v v v v
string: "I am a third example sentence, with anEvenABitLongerWord in me."
^ ^^
wb en
^
l
+------------+
|I am a third|
|example |
|sentence, |
|with |
|anEvenABitL\|
|ongerWord in me."
^ ^^
l en
</pre></p>
<p>
Of course it's also possible that the remainder still doesn't fit on a line,
and has to be broken again.
</p>
<p><pre>
v v v v
string: "I am another example sentence, with aTerriblyLongWordThatDoesn'tSeemToEnd in me."
^ ^^ ^^
l wb en
+------------+
|I am another|
|example |
|sentence, |
|with aTerri\|
|blyLongWordThatDoesn'tSeemToEnd in me.
^ ^^ ^^
l wb en
e>b -> wrap
</pre></p>
<p><pre>
v v v v
string: "I am another example sentence, with aTerriblyLongWordThatDoesn'tSeemToEnd in me."
^ ^^ ^ ^^
l wb t en
+------------+
|I am another|
|example |
|sentence, |
|with aTerri\|
|blyLongWordThatDoes\|
^ ^^ ^
l wb t
t>b -> don't fill
</pre></p>
<p><pre>
v v v v v
string: "I am another example sentence, with aTerriblyLongWordThatDoesn'tSeemToEnd in me."
^ ^^ ^
w nb e
^
l
+------------+
|I am another|
|example |
|sentence, |
|with aTerri\|
|blyLongWord\|
|ThatDoesn'tSeemToEnd in me.
^ ^^ ^
l nb e
</pre></p>
<p>
This will be repeated, until the whole word is stored.
</p>
<p><pre>
v v v v v
string: "I am another example sentence, with aTerriblyLongWordThatDoesn'tSeemToEnd in me."
^ ^^ ^^
l wb en
e>b -> wrap
</pre></p>
<p><pre>
v v v v v v
string: "I am another example sentence, with aTerriblyLongWordThatDoesn'tSeemToEnd in me."
^ ^^
w en
^
l
+------------+
|I am another|
|example |
|sentence, |
|with aTerri\|
|blyLongWord\|
|ThatDoesn't\|
|SeemToEnd in me.
^ ^^
l en
</pre></p>
<p>
Of course, a line break is always inserted in front of the word no matter if it
would fit on the old line, if the word follows a newline character.
</p>
<p>
Implementing this without considerable bloat and slowdown requires a little
trick: We test for the newline after normally scanning for the word end. Now if
there is a newline, we just set "word_end" to "word_start+width" -- we just
pretend that the current word is exactly as long as the line, so it will always
be put on a new line.
</p>
<p><pre>
v v v v v
string: "I am a stupid example sentence with a newline/in me."
^ ^ ^ ^^^
s l w enb
+------------+
|I am a |
[...]
|newline/in me.
^ ^ ^^^
l w enb
^newline in front of word
</pre></p>
<p><pre>
v v v v v
string: "I am a stupid example sentence with a newline/in me."
^ ^ ^ ^^ ^
s l w nb e
+------------+
|I am a |
[...]
|newline/in me.
^ ^ ^^ ^
l w nb e
e>b -> wrap
</pre></p>
<p><pre>
+------------+
|I am a |
[...]
|newline/ |
|in me.
^ ^ ^
w n e
^
l
</pre></p>
<p>
Note that "next_word_start" is *not* modified; this is important to ensure that
processing in the next iteration will continue with the following word
normally!
</p>
</a> <!--lineBreaking-->
</a> <!--assignWidth-->
<h3>calc_ywidth()</h3>
<p>
The third pass is simple again. calc_ywidth() calculates minimal heights
(y-widths) for all items.
</p>
<p>
Presently this is complete overkill; assign_ywidth() doesn't really need this.
However, it will be necessary as soon as interesting elements are
implemented (tables) -- which hopefully won't be too long now...
</p>
<p>
Like <a href="#calcWidth">calc_width()</a>, it traverses the tree
bottom to top, and for every item it stores the minimal height to "y_end".
</p>
<p>
Blank lines always have the height of 1. Anchor items have no height. (They are virtual...)
</p>
<p>
Text items have their height assigned already in
<a href="#assignWidth">assign_width()</a>, while <a
href="#lineBreaking">Breaking String into Lines</a>.
</p>
<p><pre>
##################################################################################
+--------------------------------------------------------------------------------+___
|header text |__0
+--------------------------------------------------------------------------------+ 1<--y_end
**********************************************************************************___
* *__0
********************************************************************************** 1<--
[...]
+--------------------------------------------------------------------------------+___
|this very long second paragraph contains some special characters (including a | 0
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: | 1
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the| 2
|only tag with parameters); and finally a blank row |__3
+--------------------------------------------------------------------------------+ 4<--
+--------------------------------------------------------------------------------+___
|(a single tag) |__0
+--------------------------------------------------------------------------------+ 1<--
</pre></p>
<p>
The height of a box item is calculated by summing up the height of all its
sub-items. This is done by "first_child" and "next", just as seeking the widest
sub-item in <a href="#calcWidth">calc_width()</a>.
</p>
<p><pre>
##################################################################################
+--------------------------------------------------------------------------------+
|header text |
+--------------------------------------------------------------------------------+ <-- 1
**********************************************************************************
* *
********************************************************************************** <-- +1
[...] [...]
+--------------------------------------------------------------------------------+
|this very long second paragraph contains some special characters (including a |
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: |
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the|
|only tag with parameters); and finally a blank row |
+--------------------------------------------------------------------------------+ <-- +4
+--------------------------------------------------------------------------------+
|(a single tag) |
+--------------------------------------------------------------------------------+ <-- +1
################################################################################## <## 13
item_tree->y_end
</pre></p>
<a name="assignYwidth" id="assignYwidth">
<h3>assign_ywidth()</h3>
<p>
The fourth pass is also fairly simple. It assigns y-coordinates to all items.
There is no initialisation necessary before calling it, as the y_end of the
global item is already set by calc_ywidth(), and y_start is always 0.
</p>
<p>
Like in <a href="#assignWidth">assign_width()</a>, the tree is
traversed top to bottom, and the sizes of all sub-items are assigned while
processing the parent.
</p>
<p>
Thus, text and blank items do not need any processing -- they do not have any
children.
</p>
<p>
For box items, the coordinates of all sub-items are assigned in a loop, like in
<a href="#assignWidth">assign_width()</a>. Every item is put
immediately after the previous one, i.e. it starts where the previous one ends.
We keep track of the current position by "y_pos". At the beginning it is
initialized to "y_start" of the box.
</p>
<p><pre>
##################################################################################
0 <## <== y_pos
item_tree->y_start
</pre></p>
<p>
For every item, y_start is set to the current y_pos.
</p>
<p><pre>
y_end y_start
| |
+--###############################################################################____ v v
|header text |___0 <--
* *___1 <-- <**
|heading |___2 <** <----
* *___3 <---- <****
|first paragraph of text; includes multiple spaces and newlines, emphasized text | 4 <**** <--
|and strong text |___5
* *___6 <-- <**
|starting with an evil center tag, |___7 <** <----
|this very long second paragraph contains some special characters (including a | 8 <---- <-- <==
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: | cur_item->y_start=y_pos=8
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the|
|only tag with parameters); and finally a blank row |
+--------------------------------------------------------------------------------+ <-- cur_item->y_end=4
+--------------------------------------------------------------------------------+
|(a single tag) |
+--------------------------------------------------------------------------------+ <---- 1
################################################################################## <## 13
</pre></p>
<p>
"y_end" of an item is determined by adding the y-size (stored in "y_end" up to
now) to "y_start" of the item.
</p>
<p><pre>
+--###############################################################################____
|header text |___0 <--
* *___1 <-- <**
|heading |___2 <** <----
* *___3 <---- <****
|first paragraph of text; includes multiple spaces and newlines, emphasized text | 4 <**** <--
|and strong text |___5
* *___6 <-- <**
|starting with an evil center tag, |___7 <** <----
|this very long second paragraph contains some special characters (including a | 8 <---- <-- <==
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: | 9
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the| 10
|only tag with parameters); and finally a blank row |__11
+--------------------------------------------------------------------------------+ 12 <-- cur_item->y_end=cur_item->ystart+cur_item->y_end=12
+--------------------------------------------------------------------------------+
|(a single tag) |
+--------------------------------------------------------------------------------+ <---- 1
################################################################################## <## 13
</pre></p>
<p>
"y_pos" is adjusted to the end of the item.
</p>
<p><pre>
+--###############################################################################____
|header text |___0 <--
* *___1 <-- <**
|heading |___2 <** <----
* *___3 <---- <****
|first paragraph of text; includes multiple spaces and newlines, emphasized text | 4 <**** <--
|and strong text |___5
* *___6 <-- <**
|starting with an evil center tag, |___7 <** <----
|this very long second paragraph contains some special characters (including a | 8 <---- <--
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: | 9
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the| 10
|only tag with parameters); and finally a blank row |__11
+--------------------------------------------------------------------------------+ 12 <-- <==
+--------------------------------------------------------------------------------+
|(a single tag) |
+--------------------------------------------------------------------------------+<---- 1
################################################################################## <## 13
</pre></p>
<p>
In the next iteration, "y_pos" -- which now points to the end of the current
item -- is used as the beginning of the new item.
</p>
<p><pre>
+--###############################################################################____
|header text |___0 <--
* *___1 <-- <**
|heading |___2 <** <----
* *___3 <---- <****
|first paragraph of text; includes multiple spaces and newlines, emphasized text | 4 <**** <--
|and strong text |___5
* *___6 <-- <**
|starting with an evil center tag, |___7 <** <----
|this very long second paragraph contains some special characters (including a | 8 <---- <--
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: | 9
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the| 10
|only tag with parameters); and finally a blank row |__11
|(a single tag) | 12 <-- <---- <==
+--------------------------------------------------------------------------------+<---- 1
################################################################################## <## 13
</pre></p>
<a name="linkCoords" id="linkCoords">
<h4>Link Coordinates</h4>
<p>
Coordinates of links and anchors (both x and y!) are presently also assigned
here. Probably it would be a better idea to do that in extra pass... As soon as
the current link implementation is dropped and anchors are cleanly implemented,
the assignment can be cleanly and logically split between assign_xwidth() and
assign_ywidth(), just as for other item types.
</p>
<p>
Link coordinates are assigned by the parent text item. The text block is
scanned, one link after the other, for the lines containing the link start and
end. (The first line that ends after the link start, and the first line that
ends at/after the link end.) As an optimization, the search for the next link
is started in the line where the previous ended, not from beginning -- links
can't be nested.
</p>
<p>
After having the line, (and thus also the y coordinate), the x coordinate is
calculated by adding the link's relative position inside the line
(link[].start-line_start) to the x coordinate of the line start. (Which is
equal to the item start for normal text, but has to be calculated seperately
for every line in centered text items.)
</p>
<p>
Inline anchors are very similar; however, they get their coordinates while
processing the anchor item, not the text item containing them. (The text item
doesn't know anything about the anchors.) Anchors can be nested if created by
<span> or so; thus the above optimization isn't possible. (It would be harder
anyhow, due to the anchors being processed every one on its own.)
</p>
<p>
Block anchors need another processing, of course: If they are empty, they keep
the coordinates assigned to them by the parent box; otherwise, all (immediate)
virtual children are scanned for the minimum/maximum for each of the four
coordinates; these are assigned as the coordinates of the anchor virtual box.
</p>
</a> <!-- linkCoords -->
</a> <!-- assignYwidth -->
<a name="createMap" id="createMap">
<h3>create_map()</h3>
<a name="pageMap" id="pageMap">
<p>
The last sub-pass generates the "page_map[]". (This could be also done inside
assign_ywidth()...) This map is necessary to quickly determine which elements
show up in the visible area of the page, when it is displayed in the viewer.
</p>
<p>
The page usage map stores references to all items that show up in any given
line of the output page. This is a very simple approach, but it should be
perfectly sufficient as long as netrik has no full graphic mode -- and this
will be probably for quite a while...
</p>
<p>
"page_map[]" is an array containing an "Item_list" structure for every line of
the output page. This structure is declared in items.h . It contains a count of
items in this line, and an array of pointers to the items.
</p>
</a> <!--pageMap-->
<p>
The whole line map is allocated at the beginning. Afterwards, all items are
processed in a loop. For every item that is visible on the screen (presently
this are only text items), a reference to this item is stored to every line of
"page_map[]" between "y_start" and "y_end" of the item.
</p>
<p><pre>
line|page_map[line]
+--###############################################################################__ ----+--------------
|header text |__ 0 | t0
* *__ 1 | -
|heading |__ 2 | t1
* *______3_|_-____
|first paragraph of text; includes multiple spaces and newlines, emphasized text | 4 | t2 <-- cur_item->y_start
|and strong text |______5_|_t2___
* *__ 6 | - <-- cur_item->y_end
|starting with an evil center tag, |__ 7 | -
|this very long second paragraph contains some special characters (including a | 8 | -
|simple space...): &; <>"=/ plus a big gap###and two unicode escapes (decimal: | 9 | -
|and hexal: ) but also an anchor embedded inside a word (this anchor also is the| 10 | -
|only tag with parameters); and finally a blank row |__ 1 | -
|(a single tag) |__ 12 | -
###############################################################################--+
</pre></p>
<p>
"page_map[]" is returned to pre_render(), and from there to main(), where it is
passed to <a href="#dump">dump()</a> or to
<a href="#render">render()</a> (via
<a href="hacking-pager.html#display">display()</a>, see
<a href="hacking-pager.html">hacking-pager.*</a>) along with "item_tree".
</p>
</a> <!--createMap-->
<a name="freeMap" id="freeMap">
<h3>free_map()</h3>
<p>
This function frees the memory allocated for the page map.
</p>
<p>
First it goes through the table line by line, and frees the associated "list"
for each one. Afterwards, it frees the table itself.
</p>
</a> <!--freeMap-->
</a> <!--preRender-->
<a name="renderC" id="renderC">
<h2>8. render.c</h2>
<p>
With the item tree and the page usage map prepared in pre_render(), we can now
actually render the page. There are two different rendering functions:
</p>
<p>
dump() renders the whole page and dumps the output to the terminal. The output
is layouted correctly, using all the coordinates and text attributes.
</p>
<p>
render() renders only a specified area of the page, and outputs it to the
curses screen.
</p>
<a name="dump" id="dump">
<h3>dump()</h3>
<p>
The page is rendered line by line, using
"<a href="#pageMap">page_map[]</a>" (see
<a href="#createMap">create_map()</a> above) to determine which
items we need to render in every line. Of course this isn't really necessary,
as we do not ever have more than one item in a line presently. However, using
"page_map[]" here is *not* overkill, just for a change ;-) On the contrary,
this is a pragmatic approach. Dumping the page item by item without using
"page_map[]" would be more efficient; however, it would be also more
complicated than dumping line by line.
</p>
<p>
In each line, we process all items (from "page_map[line]") one after the other.
If there was actually more then one item (which is impossible presently...),
they would be printed one after the other -- with disasterous results... No
code is implemented for really handling this situation yet.
</p>
<p>
First action to do for each item is setting the cursor position to the
beginning of the text of this line. (Retrieved by
<a href="#linePos">line_pos()</a> and stored in "x_start".) This
we do by going forward as much character positions as necessary, by printing
that many space characters.
</p>
<p>
Now we can output the text itself. To know what is actually to be printed in
this line, the start and end positions of this line's text inside the string
(text block) are retrieved with <a href="#lineStartEnd">line_start() and
line_end()</a>. (And stored in "text_start" and "text_end".)
</p>
<p>
Having this, we print all attribute divisions in a loop. But first we have to
find the first one that shows up in the current line.
</p>
<p><pre>
div[0].end ... div[3].end
v v v v
text: "first[...]multiple spaces and newlines, emphasized text and strong text"
| ^ | ^
text_start text_end
</pre></p>
<p>
In every iteration we print all the text between "div_start" end "div_end",
which normally point to the start and end of the current division. For the
first iteration, "div_start" is set to "text_start" -- we only want the part of
the div that acutally shows up in the line.
</p>
<p><pre>
v v v v
text: "first[...]multiple spaces and newlines, emphasized text and strong text"
^ ^ ^
div_start=text_start div_end=div[0].end text_end
output:
first paragraph of text; contains multiple
spaces and newlines, <-- line
</pre></p>
<p>
The next div starts where the current one ends.
</p>
<p><pre>
v v v v
text: "first[...]multiple spaces and newlines, emphasized text and strong text"
^ ^ ^ ^
text_start div_start div_end text_end
</pre></p>
<p>
The last division is truncated to "text_end" -- as with the first one, we only
want the part that shows up in the current line.
</p>
<p><pre>
v v v v
text: "first[...]multiple spaces and newlines, emphasized text and strong text"
^ ^ ^
div_start div_end
=text_end
output:
first paragraph of text; contains multiple
spaces and newlines, emphasized text and strong
</pre></p>
<p>
Before actually printing the text, it is copied into a temporary string, but
replacing all &nbsp; characters by normal spaces. (There are various problems
resulting from putting real '\xa0' characters on the screen: For one, with
fonts where this char isn't really blank (always the case if the charset isn't
iso-8859-x) it's unusable. Also, copying via screen/GPM/X clipboard usually has
undesired results.)
</p>
<p>
Finally, we test if the line ends with a word break, and print the break
character if it does. We know it does when the character at the line end is a
word character (not a space, newline, or string end), because every word end is
followed by the space separating it from the next word; if there is no space at
the line end, we are inside a wrapped word.
</p>
<p><pre>
v v
text "Some sentence containing aVeryLongAndThusBrokenWord."
^
output:
Some sentence
containing aVeryLongAn\
...
dThusBrokenWord.
</pre></p>
</a> <!--dump-->
<a name="render" id="render">
<h3>render()</h3>
<p>
render() works similar to <a href="#dump">dump()</a>. The main difference is how the output is
printed. However, there are a couple of differences in screen position handling
and other calculations also.
</p>
<p>
render() takes the starting positon of the rendered area inside the page, the
starting position on the screen, and the size of the rendered area as
arguments.
</p>
<p>
The area is processed line by line, and every line is processed item by item,
just like in dump().
</p>
<p>
"x_start" describes the starting column relative to the beginning of the
rendered area, not the screen. (dump() always dumps whole lines, and thus there
is no difference.) If it turns out that the line starts before the rendered
area, it has to be truncated. the ending position "x_end" is calculated in a
similar fashion.
</p>
<p>
Before the line is printed (div by div, as in dump()), the cursor is set to the
start position of the line. The column is the starting position relative to the
area ("x_start"), plus the starting position of the area on the screen;
likewise the row.
</p>
<p>
Before anything is printed, we test if some part of the line shows up inside
the rendered area at all. The word break indicator is also printed only if the
line ends inside the area.
</p>
<p>
If render() was called with the "overpaint" flag, the requested area is cleaned
before rendering anything, so any garbage will be removed. (Areas not
containing any text aren't affected otherwise.) This is done by overwriting the
desired part of each line with a string of spaces.
</p>
</a> <!--render-->
<a name="dumpItems" id="dumpItems">
<h3>dump_items()</h3>
<p>
dump_items() dumps the item tree, including the text of text items. The text is
printed with correct attributes, but ignoring any coordinates and line breaks.
This function is for debugging purposes, and may be called anywhere inside or
after pre_render() (anywhere after parse_struct()).
</p>
<p>
The reason this function is in render.c is that it needs the same screen
handling functions as <a href="#dump">dump()</a>. Moreover, it
works in a very similar fashion.
</p>
<p>
The difference is that it does not dump line by line, but item by item
(traversing the tree top to bottom). After printing some information about the
item itself, it dumps the text division by division in the same way dump()
does, only it doesn't need to care about positions or line breaks; it dumps the
whole string at once.
</p>
</a> <!--dumpItems-->
</a> <!--renderC-->
<a name="itemsC" id="itemsC">
<h2>10. items.c</h2>
<p>
items.c contains a few simple helper functions intended to simplify retrieving
some common date from the structure (item) tree.
</p>
<p>
The advantage of using such helper functions, even if they are really simple,
is less code duplication -- which improves maintainability and probably makes
the code also easier to understand. (This is actually an approach toward
so-called object oriented programming...) On the other hand, these functions
are extremly inefficient, as they calculate intermediate values which could be
shared, and the calling overhead itself is fairly big for such simple
functions. (Effectively probably increasing code size.) Maybe we should try to
define them as macros, or just put them into an include file so they can be
inlined during optimized compilation. (Let the compiler decide...)
</p>
<a name="lineStartEnd" id="lineStartEnd">
<h3>line_start() and line_end()</h3>
<p>
line_start() and line_end() are used to find out at which position some
specific text line starts/ends inside the string of a (wrapped) text block.
</p>
<p>
The positions are read from the line_table[] (see
<a href="lineBreaking">Breaking String into Lines</a>). This is
normally trivial, but there are exceptions for the first and last lines.
</p>
<p>
line_end() has an additional quirk: If the line ends with a blank, the line end
position is decremented -- when a line wraps at a blank, this blank is always
discarded.
</p>
<p>
Note that the line number given to these functions is *not* the line number
relative to the start of the text block, but the page line.
</p>
</a> <!-- lineStartEnd -->
<a name="linePos" id="linePos">
<h3>line_pos()</h3>
<p>
The horizontal page position of a single text line can be retrieved using
line_pos(). This is useful because of centered text items, where the line
starts do not equal the text block start, and differ from line to line.
</p>
<p>
The line number argument is in page coordinates just as in
<a href="#lineStartEnd">line_start() and line_end()</a> above.
</p>
</a> <!-- linePos -->
</a> <!-- itemsC -->
</body>
</html>
|