DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Untracked file

Line Code
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-R
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.0 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 *
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

/*
 * layimage.c - Image layout and fetching/prefetching
 *
 */

#include "xp.h"
#include "net.h"
#include "xp_rgb.h"
#include "pa_parse.h"
#include "layout.h"
#include "laylayer.h"
#include "laystyle.h"
#include "secnav.h"
#include "prefapi.h"
#include "xlate.h"
#include "layers.h"
#ifdef DOM
#include "domstyle.h"
#include "lm_dom.h"
#include "laydom.h"
#endif

extern int MK_OUT_OF_MEMORY;

#define IL_CLIENT               /* Defined by Image Library clients */
#include "libimg.h"             /* Image Library public API. */

#include "libevent.h"

#include "timing.h"

#ifdef PROFILE
#pragma profile on
#endif

#define IMAGE_DEF_DIM			50
#define IMAGE_DEF_BORDER		0
#define IMAGE_DEF_ANCHOR_BORDER		2
#define IMAGE_DEF_VERTICAL_SPACE	0
#define IMAGE_DEF_HORIZONTAL_SPACE	0
#define IMAGE_DEF_FLOAT_HORIZONTAL_SPACE	3

/* Closure data to be passed into lo_ImageObserver. */
typedef struct lo_ImageObsClosure {
    MWContext *context;
    LO_ImageStruct *lo_image;
    XP_ObserverList obs_list;
} lo_ImageObsClosure;

extern LO_TextStruct *lo_AltTextElement(MWContext *context);

PUBLIC char *
LO_GetBookmarkIconURLForPage(MWContext *context, BMIconType type)
{
	int32 doc_id;
	lo_TopState *top_state;

	/*
	 * Get the unique document ID, and retreive this
	 * documents layout state.
	 */
	doc_id = XP_DOCID(context);
	top_state = lo_FetchTopState(doc_id);
	if (!top_state)
		return NULL;

	if(type == SMALL_BM_ICON)
		return top_state->small_bm_icon;
	else if(type == SMALL_BM_ICON)
		return top_state->large_bm_icon;

	return NULL;
}


/* Primitive image allocator also sets defaults for image structure */
static LO_ImageStruct *
lo_new_image_element(MWContext *context, lo_DocState *state,
                     void *edit_element, LO_TextAttr *tptr)
{
    LO_ImageStruct *image;
    image = (LO_ImageStruct *)lo_NewElement(context, state, LO_IMAGE, edit_element, 0);
    if (image == NULL)
        return NULL;

    /*
     * preserve the edit element and offset across the bzero.
     * The edit_element and edit_offset were set in lo_NewElement.
     */
    {
        void* save_edit_element = image->edit_element;
        int32 save_edit_offset = image->edit_offset;
        XP_BZERO(image, sizeof(*image));
        image->edit_element = save_edit_element;
        image->edit_offset = save_edit_offset;
    }

    image->type = LO_IMAGE;

    /*
     * Fill in default font information.
     */
    if (tptr == NULL)
    {
        LO_TextAttr tmp_attr;

        lo_SetDefaultFontAttr(state, &tmp_attr, context);
        tptr = lo_FetchTextAttr(state, &tmp_attr);
    }
    image->text_attr = tptr;
    image->image_attr = XP_NEW(LO_ImageAttr);
    if (image->image_attr == NULL)
    {	/* clean up all previously allocated objects */
        lo_FreeElement(context, (LO_Element *)image, FALSE);
        state->top_state->out_of_memory = TRUE;
        return 0;
    }

    image->image_attr->attrmask = 0;
    image->image_attr->layer_id = LO_DOCUMENT_LAYER_ID;
    image->image_attr->form_id = -1;
    image->image_attr->alignment = LO_ALIGN_BASELINE;
    image->image_attr->usemap_name = NULL;
    image->image_attr->usemap_ptr = NULL;
    
    image->ele_attrmask = 0;

    image->sel_start = -1;
    image->sel_end = -1;

    image->is_icon = FALSE;
	image->image_status = IL_START_URL;
	
    return image; 
}

PRIVATE
Bool
lo_parse_rgb(char *rgb, uint8 *red_ptr, uint8 *green_ptr, uint8 *blue_ptr, XP_Bool double_three_byte_codes)
{
	char *ptr;
	int32 i, j, len;
	int32 val, bval;
	int32 red_val, green_val, blue_val;
	intn bytes_per_val;

	*red_ptr = 0;
	*green_ptr = 0;
	*blue_ptr = 0;
	red_val = 0;
	green_val = 0;
	blue_val = 0;

	if (rgb == NULL)
	{
		return FALSE;
	}

	len = XP_STRLEN(rgb);
	if (len == 0)
	{
		return FALSE;
	}

	/*
	 * Strings not starting with a '#' are probably named colors.
	 * look them up in the xp lookup table.
	 */
	ptr = rgb;
	if (*ptr == '#')
	{
		ptr++;
		len--;
	}
	else
	{
		/*
		 * If we successfully look up a color name, return its RGB.
		 */
		if (XP_ColorNameToRGB(ptr, red_ptr, green_ptr, blue_ptr) == 0)
		{
			return TRUE;
		}
	}

	if (len == 0)
	{
		return FALSE;
	}

	bytes_per_val = (intn)((len + 2) / 3);
	if (bytes_per_val > 4)
	{
		bytes_per_val = 4;
	}

	for (j=0; j<3; j++)
	{
		val = 0;
		for (i=0; i<bytes_per_val; i++)
		{
			if (*ptr == '\0')
			{
				bval = 0;
			}
			else
			{
				bval = TOLOWER((unsigned char)*ptr);
				if ((bval >= '0')&&(bval <= '9'))
				{
					bval = bval - '0';
				}
				else if ((bval >= 'a')&&(bval <= 'f'))
				{
					bval = bval - 'a' + 10;
				}
				else
				{
					bval = 0;
				}
				ptr++;
			}
			val = (val << 4) + bval;
		}
		if (j == 0)
		{
			red_val = val;
		}
		else if (j == 1)
		{
			green_val = val;
		}
		else
		{
			blue_val = val;
		}
	}

	if(double_three_byte_codes && bytes_per_val == 1)
	{
		red_val = (red_val << 4) + red_val;
		green_val = (green_val << 4) + green_val;
		blue_val = (blue_val << 4) + blue_val;
	}

	while ((red_val > 255)||(green_val > 255)||(blue_val > 255))
	{
		red_val = (red_val >> 4);
		green_val = (green_val >> 4);
		blue_val = (blue_val >> 4);
	}
	*red_ptr = (uint8)red_val;
	*green_ptr = (uint8)green_val;
	*blue_ptr = (uint8)blue_val;
	return TRUE;
}

Bool
LO_ParseRGB(char *rgb, uint8 *red_ptr, uint8 *green_ptr, uint8 *blue_ptr)
{
	return lo_parse_rgb(rgb, red_ptr, green_ptr, blue_ptr, FALSE);
}


/* parse a comma separated list of 3 values
 *
 * Format is "rgb(red, green, blue)"
 * color values can be in integer 1-255 or percent 1%-100%
 * illegal values must be truncated 
 *
 * if there are fewer than three arguments this function will
 * set colors for whatever number exist.
 * if more than 3 args are passed in they will be ignored
 */
static Bool
lo_parse_style_RGB_functional_notation(char *rgb, 
		     uint8 *red_ptr, 
		     uint8 *green_ptr, 
		     uint8 *blue_ptr)
{
	int index=0;
	char *value, *end;
	int num_value;

	rgb = XP_StripLine(rgb);

    /* go past rgb(  and remove the last ')' */
#define _RGB "rgb"
    if(!strncasecomp(rgb, _RGB, sizeof(_RGB)-1))
    {
        char *end;

        /* go past "RGB" */
        rgb += sizeof(_RGB)-1;

		/* go past spaces */
		while(XP_IS_SPACE(*rgb)) rgb++;

		/* go past '(' */
		if(*rgb == '(')
			rgb++;

		/* kill any more spaces */
		while(XP_IS_SPACE(*rgb)) rgb++;

        end = &rgb[XP_STRLEN(rgb)-1];
        if(*end == ')')
            *end = '\0';
    }

	value = XP_STRTOK(rgb, ",");

	while(value && index < 3)
	{
		int scaled_value;

		value = XP_StripLine(value);

		num_value = XP_ATOI(value);

		if(num_value < 0)
			num_value = 0;

        end = &rgb[XP_STRLEN(rgb)-1];
        if(*end == '%')
		{
			/* percentage value */

			if(num_value > 100)
				num_value = 100;

			scaled_value = (255 * num_value) / 100;
			
		}
		else
		{
			if(num_value > 255)
				num_value = 255;

			scaled_value = num_value;
		}

		if(index == 0)
			*red_ptr = scaled_value;
		else if(index == 1)
			*green_ptr = scaled_value;
		else if(index == 2)
			*blue_ptr = scaled_value;

		index++;
		value = XP_STRTOK(NULL, ",");
	}

	if(index < 3)
		return FALSE;
	else
		return TRUE;
}

Bool
LO_ParseStyleSheetRGB(char *rgb, uint8 *red_ptr, uint8 *green_ptr, uint8 *blue_ptr)
{
	/* can take the form of:
	 * #f00
	 * #ff0000
	 * or rgb(255, 0, 0)
	 * or rgb(100%, 0%, 0%)
	 * all are equivalent values
	 * need to truncate illegal values like 265 or 110%
	 */

	rgb = XP_StripLine(rgb);

	if(!strcasecomp(rgb, "transparent"))
	{
		*red_ptr = 0;
		*green_ptr = 0;
		*blue_ptr = 0;
		return FALSE;
	}
	else if(!strncasecomp(rgb, _RGB, sizeof(_RGB)-1)) /* functional notation */
	{
		return lo_parse_style_RGB_functional_notation(rgb, 
													  red_ptr,
													  green_ptr,
													  blue_ptr);
	}
	else /* assume #FF0000 or #F00 notation */
	{
		return lo_parse_rgb(rgb, red_ptr, green_ptr, blue_ptr, TRUE);
	}
}

/* 
 * Parses the BACKGROUND attribute (if one exists) and returns a url.
 * If there is no attribute specified, it returns NULL. For code sharing
 * between tags that have a BACKGROUND attribute.
 */
char *
lo_ParseBackgroundAttribute(MWContext *context, lo_DocState *state, 
                            PA_Tag *tag, Bool from_user)
{
	PA_Block buff, image_url;
	char *str;

	/*
	 * Get the required src parameter, and make the resulting
	 * url and absolute url.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_BACKGROUND);
	if (buff != NULL)
	{
		PA_Block new_buff;
		char *new_str;

		PA_LOCK(str, char *, buff);
		if (str != NULL)
		{
			int32 len;

			len = lo_StripTextWhitespace(str, XP_STRLEN(str));
		}
		/*
		 * Do not allow BACKGROUND=""
		 */
		if ((str != NULL)&&(*str != '\0'))
		{
			new_str = NET_MakeAbsoluteURL(
					state->top_state->base_url, str);
		}
		else
		{
			new_str = NULL;
		}
		if (new_str == NULL)
		{
			new_buff = NULL;
		}
		else
		{
			char *url;

			new_buff = PA_ALLOC(XP_STRLEN(new_str) + 1);
			if (new_buff != NULL)
			{
				PA_LOCK(url, char *, new_buff);
				XP_STRCPY(url, new_str);
				PA_UNLOCK(new_buff);
			}
			else
			{
				state->top_state->out_of_memory = TRUE;
			}
			XP_FREE(new_str);
		}
		PA_UNLOCK(buff);
		PA_FREE(buff);
		image_url = new_buff;

        /*
         * Make sure we don't load insecure images inside
         * a secure document.
         */
        if (state->top_state->security_level > 0)
        {
            PA_LOCK(str, char *, image_url);
            
            if (((from_user == FALSE)&&(str != NULL))||
                ((from_user != FALSE)&&(str != NULL)&&
                 (XP_STRNCMP(str, "file:/", 6) != 0)))
             {
                 if (NET_IsURLSecure(str) == FALSE)
                 {
                     PA_UNLOCK(image_url);
                     PA_FREE(image_url);
                     image_url = PA_ALLOC(
                         XP_STRLEN("internal-icon-insecure") + 1);
                     PA_LOCK(str, char *, image_url);
                     if (image_url != NULL)
                     {
                         XP_STRCPY(str, "internal-icon-insecure");
                     }
                     else
                     {
                         state->top_state->out_of_memory = TRUE;
                     }
                     if (state->top_state->insecure_images == FALSE)
                     {
                         state->top_state->insecure_images = TRUE;
                         SECNAV_SecurityDialog(context,
											   SD_INSECURE_DOCS_WITHIN_SECURE_DOCS_NOT_SHOWN);
                     }
                 }
             }
            PA_UNLOCK(image_url);
        }
        
        return (char*)image_url;
	}
    else
        return NULL;
}


