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
|
// Copyright (C) 2004-2025 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include "mupdf/ucdn.h"
#include "html-imp.h"
#include "hb.h"
#include "hb-ft.h"
#include <ft2build.h>
#include <math.h>
#include <assert.h>
#undef DEBUG_HARFBUZZ
#undef DEBUG_DESPERATE_SPLITTING
/*
Some notes on the layout code below and the concepts used.
THE MODEL:
The standard CSS box model is used here. Each box has a margin, a border, padding, and content, any of which can be zero in size.
+-------------------------------------------------------------------+
| margin |
| +-------------------------------------------------+ |
| | border | |
| | +-------------------------------+ | |
| | | padding | | |
| | | +---------+ | | |
| | | | content | | | |
| | | +---------+ | | |
| | | | | |
| | +-------------------------------+ | |
| | | |
| +-------------------------------------------------+ |
| |
+-------------------------------------------------------------------+
In our structures, the box->x,y,w,b refer to the bounding box of the content section alone. The margins/borders/paddings are applied
to the outside of this automatically.
HOW WE LAYOUT:
Our implementation is a 'simple' recursive descent of the structure. We know the width of the area we are laying out into at each point,
and we know the top/left coords of the enclosing block. So we start with a well defined left/top point, offset that for margins/borders/padding
etc, calculate the width from the enclosing width (via the CSS). We set the height of the content to be zero to start with, and after laying
out each of (any) child elements, we ensure that the base of our box is always large enough to enclose the child's boxes with the appropriate
padding.
VERTICAL MARGIN COLLAPSE:
The big complexity in this code is the need to do 'vertical margin collapse'. Basically, if we have 2 blocks, laid out vertically from one
another, the margin between them is collapsed to be the larger of margins of the two blocks, rather than the sum of the margins of the
two blocks.
So block A with a margin of 30, and block B with a margin of 20 will be separated by 30, not 50.
To make matters more complicated, margin collapse can happen 'through' nestings of blocks, and even through empty blocks. It cannot
happen through blocks with borders, or padding.
Accordingly, as we recursively pass through our structure laying out, we keep a 'vertical' parameter, representing the amount of vertical
space to be considered for collapse. This is set to zero if we hit borders or padding or non-zero height content boxes, and our margins are
adjusted by it.
While the margins of a block may be adjusted by a parent 'inheriting' those margins, the bbox of the content of a block will never be
changed.
NON-RESTARTING OPERATION:
The standard layout operation as used for HTML documents and ebooks has us layout onto an infinitely long page, with the vertical
offsets of boxes adjusted so that a given line of text (or image) never spans a page end (defined to be a multiple of page_h). This
layout can proceed from start to finish with no need to stop. This is achieved by calling the code with restart == NULL.
RESTARTING:
In order to cope with laying out a text story into multiple (potentially differently sized) areas, we need to be able to stop the layout
when the available area is full, and restart again later. We do this by having a restart record. The tree structure means we can't
simply return a pointer to the node to continue processing at - we need to recursively redescend the tree until we reach the same
position, whereupon we will continue. Also, because of the fact that the area being laid out into is potentially a completely different
size, we need to recalculate widths etc, so at least part of the tree needs to be reprocessed when we restart.
Accordingly, two ways to do this immediately suggest themselves. The first is to store the path down the tree so that we can quickly
and efficiently skip whole areas of the tree that do not need reprocessing and redescend just the sections we need to. The second is to
accept that we will reprocess the entire prefix of the tree each time, and just not actually do any work until we reach the exact
box where we need to restart.
We opt for the second one here, on the grounds that 1) it's easier to code, 2) it requires a constant amount of storage, and 3) that
the trees we will be reprocessing will be relatively small. Should we ever need to revisit this decision, we can do so.
*/
enum { T, R, B, L };
/* === LAYOUT INLINE TEXT === */
typedef struct string_walker
{
fz_context *ctx;
hb_buffer_t *hb_buf;
int rtl;
const char *start;
const char *end;
const char *s;
fz_font *base_font;
int script;
int language;
int small_caps;
fz_font *font;
fz_font *next_font;
hb_glyph_position_t *glyph_pos;
hb_glyph_info_t *glyph_info;
unsigned int glyph_count;
int scale;
int graphemes;
} string_walker;
static int quick_ligature_mov(fz_context *ctx, string_walker *walker, unsigned int i, unsigned int n, int unicode)
{
unsigned int k;
for (k = i + n + 1; k < walker->glyph_count; ++k)
{
walker->glyph_info[k-n] = walker->glyph_info[k];
walker->glyph_pos[k-n] = walker->glyph_pos[k];
}
walker->glyph_count -= n;
return unicode;
}
static int quick_ligature(fz_context *ctx, string_walker *walker, unsigned int i)
{
if (walker->glyph_info[i].codepoint == 'f' && i + 1 < walker->glyph_count && !fz_font_flags(walker->font)->is_mono)
{
if (walker->glyph_info[i+1].codepoint == 'f')
{
if (i + 2 < walker->glyph_count && walker->glyph_info[i+2].codepoint == 'i')
{
if (fz_encode_character(ctx, walker->font, 0xFB03))
return quick_ligature_mov(ctx, walker, i, 2, 0xFB03);
}
if (i + 2 < walker->glyph_count && walker->glyph_info[i+2].codepoint == 'l')
{
if (fz_encode_character(ctx, walker->font, 0xFB04))
return quick_ligature_mov(ctx, walker, i, 2, 0xFB04);
}
if (fz_encode_character(ctx, walker->font, 0xFB00))
return quick_ligature_mov(ctx, walker, i, 1, 0xFB00);
}
if (walker->glyph_info[i+1].codepoint == 'i')
{
if (fz_encode_character(ctx, walker->font, 0xFB01))
return quick_ligature_mov(ctx, walker, i, 1, 0xFB01);
}
if (walker->glyph_info[i+1].codepoint == 'l')
{
if (fz_encode_character(ctx, walker->font, 0xFB02))
return quick_ligature_mov(ctx, walker, i, 1, 0xFB02);
}
}
return walker->glyph_info[i].codepoint;
}
static void init_string_walker(fz_context *ctx, string_walker *walker, hb_buffer_t *hb_buf, int rtl, fz_font *font, int script, int language, int small_caps, const char *text, int cluster_as_graphemes)
{
walker->ctx = ctx;
walker->hb_buf = hb_buf;
walker->rtl = rtl;
walker->start = text;
walker->end = text;
walker->s = text;
walker->base_font = font;
walker->script = script;
walker->language = language;
walker->font = NULL;
walker->next_font = NULL;
walker->small_caps = small_caps;
walker->graphemes = cluster_as_graphemes;
}
static void
destroy_hb_shaper_data(fz_context *ctx, void *handle)
{
fz_hb_lock(ctx);
hb_font_destroy(handle);
fz_hb_unlock(ctx);
}
static const hb_feature_t small_caps_feature[1] = {
{ HB_TAG('s','m','c','p'), 1, 0, (unsigned int)-1 }
};
static int walk_string(string_walker *walker)
{
fz_context *ctx = walker->ctx;
FT_Face face;
int fterr;
int quickshape;
char lang[8];
walker->start = walker->end;
walker->end = walker->s;
walker->font = walker->next_font;
if (*walker->start == 0)
return 0;
/* Run through the string, encoding chars until we find one
* that requires a different fallback font. */
while (*walker->s)
{
int c;
walker->s += fz_chartorune(&c, walker->s);
(void)fz_encode_character_with_fallback(ctx, walker->base_font, c, walker->script, walker->language, &walker->next_font);
if (walker->next_font != walker->font)
{
if (walker->font != NULL)
break;
walker->font = walker->next_font;
}
walker->end = walker->s;
}
/* Disable harfbuzz shaping if script is common or LGC and there are no opentype tables. */
quickshape = 0;
if (walker->script <= 3 && !walker->rtl && !fz_font_flags(walker->font)->has_opentype)
quickshape = 1;
fz_hb_lock(ctx);
fz_try(ctx)
{
face = fz_font_ft_face(ctx, walker->font);
walker->scale = face->units_per_EM;
fterr = FT_Set_Char_Size(face, walker->scale, walker->scale, 72, 72);
if (fterr)
fz_throw(ctx, FZ_ERROR_LIBRARY, "freetype setting character size: %s", ft_error_string(fterr));
hb_buffer_clear_contents(walker->hb_buf);
hb_buffer_set_direction(walker->hb_buf, walker->rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
/* hb_buffer_set_script(walker->hb_buf, hb_ucdn_script_translate(walker->script)); */
if (walker->language)
{
fz_string_from_text_language(lang, walker->language);
Memento_startLeaking(); /* HarfBuzz leaks harmlessly */
hb_buffer_set_language(walker->hb_buf, hb_language_from_string(lang, (int)strlen(lang)));
Memento_stopLeaking(); /* HarfBuzz leaks harmlessly */
}
if (walker->graphemes)
hb_buffer_set_cluster_level(walker->hb_buf, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES);
else
hb_buffer_set_cluster_level(walker->hb_buf, HB_BUFFER_CLUSTER_LEVEL_CHARACTERS);
hb_buffer_add_utf8(walker->hb_buf, walker->start, walker->end - walker->start, 0, -1);
if (!quickshape)
{
fz_shaper_data_t *hb = fz_font_shaper_data(ctx, walker->font);
Memento_startLeaking(); /* HarfBuzz leaks harmlessly */
if (hb->shaper_handle == NULL)
{
hb->destroy = destroy_hb_shaper_data;
hb->shaper_handle = hb_ft_font_create(face, NULL);
}
hb_buffer_guess_segment_properties(walker->hb_buf);
if (walker->small_caps)
hb_shape(hb->shaper_handle, walker->hb_buf, small_caps_feature, nelem(small_caps_feature));
else
hb_shape(hb->shaper_handle, walker->hb_buf, NULL, 0);
Memento_stopLeaking();
}
walker->glyph_pos = hb_buffer_get_glyph_positions(walker->hb_buf, &walker->glyph_count);
walker->glyph_info = hb_buffer_get_glyph_infos(walker->hb_buf, NULL);
}
fz_always(ctx)
{
fz_hb_unlock(ctx);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
if (quickshape)
{
unsigned int i;
for (i = 0; i < walker->glyph_count; ++i)
{
int glyph, unicode;
unicode = quick_ligature(ctx, walker, i);
if (walker->small_caps)
glyph = fz_encode_character_sc(ctx, walker->font, unicode);
else
glyph = fz_encode_character(ctx, walker->font, unicode);
walker->glyph_info[i].codepoint = glyph;
walker->glyph_pos[i].x_offset = 0;
walker->glyph_pos[i].y_offset = 0;
walker->glyph_pos[i].x_advance = fz_advance_glyph(ctx, walker->font, glyph, 0) * face->units_per_EM;
walker->glyph_pos[i].y_advance = 0;
}
}
return 1;
}
static const char *get_node_text(fz_context *ctx, fz_html_flow *node)
{
if (node->type == FLOW_WORD)
return node->content.text;
else if (node->type == FLOW_SPACE)
return " ";
else if (node->type == FLOW_SHYPHEN)
return "-";
else
return "";
}
static void measure_string_w(fz_context *ctx, fz_html_flow *node, hb_buffer_t *hb_buf)
{
float em = node->box->s.layout.em;
string_walker walker;
unsigned int i;
const char *s;
node->w = 0;
s = get_node_text(ctx, node);
init_string_walker(ctx, &walker, hb_buf, node->bidi_level & 1, node->box->style->font, node->script, node->markup_lang, node->box->style->small_caps, s, 0);
while (walk_string(&walker))
{
int x = 0;
for (i = 0; i < walker.glyph_count; i++)
x += walker.glyph_pos[i].x_advance;
node->w += x * em / walker.scale;
}
}
static void measure_string_h(fz_context *ctx, fz_html_flow *node)
{
float em = node->box->s.layout.em;
if (fz_css_number_defined(node->box->style->leading))
node->h = fz_from_css_number(node->box->style->leading, em, em, 0);
else
node->h = fz_from_css_number_scale(node->box->style->line_height, em);
}
static float measure_line(fz_html_flow *node, fz_html_flow *end, float *baseline)
{
float max_a = 0, max_d = 0, h = node->h;
while (node != end)
{
if (node->type == FLOW_IMAGE)
{
if (node->h > max_a)
max_a = node->h;
}
else if (node->type != FLOW_SBREAK && node->type != FLOW_BREAK && node->type != FLOW_ANCHOR)
{
// Clamp ascender/descender to line-height size.
// TODO: This is not entirely to spec, but close enough.
float s = fz_min(node->box->s.layout.em, node->h);
float a = s * 0.8f;
float d = s * 0.2f;
if (a > max_a) max_a = a;
if (d > max_d) max_d = d;
}
if (node->h > h)
h = node->h;
node = node->next;
}
*baseline = max_a + (h - max_a - max_d) / 2;
return h;
}
static void layout_line(fz_context *ctx, float indent, float page_w, float line_w, int align, fz_html_flow *start, fz_html_flow *end, fz_html_box *box, float baseline, float line_h)
{
float x = box->s.layout.x + indent;
float y = box->s.layout.b;
float slop = page_w - line_w;
float justify = 0;
float va;
int n, i;
fz_html_flow *node;
fz_html_flow **reorder;
unsigned int min_level, max_level;
/* Count the number of nodes on the line */
for(i = 0, n = 0, node = start; node != end; node = node->next)
{
n++;
if (node->type == FLOW_SPACE && node->expand && !node->breaks_line)
i++;
}
if (align == TA_JUSTIFY)
{
justify = slop / (i ? i : 1);
}
else if (align == TA_RIGHT)
x += slop;
else if (align == TA_CENTER)
x += slop / 2;
/* We need a block to hold the node pointers while we reorder */
reorder = fz_malloc_array(ctx, n, fz_html_flow*);
min_level = start->bidi_level;
max_level = start->bidi_level;
for(i = 0, node = start; node != end; i++, node = node->next)
{
reorder[i] = node;
if (node->bidi_level < min_level)
min_level = node->bidi_level;
if (node->bidi_level > max_level)
max_level = node->bidi_level;
}
/* Do we need to do any reordering? */
if (min_level != max_level || (min_level & 1))
{
/* The lowest level we swap is always a rtl one */
min_level |= 1;
/* Each time around the loop we swap runs of fragments that have
* levels >= max_level (and decrement max_level). */
do
{
int start_idx = 0;
int end_idx;
do
{
/* Skip until we find a level that's >= max_level */
while (start_idx < n && reorder[start_idx]->bidi_level < max_level)
start_idx++;
/* If start >= n-1 then no more runs. */
if (start_idx >= n-1)
break;
/* Find the end of the match */
i = start_idx+1;
while (i < n && reorder[i]->bidi_level >= max_level)
i++;
/* Reverse from start to i-1 */
end_idx = i-1;
while (start_idx < end_idx)
{
fz_html_flow *t = reorder[start_idx];
reorder[start_idx++] = reorder[end_idx];
reorder[end_idx--] = t;
}
start_idx = i+1;
}
while (start_idx < n);
max_level--;
}
while (max_level >= min_level);
}
for (i = 0; i < n; i++)
{
float w;
node = reorder[i];
w = node->w;
if (node->type == FLOW_SPACE && node->breaks_line)
w = 0;
else if (node->type == FLOW_SPACE && !node->breaks_line)
w += node->expand ? justify : 0;
else if (node->type == FLOW_SHYPHEN && !node->breaks_line)
w = 0;
else if (node->type == FLOW_SHYPHEN && node->breaks_line)
w = node->w;
node->x = x;
x += w;
switch (node->box->style->vertical_align)
{
default:
case VA_BASELINE:
case VA_SUB:
case VA_SUPER:
case VA_MIDDLE:
va = node->box->s.layout.baseline;
break;
case VA_TOP:
case VA_TEXT_TOP:
va = -baseline + node->box->s.layout.em * 0.8f;
break;
case VA_BOTTOM:
case VA_TEXT_BOTTOM:
va = -baseline + line_h - node->box->s.layout.em * 0.2f;
break;
}
if (node->type == FLOW_IMAGE)
node->y = y + baseline - node->h;
else
{
node->y = y + baseline + va;
node->h = node->box->s.layout.em;
}
}
fz_free(ctx, reorder);
if (box->s.layout.x + box->s.layout.w < x)
box->s.layout.w = x - box->s.layout.x;
}
typedef struct
{
fz_pool *pool;
float page[4];
/* The ancestor trbl is the trbl for the last positioned
* node we have walked though. Boxes with "absolute" positioning
* will be positioned relative to this. Initially this will be the
* same as page. */
float ancestor[4];
/* bounds is the trbl that is the current bounds we're filling
* into. This will initially be page, but can be changed by
* positions/width/heights etc. Our next fill should be at
* bounds[L],[T]. */
float bounds[4];
/* used should be updated on exit from any layout stage to
* be the area used by this block. */
float used[4];
hb_buffer_t *hb_buf;
fz_html_restarter *restart;
} layout_data;
/* "are we in a restarting context, and still skipping?" */
#define WE_ARE_SKIPPING(restart) ((restart) && (restart)->start != NULL)
/* "are we in a restarting context without having stopped for a restart yet?" */
#define SHOULD_STOP_FOR_RESTART(restart) ((restart) && (restart)->end == NULL)
/* "are we in a restarting context, and stopping already?" */
#define STOPPING_FOR_RESTART(restart) ((restart) && (restart)->end != NULL)
#define TRBLCPY(DST,SRC) \
do { DST[T] = SRC[T] ; DST[R] = SRC[R]; DST[B] = SRC[B]; DST[L] = SRC[L]; } while (0)
static int flush_line(fz_context *ctx, fz_html_box *box, layout_data *ld, float line_w, int align, float indent, fz_html_flow *a, fz_html_flow *b, fz_html_restarter *restart)
{
float avail, line_h, baseline;
float page_w = ld->bounds[R] - ld->bounds[L];
float page_h = ld->page[B] - ld->page[T];
float page_top = ld->page[T];
line_h = measure_line(a, b, &baseline);
if (page_h > 0)
{
avail = page_h - fmodf(box->s.layout.b - page_top, page_h);
/* If the line is larger than the available space skip to the start
* of the next page. */
if (line_h > avail)
{
if (restart)
{
assert(restart->start == NULL);
if (restart->potential)
restart->end = restart->potential;
else
{
restart->end = box;
restart->end_flow = a;
}
restart->reason = FZ_HTML_RESTART_REASON_LINE_HEIGHT;
return 1;
}
box->s.layout.b += avail;
}
}
layout_line(ctx, indent, page_w, line_w, align, a, b, box, baseline, line_h);
box->s.layout.b += line_h;
if (box->s.layout.b > ld->used[B])
ld->used[B] = box->s.layout.b;
if (restart)
restart->potential = NULL;
return 0;
}
static void split_flow_at_byte_offset(fz_context *ctx, fz_pool *pool, fz_html_flow *flow, size_t offset)
{
fz_html_flow *new_flow;
char *text;
size_t len;
assert(flow->type == FLOW_WORD);
assert(offset != 0);
text = flow->content.text + offset;
len = strlen(text);
new_flow = fz_pool_alloc(ctx, pool, offsetof(fz_html_flow, content) + len+1);
memcpy(new_flow, flow, offsetof(fz_html_flow, content));
new_flow->next = flow->next;
flow->next = new_flow;
strcpy(new_flow->content.text, text);
*text = 0;
}
/* node becomes last cluster, node->next becomes the rest */
static void split_flow_at_byte_offset_reverse(fz_context *ctx, fz_pool *pool, fz_html_flow *flow, size_t offset)
{
fz_html_flow *new_flow;
char *text;
size_t len;
assert(flow->type == FLOW_WORD);
assert(offset != 0);
text = flow->content.text + offset;
len = strlen(text);
new_flow = fz_pool_alloc(ctx, pool, offsetof(fz_html_flow, content) + offset+1);
memcpy(new_flow, flow, offsetof(fz_html_flow, content));
new_flow->next = flow->next;
flow->next = new_flow;
memcpy(new_flow->content.text, flow->content.text, offset);
new_flow->content.text[offset] = 0;
memmove(flow->content.text, text, len);
flow->content.text[len] = 0;
}
static void break_word_for_overflow_wrap(fz_context *ctx, fz_html_flow *node, layout_data *ld, float max_w)
{
hb_buffer_t *hb_buf = ld->hb_buf;
const char *text = node->content.text;
string_walker walker;
float w = 0;
unsigned int at = (unsigned int)-1;
float em = node->box->s.layout.em;
assert(node->type == FLOW_WORD);
assert(node->atomic == 0);
/* The entire flow doesn't fit on a line, so we need to break it in the middle.
* We need to be careful not to break it in the middle of a cluster, as this
* would really mess shaping up.
*
* For left 2 right text, this means split the first cluster into its own 'atomic'
* node, and shorten the remainder.
*
* For right 2 left text, we split the last cluster into its own 'atomic' node
* and shorten the remainder. This is fine, cos although it appears to change the
* logical ordering for the text, we never extract from HTML, and the correct
* appearance is preserved.
*
* Desperately breaking in the middle of a word like this should should
* rarely (if ever) come up.
*
* TODO: Split at all the clusters in the word at once.
*/
/* Walk string and split at the first cluster. */
if ((node->bidi_level & 1) == 0)
{
/* Left 2 Right */
init_string_walker(ctx, &walker, hb_buf, 0 /* L2R */, node->box->style->font, node->script, node->markup_lang, node->box->style->small_caps, text, 1);
while (walk_string(&walker))
{
unsigned int i;
#ifdef DEBUG_DESPERATE_SPLITTING
for (i = 0; i < walker.glyph_count; ++i)
{
uint32_t can_break_here = (hb_glyph_info_get_glyph_flags(&walker.glyph_info[i]) & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) == 0;
printf("%s(%x, %d)",
can_break_here ? "|" : " ",
walker.glyph_info[i].codepoint, walker.glyph_info[i].cluster);
}
printf("\n");
#endif
for (i = 0; i < walker.glyph_count; ++i)
{
uint32_t can_break_here = (hb_glyph_info_get_glyph_flags(&walker.glyph_info[i]) & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) == 0;
if (can_break_here)
{
/* If this fragment would take us beyond the end, then give up. */
if (w > max_w)
break;
at = walker.start + walker.glyph_info[i].cluster - text;
}
w += walker.glyph_pos[i].x_advance * em / walker.scale;
/* Make sure we have the whole cluster */
while (i+1 < walker.glyph_count && walker.glyph_info[i].cluster == walker.glyph_info[i+1].cluster)
{
i++;
w += walker.glyph_pos[i].x_advance * em / walker.scale;
}
}
}
if (at != (unsigned int)-1 && at != 0 && at != strlen(text))
{
/* node becomes first cluster, node->next becomes the rest */
split_flow_at_byte_offset(ctx, ld->pool, node, at);
node->next->overflow_wrap = 1;
measure_string_w(ctx, node, ld->hb_buf);
measure_string_w(ctx, node->next, ld->hb_buf);
return;
}
}
else
{
/* Right 2 Left */
init_string_walker(ctx, &walker, hb_buf, 1 /* R2L */, node->box->style->font, node->script, node->markup_lang, node->box->style->small_caps, text, 1);
while (walk_string(&walker))
{
unsigned int i;
#ifdef DEBUG_DESPERATE_SPLITTING
for (i = 0; i < walker.glyph_count; ++i)
{
uint32_t can_break_here = (hb_glyph_info_get_glyph_flags(&walker.glyph_info[i]) & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) == 0;
printf("%s(%x, %d)",
can_break_here ? "|" : " ",
walker.glyph_info[i].codepoint, walker.glyph_info[i].cluster);
}
printf("\n");
#endif
/* Find the first cluster we can break before. */
/* We can always break at the start of a fragment returned by walk_string
* (unless it's the very first one!) */
if (w > max_w)
break;
if (walker.start != text)
at = walker.start - text;
for(i = 0; i < walker.glyph_count; i++)
{
uint32_t can_break_here = (hb_glyph_info_get_glyph_flags(&walker.glyph_info[i]) & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) == 0;
w += walker.glyph_pos[i].x_advance * em / walker.scale;
if (can_break_here)
{
/* If this fragment would take us beyond the end, then give up. */
if (w > max_w)
break;
if (i == walker.glyph_count)
at = walker.start - text;
else
at = walker.start + walker.glyph_info[i].cluster - text;
}
}
}
if (at != (unsigned int)-1 && at != 0 && at != strlen(text))
{
/* Split at the last point found */
/* node becomes last cluster, node->next becomes the rest */
split_flow_at_byte_offset_reverse(ctx, ld->pool, node, at);
node->next->overflow_wrap = 1;
measure_string_w(ctx, node, ld->hb_buf);
measure_string_w(ctx, node->next, ld->hb_buf);
return;
}
}
/* Unless we've overflowed word is already only one cluster. Don't try breaking here again! */
if (w <= max_w)
node->atomic = 1;
}
/*
Layout a BOX_FLOW.
Flow box is in a BOX_BLOCK or BOX_TABLE_CELL context, and has no margin/padding/border.
box: The BOX_FLOW to layout.
top: The enclosing box.
*/
static void layout_flow(fz_context *ctx, layout_data *ld, fz_html_box *box, fz_html_box *top)
{
fz_html_flow *node, *line, *candidate, *desperate;
fz_html_flow *start_flow = NULL;
float line_w, candidate_w, desperate_w, indent, bounds_w;
int justify_align;
int align;
fz_html_restarter *restart = ld->restart;
float em = box->s.layout.em;
indent = box->is_first_flow ? fz_from_css_number(top->style->text_indent, em, ld->bounds[R] - ld->bounds[L], 0) : 0;
align = top->style->text_align;
justify_align = TA_LEFT;
if (box->markup_dir == FZ_BIDI_RTL)
{
justify_align = TA_RIGHT;
if (align == TA_LEFT)
align = TA_RIGHT;
else if (align == TA_RIGHT)
align = TA_LEFT;
}
/* Position the box, initially zero height. */
box->s.layout.x = ld->bounds[L];
box->s.layout.y = ld->bounds[T];
box->s.layout.w = ld->bounds[R] - ld->bounds[L];
box->s.layout.b = ld->bounds[T];
if (restart && restart->start)
{
/* If we are still skipping, and don't match, nothing to do. */
if (restart->start != box)
return;
/* We match! Remember where we should start. */
restart->start = NULL;
#ifdef DEBUG_LAYOUT_RESTARTING
/* Output us some crufty debug */
if (restart->start_flow == NULL)
printf("<restart>");
for (node = box->u.flow.head; node; node = node->next)
{
if (restart->start_flow == node)
printf("<restart>");
switch (node->type)
{
case FLOW_WORD:
printf("[%s]", node->content.text);
break;
case FLOW_BREAK:
case FLOW_SBREAK:
printf("\n");
break;
case FLOW_SPACE:
printf(" ");
break;
case FLOW_SHYPHEN:
printf("-");
break;
default:
printf("?");
break;
}
}
printf("\n");
#endif
}
/* If we have nothing to flow, nothing to do. */
if (!box->u.flow.head)
return;
/* Measure the size of all the words and images in the flow. */
node = box->u.flow.head;
/* First, if we are restarting, skip over ones we've done already. */
if (restart && restart->start_flow)
{
while(node && node != restart->start_flow)
{
indent = 0;
node = node->next;
}
start_flow = node;
restart->start_flow = NULL;
}
/* Now measure the size of the remaining nodes. */
for (; node; node = node->next)
{
node->breaks_line = 0; /* reset line breaks from previous layout */
if (node->type == FLOW_IMAGE)
{
float max_w, max_h;
float xs = 1, ys = 1, s;
float aspect = 1;
float page_h = ld->bounds[B] - ld->bounds[T];
max_w = ld->bounds[R] - ld->bounds[L];
max_h = page_h;
/* NOTE: We ignore the image DPI here, since most images in EPUB files have bogus values. */
node->w = node->content.image->w * 72.0f / 96.0f;
node->h = node->content.image->h * 72.0f / 96.0f;
aspect = node->h ? node->w / node->h : 0;
if (node->box->style->width.unit != N_AUTO)
node->w = fz_from_css_number(node->box->style->width, top->s.layout.em, ld->bounds[R] - ld->bounds[L], node->w);
if (node->box->style->height.unit != N_AUTO)
node->h = fz_from_css_number(node->box->style->height, top->s.layout.em, page_h, node->h);
if (node->box->style->width.unit == N_AUTO && node->box->style->height.unit != N_AUTO)
node->w = node->h * aspect;
if (node->box->style->width.unit != N_AUTO && node->box->style->height.unit == N_AUTO)
node->h = (aspect == 0) ? 0 : (node->w / aspect);
/* Shrink image to fit on one page if needed */
if (max_w > 0 && node->w > max_w)
xs = max_w / node->w;
if (max_h > 0 && node->h > max_h)
ys = max_h / node->h;
s = fz_min(xs, ys);
node->w = node->w * s;
node->h = node->h * s;
}
else if (node->type == FLOW_ANCHOR)
{
node->h = 0;
}
else
{
/* Note: already measured width in layout_update_widths */
measure_string_h(ctx, node);
}
}
node = box->u.flow.head;
if (start_flow)
{
line_w = start_flow == node ? indent : 0;
node = start_flow;
}
else
{
line_w = indent;
}
line = node;
candidate = NULL;
candidate_w = 0;
desperate = NULL;
desperate_w = 0;
bounds_w = ld->bounds[R] - ld->bounds[L];
/* Now collate the measured nodes into a line. */
while (node)
{
switch (node->type)
{
default:
// always same width
line_w += node->w;
break;
case FLOW_WORD:
/* Allow desperate breaking in middle of word if overflow-wrap: break-word. */
if (line_w + node->w > bounds_w && !candidate)
{
if (!node->atomic && node->box->style->overflow_wrap == OVERFLOW_WRAP_BREAK_WORD)
{
break_word_for_overflow_wrap(ctx, node, ld, bounds_w - line_w);
}
}
/* Remember overflow-wrap word fragments, unless at the beginning of a line. */
if (node->overflow_wrap && node != line)
{
desperate = node;
desperate_w = line_w;
}
line_w += node->w;
break;
case FLOW_BREAK:
case FLOW_SBREAK:
// always zero width
candidate = node;
candidate_w = line_w;
break;
case FLOW_SPACE:
// zero width if broken, default width if unbroken
candidate = node;
candidate_w = line_w;
line_w += node->w;
break;
case FLOW_SHYPHEN:
// default width if broken, zero width if unbroken
candidate = node;
candidate_w = line_w + node->w;
break;
}
/* If we see a hard break...
* or the line doesn't fit, and we have a candidate breakpoint...
* then we can flush the line so far. */
if (node->type == FLOW_BREAK || (line_w > bounds_w && (candidate || desperate)))
{
int line_align = align;
fz_html_flow *break_at;
float break_w;
if (candidate)
{
if (candidate->type == FLOW_BREAK)
line_align = (align == TA_JUSTIFY) ? justify_align : align;
candidate->breaks_line = 1;
break_at = candidate->next;
break_w = candidate_w;
}
else
{
break_at = desperate;
break_w = desperate_w;
}
if (flush_line(ctx, box, ld, break_w, line_align, indent, line, break_at, restart))
return;
line = break_at;
node = break_at;
candidate = NULL;
candidate_w = 0;
desperate = NULL;
desperate_w = 0;
indent = 0;
line_w = 0;
}
else if (restart && (restart->flags & FZ_HTML_RESTARTER_FLAGS_NO_OVERFLOW) && line_w > box->s.layout.w)
{
/* We are in 'no-overflow' mode, and the line doesn't fit.
* This means our first box is too wide to ever fit in a box of this width. */
assert(restart->start == NULL);
restart->end = box;
restart->end_flow = start_flow;
restart->reason = FZ_HTML_RESTART_REASON_LINE_WIDTH;
return;
}
else
{
node = node->next;
}
}
if (line)
{
int line_align = (align == TA_JUSTIFY) ? justify_align : align;
flush_line(ctx, box, ld, line_w, line_align, indent, line, NULL, restart);
}
ld->used[T] = box->s.layout.y;
ld->used[R] = box->s.layout.x + box->s.layout.w;
ld->used[B] = box->s.layout.b;
ld->used[L] = box->s.layout.x;
}
static float advance_for_spacing(fz_context *ctx, layout_data *ld, float start_b, float spacing, int *eop)
{
float page_h = ld->page[B] - ld->page[T];
float page_top = ld->page[T];
float avail = page_h - fmodf(start_b - page_top, page_h);
if (spacing > avail)
{
*eop = 1;
spacing = avail;
}
return start_b + spacing;
}
static int layout_block_page_break(fz_context *ctx, layout_data *ld, float *yp, int page_break)
{
fz_html_restarter *restart = ld->restart;
float page_h = ld->page[B] - ld->page[T];
float page_top = ld->page[T];
float avail;
int side; /* 0 = right, 1 = left */
/* If we are skipping, nothing to do! */
if (WE_ARE_SKIPPING(restart))
return 0;
/* If the page height is screwy (or the rectangle height), then bale. */
if (page_h <= 0)
return 0;
/* If we aren't doing some kind of page break, bale. */
if (page_break != PB_ALWAYS && page_break != PB_LEFT && page_break != PB_RIGHT)
return 0;
/* How much space left on the page? */
avail = page_h - fmodf(*yp - page_top, page_h);
/* Which side of the page are we on? In restarting (i.e. story) contexts,
* this is passed in explicitly. In traditional contexts, we calculate it
* according to which page we're on. */
if (restart != NULL)
side = restart->left_page;
else
side = 1 & (int)((*yp - page_top) / page_h);
/* If we haven't used ANY of the page, then we might not need to do anything. */
if (avail == page_h)
{
if (page_break == PB_ALWAYS)
return 0;
if (page_break == PB_LEFT && side == 1)
return 0;
if (page_break == PB_RIGHT && side == 0)
return 0;
}
/* Move us to the next page. */
if (avail > 0)
*yp += avail, side ^= 1;
/* Do we need to move further? */
if (page_break == PB_LEFT)
{
/* If we're restarting, make sure we only restart on a left page. */
if (SHOULD_STOP_FOR_RESTART(restart))
restart->end_flags = FZ_HTML_RESTARTER_START_END_FLAGS_SPECIFIC_SIDE | FZ_HTML_RESTARTER_START_END_FLAGS_LEFT_SIDE;
if (side == 0) /* right pages, side == 0 */
*yp += page_h;
}
if (page_break == PB_RIGHT)
{
/* If we're restarting, make sure we only restart on a right page. */
if (SHOULD_STOP_FOR_RESTART(restart))
restart->end_flags = FZ_HTML_RESTARTER_START_END_FLAGS_SPECIFIC_SIDE;
if (side == 1) /* left pages, side == 1 */
*yp += page_h;
}
return 1;
}
/* === POSITION LOGIC === */
/* We support the following 4 types of "position:" style.
* POS_STATIC: Nothing to do.
* POS_RELATIVE: Offset everything inside by a constant amount.
* POS_FIXED: Rewrite the block according to the top level block.
* POS_ABSOLUTE: Positioned relative to the nearest positioned ancestor.
*/
static void
shift_box_contents(fz_html_box *box, float x, float y)
{
fz_html_box *child;
box->s.layout.x += x;
box->s.layout.y += y;
box->s.layout.b += y;
if (box->type == BOX_FLOW)
{
fz_html_flow *flow;
for (flow = box->u.flow.head; flow != NULL; flow = flow->next)
flow->x += x, flow->y += y;
}
for (child = box->down; child; child = child->next)
{
if (child->style->position == POS_FIXED || child->style->position == POS_ABSOLUTE)
return;
shift_box_contents(child, x, y);
}
}
typedef struct
{
float ancestor[4];
float bounds[4];
float inset[4];
int has_width;
float width;
int has_height;
float height;
} position_data;
/* pre_position: called when we are about to layout a block.
* This expects that all the ld trbls are valid. It stores as
* many as need to be stored in position_data, recalculates
* bounds and fill to allow for margins/borders/padding,
* and sets the initial box position appropriately.
*
* For positioned blocks, it calculates inset, and rewrites
* ld->bounds and ld->fill as appropriate.
*
* If this returns 1, then we are moving onto a new page.
*/
static int
pre_position(fz_context *ctx, position_data *pd, layout_data *ld, fz_html_box *box, int ignore_width)
{
float em = box->s.layout.em;
const float *margin = box->u.block.margin;
const float *border = box->u.block.border;
const float *padding = box->u.block.padding;
float auto_w, auto_h;
fz_html_restarter *restart = ld->restart;
int eop = 0;
/* On entry, ld->bounds is the area we expect to fill this block into.
* ld->fill is the area of this that has been used to date.
*/
/* Store the bounds. */
TRBLCPY(pd->bounds, ld->bounds);
/* We want to adjust bounds before we lay the children out so that
* any margin/borders/padding are all outside them. */
if (WE_ARE_SKIPPING(restart))
{
/* We're still skipping, so any child should inherit 0 vertical margin from us. */
}
else
{
/* We're not skipping, so add in the spacings to the top edge of our box. */
ld->bounds[T] = advance_for_spacing(ctx, ld, ld->bounds[T], margin[T] + border[T] + padding[T], &eop);
if (eop && SHOULD_STOP_FOR_RESTART(restart))
return 1;
}
/* Now calculate the bounds for any sub blocks. */
pd->has_width = fz_css_number_defined_not_auto(box->style->width);
pd->width = fz_from_css_number(box->style->width, em, ld->bounds[R] - ld->bounds[L], 0);
pd->has_height = fz_css_number_defined_not_auto(box->style->height);
pd->height = fz_from_css_number(box->style->height, em, ld->bounds[B] - ld->bounds[T], 0);
/* Adjust horizontally for Width and spacings */
auto_w = ld->bounds[R] - ld->bounds[L] - (margin[L] + margin[R] + border[L] + border[R] + padding[L] + padding[R]);
ld->bounds[L] = ld->bounds[L] + margin[L] + border[L] + padding[L];
if (ignore_width)
ld->bounds[R] = ld->bounds[L] + auto_w;
else
ld->bounds[R] = ld->bounds[L] + fz_from_css_number(box->style->width, em, auto_w, auto_w);
/* Adjust vertically for Height and spacings (noting that we have already done the top ones above!) */
auto_h = ld->bounds[B] - ld->bounds[T] - (margin[B] + border[B] + padding[B]);
ld->bounds[B] = ld->bounds[T] + fz_from_css_number(box->style->height, em, auto_h, auto_h);
/* So bounds and fixed are calculated as they should be for positioned: static */
ld->used[T] = ld->bounds[T];
ld->used[R] = ld->bounds[L];
ld->used[B] = ld->bounds[T];
ld->used[L] = ld->bounds[L];
/* So we know the position of the box in the positioned: static case. */
box->s.layout.y = ld->bounds[T];
box->s.layout.x = ld->bounds[L];
box->s.layout.b = ld->bounds[T];
box->s.layout.w = ld->bounds[R]-ld->bounds[L];
if (!WE_ARE_SKIPPING(restart))
{
if (box->style->position == POS_STATIC || box->style->position == POS_RELATIVE)
{
if (pd->has_width && box->s.layout.w < pd->width)
box->s.layout.w = pd->width;
if (pd->has_height && box->s.layout.b - box->s.layout.y < pd->height)
box->s.layout.b = pd->height + box->s.layout.y;
}
}
/* So in the common case, we're done! */
if (box->style->position == POS_STATIC)
return eop;
/* For non-static positionings, this becomes our ancestor */
TRBLCPY(pd->ancestor, ld->ancestor);
TRBLCPY(ld->ancestor, ld->bounds);
/* Calculate insets. */
pd->inset[T] = fz_from_css_number(box->style->inset[T], em, em, 0);
pd->inset[R] = fz_from_css_number(box->style->inset[R], em, em, 0);
pd->inset[B] = fz_from_css_number(box->style->inset[B], em, em, 0);
pd->inset[L] = fz_from_css_number(box->style->inset[L], em, em, 0);
switch (box->style->position)
{
case POS_RELATIVE:
/* Nothing to do here. We'll adjust it later. */
break;
case POS_FIXED:
{
float page[4];
page[T] = ld->page[T] + margin[T] + border[T] + padding[T];
page[R] = ld->page[R] - margin[R] - border[R] - padding[R];
page[B] = ld->page[B] - margin[B] - border[B] - padding[B];
page[L] = ld->page[L] + margin[L] + border[L] + padding[L];
if (fz_css_number_defined(box->style->inset[L]))
{
ld->bounds[L] = page[L] + pd->inset[L];
if (pd->has_width)
{
/* Left and Width determine the box. */
ld->bounds[R] = ld->bounds[L] + pd->width;
}
else
{
/* Left and bounds right determine the box. */
ld->bounds[R] = page[R] - pd->inset[L];
}
}
else
{
if (pd->has_width)
{
/* Right and Width determine the box. */
ld->bounds[R] = page[R] - pd->inset[R];
ld->bounds[L] = ld->bounds[R] - pd->width;
}
else
{
/* Right and page define the box. */
ld->bounds[L] = page[L];
ld->bounds[R] = page[R] - pd->inset[R];
}
}
if (fz_css_number_defined(box->style->inset[T]))
{
ld->bounds[T] = page[T] + pd->inset[T];
if (pd->has_height)
{
/* Top and Height determine the box. */
ld->bounds[B] = ld->bounds[T] + pd->height;
}
else
{
/* Top and Bottom determine the box. */
ld->bounds[B] = page[B] - pd->inset[T];
}
}
else
{
if (pd->has_height)
{
/* Bottom and Height determine the box. */
ld->bounds[B] = page[B] - pd->inset[B];
ld->bounds[T] = ld->bounds[B] - pd->height;
}
else
{
/* Bottom and page define the box. */
ld->bounds[T] = page[T];
ld->bounds[B] = page[B] - pd->inset[B];
}
}
break;
}
case POS_ABSOLUTE:
{
if (fz_css_number_defined(box->style->inset[L]))
{
ld->bounds[L] = pd->ancestor[L] + pd->inset[L];
if (pd->has_width)
{
/* Left and Width determine the box. */
ld->bounds[R] = ld->bounds[L] + pd->width;
}
else
{
/* Left and right determine the box. */
ld->bounds[R] = pd->ancestor[R] - pd->inset[L];
}
}
else
{
if (pd->has_width)
{
/* Right and Width determine the box. */
ld->bounds[R] = pd->ancestor[R] - pd->inset[R];
ld->bounds[L] = ld->bounds[R] - pd->width;
}
else
{
/* Right and ancestor define the box. */
ld->bounds[L] = pd->ancestor[L];
ld->bounds[R] = pd->ancestor[R] - pd->inset[R];
}
}
if (fz_css_number_defined(box->style->inset[T]))
{
ld->bounds[T] = pd->ancestor[T] + pd->inset[T];
if (pd->has_height)
{
/* Top and Height determine the box. */
ld->bounds[B] = ld->bounds[T] + pd->height;
}
else
{
/* Top and Bottom determine the box. */
ld->bounds[B] = pd->ancestor[B] - pd->inset[T];
}
}
else
{
if (pd->has_height)
{
/* Bottom and Height determine the box. */
ld->bounds[B] = pd->ancestor[B] - pd->inset[B];
ld->bounds[T] = ld->bounds[B] - pd->height;
}
else
{
/* Bottom and ancestor define the box. */
ld->bounds[T] = pd->ancestor[T];
ld->bounds[B] = pd->ancestor[B] - pd->inset[B];
}
}
break;
}
}
box->s.layout.x = ld->bounds[L];
box->s.layout.w = 0;
box->s.layout.y = ld->bounds[T];
box->s.layout.b = ld->bounds[T];
ld->used[B] = ld->bounds[T];
ld->used[L] = ld->bounds[L]; /* Deliberate! */
return eop;
}
/* post_position: called after we have laid out a block.
* Restore anything that was stashed.
* Rewrites ld->used as appropriate.
*
* Returns eop
*/
static int
post_position(fz_context *ctx, position_data *pd, layout_data *ld, fz_html_box *box, int widths_set)
{
int eop = 0; /* dummy */
fz_html_restarter *restart = ld->restart;
float em = box->s.layout.em;
const float *margin = box->u.block.margin;
const float *border = box->u.block.border;
const float *padding = box->u.block.padding;
/* Restore the bounds */
TRBLCPY(ld->bounds, pd->bounds);
if (ld->used[B] > box->s.layout.b)
box->s.layout.b = ld->used[B];
/* Unless we're still skipping, add the padding/border/margin on the bottom. */
if (!WE_ARE_SKIPPING(restart))
{
if (box->style->position == POS_STATIC || box->style->position == POS_RELATIVE)
{
if (pd->has_width && box->s.layout.w < pd->width && !widths_set)
box->s.layout.w = pd->width;
if (pd->has_height && box->s.layout.b - box->s.layout.y < pd->height)
box->s.layout.b = pd->height + box->s.layout.y;
}
ld->used[B] = advance_for_spacing(ctx, ld,
ld->used[B],
box->u.block.padding[B] + box->u.block.border[B] + box->u.block.margin[B],
&eop);
}
/* In the common case, used is just whatever we laid out to.*/
ld->used[T] = box->s.layout.y;
ld->used[R] = box->s.layout.x + box->s.layout.w + padding[R] + border[R] + margin[R];
ld->used[L] = box->s.layout.x;
if (ld->used[B] < box->s.layout.b)
ld->used[B] = box->s.layout.b;
/* So in the common case, we're done! */
if (box->style->position == POS_STATIC)
return eop;
/* Retore the stashed ancestor */
TRBLCPY(ld->ancestor, pd->ancestor);
switch (box->style->position)
{
case POS_RELATIVE:
{
float x = fz_css_number_defined(box->style->inset[L]) ? pd->inset[L] : -pd->inset[R];
float y = fz_css_number_defined(box->style->inset[T]) ? pd->inset[T] : -pd->inset[B];
/* Space is taken out of the flow as if this stuff had been laid out without
* relativeness. So we just need to move the content. */
shift_box_contents(box, x, y);
break;
}
case POS_FIXED:
{
/* Some adjustments are required:
* At this point, width will be correct, height will not.
* Contents will be positioned as if from top/left.
*/
float page[4];
page[T] = ld->page[T] + margin[T] + border[T] + padding[T];
page[R] = ld->page[R] - margin[R] - border[R] - padding[R];
page[B] = ld->page[B] - margin[B] - border[B] - padding[B];
page[L] = ld->page[L] + margin[L] + border[L] + padding[L];
if (fz_css_number_defined(box->style->inset[L]))
{
if (pd->has_width)
{
/* Already fine */
box->s.layout.w = pd->width;
}
else
{
/* Already fine */
(void) box;
}
}
else
{
if (pd->has_width)
{
/* Right and Width determine the box. */
/* Already fine */
box->s.layout.w = pd->width;
}
else
{
/* Right and page define the box. */
/* We laid this out on the left, cos we didn't know the width. Now we
* know the width, shift it right. */
shift_box_contents(box, page[R] - page[L] - box->s.layout.w - pd->inset[R], 0);
}
}
if (fz_css_number_defined(box->style->inset[T]))
{
if (fz_css_number_defined_not_auto(box->style->height))
{
float h = fz_from_css_number(box->style->height, em, em, 0);
/* Top and Height determine the box. */
box->s.layout.b = box->s.layout.y + h;
}
else
{
/* Top and page bottom determine the box. */
/* Leave the height as is. */
(void) box;
}
}
else
{
if (pd->has_height)
{
/* Bottom and Height determine the box. */
box->s.layout.b = box->s.layout.y + pd->height;
}
else
{
/* Bottom and page define the box. */
shift_box_contents(box, 0, page[B] - box->s.layout.b - pd->inset[B]);
}
}
/* As far as the caller is concerned, no space used. */
ld->used[T] = ld->used[B];
ld->used[L] = ld->used[R];
break;
}
case POS_ABSOLUTE:
{
/* Some adjustments are required:
* At this point, width will be correct, height will not.
* Contents will be positioned as if from top/left.
*/
if (fz_css_number_defined(box->style->inset[L]))
{
if (pd->has_width)
{
/* Already fine */
box->s.layout.w = pd->width;
}
else
{
/* Already fine */
(void) box;
}
}
else
{
if (pd->has_width)
{
/* Right and Width determine the box. */
/* Already fine */
box->s.layout.w = pd->width;
}
else
{
/* Right and ancestor define the box. */
/* We laid this out on the left, cos we didn't know the width. Now we
* know the width, shift it right. */
shift_box_contents(box, ld->ancestor[R] - ld->ancestor[L] - box->s.layout.w - pd->inset[R], 0);
}
}
if (fz_css_number_defined(box->style->inset[T]))
{
if (pd->has_height)
{
/* Top and Height determine the box. */
box->s.layout.b = box->s.layout.y + pd->height;
}
else
{
/* Top and ancestor bottom determine the box. */
/* Leave the height as is. */
(void) box;
}
}
else
{
if (pd->has_height)
{
/* Bottom and Height determine the box. */
box->s.layout.b = box->s.layout.y + pd->height;
}
else
{
/* Bottom and ancestor bottom define the box. */
shift_box_contents(box, 0, ld->ancestor[B] - box->s.layout.b - pd->inset[B]);
}
}
/* As far as the caller is concerned, no space used. */
ld->used[T] = ld->used[B];
ld->used[L] = ld->used[R];
break;
}
}
return eop;
}
static int layout_block(fz_context *ctx, layout_data *ld, fz_html_box *box);
static int layout_table(fz_context *ctx, layout_data *ld, fz_html_box *box);
/* === LAYOUT TABLE === */
/* The column widths here should include any border widths and padding.
* This is consistent with how the 'width' figures are understood in HTML.
*
* For the border-collapse cases, this is modified slightly.
* Each column only contains half of each border as they are shared with
* their neighbours. The exception to this is for the edges of the table,
* when they contain the full widths.
*/
typedef struct {
int fixed;
float min, max, actual;
} column_width;
typedef struct
{
/* If set, this bit means this cell is either spanned, or omitted from the definition. */
unsigned int spanned : 1;
/* If set, this bit means that the cell is defined with a "fixed" width. */
unsigned int fixed : 1;
float minw;
float maxw;
uint16_t rowspan;
uint16_t colspan;
fz_html_box *box;
} table_cell;
typedef struct
{
/* How large the grid is allocated as */
int max_w;
int max_h;
/* How large the grid is populated */
int w;
int h;
/* Where are we in filling the grid */
int x;
int y;
float spacing;
table_cell *cells;
float *row_b;
float *row_max_bottom_border;
} table_grid;
static table_grid *
new_table_grid(fz_context *ctx, float spacing)
{
table_grid *grid = fz_malloc_struct(ctx, table_grid);
grid->spacing = spacing;
return grid;
}
static void drop_table_grid(fz_context *ctx, table_grid *grid)
{
if (grid)
{
fz_free(ctx, grid->cells);
fz_free(ctx, grid->row_b);
fz_free(ctx, grid->row_max_bottom_border);
fz_free(ctx, grid);
}
}
static void resize_grid(fz_context *ctx, table_grid *grid, int w, int h)
{
int y;
assert(w >= grid->max_w && h >= grid->max_h);
grid->cells = fz_realloc(ctx, grid->cells, sizeof(grid->cells[0]) * w * h);
if (grid->max_w != w)
{
for (y = grid->max_h-1; y >= 0; y--)
{
memmove(&grid->cells[y * w], &grid->cells[y * grid->max_w], sizeof(grid->cells[0]) * grid->max_w);
memset(&grid->cells[y * w + grid->max_w], 0, sizeof(grid->cells[0]) * (w - grid->max_w));
}
}
if (grid->max_h != h)
memset(&grid->cells[w * grid->max_h], 0, sizeof(grid->cells[0]) * w * (h - grid->max_h));
grid->max_w = w;
grid->max_h = h;
}
static table_cell *cell_at(fz_context *ctx, table_grid *grid, int x, int y)
{
if (x >= grid->max_w || y >= grid->max_h)
{
int new_w = grid->max_w;
int new_h = grid->max_h;
if (new_w == 0)
new_w = 8;
if (new_h == 0)
new_h = 8;
while (x >= new_w)
new_w *= 2;
while (y >= new_h)
new_h *= 2;
resize_grid(ctx, grid, new_w, new_h);
}
if (y >= grid->h)
grid->h = y+1;
if (x >= grid->w)
{
/* If we're widening the active area, make sure the unused cells above
* are marked as spanning. */
int x0, y0;
for (y0 = 0; y0 < grid->y-1; y0++)
for (x0 = grid->w; x0 <= x; x0++)
grid->cells[grid->max_w * y0 + x0].spanned = 1;
grid->w = x+1;
}
return &grid->cells[grid->max_w * y + x];
}
static table_cell *next_table_cell(fz_context *ctx, table_grid *grid, fz_html_box *box, int colspan, int rowspan)
{
table_cell *cell;
int x, y;
while (1)
{
cell = cell_at(ctx, grid, grid->x, grid->y);
if (!cell->spanned)
break;
grid->x++;
}
if (colspan < 1)
colspan = 1;
if (rowspan < 1)
rowspan = 1;
/* Mark all as being spanned. */
for (y = 0; y < rowspan; y++)
for (x = 0; x < colspan; x++)
{
table_cell *cell2 = cell_at(ctx, grid, grid->x + x, grid->y + y);
cell2->spanned = 1;
}
/* Reload the original cell (as it may have moved due to resizing) */
cell = cell_at(ctx, grid, grid->x, grid->y);
cell->spanned = 0;
cell->colspan = colspan;
cell->rowspan = rowspan;
cell->box = box;
grid->x += colspan;
return cell;
}
static void next_table_row(fz_context *ctx, table_grid *grid)
{
while (grid->x < grid->w)
{
table_cell *cell = cell_at(ctx, grid, grid->x, grid->y);
cell->spanned = 1;
grid->x++;
}
grid->x = 0;
grid->y++;
}
static column_width *
table_grid_complete(fz_context *ctx, table_grid *grid, float top)
{
int w = grid->w;
int h = grid->h;
int x, y, x2;
column_width *colw = Memento_label(fz_calloc(ctx, grid->w, sizeof(column_width)), "column_width");
/* Abuse fixed to serve double duty as meaning "no clue" */
for (x = 0; x < w; x++)
colw[x].fixed = -1;
/* Look for widths, without colspan confusing them. */
for (x = 0; x < w; x++)
{
float minw = 0, maxw = 0;
int fixed = 0;
int found = 0;
for (y = 0; y < h; y++)
{
/* Can never throw, as x/y smaller than grid->w/h */
table_cell *cell = cell_at(ctx, grid, x, y);
if (cell->spanned || cell->colspan > 1)
continue;
if (cell->fixed)
{
fixed = 1;
minw = cell->minw;
maxw = cell->maxw;
found = 1;
}
else if (fixed)
{
/* We should ignore a fixed size if another cell's minimum width is larger. */
if (cell->minw > minw)
{
minw = cell->minw;
if (minw > maxw)
maxw = minw;
}
}
else
{
/* Logic looks wrong, but isn't! */
if (!found || cell->minw > minw)
minw = cell->minw;
if (cell->maxw > maxw)
maxw = cell->maxw;
found = 1;
}
}
if (found)
{
colw[x].min = minw;
colw[x].max = maxw;
colw[x].fixed = fixed;
}
}
/* Now we look at colspanning cells. */
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
{
float minw;
int unknown, nonfixed;
table_cell *cell = cell_at(ctx, grid, x, y);
if (cell->spanned || cell->colspan == 1)
continue;
/* So, we have a spanning cell starting at (x, y) */
minw = 0;
unknown = 0;
nonfixed = 0;
for (x2 = x; x2 < x + cell->colspan; x2++)
{
if (colw[x2].fixed == -1)
{
unknown++;
continue;
}
if (!colw[x2].fixed)
nonfixed++;
minw += colw[x2].min;
}
if (minw < cell->minw)
{
/* We need to boost the minimum widths. */
float boost = (cell->minw - minw);
if (unknown > 0)
{
/* Divide the required space between 'unknown' columns
* that currently have no width. */
boost /= unknown;
for (x2 = x; x2 < x + cell->colspan; x2++)
{
if (colw[x2].fixed == -1)
{
colw[x2].fixed = cell->fixed;
colw[x2].min = boost;
colw[x2].max = colw[x2].min;
}
}
}
else if (nonfixed > 0)
{
/* Divide the required space between 'nonfixed' columns
* that don't have fixed widths. */
boost /= nonfixed;
for (x2 = x; x2 < x + cell->colspan; x2++)
{
if (colw[x2].fixed)
continue;
colw[x2].fixed = cell->fixed;
colw[x2].min += boost;
if (colw[x2].max < colw[x2].min)
colw[x2].max = colw[x2].min;
}
}
else
{
/* We need more space, but everything is fixed! There seems to be
* a contradiction here. Just live with it. */
}
}
}
}
/* Everything should now be set.
* But it might not be.
* <table><tbody><tr><th colspan="3">A</th></tr>
* <tr><td>B</td><td>C</td>
* </tbody></table>
* will give us a 3 column table, with no clue what the size should be for column 3.
*/
for (x = 0; x < w; x++)
{
if (colw[x].fixed == -1)
colw[x].fixed = 0;
}
fz_try(ctx)
{
grid->row_b = fz_malloc(ctx, grid->h * sizeof(float));
grid->row_max_bottom_border = fz_calloc(ctx, grid->h, sizeof(float));
}
fz_catch(ctx)
{
fz_free(ctx, colw);
fz_rethrow(ctx);
}
/* Don't add any spacing in here, because we may be skipping!
* We just want a good enough value to use for 'min'. */
for (y = 0; y < h; y++)
grid->row_b[y] = top;
return colw;
}
static float table_cell_padding(fz_context *ctx, fz_html_box *box)
{
return (
box->u.block.padding[L] + box->u.block.border[L] +
box->u.block.padding[R] + box->u.block.border[R]
);
}
typedef struct
{
float max_left_border;
float max_right_border;
} border_collapse_info;
static float table_cell_padding_collapsed(fz_context *ctx, fz_html_box *box, border_collapse_info *bci, int leftmost, int rightmost)
{
float left = leftmost ? bci->max_left_border : box->u.block.border[L]/2;
float right = rightmost ? bci->max_right_border : box->u.block.border[R]/2;
return (
box->u.block.padding[L] + left +
box->u.block.padding[R] + right
);
}
static float block_padding(fz_context *ctx, fz_html_box *box)
{
return (
box->u.block.padding[L] + box->u.block.border[L] + box->u.block.margin[L] +
box->u.block.padding[R] + box->u.block.border[R] + box->u.block.margin[R]
);
}
static float largest_min_width(fz_context *ctx, fz_html_box *box)
{
/* The "minimum" width is the largest word in a paragraph. */
float r_min = 0;
if (box->type == BOX_BLOCK)
{
fz_html_box *child = box->down;
while (child)
{
float min = largest_min_width(ctx, child);
if (min > r_min)
r_min = min;
child = child->next;
}
r_min += block_padding(ctx, box);
}
else if (box->type == BOX_FLOW)
{
fz_html_flow *flow;
for (flow = box->u.flow.head; flow; flow = flow->next)
if (flow->w > r_min)
r_min = flow->w;
}
else
{
// TODO: nested TABLE
}
return r_min;
}
static float largest_max_width(fz_context *ctx, fz_html_box *box)
{
/* The "maximum" width is the length of the longest paragraph laid out on one line. */
float r_max = 0;
if (box->type == BOX_BLOCK)
{
fz_html_box *child = box->down;
while (child)
{
float max = largest_max_width(ctx, child);
if (max > r_max)
r_max = max;
child = child->next;
}
r_max += block_padding(ctx, box);
}
else if (box->type == BOX_FLOW)
{
fz_html_flow *flow;
float max = 0;
for (flow = box->u.flow.head; flow; flow = flow->next)
{
max += flow->w;
if (flow->type == FLOW_BREAK)
{
if (max > r_max)
r_max = max;
max = 0;
}
}
if (max > r_max)
r_max = max;
}
else
{
// TODO: nested TABLE
}
return r_max;
}
static void squish_block(fz_context *ctx, fz_html_box *box)
{
fz_html_box *child;
box->s.layout.b = box->s.layout.y;
if (box->type == BOX_FLOW)
return;
for (child = box->down; child; child = child->next)
squish_block(ctx, child);
}
static void layout_table_row(fz_context *ctx, layout_data *ld, table_grid *grid, int celly, fz_html_box *row, int ncol, column_width *colw, float spacing, border_collapse_info *border_collapse)
{
fz_html_box *cell, *child;
int col = 0;
float x = row->s.layout.x;
float y;
float saved_bounds[4];
float row_height;
float em = row->s.layout.em;
int fixed_height = 0;
table_cell *tcell;
float max_cell_border_above, max_cell_border_below;
/* Always layout the full row since we can't restart in the middle of a cell.
* If the row doesn't fit fully, we'll postpone it to the next page.
* FIXME: If the row doesn't fit then either, we should split it with the offset.
*/
fz_html_restarter *save_restart = ld->restart;
ld->restart = NULL;
/* Note: margin is ignored for table cells and rows */
TRBLCPY(saved_bounds, ld->bounds);
if (fz_css_number_defined_not_auto(row->style->height))
{
row_height = fz_from_css_number(row->style->height, em, em, 0);
fixed_height = 1;
}
/* Find the maximum cell border in this row. */
max_cell_border_above = 0;
max_cell_border_below = 0;
for (cell = row->down; cell; cell = cell->next)
{
if (max_cell_border_above < cell->u.block.border[T])
max_cell_border_above = cell->u.block.border[T];
if (max_cell_border_below < cell->u.block.border[B])
max_cell_border_below = cell->u.block.border[B];
}
/* For each cell in the row */
for (cell = row->down; cell; cell = cell->next)
{
float cell_pad = table_cell_padding(ctx, cell);
int colspan, rowspan;
float cellw;
int i;
/* Find the cell record in the grid. */
while (1)
{
assert(col < grid->w && celly <= grid->h);
tcell = cell_at(ctx, grid, col, celly);
if (tcell->box == cell)
break;
x += colw[col++].actual + spacing;
}
colspan = tcell->colspan;
assert(colspan >= 1);
rowspan = tcell->rowspan;
assert(rowspan >= 1);
/* Find the actual width of this cell */
cellw = 0;
for (i = 0; i < colspan; i++)
cellw += colw[col+i].actual + (i != 0 ? spacing : 0);
x += spacing;
/* Position the cell */
ld->bounds[T] = row->s.layout.y + max_cell_border_above + cell->u.block.padding[T];
ld->bounds[L] = x + cell->u.block.padding[L] + cell->u.block.border[L];
ld->bounds[R] = ld->bounds[L] + cellw - cell_pad;
ld->bounds[B] = cell->s.layout.y;
if (border_collapse)
{
if (col == 0)
{
/* Adjust the left hand edge so that all the borders line up */
ld->bounds[L] += border_collapse->max_left_border/2;
}
/* We share half the border with our predecessor, so move back a bit. */
ld->bounds[L] -= cell->u.block.border[L]/2;
if (col + colspan == grid->w)
{
/* Adjust the right hand edge so that all the borders line up */
ld->bounds[R] -= border_collapse->max_right_border/2;
}
/* We share half the border with our successor, so move on a bit. */
ld->bounds[R] += cell->u.block.border[R]/2;
/* Reuse T and B margins to allow for the required inner cell alignment spacing */
cell->u.block.margin[T] = (max_cell_border_above - cell->u.block.border[T])/2;
cell->u.block.margin[B] = (max_cell_border_below - cell->u.block.border[B])/2;
}
else
{
/* Reuse T and B margins to allow for the required inner cell alignment spacing */
cell->u.block.margin[T] = max_cell_border_above - cell->u.block.border[T];
cell->u.block.margin[B] = max_cell_border_below - cell->u.block.border[B];
}
cell->s.layout.x = ld->bounds[L];
cell->s.layout.w = ld->bounds[R] - ld->bounds[L];
cell->s.layout.y = ld->bounds[T];
/* Layout cell contents into the cell. */
for (child = cell->down; child; child = child->next)
{
if (child->type == BOX_BLOCK)
{
(void)layout_block(ctx, ld, child);
}
else if (child->type == BOX_TABLE)
{
(void)layout_table(ctx, ld, child);
}
else if (child->type == BOX_FLOW)
{
layout_flow(ctx, ld, child, cell);
}
ld->bounds[T] = ld->used[B];
}
cell->s.layout.b = ld->used[B];
if (fz_css_number_defined_not_auto(cell->style->height))
{
float cellheight = fz_from_css_number(cell->style->height, em, em, 0);
if (cell->s.layout.b < cell->s.layout.y + cellheight)
cell->s.layout.b = cell->s.layout.y + cellheight;
}
else if (fixed_height && cell->s.layout.b < cell->s.layout.y + row_height)
cell->s.layout.b = cell->s.layout.y + row_height;
/* Advance to next column */
x += colw[col].actual;
/* Update row_b (most importantly for this row, but ensure the others
* are plausibly increasing too). */
y = cell->s.layout.b + cell->u.block.padding[B] + cell->u.block.border[B] + cell->u.block.margin[B];
if (border_collapse)
{
/* Half our border is shared with the next cell. */
y -= cell->u.block.border[B]/2;
}
for (i = celly + rowspan - 1; i < grid->h; i++)
{
if (grid->row_b[i] < y)
grid->row_b[i] = y;
y += spacing;
}
if (border_collapse)
{
if (grid->row_max_bottom_border[celly + rowspan - 1] < cell->u.block.border[B])
grid->row_max_bottom_border[celly + rowspan - 1] = cell->u.block.border[B];
}
++col;
}
row->s.layout.b = grid->row_b[celly];
ld->restart = save_restart;
TRBLCPY(ld->bounds, saved_bounds);
}
static inline int
cell_rowspan(fz_html_box *cell)
{
int r = cell->style->rowspan;
if (r < 1)
return 1;
return r;
}
static inline int
cell_colspan(fz_html_box *cell)
{
int c = cell->style->colspan;
if (c < 1)
return 1;
return c;
}
static void
fixup_collapsed_cell_bottoms_for_row(fz_context *ctx, table_grid *grid, int row, fz_html_box *box)
{
int row0;
fz_html_box *rowbox, *cell;
for (row0 = 0, rowbox = box->down; row0 <= row && rowbox; row0++, rowbox = rowbox->next)
{
for (cell = rowbox->down; cell; cell = cell->next)
{
float y;
int rowspan = cell_rowspan(cell);
if (row0 + rowspan - 1 != row)
continue;
y = grid->row_b[row0 + rowspan - 1] - (cell->u.block.padding[B] + cell->u.block.border[B]/2 + cell->u.block.margin[B]);
if (cell->style->vertical_align == VA_MIDDLE || cell->style->vertical_align == VA_BOTTOM)
{
float offset = (y - cell->s.layout.b);
if (cell->style->vertical_align == VA_MIDDLE)
offset /= 2;
shift_box_contents(cell, 0, offset);
cell->s.layout.y -= offset;
}
cell->s.layout.b = y;
}
}
}
static void
fixup_cell_bottoms(fz_context *ctx, table_grid *grid, fz_html_box *box)
{
fz_html_box *rowbox, *cell;
int row;
/* Second pass, set the bottoms of all cells in the tables. */
for (row = 0, rowbox = box; row <= grid->h && rowbox; row++, rowbox = rowbox->next)
{
for (cell = rowbox->down; cell; cell = cell->next)
{
float y;
int rowspan = cell_rowspan(cell);
y = grid->row_b[row + rowspan - 1] - (cell->u.block.padding[B] + cell->u.block.border[B] + cell->u.block.margin[B]);
if (cell->style->vertical_align == VA_MIDDLE || cell->style->vertical_align == VA_BOTTOM)
{
float offset = (y - cell->s.layout.b);
if (cell->style->vertical_align == VA_MIDDLE)
offset /= 2;
shift_box_contents(cell, 0, offset);
cell->s.layout.y -= offset;
}
cell->s.layout.b = y;
}
}
}
static void collapse_table_borders(fz_context *ctx, table_grid *grid, border_collapse_info *bci)
{
int w = grid->w;
int h = grid->h;
int x, y, x1, y1;
/* Collapse columns */
bci->max_right_border = 0;
for (x = 0; x < w-1; x++)
{
for (y = 0; y < h; y++)
{
table_cell *cell0 = cell_at(ctx, grid, x, y);
table_cell *cell1;
float b0, b1;
if (cell0->spanned == 1 || cell0->box == NULL)
continue;
cell0->box->collapsed_cell = 1;
x1 = x + cell0->colspan;
if (x1 == w)
{
if (bci->max_right_border < cell0->box->u.block.border[R])
bci->max_right_border = cell0->box->u.block.border[R];
continue;
}
cell1 = cell_at(ctx, grid, x1, y);
if (cell1->box == NULL)
continue;
b0 = cell0->box->u.block.border[R];
b1 = cell1->box->u.block.border[L];
if (b0 < b1 && b1 >= 0)
{
cell0->box->u.block.border[R] = b1;
cell0->box->suppress_border |= 1<<R;
}
else
{
cell1->box->u.block.border[L] = b0;
cell1->box->suppress_border |= 1<<L;
}
}
}
for (y = 0; y < h; y++)
{
table_cell *cell0 = cell_at(ctx, grid, w-1, y);
if (cell0->spanned == 1 || cell0->box == NULL)
continue;
x1 = x + cell0->colspan;
if (x1 == w)
{
if (bci->max_right_border < cell0->box->u.block.border[R])
bci->max_right_border = cell0->box->u.block.border[R];
continue;
}
}
bci->max_left_border = 0;
for (y = 0; y < h; y++)
{
table_cell *cell0 = cell_at(ctx, grid, 0, y);
if (cell0->spanned == 1 || cell0->box == NULL)
continue;
if (bci->max_left_border < cell0->box->u.block.border[L])
bci->max_left_border = cell0->box->u.block.border[L];
}
/* Collapse rows */
for (y = 0; y < h-1; y++)
{
for (x = 0; x < w; x++)
{
table_cell *cell0 = cell_at(ctx, grid, x, y);
table_cell *cell1;
float b0, b1;
if (cell0->spanned == 1 || cell0->box == NULL)
continue;
y1 = y + cell0->rowspan;
if (y1 >= h)
continue;
cell1 = cell_at(ctx, grid, x, y1);
if (cell1->box == NULL)
continue;
b0 = cell0->box->u.block.border[B];
b1 = cell1->box->u.block.border[T];
if (b0 < b1 && b1 >= 0)
{
cell0->box->u.block.border[B] = b1;
cell0->box->suppress_border |= 1<<B;
}
else
{
cell1->box->u.block.border[T] = b0;
cell1->box->suppress_border |= 1<<T;
}
}
}
}
static int layout_table(fz_context *ctx, layout_data *ld, fz_html_box *box)
{
fz_html_box *row;
int col, ncol;
float min_tabw, max_tabw;
column_width *colw = NULL;
fz_html_restarter *restart = ld->restart;
float spacing;
int eop = 0;
float avail_w;
table_grid *table;
int x, y;
int table_has_width;
int border_collapse = box->style->border_collapse;
border_collapse_info bci;
position_data position;
spacing = fz_from_css_number(box->style->border_spacing, box->s.layout.em, box->s.layout.w, 0);
if (border_collapse)
spacing = 0;
if (restart)
{
/* We have reached the restart point */
if (restart->start == box)
restart->start = NULL;
}
/* We honour positions on tables. We don't honour positions on elements of tables (like rows/cells etc). */
eop = pre_position(ctx, &position, ld, box, 1);
if (eop)
{
if (restart && restart->end == NULL)
{
box->s.layout.b = box->s.layout.y;
if (restart->potential)
restart->end = restart->potential;
else
restart->end = box;
return 0;
}
}
/* Add initial border-spacing */
ld->used[B] += spacing;
box->s.layout.b += spacing;
table = new_table_grid(ctx, spacing);
fz_var(colw);
fz_try(ctx)
{
/* Table Autolayout algorithm from HTML */
/* https://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.5.2 */
/* Make the grid. */
for (row = box->down; row; row = row->next)
{
fz_html_box *cell;
for (cell = row->down; cell; cell = cell->next)
next_table_cell(ctx, table, cell, cell->style->colspan, cell->style->rowspan);
next_table_row(ctx, table);
}
/* Collapse the cell borders if required. */
if (border_collapse)
collapse_table_borders(ctx, table, &bci);
/* Now walk the cells, populating minw/maxw/fixed. */
for (y = 0; y < table->h; y++)
{
for (x = 0; x < table->w; x++)
{
fz_html_box *cell, *child;
float cell_pad;
table_cell *tc = cell_at(ctx, table, x, y);
if (tc->spanned)
continue;
cell = tc->box;
if (cell == NULL)
continue;
if (border_collapse)
cell_pad = table_cell_padding_collapsed(ctx, cell, &bci, x == 0, x + tc->colspan == table->w);
else
cell_pad = table_cell_padding(ctx, cell);
if (fz_css_number_defined_not_auto(cell->style->width))
{
float em = box->s.layout.em;
float w = fz_from_css_number(cell->style->width, em, em, 0) + cell_pad;
tc->fixed = 1;
tc->minw = w;
tc->maxw = w;
}
else
{
float cellminw = 0;
float cellmaxw = 0;
for (child = cell->down; child; child = child->next)
{
float min = largest_min_width(ctx, child) + cell_pad;
float max = largest_max_width(ctx, child) + cell_pad;
/* The logic here looks screwy, but isn't! */
if (min > cellminw)
cellminw = min;
if (max > cellmaxw)
cellmaxw = max;
}
tc->fixed = 0;
tc->minw = cellminw;
tc->maxw = cellmaxw;
}
}
}
/* Now we can calculate the column widths. */
colw = table_grid_complete(ctx, table, box->s.layout.b);
ncol = table->w;
min_tabw = max_tabw = 0;
for (col = 0; col < ncol; ++col)
{
min_tabw += colw[col].min;
max_tabw += colw[col].max;
}
min_tabw += spacing * (ncol + 1);
max_tabw += spacing * (ncol + 1);
avail_w = fz_from_css_number(box->style->width, box->s.layout.em, ld->bounds[R] - ld->bounds[L], ld->bounds[R] - ld->bounds[L]);
table_has_width = fz_css_number_defined_not_auto(box->style->width);
if (min_tabw >= avail_w)
{
/* The minimum table width is equal to or wider than the available space.
* Assign the minimum widths and let the lines overflow...
*/
box->s.layout.w = min_tabw;
for (col = 0; col < ncol; ++col)
colw[col].actual = colw[col].min;
}
else if (max_tabw <= avail_w && !table_has_width)
{
/* The maximum table width fits within the available space. */
/* Set the columns to their maximum widths. */
box->s.layout.w = max_tabw;
for (col = 0; col < ncol; ++col)
colw[col].actual = colw[col].max;
}
else
{
/* The maximum width of the table is greater than the available space, but
* the minimum table width is smaller. In this case, find the difference
* between the available space and the minimum table width, lets call it
* W. Lets also call D the difference between maximum and minimum width of
* the table.
*
* For each column, let d be the difference between maximum and minimum
* width of that column. Now set the column's width to the minimum width
* plus d times W over D. This makes columns with large differences
* between minimum and maximum widths wider than columns with smaller
* differences.
*/
float W = (avail_w - min_tabw);
float D = (max_tabw - min_tabw);
box->s.layout.w = avail_w;
if (D == 0)
{
for (col = 0; col < ncol; ++col)
colw[col].actual = colw[col].min + W / ncol;
}
else
{
for (col = 0; col < ncol; ++col)
colw[col].actual = colw[col].min + (colw[col].max - colw[col].min) * W / D;
}
}
/* Layout each row in turn. */
for (row = box->down, y = 0; row && y < table->h; row = row->next, y++)
{
/* Position the row, zero height for now. */
if (box->s.layout.b < table->row_b[y])
box->s.layout.b = table->row_b[y];
row->s.layout.x = box->s.layout.x;
row->s.layout.w = avail_w;
row->s.layout.y = row->s.layout.b = box->s.layout.b;
if (restart && restart->start != NULL)
{
if (restart->start == row)
restart->start = NULL;
else
{
squish_block(ctx, row);
continue; /* still skipping */
}
}
if (border_collapse && y > 0)
{
/* When border collapsing, every row's borders overlap the ones from
* the one above. */
row->s.layout.y -= table->row_max_bottom_border[y-1]/2;
}
layout_table_row(ctx, ld, table, y, row, ncol, colw, spacing, border_collapse ? &bci : NULL);
if (border_collapse)
fixup_collapsed_cell_bottoms_for_row(ctx, table, y, box);
/* If the row doesn't fit on the current page, break here and put the row on the next page.
* Unless the row was at the very start of the page, in which case it'll overflow instead.
* FIXME: Don't overflow, draw twice with offset to break it abruptly at the page border!
*/
if (ld->page[B] != ld->page[T])
{
float page_h = ld->page[B] - ld->page[T];
float avail = page_h - fmodf(row->s.layout.y - ld->page[T], page_h);
float used = row->s.layout.b - row->s.layout.y;
if (used > avail && avail < page_h)
{
if (restart)
{
restart->end = row;
goto exit;
}
else
{
row->s.layout.y += avail;
layout_table_row(ctx, ld, table, y, row, ncol, colw, spacing, border_collapse ? &bci : NULL);
}
}
}
box->s.layout.b = row->s.layout.b + spacing;
ld->used[B] = box->s.layout.b;
}
/* And align the cell bottoms. */
if (!border_collapse)
fixup_cell_bottoms(ctx, table, box->down);
exit:;
}
fz_always(ctx)
{
drop_table_grid(ctx, table);
fz_free(ctx, colw);
}
fz_catch(ctx)
fz_rethrow(ctx);
if (post_position(ctx, &position, ld, box, 1))
{
if (SHOULD_STOP_FOR_RESTART(restart))
{
if (restart->potential)
restart->end = restart->potential;
else
{
/* NEVER stop to restart on this object, or we will get into
* an infinite loop. We'd rather just risk losing the padding/margin/
* border at the bottom. Return 1, so we restart on the next box. */
/* DO NOT DO: restart->end = box; */
return 1;
}
return 0;
}
}
if (layout_block_page_break(ctx, ld, &ld->used[B], box->style->page_break_after))
{
if (SHOULD_STOP_FOR_RESTART(restart))
return 1;
}
return 0;
}
/* === LAYOUT BLOCKS === */
/*
Layout a BOX_BLOCK.
ctx: The ctx in use.
ld: The layout data.
We should fill into ld->bounds, and update ld->fill to be what we used before we return.
A lot of the time, ld->fill will be the same as box->u.layout on exit, but (because of
positioning) not always.
box: The BOX_BLOCK to layout.
Return 1 if we should restart at the next block, 0 otherwise.
*/
static int layout_block(fz_context *ctx, layout_data *ld, fz_html_box *box)
{
fz_html_box *child;
fz_html_restarter *restart = ld->restart;
const fz_css_style *style = box->style;
float em = box->s.layout.em;
int eop = 0;
position_data position;
assert(fz_html_box_has_boxes(box));
/* If restart is non-NULL, we are running in restartable mode. */
if (restart)
{
/* If restart->start == box, then we've been skipping, and this
* is the point at which we should restart. */
if (restart->start == box)
{
if (restart->start_flags & FZ_HTML_RESTARTER_START_END_FLAGS_SPECIFIC_SIDE)
{
/* If the side matches, then great. If not, we need to end here! */
if (!!(restart->start_flags & FZ_HTML_RESTARTER_START_END_FLAGS_LEFT_SIDE) != restart->left_page)
{
/* Skip straight to stopping! */
restart->end = box;
restart->end_flags = restart->start_flags;
return 1;
}
}
/* Set restart->start to be NULL to indicate that we aren't skipping
* any more. */
restart->start = NULL;
}
/* In the event that no content fits, we want to restart with us, not with the
* content that doesn't fit. */
if (restart->potential == NULL)
restart->potential = box;
}
/* TODO: remove 'vertical' margin adjustments across automatic page breaks */
eop = layout_block_page_break(ctx, ld, &ld->used[B], style->page_break_before);
/* Cope with positioned blocks. */
eop |= pre_position(ctx, &position, ld, box, 0);
if (eop)
{
if (SHOULD_STOP_FOR_RESTART(restart))
{
box->s.layout.b = box->s.layout.y = ld->used[B];
if (restart->potential)
restart->end = restart->potential;
else
restart->end = box;
return 0;
}
}
/* At this point, fill is already setup. */
for (child = box->down; child; child = child->next)
{
if (SHOULD_STOP_FOR_RESTART(restart))
{
/* If advancing over the bottom margin/border/padding of a child has previously
* brought us to the end of a page, this is where we end. */
if (eop)
{
if (restart->potential)
restart->end = restart->potential;
else
restart->end = child;
break;
}
}
ld->used[L] = ld->bounds[L];
if (child->type == BOX_BLOCK)
eop = layout_block(ctx, ld, child);
else if (child->type == BOX_TABLE)
eop = layout_table(ctx, ld, child);
else if (child->type == BOX_FLOW)
{
layout_flow(ctx, ld, child, box);
if (!WE_ARE_SKIPPING(restart))
{
if (child->s.layout.b > child->s.layout.y)
box->s.layout.b = ld->used[B];
}
}
if (!WE_ARE_SKIPPING(restart))
{
if (ld->used[B] > box->s.layout.b)
box->s.layout.b = ld->used[B];
if (ld->used[R] > box->s.layout.x + box->s.layout.w)
box->s.layout.w = ld->used[R] - box->s.layout.x;
if (ld->used[B] > box->s.layout.b)
box->s.layout.b = ld->used[B];
}
/* Stop if we've reached the endpoint. */
if (STOPPING_FOR_RESTART(restart))
break;
if (restart && box->s.layout.b != box->s.layout.y)
restart->potential = NULL;
ld->bounds[T] = ld->used[B];
}
if (post_position(ctx, &position, ld, box, 0))
{
if (SHOULD_STOP_FOR_RESTART(restart))
{
if (restart->potential)
restart->end = restart->potential;
else
{
/* NEVER stop to restart on this object, or we will get into
* an infinite loop. We'd rather just risk losing the padding/margin/
* border at the bottom. Return 1, so we restart on the next box. */
/* DO NOT DO: restart->end = box; */
return 1;
}
return 0;
}
}
/* If we're still skipping, exit. */
if (WE_ARE_SKIPPING(restart))
return 0;
/* If we've reached the endpoint, exit. */
if (STOPPING_FOR_RESTART(restart))
return 0;
/* reserve space for the list mark */
if (box->list_item && box->s.layout.y == box->s.layout.b)
{
float delta = fz_from_css_number_scale(style->line_height, em);
box->s.layout.b += delta;
ld->used[B] += delta;
}
if (layout_block_page_break(ctx, ld, &ld->used[B], style->page_break_after))
{
/* Tricky. We need to restart, but our restart marker should be on
* the NEXT one. */
if (SHOULD_STOP_FOR_RESTART(restart))
return 1;
}
return 0;
}
/* === LAYOUT === */
// Compute new em, padding, border, margin.
// Also compute layout x and w.
static void layout_update_styles(fz_context *ctx, fz_html_box *box, fz_html_box *top)
{
float top_em = top->s.layout.em;
float top_baseline = top->s.layout.baseline;
float top_w = top->s.layout.w;
while (box)
{
const fz_css_style *style = box->style;
float em = box->s.layout.em = fz_from_css_number(style->font_size, top_em, top_em, top_em);
if (style->vertical_align == VA_SUPER)
box->s.layout.baseline = top_baseline - top_em / 3;
else if (style->vertical_align == VA_SUB)
box->s.layout.baseline = top_baseline + top_em / 5;
else
box->s.layout.baseline = top_baseline;
if (box->type != BOX_INLINE && box->type != BOX_FLOW)
{
float *margin = box->u.block.margin;
float *border = box->u.block.border;
float *padding = box->u.block.padding;
margin[T] = fz_from_css_number(style->margin[T], em, top_w, 0);
margin[R] = fz_from_css_number(style->margin[R], em, top_w, 0);
margin[B] = fz_from_css_number(style->margin[B], em, top_w, 0);
margin[L] = fz_from_css_number(style->margin[L], em, top_w, 0);
padding[T] = fz_from_css_number(style->padding[T], em, top_w, 0);
padding[R] = fz_from_css_number(style->padding[R], em, top_w, 0);
padding[B] = fz_from_css_number(style->padding[B], em, top_w, 0);
padding[L] = fz_from_css_number(style->padding[L], em, top_w, 0);
border[T] = style->border_style_0 ? fz_from_css_number(style->border_width[T], em, top_w, 0) : 0;
border[R] = style->border_style_1 ? fz_from_css_number(style->border_width[R], em, top_w, 0) : 0;
border[B] = style->border_style_2 ? fz_from_css_number(style->border_width[B], em, top_w, 0) : 0;
border[L] = style->border_style_3 ? fz_from_css_number(style->border_width[L], em, top_w, 0) : 0;
// TODO: BLOCK nested inside TABLE!
if (box->type == BOX_BLOCK || box->type == BOX_TABLE)
{
/* Compute preliminary width (will be adjusted if block is nested in table cell later) */
float auto_w = top_w - (margin[L] + margin[R] + border[L] + border[R] + padding[L] + padding[R]);
box->s.layout.w = fz_from_css_number(box->style->width, em, auto_w, auto_w);
}
}
else if (box->type == BOX_FLOW)
{
box->s.layout.x = top->s.layout.x;
box->s.layout.w = top->s.layout.w;
}
if (box->down)
layout_update_styles(ctx, box->down, box);
box = box->next;
}
}
static void layout_update_widths(fz_context *ctx, fz_html_box *box, fz_html_box *top, hb_buffer_t *hb_buf)
{
while (box)
{
if (box->type == BOX_FLOW)
{
fz_html_flow *node;
for (node = box->u.flow.head; node; node = node->next)
{
if (node->type == FLOW_IMAGE)
{
/* start with "native" size (only used for table width calculations) */
node->w = node->content.image->w * 72.0f / 96.0f;
node->w = fz_from_css_number(node->box->style->width, top->s.layout.em, node->w, node->w);
}
else if (node->type == FLOW_WORD || node->type == FLOW_SPACE || node->type == FLOW_SHYPHEN)
measure_string_w(ctx, node, hb_buf);
}
}
if (box->down)
layout_update_widths(ctx, box->down, box, hb_buf);
box = box->next;
}
}
static int is_layout_box(fz_html_box *box)
{
return box->type == BOX_BLOCK || box->type == BOX_TABLE;
}
static int is_empty_block_box(fz_html_box *box)
{
fz_html_box *child;
if (box->type == BOX_BLOCK)
{
if (box->u.block.padding[T] != 0 || box->u.block.padding[B] != 0)
return 0;
if (box->u.block.border[T] != 0 || box->u.block.border[B] != 0)
return 0;
for (child = box->down; child; child = child->next)
{
if (child->type != BOX_BLOCK)
return 0;
if (!is_empty_block_box(child))
return 0;
if (child->u.block.margin[T] != 0 || child->u.block.margin[B] != 0)
return 0;
}
return 1;
}
return 0;
}
static void layout_collapse_margin_with_children(fz_context *ctx, fz_html_box *here)
{
fz_html_box *child, *first, *last = NULL;
first = NULL;
for (child = here->down; child; child = child->next)
{
/* Ignore absolute or fixed position blocks. */
if (child->style->position != POS_FIXED && child->style->position != POS_ABSOLUTE)
{
if (first == NULL)
first = child;
last = child;
}
layout_collapse_margin_with_children(ctx, child);
}
if (here->style->position == POS_FIXED || here->style->position == POS_ABSOLUTE)
return;
if (is_layout_box(here))
{
if (first && is_layout_box(first))
{
if (first->u.block.border[T] == 0 && first->u.block.padding[T] == 0)
{
float m = fz_max(first->u.block.margin[T], here->u.block.margin[T]);
here->u.block.margin[T] = m;
first->u.block.margin[T] = 0;
}
}
if (last && is_layout_box(last))
{
if (last->u.block.border[T] == 0 && last->u.block.padding[T] == 0)
{
float m = fz_max(last->u.block.margin[B], here->u.block.margin[B]);
here->u.block.margin[B] = m;
last->u.block.margin[B] = 0;
}
}
}
}
static void layout_collapse_margin_with_siblings(fz_context *ctx, fz_html_box *here)
{
fz_html_box *prev = NULL;
while (here)
{
if (here->down)
layout_collapse_margin_with_siblings(ctx, here->down);
if (here->style->position != POS_ABSOLUTE && here->style->position != POS_FIXED)
{
if (prev && is_layout_box(prev) && is_layout_box(here))
{
float m = fz_max(prev->u.block.margin[B], here->u.block.margin[T]);
prev->u.block.margin[B] = m;
here->u.block.margin[T] = 0;
}
prev = here;
}
here = here->next;
}
}
static void layout_collapse_margin_with_self(fz_context *ctx, fz_html_box *here)
{
while (here)
{
if (here->down)
layout_collapse_margin_with_self(ctx, here->down);
if (is_layout_box(here) && is_empty_block_box(here))
{
float m = fz_max(here->u.block.margin[T], here->u.block.margin[B]);
here->u.block.margin[T] = 0;
here->u.block.margin[B] = m;
}
here = here->next;
}
}
static void layout_collapse_margins(fz_context *ctx, fz_html_box *box, fz_html_box *top)
{
// Vertical margins are collapsed in these cases:
// 1) Adjacent siblings
// 2) No content separating parent and descendants. The margin ends up outside the parent.
// 3) Empty blocks
layout_collapse_margin_with_self(ctx, box);
layout_collapse_margin_with_children(ctx, box);
layout_collapse_margin_with_siblings(ctx, box);
}
void
fz_restartable_layout_html(fz_context *ctx, fz_html_tree *tree, float start_x, float start_y, float page_w, float page_h, float em, fz_html_restarter *restart)
{
int unlocked = 0;
layout_data ld = { 0 };
fz_html_box *box = tree->root;
fz_var(ld.hb_buf);
fz_var(unlocked);
// nothing to layout
if (!box->down)
{
fz_warn(ctx, "html: nothing to layout");
box->s.layout.em = em;
box->s.layout.baseline = 0;
box->s.layout.x = start_x;
box->s.layout.w = page_w;
box->s.layout.y = start_y;
box->s.layout.b = start_y;
return;
}
fz_hb_lock(ctx);
fz_try(ctx)
{
Memento_startLeaking(); /* HarfBuzz leaks harmlessly */
ld.hb_buf = hb_buffer_create();
Memento_stopLeaking(); /* HarfBuzz leaks harmlessly */
unlocked = 1;
fz_hb_unlock(ctx);
ld.restart = restart;
ld.page[T] = start_y;
ld.page[R] = start_x + page_w;
ld.page[B] = start_y + page_h;
ld.page[L] = start_x;
TRBLCPY(ld.ancestor, ld.page);
TRBLCPY(ld.bounds, ld.page);
ld.pool = tree->pool;
if (restart)
restart->potential = NULL;
// Update em/margin/padding/border if necessary.
if (box->s.layout.em != em || box->s.layout.x != start_x || box->s.layout.w != page_w)
{
box->s.layout.em = em;
box->s.layout.baseline = 0;
box->s.layout.x = start_x;
box->s.layout.w = page_w;
layout_update_styles(ctx, box->down, box);
layout_update_widths(ctx, box->down, box, ld.hb_buf);
layout_collapse_margins(ctx, box->down, box);
}
assert(box->type == BOX_BLOCK);
layout_block(ctx, &ld, box);
}
fz_always(ctx)
{
if (unlocked)
fz_hb_lock(ctx);
hb_buffer_destroy(ld.hb_buf);
fz_hb_unlock(ctx);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
void
fz_layout_html(fz_context *ctx, fz_html *html, float w, float h, float em)
{
/* If we're already laid out to the specifications we need,
* nothing to do. */
if (html->layout_w == w && html->layout_h == h && html->layout_em == em)
return;
html->page_margin[T] = fz_from_css_number(html->tree.root->style->margin[T], em, em, 0);
html->page_margin[B] = fz_from_css_number(html->tree.root->style->margin[B], em, em, 0);
html->page_margin[L] = fz_from_css_number(html->tree.root->style->margin[L], em, em, 0);
html->page_margin[R] = fz_from_css_number(html->tree.root->style->margin[R], em, em, 0);
html->page_w = w - html->page_margin[L] - html->page_margin[R];
if (html->page_w <= 72)
html->page_w = 72; /* enforce a minimum page size! */
if (h > 0)
{
html->page_h = h - html->page_margin[T] - html->page_margin[B];
if (html->page_h <= 72)
html->page_h = 72; /* enforce a minimum page size! */
}
else
{
/* h 0 means no pagination */
html->page_h = 0;
}
fz_restartable_layout_html(ctx, &html->tree, 0, 0, html->page_w, html->page_h, em, NULL);
if (h == 0)
html->page_h = html->tree.root->s.layout.b;
/* Remember how we're laid out so we can avoid needless
* relayouts in future. */
html->layout_w = w;
html->layout_h = h;
html->layout_em = em;
#ifndef NDEBUG
if (fz_atoi(getenv("FZ_DEBUG_HTML")))
fz_debug_html(ctx, html->tree.root);
#endif
}
/* === DRAW === */
typedef struct
{
float rgb[3];
float a;
} unpacked_color;
static inline unpacked_color
unpack_color(const fz_css_color src)
{
unpacked_color dst;
dst.rgb[0] = src.r / 255.0f;
dst.rgb[1] = src.g / 255.0f;
dst.rgb[2] = src.b / 255.0f;
dst.a = src.a / 255.0f;
return dst;
}
static inline int
color_eq(fz_css_color a, fz_css_color b)
{
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
static int draw_flow_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, hb_buffer_t *hb_buf, fz_html_restarter *restart)
{
fz_html_flow *node;
fz_text *text = NULL;
fz_path *line = NULL;
fz_matrix trm;
fz_css_color prev_color = { 0, 0, 0, 0 };
fz_css_color prev_fill_color = { 0, 0, 0, 0 };
fz_css_color prev_stroke_color = { 0, 0, 0, 0 };
float line_width, prev_line_width = 0;
int filling, prev_filling = 0;
int stroking, prev_stroking = 0;
int restartable_ended = 0;
fz_stroke_state *ss = NULL;
fz_stroke_state *line_ss = NULL;
fz_var(text);
fz_var(line);
fz_var(ss);
fz_var(line_ss);
/* FIXME: HB_DIRECTION_TTB? */
if (restart && restart->start != NULL && restart->start != box)
return 0;
fz_try(ctx)
{
for (node = box->u.flow.head; node; node = node->next)
{
const fz_css_style *style = node->box->style;
if (restart)
{
if (restart->start_flow != NULL)
{
if (restart->start_flow != node)
continue;
restart->start = NULL;
restart->start_flow = NULL;
}
if (restart->end == box && restart->end_flow == node)
{
restartable_ended = 1;
break;
}
}
if (node->type == FLOW_IMAGE)
{
if (node->y >= page_bot || node->y + node->h <= page_top)
continue;
}
else
{
if (node->y > page_bot || node->y < page_top)
continue;
}
if (node->type == FLOW_WORD || node->type == FLOW_SPACE || node->type == FLOW_SHYPHEN)
{
string_walker walker;
const char *s;
float x, y;
float em;
if (node->type == FLOW_SPACE && node->breaks_line)
continue;
if (node->type == FLOW_SHYPHEN && !node->breaks_line)
continue;
if (style->visibility != V_VISIBLE)
continue;
em = node->box->s.layout.em;
line_width = fz_from_css_number(style->text_stroke_width, em, em, 0);
filling = style->text_fill_color.a != 0;
stroking = style->text_stroke_color.a != 0;
if (stroking)
{
if (ss == NULL)
ss = fz_new_stroke_state(ctx);
}
if (line)
{
if (line_ss == NULL)
line_ss = fz_new_stroke_state(ctx);
}
if ( /* If we've changed whether we're filling... */
filling != prev_filling ||
/* Or we're filling and the color has changed... */
(prev_filling && !color_eq(style->text_fill_color, prev_fill_color)) ||
/* Or we've changed whether we're stroking... */
stroking != prev_stroking ||
/* Or we're stroking, and the color or linewidth has changed... */
(prev_stroking && (!color_eq(style->text_stroke_color, prev_stroke_color) || line_width != prev_line_width)))
{
if (text)
{
if (prev_filling)
{
unpacked_color color = unpack_color(prev_fill_color);
fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
}
if (prev_stroking)
{
unpacked_color color = unpack_color(prev_stroke_color);
ss->linewidth = prev_line_width;
fz_stroke_text(ctx, dev, text, ss, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
}
fz_drop_text(ctx, text);
text = NULL;
}
prev_filling = filling;
prev_stroking = stroking;
}
prev_fill_color = style->text_fill_color;
prev_stroke_color = style->text_stroke_color;
prev_line_width = line_width;
if (!color_eq(style->color, prev_color))
{
if (line)
{
unpacked_color color = unpack_color(prev_color);
fz_stroke_path(ctx, dev, line, line_ss, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
fz_drop_path(ctx, line);
line = NULL;
}
prev_color = style->color;
}
if (style->text_decoration > 0)
{
if (!line)
{
line = fz_new_path(ctx);
if (line_ss == NULL)
line_ss = fz_new_stroke_state(ctx);
}
if (style->text_decoration & TD_UNDERLINE)
{
fz_moveto(ctx, line, node->x, node->y + 1.5f - page_top);
fz_lineto(ctx, line, node->x + node->w, node->y + 1.5f - page_top);
}
if (style->text_decoration & TD_LINE_THROUGH)
{
fz_moveto(ctx, line, node->x, node->y - em * 0.3f - page_top);
fz_lineto(ctx, line, node->x + node->w, node->y - em * 0.3f - page_top);
}
}
if (!text)
text = fz_new_text(ctx);
if (node->bidi_level & 1)
x = node->x + node->w;
else
x = node->x;
y = node->y;
trm.a = em;
trm.b = 0;
trm.c = 0;
trm.d = -em;
trm.e = x;
trm.f = y - page_top;
s = get_node_text(ctx, node);
init_string_walker(ctx, &walker, hb_buf, node->bidi_level & 1, style->font, node->script, node->markup_lang, style->small_caps, s, 0);
while (walk_string(&walker))
{
float node_scale = node->box->s.layout.em / walker.scale;
unsigned int i;
uint32_t k;
int c, n;
/* Flatten advance and offset into offset array. */
int x_advance = 0;
int y_advance = 0;
for (i = 0; i < walker.glyph_count; ++i)
{
walker.glyph_pos[i].x_offset += x_advance;
walker.glyph_pos[i].y_offset += y_advance;
x_advance += walker.glyph_pos[i].x_advance;
y_advance += walker.glyph_pos[i].y_advance;
}
if (node->bidi_level & 1)
x -= x_advance * node_scale;
/* Walk characters to find glyph clusters */
k = 0;
while (walker.start + k < walker.end)
{
n = fz_chartorune(&c, walker.start + k);
for (i = 0; i < walker.glyph_count; ++i)
{
if (walker.glyph_info[i].cluster == k)
{
trm.e = x + walker.glyph_pos[i].x_offset * node_scale;
trm.f = y - walker.glyph_pos[i].y_offset * node_scale - page_top;
fz_show_glyph(ctx, text, walker.font, trm,
walker.glyph_info[i].codepoint, c,
0, node->bidi_level, box->markup_dir, node->markup_lang);
c = -1; /* for subsequent glyphs in x-to-many mappings */
}
}
/* no glyph found (many-to-many or many-to-one mapping) */
if (c != -1)
{
fz_show_glyph(ctx, text, walker.font, trm,
-1, c,
0, node->bidi_level, box->markup_dir, node->markup_lang);
}
k += n;
}
if ((node->bidi_level & 1) == 0)
x += x_advance * node_scale;
y += y_advance * node_scale;
}
}
else if (node->type == FLOW_IMAGE)
{
if (text)
{
if (filling)
{
unpacked_color color = unpack_color(prev_fill_color);
fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
}
if (stroking)
{
unpacked_color color = unpack_color(prev_stroke_color);
ss->linewidth = line_width;
fz_stroke_text(ctx, dev, text, ss, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
}
fz_drop_text(ctx, text);
text = NULL;
}
if (style->visibility == V_VISIBLE)
{
float alpha = style->color.a / 255.0f;
fz_matrix itm = fz_pre_translate(ctm, node->x, node->y - page_top);
itm = fz_pre_scale(itm, node->w, node->h);
fz_fill_image(ctx, dev, node->content.image, itm, alpha, fz_default_color_params);
}
}
}
if (text)
{
if (filling)
{
unpacked_color color = unpack_color(prev_fill_color);
fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
}
if (stroking)
{
unpacked_color color = unpack_color(prev_stroke_color);
ss->linewidth = prev_line_width;
fz_stroke_text(ctx, dev, text, ss, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
}
fz_drop_text(ctx, text);
text = NULL;
}
if (line)
{
unpacked_color color = unpack_color(prev_color);
fz_stroke_path(ctx, dev, line, line_ss, ctm, fz_device_rgb(ctx), color.rgb, color.a, fz_default_color_params);
fz_drop_path(ctx, line);
line = NULL;
}
}
fz_always(ctx)
{
fz_drop_text(ctx, text);
fz_drop_path(ctx, line);
fz_drop_stroke_state(ctx, ss);
fz_drop_stroke_state(ctx, line_ss);
}
fz_catch(ctx)
fz_rethrow(ctx);
return restartable_ended;
}
static void draw_rect(fz_context *ctx, fz_device *dev, fz_matrix ctm, float page_top, fz_css_color color, float x0, float y0, float x1, float y1)
{
float rgb[3];
fz_path *path;
if (color.a <= 0)
return;
path = fz_new_path(ctx);
fz_try(ctx)
{
fz_moveto(ctx, path, x0, y0 - page_top);
fz_lineto(ctx, path, x1, y0 - page_top);
fz_lineto(ctx, path, x1, y1 - page_top);
fz_lineto(ctx, path, x0, y1 - page_top);
fz_closepath(ctx, path);
rgb[0] = color.r / 255.0f;
rgb[1] = color.g / 255.0f;
rgb[2] = color.b / 255.0f;
fz_fill_path(ctx, dev, path, 0, ctm, fz_device_rgb(ctx), rgb, color.a / 255.0f, fz_default_color_params);
}
fz_always(ctx)
fz_drop_path(ctx, path);
fz_catch(ctx)
fz_rethrow(ctx);
}
static void draw_quad(fz_context *ctx, fz_device *dev, fz_matrix ctm, fz_css_color color,
float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3)
{
float rgb[3];
fz_path *path;
if (color.a <= 0)
return;
path = fz_new_path(ctx);
fz_try(ctx)
{
fz_moveto(ctx, path, x0, y0);
fz_lineto(ctx, path, x1, y1);
fz_lineto(ctx, path, x2, y2);
fz_lineto(ctx, path, x3, y3);
fz_closepath(ctx, path);
rgb[0] = color.r / 255.0f;
rgb[1] = color.g / 255.0f;
rgb[2] = color.b / 255.0f;
fz_fill_path(ctx, dev, path, 0, ctm, fz_device_rgb(ctx), rgb, color.a / 255.0f, fz_default_color_params);
}
fz_always(ctx)
fz_drop_path(ctx, path);
fz_catch(ctx)
fz_rethrow(ctx);
}
static void
fudge_len(float *wp, float len, int count, float extra)
{
/* We have a line of length len, in which we have to fit n*count+extra sections
* for some integer n, n > 1. We'd like to find n s.t. each of those sections is
* approximately w.
*
* So, ideally len = (n*count+extra)*w
* (len/w - extra)/count = n
* ((len - w*extra)/count/w = n
*/
float w = *wp;
int n = 0;
assert(w > 0 && count > 0);
if (len > w*extra)
n = ((len - w*extra) + (count*w)/2) / (count*w);
if (n < 1)
n = 1;
*wp = len / (n * count + extra);
}
static void draw_dotted_line(fz_context *ctx, fz_device *dev, fz_matrix ctm, fz_css_color color, int dashed, float w,
float x0, float y0, float x1, float y1)
{
float rgb[3];
fz_path *path;
fz_stroke_state *stroke;
float len;
if (color.a <= 0 || w <= 0)
return;
/* Rely on the fact that we only draw othogonal lines here! */
len = x1 - x0 + y1 - y0;
if (len < 0)
len = -len;
path = fz_new_path(ctx);
stroke = NULL;
fz_var(stroke);
fz_try(ctx)
{
fz_moveto(ctx, path, x0, y0);
fz_lineto(ctx, path, x1, y1);
rgb[0] = color.r / 255.0f;
rgb[1] = color.g / 255.0f;
rgb[2] = color.b / 255.0f;
stroke = fz_new_stroke_state_with_dash_len(ctx, 2);
stroke->linewidth = w;
stroke->dash_len = 2;
if (dashed)
{
fudge_len(&w, len, 4, 1);
stroke->start_cap = FZ_LINECAP_SQUARE;
stroke->dash_cap = FZ_LINECAP_SQUARE;
stroke->end_cap = FZ_LINECAP_SQUARE;
stroke->dash_list[0] = w;
stroke->dash_list[1] = w*3;
}
else
{
fudge_len(&w, len, 2, 0.01f);
stroke->start_cap = FZ_LINECAP_ROUND;
stroke->dash_cap = FZ_LINECAP_ROUND;
stroke->end_cap = FZ_LINECAP_ROUND;
stroke->dash_list[0] = 0;
stroke->dash_list[1] = w*2;
}
fz_stroke_path(ctx, dev, path, stroke, ctm, fz_device_rgb(ctx), rgb, color.a / 255.0f, fz_default_color_params);
}
fz_always(ctx)
{
fz_drop_stroke_state(ctx, stroke);
fz_drop_path(ctx, path);
}
fz_catch(ctx)
fz_rethrow(ctx);
}
static const char *roman_uc[3][10] = {
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
{ "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
{ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
};
static const char *roman_lc[3][10] = {
{ "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix" },
{ "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc" },
{ "", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm" },
};
static void format_roman_number(fz_context *ctx, char *buf, int size, int n, const char *sym[3][10], const char *sym_m)
{
int I = n % 10;
int X = (n / 10) % 10;
int C = (n / 100) % 10;
int M = (n / 1000);
fz_strlcpy(buf, "", size);
while (M--)
fz_strlcat(buf, sym_m, size);
fz_strlcat(buf, sym[2][C], size);
fz_strlcat(buf, sym[1][X], size);
fz_strlcat(buf, sym[0][I], size);
fz_strlcat(buf, ". ", size);
}
static void format_alpha_number(fz_context *ctx, char *buf, int size, int n, int alpha, int omega)
{
int base = omega - alpha + 1;
int tmp[40];
int i, c;
if (alpha > 256) /* to skip final-s for greek */
--base;
/* Bijective base-26 (base-24 for greek) numeration */
i = 0;
while (n > 0)
{
--n;
c = n % base + alpha;
if (alpha > 256 && c > alpha + 16) /* skip final-s for greek */
++c;
tmp[i++] = c;
n /= base;
}
while (i > 0)
buf += fz_runetochar(buf, tmp[--i]);
*buf++ = '.';
*buf++ = ' ';
*buf = 0;
}
static void format_list_number(fz_context *ctx, int type, int x, char *buf, int size)
{
switch (type)
{
case LST_NONE: fz_strlcpy(buf, "", size); break;
case LST_DISC: fz_snprintf(buf, size, "%C ", 0x2022); break; /* U+2022 BULLET */
case LST_CIRCLE: fz_snprintf(buf, size, "%C ", 0x25CB); break; /* U+25CB WHITE CIRCLE */
case LST_SQUARE: fz_snprintf(buf, size, "%C ", 0x25A0); break; /* U+25A0 BLACK SQUARE */
default:
case LST_DECIMAL: fz_snprintf(buf, size, "%d. ", x); break;
case LST_DECIMAL_ZERO: fz_snprintf(buf, size, "%02d. ", x); break;
case LST_LC_ROMAN: format_roman_number(ctx, buf, size, x, roman_lc, "m"); break;
case LST_UC_ROMAN: format_roman_number(ctx, buf, size, x, roman_uc, "M"); break;
case LST_LC_ALPHA: format_alpha_number(ctx, buf, size, x, 'a', 'z'); break;
case LST_UC_ALPHA: format_alpha_number(ctx, buf, size, x, 'A', 'Z'); break;
case LST_LC_LATIN: format_alpha_number(ctx, buf, size, x, 'a', 'z'); break;
case LST_UC_LATIN: format_alpha_number(ctx, buf, size, x, 'A', 'Z'); break;
case LST_LC_GREEK: format_alpha_number(ctx, buf, size, x, 0x03B1, 0x03C9); break;
case LST_UC_GREEK: format_alpha_number(ctx, buf, size, x, 0x0391, 0x03A9); break;
}
}
static fz_html_flow *find_list_mark_anchor(fz_context *ctx, fz_html_box *box)
{
/* find first flow node in <li> tag */
while (box)
{
if (box->type == BOX_FLOW)
return box->u.flow.head;
box = box->down;
}
return NULL;
}
static void draw_list_mark(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, int n)
{
fz_font *font;
fz_text *text;
fz_matrix trm;
fz_html_flow *line;
float y, w;
float color[4];
const char *s;
char buf[40];
int c, g;
trm = fz_scale(box->s.layout.em, -box->s.layout.em);
line = find_list_mark_anchor(ctx, box);
if (line)
{
y = line->y;
}
else
{
float h = fz_from_css_number_scale(box->style->line_height, box->s.layout.em);
float a = box->s.layout.em * 0.8f;
float d = box->s.layout.em * 0.2f;
if (a + d > h)
h = a + d;
y = box->s.layout.y + a + (h - a - d) / 2;
}
if (y > page_bot || y < page_top)
return;
format_list_number(ctx, box->style->list_style_type, n, buf, sizeof buf);
s = buf;
w = 0;
while (*s)
{
s += fz_chartorune(&c, s);
g = fz_encode_character_with_fallback(ctx, box->style->font, c, UCDN_SCRIPT_LATIN, FZ_LANG_UNSET, &font);
w += fz_advance_glyph(ctx, font, g, 0) * box->s.layout.em;
}
text = fz_new_text(ctx);
fz_try(ctx)
{
s = buf;
trm.e = box->s.layout.x - w;
trm.f = y - page_top;
while (*s)
{
s += fz_chartorune(&c, s);
g = fz_encode_character_with_fallback(ctx, box->style->font, c, UCDN_SCRIPT_LATIN, FZ_LANG_UNSET, &font);
fz_show_glyph(ctx, text, font, trm, g, c, 0, 0, FZ_BIDI_NEUTRAL, FZ_LANG_UNSET);
trm.e += fz_advance_glyph(ctx, font, g, 0) * box->s.layout.em;
}
color[0] = box->style->color.r / 255.0f;
color[1] = box->style->color.g / 255.0f;
color[2] = box->style->color.b / 255.0f;
color[3] = box->style->color.a / 255.0f;
fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, color[3], fz_default_color_params);
}
fz_always(ctx)
fz_drop_text(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
}
static int draw_block_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, hb_buffer_t *hb_buf, fz_html_restarter *restart);
static int draw_table_row(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, hb_buffer_t *hb_buf, fz_html_restarter *restart);
static int draw_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, hb_buffer_t *hb_buf, fz_html_restarter *restart)
{
int ret = 0;
int str = fz_html_tag_to_structure(box->tag);
if (str != FZ_STRUCTURE_INVALID)
fz_begin_structure(ctx, dev, str, box->tag, 0);
switch (box->type)
{
case BOX_TABLE_ROW:
if (restart && restart->end == box)
ret = 1;
else if (draw_table_row(ctx, box, page_top, page_bot, dev, ctm, hb_buf, restart))
ret = 1;
break;
case BOX_TABLE:
case BOX_TABLE_CELL:
case BOX_BLOCK:
if (restart && restart->end == box)
ret = 1;
else if (draw_block_box(ctx, box, page_top, page_bot, dev, ctm, hb_buf, restart))
ret = 1;
break;
case BOX_FLOW:
if (draw_flow_box(ctx, box, page_top, page_bot, dev, ctm, hb_buf, restart))
ret = 1;
break;
}
if (str != FZ_STRUCTURE_INVALID)
fz_end_structure(ctx, dev);
return ret;
}
static fz_css_color
brighten(fz_css_color c)
{
int delta;
float S;
float C;
float H;
if (c.r > c.g)
{
if (c.b > c.r)
{
/* b > r > g */
/* All 6 cases are very similar, we'll just comment the first one. */
delta = c.b - c.g; /* Never zero */
S = delta/(float)c.b;
/* A proper conversion from RGB -> HSV would map H into a sextant;
* given we're going to convert back, we don't bother. */
H = (c.r - c.g)/(float)delta; /* Proper H would be: 4 <= H < 5 */
c.b = (c.b>>1) + 128;
/* v = cmax = c.b */
C = c.b * S;
/* m = cmax - C */
c.g = c.b - C;
c.r = c.g + C * H;
c.b = c.g + C;
}
else if (c.b > c.g)
{
/* r >= b > g */
delta = c.r - c.g; /* Never zero */
S = delta/(float)c.r;
H = (c.b - c.g)/(float)delta; /* Proper H would be: 5 <= H < 6 */
c.r = (c.r>>1) + 128;
C = c.r * S;
c.g = c.r - C;
c.b = c.g + C * H;
c.r = c.g + C;
}
else
{
/* r > g >= b */
delta = c.r - c.b; /* Never zero */
S = delta/(float)c.r;
H = (c.g - c.b)/(float)delta; /* Proper H would be: 0 <= H < 1 */
c.r = (c.r>>1) + 128;
C = c.r * S;
c.b = c.r - C;
c.g = c.b + C * H;
c.r = c.b + C;
}
}
else
{
if (c.b > c.g)
{
/* b > g >= r */
delta = c.b - c.r; /* Never zero */
S = delta/(float)c.b;
H = (c.g - c.r)/(float)delta; /* Proper H would be: 3 <= H < 4 */
c.b = (c.b>>1) + 128;
C = c.b * S;
c.r = c.b - C;
c.g = c.r + C * H;
c.b = c.r + C;
}
else if (c.r > c.b)
{
/* g >= r > b */
delta = c.g - c.b; /* Never zero */
S = delta/(float)c.g;
H = (c.r - c.b)/(float)delta; /* Proper H would be: 1 <= H < 2 */
c.g = (c.g>>1) + 128;
C = c.g * S;
c.b = c.g - C;
c.r = c.b + C * H;
c.g = c.b + C;
}
else if (c.g == c.b && c.g == c.r)
{
/* r = g = b */
H = 0;
c.g = (c.g>>1) + 128;
c.r = c.g;
c.b = c.g;
}
else
{
/* g >= b >= r */
delta = c.g - c.r; /* Never zero */
S = delta/(float)c.g;
H = (c.b - c.r)/(float)delta; /* Proper H would be: 2 <= H < 3 */
c.g = (c.g>>1) + 128;
C = c.g * S;
c.r = c.g - C;
c.b = c.r + C * H;
c.g = c.r + C;
}
}
return c;
}
static fz_css_color
darken(fz_css_color c)
{
int delta;
float S;
float C;
float H;
if (c.r > c.g)
{
if (c.b > c.r)
{
/* b > r > g */
/* All 6 cases are very similar, we'll just comment the first one. */
delta = c.b - c.g; /* Never zero */
S = delta/(float)c.b;
/* A proper conversion from RGB -> HSV would map H into a sextant;
* given we're going to convert back, we don't bother. */
H = (c.r - c.g)/(float)delta; /* Proper H would be: 4 <= H < 5 */
c.b >>= 1;
/* v = cmax = c.b */
C = c.b * S;
/* m = cmax - C */
c.g = c.b - C;
c.r = c.g + C * H;
c.b = c.g + C;
}
else if (c.b > c.g)
{
/* r >= b > g */
delta = c.r - c.g; /* Never zero */
S = delta/(float)c.r;
H = (c.b - c.g)/(float)delta; /* Proper H would be: 5 <= H < 6 */
c.r >>= 1;
C = c.r * S;
c.g = c.r - C;
c.b = c.g + C * H;
c.r = c.g + C;
}
else
{
/* r > g >= b */
delta = c.r - c.b; /* Never zero */
S = delta/(float)c.r;
H = (c.g - c.b)/(float)delta; /* Proper H would be: 0 <= H < 1 */
c.r >>= 1;
C = c.r * S;
c.b = c.r - C;
c.g = c.b + C * H;
c.r = c.b + C;
}
}
else
{
if (c.b > c.g)
{
/* b > g >= r */
delta = c.b - c.r; /* Never zero */
S = delta/(float)c.b;
H = (c.g - c.r)/(float)delta; /* Proper H would be: 3 <= H < 4 */
c.b >>= 1;
C = c.b * S;
c.r = c.b - C;
c.g = c.r + C * H;
c.b = c.r + C;
}
else if (c.r > c.b)
{
/* g >= r > b */
delta = c.g - c.b; /* Never zero */
S = delta/(float)c.g;
H = (c.r - c.b)/(float)delta; /* Proper H would be: 1 <= H < 2 */
c.g >>= 1;
C = c.g * S;
c.b = c.g - C;
c.r = c.b + C * H;
c.g = c.b + C;
}
else if (c.g == c.b && c.g == c.r)
{
/* r = g = b */
H = 0;
c.g >>= 1;
c.r = c.g;
c.b = c.g;
}
else
{
/* g >= b >= r */
delta = c.g - c.r; /* Never zero */
S = delta/(float)c.g;
H = (c.b - c.r)/(float)delta; /* Proper H would be: 2 <= H < 3 */
c.g >>= 1;
C = c.g * S;
c.r = c.g - C;
c.b = c.r + C * H;
c.g = c.r + C;
}
}
return c;
}
static int collapse_border_style(int style, int collapsed)
{
if (collapsed)
{
if (style == BS_INSET)
style = BS_RIDGE;
if (style == BS_OUTSET)
style = BS_GROOVE;
}
return style;
}
static void
draw_border(fz_context *ctx, fz_device *dev, fz_matrix ctm, fz_html_box *box, int edge, float x0, float y0, float x1, float y1)
{
float *border = box->u.block.border;
const fz_css_style *style = box->style;
int collapsed = box->collapsed_cell;
switch (edge)
{
case T:
switch (collapse_border_style(style->border_style_0, collapsed))
{
case BS_NONE:
return; /* Should never happen */
case BS_OUTSET:
draw_quad(ctx, dev, ctm, brighten(style->border_color[T]), x0 - border[L], y0 - border[T], x0, y0, x1, y0, x1 + border[R], y0 - border[T]);
break;
case BS_INSET:
draw_quad(ctx, dev, ctm, darken(style->border_color[T]), x0 - border[L], y0 - border[T], x0, y0, x1, y0, x1 + border[R], y0 - border[T]);
break;
case BS_RIDGE:
draw_quad(ctx, dev, ctm, brighten(style->border_color[T]), x0 - border[L], y0 - border[T], x0 - border[L]/2, y0 - border[T]/2, x1 + border[R]/2, y0 - border[T]/2, x1 + border[R], y0 - border[T]);
draw_quad(ctx, dev, ctm, darken(style->border_color[T]), x0 - border[L]/2, y0 - border[T]/2, x0, y0, x1, y0, x1 + border[R]/2, y0 - border[T]/2);
break;
case BS_GROOVE:
draw_quad(ctx, dev, ctm, darken(style->border_color[T]), x0 - border[L], y0 - border[T], x0 - border[L]/2, y0 - border[T]/2, x1 + border[R]/2, y0 - border[T]/2, x1 + border[R], y0 - border[T]);
draw_quad(ctx, dev, ctm, brighten(style->border_color[T]), x0 - border[L]/2, y0 - border[T]/2, x0, y0, x1, y0, x1 + border[R]/2, y0 - border[T]/2);
break;
case BS_DOUBLE:
draw_quad(ctx, dev, ctm, style->border_color[T], x0 - border[L], y0 - border[T], x0 - 2*border[L]/3, y0 - 2*border[T]/3, x1 + 2*border[R]/3, y0 - 2*border[T]/3, x1 + border[R], y0 - border[T]);
draw_quad(ctx, dev, ctm, style->border_color[T], x0 - border[L]/3, y0 - border[T]/3, x0, y0, x1, y0, x1 + border[R]/3, y0 - border[T]/3);
break;
case BS_DOTTED:
{
float left = border[L] - border[T]/2;
float right = border[R] - border[T]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[T], 0, border[T], x0 - left, y0 - border[T]/2, x1 + right, y0 - border[T]/2);
break;
}
case BS_DASHED:
{
float left = border[L] - border[T]/2;
float right = border[R] - border[T]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[T], 1, border[T], x0 - left, y0 - border[T]/2, x1 + right, y0 - border[T]/2);
break;
}
case BS_SOLID:
draw_rect(ctx, dev, ctm, 0, style->border_color[T], x0 - border[L], y0 - border[T], x1 + border[R], y0);
break;
}
break;
case R:
switch (collapse_border_style(style->border_style_1, collapsed))
{
case BS_NONE:
return; /* Should never happen */
case BS_OUTSET:
draw_quad(ctx, dev, ctm, darken(style->border_color[R]), x1 + border[R], y0 - border[T], x1, y0, x1, y1, x1 + border[R], y1 + border[B]);
break;
case BS_INSET:
draw_quad(ctx, dev, ctm, brighten(style->border_color[R]), x1 + border[R], y0 - border[T], x1, y0, x1, y1, x1 + border[R], y1 + border[B]);
break;
case BS_RIDGE:
draw_quad(ctx, dev, ctm, darken(style->border_color[R]), x1 + border[R], y0 - border[T], x1 + border[R]/2, y0 - border[T]/2, x1 + border[R]/2, y1 + border[B]/2, x1 + border[R], y1 + border[B]);
draw_quad(ctx, dev, ctm, brighten(style->border_color[R]), x1 + border[R]/2, y0 - border[T]/2, x1, y0, x1, y1, x1 + border[R]/2, y1 + border[B]/2);
break;
case BS_GROOVE:
draw_quad(ctx, dev, ctm, brighten(style->border_color[R]), x1 + border[R], y0 - border[T], x1 + border[R]/2, y0 - border[T]/2, x1 + border[R]/2, y1 + border[B]/2, x1 + border[R], y1 + border[B]);
draw_quad(ctx, dev, ctm, darken(style->border_color[R]), x1 + border[R]/2, y0 - border[T]/2, x1, y0, x1, y1, x1 + border[R]/2, y1 + border[B]/2);
break;
case BS_DOUBLE:
draw_quad(ctx, dev, ctm, style->border_color[R], x1 + border[R], y0 - border[T], x1 + 2*border[R]/3, y0 - 2*border[T]/3, x1 + 2*border[R]/3, y1 + 2*border[B]/3, x1 + border[R], y1 + border[B]);
draw_quad(ctx, dev, ctm, style->border_color[R], x1 + border[R]/3, y0 - border[T]/3, x1, y0, x1, y1, x1 + border[R]/3, y1 + border[B]/3);
break;
case BS_DOTTED:
{
float top = border[T] - border[R]/2;
float bottom = border[B] - border[R]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[R], 0, border[R], x1 + border[R]/2, y0 - top, x1 + border[R]/2, y1 + bottom);
break;
}
case BS_DASHED:
{
float top = border[T] - border[R]/2;
float bottom = border[B] - border[R]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[R], 1, border[R], x1 + border[R]/2, y0 - top, x1 + border[R]/2, y1 + bottom);
break;
}
case BS_SOLID:
draw_rect(ctx, dev, ctm, 0, style->border_color[R], x1, y0 - border[T], x1 + border[R], y1 + border[B]);
break;
}
break;
case B:
switch (collapse_border_style(style->border_style_2, collapsed))
{
case BS_NONE:
return; /* Should never happen */
case BS_OUTSET:
draw_quad(ctx, dev, ctm, darken(style->border_color[B]), x0 - border[L], y1 + border[B], x0, y1, x1, y1, x1 + border[R], y1 + border[B]);
break;
case BS_INSET:
draw_quad(ctx, dev, ctm, brighten(style->border_color[B]), x0 - border[L], y1 + border[B], x0, y1, x1, y1, x1 + border[R], y1 + border[B]);
break;
case BS_RIDGE:
draw_quad(ctx, dev, ctm, darken(style->border_color[B]), x0 - border[L], y1 + border[B], x0 - border[L]/2, y1 + border[B]/2, x1 + border[R]/2, y1 + border[B]/2, x1 + border[R], y1 + border[B]);
draw_quad(ctx, dev, ctm, brighten(style->border_color[B]), x0 - border[L]/2, y1 + border[B]/2, x0, y1, x1, y1, x1 + border[R]/2, y1 + border[B]/2);
break;
case BS_GROOVE:
draw_quad(ctx, dev, ctm, brighten(style->border_color[B]), x0 - border[L], y1 + border[B], x0 - border[L]/2, y1 + border[B]/2, x1 + border[R]/2, y1 + border[B]/2, x1 + border[R], y1 + border[B]);
draw_quad(ctx, dev, ctm, darken(style->border_color[B]), x0 - border[L]/2, y1 + border[B]/2, x0, y1, x1, y1, x1 + border[R]/2, y1 + border[B]/2);
break;
case BS_DOUBLE:
draw_quad(ctx, dev, ctm, style->border_color[B], x0 - border[L], y1 + border[B], x0 - 2*border[L]/3, y1 + 2*border[B]/3, x1 + 2*border[R]/3, y1 + 2*border[B]/3, x1 + border[R], y1 + border[B]);
draw_quad(ctx, dev, ctm, style->border_color[B], x0 - border[L]/3, y1 + border[B]/3, x0, y1, x1, y1, x1 + border[R]/3, y1 + border[B]/3);
break;
case BS_DOTTED:
{
float left = border[L] - border[B]/2;
float right = border[R] - border[B]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[B], 0, border[B], x1 + right, y1 + border[B]/2, x0 - left, y1 + border[B]/2);
break;
}
case BS_DASHED:
{
float left = border[L] - border[B]/2;
float right = border[R] - border[B]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[B], 1, border[B], x1 + right, y1 + border[B]/2, x0 - left, y1 + border[B]/2);
break;
}
case BS_SOLID:
draw_rect(ctx, dev, ctm, 0, style->border_color[B], x0 - border[L], y1, x1 + border[R], y1 + border[B]);
break;
}
break;
case L:
switch (collapse_border_style(style->border_style_3, collapsed))
{
case BS_NONE:
return; /* Should never happen */
case BS_OUTSET:
draw_quad(ctx, dev, ctm, brighten(style->border_color[L]), x0 - border[L], y0 - border[T], x0, y0, x0, y1, x0 - border[L], y1 + border[B]);
break;
case BS_INSET:
draw_quad(ctx, dev, ctm, darken(style->border_color[L]), x0 - border[L], y0 - border[T], x0, y0, x0, y1, x0 - border[L], y1 + border[B]);
break;
case BS_RIDGE:
draw_quad(ctx, dev, ctm, brighten(style->border_color[L]), x0 - border[L], y0 - border[T], x0 - border[L]/2, y0 - border[T]/2, x0 - border[L]/2, y1 + border[B]/2, x0 - border[L], y1 + border[B]);
draw_quad(ctx, dev, ctm, darken(style->border_color[L]), x0 - border[L]/2, y0 - border[T]/2, x0, y0, x0, y1, x0 - border[L]/2, y1 + border[B]/2);
break;
case BS_GROOVE:
draw_quad(ctx, dev, ctm, darken(style->border_color[L]), x0 - border[L], y0 - border[T], x0 - border[L]/2, y0 - border[T]/2, x0 - border[L]/2, y1 + border[B]/2, x0 - border[L], y1 + border[B]);
draw_quad(ctx, dev, ctm, brighten(style->border_color[L]), x0 - border[L]/2, y0 - border[T]/2, x0, y0, x0, y1, x0 - border[L]/2, y1 + border[B]/2);
break;
case BS_DOUBLE:
draw_quad(ctx, dev, ctm, style->border_color[L], x0 - border[L], y0 - border[T], x0 - 2*border[L]/3, y0 - 2*border[T]/3, x0 - 2*border[L]/3, y1 + 2*border[B]/3, x0 - border[L], y1 + border[B]);
draw_quad(ctx, dev, ctm, style->border_color[L], x0 - border[L]/3, y0 - border[T]/3, x0, y0, x0, y1, x0 - border[L]/3, y1 + border[B]/3);
break;
case BS_DOTTED:
{
float top = border[T] - border[L]/2;
float bottom = border[B] - border[L]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[L], 0, border[L], x0 - border[L]/2, y1 + bottom, x0 - border[L]/2, y0 - top);
break;
}
case BS_DASHED:
{
float top = border[T] - border[L]/2;
float bottom = border[B] - border[L]/2;
draw_dotted_line(ctx, dev, ctm, style->border_color[L], 1, border[L], x0 - border[L]/2, y1 + bottom, x0 - border[L]/2, y0 - top);
break;
}
case BS_SOLID:
draw_rect(ctx, dev, ctm, 0, style->border_color[L], x0 - border[L], y0 - border[T], x0, y1 + border[B]);
break;
}
break;
}
}
static void
do_borders(fz_context *ctx, fz_device *dev, fz_matrix ctm, float page_top, fz_html_box *box, int suppress)
{
float cell_adjust_top = box->type == BOX_TABLE_CELL ? box->u.block.margin[T] : 0;
float cell_adjust_right = box->type == BOX_TABLE_CELL ? box->u.block.margin[R] : 0;
float cell_adjust_bot = box->type == BOX_TABLE_CELL ? box->u.block.margin[B] : 0;
float cell_adjust_left = box->type == BOX_TABLE_CELL ? box->u.block.margin[L] : 0;
float *border = box->u.block.border;
float *padding = box->u.block.padding;
float x0 = box->s.layout.x - padding[L] - cell_adjust_left;
float y0 = box->s.layout.y - padding[T] - page_top - cell_adjust_top;
float x1 = box->s.layout.x + box->s.layout.w + padding[R] + cell_adjust_right;
float y1 = box->s.layout.b + padding[B] - page_top + cell_adjust_bot;
suppress |= box->suppress_border;
if (border[T] > 0 && !(suppress & (1<<T)))
draw_border(ctx, dev, ctm, box, T, x0, y0, x1, y1);
if (border[R] > 0 && !(suppress & (1<<R)))
draw_border(ctx, dev, ctm, box, R, x0, y0, x1, y1);
if (border[B] > 0 && !(suppress & (1<<B)))
draw_border(ctx, dev, ctm, box, B, x0, y0, x1, y1);
if (border[L] > 0 && !(suppress & (1<<L)))
draw_border(ctx, dev, ctm, box, L, x0, y0, x1, y1);
}
static int draw_block_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, hb_buffer_t *hb_buf, fz_html_restarter *restart)
{
fz_html_box *child;
float x0, y0, x1, y1;
float *padding = box->u.block.padding;
int stopped = 0;
int skipping;
float cell_padding_top = box->type == BOX_TABLE_CELL ? box->u.block.margin[T] : 0;
float cell_padding_bot = box->type == BOX_TABLE_CELL ? box->u.block.margin[B] : 0;
assert(fz_html_box_has_boxes(box));
x0 = box->s.layout.x - padding[L];
y0 = box->s.layout.y - padding[T];
x1 = box->s.layout.x + box->s.layout.w + padding[R];
y1 = box->s.layout.b + padding[B];
if (y0 > page_bot || y1 < page_top)
return 0;
/* If we're skipping, is this the place we should restart? */
if (restart)
{
if (restart->start == box)
restart->start = NULL;
if (restart->end == box)
return 1;
}
if (restart && restart->end == box)
return 1;
/* Are we skipping? */
skipping = (restart && restart->start != NULL);
/* Only draw the content if it's visible */
if (box->style->visibility == V_VISIBLE)
{
int suppress;
/* We draw the background rectangle regardless if we are skipping or not, because
* we might find the end-of-skip point inside this box. If there is no content
* then the box height will be 0, so nothing will be drawn. */
if (y1 > y0)
draw_rect(ctx, dev, ctm, page_top, box->style->background_color, x0, y0 - cell_padding_top, x1, y1 + cell_padding_bot);
if (!skipping)
{
/* Draw a selection of borders. */
/* If we are restarting, don't do the bottom one yet. */
suppress = restart ? (1<<B) : 0;
do_borders(ctx, dev, ctm, page_top, box, suppress);
if (box->list_item)
draw_list_mark(ctx, box, page_top, page_bot, dev, ctm, box->list_item);
}
}
for (child = box->down; child; child = child->next)
{
if (draw_box(ctx, child, page_top, page_bot, dev, ctm, hb_buf, restart))
{
stopped = 1;
break;
}
}
if (box->style->visibility == V_VISIBLE && restart && restart->start == NULL)
{
/* We didn't draw (at least some of) the borders on the way down,
* because we were in restart mode. */
/* We never want to draw the top one. Either it will have been drawn
* before, or we were skipping at that point. */
int suppress = (1<<T);
/* If we were skipping, and we're not any more, better draw in the
* left and right ones we missed. */
suppress += (skipping && restart->start == NULL) ? 0 : ((1<<L) + (1<<R));
/* If we've stopped now, don't do the bottom one either. */
suppress += stopped ? (1<<B) : 0;
/* FIXME: background color? list mark is probably OK as we only want
* it once. */
do_borders(ctx, dev, ctm, page_top, box, suppress);
}
return stopped;
}
static int draw_table_row(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_device *dev, fz_matrix ctm, hb_buffer_t *hb_buf, fz_html_restarter *restart)
{
/* Table rows don't draw background colors or borders */
fz_html_box *child;
float y0 = box->s.layout.y;
float y1 = box->s.layout.b;
if (y0 > page_bot || y1 < page_top)
return 0;
/* If we're skipping, is this the place we should restart? */
if (restart)
{
if (restart->start == box)
restart->start = NULL;
if (restart->end == box)
return 1;
}
if (restart && restart->end == box)
return 1;
for (child = box->down; child; child = child->next)
if (draw_box(ctx, child, page_top, page_bot, dev, ctm, hb_buf, restart))
return 1;
return 0;
}
void
fz_draw_restarted_html(fz_context *ctx, fz_device *dev, fz_matrix ctm, fz_html_box *top, float page_top, float page_bot, fz_html_restarter *restart)
{
fz_html_box *box;
hb_buffer_t *hb_buf = NULL;
int unlocked = 0;
fz_var(hb_buf);
fz_var(unlocked);
fz_hb_lock(ctx);
fz_try(ctx)
{
hb_buf = hb_buffer_create();
fz_hb_unlock(ctx);
unlocked = 1;
for (box = top->down; box; box = box->next)
if (draw_box(ctx, box, page_top, page_bot, dev, ctm, hb_buf, restart))
break;
}
fz_always(ctx)
{
if (unlocked)
fz_hb_lock(ctx);
hb_buffer_destroy(hb_buf);
fz_hb_unlock(ctx);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
void
fz_draw_html(fz_context *ctx, fz_device *dev, fz_matrix ctm, fz_html *html, int page)
{
float page_top = page * html->page_h;
float page_bot = (page + 1) * html->page_h;
draw_rect(ctx, dev, ctm, 0, html->tree.root->style->background_color,
0, 0,
html->page_w + html->page_margin[L] + html->page_margin[R],
html->page_h + html->page_margin[T] + html->page_margin[B]);
ctm = fz_pre_translate(ctm, html->page_margin[L], html->page_margin[T]);
fz_draw_restarted_html(ctx, dev, ctm, html->tree.root, page_top, page_bot, NULL);
}
void fz_draw_story(fz_context *ctx, fz_story *story, fz_device *dev, fz_matrix ctm)
{
float page_top, page_bot;
fz_path *clip;
fz_rect bbox;
if (story == NULL || story->complete)
return;
bbox = story->bbox;
page_top = bbox.y0;
page_bot = bbox.y1;
if (dev)
{
clip = fz_new_path(ctx);
fz_try(ctx)
{
fz_moveto(ctx, clip, bbox.x0, bbox.y0);
fz_lineto(ctx, clip, bbox.x1, bbox.y0);
fz_lineto(ctx, clip, bbox.x1, bbox.y1);
fz_lineto(ctx, clip, bbox.x0, bbox.y1);
fz_closepath(ctx, clip);
fz_clip_path(ctx, dev, clip, 0, ctm, bbox);
}
fz_always(ctx)
fz_drop_path(ctx, clip);
fz_catch(ctx)
fz_rethrow(ctx);
}
story->restart_draw.left_page ^= 1;
story->restart_place = story->restart_draw;
if (dev)
fz_draw_restarted_html(ctx, dev, ctm, story->tree.root->down, 0, page_bot+page_top, &story->restart_place);
story->restart_place.start = story->restart_draw.end;
story->restart_place.start_flow = story->restart_draw.end_flow;
story->restart_place.start_flags = story->restart_draw.end_flags;
story->restart_place.end = NULL;
story->restart_place.end_flow = NULL;
story->restart_place.end_flags = 0;
story->rect_count++;
if (story->restart_place.start == NULL)
story->complete = 1;
if (dev)
fz_pop_clip(ctx, dev);
}
void fz_reset_story(fz_context *ctx, fz_story *story)
{
if (story == NULL)
return;
story->restart_place.start = NULL;
story->restart_place.start_flow = NULL;
story->restart_place.start_flags = 0;
story->restart_place.end = NULL;
story->restart_place.end_flow = NULL;
story->restart_place.end_flags = 0;
story->restart_place.left_page = 0;
story->restart_draw.start = NULL;
story->restart_draw.start_flow = NULL;
story->restart_draw.start_flags = 0;
story->restart_draw.end = NULL;
story->restart_draw.end_flow = NULL;
story->restart_draw.end_flags = 0;
story->restart_draw.left_page = 0;
story->rect_count = 0;
story->complete = 0;
}
static char *
gather_text(fz_context *ctx, fz_html_box *box)
{
fz_html_flow *node;
char *text = NULL;
fz_var(text);
fz_try(ctx)
{
for (node = box->u.flow.head; node; node = node->next)
{
const fz_css_style *style = node->box->style;
if (node->type == FLOW_WORD || node->type == FLOW_SPACE || node->type == FLOW_SHYPHEN)
{
const char *s;
if (node->type == FLOW_SPACE && node->breaks_line)
continue;
if (node->type == FLOW_SHYPHEN && !node->breaks_line)
continue;
if (style->visibility != V_VISIBLE)
continue;
s = get_node_text(ctx, node);
if (text)
{
size_t newsize = strlen(text) + strlen(s) + 1;
text = fz_realloc(ctx, text, newsize);
strcat(text, s);
}
else
{
text = fz_strdup(ctx, s);
}
}
else if (node->type == FLOW_IMAGE)
{
}
}
}
fz_catch(ctx)
{
fz_free(ctx, text);
fz_rethrow(ctx);
}
return text;
}
static int enumerate_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_story_position_callback *cb, void *arg, int depth, int rect_num, fz_html_restarter *restart);
static int enumerate_block_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_story_position_callback *cb, void *arg, int depth, int rect_num, fz_html_restarter *restart)
{
fz_html_box *child;
float y0, y1;
float *padding = box->u.block.padding;
int stopped = 0;
int skipping;
fz_story_element_position pos;
int heading;
assert(fz_html_box_has_boxes(box));
y0 = box->s.layout.y - padding[T];
y1 = box->s.layout.b + padding[B];
if (y0 > page_bot || y1 < page_top)
return 0;
/* If we're skipping, is this the place we should restart? */
if (restart)
{
if (restart->start == box)
restart->start = NULL;
if (restart->end == box)
return 1;
}
if (restart && restart->end == box)
return 1;
/* Are we skipping? */
skipping = (restart && restart->start != NULL);
if (box->style->visibility == V_VISIBLE && !skipping)
{
heading = box->heading;
if (heading || box->id != NULL || box->href)
{
/* We have a box worthy of a callback. */
char *text = NULL;
pos.text = NULL;
if (heading && box->down)
pos.text = text = gather_text(ctx, box->down);
pos.depth = depth;
pos.heading = heading;
pos.open_close = 1;
pos.id = box->id;
pos.href = box->href;
pos.rect.x0 = box->s.layout.x;
pos.rect.y0 = box->s.layout.y;
pos.rect.x1 = box->s.layout.x + box->s.layout.w;
pos.rect.y1 = box->s.layout.b;
pos.rectangle_num = rect_num;
fz_try(ctx)
cb(ctx, arg, &pos);
fz_always(ctx)
fz_free(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
pos.text = NULL;
}
}
for (child = box->down; child; child = child->next)
{
if (enumerate_box(ctx, child, page_top, page_bot, cb, arg, depth+1, rect_num, restart))
{
stopped = 1;
break;
}
}
if (box->style->visibility == V_VISIBLE && !skipping)
{
if (heading || box->id != NULL || box->href)
{
/* We have a box worthy of a callback that needs closing. */
pos.open_close = 2;
pos.rectangle_num = rect_num;
cb(ctx, arg, &pos);
}
}
return stopped;
}
static int enumerate_flow_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_story_position_callback *cb, void *arg, int depth, int rect_num, fz_html_restarter *restart)
{
fz_html_flow *node;
int restartable_ended = 0;
/* FIXME: HB_DIRECTION_TTB? */
if (restart && restart->start != NULL && restart->start != box)
return 0;
for (node = box->u.flow.head; node; node = node->next)
{
const fz_css_style *style = node->box->style;
if (restart)
{
if (restart->start_flow != NULL)
{
if (restart->start_flow != node)
continue;
restart->start = NULL;
restart->start_flow = NULL;
}
if (restart->end == box && restart->end_flow == node)
{
restartable_ended = 1;
break;
}
}
if (node->type == FLOW_IMAGE)
{
if (node->y >= page_bot || node->y + node->h <= page_top)
continue;
}
else
{
if (node->y > page_bot || node->y < page_top)
continue;
}
if (node->box->id || node->box->href)
{
/* We have a node to callback for. */
fz_story_element_position pos;
pos.text = NULL;
pos.depth = depth;
pos.heading = 0;
pos.open_close = 1 | 2;
pos.id = node->box->id;
pos.href = node->box->href;
/* We only have the baseline and the em, so the bbox is a bit of a fudge. */
pos.rect.x0 = node->x;
pos.rect.y0 = node->y - node->h * 0.8f;
pos.rect.x1 = node->x + node->w;
pos.rect.y1 = node->y + node->h * 0.2f;
pos.rectangle_num = rect_num;
cb(ctx, arg, &pos);
}
if (node->type == FLOW_WORD || node->type == FLOW_SPACE || node->type == FLOW_SHYPHEN)
{
}
else if (node->type == FLOW_IMAGE)
{
if (style->visibility == V_VISIBLE)
{
/* FIXME: Maybe callback for images? */
}
}
}
return restartable_ended;
}
static int enumerate_box(fz_context *ctx, fz_html_box *box, float page_top, float page_bot, fz_story_position_callback *cb, void *arg, int depth, int rect_num, fz_html_restarter *restart)
{
switch (box->type)
{
case BOX_TABLE:
case BOX_TABLE_ROW:
case BOX_TABLE_CELL:
case BOX_BLOCK:
if (restart && restart->end == box)
return 1;
if (enumerate_block_box(ctx, box, page_top, page_bot, cb, arg, depth, rect_num, restart))
return 1;
break;
case BOX_FLOW:
if (enumerate_flow_box(ctx, box, page_top, page_bot, cb, arg, depth, rect_num, restart))
return 1;
break;
}
return 0;
}
void fz_story_positions(fz_context *ctx, fz_story *story, fz_story_position_callback *cb, void *arg)
{
float page_top, page_bot;
fz_html_box *b;
fz_html_restarter restart;
fz_html_box *box;
fz_html_box *top;
if (story == NULL || story->complete)
return;
b = story->tree.root;
page_top = b->s.layout.y - b->u.block.margin[T] - b->u.block.border[T] - b->u.block.padding[T];
page_bot = b->s.layout.b + b->u.block.margin[B] + b->u.block.border[B] + b->u.block.padding[B];
top = story->tree.root->down;
restart = story->restart_draw;
for (box = top->down; box; box = box->next)
if (enumerate_box(ctx, box, page_top, page_bot, cb, arg, 0, story->rect_count+1, &restart))
break;
}
|