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
|
/*
*--------------------------------------------------------------------------
* Copyright (c) 2006 Dan Kennedy.
* All rights reserved.
*
* This Open Source project was made possible through the financial support
* of Eolas Technologies Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Eolas Technologies Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
#include "html.h"
#define ISNEWLINE(x) ((x) == '\n' || (x) == '\r')
#define ISTAB(x) ((x) == '\t')
#define ISSPACE(x) isspace((unsigned char)(x))
/*
* This file implements the experimental [tag] widget method. The
* following summarizes the interface supported:
*
* html tag add TAGNAME FROM-NODE FROM-INDEX TO-NODE TO-INDEX
* html tag remove TAGNAME FROM-NODE FROM-INDEX TO-NODE TO-INDEX
* html tag delete TAGNAME
* html tag configure TAGNAME ?-fg COLOR? ?-bg COLOR?
*
* This file exports the following functions:
*
* HtmlTranslateEscapes()
* Translates Html escapes (i.e. " ").
*
* HtmlTagAddRemoveCmd()
* Implementation of [pathName tag add] and [pathName tag remove]
*
* HtmlTagDeleteCmd()
* Implementation of [pathName tag delete]
*
* HtmlTagConfigureCmd()
* Implementation of [pathName tag configured]
*
* HtmlTagCleanupNode()
* HtmlTagCleanupTree()
* Respectively called when an HtmlNode or HtmlTree structure is being
* deallocated to free outstanding tag related stuff.
*
*
* Also:
*
* HtmlTextTextCmd()
* HtmlTextBboxCmd()
* HtmlTextOffsetCmd()
* HtmlTextIndexCmd()
*/
/****************** Begin Escape Sequence Translator *************/
/*
** The next section of code implements routines used to translate
** the '&' escape sequences of SGML to individual characters.
** Examples:
**
** & &
** < <
** > >
** nonbreakable space
*/
/* Each escape sequence is recorded as an instance of the following
** structure
*/
struct sgEsc {
char *zName; /* The name of this escape sequence. ex: "amp" */
char value[8]; /* The value for this sequence. ex: "&" */
struct sgEsc *pNext; /* Next sequence with the same hash on zName */
};
/* The following is a table of all escape sequences. Add new sequences
** by adding entries to this table.
*/
static struct sgEsc esc_sequences[] = {
{"nbsp", "\xC2\xA0", 0}, /* Unicode code-point 160 */
{"iexcl", "\xC2\xA1", 0}, /* Unicode code-point 161 */
{"cent", "\xC2\xA2", 0}, /* Unicode code-point 162 */
{"pound", "\xC2\xA3", 0}, /* Unicode code-point 163 */
{"curren", "\xC2\xA4", 0}, /* Unicode code-point 164 */
{"yen", "\xC2\xA5", 0}, /* Unicode code-point 165 */
{"brvbar", "\xC2\xA6", 0}, /* Unicode code-point 166 */
{"sect", "\xC2\xA7", 0}, /* Unicode code-point 167 */
{"uml", "\xC2\xA8", 0}, /* Unicode code-point 168 */
{"copy", "\xC2\xA9", 0}, /* Unicode code-point 169 */
{"ordf", "\xC2\xAA", 0}, /* Unicode code-point 170 */
{"laquo", "\xC2\xAB", 0}, /* Unicode code-point 171 */
{"not", "\xC2\xAC", 0}, /* Unicode code-point 172 */
{"shy", "\xC2\xAD", 0}, /* Unicode code-point 173 */
{"reg", "\xC2\xAE", 0}, /* Unicode code-point 174 */
{"macr", "\xC2\xAF", 0}, /* Unicode code-point 175 */
{"deg", "\xC2\xB0", 0}, /* Unicode code-point 176 */
{"plusmn", "\xC2\xB1", 0}, /* Unicode code-point 177 */
{"sup2", "\xC2\xB2", 0}, /* Unicode code-point 178 */
{"sup3", "\xC2\xB3", 0}, /* Unicode code-point 179 */
{"acute", "\xC2\xB4", 0}, /* Unicode code-point 180 */
{"micro", "\xC2\xB5", 0}, /* Unicode code-point 181 */
{"para", "\xC2\xB6", 0}, /* Unicode code-point 182 */
{"middot", "\xC2\xB7", 0}, /* Unicode code-point 183 */
{"cedil", "\xC2\xB8", 0}, /* Unicode code-point 184 */
{"sup1", "\xC2\xB9", 0}, /* Unicode code-point 185 */
{"ordm", "\xC2\xBA", 0}, /* Unicode code-point 186 */
{"raquo", "\xC2\xBB", 0}, /* Unicode code-point 187 */
{"frac14", "\xC2\xBC", 0}, /* Unicode code-point 188 */
{"frac12", "\xC2\xBD", 0}, /* Unicode code-point 189 */
{"frac34", "\xC2\xBE", 0}, /* Unicode code-point 190 */
{"iquest", "\xC2\xBF", 0}, /* Unicode code-point 191 */
{"Agrave", "\xC3\x80", 0}, /* Unicode code-point 192 */
{"Aacute", "\xC3\x81", 0}, /* Unicode code-point 193 */
{"Acirc", "\xC3\x82", 0}, /* Unicode code-point 194 */
{"Atilde", "\xC3\x83", 0}, /* Unicode code-point 195 */
{"Auml", "\xC3\x84", 0}, /* Unicode code-point 196 */
{"Aring", "\xC3\x85", 0}, /* Unicode code-point 197 */
{"AElig", "\xC3\x86", 0}, /* Unicode code-point 198 */
{"Ccedil", "\xC3\x87", 0}, /* Unicode code-point 199 */
{"Egrave", "\xC3\x88", 0}, /* Unicode code-point 200 */
{"Eacute", "\xC3\x89", 0}, /* Unicode code-point 201 */
{"Ecirc", "\xC3\x8A", 0}, /* Unicode code-point 202 */
{"Euml", "\xC3\x8B", 0}, /* Unicode code-point 203 */
{"Igrave", "\xC3\x8C", 0}, /* Unicode code-point 204 */
{"Iacute", "\xC3\x8D", 0}, /* Unicode code-point 205 */
{"Icirc", "\xC3\x8E", 0}, /* Unicode code-point 206 */
{"Iuml", "\xC3\x8F", 0}, /* Unicode code-point 207 */
{"ETH", "\xC3\x90", 0}, /* Unicode code-point 208 */
{"Ntilde", "\xC3\x91", 0}, /* Unicode code-point 209 */
{"Ograve", "\xC3\x92", 0}, /* Unicode code-point 210 */
{"Oacute", "\xC3\x93", 0}, /* Unicode code-point 211 */
{"Ocirc", "\xC3\x94", 0}, /* Unicode code-point 212 */
{"Otilde", "\xC3\x95", 0}, /* Unicode code-point 213 */
{"Ouml", "\xC3\x96", 0}, /* Unicode code-point 214 */
{"times", "\xC3\x97", 0}, /* Unicode code-point 215 */
{"Oslash", "\xC3\x98", 0}, /* Unicode code-point 216 */
{"Ugrave", "\xC3\x99", 0}, /* Unicode code-point 217 */
{"Uacute", "\xC3\x9A", 0}, /* Unicode code-point 218 */
{"Ucirc", "\xC3\x9B", 0}, /* Unicode code-point 219 */
{"Uuml", "\xC3\x9C", 0}, /* Unicode code-point 220 */
{"Yacute", "\xC3\x9D", 0}, /* Unicode code-point 221 */
{"THORN", "\xC3\x9E", 0}, /* Unicode code-point 222 */
{"szlig", "\xC3\x9F", 0}, /* Unicode code-point 223 */
{"agrave", "\xC3\xA0", 0}, /* Unicode code-point 224 */
{"aacute", "\xC3\xA1", 0}, /* Unicode code-point 225 */
{"acirc", "\xC3\xA2", 0}, /* Unicode code-point 226 */
{"atilde", "\xC3\xA3", 0}, /* Unicode code-point 227 */
{"auml", "\xC3\xA4", 0}, /* Unicode code-point 228 */
{"aring", "\xC3\xA5", 0}, /* Unicode code-point 229 */
{"aelig", "\xC3\xA6", 0}, /* Unicode code-point 230 */
{"ccedil", "\xC3\xA7", 0}, /* Unicode code-point 231 */
{"egrave", "\xC3\xA8", 0}, /* Unicode code-point 232 */
{"eacute", "\xC3\xA9", 0}, /* Unicode code-point 233 */
{"ecirc", "\xC3\xAA", 0}, /* Unicode code-point 234 */
{"euml", "\xC3\xAB", 0}, /* Unicode code-point 235 */
{"igrave", "\xC3\xAC", 0}, /* Unicode code-point 236 */
{"iacute", "\xC3\xAD", 0}, /* Unicode code-point 237 */
{"icirc", "\xC3\xAE", 0}, /* Unicode code-point 238 */
{"iuml", "\xC3\xAF", 0}, /* Unicode code-point 239 */
{"eth", "\xC3\xB0", 0}, /* Unicode code-point 240 */
{"ntilde", "\xC3\xB1", 0}, /* Unicode code-point 241 */
{"ograve", "\xC3\xB2", 0}, /* Unicode code-point 242 */
{"oacute", "\xC3\xB3", 0}, /* Unicode code-point 243 */
{"ocirc", "\xC3\xB4", 0}, /* Unicode code-point 244 */
{"otilde", "\xC3\xB5", 0}, /* Unicode code-point 245 */
{"ouml", "\xC3\xB6", 0}, /* Unicode code-point 246 */
{"divide", "\xC3\xB7", 0}, /* Unicode code-point 247 */
{"oslash", "\xC3\xB8", 0}, /* Unicode code-point 248 */
{"ugrave", "\xC3\xB9", 0}, /* Unicode code-point 249 */
{"uacute", "\xC3\xBA", 0}, /* Unicode code-point 250 */
{"ucirc", "\xC3\xBB", 0}, /* Unicode code-point 251 */
{"uuml", "\xC3\xBC", 0}, /* Unicode code-point 252 */
{"yacute", "\xC3\xBD", 0}, /* Unicode code-point 253 */
{"thorn", "\xC3\xBE", 0}, /* Unicode code-point 254 */
{"yuml", "\xC3\xBF", 0}, /* Unicode code-point 255 */
{"quot", "\x22", 0}, /* Unicode code-point 34 */
{"amp", "\x26", 0}, /* Unicode code-point 38 */
{"lt", "\x3C", 0}, /* Unicode code-point 60 */
{"gt", "\x3E", 0}, /* Unicode code-point 62 */
{"OElig", "\xC5\x92", 0}, /* Unicode code-point 338 */
{"oelig", "\xC5\x93", 0}, /* Unicode code-point 339 */
{"Scaron", "\xC5\xA0", 0}, /* Unicode code-point 352 */
{"scaron", "\xC5\xA1", 0}, /* Unicode code-point 353 */
{"Yuml", "\xC5\xB8", 0}, /* Unicode code-point 376 */
{"circ", "\xCB\x86", 0}, /* Unicode code-point 710 */
{"tilde", "\xCB\x9C", 0}, /* Unicode code-point 732 */
{"ensp", "\xE2\x80\x82", 0}, /* Unicode code-point 8194 */
{"emsp", "\xE2\x80\x83", 0}, /* Unicode code-point 8195 */
{"thinsp", "\xE2\x80\x89", 0}, /* Unicode code-point 8201 */
{"zwnj", "\xE2\x80\x8C", 0}, /* Unicode code-point 8204 */
{"zwj", "\xE2\x80\x8D", 0}, /* Unicode code-point 8205 */
{"lrm", "\xE2\x80\x8E", 0}, /* Unicode code-point 8206 */
{"rlm", "\xE2\x80\x8F", 0}, /* Unicode code-point 8207 */
{"ndash", "\xE2\x80\x93", 0}, /* Unicode code-point 8211 */
{"mdash", "\xE2\x80\x94", 0}, /* Unicode code-point 8212 */
{"lsquo", "\xE2\x80\x98", 0}, /* Unicode code-point 8216 */
{"rsquo", "\xE2\x80\x99", 0}, /* Unicode code-point 8217 */
{"sbquo", "\xE2\x80\x9A", 0}, /* Unicode code-point 8218 */
{"ldquo", "\xE2\x80\x9C", 0}, /* Unicode code-point 8220 */
{"rdquo", "\xE2\x80\x9D", 0}, /* Unicode code-point 8221 */
{"bdquo", "\xE2\x80\x9E", 0}, /* Unicode code-point 8222 */
{"dagger", "\xE2\x80\xA0", 0}, /* Unicode code-point 8224 */
{"Dagger", "\xE2\x80\xA1", 0}, /* Unicode code-point 8225 */
{"permil", "\xE2\x80\xB0", 0}, /* Unicode code-point 8240 */
{"lsaquo", "\xE2\x80\xB9", 0}, /* Unicode code-point 8249 */
{"rsaquo", "\xE2\x80\xBA", 0}, /* Unicode code-point 8250 */
{"euro", "\xE2\x82\xAC", 0}, /* Unicode code-point 8364 */
{"fnof", "\xC6\x92", 0}, /* Unicode code-point 402 */
{"Alpha", "\xCE\x91", 0}, /* Unicode code-point 913 */
{"Beta", "\xCE\x92", 0}, /* Unicode code-point 914 */
{"Gamma", "\xCE\x93", 0}, /* Unicode code-point 915 */
{"Delta", "\xCE\x94", 0}, /* Unicode code-point 916 */
{"Epsilon", "\xCE\x95", 0}, /* Unicode code-point 917 */
{"Zeta", "\xCE\x96", 0}, /* Unicode code-point 918 */
{"Eta", "\xCE\x97", 0}, /* Unicode code-point 919 */
{"Theta", "\xCE\x98", 0}, /* Unicode code-point 920 */
{"Iota", "\xCE\x99", 0}, /* Unicode code-point 921 */
{"Kappa", "\xCE\x9A", 0}, /* Unicode code-point 922 */
{"Lambda", "\xCE\x9B", 0}, /* Unicode code-point 923 */
{"Mu", "\xCE\x9C", 0}, /* Unicode code-point 924 */
{"Nu", "\xCE\x9D", 0}, /* Unicode code-point 925 */
{"Xi", "\xCE\x9E", 0}, /* Unicode code-point 926 */
{"Omicron", "\xCE\x9F", 0}, /* Unicode code-point 927 */
{"Pi", "\xCE\xA0", 0}, /* Unicode code-point 928 */
{"Rho", "\xCE\xA1", 0}, /* Unicode code-point 929 */
{"Sigma", "\xCE\xA3", 0}, /* Unicode code-point 931 */
{"Tau", "\xCE\xA4", 0}, /* Unicode code-point 932 */
{"Upsilon", "\xCE\xA5", 0}, /* Unicode code-point 933 */
{"Phi", "\xCE\xA6", 0}, /* Unicode code-point 934 */
{"Chi", "\xCE\xA7", 0}, /* Unicode code-point 935 */
{"Psi", "\xCE\xA8", 0}, /* Unicode code-point 936 */
{"Omega", "\xCE\xA9", 0}, /* Unicode code-point 937 */
{"alpha", "\xCE\xB1", 0}, /* Unicode code-point 945 */
{"beta", "\xCE\xB2", 0}, /* Unicode code-point 946 */
{"gamma", "\xCE\xB3", 0}, /* Unicode code-point 947 */
{"delta", "\xCE\xB4", 0}, /* Unicode code-point 948 */
{"epsilon", "\xCE\xB5", 0}, /* Unicode code-point 949 */
{"zeta", "\xCE\xB6", 0}, /* Unicode code-point 950 */
{"eta", "\xCE\xB7", 0}, /* Unicode code-point 951 */
{"theta", "\xCE\xB8", 0}, /* Unicode code-point 952 */
{"iota", "\xCE\xB9", 0}, /* Unicode code-point 953 */
{"kappa", "\xCE\xBA", 0}, /* Unicode code-point 954 */
{"lambda", "\xCE\xBB", 0}, /* Unicode code-point 955 */
{"mu", "\xCE\xBC", 0}, /* Unicode code-point 956 */
{"nu", "\xCE\xBD", 0}, /* Unicode code-point 957 */
{"xi", "\xCE\xBE", 0}, /* Unicode code-point 958 */
{"omicron", "\xCE\xBF", 0}, /* Unicode code-point 959 */
{"pi", "\xCF\x80", 0}, /* Unicode code-point 960 */
{"rho", "\xCF\x81", 0}, /* Unicode code-point 961 */
{"sigmaf", "\xCF\x82", 0}, /* Unicode code-point 962 */
{"sigma", "\xCF\x83", 0}, /* Unicode code-point 963 */
{"tau", "\xCF\x84", 0}, /* Unicode code-point 964 */
{"upsilon", "\xCF\x85", 0}, /* Unicode code-point 965 */
{"phi", "\xCF\x86", 0}, /* Unicode code-point 966 */
{"chi", "\xCF\x87", 0}, /* Unicode code-point 967 */
{"psi", "\xCF\x88", 0}, /* Unicode code-point 968 */
{"omega", "\xCF\x89", 0}, /* Unicode code-point 969 */
{"thetasym", "\xCF\x91", 0}, /* Unicode code-point 977 */
{"upsih", "\xCF\x92", 0}, /* Unicode code-point 978 */
{"piv", "\xCF\x96", 0}, /* Unicode code-point 982 */
{"bull", "\xE2\x80\xA2", 0}, /* Unicode code-point 8226 */
{"hellip", "\xE2\x80\xA6", 0}, /* Unicode code-point 8230 */
{"prime", "\xE2\x80\xB2", 0}, /* Unicode code-point 8242 */
{"Prime", "\xE2\x80\xB3", 0}, /* Unicode code-point 8243 */
{"oline", "\xE2\x80\xBE", 0}, /* Unicode code-point 8254 */
{"frasl", "\xE2\x81\x84", 0}, /* Unicode code-point 8260 */
{"weierp", "\xE2\x84\x98", 0}, /* Unicode code-point 8472 */
{"image", "\xE2\x84\x91", 0}, /* Unicode code-point 8465 */
{"real", "\xE2\x84\x9C", 0}, /* Unicode code-point 8476 */
{"trade", "\xE2\x84\xA2", 0}, /* Unicode code-point 8482 */
{"alefsym", "\xE2\x84\xB5", 0}, /* Unicode code-point 8501 */
{"larr", "\xE2\x86\x90", 0}, /* Unicode code-point 8592 */
{"uarr", "\xE2\x86\x91", 0}, /* Unicode code-point 8593 */
{"rarr", "\xE2\x86\x92", 0}, /* Unicode code-point 8594 */
{"darr", "\xE2\x86\x93", 0}, /* Unicode code-point 8595 */
{"harr", "\xE2\x86\x94", 0}, /* Unicode code-point 8596 */
{"crarr", "\xE2\x86\xB5", 0}, /* Unicode code-point 8629 */
{"lArr", "\xE2\x87\x90", 0}, /* Unicode code-point 8656 */
{"uArr", "\xE2\x87\x91", 0}, /* Unicode code-point 8657 */
{"rArr", "\xE2\x87\x92", 0}, /* Unicode code-point 8658 */
{"dArr", "\xE2\x87\x93", 0}, /* Unicode code-point 8659 */
{"hArr", "\xE2\x87\x94", 0}, /* Unicode code-point 8660 */
{"forall", "\xE2\x88\x80", 0}, /* Unicode code-point 8704 */
{"part", "\xE2\x88\x82", 0}, /* Unicode code-point 8706 */
{"exist", "\xE2\x88\x83", 0}, /* Unicode code-point 8707 */
{"empty", "\xE2\x88\x85", 0}, /* Unicode code-point 8709 */
{"nabla", "\xE2\x88\x87", 0}, /* Unicode code-point 8711 */
{"isin", "\xE2\x88\x88", 0}, /* Unicode code-point 8712 */
{"notin", "\xE2\x88\x89", 0}, /* Unicode code-point 8713 */
{"ni", "\xE2\x88\x8B", 0}, /* Unicode code-point 8715 */
{"prod", "\xE2\x88\x8F", 0}, /* Unicode code-point 8719 */
{"sum", "\xE2\x88\x91", 0}, /* Unicode code-point 8721 */
{"minus", "\xE2\x88\x92", 0}, /* Unicode code-point 8722 */
{"lowast", "\xE2\x88\x97", 0}, /* Unicode code-point 8727 */
{"radic", "\xE2\x88\x9A", 0}, /* Unicode code-point 8730 */
{"prop", "\xE2\x88\x9D", 0}, /* Unicode code-point 8733 */
{"infin", "\xE2\x88\x9E", 0}, /* Unicode code-point 8734 */
{"ang", "\xE2\x88\xA0", 0}, /* Unicode code-point 8736 */
{"and", "\xE2\x88\xA7", 0}, /* Unicode code-point 8743 */
{"or", "\xE2\x88\xA8", 0}, /* Unicode code-point 8744 */
{"cap", "\xE2\x88\xA9", 0}, /* Unicode code-point 8745 */
{"cup", "\xE2\x88\xAA", 0}, /* Unicode code-point 8746 */
{"int", "\xE2\x88\xAB", 0}, /* Unicode code-point 8747 */
{"there4", "\xE2\x88\xB4", 0}, /* Unicode code-point 8756 */
{"sim", "\xE2\x88\xBC", 0}, /* Unicode code-point 8764 */
{"cong", "\xE2\x89\x85", 0}, /* Unicode code-point 8773 */
{"asymp", "\xE2\x89\x88", 0}, /* Unicode code-point 8776 */
{"ne", "\xE2\x89\xA0", 0}, /* Unicode code-point 8800 */
{"equiv", "\xE2\x89\xA1", 0}, /* Unicode code-point 8801 */
{"le", "\xE2\x89\xA4", 0}, /* Unicode code-point 8804 */
{"ge", "\xE2\x89\xA5", 0}, /* Unicode code-point 8805 */
{"sub", "\xE2\x8A\x82", 0}, /* Unicode code-point 8834 */
{"sup", "\xE2\x8A\x83", 0}, /* Unicode code-point 8835 */
{"nsub", "\xE2\x8A\x84", 0}, /* Unicode code-point 8836 */
{"sube", "\xE2\x8A\x86", 0}, /* Unicode code-point 8838 */
{"supe", "\xE2\x8A\x87", 0}, /* Unicode code-point 8839 */
{"oplus", "\xE2\x8A\x95", 0}, /* Unicode code-point 8853 */
{"otimes", "\xE2\x8A\x97", 0}, /* Unicode code-point 8855 */
{"perp", "\xE2\x8A\xA5", 0}, /* Unicode code-point 8869 */
{"sdot", "\xE2\x8B\x85", 0}, /* Unicode code-point 8901 */
{"lceil", "\xE2\x8C\x88", 0}, /* Unicode code-point 8968 */
{"rceil", "\xE2\x8C\x89", 0}, /* Unicode code-point 8969 */
{"lfloor", "\xE2\x8C\x8A", 0}, /* Unicode code-point 8970 */
{"rfloor", "\xE2\x8C\x8B", 0}, /* Unicode code-point 8971 */
{"lang", "\xE2\x8C\xA9", 0}, /* Unicode code-point 9001 */
{"rang", "\xE2\x8C\xAA", 0}, /* Unicode code-point 9002 */
{"loz", "\xE2\x97\x8A", 0}, /* Unicode code-point 9674 */
{"spades", "\xE2\x99\xA0", 0}, /* Unicode code-point 9824 */
{"clubs", "\xE2\x99\xA3", 0}, /* Unicode code-point 9827 */
{"hearts", "\xE2\x99\xA5", 0}, /* Unicode code-point 9829 */
{"diams", "\xE2\x99\xA6", 0}, /* Unicode code-point 9830 */
/* Non-standard. But very common. */
{"quote", "\"", 0},
{"apos", "'", 0},
};
/* The size of the handler hash table. For best results this should
** be a prime number which is about the same size as the number of
** escape sequences known to the system. */
#define ESC_HASH_SIZE (sizeof(esc_sequences)/sizeof(esc_sequences[0])+7)
/* The hash table
**
** If the name of an escape sequences hashes to the value H, then
** apEscHash[H] will point to a linked list of Esc structures, one of
** which will be the Esc structure for that escape sequence.
*/
static struct sgEsc *apEscHash[ESC_HASH_SIZE];
/* Hash a escape sequence name. The value returned is an integer
** between 0 and ESC_HASH_SIZE-1, inclusive.
*/
static int
EscHash(zName)
const char *zName;
{
int h = 0; /* The hash value to be returned */
char c; /* The next character in the name
* being hashed */
while ((c = *zName) != 0) {
h = h << 5 ^ h ^ c;
zName++;
}
if (h < 0) {
h = -h;
}
else {
}
return h % ESC_HASH_SIZE;
}
#ifdef TEST
/*
** Compute the longest and average collision chain length for the
** escape sequence hash table
*/
static void
EscHashStats(void)
{
int i;
int sum = 0;
int max = 0;
int cnt;
int notempty = 0;
struct sgEsc *p;
for (i = 0; i < sizeof(esc_sequences) / sizeof(esc_sequences[0]); i++) {
cnt = 0;
p = apEscHash[i];
if (p)
notempty++;
while (p) {
cnt++;
p = p->pNext;
}
sum += cnt;
if (cnt > max)
max = cnt;
}
printf("Longest chain=%d avg=%g slots=%d empty=%d (%g%%)\n",
max, (double) sum / (double) notempty, i, i - notempty,
100.0 * (i - notempty) / (double) i);
}
#endif
/* Initialize the escape sequence hash table
*/
static void
EscInit()
{
int i; /* For looping thru the list of escape
* sequences */
int h; /* The hash on a sequence */
for (i = 0; i < sizeof(esc_sequences) / sizeof(esc_sequences[i]); i++) {
/* #ifdef TCL_UTF_MAX */
#if 0
{
int c = esc_sequences[i].value[0];
Tcl_UniCharToUtf(c, esc_sequences[i].value);
}
#endif
h = EscHash(esc_sequences[i].zName);
esc_sequences[i].pNext = apEscHash[h];
apEscHash[h] = &esc_sequences[i];
}
#ifdef TEST
EscHashStats();
#endif
}
/*
** This table translates the non-standard microsoft characters between
** 0x80 and 0x9f into plain ASCII so that the characters will be visible
** on Unix systems. Care is taken to translate the characters
** into values less than 0x80, to avoid UTF-8 problems.
*/
#ifndef __WIN32__
static char acMsChar[] = {
/*
* 0x80
*/ 'C',
/*
* 0x81
*/ ' ',
/*
* 0x82
*/ ',',
/*
* 0x83
*/ 'f',
/*
* 0x84
*/ '"',
/*
* 0x85
*/ '.',
/*
* 0x86
*/ '*',
/*
* 0x87
*/ '*',
/*
* 0x88
*/ '^',
/*
* 0x89
*/ '%',
/*
* 0x8a
*/ 'S',
/*
* 0x8b
*/ '<',
/*
* 0x8c
*/ 'O',
/*
* 0x8d
*/ ' ',
/*
* 0x8e
*/ 'Z',
/*
* 0x8f
*/ ' ',
/*
* 0x90
*/ ' ',
/*
* 0x91
*/ '\'',
/*
* 0x92
*/ '\'',
/*
* 0x93
*/ '"',
/*
* 0x94
*/ '"',
/*
* 0x95
*/ '*',
/*
* 0x96
*/ '-',
/*
* 0x97
*/ '-',
/*
* 0x98
*/ '~',
/*
* 0x99
*/ '@',
/*
* 0x9a
*/ 's',
/*
* 0x9b
*/ '>',
/*
* 0x9c
*/ 'o',
/*
* 0x9d
*/ ' ',
/*
* 0x9e
*/ 'z',
/*
* 0x9f
*/ 'Y',
};
#endif
static int
translateNumericEscape(z, pI)
char *z;
int *pI; /* IN/OUT: Index into string z */
{
char *zLocal = &z[*pI];
int base = 10;
int iRet = 10;
if (*zLocal == 'x' || *zLocal == 'X') {
/* Hexadecimal number */
base = 16;
zLocal++;
}
iRet = strtol(zLocal, &zLocal, base);
/* Gobble up a trailing semi-colon */
if (*zLocal == ';') {
zLocal++;
}
*pI = (zLocal - z);
return iRet;
}
/* Translate escape sequences in the string "z". "z" is overwritten
** with the translated sequence.
**
** Unrecognized escape sequences are unaltered.
**
** Example:
**
** input = "AT&T > MCI"
** output = "AT&T > MCI"
*/
void
HtmlTranslateEscapes(z)
char *z;
{
int from; /* Read characters from this position
* in z[] */
int to; /* Write characters into this position
* in z[] */
int h; /* A hash on the escape sequence */
struct sgEsc *p; /* For looping down the escape
* sequence collision chain */
static int isInit = 0; /* True after initialization */
from = to = 0;
if (!isInit) {
EscInit();
isInit = 1;
}
while (z[from]) {
if (z[from] == '&') {
if (z[from + 1] == '#') {
int i = from + 2;
int v = translateNumericEscape(z, &i);
/*
* On Unix systems, translate the non-standard microsoft **
* characters in the range of 0x80 to 0x9f into something **
* we can see.
*/
#ifndef __WIN32__
if (v >= 0x80 && v < 0xa0) {
v = acMsChar[v & 0x1f];
}
#endif
/*
* Put the character in the output stream in place of ** the
* "�". How we do this depends on whether or ** not we
* are using UTF-8.
*/
#ifdef TCL_UTF_MAX
{
int j, n;
char value[8];
n = Tcl_UniCharToUtf(v, value);
for (j = 0; j < n; j++) {
z[to++] = value[j];
}
}
#else
z[to++] = v;
#endif
from = i;
}
else {
int i = from + 1;
int c;
while (z[i] && isalnum(z[i])) {
i++;
}
c = z[i];
z[i] = 0;
h = EscHash(&z[from + 1]);
p = apEscHash[h];
while (p && strcmp(p->zName, &z[from + 1]) != 0) {
p = p->pNext;
}
z[i] = c;
if (p) {
int j;
for (j = 0; p->value[j]; j++) {
z[to++] = p->value[j];
}
from = i;
if (c == ';') {
from++;
}
}
else {
z[to++] = z[from++];
}
}
/*
* On UNIX systems, look for the non-standard microsoft
* characters ** between 0x80 and 0x9f and translate them into
* printable ASCII ** codes. Separate algorithms are required to
* do this for plain ** ascii and for utf-8.
*/
#ifndef __WIN32__
#ifdef TCL_UTF_MAX
}
else if ((z[from] & 0x80) != 0) {
Tcl_UniChar c;
int n;
n = Tcl_UtfToUniChar(&z[from], &c);
if (c >= 0x80 && c < 0xa0) {
z[to++] = acMsChar[c & 0x1f];
from += n;
}
else {
while (n--)
z[to++] = z[from++];
}
#else /* if !defined(TCL_UTF_MAX) */
}
else if (((unsigned char) z[from]) >= 0x80
&& ((unsigned char) z[from]) < 0xa0) {
z[to++] = acMsChar[z[from++] & 0x1f];
#endif /* TCL_UTF_MAX */
#endif /* __WIN32__ */
}
else {
z[to++] = z[from++];
}
}
z[to] = 0;
}
static HtmlWidgetTag *
getWidgetTag(pTree, zTag, pIsNew)
HtmlTree *pTree;
const char *zTag;
int *pIsNew;
{
Tcl_HashEntry *pEntry;
int isNew;
HtmlWidgetTag *pTag;
pEntry = Tcl_CreateHashEntry(&pTree->aTag, zTag, &isNew);
if (isNew) {
Tk_OptionTable otab = pTree->tagOptionTable;
static Tk_OptionSpec ospec[] = {
{TK_OPTION_COLOR, "-foreground", "", "", "white", -1, \
Tk_Offset(HtmlWidgetTag, foreground), 0, 0, 0},
{TK_OPTION_COLOR, "-background", "", "", "black", -1, \
Tk_Offset(HtmlWidgetTag, background), 0, 0, 0},
{TK_OPTION_SYNONYM, "-bg", 0, 0, 0, 0, -1, 0, "-background", 0},
{TK_OPTION_SYNONYM, "-fg", 0, 0, 0, 0, -1, 0, "-foreground", 0},
{TK_OPTION_END, 0, 0, 0, 0, 0, 0, 0, 0}
};
pTag = (HtmlWidgetTag *)HtmlClearAlloc("", sizeof(HtmlWidgetTag));
Tcl_SetHashValue(pEntry, pTag);
if (0 == otab) {
pTree->tagOptionTable = Tk_CreateOptionTable(pTree->interp, ospec);
otab = pTree->tagOptionTable;
assert(otab);
}
Tk_InitOptions(pTree->interp, (char *)pTag, otab, pTree->tkwin);
assert(pTag->foreground && pTag->background);
} else {
pTag = (HtmlWidgetTag *)Tcl_GetHashValue(pEntry);
}
if (pIsNew) {
*pIsNew = isNew;
}
return pTag;
}
static HtmlNode *
orderIndexPair(ppA, piA, ppB, piB)
HtmlNode **ppA;
int *piA;
HtmlNode **ppB;
int *piB;
{
HtmlNode *pA;
HtmlNode *pB;
HtmlNode *pParent;
int nDepthA = 0;
int nDepthB = 0;
int ii;
int swap = 0;
for(pA = HtmlNodeParent(*ppA); pA; pA = HtmlNodeParent(pA)) nDepthA++;
for(pB = HtmlNodeParent(*ppB); pB; pB = HtmlNodeParent(pB)) nDepthB++;
pA = *ppA;
pB = *ppB;
for(ii = 0; ii < (nDepthA - nDepthB); ii++) pA = HtmlNodeParent(pA);
for(ii = 0; ii < (nDepthB - nDepthA); ii++) pB = HtmlNodeParent(pB);
if (pA == pB) {
if (nDepthA == nDepthB) {
/* In this case *ppA and *ppB are the same node */
swap = (*piA > *piB);
} else {
/* One of (*ppA, *ppB) is a descendant of the other */
swap = (nDepthA > nDepthB);
}
pParent = pA;
} else {
while (HtmlNodeParent(pA) != HtmlNodeParent(pB)) {
pA = HtmlNodeParent(pA);
pB = HtmlNodeParent(pB);
assert(pA && pB && pA != pB);
}
pParent = HtmlNodeParent(pA);
for (ii = 0; ; ii++) {
HtmlNode *pChild = HtmlNodeChild(pParent, ii);
assert(ii < HtmlNodeNumChildren(pParent) && pChild);
if (pChild == pA) break;
if (pChild == pB) {
swap = 1;
break;
}
}
}
if (swap) {
HtmlNode *p;
int i;
p = *ppB;
*ppB = *ppA;
*ppA = p;
i = *piB;
*piB = *piA;
*piA = i;
}
return pParent;
}
/*
*---------------------------------------------------------------------------
*
* removeTagFromNode --
*
* Results:
* Returns non-zero if one or more characters of this node were tagged.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
static int
removeTagFromNode(pTextNode, pTag)
HtmlTextNode *pTextNode;
HtmlWidgetTag *pTag;
{
int eRet = 0;
HtmlTaggedRegion *pTagged = pTextNode->pTagged;
if (pTagged) {
HtmlTaggedRegion **pPtr = &pTextNode->pTagged;
while (pTagged) {
if (pTagged->pTag == pTag) {
*pPtr = pTagged->pNext;
HtmlFree(pTagged);
eRet = 1;
} else {
pPtr = &pTagged->pNext;
}
pTagged = *pPtr;
}
}
#ifndef NDEBUG
for (pTagged = pTextNode->pTagged; pTagged ; pTagged = pTagged->pNext) {
assert(pTagged->pTag != pTag);
}
#endif
return eRet;
}
static HtmlTaggedRegion *
findTagInNode(pTextNode, pTag, ppPtr)
HtmlTextNode *pTextNode;
HtmlWidgetTag *pTag;
HtmlTaggedRegion ***ppPtr;
{
HtmlTaggedRegion *pTagged;
HtmlTaggedRegion **pPtr = &pTextNode->pTagged;
for (pTagged = pTextNode->pTagged; pTagged; pTagged = pTagged->pNext) {
if (pTagged->pTag == pTag) {
*ppPtr = pPtr;
return pTagged;
}
pPtr = &pTagged->pNext;
}
*ppPtr = pPtr;
return 0;
}
typedef struct TagOpData TagOpData;
struct TagOpData {
HtmlNode *pFrom;
int iFrom;
HtmlNode *pTo;
int iTo;
int eSeenFrom; /* True after pFrom has been traversed */
HtmlWidgetTag *pTag;
int isAdd; /* True for [add] false for [remove] */
HtmlNode *pFirst;
HtmlNode *pLast;
int iFirst;
int iLast;
};
#define OVERLAP_NONE 1
#define OVERLAP_SUPER 2
#define OVERLAP_SUB 3
#define OVERLAP_FROM 4
#define OVERLAP_TO 5
#define OVERLAP_EXACT 6
static int
getOverlap(pTagged, iFrom, iTo)
HtmlTaggedRegion *pTagged;
int iFrom;
int iTo;
{
assert(iFrom <= iTo);
assert(pTagged->iFrom <= pTagged->iTo);
if (iFrom == pTagged->iFrom && iTo == pTagged->iTo) {
return OVERLAP_EXACT;
}
if (iFrom <= pTagged->iFrom && iTo >= pTagged->iTo) {
return OVERLAP_SUPER;
}
if (iFrom >= pTagged->iFrom && iTo <= pTagged->iTo) {
return OVERLAP_SUB;
}
if (iFrom > pTagged->iTo || iTo < pTagged->iFrom) {
return OVERLAP_NONE;
}
if (iFrom > pTagged->iFrom) {
assert(iFrom <= pTagged->iTo);
assert(iTo > pTagged->iTo);
return OVERLAP_TO;
}
assert(iTo >= pTagged->iFrom);
assert(iTo < pTagged->iTo);
assert(iFrom < pTagged->iFrom);
return OVERLAP_FROM;
}
static int
tagAddRemoveCallback(pTree, pNode, clientData)
HtmlTree *pTree;
HtmlNode *pNode;
ClientData clientData;
{
TagOpData *pData = (TagOpData *)clientData;
HtmlTextNode *pTextNode = HtmlNodeAsText(pNode);
if (pNode == pData->pFrom) {
assert(0 == pData->eSeenFrom);
pData->eSeenFrom = 1;
}
if (pTextNode && pData->eSeenFrom) {
HtmlTaggedRegion *pTagged;
HtmlTaggedRegion **pPtr;
int iFrom = 0;
int iTo = 1000000;
if (pNode == pData->pFrom) iFrom = pData->iFrom;
if (pNode == pData->pTo) iTo = pData->iTo;
assert(iFrom <= iTo);
pTagged = findTagInNode(pTextNode, pData->pTag, &pPtr);
assert(*pPtr == pTagged);
switch (pData->isAdd) {
case HTML_TAG_ADD:
while (pTagged && pTagged->pTag == pData->pTag) {
int e = getOverlap(pTagged, iFrom, iTo);
pPtr = &pTagged->pNext;
if (e != OVERLAP_NONE) {
if (0 == pData->pFirst) {
if (e == OVERLAP_SUPER || e == OVERLAP_FROM) {
pData->pFirst = pNode;
pData->iFirst = iFrom;
} else if (e == OVERLAP_TO) {
pData->pFirst = pNode;
pData->iFirst = pTagged->iTo;
}
}
if (e == OVERLAP_TO || e == OVERLAP_SUPER) {
pData->pLast = pNode;
pData->iLast = iTo;
} if (e == OVERLAP_FROM) {
pData->pLast = pNode;
pData->iLast = pTagged->iFrom;
}
pTagged->iFrom = MIN(pTagged->iFrom, iFrom);
pTagged->iTo = MAX(pTagged->iTo, iTo);
break;
}
pTagged = *pPtr;
}
if (!pTagged || pTagged->pTag != pData->pTag) {
HtmlTaggedRegion *pNew = (HtmlTaggedRegion *)
HtmlClearAlloc("", sizeof(HtmlTaggedRegion));
pNew->iFrom = iFrom;
pNew->iTo = iTo;
pNew->pNext = pTagged;
pNew->pTag = pData->pTag;
if (!pData->pFirst) {
pData->pFirst = pNode;
pData->iFirst = iFrom;
}
pData->pLast = pNode;
pData->iLast = iTo;
*pPtr = pNew;
}
break;
case HTML_TAG_REMOVE:
while (pTagged && pTagged->pTag == pData->pTag) {
int eOverlap = getOverlap(pTagged, iFrom, iTo);
switch (eOverlap) {
case OVERLAP_EXACT:
case OVERLAP_SUPER: {
/* Delete the whole list entry */
*pPtr = pTagged->pNext;
HtmlFree(pTagged);
break;
};
case OVERLAP_TO:
pTagged->iTo = iFrom;
pPtr = &pTagged->pNext;
break;
case OVERLAP_FROM:
pTagged->iFrom = iTo;
pPtr = &pTagged->pNext;
break;
case OVERLAP_NONE:
/* Do nothing */
pPtr = &pTagged->pNext;
break;
case OVERLAP_SUB: {
HtmlTaggedRegion *pNew = (HtmlTaggedRegion *)
HtmlClearAlloc("", sizeof(HtmlTaggedRegion));
pNew->iFrom = iTo;
pNew->iTo = pTagged->iTo;
pNew->pTag = pData->pTag;
pNew->pNext = pTagged->pNext;
pTagged->pNext = pNew;
pTagged->iTo = iFrom;
pPtr = &pNew->pNext;
break;
}
}
pTagged = *pPtr;
}
break;
}
}
if (pNode == pData->pTo) {
return HTML_WALK_ABANDON;
}
return HTML_WALK_DESCEND;
}
int
HtmlTagAddRemoveCmd(clientData, interp, objc, objv, isAdd)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
int isAdd;
{
HtmlTree *pTree = (HtmlTree *)clientData;
HtmlNode *pParent;
HtmlWidgetTag *pTag;
TagOpData sData;
memset(&sData, 0, sizeof(TagOpData));
assert(isAdd == HTML_TAG_REMOVE || isAdd == HTML_TAG_ADD);
if (objc != 8) {
Tcl_WrongNumArgs(interp, 3, objv,
"TAGNAME FROM-NODE FROM-INDEX TO-NODE TO-INDEX"
);
return TCL_ERROR;
}
if (
0 == (sData.pFrom=HtmlNodeGetPointer(pTree, Tcl_GetString(objv[4]))) ||
TCL_OK != Tcl_GetIntFromObj(interp, objv[5], &sData.iFrom) ||
0 == (sData.pTo=HtmlNodeGetPointer(pTree, Tcl_GetString(objv[6]))) ||
TCL_OK != Tcl_GetIntFromObj(interp, objv[7], &sData.iTo)
) {
return TCL_ERROR;
}
/* If either node is an orphan node, throw a Tcl exception. */
if (HtmlNodeIsOrphan(sData.pFrom)) {
Tcl_AppendResult(interp, Tcl_GetString(objv[4]), " is an orphan", 0);
return TCL_ERROR;
}
if (HtmlNodeIsOrphan(sData.pTo)) {
Tcl_AppendResult(interp, Tcl_GetString(objv[6]), " is an orphan", 0);
return TCL_ERROR;
}
pTag = getWidgetTag(pTree, Tcl_GetString(objv[3]), 0);
sData.pTag = pTag;
sData.isAdd = isAdd;
pParent = orderIndexPair(&sData.pFrom,&sData.iFrom,&sData.pTo,&sData.iTo);
HtmlWalkTree(pTree, pParent, tagAddRemoveCallback, &sData);
if (isAdd == HTML_TAG_REMOVE) {
HtmlWidgetDamageText(pTree,
sData.pFrom, sData.iFrom,
sData.pTo, sData.iTo
);
} else if (sData.pFirst) {
assert(sData.pLast);
HtmlWidgetDamageText(pTree,
sData.pFirst, sData.iFirst,
sData.pLast, sData.iLast
);
}
return TCL_OK;
}
int
HtmlTagConfigureCmd(clientData, interp, objc, objv)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
{
HtmlTree *pTree = (HtmlTree *)clientData;
Tk_OptionTable otab;
HtmlWidgetTag *pTag;
Tk_Window win = pTree->tkwin;
int isNew;
if (objc < 4) {
Tcl_WrongNumArgs(interp, 3, objv, "TAGNAME ?options?");
return TCL_ERROR;
}
pTag = getWidgetTag(pTree, Tcl_GetString(objv[3]), &isNew);
otab = pTree->tagOptionTable;
assert(otab);
Tk_SetOptions(interp, (char *)pTag, otab, objc - 4, &objv[4], win, 0, 0);
if (!isNew) {
/* Redraw the whole viewport. Todo: Update only the tagged regions */
HtmlCallbackDamage(pTree, 0, 0, 1000000, 1000000);
}
return TCL_OK;
}
struct TagDeleteContext {
HtmlWidgetTag *pTag;
int nOcc;
};
typedef struct TagDeleteContext TagDeleteContext;
static int
tagDeleteCallback(pTree, pNode, clientData)
HtmlTree *pTree;
HtmlNode *pNode;
ClientData clientData;
{
HtmlTextNode *pTextNode = HtmlNodeAsText(pNode);
if (pTextNode) {
TagDeleteContext *p = (TagDeleteContext *)clientData;
p->nOcc += removeTagFromNode(pTextNode, p->pTag);
}
return HTML_WALK_DESCEND;
}
int
HtmlTagDeleteCmd(clientData, interp, objc, objv)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
{
const char *zTag;
Tcl_HashEntry *pEntry;
HtmlTree *pTree = (HtmlTree *)clientData;
TagDeleteContext context = {0, 0};
if (objc != 4) {
Tcl_WrongNumArgs(interp, 3, objv, "TAGNAME");
return TCL_ERROR;
}
zTag = Tcl_GetString(objv[3]);
pEntry = Tcl_FindHashEntry(&pTree->aTag, zTag);
if (pEntry) {
HtmlWidgetTag *pTag = (HtmlWidgetTag *)Tcl_GetHashValue(pEntry);
context.pTag = pTag;
HtmlWalkTree(pTree, 0, tagDeleteCallback, (ClientData)&context);
HtmlFree(pTag);
Tcl_DeleteHashEntry(pEntry);
}
/* Redraw the whole viewport. Todo: Update only the required regions */
if (context.nOcc) {
HtmlCallbackDamage(pTree, 0, 0, 1000000, 1000000);
}
return TCL_OK;
}
void
HtmlTagCleanupNode(pTextNode)
HtmlTextNode *pTextNode;
{
HtmlTaggedRegion *pTagged = pTextNode->pTagged;
while (pTagged) {
HtmlTaggedRegion *pNext = pTagged->pNext;
HtmlFree(pTagged);
pTagged = pNext;
}
pTextNode->pTagged = 0;
}
void
HtmlTagCleanupTree(pTree)
HtmlTree *pTree;
{
Tcl_HashEntry *pEntry;
Tcl_HashSearch search;
pEntry = Tcl_FirstHashEntry(&pTree->aTag, &search);
for ( ; pEntry; pEntry = Tcl_NextHashEntry(&search)) {
HtmlWidgetTag *pTag = (HtmlWidgetTag *)Tcl_GetHashValue(pEntry);
Tk_FreeConfigOptions((char *)pTag, pTree->tagOptionTable, pTree->tkwin);
HtmlFree(pTag);
}
Tcl_DeleteHashTable(&pTree->aTag);
}
/*
* The following two structs are used together to create a data-structure
* to store the text-representation of the document.
*
*/
typedef struct HtmlTextMapping HtmlTextMapping;
struct HtmlTextMapping {
HtmlTextNode *pTextNode;
int iStrIndex; /* Character offset in HtmlText.pObj */
int iNodeIndex; /* Byte offset in HtmlTextNode.zText */
HtmlTextMapping *pNext;
};
struct HtmlText {
Tcl_Obj *pObj;
HtmlTextMapping *pMapping;
};
typedef struct HtmlTextInit HtmlTextInit;
struct HtmlTextInit {
HtmlText *pText;
int eState;
int iIdx;
};
#define SEEN_TEXT 0
#define SEEN_SPACE 1
#define SEEN_BLOCK 2
static void
addTextMapping(pText, pTextNode, iNodeIndex, iStrIndex)
HtmlText *pText;
HtmlTextNode *pTextNode;
int iNodeIndex;
int iStrIndex;
{
HtmlTextMapping *p;
p = (HtmlTextMapping *)HtmlAlloc("HtmlTextMapping",sizeof(HtmlTextMapping));
p->iStrIndex = iStrIndex;
p->iNodeIndex = iNodeIndex;
p->pTextNode = pTextNode;
p->pNext = pText->pMapping;
pText->pMapping = p;
}
static void
initHtmlText_TextNode(pTree, pTextNode, pInit)
HtmlTree *pTree;
HtmlTextNode *pTextNode;
HtmlTextInit *pInit;
{
HtmlNode *pNode = &pTextNode->node;
int isPre = (HtmlNodeComputedValues(pNode)->eWhitespace == CSS_CONST_PRE);
HtmlTextIter sIter;
if (pInit->eState == SEEN_BLOCK) {
Tcl_AppendToObj(pInit->pText->pObj, "\n", 1);
pInit->iIdx++;
}
for (
HtmlTextIterFirst(pTextNode, &sIter);
HtmlTextIterIsValid(&sIter);
HtmlTextIterNext(&sIter)
) {
int eType = HtmlTextIterType(&sIter);
int nData = HtmlTextIterLength(&sIter);
char const * zData = HtmlTextIterData(&sIter);
switch (eType) {
case HTML_TEXT_TOKEN_NEWLINE:
case HTML_TEXT_TOKEN_SPACE:
if (isPre) {
int ii;
const char *zWhite;
zWhite = (eType==HTML_TEXT_TOKEN_SPACE ? " " : "\n");
for (ii = 0; ii < nData; ii++) {
Tcl_AppendToObj(pInit->pText->pObj, zWhite, 1);
}
pInit->iIdx += nData;
pInit->eState = SEEN_TEXT;
} else {
pInit->eState = MAX(pInit->eState, SEEN_SPACE);
}
break;
case HTML_TEXT_TOKEN_TEXT:
if (pInit->iIdx > 0 && pInit->eState == SEEN_SPACE) {
Tcl_AppendToObj(pInit->pText->pObj, " ", 1);
pInit->iIdx++;
}
addTextMapping(pTree->pText,
pTextNode, (zData - pTextNode->zText), pInit->iIdx
);
Tcl_AppendToObj(pInit->pText->pObj, zData, nData);
pInit->eState = SEEN_TEXT;
assert(nData >= 0);
pInit->iIdx += Tcl_NumUtfChars(zData, nData);
break;
default:
assert(!"Bad return value from HtmlTextIterType()");
}
}
}
static void
initHtmlText_Elem(pTree, pElem, pInit)
HtmlTree *pTree;
HtmlElementNode *pElem;
HtmlTextInit *pInit;
{
HtmlNode *pNode = &pElem->node;
int eDisplay = HtmlNodeComputedValues(pNode)->eDisplay;
int ii;
/* If the element has "display:none" or a replacement window, do
* not consider any text children to be part of the text
* rendering of the document.
*/
if (
(eDisplay == CSS_CONST_NONE) ||
(pElem->pReplacement && pElem->pReplacement->win)
) {
return;
}
if (eDisplay != CSS_CONST_INLINE) {
pInit->eState = SEEN_BLOCK;
}
for (ii = 0; ii < HtmlNodeNumChildren(pNode); ii++) {
HtmlNode *p = HtmlNodeChild(pNode, ii);
if (HtmlNodeIsText(p)) {
initHtmlText_TextNode(pTree, HtmlNodeAsText(p), pInit);
} else {
initHtmlText_Elem(pTree, HtmlNodeAsElement(p), pInit);
}
}
if (eDisplay != CSS_CONST_INLINE) {
pInit->eState = SEEN_BLOCK;
}
}
/*
*---------------------------------------------------------------------------
*
* initHtmlText --
*
* This function is called to initialise the HtmlText data structure
* at HtmlTree.pText. If the data structure is already initialised
* this function is a no-op.
*
* Results:
* None.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
static void
initHtmlText(pTree)
HtmlTree *pTree;
{
if (!pTree->pText) {
HtmlTextInit sInit;
HtmlCallbackForce(pTree);
pTree->pText = (HtmlText *)HtmlClearAlloc(0, sizeof(HtmlText));
memset(&sInit, 0, sizeof(HtmlTextInit));
sInit.pText = pTree->pText;
sInit.pText->pObj = Tcl_NewObj();
Tcl_IncrRefCount(sInit.pText->pObj);
initHtmlText_Elem(pTree, HtmlNodeAsElement(pTree->pRoot), &sInit);
Tcl_AppendToObj(sInit.pText->pObj, "\n", 1);
}
}
void
HtmlTextInvalidate(pTree)
HtmlTree *pTree;
{
if (pTree->pText) {
HtmlText *pText = pTree->pText;
HtmlTextMapping *pMapping = pText->pMapping;
Tcl_DecrRefCount(pTree->pText->pObj);
while (pMapping) {
HtmlTextMapping *pNext = pMapping->pNext;
HtmlFree(pMapping);
pMapping = pNext;
}
HtmlFree(pTree->pText);
pTree->pText = 0;
}
}
int
HtmlTextTextCmd(clientData, interp, objc, objv)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
{
HtmlTree *pTree = (HtmlTree *)clientData;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 3, objv, "");
return TCL_ERROR;
}
initHtmlText(pTree);
Tcl_SetObjResult(interp, pTree->pText->pObj);
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* HtmlTextIndexCmd --
*
* $html text index OFFSET ?OFFSET? ?OFFSET?
*
* The argument $OFFSET is an offset into the string returned
* by [$html text text]. This Tcl command returns a list of two
* elements - the node and node index corresponding to the
* equivalent point in the document tree.
*
* Results:
* None.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
int
HtmlTextIndexCmd(clientData, interp, objc, objv)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
{
HtmlTree *pTree = (HtmlTree *)clientData;
int ii;
Tcl_Obj *p = Tcl_NewObj();
HtmlTextMapping *pMap = 0;
int iPrev = 0;
if (objc < 4) {
Tcl_WrongNumArgs(interp, 3, objv, "OFFSET ?OFFSET? ...");
return TCL_ERROR;
}
initHtmlText(pTree);
for (ii = objc-1; ii >= 3; ii--) {
int iIndex;
if (Tcl_GetIntFromObj(interp, objv[ii], &iIndex)) {
return TCL_ERROR;
}
if (pMap == 0 || iIndex > iPrev) {
pMap = pTree->pText->pMapping;
}
for ( ; pMap; pMap = pMap->pNext) {
assert(!pMap->pNext || pMap->iStrIndex >= pMap->pNext->iStrIndex);
if (pMap->iStrIndex <= iIndex || !pMap->pNext) {
int iNodeIdx = pMap->iNodeIndex;
Tcl_Obj *apObj[2];
int nExtra = iIndex - pMap->iStrIndex;
char *zExtra = &(pMap->pTextNode->zText[iNodeIdx]);
iNodeIdx += (Tcl_UtfAtIndex(zExtra, nExtra) - zExtra);
apObj[0] = HtmlNodeCommand(pTree, &pMap->pTextNode->node);
apObj[1] = Tcl_NewIntObj(iNodeIdx);
Tcl_ListObjReplace(0, p, 0, 0, 2, apObj);
break;
}
}
iPrev = iIndex;
}
Tcl_SetObjResult(interp, p);
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* HtmlTextOffsetCmd --
*
* $html text index NODE INDEX
*
* Given the supplied node/index pair, return the corresponding offset
* in the text representation of the document.
*
* The INDEX parameter is in bytes. The returned value is in characters.
*
* Results:
* None.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
int
HtmlTextOffsetCmd(clientData, interp, objc, objv)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
{
HtmlTree *pTree = (HtmlTree *)clientData;
HtmlTextMapping *pMap;
/* C interpretations of arguments passed to the Tcl command */
HtmlNode *pNode;
HtmlTextNode *pTextNode;
int iIndex;
/* Return value for the Tcl command. Anything less than 0 results
* in an empty string being returned.
*/
int iRet = -1;
if (objc != 5) {
Tcl_WrongNumArgs(interp, 3, objv, "NODE INDEX");
return TCL_ERROR;
}
if (
0 == (pNode = HtmlNodeGetPointer(pTree, Tcl_GetString(objv[3]))) ||
TCL_OK != Tcl_GetIntFromObj(interp, objv[4], &iIndex)
) {
return TCL_ERROR;
}
if (!(pTextNode = HtmlNodeAsText(pNode))) {
const char *zNode = Tcl_GetString(objv[3]);
Tcl_AppendResult(interp, zNode, " is not a text node", 0);
return TCL_ERROR;
}
initHtmlText(pTree);
for (pMap = pTree->pText->pMapping; pMap; pMap = pMap->pNext) {
if (pMap->pTextNode == pTextNode && pMap->iNodeIndex <= iIndex) {
char *zExtra = &pTextNode->zText[pMap->iNodeIndex];
int nExtra = iIndex - pMap->iNodeIndex;
iRet = pMap->iStrIndex + Tcl_NumUtfChars(zExtra, nExtra);
break;
}
}
if (iRet >= 0) {
Tcl_SetObjResult(interp, Tcl_NewIntObj(iRet));
}
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* HtmlTextBboxCmd --
*
* $html text bbox NODE1 INDEX1 NODE2 INDEX2
*
* Results:
* None.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
int
HtmlTextBboxCmd(clientData, interp, objc, objv)
ClientData clientData; /* The HTML widget */
Tcl_Interp *interp; /* The interpreter */
int objc; /* Number of arguments */
Tcl_Obj *CONST objv[]; /* List of all arguments */
{
HtmlTree *pTree = (HtmlTree *)clientData;
HtmlNode *pFrom;
HtmlNode *pTo;
int iFrom;
int iTo;
int iTop, iLeft, iBottom, iRight;
if (objc != 7) {
Tcl_WrongNumArgs(interp, 3, objv,
"FROM-NODE FROM-INDEX TO-NODE TO-INDEX"
);
return TCL_ERROR;
}
if (
0 == (pFrom=HtmlNodeGetPointer(pTree, Tcl_GetString(objv[3]))) ||
TCL_OK != Tcl_GetIntFromObj(interp, objv[4], &iFrom) ||
0 == (pTo=HtmlNodeGetPointer(pTree, Tcl_GetString(objv[5]))) ||
TCL_OK != Tcl_GetIntFromObj(interp, objv[6], &iTo)
) {
return TCL_ERROR;
}
orderIndexPair(&pFrom, &iFrom, &pTo, &iTo);
HtmlWidgetBboxText(pTree, pFrom, iFrom, pTo, iTo,
&iTop, &iLeft, &iBottom, &iRight
);
if (iTop < iBottom && iLeft < iRight) {
Tcl_Obj *pRes = Tcl_NewObj();
Tcl_ListObjAppendElement(0, pRes, Tcl_NewIntObj(iLeft));
Tcl_ListObjAppendElement(0, pRes, Tcl_NewIntObj(iTop));
Tcl_ListObjAppendElement(0, pRes, Tcl_NewIntObj(iRight));
Tcl_ListObjAppendElement(0, pRes, Tcl_NewIntObj(iBottom));
Tcl_SetObjResult(interp, pRes);
}
return TCL_OK;
}
/*
* NOTES ON INTERNALS OF HtmlTextNode:
*
* The internals of the HtmlTextNode structure should be accessed
* via the following API:
*
* HtmlTextNew()
* HtmlTextFree()
*
* HtmlTextIterFirst
* HtmlTextIterIsValid
* HtmlTextIterNext
*
* HtmlTextIterType
* HtmlTextIterLength
* HtmlTextIterData
*
* HtmlNodeIsWhitespace
*
* HtmlTextCombine()
*
* An HtmlTextNode object stores it's text in two parts:
*
* * A character string with SGML escape sequences replaced their
* UTF-8 equivalents and each block of whitespace replaced by
* a single space character.
*
* * An ordered list of tokens. Each token one of the following:
* + An unwrappable block of text (i.e. a word),
* + One or more newline characters, or
* + One or more space characters.
*
* Each token is represented by an instance of struct
* HtmlTextToken. It has 1 byte type and a 1 byte unsigned
* length indicating the number of bytes, spaces or
* newlines represented by the token.
*
* The following block of text:
*
* "abcde <NEWLINE>><<NEWLINE><NEWLINE>hello"
*
* Is represented by the data structures:
*
* HtmlTextNode.aToken = {
* {TEXT, 5}, {SPACE, 3}, {NEWLINE, 1},
* {TEXT, 2}, {NEWLINE 2},
* {TEXT, 5},
* {END, <unused>},
* }
* HtmlTextNode.zText = "abcde <> hello"
*
* Storing the text in this format means that we are well-placed
* to rationalise a good percentage calls to Tk_DrawChars() etc. in
* other parts of the widget code.
*
* Blocks of contiguous space or newline characters longer than 255
* characters (the maximum value of HtmlTextToken.n) are stored
* as two or more contiguous SPACE tokens. i.e. 650 contiguous spaces
* require the following three tokens in the aToken array:
*
* {SPACE, 255}, {SPACE, 255}, {SPACE, 140}
*
* Blocks of non-whitespace longer than 255 characters are trickier.
* They always require exactly three tokens to be added to the array.
* For a 650 byte block of text the following three tokens:
*
* {LONGTEXT, {650 >> 16} & 0xFF},
* {LONGTEXT, {650 >> 8} & 0xFF},
* {LONGTEXT, {650 >> 0} & 0xFF}
*
* If a node consists entirely of white-space, then the HtmlTextNode.zText
* string is zero bytes in length. In this case HtmlTextNode.zText is
* set to NULL, to speed up checking if the node consists entirely
* of whitespace.
*
* Todo: It's tempting to use single byte tokens, instead of two. Three
* bits for the type and five for the length. On the other hand,
* premature optimization.....
*/
struct HtmlTextToken {
unsigned char n;
unsigned char eType;
};
/* Return true if the argument is a unicode codpoint that should be handled
* as a 'cjk' character.
*/
#define ISCJK(ii) ((ii)>=0x3000 && (ii)<=0x9fff)
/*
*---------------------------------------------------------------------------
*
* utf8Read --
*
* Decode a single UTF8 character from the buffer pointed to by z.
*
* Results:
* Unicode codepoint of read character, or 0 if the end of the buffer
* has been reached.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
static Tcl_UniChar utf8Read(
const unsigned char *z, /* first byte of utf-8 character */
const unsigned char *zTerm, /* pretend this byte is 0x00 */
const unsigned char **pzNext /* write first byte past utf-8 char here */
){
static const unsigned char UtfTrans[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
};
unsigned int c = 0;
if (zTerm>z) {
c = (unsigned int)*z;
if ((c&0xC0)==0xC0) {
const unsigned char *zCsr = &z[1];
c = UtfTrans[c-0xC0];
while (zCsr!=zTerm && ((*zCsr)&0xC0)==0x80){
c = (c << 6) + ((*zCsr)&0x3F);
zCsr++;
}
*pzNext = zCsr;
} else {
*pzNext = &z[1];
}
} else {
*pzNext = zTerm;
}
return c;
}
/*
*---------------------------------------------------------------------------
*
* tokenLength --
*
* Argument zToken points at the start of a text token (a non-whitespace
* character). This function returns the number of bytes in the token.
* zEnd points to 1 byte passed the end of the buffer - reading *zEnd
* would be a seg-fault.
*
* Results:
* Length of token at zToken in bytes.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
static int
tokenLength(zToken, zEnd)
const unsigned char *zToken;
const unsigned char *zEnd;
{
Tcl_UniChar iChar = 0;
const unsigned char *zCsr = zToken;
const unsigned char *zNext = zCsr;
iChar = utf8Read(zCsr, zEnd, &zNext);
while (iChar && (iChar >= 256 || !ISSPACE(iChar)) && !ISCJK(iChar)){
zCsr = zNext;
iChar = utf8Read(zCsr, zEnd, &zNext);
}
return ((zCsr==zToken)?zNext:zCsr)-zToken;
}
/*
*---------------------------------------------------------------------------
*
* populateTextNode --
*
* This function is called to tokenize a block of document text into
* an HtmlTextNode structure. It is a helper function for HtmlTextNew().
*
* This function is designed to be called twice for each block of text
* parsed to an HtmlTextNode. The first time, it determines the space
* (number of tokens and number of bytes of text) required for the
* the new HtmlTextNode. The caller then allocates this space and
* calls the function a second time to populate the new structure.
*
* Results:
* None.
*
* Side effects:
*
*---------------------------------------------------------------------------
*/
static void
populateTextNode(n, z, pText, pnToken, pnText)
int n; /* Length of input text */
char const *z; /* Input text */
HtmlTextNode *pText; /* OUT: The structure to populate (or NULL) */
int *pnToken; /* OUT: Number of tokens required (or used) */
int *pnText; /* OUT: Bytes of text required (or used) */
{
char const *zCsr = z;
char const *zStop = &z[n];
/* A running count of the number of tokens and bytes of text storage
* required to store the parsed form of the input text.
*/
int nToken = 0;
int nText = 0;
/* This variable is used to expand tabs. */
int iCol = 0;
int isPrevTokenText = 0;
while (zCsr < zStop) {
unsigned char c = (int)(*zCsr);
char const *zStart = zCsr;
if (ISSPACE(c)) {
int nSpace = 0; /* Eventual token length */
int eType = HTML_TEXT_TOKEN_SPACE; /* Eventual token type */
if (ISNEWLINE(c)) {
eType = HTML_TEXT_TOKEN_NEWLINE;
}
do {
/* If a tab character, this is equivalent to adding between
* one and eight spaces, depending on the current value of
* variable iCol (see comments above variable declaration).
*/
if (ISTAB(*zCsr)) nSpace += (7 - (iCol%8));
/* The sequence CR (carraige return) followed by LF (line
* feed) counts as a single newline. If either is encountered
* alone, this is also a single newline. LF followed by CR
* is two newlines.
*/
if (zCsr[0] == '\r' && &zCsr[1] < zStop && zCsr[1] == '\n') {
zCsr++;
}
nSpace++;
zCsr++;
iCol += nSpace;
} while (
zCsr < zStop && nSpace < (255 - 8) && ISSPACE(*zCsr) && (
(eType == HTML_TEXT_TOKEN_NEWLINE && ISNEWLINE(*zCsr)) ||
(eType == HTML_TEXT_TOKEN_SPACE && !ISNEWLINE(*zCsr))
)
);
if (eType == HTML_TEXT_TOKEN_NEWLINE) {
iCol = 0;
}
assert(nSpace <= 255);
if (pText) {
pText->aToken[nToken].n = nSpace;
pText->aToken[nToken].eType = eType;
}
nToken++;
/* If the previous token was text, add a single space character
* to the HtmlTextNode.zText buffer. Otherwise, add nothing
* to the text buffer.
*/
if (isPrevTokenText) {
if (pText) {
pText->zText[nText] = ' ';
}
nText++;
isPrevTokenText = 0;
}
} else {
/* This block sets nThisText to the number of bytes (not
* characters) in the token starting at zStart. zCsr is
* left pointing to the byte immediately after the token.
*/
int nThisText = tokenLength(
(unsigned char *)zCsr, (unsigned char *)zStop
);
assert(zCsr == zStart);
assert(nThisText>0);
zCsr = &zCsr[nThisText];
if (nThisText > 255) {
if (pText) {
pText->aToken[nToken].eType = HTML_TEXT_TOKEN_LONGTEXT;
pText->aToken[nToken+1].eType = HTML_TEXT_TOKEN_LONGTEXT;
pText->aToken[nToken+2].eType = HTML_TEXT_TOKEN_LONGTEXT;
pText->aToken[nToken].n = ((nThisText >> 16) & 0x000000FF);
pText->aToken[nToken+1].n = ((nThisText >> 8) & 0x000000FF);
pText->aToken[nToken+2].n = (nThisText & 0x000000FF);
memcpy(&pText->zText[nText], zStart, nThisText);
}
nToken += 3;
} else {
if (pText) {
pText->aToken[nToken].eType = HTML_TEXT_TOKEN_TEXT;
pText->aToken[nToken].n = nThisText;
memcpy(&pText->zText[nText], zStart, nThisText);
}
nToken++;
}
nText += nThisText;
isPrevTokenText = 1;
iCol += nThisText;
}
}
/* Add the terminator token */
if (pText) {
pText->aToken[nToken].eType = HTML_TEXT_TOKEN_END;
}
nToken++;
if (pnToken) *pnToken = nToken;
if (pnText) *pnText = nText;
}
void
HtmlTextSet(pText, n, z, isTrimEnd, isTrimStart)
HtmlTextNode *pText;
int n;
const char *z;
int isTrimEnd;
int isTrimStart;
{
char *z2;
HtmlTextToken *pFinal;
int nText = 0;
int nToken = 0;
int nAlloc; /* Number of bytes allocated */
if (pText->aToken) {
HtmlFree(pText->aToken);
}
/* Make a temporary copy of the text and translate any embedded html
* escape characters (i.e. " "). Todo: Avoid this copy by changing
* populateTextNode() so that it deals with escapes.
*/
z2 = (char *)HtmlAlloc("temp", n + 1);
memcpy(z2, z, n);
z2[n] = '\0';
HtmlTranslateEscapes(z2);
/* Figure out how much space is required for this HtmlTextNode. */
populateTextNode(strlen(z2), z2, 0, &nToken, &nText);
assert(nText >= 0 && nToken > 0);
/* Allocate space for HtmlTextNode.aToken and HtmlTextNode.zText */
nAlloc = nText + (nToken * sizeof(HtmlTextToken));
pText->aToken = (HtmlTextToken *)HtmlClearAlloc("TextNode.aToken", nAlloc);
if (nText > 0) {
pText->zText = (char *)&pText->aToken[nToken];
} else {
/* If the node is all white-space, set HtmlTextNode.zText to NULL */
pText->zText = 0;
}
/* Populate the HtmlTextNode.aToken and zText arrays. */
populateTextNode(strlen(z2), z2, pText, 0, 0);
HtmlFree(z2);
assert(pText->aToken[nToken-1].eType == HTML_TEXT_TOKEN_END);
pFinal = &pText->aToken[nToken-2];
if (isTrimEnd && pFinal->eType == HTML_TEXT_TOKEN_NEWLINE) {
pFinal->n--;
if( pFinal->n==0 ){
pFinal->eType = HTML_TEXT_TOKEN_END;
nToken--;
}
}
if (isTrimStart && pText->aToken[0].eType == HTML_TEXT_TOKEN_NEWLINE) {
memmove(pText->aToken, &pText->aToken[1], sizeof(HtmlTextToken)*nToken);
}
#ifndef NDEBUG
/* This assert() block checks the following:
*
* 1) If there is nothing but white-space in this node, then
* HtmlTextNode.zText is set to NULL.
* 2) If there are any text elements in the node HtmlTextNode.zText
* is not set to NULL.
*
* In other words, the node is either white-space or not. This test is
* included because I am paranoid the optimized HtmlNodeIsWhitespace()
* test (pText->zText==0) will malfunction one day.
*/
if (1) {
int haveText = 0;
HtmlTextIter sIter;
HtmlTextIterFirst(pText, &sIter);
for ( ; HtmlTextIterIsValid(&sIter); HtmlTextIterNext(&sIter)) {
if (HtmlTextIterType(&sIter) == HTML_TEXT_TOKEN_TEXT) {
haveText = 1;
}
}
assert(
(!haveText && pText->zText == 0) || /* white-space */
(haveText && pText->zText) /* not white-space */
);
}
#endif
}
HtmlTextNode *
HtmlTextNew(n, z, isTrimEnd, isTrimStart)
int n;
const char *z;
int isTrimEnd;
int isTrimStart;
{
HtmlTextNode *pText;
/* Allocate space for the HtmlTextNode. */
pText = HtmlNew(HtmlTextNode);
HtmlTextSet(pText, n, z, isTrimEnd, isTrimStart);
return pText;
}
/*
*---------------------------------------------------------------------------
*
* HtmlTextCombine --
*
* Results:
*
* Side effects:
*
*---------------------------------------------------------------------------
*/
#if 0
static HtmlTextNode *
HtmlTextCombine(p1, p2)
HtmlTextNode *p1;
HtmlTextNode *p2;
{
/* HtmlTextNode *pNew; */
return 0;
}
#endif
/*
*---------------------------------------------------------------------------
*
* HtmlTextFree --
*
* Free a text-node structure allocated by HtmlTextNew().
*
* Results:
* None.
*
* Side effects:
* Text-node p is deleted.
*
*---------------------------------------------------------------------------
*/
void
HtmlTextFree(p)
HtmlTextNode *p;
{
HtmlFree(p);
}
void
HtmlTextIterFirst(pTextNode, pTextIter)
HtmlTextNode *pTextNode;
HtmlTextIter *pTextIter;
{
pTextIter->pTextNode = pTextNode;
pTextIter->iText = 0;
pTextIter->iToken = 0;
}
int
HtmlTextIterIsValid(pTextIter)
HtmlTextIter *pTextIter;
{
HtmlTextToken *p = &pTextIter->pTextNode->aToken[pTextIter->iToken];
return (p->eType != HTML_TEXT_TOKEN_END) ? 1 : 0;
}
void
HtmlTextIterNext(pTextIter)
HtmlTextIter *pTextIter;
{
HtmlTextToken *p = &pTextIter->pTextNode->aToken[pTextIter->iToken];
int eType = p[0].eType;
int eNext = p[1].eType;
assert(eType != HTML_TEXT_TOKEN_END);
if (eType == HTML_TEXT_TOKEN_TEXT) {
pTextIter->iText += p->n;
}
else if (eType == HTML_TEXT_TOKEN_LONGTEXT) {
int n = (p[0].n << 16) + (p[1].n << 8) + p[2].n;
pTextIter->iText += n;
pTextIter->iToken += 2;
}
if ((eType == HTML_TEXT_TOKEN_TEXT || eType == HTML_TEXT_TOKEN_LONGTEXT) &&
(eNext != HTML_TEXT_TOKEN_TEXT && eNext != HTML_TEXT_TOKEN_LONGTEXT)
) {
pTextIter->iText++;
}
pTextIter->iToken++;
}
int
HtmlTextIterIsLast(pTextIter)
HtmlTextIter *pTextIter;
{
HtmlTextIter sIter;
memcpy(&sIter, pTextIter, sizeof(HtmlTextIter));
HtmlTextIterNext(&sIter);
return !HtmlTextIterIsValid(&sIter);
}
int
HtmlTextIterType(pTextIter)
HtmlTextIter *pTextIter;
{
HtmlTextToken *p = &pTextIter->pTextNode->aToken[pTextIter->iToken];
if (p->eType == HTML_TEXT_TOKEN_LONGTEXT) return HTML_TEXT_TOKEN_TEXT;
return (int)(p->eType);
}
int
HtmlTextIterLength(pTextIter)
HtmlTextIter *pTextIter;
{
HtmlTextToken *p = &pTextIter->pTextNode->aToken[pTextIter->iToken];
if (p->eType == HTML_TEXT_TOKEN_LONGTEXT) {
int n = (p[0].n << 16) + (p[1].n << 8) + p[2].n;
return n;
}
return (int)(p->n);
}
const char *
HtmlTextIterData(pTextIter)
HtmlTextIter *pTextIter;
{
return (const char *)(&pTextIter->pTextNode->zText[pTextIter->iText]);
}
|