void
lo_BodyForeground(MWContext *context, lo_DocState *state, PA_Tag *tag)
{
	PA_Block buff;
	char *str;
	uint8 red, green, blue;

	/*
	 * Don't do this is we had a TEXT from a previous BODY tag.
	 */
	if ((state->top_state->body_attr & BODY_ATTR_TEXT) == 0)
	{
		/*
		 * Get the document text TEXT parameter.
		 */
		buff = lo_FetchParamValue(context, tag, PARAM_TEXT);
		if (buff != NULL)
		{
            LO_Color text_fg;
            
			PA_LOCK(str, char *, buff);
			LO_ParseRGB(str, &red, &green, &blue);
			PA_UNLOCK(buff);
			PA_FREE(buff);

			text_fg.red = red;
			text_fg.green = green;
			text_fg.blue = blue;
            /* Last param tells this to check state->top_state->body_attr
               state to suppress changing color during layout
            */
            lo_ChangeBodyTextFGColor(context, state, &text_fg);
		}
	}

	/*
	 * Don't do this is we had a LINK from a previous BODY tag.
	 */
	if ((state->top_state->body_attr & BODY_ATTR_LINK) == 0)
	{
		/*
		 * Get the anchor text LINK parameter.
		 */
		buff = lo_FetchParamValue(context, tag, PARAM_LINK);
		if (buff != NULL)
		{
			state->top_state->body_attr |= BODY_ATTR_LINK;
			PA_LOCK(str, char *, buff);
			LO_ParseRGB(str, &red, &green, &blue);
			PA_UNLOCK(buff);
			PA_FREE(buff);

			state->anchor_color.red = red;
			state->anchor_color.green = green;
			state->anchor_color.blue = blue;
		}
	}

	/*
	 * Don't do this is we had a VLINK from a previous BODY tag.
	 */
	if ((state->top_state->body_attr & BODY_ATTR_VLINK) == 0)
	{
		/*
		 * Get the visited anchor text VLINK parameter.
		 */
		buff = lo_FetchParamValue(context, tag, PARAM_VLINK);
		if (buff != NULL)
		{
			state->top_state->body_attr |= BODY_ATTR_VLINK;
			PA_LOCK(str, char *, buff);
			LO_ParseRGB(str, &red, &green, &blue);
			PA_UNLOCK(buff);
			PA_FREE(buff);

			state->visited_anchor_color.red = red;
			state->visited_anchor_color.green = green;
			state->visited_anchor_color.blue = blue;
		}
	}

	/*
	 * Don't do this is we had a ALINK from a previous BODY tag.
	 */
	if ((state->top_state->body_attr & BODY_ATTR_ALINK) == 0)
	{
		/*
		 * Get the active anchor text ALINK parameter.
		 */
		buff = lo_FetchParamValue(context, tag, PARAM_ALINK);
		if (buff != NULL)
		{
			state->top_state->body_attr |= BODY_ATTR_ALINK;
			PA_LOCK(str, char *, buff);
			LO_ParseRGB(str, &red, &green, &blue);
			PA_UNLOCK(buff);
			PA_FREE(buff);

			state->active_anchor_color.red = red;
			state->active_anchor_color.green = green;
			state->active_anchor_color.blue = blue;
		}
	}
}


static void
lo_GetStyleSheetBodyBackgroundAndColor(MWContext *context,
                                       lo_DocState *state, 
                                       char **image_urlp,
                                       char **color_str,
									   lo_TileMode *tile_mode)
{
    char *image_url;
    char *absolute_image_url;
	char *tile_mode_prop;
	StyleStruct *ss;

    /* Set defaults in case something goes wrong below. */
    *color_str = NULL;
    *image_urlp = NULL;
    
    /* //CLM: This is null in Editor (sometimes?)*/
    if(!state->top_state->style_stack)
        return;
  
	/* get the top style */
	ss = STYLESTACK_GetStyleByIndex(state->top_state->style_stack, 0);

	/* see if it has a bgcolor attribute */
	if(!ss)
		return;

	*color_str = STYLESTRUCT_GetString(ss, BG_COLOR_STYLE);

	image_url = STYLESTRUCT_GetString(ss, BG_IMAGE_STYLE);

	if(!image_url)
		return;

	if(!strcasecomp(image_url, "none"))
	{
		XP_FREE(image_url);
		return;	
	}

	absolute_image_url = NET_MakeAbsoluteURL(state->top_state->base_url, 
											 lo_ParseStyleSheetURL(image_url));
	XP_FREE(image_url);

	if(!absolute_image_url)
		return;
    
    *image_urlp = absolute_image_url;

	/* get the tiling mode */
	tile_mode_prop = STYLESTRUCT_GetString(ss, BG_REPEAT_STYLE);
	if(tile_mode_prop)
	{
		if(!strcasecomp(tile_mode_prop, BG_REPEAT_NONE_STYLE))
			*tile_mode = LO_NO_TILE;
		else if(!strcasecomp(tile_mode_prop, BG_REPEAT_X_STYLE))
			*tile_mode = LO_TILE_HORIZ;
		else if(!strcasecomp(tile_mode_prop, BG_REPEAT_Y_STYLE))
			*tile_mode = LO_TILE_VERT;
		else 
			*tile_mode = LO_TILE_BOTH;
	}
}


void
lo_BodyBackground(MWContext *context, lo_DocState *state, PA_Tag *tag,
	Bool from_user)
{
    LO_Color rgb;
	PA_Block buff=NULL;
	char *color_str = NULL;
	char *image_url = NULL;
    CL_Layer *layer;
	lo_TileMode tile_mode = LO_TILE_BOTH;

    if (! context->compositor)
        return;

	/* if we are here then we must be in the BODY tag.
	 * Get the top state from the style sheet and get BG color and
	 * BG Image from it
	 */
	lo_GetStyleSheetBodyBackgroundAndColor(context,
										   state, 
										   &image_url,
										   &color_str,
										   &tile_mode);

    /* If there isn't a style sheet backdrop get one from the BODY's
       BACKGROUND attribute, if present */
    if (!image_url)
        image_url = lo_ParseBackgroundAttribute(context, state, tag, from_user);

	/*
	 * Get the BGCOLOR parameter.  This is for a color background
	 * instead of an image backdrop.  BACKGROUND if it exists overrides
	 * the BGCOLOR specification.
	 *
	 * If color_str is already set then we got the color from a stylesheet
	 */
	if(!color_str)
	{
		buff = lo_FetchParamValue(context, tag, PARAM_BGCOLOR);
        if (buff) {
            PA_LOCK(color_str, char *, buff);
            LO_ParseRGB(color_str, &rgb.red, &rgb.green, &rgb.blue);
            PA_UNLOCK(buff);
            PA_FREE(buff);
        }
	}
	else
    {
        LO_ParseStyleSheetRGB(color_str, &rgb.red, &rgb.green, &rgb.blue);
		XP_FREE(color_str);
	}

    layer = lo_CurrentLayer(state);

    /*
     * Don't do this is we had a BGCOLOR from a previous BODY tag.
     */
    if (((state->top_state->body_attr & BODY_ATTR_BGCOLOR) == 0)||
	(from_user != FALSE))
    {
	if ((color_str != NULL)&&(!lo_InsideLayer(state))&&(from_user == FALSE))
	{
		state->top_state->body_attr |= BODY_ATTR_BGCOLOR;
	}
	/* Set the document's solid background color */
	if (color_str && !lo_InsideLayer(state)) {
		state->top_state->doc_specified_bg = TRUE;
		state->text_bg.red = rgb.red;
		state->text_bg.green = rgb.green;
		state->text_bg.blue = rgb.blue;
	}
    
	if (color_str)
		LO_SetLayerBgColor(layer, &rgb);
	else if (!lo_InsideLayer(state))
		LO_SetLayerBgColor(layer, &state->text_bg);
    }
    
    /*
     * Don't do this is we had a BACKGROUND from a previous BODY tag.
     */
    if (((state->top_state->body_attr & BODY_ATTR_BACKGROUND) == 0)||
	(from_user != FALSE))
    {
	if ((image_url != NULL)&&(!lo_InsideLayer(state))&&(from_user == FALSE))
	{
		state->top_state->body_attr |= BODY_ATTR_BACKGROUND;
	}
	/* Set the document's backdrop */
	if (image_url) {
		if (!lo_InsideLayer(state))
			state->top_state->doc_specified_bg = TRUE;
		LO_SetLayerBackdropURL(layer, image_url);
		lo_SetLayerBackdropTileMode(layer, tile_mode);
		XP_FREE(image_url);
	}
    }
    
    lo_BodyForeground(context, state, tag);
}

static void lo_edt_AvoidImageBlock(LO_ImageStruct *image);

static void lo_edt_AvoidImageBlock(LO_ImageStruct *image)
{
    int originalWidth = 0;
    int originalHeight = 0;
    IL_GetNaturalDimensions( image->image_req, &originalWidth, &originalHeight);
    if ( originalWidth <= 0 ) {
        originalWidth = 10;
    }
    if ( originalHeight <= 0 ) {
        originalHeight = 10;
    }
    if (image->width == 0 )
    {
        image->width = originalWidth;
    }
    if (image->height == 0)
    {
        image->height = originalHeight;
    }
}

extern Bool
lo_FindMochaExpr(char *str, char **expr_start, char **expr_end);

void
lo_BlockedImageLayout(MWContext *context, lo_DocState *state, PA_Tag *tag,
                      char *base_url)
{
	LO_ImageStruct *image;
	PA_Block buff;
	char *str;
	int32 val;
	/* int32 doc_width; */
	PushTagStatus push_status;
	char *start, *end;
	
    XP_ObserverList image_obs_list;   /* List of observers for an image request. */

	/* this image layout is going on outside the context
	 * of normal layout.  We therfore need to manage the
	 * style stack in here as well as in normal layout
	 * mode so that we can set image widths
	 */
	push_status = LO_PushTagOnStyleStack(context, state, tag);

	/* 
	 * if there are any entities in this tag don't prefetch
	 *   it --- wait until we have had a chance to redo it
	 */
	if (lo_FindMochaExpr((char *)tag->data, &start, &end))
	    return;

	/*
	 * Fill in the image structure with default data
	 */
    image = lo_new_image_element(context, state, tag->edit_element, NULL);
    if (!image)
        return;
	image->ele_id = NEXT_ELEMENT;

	image->border_width = -1;
	image->border_vert_space = IMAGE_DEF_VERTICAL_SPACE;
	image->border_horiz_space = IMAGE_DEF_HORIZONTAL_SPACE;

	/*
	 * Check for the ISMAP parameter
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_ISMAP);
	if (buff != NULL)
	{
		image->image_attr->attrmask |= LO_ATTR_ISMAP;
		PA_FREE(buff);
	}

	/*
	 * Check for the USEMAP parameter.  The name is stored in the
	 * image attributes immediately, the map pointer will be
	 * stored in later.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_USEMAP);
	if (buff != NULL)
	{
		char *new_str;
		char *map_name;
		char *tptr;

		map_name = NULL;
		PA_LOCK(str, char *, buff);

		/*
		 * Make this an absolute URL
		 */
		if (str != NULL)
		{
			int32 len;

			len = lo_StripTextWhitespace(str, XP_STRLEN(str));
		}
		/*
		 * The IETF draft says evaluate #name relative to real,
		 * and not the base url.
		 */
		if ((str != NULL)&&(*str == '#'))
		{
			new_str = NET_MakeAbsoluteURL(
				state->top_state->url, str);
		}
		else
		{
			new_str = NET_MakeAbsoluteURL(
				base_url, str);
		}

		/*
		 * If we have a local URL, we can use the USEMAP.
		 */
		if ((new_str != NULL)&&
			(lo_IsAnchorInDoc(state, new_str) != FALSE))
		{
			tptr = strrchr(new_str, '#');
			if (tptr == NULL)
			{
				tptr = new_str;
			}
			else
			{
				tptr++;
			}
			map_name = XP_STRDUP(tptr);
			XP_FREE(new_str);
		}
		/*
		 * Else a non-local URL, right now we don't support this.
		 */
		else if (new_str != NULL)
		{
			XP_FREE(new_str);
		}
		PA_UNLOCK(buff);
		PA_FREE(buff);
		image->image_attr->usemap_name = map_name;
	}

	/*
	 * Check for an align parameter
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_ALIGN);
	if (buff != NULL)
	{
		Bool floating;

		floating = FALSE;
		PA_LOCK(str, char *, buff);
		image->image_attr->alignment = lo_EvalAlignParam(str,
			&floating);
		if (floating != FALSE)
		{
			image->ele_attrmask |= LO_ELE_FLOATING;
		}
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}

	if(state->top_state->style_stack)
	{
		Bool floating = FALSE;

		lo_EvalStyleSheetAlignment(
				STYLESTACK_GetStyleByIndex(state->top_state->style_stack, 0), 
				&(image->image_attr->alignment),
				&floating);	 
		
		if (floating != FALSE)
		{
			image->ele_attrmask |= LO_ELE_FLOATING;
		}
	}

	/*
	 * Get the required src parameter, and make the resulting
	 * url and absolute url.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_SRC);
	if (buff != NULL)
	{
		PA_Block new_buff;
		char *new_str;

		new_buff = NULL;
		PA_LOCK(str, char *, buff);
		if (str != NULL)
		{
			int32 len;

			len = lo_StripTextWhitespace(str, XP_STRLEN(str));
		}
		new_str = NET_MakeAbsoluteURL(base_url, str);
		if (new_str != NULL)
		{
			char *url;

			new_buff = PA_ALLOC(XP_STRLEN(new_str) + 1);
			if (new_buff != NULL)
			{
				PA_LOCK(url, char *, new_buff);
				XP_STRCPY(url, new_str);
				PA_UNLOCK(new_buff);
			}
			XP_FREE(new_str);
		}
		PA_UNLOCK(buff);
		PA_FREE(buff);
		buff = new_buff;
		
		if (new_str == NULL || new_buff == NULL)
		{	/* clean up all previously allocated objects */
			lo_FreeElement(context, (LO_Element *)image, TRUE);
			state->top_state->out_of_memory = TRUE;
			return;
		}
	}
	image->image_url = buff;

	/*
	 * Get the optional lowsrc parameter, and make the resulting
	 * url and absolute url.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_LOWSRC);
	if (buff != NULL)
	{
	    PA_Block new_buff;
	    char *new_str;

	    new_buff = NULL;
	    PA_LOCK(str, char *, buff);
	    if (str != NULL)
	    {
		int32 len;

		len = lo_StripTextWhitespace(str, XP_STRLEN(str));
	    }
	    new_str = NET_MakeAbsoluteURL(base_url, str);
	    if (new_str != NULL)
	    {
		    char *url;

		    new_buff = PA_ALLOC(XP_STRLEN(new_str) + 1);
		    if (new_buff != NULL)
		    {
			    PA_LOCK(url, char *, new_buff);
			    XP_STRCPY(url, new_str);
			    PA_UNLOCK(new_buff);
		    }
		    XP_FREE(new_str);
	    }
	    PA_UNLOCK(buff);
	    PA_FREE(buff);
	    buff = new_buff;

	    if (new_str == NULL || new_buff == NULL)
	    {       /* clean up all previously allocated objects */
		    lo_FreeElement(context, (LO_Element *)image, TRUE);
		    state->top_state->out_of_memory = TRUE;
		    return;
	    }
	}
	image->lowres_image_url = buff;

	/*
	 * Get the alt parameter, and store the resulting
	 * text, and its length.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_ALT);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		image->alt_len = XP_STRLEN(str);
		image->alt_len = (int16)lo_StripTextNewlines(str,
						(int32)image->alt_len);
		PA_UNLOCK(buff);
	}
	image->alt = buff;

	/*
	 * Get the suppress feedback parameter, which suppresses the delayed icon,
     * temporary border and alt text when the image is coming in, and also
     * suppresses keyboard navigation feedback.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_SUPPRESS);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);

        if (!XP_STRNCASECMP(str, "true", 4))
            image->suppress_mode = LO_SUPPRESS;
        else if (!XP_STRNCASECMP(str, "false", 5))
            image->suppress_mode = LO_DONT_SUPPRESS;
        else
            image->suppress_mode = LO_SUPPRESS_UNDEFINED;

		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	
	/*
	doc_width = state->right_margin - state->left_margin;
	*/

	/*
	 * Get the width parameter, in absolute or percentage.
	 * If percentage, make it absolute.
	 */
	/*
	buff = lo_FetchParamValue(context, tag, PARAM_WIDTH);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			if (state->allow_percent_width == FALSE)
			{
				val = 0;
			}
			else
			{
				val = doc_width * val / 100;
			}
		}
		else
		{
			val = FEUNITS_X(val, context);
		}
		image->width = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	*/

	buff = lo_FetchParamValue(context, tag, PARAM_WIDTH);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			image->percent_width = val;
		}
		else
		{
			image->percent_width = 0;
			val = FEUNITS_X(val, context);
		}
		if (val < 0)
			val = 0;
		image->width = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	

	/* disabled because it is broken by JS threading 
	 *
	 * val = LO_GetWidthFromStyleSheet(context, state);
	 * if(val)
	 *	image->width = val;
	 */

	/*
	 * Get the height parameter, in absolute or percentage.
	 * If percentage, make it absolute.
	 */

	/*
	buff = lo_FetchParamValue(context, tag, PARAM_HEIGHT);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			if (state->allow_percent_height == FALSE)
			{
				val = 0;
			}
			else
			{
				val = state->win_height * val / 100;
			}
		}
		else
		{
			val = FEUNITS_Y(val, context);
		}
		image->height = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	*/

	buff = lo_FetchParamValue(context, tag, PARAM_HEIGHT);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			image->percent_height = val;
		}
		else
		{
			image->percent_height = 0;
			val = FEUNITS_Y(val, context);
		}
		if (val < 0)
			val = 0;
		image->height = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}

	/* disabled because it is broken by JS threading 
	 *
	 * val = LO_GetHeightFromStyleSheet(context, state);
	 * if(val)
	 *	image->height = val;
	 */

	/*
	 * Get the border parameter.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_BORDER);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		val = XP_ATOI(str);
		if (val < 0)
		{
			val = 0;
		}
		image->border_width = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	image->border_width = FEUNITS_X(image->border_width, context);

	/*
	 * Get the extra vertical space parameter.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_VSPACE);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		val = XP_ATOI(str);
		if (val < 0)
		{
			val = 0;
		}
		image->border_vert_space = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	image->border_vert_space = FEUNITS_Y(image->border_vert_space, context);

	/*
	 * Get the extra horizontal space parameter.
	 */
	if (image->ele_attrmask & LO_ELE_FLOATING)
	{
		image->border_horiz_space = IMAGE_DEF_FLOAT_HORIZONTAL_SPACE;
	}
	else
	{
		image->border_horiz_space = IMAGE_DEF_HORIZONTAL_SPACE;
	}
	buff = lo_FetchParamValue(context, tag, PARAM_HSPACE);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		val = XP_ATOI(str);
		if (val < 0)
		{
			val = 0;
		}
		image->border_horiz_space = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	image->border_horiz_space = FEUNITS_X(image->border_horiz_space,
						context);

	lo_FillInImageGeometry( state, image );

	/*
	 * Make sure we don't load insecure images inside
	 * a secure document.
	 */
	if (state->top_state->security_level > 0)
	{
		if (image->image_url != NULL)
		{
			PA_LOCK(str, char *, image->image_url);
			if (NET_IsURLSecure(str) == FALSE)
			{
				PA_UNLOCK(image->image_url);
				PA_FREE(image->image_url);
				image->image_url = PA_ALLOC(
				    XP_STRLEN("internal-icon-insecure") + 1);
				if (image->image_url != NULL)
				{
				    PA_LOCK(str, char *, image->image_url);
				    XP_STRCPY(str, "internal-icon-insecure");
				    PA_UNLOCK(image->image_url);
				}
				else
				{
				    state->top_state->out_of_memory = TRUE;
				}
				if (state->top_state->insecure_images == FALSE)
				{
					state->top_state->insecure_images = TRUE;
					SECNAV_SecurityDialog(context, SD_INSECURE_DOCS_WITHIN_SECURE_DOCS_NOT_SHOWN);
				}
			}
			else
			{
				PA_UNLOCK(image->image_url);
			}
		}
	}

    if (context->compositor) {
        image->layer = lo_CreateImageLayer(context, image, NULL);
    }

	/* Initiate the loading of this image. */
    image_obs_list = lo_NewImageObserverList(context, image);
    if (!image_obs_list)
        return;
	lo_GetImage(context, context->img_cx, image, image_obs_list,
                state->top_state->force_reload);

	tag->lo_data = (void *)image;

    if (state->top_state->style_stack)
        STYLESTACK_PopTag(state->top_state->style_stack,
                          (char*)pa_PrintTagToken((int32)tag->type));

}


void
lo_PartialFormatImage(MWContext *context, lo_DocState *state, PA_Tag *tag)
{
	LO_ImageStruct *image;
    int32 layer_id;
    LM_ImageEvent mocha_event;

	image = (LO_ImageStruct *)tag->lo_data;
	if (image == NULL)
	{
		return;
	}

    layer_id = lo_CurrentLayerId(state);
    image->layer_id = layer_id;
    /* Reflect the image into Javascript */
    lo_ReflectImage(context, state, tag, image, FALSE, layer_id);

    /* Send any pending events to JavaScript */
    if ((image->image_status == IL_IMAGE_COMPLETE) || 
        (image->image_status == IL_FRAME_COMPLETE) ||
        (image->image_status == IL_NOT_IN_CACHE)) {
        mocha_event = LM_IMGLOAD;
        ET_SendImageEvent(context, image, mocha_event);
    }
    else if (image->image_status == IL_ABORTED) {
        mocha_event = LM_IMGABORT;
        ET_SendImageEvent(context, image, mocha_event);
    }
    else if ((image->image_status == IL_ERROR_NO_DATA) ||
             (image->image_status == IL_ERROR_IMAGE_DATA_CORRUPT) ||
             (image->image_status == IL_ERROR_IMAGE_DATA_TRUNCATED) ||
             (image->image_status == IL_ERROR_IMAGE_DATA_ILLEGAL) ||
             (image->image_status == IL_ERROR_INTERNAL)) {
        mocha_event = LM_IMGERROR;
        ET_SendImageEvent(context, image, mocha_event);
    }

	tag->lo_data = NULL;

	/*
	 * Assign it a properly sequential element id.
	 */

	image->ele_id = NEXT_ELEMENT;
	image->x = state->x;
	image->y = state->y;

	image->anchor_href = state->current_anchor;

	if (image->border_width < 0)
	{
		if ((image->anchor_href != NULL)||
		    (image->image_attr->usemap_name != NULL))
		{
			image->border_width = IMAGE_DEF_ANCHOR_BORDER;
		}
		else
		{
			image->border_width = IMAGE_DEF_BORDER;
		}
	
		image->border_width = FEUNITS_X(image->border_width, context);

	}

#ifdef DOM
        image->text_attr = lo_GetCurrentTextAttr(state, context);
#else
	if (state->font_stack == NULL)
	{
		LO_TextAttr tmp_attr;
		LO_TextAttr *tptr;

		/*
		 * Fill in default font information.
		 */
		lo_SetDefaultFontAttr(state, &tmp_attr, context);
		tptr = lo_FetchTextAttr(state, &tmp_attr);
		image->text_attr = tptr;
	}
	else
	{
		image->text_attr = state->font_stack->text_attr;
	}
#endif

	if ((image->text_attr != NULL)&&
		(image->text_attr->attrmask & LO_ATTR_ANCHOR))
	{
		image->image_attr->attrmask |= LO_ATTR_ANCHOR;
	}

#ifdef DOM
        /* do we want a better way to do this USEMAP hackery? */
#endif
	if (image->image_attr->usemap_name != NULL)
	{
		LO_TextAttr tmp_attr;

		lo_CopyTextAttr(image->text_attr, &tmp_attr);
		tmp_attr.fg.red =   STATE_UNVISITED_ANCHOR_RED(state);
		tmp_attr.fg.green = STATE_UNVISITED_ANCHOR_GREEN(state);
		tmp_attr.fg.blue =  STATE_UNVISITED_ANCHOR_BLUE(state);
		image->text_attr = lo_FetchTextAttr(state, &tmp_attr);
	}

    /* During relayout the editor is not allowed to block. */
    if ( (image->width == 0)||(image->height == 0))
    {
        if ( EDT_IN_RELAYOUT( context ) ){
            lo_edt_AvoidImageBlock(image);
        }
    }

    /*
	 * We may have to block on this image until later
	 * when the front end can give us the width and height.
	 */
	if (((image->width == 0)||(image->height == 0))
        && EDT_IS_EDITOR(context))
	{
		state->top_state->layout_blocking_element = (LO_Element *)image;
	}
	else
	{
		lo_FinishImage(context, state, image);
        if ((image->width == 0) || (image->height == 0))
            context->requires_reflow = PR_TRUE;
	}
}


/*************************************
 * Function: lo_FormatImage
 *
 * Description: This function does all the work to format
 *	and lay out an image on a line of elements.
 *	Creates the new image structure.  Fills it
 *	in based on the parameters in the image tag.
 *	Calls the front end to start fetching the image
 *	and get image dimentions if necessary.
 *	Places image on line, breaking line if necessary.
 *
 * Params: Window context, document state, and the image tag data.
 *
 * Returns: Nothing.
 *************************************/
void
lo_FormatImage(MWContext *context, lo_DocState *state, PA_Tag *tag)
{
    LO_ImageStruct *image;
    LO_TextAttr *tptr = NULL;
    PA_Block buff;
    char *str;
    int32 val;
    /* int32 doc_width; */
    Bool is_a_form;
    XP_ObserverList image_obs_list;   /* List of observers for an image request. */
    lo_DocLists *doc_lists;
    int32 layer_id;
#ifdef DOM
    JSContext *cx = context->mocha_context;
    DOM_StyleDatabase *db = state->top_state->style_db;
    DOM_Element *element = (DOM_Element *)ACTIVE_NODE(state);
    DOM_AttributeEntry *entry;
#endif

    doc_lists = lo_GetCurrentDocLists(state);
    
#ifdef DOM
    tptr = lo_GetCurrentTextAttr(state, context);
#else
    if (state->font_stack)
        tptr = state->font_stack->text_attr;
#endif
	/*
	 * Fill in the image structure with default data
	 */
    image = lo_new_image_element(context, state, tag->edit_element, tptr);
    if (!image)
        return;

	
	/*
	image->ele_id = NEXT_ELEMENT;
	image->x = state->x;
	image->y = state->y;
	*/
	image->anchor_href = state->current_anchor;

#ifdef DOM
#ifdef DEBUG_shaver
        fprintf(stderr, "----GETTING IMAGE DATA----\n");
#endif
        if (db) {
            if (!DOM_StyleGetProperty(cx, db, (DOM_Node *)element,
                                      BORDERWIDTH_STYLE, &entry))
                goto error;
            if (entry) {
                /* XXX handle borderTop/borderBottom/borderLeft/borderRight */
                if (!DOM_GetCleanEntryData(cx, entry, lo_SSUnitsToData, NULL,
                                           &image->border_width, context))
                    goto error;
            } else {
                /* no style value for image border */
#ifdef DEBUG_shaver
                fprintf(stderr, "no style value for IMG.border\n");
#endif
                image->border_width = IMAGE_DEF_BORDER;
            }
            if (!DOM_StyleGetProperty(cx, db, (DOM_Node *)element,
                                      PADDING_STYLE, &entry))
                goto error;
            if (entry) {
                /* XXX handle paddingTop/paddingBottom/paddingLeft/paddingRight */
                if (!DOM_GetCleanEntryData(cx, entry, lo_SSUnitsToData, NULL,
                                           &image->border_vert_space, context))
                    goto error;
                image->border_horiz_space = image->border_vert_space;
            } else {
                /* no style value for image padding */
                image->border_vert_space = IMAGE_DEF_VERTICAL_SPACE;
                image->border_horiz_space = IMAGE_DEF_HORIZONTAL_SPACE;
            }
        } else {
#endif /* DOM */
            
	if (image->anchor_href != NULL)
	{
		image->border_width = IMAGE_DEF_ANCHOR_BORDER;
	}
	else
	{
		image->border_width = IMAGE_DEF_BORDER;
	}
	image->border_vert_space = IMAGE_DEF_VERTICAL_SPACE;
	image->border_horiz_space = IMAGE_DEF_HORIZONTAL_SPACE;
#ifdef DOM
        }
#endif

	if ((image->text_attr != NULL)&&
		(image->text_attr->attrmask & LO_ATTR_ANCHOR))
	{
		image->image_attr->attrmask |= LO_ATTR_ANCHOR;
	}

	if (tag->type == P_INPUT)
	{
		is_a_form = TRUE;
	}
	else
	{
		is_a_form = FALSE;
	}

	if (is_a_form != FALSE)
	{
		LO_TextAttr tmp_attr;

                image->image_attr->layer_id = lo_CurrentLayerId(state);
		image->image_attr->form_id = doc_lists->current_form_num;
		image->image_attr->attrmask |= LO_ATTR_ISFORM;
		image->border_width = IMAGE_DEF_ANCHOR_BORDER;
		lo_CopyTextAttr(image->text_attr, &tmp_attr);
		tmp_attr.fg.red =   STATE_UNVISITED_ANCHOR_RED(state);
		tmp_attr.fg.green = STATE_UNVISITED_ANCHOR_GREEN(state);
		tmp_attr.fg.blue =  STATE_UNVISITED_ANCHOR_BLUE(state);
		image->text_attr = lo_FetchTextAttr(state, &tmp_attr);
	}

	image->image_attr->usemap_name = NULL;
	image->image_attr->usemap_ptr = NULL;

	image->ele_attrmask = 0;

	image->sel_start = -1;
	image->sel_end = -1;

	/*
	 * Check for the ISMAP parameter
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_ISMAP);
	if (buff != NULL)
	{
		image->image_attr->attrmask |= LO_ATTR_ISMAP;
		PA_FREE(buff);
	}

	/*
	 * Check for the USEMAP parameter.  The name is stored in the
	 * image attributes immediately, the map pointer will be
	 * stored in later.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_USEMAP);
	if (buff != NULL)
	{
		char *new_str;
		char *map_name;
		char *tptr;

		map_name = NULL;
		PA_LOCK(str, char *, buff);

		/*
		 * Make this an absolute URL
		 */
		if (str != NULL)
		{
			int32 len;

			len = lo_StripTextWhitespace(str, XP_STRLEN(str));
		}
		/*
		 * The IETF draft says evaluate #name relative to real,
		 * and not the base url.
		 */
		if ((str != NULL)&&(*str == '#'))
		{
			new_str = NET_MakeAbsoluteURL(
				state->top_state->url, str);
		}
		else
		{
			new_str = NET_MakeAbsoluteURL(
				state->top_state->base_url, str);
		}

		/*
		 * If we have a local URL, we can use the USEMAP.
		 */
		if ((new_str != NULL)&&
			(lo_IsAnchorInDoc(state, new_str) != FALSE))
		{
			tptr = strrchr(new_str, '#');
			if (tptr == NULL)
			{
				tptr = new_str;
			}
			else
			{
				tptr++;
			}
			map_name = XP_STRDUP(tptr);
			XP_FREE(new_str);
		}
		/*
		 * Else a non-local URL, right now we don't support this.
		 */
		else if (new_str != NULL)
		{
			XP_FREE(new_str);
		}

		PA_UNLOCK(buff);
		PA_FREE(buff);
		image->image_attr->usemap_name = map_name;
		if (image->image_attr->usemap_name != NULL)
		{
			LO_TextAttr tmp_attr;

			image->border_width = IMAGE_DEF_ANCHOR_BORDER;
			lo_CopyTextAttr(image->text_attr, &tmp_attr);
			tmp_attr.fg.red =   STATE_UNVISITED_ANCHOR_RED(state);
			tmp_attr.fg.green = STATE_UNVISITED_ANCHOR_GREEN(state);
			tmp_attr.fg.blue =  STATE_UNVISITED_ANCHOR_BLUE(state);
			image->text_attr = lo_FetchTextAttr(state, &tmp_attr);
		}
	}

	/*
	 * Check for an align parameter
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_ALIGN);
	if (buff != NULL)
	{
		Bool floating;

		floating = FALSE;
		PA_LOCK(str, char *, buff);
		image->image_attr->alignment = lo_EvalAlignParam(str,
			&floating);
		if (floating != FALSE)
		{
			image->ele_attrmask |= LO_ELE_FLOATING;
		}
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	
	if(state->top_state->style_stack)
	{
		Bool floating = FALSE;

		lo_EvalStyleSheetAlignment(
				STYLESTACK_GetStyleByIndex(state->top_state->style_stack, 0), 
				&(image->image_attr->alignment),
				&floating);	 
		
		if (floating != FALSE)
		{
			image->ele_attrmask |= LO_ELE_FLOATING;
		}
	}

	/*
	 * Get the required src parameter, and make the resulting
	 * url and absolute url.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_SRC);
	if (buff != NULL)
	{
		PA_Block new_buff;
		char *new_str;

		PA_LOCK(str, char *, buff);
		if (str != NULL)
		{
			int32 len;

			len = lo_StripTextWhitespace(str, XP_STRLEN(str));
		}
		new_str = NET_MakeAbsoluteURL(state->top_state->base_url, str);
		if (new_str == NULL)
		{
			new_buff = NULL;
		}
		else
		{
			char *url;

			new_buff = PA_ALLOC(XP_STRLEN(new_str) + 1);
			if (new_buff != NULL)
			{
				PA_LOCK(url, char *, new_buff);
				XP_STRCPY(url, new_str);
				PA_UNLOCK(new_buff);
			}
			else
			{
				state->top_state->out_of_memory = TRUE;
			}

			/* Complete kludge: if this image has the magic SRC,
			 * then also parse an HREF from it (the IMG tag doesn't
			 * normally have an HREF parameter.)
			 *
			 * The front ends know that when the user clicks on an
			 * image with the magic SRC, that they may find the
			 * "real" URL in the HREF... Gag me.
			 */
			if (!XP_STRCMP(new_str, "internal-external-reconnect"))
			{
				PA_Block hbuf;

				hbuf = lo_FetchParamValue(context, tag, PARAM_HREF);
				if (hbuf != NULL)
				{
					image->anchor_href =
					    lo_NewAnchor(state, hbuf, NULL);
				}
			}

			XP_FREE(new_str);
		}
		PA_UNLOCK(buff);
		PA_FREE(buff);
		buff = new_buff;
	}
	image->image_url = buff;

	/*
	 * Get the optional lowsrc parameter, and make the resulting
	 * url and absolute url.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_LOWSRC);
	if (buff != NULL)
	{
	    PA_Block new_buff;
	    char *new_str;

	    PA_LOCK(str, char *, buff);
	    if (str != NULL)
	    {
		int32 len;

		len = lo_StripTextWhitespace(str, XP_STRLEN(str));
	    }
	    new_str = NET_MakeAbsoluteURL(state->top_state->base_url, str);
	    if (new_str == NULL)
	    {
		    new_buff = NULL;
	    }
	    else
	    {
		    char *url;

		    new_buff = PA_ALLOC(XP_STRLEN(new_str) + 1);
		    if (new_buff != NULL)
		    {
			    PA_LOCK(url, char *, new_buff);
			    XP_STRCPY(url, new_str);
			    PA_UNLOCK(new_buff);
		    }
		    else
		    {
			    state->top_state->out_of_memory = TRUE;
		    }
		    XP_FREE(new_str);
	    }
	    PA_UNLOCK(buff);
	    PA_FREE(buff);
	    buff = new_buff;
	}
	image->lowres_image_url = buff;

	/*
	 * Get the alt parameter, and store the resulting
	 * text, and its length.  Form the special image form
	 * element, store the name in alt.
	 */
	if (is_a_form != FALSE)
	{
		buff = lo_FetchParamValue(context, tag, PARAM_NAME);
	}
	else
	{
		buff = lo_FetchParamValue(context, tag, PARAM_ALT);
	}
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		image->alt_len = XP_STRLEN(str);
		image->alt_len = (int16)lo_StripTextNewlines(str,
						(int32)image->alt_len);
		PA_UNLOCK(buff);
	}
	image->alt = buff;

	/*
	doc_width = state->right_margin - state->left_margin;
	*/

	/*
	 * Get the width parameter, in absolute or percentage.
	 * If percentage, make it absolute.
	 */

	/*
	buff = lo_FetchParamValue(context, tag, PARAM_WIDTH);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			if (state->allow_percent_width == FALSE)
			{
				val = 0;
			}
			else
			{
				val = doc_width * val / 100;
			}
		}
		else
		{
			val = FEUNITS_X(val, context);
		}
		image->width = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	*/


	buff = lo_FetchParamValue(context, tag, PARAM_WIDTH);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			image->percent_width = val;
		}
		else
		{
			image->percent_width = 0;
			val = FEUNITS_X(val, context);			
		}	
		if (val < 0)
			val = 0;	
		image->width = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}


	/* disabled because it is broken by JS threading 
	 *
	 * val = LO_GetWidthFromStyleSheet(context, state);
	 * if(val)
	 *	image->width = val;
	 */

	/*
	 * Get the height parameter, in absolute or percentage.
	 * If percentage, make it absolute.
	 */
	/*
	buff = lo_FetchParamValue(context, tag, PARAM_HEIGHT);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			if (state->allow_percent_height == FALSE)
			{
				val = 0;
			}
			else
			{
				val = state->win_height * val / 100;
			}
		}
		else
		{
			val = FEUNITS_Y(val, context);
		}
		image->height = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	*/

	buff = lo_FetchParamValue(context, tag, PARAM_HEIGHT);
	if (buff != NULL)
	{
		Bool is_percent;

		PA_LOCK(str, char *, buff);
		val = lo_ValueOrPercent(str, &is_percent);
		if (is_percent != FALSE)
		{
			image->percent_height = val;
		}
		else
		{
			image->percent_height = 0;
			val = FEUNITS_Y(val, context);
		}
		if (val < 0)
			val = 0;
		image->height = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}

	/* disabled because it is broken by JS threading 
	 *
	 * val = LO_GetHeightFromStyleSheet(context, state);
	 * if(val)
	 *	image->height = val;
	 */

	/*
	 * Get the border parameter.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_BORDER);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		val = XP_ATOI(str);
		if (val < 0)
		{
			val = 0;
		}
		image->border_width = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	image->border_width = FEUNITS_X(image->border_width, context);

	/*
	 * Get the extra vertical space parameter.
	 */
	buff = lo_FetchParamValue(context, tag, PARAM_VSPACE);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		val = XP_ATOI(str);
		if (val < 0)
		{
			val = 0;
		}
		image->border_vert_space = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	image->border_vert_space = FEUNITS_Y(image->border_vert_space, context);

	/*
	 * Get the extra horizontal space parameter.
	 */
	if (image->ele_attrmask & LO_ELE_FLOATING)
	{
		image->border_horiz_space = IMAGE_DEF_FLOAT_HORIZONTAL_SPACE;
	}
	else
	{
		image->border_horiz_space = IMAGE_DEF_HORIZONTAL_SPACE;
	}
	buff = lo_FetchParamValue(context, tag, PARAM_HSPACE);
	if (buff != NULL)
	{
		PA_LOCK(str, char *, buff);
		val = XP_ATOI(str);
		if (val < 0)
		{
			val = 0;
		}
		image->border_horiz_space = val;
		PA_UNLOCK(buff);
		PA_FREE(buff);
	}
	image->border_horiz_space = FEUNITS_X(image->border_horiz_space,
						context);


	lo_FillInImageGeometry( state, image );

	/*
	 * Make sure we don't load insecure images inside
	 * a secure document.
	 */
	if (state->top_state->security_level > 0)
	{
		if (image->image_url != NULL)
		{
			PA_LOCK(str, char *, image->image_url);
			if (NET_IsURLSecure(str) == FALSE)
			{
				PA_UNLOCK(image->image_url);
				PA_FREE(image->image_url);
				image->image_url = PA_ALLOC(
				    XP_STRLEN("internal-icon-insecure") + 1);
				if (image->image_url != NULL)
				{
				    PA_LOCK(str, char *, image->image_url);
				    XP_STRCPY(str, "internal-icon-insecure");
				    PA_UNLOCK(image->image_url);
				}
				else
				{
				    state->top_state->out_of_memory = TRUE;
				}
				if (state->top_state->insecure_images == FALSE)
				{
					state->top_state->insecure_images = TRUE;
					SECNAV_SecurityDialog(context, SD_INSECURE_DOCS_WITHIN_SECURE_DOCS_NOT_SHOWN);
				}
			}
			else
			{
				PA_UNLOCK(image->image_url);
			}
		}
	}

    layer_id = lo_CurrentLayerId(state);
    image->layer_id = layer_id;
    
    /* Reflect the image into Javascript */
    lo_ReflectImage(context, state, tag, image, FALSE, layer_id);

    if (context->compositor) {
        image->layer = lo_CreateImageLayer(context, image, NULL);
    }

	/* Initiate the loading of this image. */
    image_obs_list = lo_NewImageObserverList(context, image);
    if (!image_obs_list)
        return;

    /* If a lowres image has been specified, we'll actually be loading
       that, not the real (hires) url */
	lo_GetImage(context, context->img_cx, image, image_obs_list,
                    state->top_state->force_reload);

    /* During relayout the editor is not allowed to block. */
    if ( (image->width == 0)||(image->height == 0))
    {
        if ( EDT_IN_RELAYOUT( context ) ){
            lo_edt_AvoidImageBlock(image);
        }
    }

    /*
	 * We may have to block on this image until later
	 * when the front end can give us the width and height.
	 */
	if (((image->width == 0)||(image->height == 0))
        && (EDT_IS_EDITOR(context)))
	{
        /* If a lowres image has been specified, we'll actually be
           blocking on that, not the real (hires) url */
        TIMING_STARTCLOCK_NAME("lo:blk-img", image->image_url);

		state->top_state->layout_blocking_element = (LO_Element *)image;
	}
	else
	{
		lo_FinishImage(context, state, image);
        if ((image->width == 0) || (image->height == 0))
            context->requires_reflow = PR_TRUE;
	}
 error:
     return;
        /* XXX free the image */
}


void
lo_FinishImage(MWContext *context, lo_DocState *state, LO_ImageStruct *image)
{
	/* PA_Block buff;
	char *str;
	Bool line_break;
	*/
	int32 baseline_inc;
	int32 line_inc;
	/*
	int32 image_height, image_width;
	LO_TextStruct tmp_text;
	LO_TextInfo text_info;
	*/

	if (context->compositor && lo_CurrentLayerState(state)) {
	    CL_Layer *parent, *doc_layer, *content_layer;
            lo_LayerDocState *layer_state = lo_CurrentLayerState(state);
        
	    doc_layer = CL_GetCompositorRoot(context->compositor);
	    if (layer_state->layer == doc_layer) 
		parent = CL_GetLayerChildByName(doc_layer,
                                            LO_BODY_LAYER_NAME);
	    else
		parent = layer_state->layer;

            content_layer = CL_GetLayerChildByName(parent, 
                                                   LO_CONTENT_LAYER_NAME);
            XP_ASSERT(content_layer);
            if (content_layer)
                CL_InsertChild(parent, image->layer, content_layer, CL_ABOVE);
	}

	/*
	 * All this work is to get the text_info filled in for the current
	 * font in the font stack. Yuck, there must be a better way.
	 */
	
	/*
	memset (&tmp_text, 0, sizeof (tmp_text));
	buff = PA_ALLOC(1);
	if (buff == NULL)
	{
		state->top_state->out_of_memory = TRUE;
		return;
	}
	PA_LOCK(str, char *, buff);
	str[0] = ' ';
	PA_UNLOCK(buff);
	tmp_text.text = buff;
	tmp_text.text_len = 1;
	tmp_text.text_attr =
		state->font_stack->text_attr;
	FE_GetTextInfo(context, &tmp_text, &text_info);
	PA_FREE(buff);
	*/

	/*
	 * Eventually this will never happen since we always
	 * have dims here from either the image tag itself or the
	 * front end.
	 */
	
	/*
	if (image->width == 0)
	{
		image->width = IMAGE_DEF_DIM;
	}
	if (image->height == 0)
	{
		image->height = IMAGE_DEF_DIM;
	}

	image_width = image->width + (2 * image->border_width) +
		(2 * image->border_horiz_space);
	image_height = image->height + (2 * image->border_width) +
		(2 * image->border_vert_space);

	*/


	/*
	 * SEVERE FLOW BREAK!  This may be a floating image,
	 * which means at this point we go do something completely
	 * different.
	 */
	if (image->ele_attrmask & LO_ELE_FLOATING)
	{	/*
		if (image->image_attr->alignment == LO_ALIGN_RIGHT)
		{
			if (state->right_margin_stack == NULL)
			{
				image->x = state->right_margin - image_width;
			}
			else
			{
				image->x = state->right_margin_stack->margin -
					image_width;
			}
			if (image->x < 0)
			{
				image->x = 0;
			}
		}
		else
		{
			image->x = state->left_margin;
		}

		image->y = -1;
		image->x_offset += (int16)image->border_horiz_space;
		image->y_offset += (int32)image->border_vert_space;

		lo_AddMarginStack(state, image->x, image->y,
			image->width, image->height,
			image->border_width,
			image->border_vert_space, image->border_horiz_space,
			image->image_attr->alignment);

		*/

		/*
		 * Insert this element into the float list.
		 */
		image->next = state->float_list;
		state->float_list = (LO_Element *)image;

		/* Update its offsets */
		image->x_offset += (int16)image->border_horiz_space;
		image->y_offset += (int32)image->border_vert_space;	

		/* 
		 * Append a dummy layout element in the line list.  When the relayout engine
		 * will see this dummy element, it will call lo_LayoutFloatImage()
		 */
		lo_AppendFloatInLineList(context, state, (LO_Element *) image, NULL );
		
		lo_LayoutFloatImage( context, state, image, TRUE);
		
		/*
		if (state->at_begin_line != FALSE)
		{
			lo_FindLineMargins(context, state);
			state->x = state->left_margin;
		}
		*/

		return;
	}

	/*
	 * Will this image make the line too wide.
	 */
	/*
	if ((state->x + image_width) > state->right_margin)
	{
		line_break = TRUE;
	}
	else
	{
		line_break = FALSE;
	}
	*/

	/*
	 * We cannot break a line if we have no break positions.
	 * Usually happens with a single line of unbreakable text.
	 */
	/*
	 * This doesn't work right now, I don't know why, seems we
	 * have lost the last old_break_pos somehow.
	 */
#ifdef FIX_THIS
	if ((line_break != FALSE)&&(state->break_pos == -1))
	{
		/*
		 * It may be possible to break a previous
		 * text element on the same line.
		 */
		if (state->old_break_pos != -1)
		{
			lo_BreakOldElement(context, state);
			line_break = FALSE;
		}
		else
		{
			line_break = FALSE;
		}
	}
#endif /* FIX_THIS */

	/*
	 * if we are at the beginning of the line.  There is
	 * no point in breaking, we are just too wide.
	 * Also don't break in unwrapped preformatted text.
	 * Also can't break inside a NOBR section.
	 */
	/*
	if ((state->at_begin_line != FALSE)||
		(state->preformatted == PRE_TEXT_YES)||
		(state->breakable == FALSE))
	{
		line_break = FALSE;
	}
	*/

	/*
	 * break on the image if we have
	 * a break.
	 */
	/*
	if (line_break != FALSE)
	{
	*/
		/*
		 * We need to make the elements sequential, linefeed
		 * before image.
		 */
	/*
		state->top_state->element_id = image->ele_id;

		lo_SoftLineBreak(context, state, TRUE);
		image->x = state->x;
		image->y = state->y;
		image->ele_id = NEXT_ELEMENT;
	}
	*/

	/*
	 * Figure out how to align this image.
	 * baseline_inc is how much to increase the baseline
	 * of previous element of this line.  line_inc is how
	 * much to increase the line height below the baseline.
	 */
	/*
	baseline_inc = 0;
	line_inc = 0;
	*/

	/*
	 * If we are at the beginning of a line, with no baseline,
	 * we first set baseline and line_height based on the current
	 * font, then place the image.
	 */
	if (state->baseline == 0)
	{
#ifdef ALIGN_IMAGE_WITH_FAKE_TEXT
		state->baseline = text_info.ascent;
		state->line_height = text_info.ascent + text_info.descent;
#else
		state->baseline = 0;
/* Why do we need this?
		if (state->line_height < 1)
		{
			state->line_height = 1;
		}
*/
#endif /* ALIGN_IMAGE_WITH_FAKE_TEXT */
	}

	/*
	lo_CalcAlignOffsets(state, &text_info, image->image_attr->alignment,
		image_width, image_height,
		&image->x_offset, &image->y_offset, &line_inc, &baseline_inc);

	image->x_offset += (int16)image->border_horiz_space;
	image->y_offset += (int32)image->border_vert_space;
	*/

	lo_LayoutInflowImage( context, state, image, FALSE, &line_inc, &baseline_inc);

	lo_AppendToLineList(context, state,
		(LO_Element *)image, baseline_inc);

	lo_UpdateStateAfterImageLayout( state, image, line_inc, baseline_inc );

	/*
	state->baseline += (intn) baseline_inc;
	state->line_height += (intn) (baseline_inc + line_inc);
	*/

	/*
	 * Clean up state
	 */

	/*
	state->x = state->x + image->x_offset +
		image_width - image->border_horiz_space;
	state->linefeed_state = 0;
	state->at_begin_line = FALSE;
	state->trailing_space = FALSE;
	state->cur_ele_type = LO_IMAGE;
	*/
}


Bool
LO_BlockedOnImage(MWContext *context, LO_ImageStruct *image)
{
	int32 doc_id;
	lo_TopState *top_state;

	/*
	 * Get the unique document ID, and retreive this
	 * documents layout state.
	 */
	doc_id = XP_DOCID(context);
	top_state = lo_FetchTopState(doc_id);
	if ((top_state == NULL)||(top_state->doc_state == NULL))
	{
		return(FALSE);
	}

	if ((top_state->layout_blocking_element != NULL)&&
	    (top_state->layout_blocking_element == (LO_Element *)image))
	{
		return(TRUE);
	}
	return(FALSE);
}


/*
 * Make sure the image library has been informed of all possible
 * image URLs that might be loaded by this document.
 */
void
lo_NoMoreImages(MWContext *context)
{
	PA_Tag *tag_ptr;
	int32 doc_id;
	lo_TopState *top_state;
	lo_DocState *state;

	/*
	 * All blocked tags should be at the top level of state
	 */
	doc_id = XP_DOCID(context);
	top_state = lo_FetchTopState(doc_id);
	if ((top_state == NULL)||(top_state->doc_state == NULL))
	{
#ifndef M12N                    /* XXXM12N Fix me. IL_NoMoreImages was used
                                   by the image library to detect when it
                                   could install a custom colormap. */
		/*
		 * No matter what, we must be done with all images now
		 */
		IL_NoMoreImages(context);
#else
        /* XXXM12N Need an observer callback. */
#endif /* M12N */
		return;
	}
	state = top_state->doc_state;
	tag_ptr = top_state->tags;

	/*
	 * Go through all the remaining blocked tags looking
	 * for <INPUT TYPE=IMAGE> tags.
	 */
	while (tag_ptr != NULL)
	{
		if (tag_ptr->type == P_INPUT)
		{
			PA_Block buff;
			char *str;
			int32 type;

			type = FORM_TYPE_TEXT;
			buff = lo_FetchParamValue(context, tag_ptr, PARAM_TYPE);
			if (buff != NULL)
			{
				PA_LOCK(str, char *, buff);
				type = lo_ResolveInputType(str);
				PA_UNLOCK(buff);
				PA_FREE(buff);
			}

			/*
			 * Prefetch this image.
			 * The ImageStruct created will get stuck into
			 * tag_ptr->lo_data, and freed later in 
			 * lo_ProcessInputTag().
			 */
			if (type == FORM_TYPE_IMAGE)
			{
				lo_BlockedImageLayout(context, state, tag_ptr, 
                                      top_state->base_url);
			}
		}
		tag_ptr = tag_ptr->next;
	}

#ifndef M12N                    /* XXXM12N Fix me. IL_NoMoreImages was used
                                   by the image library to detect when it
                                   could install a custom colormap. */
		/*
		 * No matter what, we must be done with all images now
		 */
		IL_NoMoreImages(context);
#else
        /* XXXM12N Need an observer callback. */
#endif /* M12N */
}

#ifdef EDITOR
LO_ImageStruct* LO_NewImageElement( MWContext* context ){
	int32 doc_id;
	lo_TopState *top_state;
	lo_DocState *state;
	LO_ImageStruct *image;

	/*
	 * Get the unique document ID, and retreive this
	 * documents layout state.
	 */
	doc_id = XP_DOCID(context);
	top_state = lo_FetchTopState(doc_id);
	if ((top_state == NULL)||(top_state->doc_state == NULL))
	{
	    return 0;
	}
	state = top_state->doc_state;

    state->edit_current_element = 0;
	state->edit_current_offset = 0;

	/*
	 * Fill in the image structure with default data
	 */
    image = lo_new_image_element(context, state, NULL, NULL);
    if (!image)
        return NULL;

    image->border_width = -1;
    image->border_vert_space  = IMAGE_DEF_VERTICAL_SPACE;
    image->border_horiz_space = IMAGE_DEF_HORIZONTAL_SPACE;

	return image;
}
#endif        


/* Create a new observer list which will be passed into to IL_GetImage (via
   lo_GetImage) in order to permit an Image Library request to be
   monitored. The layout image observer is added to this new observer list
   before the function returns. */
XP_ObserverList
lo_NewImageObserverList(MWContext *context, LO_ImageStruct *lo_image)
{
	int32 doc_id;lo_TopState *top_state;
    XP_ObserverList image_obs_list; /* List of observer callbacks. */
    lo_ImageObsClosure *image_obs_closure; /* Closure data to be passed back
                                              to lo_ImageObserver. */
    NS_Error status;

	/* Get the unique document ID, and retrieve this document's layout
       state. */
	doc_id = XP_DOCID(context);
	top_state = lo_FetchTopState(doc_id);
	if (!top_state)	{
	    return NULL;
	}

    /* Create an XP_ObserverList for this image.  The observable IL_ImageReq
       will be set by the Image Library. */
    status = XP_NewObserverList(NULL, &image_obs_list);
    if (status < 0) {
        if (status == MK_OUT_OF_MEMORY)
            top_state->out_of_memory = TRUE;
        return NULL;
    }

    /* Closure data for the image observer. */
    image_obs_closure = XP_NEW_ZAP(lo_ImageObsClosure);
    if (!image_obs_closure) {
        top_state->out_of_memory = TRUE;
        return NULL;
    }
    image_obs_closure->context = context;
    image_obs_closure->lo_image = lo_image;
    image_obs_closure->obs_list = image_obs_list;

    /* Add the layout image observer to the observer list. */
    status = XP_AddObserver(image_obs_list, lo_ImageObserver, image_obs_closure);
    if (status < 0) {
        if (status == MK_OUT_OF_MEMORY)
            top_state->out_of_memory = TRUE;
        return NULL;
    }

    return image_obs_list;
}


static void
lo_image_pixmap_update(MWContext *context, LO_ImageStruct *lo_image,
                       IL_Rect *update_rect)
{

#if defined(XP_WIN) || defined(XP_MAC) || defined(XP_UNIX)
	IL_ImageReq *image_req = NULL;	/* See below at the end of the function. */
#endif

    /* This lo_image cannot correspond to an icon since we are receiving
       pixmap update messages. */
    lo_image->is_icon = FALSE;

    /* Update the image layer if it is appropriate to do so. */
    if (context->compositor && lo_image && lo_image->layer &&
        lo_image->image_attr) {
        if (lo_image->image_attr->attrmask & LO_ATTR_BACKDROP) {
            /* Only update backdrops when the image has
               completed decoding, not when the pixmap is updated. */
        }
        else {
            /* Non-backdrop images only respond to pixmap update messages
               so that they can load incrementally. */
            XP_Rect rect, *valid_rect;

            rect.left = FEUNITS_X(update_rect->x_origin, context);
            rect.top = FEUNITS_Y(update_rect->y_origin, context);
            rect.bottom = rect.top + FEUNITS_Y(update_rect->height, context);
            rect.right = rect.left + FEUNITS_X(update_rect->width, context);

            valid_rect = &lo_image->valid_rect;
            XP_RectsBbox(&rect, valid_rect, valid_rect);
            CL_ResizeLayer(lo_image->layer, valid_rect->right - valid_rect->left,
                           valid_rect->bottom - valid_rect->top);
            CL_UpdateLayerRect(context->compositor, lo_image->layer, &rect,
                               PR_TRUE); /* XXX FALSE on the final update? */
        }
    }

#if defined(XP_WIN) || defined(XP_MAC) || defined(XP_UNIX)
    /*
     * This is a hack in the update event to allow RDF images to get updates
     * even though they have no compositors in their contexts.
     */
	if (context->type == MWContextIcon)
	{
		image_req = lo_image->image_req;
        if (image_req) {
            /* Forces a DisplayPixmap update. */
            IL_DisplaySubImage(image_req, 0, 0, 0, 0, 1, 1);
        }
	}
#endif
}

static void
lo_frame_complete(MWContext *context, LO_ImageStruct *lo_image)
{
    PRBool is_anim, is_backdrop, is_cell_backdrop;

    XP_ASSERT(lo_image && lo_image->image_attr);
    if (!lo_image || !lo_image->image_attr) /* Paranoia */
        return;

    /* If we've already received a frame-complete message, then this
       must be an animated image. */
    is_anim = (PRBool)(lo_image->image_status == IL_FRAME_COMPLETE); 
    is_backdrop = (PRBool)(lo_image->image_attr->attrmask & LO_ATTR_BACKDROP);
	is_cell_backdrop = (PRBool)(lo_image->image_attr->attrmask & LO_ATTR_CELL_BACKDROP);

    if (!is_cell_backdrop)
	    ET_SendImageEvent(context, lo_image, LM_IMGLOAD);

    if (!lo_image->layer)
        return;

	if (is_backdrop) {
        /* Only update cell/layer/doc backdrops when the image has completed
           loading, not when the pixmap is updated. */
        XP_Rect rect = CL_MAX_RECT;
        CL_UpdateLayerRect(context->compositor, lo_image->layer, &rect, PR_FALSE);

        /* Unlike ordinary images, backdrop images are assumed to be transparent
           until they are displayed.  (Ordinary images are assumed to be opaque
           until they are determined to be transparent.) */
        CL_ChangeLayerFlag(lo_image->layer, CL_OPAQUE, (PRBool)!lo_image->is_transparent);
        
        /* If this animated GIF serves as a backdrop image, force
           offscreen compositing in order to reduce flicker. */
		if (is_anim)
			CL_ChangeLayerFlag(lo_image->layer, CL_PREFER_DRAW_OFFSCREEN, PR_TRUE);
    }
	else {
		/* We may need to reset the layer transparency if the current image
		   is opaque and it is replacing a previous image. */
		if (!lo_image->is_transparent) {
            CL_ChangeLayerFlag(lo_image->layer, CL_OPAQUE, PR_TRUE);
            CL_ChangeLayerFlag(lo_image->layer, CL_PREFER_DRAW_OFFSCREEN,
		                       PR_FALSE);
		}
	}

}


static void
lo_image_dimensions(MWContext *context, LO_ImageStruct *lo_image,
                    int width, int height)
{
    if(!lo_image)    {
        return;
    }

    width = FEUNITS_X(width, context);
    height = FEUNITS_Y(height, context);

    /* No need to change the image info if the image's size has
       remained the same. */
    if ((lo_image->width == width) &&
        (lo_image->height == height)) {
        return;
    }

    lo_image->width = width;
    lo_image->height = height;

    /* Don't call LO_SetImageInfo() for backdrop images. */
    if (lo_image->image_attr &&
        !(lo_image->image_attr->attrmask & LO_ATTR_BACKDROP))
        {
            LO_SetImageInfo (context, lo_image->ele_id, width, height);
        }
}

static void
lo_icon_box( MWContext *context, LO_ImageStruct *lo_image, int icon_width, int icon_height )
{
	LO_TextStruct text;
	LO_TextAttr text_attr;
	LO_TextInfo text_info;
	int32 doc_id;
	lo_TopState *top_state;
	lo_DocState *state;
	char *image_url;
      
 /* ptn: The code that determines the area needed for the alt text
	will be put in a separate function so that it is the same
	code called for the display alttext as well as the area estimation.
	This code was for an immediate bug fix with time limits */
		
   	PA_LOCK(image_url, char *, lo_image->image_url);
	/* Determine whether the icon was explicitly requested by URL
	(in which case only the icon is drawn) or whether it needs
	to be drawn as a placeholder (along with the ALT text and
	placeholder border) as a result of a failed image request. */
	if (image_url &&
        (*image_url == 'i'              ||
		!XP_STRNCMP(image_url, "/mc-", 4)  ||
		!XP_STRNCMP(image_url, "/ns-", 4))) {
			lo_image->width = FEUNITS_X(icon_width, context);
			lo_image->height = FEUNITS_Y(icon_height, context);
	}
	else {
		lo_image->width = FEUNITS_X(icon_width + (2*ICON_X_OFFSET), 
                                            context);
		lo_image->height = FEUNITS_Y(icon_height + (2*ICON_Y_OFFSET), 
                                             context);
		
		if(lo_image->alt){
				
			/* Get the unique doc ID, and retrieve this doc's layout state. 
			*/
			doc_id = XP_DOCID(context);
			top_state = lo_FetchTopState(doc_id);
			if (!top_state)	{
				return;
			}
			state = lo_CurrentSubState(top_state->doc_state);
			memset (&text, 0, sizeof (text));
			text.text_attr = &text_attr;
			lo_SetDefaultFontAttr(state, text.text_attr, context);
			text.text = lo_image->alt;
			text.text_len = lo_image->alt_len;

 			FE_GetTextInfo(context, &text, &text_info);

			text.width = text_info.max_width;
			text.height = text_info.ascent + text_info.descent;
			if( text.width && text.height ){
				lo_image->width  = FEUNITS_X(icon_width + (4*ICON_X_OFFSET), context) + text.width;
				lo_image->height= (icon_height > text.height) ? 
						FEUNITS_Y(icon_height + (2*ICON_Y_OFFSET), context) : text.height + FEUNITS_Y(2*ICON_Y_OFFSET, context);
			}	
		}
	} 
	PA_UNLOCK(lo_image->image_url);
}

static void
lo_internal_image(MWContext *context, LO_ImageStruct *lo_image, int icon_number,
                  int icon_width, int icon_height)
{
    /* Don't draw icons for mocha images. */
    if (lo_image->image_attr->attrmask & LO_ATTR_MOCHA_IMAGE)
        return;

    /* Mark this lo_image as an icon, so that drawing calls will use
       IL_DisplayIcon instead of IL_DisplaySubImage. */
    lo_image->is_icon = TRUE;
    lo_image->icon_number = icon_number;
	
	/* Set the lo_image->width and lo_image->height so that 
	icons and alt text are displayed, if present */

	if(!lo_image->width || !lo_image->height )
		lo_icon_box( context, lo_image, icon_width, icon_height );

    /* Tell layout the dimensions of the image. */
	LO_SetImageInfo (context, lo_image->ele_id, lo_image->width, lo_image->height);

    /* Refresh the area occupied by the image. */
    if (context->compositor && lo_image->layer) {
        XP_Rect update_rect, *valid_rect;

        update_rect.left = 0;
        update_rect.top = 0;
        update_rect.right = lo_image->width;
        update_rect.bottom = lo_image->height;

        valid_rect = &lo_image->valid_rect;
        XP_RectsBbox(&update_rect, valid_rect, valid_rect);
        CL_SetLayerBbox(lo_image->layer, valid_rect);
        CL_ChangeLayerFlag(lo_image->layer, CL_OPAQUE, PR_FALSE);

        CL_UpdateLayerRect(context->compositor, lo_image->layer, &update_rect,
                           PR_FALSE);
    }
}


static void
lo_image_incomplete(MWContext *context, LO_ImageStruct *lo_image,
					XP_ObservableMsg message, int icon_number,
					int icon_width, int icon_height, LM_ImageEvent mocha_event)
{

    if (lo_image->lowres_image_req) {
        /* If we already have a lowres image, then use that instead. */
        lo_image->image_req = lo_image->lowres_image_req;
    }
    else {
        lo_image->image_status = (uint16)message;
            
        if (lo_image->width && lo_image->height) {
            int valid_width, valid_height;
            XP_Rect *valid_rect = &lo_image->valid_rect;

            valid_width = valid_rect->right - valid_rect->left;
            valid_height = valid_rect->bottom - valid_rect->top;
            if (!valid_height || !valid_width)
                lo_internal_image(context, lo_image, icon_number,
                                  icon_width, icon_height);
        }
        else {
            lo_internal_image(context, lo_image, icon_number,
                              icon_width, icon_height);
        }

		if (lo_image->image_attr &&
			!(lo_image->image_attr->attrmask & LO_ATTR_CELL_BACKDROP))
                ET_SendImageEvent(context, lo_image, mocha_event);
    }
}

		
/* Image observer callback. */
void
lo_ImageObserver(XP_Observable observable, XP_ObservableMsg message,
                 void *message_data, void *closure)
{
    IL_MessageData *data = (IL_MessageData*)message_data;
    lo_ImageObsClosure *image_obs_closure = (lo_ImageObsClosure *)closure;
    MWContext *context=NULL;
    LO_ImageStruct *lo_image;
    LM_ImageEvent mocha_event;

    if (image_obs_closure) {
        context = image_obs_closure->context;
        lo_image = image_obs_closure->lo_image;
    }

    /* The lo_image may not have a valid image handle at this point, or
       the image's URL property may have been changed in JavaScript,
       so give the lo_image the image handle passed in with the message
       data. */
    if(lo_image)    {
        lo_image->image_req = data->image_instance;
    
	switch(message) {
	case IL_DIMENSIONS:
	    lo_image_dimensions(context, lo_image, data->width, data->height);
	    break;

	case IL_IS_TRANSPARENT:
	    lo_image->is_transparent = TRUE;
	    if (context->compositor && lo_image->layer) {
		CL_ChangeLayerFlag(lo_image->layer, CL_OPAQUE, PR_FALSE);
		CL_ChangeLayerFlag(lo_image->layer, CL_PREFER_DRAW_OFFSCREEN,
				   PR_TRUE);
	    }
	   break;

	case IL_DESCRIPTION:
	    /* This must be a call to the stand alone image viewer, so
		the document title is set in the Title observer in 
	       libimg/src/dummy_nc.c.*/  	   
	    break;

	case IL_PIXMAP_UPDATE:
	    lo_image_pixmap_update(context, lo_image, &data->update_rect);
	    break;

	case IL_IMAGE_COMPLETE:
	    lo_image->image_status = (uint16)message;
	    if( (lo_image->lowres_image_url) != NULL ){
		    if(!lo_image->lowres_image_req){
			    XP_ObserverList tmp_obs_list;
			    int32 doc_id;
			    lo_TopState *top_state;
		     
			    /* Get the unique document ID, and retrieve this document's layout
		   state. 
			    */
			    doc_id = XP_DOCID(context);
			    top_state = lo_FetchTopState(doc_id);
			    if (!top_state)	{
				    return;
			    }

		/* Hold on to lowres image handle in case the highres image
		   fails. */
		lo_image->lowres_image_req = lo_image->image_req;

			    tmp_obs_list = lo_NewImageObserverList(context, lo_image);
			    /* Lowres image request is overwritten. Destruction of
		   lowres image occurs when document is destroyed.	*/
			    lo_GetImage(context, context->img_cx, lo_image, tmp_obs_list,
			    top_state->force_reload);
			    }
		}	
	    break;
        
	case IL_FRAME_COMPLETE:
	    lo_frame_complete(context, lo_image);
	    lo_image->image_status = (uint16)message;
	    break;

	case IL_NOT_IN_CACHE:
	    if (lo_image->lowres_image_req) {
		/* If we already have a lowres image, then use that instead. */
		lo_image->image_req = lo_image->lowres_image_req;
	    }
	    else {
		lo_image->image_status = (uint16)message;
		lo_internal_image(context, lo_image, data->icon_number,
				  data->icon_width, data->icon_height);
		/* As far as JavaScript events go, we treat an image that wasn't in
		   the cache the same as if it was actually loaded.  That way, a
		   Javascript program will run the same way with images turned on
		   or off. */
		 mocha_event = LM_IMGLOAD;
			     if (lo_image->image_attr &&
				 !(lo_image->image_attr->attrmask & LO_ATTR_CELL_BACKDROP))
			  ET_SendImageEvent(context, lo_image, mocha_event);
	    }
	    break;

	case IL_ABORTED:
	   lo_image_incomplete(context, lo_image, message, data->icon_number,
					    data->icon_width, data->icon_height, LM_IMGABORT);
	   break;

	case IL_ERROR_NO_DATA:
	case IL_ERROR_IMAGE_DATA_CORRUPT:
	case IL_ERROR_IMAGE_DATA_TRUNCATED:
	case IL_ERROR_IMAGE_DATA_ILLEGAL:
	case IL_ERROR_INTERNAL:
	    lo_image_incomplete(context, lo_image, message, data->icon_number,
					    data->icon_width, data->icon_height, LM_IMGERROR);
	    break;

	case IL_INTERNAL_IMAGE:
	    lo_internal_image(context, lo_image, data->icon_number, data->icon_width,
			      data->icon_height);
	    break;

	case IL_IMAGE_DESTROYED:
	    /* Remove ourself from the observer callback list. */
	    XP_RemoveObserver(image_obs_closure->obs_list, lo_ImageObserver,
			      image_obs_closure);
	    XP_FREE(image_obs_closure);
	    lo_image->image_req = NULL;
		    
	    break;

	default:
	    break;
	}
    }
}

/* We want to write a title in the titlebar and tell all other
functions not to write over it. This is only used by il_load_image
for view streams in libimg/dummy_nc.c & external.c. */
void
lo_view_title( MWContext *context, char *title_str ){

    int32 doc_id;
    lo_TopState *top_state;
		 
	/* Get the unique document ID and the layout state.*/
	doc_id = XP_DOCID(context);
	top_state = lo_FetchTopState(doc_id);
	if (!top_state)	
          return;

    FE_SetDocTitle(context, title_str);
    top_state->have_title = TRUE;
    return;
}

#define FORCE_LOAD_ALL_IMAGES ((char *)1)

static char *force_load_images = NULL;
static PRBool autoload_images = PR_TRUE;
static PRBool pref_initialized = PR_FALSE;

/* remove Mac warning about missing prototype */
MODULE_PRIVATE int PR_CALLBACK lo_AutoloadPrefChangedFunc(const char *pref,
                                                          void *data);

MODULE_PRIVATE int PR_CALLBACK lo_AutoloadPrefChangedFunc(const char *pref,
                                                          void *data) 
{
	int status=0;

	if (!XP_STRCASECMP(pref,"general.always_load_images")) 
		status = PREF_GetBoolPref("general.always_load_images",
                                  &autoload_images);

	return status;
}

/* Inform layout of an image URL to be force loaded.  If all_images is TRUE,
   this indicates that all images are to be force loaded.  Note that this
   function does not actually trigger the loading of images. */
void LO_SetForceLoadImage(char *url, XP_Bool all_images)
{
    if (force_load_images &&
        force_load_images != FORCE_LOAD_ALL_IMAGES) {
        XP_FREE(force_load_images);
    }
    
    if (all_images) {
        force_load_images = FORCE_LOAD_ALL_IMAGES;
    }
    else {
        if (url)
            force_load_images = XP_STRDUP(url);
        else
            force_load_images = NULL;
    }
}

/* Initiate the loading of an image. */
void lo_GetImage(MWContext *context, IL_GroupContext *img_cx,
                 LO_ImageStruct *lo_image, XP_ObserverList obs_list,
                 NET_ReloadMethod requested_reload_method)
{

    XP_Bool net_request_allowed; /* Are we willing to make a net request? */
    NET_ReloadMethod reload_method;
    IL_NetContext *net_cx = NULL;
    IL_IRGB *trans_pixel;
    char *image_url, *lowres_image_url=NULL, *url_to_fetch;
	IL_ImageReq *dummy_ireq;

    /* Safety checks. */
    if (!context || !lo_image)
        return;

    /* Handle the TextFE. */
    if (context->type == MWContextText) {
        XL_GetTextImage(lo_image);
        return;
    }

    /* More safety checks. */
    if (!img_cx || !obs_list)
        return;

    /* Initialize the autoload images pref, if necessary. */
    if (!pref_initialized) {
        int status = PREF_GetBoolPref("general.always_load_images",
                                      &autoload_images);
        if (status == PREF_NOERROR) {
            PREF_RegisterCallback("general.always_load_images",
                                  lo_AutoloadPrefChangedFunc, NULL);
            pref_initialized = TRUE;
        }
    }

    /* Fetch the lowres image first if there is one.  Layout will call us
       again to fetch the hires image, at which point the lowres_image_url
       will be NULL. */
    if (lo_image->lowres_image_url) {
        PA_LOCK(lowres_image_url, char *, lo_image->lowres_image_url);
    }
    PA_LOCK(image_url, char *, lo_image->image_url);
    if ((context->type == MWContextPostScript) ||
        (context->type == MWContextPrint)) {
        url_to_fetch = image_url;
    }
    else {
		if ((lo_image->lowres_image_url)&&(!lo_image->lowres_image_req))
            url_to_fetch = lowres_image_url;
        else
            url_to_fetch = image_url;
    }
    
    /* Determine whether we are willing to load the image from the Network. */
    if ((context->type == MWContextPostScript) ||
        (context->type == MWContextPrint) ||
		(context->type == MWContextDialog) ||
        autoload_images ||
        (force_load_images == FORCE_LOAD_ALL_IMAGES) ||
        (force_load_images && image_url && !XP_STRCMP(force_load_images, image_url))) {
        net_request_allowed = TRUE;
    }
    else {
        net_request_allowed = FALSE;
    }

    /* JavaScript-generated images can change if the document is reloaded,
       so don't request a cache-only reload, even if layout requested one. */
    if (NET_URL_Type(url_to_fetch) == MOCHA_TYPE_URL)
        requested_reload_method = NET_DONT_RELOAD;
 
    /* Create a dummy Net Context for the Image Library to use for network
       operations.  This will be replaced by a true Net Context when the
       Network Library is modularized. */

    if(context->type != MWContextPrint){
        reload_method = net_request_allowed ? requested_reload_method : NET_CACHE_ONLY_RELOAD;
	}else{
		reload_method =  NET_NORMAL_RELOAD;
	}
    net_cx = IL_NewDummyNetContext(context, reload_method);

    /* Determine whether to request a mask if this is a transparent image.
       In the case of a document backdrop, we ask the image library to fill
       in the transparent area with a solid color.  For all other transparent
       images, we force the creation of a mask by passing in NULL. */
    if (context->type == MWContextPostScript) {
        trans_pixel = XP_NEW_ZAP(IL_IRGB);
        if (trans_pixel)
            trans_pixel->red = trans_pixel->green = trans_pixel->blue = 0xff;
    }
#ifndef XP_WIN32
    else if (context->type == MWContextPrint) {
        trans_pixel = context->transparent_pixel;
    }
#endif
    else {
        if (lo_image->image_attr &&
            (lo_image->image_attr->attrmask & LO_ATTR_BACKDROP) &&
			!(lo_image->image_attr->attrmask & LO_ATTR_CELL_BACKDROP) &&
			!(lo_image->image_attr->attrmask & LO_ATTR_LAYER_BACKDROP)
			) {
            trans_pixel = context->transparent_pixel;
        }
        else {
#ifdef XP_WIN32
		XP_Bool backgroundPref = FALSE;
		PREF_GetBoolPref("browser.print_background",&backgroundPref);
		if (context->type == MWContextPrint && !backgroundPref)
	        trans_pixel = context->transparent_pixel;
		else
#endif
            trans_pixel = NULL;
        }
    }

    /* Fetch the image.  We ignore the return value and only set the lo_image's
       image handle in the observer.  Any context-specific scaling of images,
       e.g. for printing, should be handled by the Front End, so we divide the
       image dimensions by the context scaling factors. */
		dummy_ireq = IL_GetImage(url_to_fetch, img_cx, obs_list,
                                      trans_pixel,
                                      lo_image->width / context->convertPixX,
                                      lo_image->height / context->convertPixY,
                                      0, net_cx);

		if(( dummy_ireq != lo_image->lowres_image_req ) && url_to_fetch )
			lo_image->image_req = dummy_ireq;

    /* Destroy the transparent pixel if this is a PostScript context. */
    if ((context->type == MWContextPostScript) && trans_pixel)
        XP_FREE(trans_pixel);

    /* The Image Library clones the dummy Net Context, so it safe to destroy
       it. */
    IL_DestroyDummyNetContext(net_cx);

    if (lo_image->lowres_image_url)
        PA_UNLOCK(lo_image->lowres_image_url);
    PA_UNLOCK(lo_image->image_url);
}

/* 
 * Fills in x, y, % width, % height of image
 * Does not change anything for absolute width/height images 
 */

void lo_FillInImageGeometry( lo_DocState *state, LO_ImageStruct *image )
{
	int32 doc_width;

	image->ele_id = NEXT_ELEMENT;
	image->x = state->x;
	image->y = state->y;

	/* Set image->width if image has a % width specified */
	doc_width = state->right_margin - state->left_margin;
	if (image->percent_width > 0) {
		int32 val = image->percent_width;
		if (state->allow_percent_width == FALSE) {
			val = 0;
		}
		else {
			val = doc_width * val / 100;
		}
		image->width = val;
	}

	/* Set image->height if image has a % height specified */
	if (image->percent_height > 0) {
		int32 val = image->percent_height;
		if (state->allow_percent_height == FALSE) {
			val = 0;
		}
		else {
			val = state->win_height * val / 100;
		}
		image->height = val;
	}
}


/* Look into possibility of reusing for other floating elements? */
void lo_LayoutFloatImage( MWContext *context, lo_DocState *state, LO_ImageStruct *image, Bool updateFE )
{
	int32 image_width;

	image_width = image->width + (2 * image->border_width) + 
		(2 * image->border_horiz_space);


	if (image->image_attr->alignment == LO_ALIGN_RIGHT)
	{
		if (state->right_margin_stack == NULL)
		{
			image->x = state->right_margin - image_width;
		}
		else
		{
			image->x = state->right_margin_stack->margin -
				image_width;
		}
		if (image->x < 0)
		{
			image->x = 0;
		}
	}
	else
	{
		image->x = state->left_margin;
	}

	image->y = -1;
	/*
	image->x_offset += (int16)image->border_horiz_space;
	image->y_offset += (int32)image->border_vert_space;	
	*/
	lo_AddMarginStack(state, image->x, image->y,
		image->width, image->height,
		image->border_width,
		image->border_vert_space, image->border_horiz_space,
		image->image_attr->alignment);

	if (state->at_begin_line != FALSE)
	{
		lo_FindLineMargins(context, state, updateFE);
		state->x = state->left_margin;
	}

}

void lo_LayoutInflowImage(MWContext *context, lo_DocState *state, LO_ImageStruct *image,
						  Bool inRelayout, int32 *line_inc, int32 *baseline_inc)
{
	PA_Block buff;
	char *str;
	LO_TextStruct tmp_text;
	LO_TextInfo text_info;
	int32 image_height, image_width;
	Bool line_break;

	/* 
	int32 baseline_inc;
	int32 line_inc;
	*/

	/*
	 * Eventually this will never happen since we always
	 * have dims here from either the image tag itself or the
	 * front end.
	 */
	if (image->width == 0)
	{
		image->width = IMAGE_DEF_DIM;
	}
	if (image->height == 0)
	{
		image->height = IMAGE_DEF_DIM;
	}

	image_width = image->width + (2 * image->border_width) +
		(2 * image->border_horiz_space);
	image_height = image->height + (2 * image->border_width) +
		(2 * image->border_vert_space);

	/*
	 * Will this image make the line too wide.
	 */
	if ((state->x + image_width) > state->right_margin)
	{
		line_break = TRUE;
	}
	else
	{
		line_break = FALSE;
	}

	/*
	 * if we are at the beginning of the line.  There is
	 * no point in breaking, we are just too wide.
	 * Also don't break in unwrapped preformatted text.
	 * Also can't break inside a NOBR section.
	 */
	if ((state->at_begin_line != FALSE)||
		(state->preformatted == PRE_TEXT_YES)||
		(state->breakable == FALSE))
	{
		line_break = FALSE;
	}

	/*
	 * break on the image if we have
	 * a break.
	 */
	if (line_break != FALSE)
	{
		/*
		 * We need to make the elements sequential, linefeed
		 * before image.
		 */
		state->top_state->element_id = image->ele_id;

		if (!inRelayout)
		{
			lo_SoftLineBreak(context, state, TRUE);
		}
		else {
			lo_rl_AddSoftBreakAndFlushLine(context, state);
		}
		image->x = state->x;
		image->y = state->y;
		image->ele_id = NEXT_ELEMENT;
	}

	/*
	 * Figure out how to align this image.
	 * baseline_inc is how much to increase the baseline
	 * of previous element of this line.  line_inc is how
	 * much to increase the line height below the baseline.
	 */
	*baseline_inc = 0;
	*line_inc = 0;

	/*
	 * All this work is to get the text_info filled in for the current
	 * font in the font stack. Yuck, there must be a better way.
	 */
	memset (&tmp_text, 0, sizeof (tmp_text));
	buff = PA_ALLOC(1);
	if (buff == NULL)
	{
		state->top_state->out_of_memory = TRUE;
		return;
	}
	PA_LOCK(str, char *, buff);
	str[0] = ' ';
	PA_UNLOCK(buff);
	tmp_text.text = buff;
	tmp_text.text_len = 1;
	tmp_text.text_attr =
#ifdef DOM
            lo_GetCurrentTextAttr(state, context);
#else
            state->font_stack->text_attr;
#endif
	FE_GetTextInfo(context, &tmp_text, &text_info);
	PA_FREE(buff);

	lo_CalcAlignOffsets(state, &text_info, image->image_attr->alignment,
		image_width, image_height,
		&image->x_offset, &image->y_offset, line_inc, baseline_inc);

	image->x_offset += (int16)image->border_horiz_space;
	image->y_offset += (int32)image->border_vert_space;
}


void lo_UpdateStateAfterImageLayout( lo_DocState *state, LO_ImageStruct *image, int32 line_inc, int32 baseline_inc )
{
	int32 image_width;
	int32 x, y;
	
	image_width = image->width + (2 * image->border_width) +
		(2 * image->border_horiz_space);

	state->baseline += (intn) baseline_inc;
	state->line_height += (intn) (baseline_inc + line_inc);
	
	/*
	 * Clean up state
	 */
	state->x = state->x + image->x_offset +
		image_width - image->border_horiz_space;
	state->linefeed_state = 0;
	state->at_begin_line = FALSE;
	state->trailing_space = FALSE;
	state->cur_ele_type = LO_IMAGE;

	/* Determine the new position of the layer. */
	x = image->x + image->x_offset + image->border_width;
	y = image->y + image->y_offset + image->border_width;

    /* Move layer to new position */
	if (image->layer != NULL)
		CL_MoveLayer(image->layer,	x, y);
}

#ifdef PROFILE
#pragma profile off
#endif