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
|
#Needs: fonts/DejaVuSerif.sfd
#
################################################################################
########################### W3C's WOFF Validator ###############################
################################################################################
#
# This software or document includes material copied from or derived from
# http://dev.w3.org/webfonts/WOFF/tools/validator/
# Copyright (C) 2012 W3C(R) (MIT, ERCIM, Keio, Beihang).
#
# This work is being provided by the copyright holders under the following
# license.
#
# License
#
# By obtaining and/or copying this work, you (the licensee) agree that you have
# read, understood, and will comply with the following terms and conditions.
#
# Permission to copy, modify, and distribute this work, with or without
# modification, for any purpose and without fee or royalty is hereby granted,
# provided that you include the following on ALL copies of the work or portions
# thereof, including modifications:
#
# The full text of this NOTICE in a location viewable to users of the
# redistributed or derivative work.
# Any pre-existing intellectual property disclaimers, notices, or terms and
# conditions. If none exist, the W3C Software and Document Short Notice should
# be included.
# Notice of any changes or modifications, through a copyright statement on
# the new code or document such as "This software or document includes material
# copied from or derived from [title and URI of the W3C document].
# Copyright (C) [YEAR] W3C(R) (MIT, ERCIM, Keio, Beihang)."
#
# Disclaimers
#
# THIS WORK IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS
# OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES
# OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
# THE SOFTWARE OR DOCUMENT WILL NOT INFRINGE ANY THIRD PARTY PATENTS,
# COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
#
# COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR
# CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENT.
#
# The name and trademarks of copyright holders may NOT be used in advertising
# or publicity pertaining to the work without specific, written prior
# permission. Title to copyright in this work will at all times remain with
# copyright holders.
#
"""
A module for validating the the file structure of WOFF Files.
*validateFont* is the only public function.
This can also be used as a command line tool for validating WOFF files.
"""
# import
import fontforge
import os
import re
import time
import sys
import struct
import zlib
import optparse
import codecs
from xml.etree import ElementTree
from xml.parsers.expat import ExpatError
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
# ----------------------
# Support: Metadata Spec
# ----------------------
"""
The Extended Metadata specifications are defined as a set of
nested Python objects. This allows for a very simple XML
validation procedure. The common element structure is as follows:
{
# ----------
# Attributes
# ----------
# In all cases, the dictionary has the attribute name at the top
# with the possible value(s) as the value. If an attribute has
# more than one representation (for exmaple xml:lang and lang)
# the two are specified as a space separated string for example
# "xml:lang lang".
# Required
"requiredAttributes" : {
# empty or one or more of the following
"name" : "default as string, list of options or None"
},
# Recommended
"recommendedAttributes" : {
# empty or one or more of the following
"name" : "default as string, list of options or None"
},
# Optional
"optionalAttributes" : {
# empty or one or more of the following
"name" : "default as string, list of options or None"
},
# -------
# Content
# -------
"contentLevel" : "not allowed", "recommended" or "required",
# --------------
# Child Elements
# --------------
# In all cases, the dictionary has the element name at the top
# with a dictionary as the value. The value dictionary defines
# the number of times the shild-element may occur along with
# the specification for the child-element.
# Required
"requiredChildElements" : {
# empty or one or more of the following
"name" : {
"minimumOccurrences" : int or None,
"maximumOccurrences" : int or None,
"spec" : {}
}
},
# Recommended
"recommendedChildElements" : {
# empty or one or more of the following
"name" : {
# minimumOccurrences is implicitly 0
"maximumOccurrences" : int or None,
"spec" : {}
}
},
# Optional
"optionalChildElements" : {
# empty or one or more of the following
"name" : {
# minimumOccurrences is implicitly 0
"maximumOccurrences" : int or None,
"spec" : {}
}
}
}
The recommendedAttributes and recommendedChildElements are optional
but they are separated from the optionalAttributes and optionalChildElements
to allow for more detailed reporting.
"""
# Metadata 1.0
# ------------
# Common Options
dirOptions_1_0 = ["ltr", "rtl"]
# Fourth-Level Elements
divSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"dir" : dirOptions_1_0,
"class" : None
},
"content" : "recommended",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {
"div" : {
"maximumOccurrences" : None,
"spec" : "recursive divSpec_1_0" # special override for recursion.
},
"span" : {
"maximumOccurrences" : None,
"spec" : "recursive spanSpec_1_0" # special override for recursion.
}
}
}
spanSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"dir" : dirOptions_1_0,
"class" : None
},
"content" : "recommended",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {
"div" : {
"maximumOccurrences" : None,
"spec" : "recursive divSpec_1_0" # special override for recursion.
},
"span" : {
"maximumOccurrences" : None,
"spec" : "recursive spanSpec_1_0" # special override for recursion.
}
}
}
# Third-Level Elements
creditSpec_1_0 = {
"requiredAttributes" : {
"name" : None
},
"recommendedAttributes" : {},
"optionalAttributes" : {
"url" : None,
"role" : None,
"dir" : dirOptions_1_0,
"class" : None
},
"content" : "not allowed",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
textSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"url" : None,
"role" : None,
"dir" : dirOptions_1_0,
"class" : None,
"xml:lang lang" : None
},
"content" : "recommended",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {
"div" : {
"maximumOccurrences" : None,
"spec" : divSpec_1_0
},
"span" : {
"maximumOccurrences" : None,
"spec" : spanSpec_1_0
}
}
}
extensionNameSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"dir" : dirOptions_1_0,
"class" : None,
"xml:lang lang" : None
},
"content" : "recommended",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
extensionValueSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"dir" : dirOptions_1_0,
"class" : None,
"xml:lang lang" : None
},
"content" : "recommended",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
extensionItemSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"id" : None
},
"content" : "not allowed",
"requiredChildElements" : {
"name" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : extensionNameSpec_1_0
},
"value" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : extensionValueSpec_1_0
}
},
"recommendedChildElements" : {
},
"optionalChildElements" : {}
}
# Second Level Elements
uniqueidSpec_1_0 = {
"requiredAttributes" : {
"id" : None
},
"recommendedAttributes" : {},
"optionalAttributes" : {},
"content" : "not allowed",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
vendorSpec_1_0 = {
"requiredAttributes" : {
"name" : None
},
"recommendedAttributes" : {},
"optionalAttributes" : {
"url" : None,
"dir" : dirOptions_1_0,
"class" : None
},
"content" : "not allowed",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
creditsSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {},
"content" : "not allowed",
"requiredChildElements" : {
"credit" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : creditSpec_1_0
}
},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
descriptionSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"url" : None,
},
"content" : "not allowed",
"requiredChildElements" : {
"text" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : textSpec_1_0
}
},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
licenseSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"url" : None,
"id" : None
},
"content" : "not allowed",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {
"text" : {
"maximumOccurrences" : None,
"spec" : textSpec_1_0
}
}
}
copyrightSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {},
"content" : "not allowed",
"requiredChildElements" : {
"text" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : textSpec_1_0
}
},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
trademarkSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {},
"content" : "not allowed",
"requiredChildElements" : {
"text" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : textSpec_1_0
}
},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
licenseeSpec_1_0 = {
"requiredAttributes" : {
"name" : None,
},
"recommendedAttributes" : {},
"optionalAttributes" : {
"dir" : dirOptions_1_0,
"class" : None
},
"content" : "not allowed",
"requiredChildElements" : {},
"recommendedChildElements" : {},
"optionalChildElements" : {}
}
extensionSpec_1_0 = {
"requiredAttributes" : {},
"recommendedAttributes" : {},
"optionalAttributes" : {
"id" : None
},
"content" : "not allowed",
"requiredChildElements" : {
"item" : {
"minimumOccurrences" : 1,
"maximumOccurrences" : None,
"spec" : extensionItemSpec_1_0
}
},
"recommendedChildElements" : {},
"optionalChildElements" : {
"name" : {
"maximumOccurrences" : None,
"spec" : extensionNameSpec_1_0
}
}
}
# First Level Elements
metadataSpec_1_0 = {
"requiredAttributes" : {
"version" : "1.0"
},
"recommendedAttributes" : {},
"optionalAttributes" : {},
"content" : "not allowed",
"requiredChildElements" : {},
"recommendedChildElements" : {
"uniqueid" : {
"maximumOccurrences" : 1,
"spec" : uniqueidSpec_1_0
}
},
"optionalChildElements" : {
"vendor" : {
"maximumOccurrences" : 1,
"spec" : vendorSpec_1_0
},
"credits" : {
"maximumOccurrences" : 1,
"spec" : creditsSpec_1_0
},
"description" : {
"maximumOccurrences" : 1,
"spec" : descriptionSpec_1_0
},
"license" : {
"maximumOccurrences" : 1,
"spec" : licenseSpec_1_0
},
"copyright" : {
"maximumOccurrences" : 1,
"spec" : copyrightSpec_1_0
},
"trademark" : {
"maximumOccurrences" : 1,
"spec" : trademarkSpec_1_0
},
"licensee" : {
"maximumOccurrences" : 1,
"spec" : licenseeSpec_1_0
},
"licensee" : {
"maximumOccurrences" : 1,
"spec" : licenseeSpec_1_0
},
"extension" : {
"maximumOccurrences" : None,
"spec" : extensionSpec_1_0
}
}
}
# ----------------------
# Support: struct Helper
# ----------------------
# This was inspired by Just van Rossum's sstruct module.
# http://fonttools.svn.sourceforge.net/svnroot/fonttools/trunk/Lib/sstruct.py
def structPack(format, obj):
keys, formatString = _structGetFormat(format)
values = []
for key in keys:
values.append(obj[key])
data = struct.pack(formatString, *values)
return data
def structUnpack(format, data):
keys, formatString = _structGetFormat(format)
size = struct.calcsize(formatString)
values = struct.unpack(formatString, data[:size])
unpacked = {}
for index, key in enumerate(keys):
value = values[index]
unpacked[key] = value
return unpacked, data[size:]
def structCalcSize(format):
keys, formatString = _structGetFormat(format)
return struct.calcsize(formatString)
_structFormatCache = {}
def _structGetFormat(format):
if format not in _structFormatCache:
keys = []
formatString = [">"] # always big endian
for line in format.strip().splitlines():
line = line.split("#", 1)[0].strip()
if not line:
continue
key, formatCharacter = line.split(":")
key = key.strip()
formatCharacter = formatCharacter.strip()
keys.append(key)
formatString.append(formatCharacter)
_structFormatCache[format] = (keys, "".join(formatString))
return _structFormatCache[format]
# -------------
# Tests: Header
# -------------
def testHeader(data, reporter):
"""
Test the WOFF header.
"""
functions = [
_testHeaderSignature,
_testHeaderFlavor,
_testHeaderLength,
_testHeaderReserved,
_testHeaderTotalSFNTSize,
_testHeaderNumTables
]
nonStoppingError = False
for function in functions:
stoppingError, nsError = function(data, reporter)
if nsError:
nonStoppingError = True
if stoppingError:
return True, nonStoppingError
return False, nonStoppingError
headerFormat = """
signature: 4s
flavor: 4s
length: L
numTables: H
reserved: H
totalSfntSize: L
majorVersion: H
minorVersion: H
metaOffset: L
metaLength: L
metaOrigLength: L
privOffset: L
privLength: L
"""
headerSize = structCalcSize(headerFormat)
def _testHeaderStructure(data, reporter):
"""
Tests:
- Header must be the proper structure.
"""
try:
structUnpack(headerFormat, data)
reporter.logPass(message="The header structure is correct.")
except:
reporter.logError(message="The header is not properly structured.")
return True, False
return False, False
def _testHeaderSignature(data, reporter):
"""
Tests:
- The signature must be "wOFF".
"""
header = unpackHeader(data)
signature = header["signature"]
if signature != b"wOFF":
reporter.logError(message="Invalid signature: %s." % signature)
return True, False
else:
reporter.logPass(message="The signature is correct.")
return False, False
def _testHeaderFlavor(data, reporter):
"""
Tests:
- The flavor should be OTTO, 0x00010000 or true. Warn if another value is found.
- If the flavor is OTTO, the CFF table must be present.
- If the flavor is not OTTO, the CFF must not be present.
- If the directory cannot be unpacked, the flavor can not be validated. Issue a warning.
"""
header = unpackHeader(data)
flavor = header["flavor"]
if flavor not in (b"OTTO", b"\000\001\000\000", b"true"):
reporter.logWarning(message="Unknown flavor: %s." % flavor)
else:
try:
tags = [table["tag"] for table in unpackDirectory(data)]
if b"CFF " in tags and flavor != b"OTTO":
reporter.logError(message="A \"CFF\" table is defined in the font and the flavor is not set to \"OTTO\".")
return False, True
elif b"CFF " not in tags and flavor == b"OTTO":
reporter.logError(message="The flavor is set to \"OTTO\" but no \"CFF\" table is defined.")
return False, True
else:
reporter.logPass(message="The flavor is a correct value.")
except:
reporter.logWarning(message="Could not validate the flavor.")
return False, False
def _testHeaderLength(data, reporter):
"""
Tests:
- The length of the data must match the defined length.
- The length of the data must be long enough for header and directory for defined number of tables.
- The length of the data must be long enough to contain the table lengths defined in the directory,
the metaLength and the privLength.
"""
header = unpackHeader(data)
length = header["length"]
numTables = header["numTables"]
minLength = headerSize + (directorySize * numTables)
if length != len(data):
reporter.logError(message="Defined length (%d) does not match actual length of the data (%d)." % (length, len(data)))
return False, True
if length < minLength:
reporter.logError(message="Invalid length defined (%d) for number of tables defined." % length)
return False, True
directory = unpackDirectory(data)
for entry in directory:
compLength = entry["compLength"]
if compLength % 4:
compLength += 4 - (compLength % 4)
minLength += compLength
metaLength = privLength = 0
if header["metaOffset"]:
metaLength = header["metaLength"]
if header["privOffset"]:
privLength = header["privLength"]
if privLength and metaLength % 4:
metaLength += 4 - (metaLength % 4)
minLength += metaLength + privLength
if length < minLength:
reporter.logError(message="Defined length (%d) does not match the required length of the data (%d)." % (length, minLength))
return False, True
reporter.logPass(message="The length defined in the header is correct.")
return False, False
def _testHeaderReserved(data, reporter):
"""
Tests:
- The reserved bit must be set to 0.
"""
header = unpackHeader(data)
reserved = header["reserved"]
if reserved != 0:
reporter.logError(message="Invalid value in reserved field (%d)." % reserved)
return False, True
else:
reporter.logPass(message="The value in the reserved field is correct.")
return False, False
def _testHeaderTotalSFNTSize(data, reporter):
"""
Tests:
- The size of the unpacked SFNT data must be a multiple of 4.
- The origLength values in the directory, with proper padding, must sum
to the totalSfntSize in the header.
"""
header = unpackHeader(data)
directory = unpackDirectory(data)
totalSfntSize = header["totalSfntSize"]
isValid = True
if totalSfntSize % 4:
reporter.logError(message="The total sfnt size (%d) is not a multiple of four." % totalSfntSize)
isValid = False
else:
numTables = header["numTables"]
requiredSize = sfntHeaderSize + (numTables * sfntDirectoryEntrySize)
for table in directory:
origLength = table["origLength"]
if origLength % 4:
origLength += 4 - (origLength % 4)
requiredSize += origLength
if totalSfntSize != requiredSize:
reporter.logError(message="The total sfnt size (%d) does not match the required sfnt size (%d)." % (totalSfntSize, requiredSize))
isValid = False
if isValid:
reporter.logPass(message="The total sfnt size is valid.")
return False, not isValid
def _testHeaderNumTables(data, reporter):
"""
Tests:
- The number of tables must be at least 1.
- The directory entries for the specified number of tables must be properly formatted.
"""
header = unpackHeader(data)
numTables = header["numTables"]
if numTables < 1:
reporter.logError(message="Invalid number of tables defined in header structure (%d)." % numTables)
return False, True
data = data[headerSize:]
for index in range(numTables):
try:
d, data = structUnpack(directoryFormat, data)
except:
reporter.logError(message="The defined number of tables in the header (%d) does not match the actual number of tables (%d)." % (numTables, index))
return False, True
reporter.logPass(message="The number of tables defined in the header is valid.")
return False, False
# -------------
# Tests: Tables
# -------------
def testDataBlocks(data, reporter):
"""
Test the WOFF data blocks.
"""
functions = [
_testBlocksOffsetLengthZero,
_testBlocksPositioning
]
nonStoppingError = False
for function in functions:
stoppingError, nsError = function(data, reporter)
if nsError:
nonStoppingError = True
if stoppingError:
return True, nonStoppingError
return False, nonStoppingError
def _testBlocksOffsetLengthZero(data, reporter):
"""
- The metadata must have the offset and length set to zero consistently.
- The private data must have the offset and length set to zero consistently.
"""
header = unpackHeader(data)
haveError = False
# metadata
metaOffset = header["metaOffset"]
metaLength = header["metaLength"]
if metaOffset == 0 or metaLength == 0:
if metaOffset == 0 and metaLength == 0:
reporter.logPass(message="The length and offset are appropriately set for empty metadata.")
else:
reporter.logError(message="The metadata offset (%d) and metadata length (%d) are not properly set. If one is 0, they both must be 0." % (metaOffset, metaLength))
haveError = True
# private data
privOffset = header["privOffset"]
privLength = header["privLength"]
if privOffset == 0 or privLength == 0:
if privOffset == 0 and privLength == 0:
reporter.logPass(message="The length and offset are appropriately set for empty private data.")
else:
reporter.logError(message="The private data offset (%d) and private data length (%d) are not properly set. If one is 0, they both must be 0." % (privOffset, privLength))
haveError = True
return False, haveError
def _testBlocksPositioning(data, reporter):
"""
Tests:
- The table data must start immediately after the directory.
- The table data must end at the beginning of the metadata, the beginning of the private data or the end of the file.
- The metadata must start immediately after the table data.
- the metadata must end at the beginning of he private data (padded as needed) or the end of the file.
- The private data must start immediately after the table data or metadata.
- The private data must end at the edge of the file.
"""
header = unpackHeader(data)
haveError = False
# table data start
directory = unpackDirectory(data)
if not directory:
return False, False
expectedTableDataStart = headerSize + (directorySize * header["numTables"])
offsets = [entry["offset"] for entry in directory]
tableDataStart = min(offsets)
if expectedTableDataStart != tableDataStart:
reporter.logError(message="The table data does not start (%d) in the required position (%d)." % (tableDataStart, expectedTableDataStart))
haveError = True
else:
reporter.logPass(message="The table data begins in the required position.")
# table data end
if header["metaOffset"]:
definedTableDataEnd = header["metaOffset"]
elif header["privOffset"]:
definedTableDataEnd = header["privOffset"]
else:
definedTableDataEnd = header["length"]
directory = unpackDirectory(data)
ends = [table["offset"] + table["compLength"] + calcPaddingLength(table["compLength"]) for table in directory]
expectedTableDataEnd = max(ends)
if expectedTableDataEnd != definedTableDataEnd:
reporter.logError(message="The table data end (%d) is not in the required position (%d)." % (definedTableDataEnd, expectedTableDataEnd))
haveError = True
else:
reporter.logPass(message="The table data ends in the required position.")
# metadata
if header["metaOffset"]:
# start
expectedMetaStart = expectedTableDataEnd
definedMetaStart = header["metaOffset"]
if expectedMetaStart != definedMetaStart:
reporter.logError(message="The metadata does not start (%d) in the required position (%d)." % (definedMetaStart, expectedMetaStart))
haveError = True
else:
reporter.logPass(message="The metadata begins in the required position.")
# end
if header["privOffset"]:
definedMetaEnd = header["privOffset"]
needMetaPadding = True
else:
definedMetaEnd = header["length"]
needMetaPadding = False
expectedMetaEnd = header["metaOffset"] + header["metaLength"]
if needMetaPadding:
expectedMetaEnd += calcPaddingLength(header["metaLength"])
if expectedMetaEnd != definedMetaEnd:
reporter.logError(message="The metadata end (%d) is not in the required position (%d)." % (definedMetaEnd, expectedMetaEnd))
haveError = True
else:
reporter.logPass(message="The metadata ends in the required position.")
# private data
if header["privOffset"]:
# start
if header["metaOffset"]:
expectedPrivateStart = expectedMetaEnd
else:
expectedPrivateStart = expectedTableDataEnd
definedPrivateStart = header["privOffset"]
if expectedPrivateStart != definedPrivateStart:
reporter.logError(message="The private data does not start (%d) in the required position (%d)." % (definedPrivateStart, expectedPrivateStart))
haveError = True
else:
reporter.logPass(message="The private data begins in the required position.")
# end
expectedPrivateEnd = header["length"]
definedPrivateEnd = header["privOffset"] + header["privLength"]
if expectedPrivateEnd != definedPrivateEnd:
reporter.logError(message="The private data end (%d) is not in the required position (%d)." % (definedPrivateEnd, expectedPrivateEnd))
haveError = True
else:
reporter.logPass(message="The private data ends in the required position.")
return False, haveError
# ----------------------
# Tests: Table Directory
# ----------------------
def testTableDirectory(data, reporter):
"""
Test the WOFF table directory.
"""
functions = [
_testTableDirectoryStructure,
_testTableDirectory4ByteOffsets,
_testTableDirectoryPadding,
_testTableDirectoryPositions,
_testTableDirectoryCompressedLength,
_testTableDirectoryDecompressedLength,
_testTableDirectoryChecksums,
_testTableDirectoryTableOrder
]
nonStoppingError = False
for function in functions:
stoppingError, nsError = function(data, reporter)
if nsError:
nonStoppingError = True
if stoppingError:
return True, nonStoppingError
return False, nonStoppingError
directoryFormat = """
tag: 4s
offset: L
compLength: L
origLength: L
origChecksum: L
"""
directorySize = structCalcSize(directoryFormat)
def _testTableDirectoryStructure(data, reporter):
"""
Tests:
- The entries in the table directory can be unpacked.
"""
header = unpackHeader(data)
numTables = header["numTables"]
data = data[headerSize:]
try:
for index in range(numTables):
table, data = structUnpack(directoryFormat, data)
reporter.logPass(message="The table directory structure is correct.")
except:
reporter.logError(message="The table directory is not properly structured.")
return True, False
return False, False
def _testTableDirectory4ByteOffsets(data, reporter):
"""
Tests:
- The font tables must each begin on a 4-byte boundary.
"""
directory = unpackDirectory(data)
haveError = False
for table in directory:
tag = table["tag"]
offset = table["offset"]
if offset % 4:
reporter.logError(message="The \"%s\" table does not begin on a 4-byte boundary (%d)." % (tag, offset))
haveError = True
else:
reporter.logPass(message="The \"%s\" table begins on a 4-byte boundary." % tag)
return False, haveError
def _testTableDirectoryPadding(data, reporter):
"""
Tests:
- All tables, including the final table, must be padded to a
four byte boundary using null bytes as needed.
"""
header = unpackHeader(data)
directory = unpackDirectory(data)
haveError = False
# test final table
endError = False
sfntEnd = None
if header["metaOffset"] != 0:
sfntEnd = header["metaOffset"]
elif header["privOffset"] != 0:
sfntEnd = header["privOffset"]
else:
sfntEnd = header["length"]
if sfntEnd % 4:
reporter.logError(message="The sfnt data does not end with proper padding.")
haveError = True
else:
reporter.logPass(message="The sfnt data ends with proper padding.")
# test the bytes used for padding
for table in directory:
tag = table["tag"]
offset = table["offset"]
length = table["compLength"]
paddingLength = calcPaddingLength(length)
if paddingLength:
paddingOffset = offset + length
padding = data[paddingOffset:paddingOffset+paddingLength]
expectedPadding = b"\0" * paddingLength
if padding != expectedPadding:
reporter.logError(message="The \"%s\" table is not padded with null bytes." % tag)
haveError = True
else:
reporter.logPass(message="The \"%s\" table is padded with null bytes." % tag)
return False, haveError
def _testTableDirectoryPositions(data, reporter):
"""
Tests:
- The table offsets must not be before the end of the header/directory.
- The table offset + length must not be greater than the edge of the available space.
- The table offsets must not be after the edge of the available space.
- Table blocks must not overlap.
- There must be no gaps between the tables.
"""
directory = unpackDirectory(data)
tablesWithProblems = set()
haveError = False
# test for overlapping tables
locations = []
for table in directory:
offset = table["offset"]
length = table["compLength"]
length = length + calcPaddingLength(length)
locations.append((offset, offset + length, table["tag"]))
for start, end, tag in locations:
for otherStart, otherEnd, otherTag in locations:
if tag == otherTag:
continue
if start >= otherStart and start < otherEnd:
reporter.logError(message="The \"%s\" table overlaps the \"%s\" table." % (tag, otherTag))
tablesWithProblems.add(tag)
tablesWithProblems.add(otherTag)
haveError = True
# test for invalid offset, length and combo
header = unpackHeader(data)
if header["metaOffset"] != 0:
tableDataEnd = header["metaOffset"]
elif header["privOffset"] != 0:
tableDataEnd = header["privOffset"]
else:
tableDataEnd = header["length"]
numTables = header["numTables"]
minOffset = headerSize + (directorySize * numTables)
maxLength = tableDataEnd - minOffset
for table in directory:
tag = table["tag"]
offset = table["offset"]
length = table["compLength"]
# offset is before the beginning of the table data block
if offset < minOffset:
tablesWithProblems.add(tag)
message = "The \"%s\" table directory entry offset (%d) is before the start of the table data block (%d)." % (tag, offset, minOffset)
reporter.logError(message=message)
haveError = True
# offset is after the end of the table data block
elif offset > tableDataEnd:
tablesWithProblems.add(tag)
message = "The \"%s\" table directory entry offset (%d) is past the end of the table data block (%d)." % (tag, offset, tableDataEnd)
reporter.logError(message=message)
haveError = True
# offset + length is after the end of the table tada block
elif (offset + length) > tableDataEnd:
tablesWithProblems.add(tag)
message = "The \"%s\" table directory entry offset (%d) + length (%d) is past the end of the table data block (%d)." % (tag, offset, length, tableDataEnd)
reporter.logError(message=message)
haveError = True
# test for gaps
tables = []
for table in directory:
tag = table["tag"]
offset = table["offset"]
length = table["compLength"]
length += calcPaddingLength(length)
tables.append((offset, offset + length, tag))
tables.sort()
for index, (start, end, tag) in enumerate(tables):
if index == 0:
continue
prevStart, prevEnd, prevTag = tables[index - 1]
if prevEnd < start:
tablesWithProblems.add(prevTag)
tablesWithProblems.add(tag)
reporter.logError(message="Extraneous data between the \"%s\" and \"%s\" tables." % (prevTag, tag))
haveError = True
# log passes
for entry in directory:
tag = entry["tag"]
if tag in tablesWithProblems:
continue
reporter.logPass(message="The \"%s\" table directory entry has a valid offset and length." % tag)
return False, haveError
def _testTableDirectoryCompressedLength(data, reporter):
"""
Tests:
- The compressed length must be less than or equal to the original length.
"""
directory = unpackDirectory(data)
haveError = False
for table in directory:
tag = table["tag"]
compLength = table["compLength"]
origLength = table["origLength"]
if compLength > origLength:
reporter.logError(message="The \"%s\" table directory entry has a compressed length (%d) larger than the original length (%d)." % (tag, compLength, origLength))
haveError = True
elif compLength == origLength:
reporter.logPass(message="The \"%s\" table directory entry is not compressed." % tag)
else:
reporter.logPass(message="The \"%s\" table directory entry has proper compLength and origLength values." % tag)
return False, haveError
def _testTableDirectoryDecompressedLength(data, reporter):
"""
Tests:
- The decompressed length of the data must match the defined original length.
"""
directory = unpackDirectory(data)
tableData = unpackTableData(data)
haveError = False
for table in directory:
tag = table["tag"]
offset = table["offset"]
compLength = table["compLength"]
origLength = table["origLength"]
if compLength >= origLength:
continue
decompressedData = tableData[tag]
# couldn't be decompressed. handled elsewhere.
if decompressedData is None:
continue
decompressedLength = len(decompressedData)
if origLength != decompressedLength:
reporter.logError(message="The \"%s\" table directory entry has an original length (%d) that does not match the actual length of the decompressed data (%d)." % (tag, origLength, decompressedLength))
haveError = True
else:
reporter.logPass(message="The \"%s\" table directory entry has a proper original length compared to the actual decompressed data." % tag)
return False, haveError
def _testTableDirectoryChecksums(data, reporter):
"""
Tests:
- The checksums for the tables must match the checksums in the directory.
- The head checksum adjustment must be correct.
"""
# check the table directory checksums
directory = unpackDirectory(data)
tables = unpackTableData(data)
haveError = False
for entry in directory:
tag = entry["tag"]
origChecksum = entry["origChecksum"]
decompressedData = tables[tag]
# couldn't be decompressed.
if decompressedData is None:
continue
newChecksum = calcChecksum(tag, decompressedData)
if newChecksum != origChecksum:
newChecksum = hex(newChecksum).strip("L")
origChecksum = hex(origChecksum).strip("L")
reporter.logError(message="The \"%s\" table directory entry original checksum (%s) does not match the checksum (%s) calculated from the data." % (tag, origChecksum, newChecksum))
haveError = True
else:
reporter.logPass(message="The \"%s\" table directory entry original checksum is correct." % tag)
# check the head checksum adjustment
if b"head" not in tables:
reporter.logWarning(message="The font does not contain a \"head\" table.")
else:
newChecksum = calcHeadChecksum(data)
data = tables[b"head"]
try:
checksum = struct.unpack(">L", data[8:12])[0]
if checksum != newChecksum:
checksum = hex(checksum).strip("L")
newChecksum = hex(newChecksum).strip("L")
reporter.logError(message="The \"head\" table checkSumAdjustment (%s) does not match the calculated checkSumAdjustment (%s)." % (checksum, newChecksum))
haveError = True
else:
reporter.logPass(message="The \"head\" table checkSumAdjustment is valid.")
except:
reporter.logError(message="The \"head\" table is not properly structured.")
haveError = True
return False, haveError
def _testTableDirectoryTableOrder(data, reporter):
"""
Tests:
- The directory entries must be stored in ascending order based on their tag.
"""
storedOrder = [table["tag"] for table in unpackDirectory(data)]
if storedOrder != sorted(storedOrder):
reporter.logError(message="The table directory entries are not stored in alphabetical order.")
return False, True
else:
reporter.logPass(message="The table directory entries are stored in the proper order.")
return False, False
# -----------------
# Tests: Table Data
# -----------------
def testTableData(data, reporter):
"""
Test the table data.
"""
functions = [
_testTableDataDecompression
]
nonStoppingError = False
for function in functions:
stoppingError, nsError = function(data, reporter)
if nsError:
nonStoppingError = True
if stoppingError:
return True, nonStoppingError
return False, nonStoppingError
def _testTableDataDecompression(data, reporter):
"""
Tests:
- The table data, when the defined compressed length is less
than the original length, must be properly compressed.
"""
haveError = False
for table in unpackDirectory(data):
tag = table["tag"]
offset = table["offset"]
compLength = table["compLength"]
origLength = table["origLength"]
if origLength <= compLength:
continue
entryData = data[offset:offset+compLength]
try:
decompressed = zlib.decompress(entryData)
reporter.logPass(message="The \"%s\" table data can be decompressed with zlib." % tag)
except zlib.error:
reporter.logError(message="The \"%s\" table data can not be decompressed with zlib." % tag)
haveError = True
return False, haveError
# ----------------
# Tests: Metadata
# ----------------
def testMetadata(data, reporter):
"""
Test the WOFF metadata.
"""
if _shouldSkipMetadataTest(data, reporter):
return False, False
functions = [
_testMetadataPadding,
_testMetadataDecompression,
_testMetadataDecompressedLength,
_testMetadataParse,
_testMetadataEncoding,
_testMetadataStructure
]
nonStoppingError = False
for function in functions:
stoppingError, nsError = function(data, reporter)
if nsError:
nonStoppingError = True
if stoppingError:
return True, nonStoppingError
return False, nonStoppingError
def _shouldSkipMetadataTest(data, reporter):
"""
This is used at the start of metadata test functions.
It writes a note and returns True if not metadata exists.
"""
header = unpackHeader(data)
metaOffset = header["metaOffset"]
metaLength = header["metaLength"]
if metaOffset == 0 or metaLength == 0:
reporter.logNote(message="No metadata to test.")
return True
def _testMetadataPadding(data, reporter):
"""
- The padding must be null.
"""
header = unpackHeader(data)
if not header["metaOffset"] or not header["privOffset"]:
return False, False
paddingLength = calcPaddingLength(header["metaLength"])
if not paddingLength:
return False, False
paddingOffset = header["metaOffset"] + header["metaLength"]
padding = data[paddingOffset:paddingOffset + paddingLength]
expectedPadding = "\0" * paddingLength
if padding != expectedPadding:
reporter.logError(message="The metadata is not padded with null bytes.")
return False, True
else:
reporter.logPass(message="The metadata is padded with null bytes,")
return False, False
# does this need to be tested?
#
# def testMetadataIsCompressed(data, reporter):
# """
# Tests:
# - The metadata must be compressed.
# """
# if _shouldSkipMetadataTest(data, reporter):
# return
# header = unpackHeader(data)
# length = header["metaLength"]
# origLength = header["metaOrigLength"]
# if length >= origLength:
# reporter.logError(message="The compressed metdata length (%d) is higher than or equal to the original, uncompressed length (%d)." % (length, origLength))
# return True
# reporter.logPass(message="The compressed metdata length is smaller than the original, uncompressed length.")
def _testMetadataDecompression(data, reporter):
"""
Tests:
- Metadata must be compressed with zlib.
"""
if _shouldSkipMetadataTest(data, reporter):
return False, False
compData = unpackMetadata(data, decompress=False, parse=False)
try:
zlib.decompress(compData)
except zlib.error:
reporter.logError(message="The metadata can not be decompressed with zlib.")
return True, False
reporter.logPass(message="The metadata can be decompressed with zlib.")
return False, False
def _testMetadataDecompressedLength(data, reporter):
"""
Tests:
- The length of the decompressed metadata must match the defined original length.
"""
if _shouldSkipMetadataTest(data, reporter):
return False, False
header = unpackHeader(data)
metadata = unpackMetadata(data, parse=False)
metaOrigLength = header["metaOrigLength"]
decompressedLength = len(metadata)
if metaOrigLength != decompressedLength:
reporter.logError(message="The decompressed metadata length (%d) does not match the original metadata length (%d) in the header." % (decompressedLength, metaOrigLength))
return False, True
else:
reporter.logPass(message="The decompressed metadata length matches the original metadata length in the header.")
return False, False
def _testMetadataParse(data, reporter):
"""
Tests:
- The metadata must be well-formed.
"""
if _shouldSkipMetadataTest(data, reporter):
return False, False
metadata = unpackMetadata(data, parse=False)
try:
tree = ElementTree.fromstring(metadata)
except (ExpatError, LookupError):
reporter.logError(message="The metadata can not be parsed.")
return True, False
reporter.logPass(message="The metadata can be parsed.")
return False, False
def _testMetadataEncoding(data, reporter):
"""
Tests:
- The metadata must be UTF-8 encoded.
"""
if _shouldSkipMetadataTest(data, reporter):
return False, False
metadata = unpackMetadata(data, parse=False)
errorMessage = "The metadata encoding is not valid."
encoding = None
# check the BOM
if not metadata.startswith("<"):
if not metadata.startswith(codecs.BOM_UTF8):
reporter.logError(message=errorMessage)
return False, True
else:
encoding = "UTF-8"
# sniff the encoding
else:
# quick test to ensure that the regular expression will work.
# the string must start with <?xml. this will catch
# other encodings such as: <\x00?\x00x\x00m\x00l
if not metadata.startswith("<?xml"):
reporter.logError(message=errorMessage)
return False, True
# go to the first occurance of >
line = metadata.split(">", 1)[0]
# find an encoding string
pattern = re.compile(
"\s+"
"encoding"
"\s*"
"="
"\s*"
"[\"']+"
"([^\"']+)"
)
m = pattern.search(line)
if m:
encoding = m.group(1)
else:
encoding = "UTF-8"
# report
if encoding != "UTF-8":
reporter.logError(message=errorMessage)
return False, True
else:
reporter.logPass(message="The metadata is properly encoded.")
return False, False
def _testMetadataStructure(data, reporter):
"""
Test the metadata structure.
"""
if _shouldSkipMetadataTest(data, reporter):
return False, False
tree = unpackMetadata(data)
# make sure the top element is metadata
if tree.tag != "metadata":
reporter.logError("The top element is not \"metadata\".")
return False, True
# sniff the version
version = tree.attrib.get("version")
if not version:
reporter.logError("The \"version\" attribute is not defined.")
return False, True
# grab the appropriate specification
versionSpecs = {
"1.0" : metadataSpec_1_0
}
spec = versionSpecs.get(version)
if spec is None:
reporter.logError("Unknown version (\"%s\")." % version)
return False, True
haveError = _validateMetadataElement(tree, spec, reporter)
if not haveError:
reporter.logPass("The \"metadata\" element is properly formatted.")
return False, haveError
def _validateMetadataElement(element, spec, reporter, parentTree=[]):
haveError = False
# unknown attributes
knownAttributes = []
for attrib in spec["requiredAttributes"].keys() + spec["recommendedAttributes"].keys() + spec["optionalAttributes"].keys():
attrib = _parseAttribute(attrib)
knownAttributes.append(attrib)
for attrib in sorted(element.attrib.keys()):
# the search is a bit complicated because there are
# attributes that have more than one name.
found = False
for knownAttrib in knownAttributes:
if knownAttrib == attrib:
found = True
break
elif isinstance(knownAttrib, list) and attrib in knownAttrib:
found = True
break
if not found:
_logMetadataResult(
reporter,
"error",
"Unknown attribute (\"%s\")" % attrib,
element.tag,
parentTree
)
haveError = True
# attributes
s = [
("requiredAttributes", "required"),
("recommendedAttributes", "recommended"),
("optionalAttributes", "optional")
]
for key, requirementLevel in s:
if spec[key]:
e = _validateAttributes(element, spec[key], reporter, parentTree, requirementLevel)
if e:
haveError = True
# unknown child-elements
knownChildElements = spec["requiredChildElements"].keys() + spec["recommendedChildElements"].keys() + spec["optionalChildElements"].keys()
for childElement in element:
if childElement.tag not in knownChildElements:
_logMetadataResult(
reporter,
"error",
"Unknown child-element (\"%s\")" % childElement.tag,
element.tag,
parentTree
)
haveError = True
# child elements
s = [
("requiredChildElements", "required"),
("recommendedChildElements", "recommended"),
("optionalChildElements", "optional")
]
for key, requirementLevel in s:
if spec[key]:
for childElementTag, childElementData in sorted(spec[key].items()):
e = _validateChildElements(element, childElementTag, childElementData, reporter, parentTree, requirementLevel)
if e:
haveError = True
# content
content = element.text
if content is not None:
content = content.strip()
if content and spec["content"] == "not allowed":
_logMetadataResult(
reporter,
"error",
"Content defined",
element.tag,
parentTree
)
haveError = True
elif not content and content and spec["content"] == "required":
_logMetadataResult(
reporter,
"error",
"Content not defined",
element.tag,
parentTree
)
elif not content and spec["content"] == "recommended":
_logMetadataResult(
reporter,
"warn",
"Content not defined",
element.tag,
parentTree
)
# log the result
if not haveError and parentTree == ["metadata"]:
reporter.logPass("The \"%s\" element is properly formatted." % element.tag)
# done
return haveError
def _parseAttribute(attrib):
if " " in attrib:
final = []
for a in attrib.split(" "):
if a.startswith("xml:"):
a = "{http://www.w3.org/XML/1998/namespace}" + a[4:]
final.append(a)
return final
return attrib
def _unEtreeAttribute(attrib):
ns = "{http://www.w3.org/XML/1998/namespace}"
if attrib.startswith(ns):
attrib = "xml:" + attrib[len(ns):]
return attrib
def _validateAttributes(element, spec, reporter, parentTree, requirementLevel):
haveError = False
for attrib, valueOptions in sorted(spec.items()):
attribs = _parseAttribute(attrib)
if isinstance(attribs, basestring):
attribs = [attribs]
found = []
for attrib in attribs:
if attrib in element.attrib:
found.append(attrib)
# make strings for reporting
if len(attribs) > 1:
attribString = ", ".join(["\"%s\"" % _unEtreeAttribute(i) for i in attribs])
else:
attribString = "\"%s\"" % attribs[0]
if len(found) == 0:
pass
elif len(found) > 1:
foundString = ", ".join(["\"%s\"" % _unEtreeAttribute(i) for i in found])
else:
foundString = "\"%s\"" % found[0]
# more than one of the mutually exclusive attributes found
if len(found) > 1:
_logMetadataResult(
reporter,
"error",
"More than one mutually exclusive attribute (%s) defined" % foundString,
element.tag,
parentTree
)
haveError = True
# missing
elif len(found) == 0:
if requirementLevel == "optional":
continue
elif requirementLevel == "required":
errorLevel = "error"
else:
errorLevel = "warn"
_logMetadataResult(
reporter,
errorLevel,
"%s \"%s\" attribute not defined" % (requirementLevel.title(), attrib),
element.tag,
parentTree
)
if requirementLevel == "required":
haveError = True
# incorrect value
else:
e = _validateAttributeValue(element, found[0], valueOptions, reporter, parentTree)
if e:
haveError = True
# done
return haveError
def _validateAttributeValue(element, attrib, valueOptions, reporter, parentTree):
haveError = False
value = element.attrib[attrib]
if isinstance(valueOptions, basestring):
valueOptions = [valueOptions]
# no defined value options
if valueOptions is None:
# the string is empty
if not value:
_logMetadataResult(
reporter,
"warn",
"Value for the \"%s\" attribute is an empty string" % attrib,
element.tag,
parentTree
)
# illegal value
elif value not in valueOptions:
_logMetadataResult(
reporter,
"error",
"Invalid value (\"%s\") for the \"%s\" attribute" % (value, attrib),
element.tag,
parentTree
)
haveError = True
# return the error state
return haveError
def _validateChildElements(element, childElementTag, childElementData, reporter, parentTree, requirementLevel):
haveError = False
# get the valid counts
minimumOccurrences = childElementData.get("minimumOccurrences", 0)
maximumOccurrences = childElementData.get("maximumOccurrences", None)
# find the appropriate elements
found = element.findall(childElementTag)
# not defined enough times
if minimumOccurrences == 1 and len(found) == 0:
_logMetadataResult(
reporter,
"error",
"%s \"%s\" child-element not defined" % (requirementLevel.title(), childElementTag),
element.tag,
parentTree
)
haveError = True
elif len(found) < minimumOccurrences:
_logMetadataResult(
reporter,
"error",
"%s \"%s\" child-element is defined %d times instead of the minimum %d times" % (requirementLevel.title(), childElementTag, len(found), minimumOccurrences),
element.tag,
parentTree
)
haveError = True
# not defined, but not recommended
elif len(found) == 0 and requirementLevel == "recommended":
_logMetadataResult(
reporter,
"warn",
"%s \"%s\" child-element is not defined" % (requirementLevel.title(), childElementTag),
element.tag,
parentTree
)
# defined too many times
if maximumOccurrences is not None:
if maximumOccurrences == 1 and len(found) > 1:
_logMetadataResult(
reporter,
"error",
"%s \"%s\" child-element defined more than once" % (requirementLevel.title(), childElementTag),
element.tag,
parentTree
)
haveError = True
elif len(found) > maximumOccurrences:
_logMetadataResult(
reporter,
"error",
"%s \"%s\" child-element defined %d times instead of the maximum %d times" % (requirementLevel.title(), childElementTag, len(found), minimumOccurrences),
element.tag,
parentTree
)
haveError = True
# validate the found elements
if not haveError:
for childElement in found:
# handle recursive child-elements
childElementSpec = childElementData["spec"]
if childElementSpec == "recursive divSpec_1_0":
childElementSpec = divSpec_1_0
elif childElementSpec == "recursive spanSpec_1_0":
childElementSpec = spanSpec_1_0
# dive
e = _validateMetadataElement(childElement, childElementSpec, reporter, parentTree + [element.tag])
if e:
haveError = True
# return the error state
return haveError
# logging support
def _logMetadataResult(reporter, result, message, elementTag, parentTree):
message = _formatMetadataResultMessage(message, elementTag, parentTree)
methods = {
"error" : reporter.logError,
"warn" : reporter.logWarning,
"note" : reporter.logNote,
"pass" : reporter.logPass
}
methods[result](message)
def _formatMetadataResultMessage(message, elementTag, parentTree):
parentTree = parentTree + [elementTag]
if parentTree[0] == "metadata":
parentTree = parentTree[1:]
if parentTree:
parentTree = ["\"%s\"" % t for t in reversed(parentTree) if t is not None]
message += " in " + " in ".join(parentTree)
message += "."
return message
# ----------------
# Metadata Display
# ----------------
def getMetadataForDisplay(data):
"""
Build a tree of the metadata. The value returned will
be a list of elements in the following dict form:
{
tag : {
attributes : {
attribute : value
},
text : string,
children : []
}
}
The value for "children" will be a list of elements
folowing the same structure defined above.
"""
test = unpackMetadata(data, parse=False)
if not test:
return None
metadata = unpackMetadata(data)
tree = []
for element in metadata:
_recurseMetadataElement(element, tree)
return tree
def _recurseMetadataElement(element, tree):
# tag
tag = element.tag
# text
text = element.text
if text:
text = text.strip()
if not text:
text = None
# attributes
attributes = {}
ns = "{http://www.w3.org/XML/1998/namespace}"
for key, value in element.attrib.items():
if key.startswith(ns):
key = "xml:" + key[len(ns):]
attributes[key] = value
# compile the dictionary
d = dict(
tag=tag,
attributes=attributes,
text=text,
children=[]
)
tree.append(d)
# run through the children
for sub in element:
_recurseMetadataElement(sub, d["children"])
# -------------------------
# Support: Misc. SFNT Stuff
# -------------------------
# Some of this was adapted from fontTools.ttLib.sfnt
sfntHeaderFormat = """
sfntVersion: 4s
numTables: H
searchRange: H
entrySelector: H
rangeShift: H
"""
sfntHeaderSize = structCalcSize(sfntHeaderFormat)
sfntDirectoryEntryFormat = """
tag: 4s
checkSum: L
offset: L
length: L
"""
sfntDirectoryEntrySize = structCalcSize(sfntDirectoryEntryFormat)
def maxPowerOfTwo(value):
exponent = 0
while value:
value = value >> 1
exponent += 1
return max(exponent - 1, 0)
def getSearchRange(numTables):
exponent = maxPowerOfTwo(numTables)
searchRange = (2 ** exponent) * 16
entrySelector = exponent
rangeShift = numTables * 16 - searchRange
return searchRange, entrySelector, rangeShift
def calcPaddingLength(length):
if not length % 4:
return 0
return 4 - (length % 4)
def padData(data):
data += b"\0" * calcPaddingLength(len(data))
return data
def sumDataULongs(data):
longs = struct.unpack(">%dL" % (len(data) / 4), data)
value = sum(longs) % (2 ** 32)
return value
def calcChecksum(tag, data):
if tag == b"head":
data = data[:8] + b"\0\0\0\0" + data[12:]
data = padData(data)
value = sumDataULongs(data)
return value
def calcHeadChecksum(data):
header = unpackHeader(data)
directory = unpackDirectory(data)
numTables = header["numTables"]
# build the sfnt directory
searchRange, entrySelector, rangeShift = getSearchRange(numTables)
sfntHeaderData = dict(
sfntVersion=header["flavor"],
numTables=numTables,
searchRange=searchRange,
entrySelector=entrySelector,
rangeShift=rangeShift
)
sfntData = structPack(sfntHeaderFormat, sfntHeaderData)
sfntEntries = {}
offset = sfntHeaderSize + (sfntDirectoryEntrySize * numTables)
directory = [(entry["offset"], entry) for entry in directory]
for o, entry in sorted(directory):
checksum = entry["origChecksum"]
tag = entry["tag"]
length = entry["origLength"]
sfntEntries[tag] = dict(
tag=tag,
checkSum=checksum,
offset=offset,
length=length
)
offset += length + calcPaddingLength(length)
for tag, sfntEntry in sorted(sfntEntries.items()):
sfntData += structPack(sfntDirectoryEntryFormat, sfntEntry)
# calculate
checkSums = [entry["checkSum"] for entry in sfntEntries.values()]
checkSums.append(sumDataULongs(sfntData))
checkSum = sum(checkSums)
checkSum = (0xB1B0AFBA - checkSum) & 0xffffffff
return checkSum
# ------------------
# Support XML Writer
# ------------------
class XMLWriter(object):
def __init__(self):
self._root = None
self._elements = []
def simpletag(self, tag, **kwargs):
ElementTree.SubElement(self._elements[-1], tag, **kwargs)
def begintag(self, tag, **kwargs):
if self._elements:
s = ElementTree.SubElement(self._elements[-1], tag, **kwargs)
else:
s = ElementTree.Element(tag, **kwargs)
if self._root is None:
self._root = s
self._elements.append(s)
def endtag(self, tag):
assert self._elements[-1].tag == tag
del self._elements[-1]
def write(self, text):
if self._elements[-1].text is None:
self._elements[-1].text = text
else:
self._elements[-1].text += text
def compile(self, encoding="utf-8"):
f = StringIO()
tree = ElementTree.ElementTree(self._root)
indent(tree.getroot())
tree.write(f, encoding=encoding)
text = f.getvalue()
del f
return text
def indent(elem, level=0):
# this is from http://effbot.python-hosting.com/file/effbotlib/ElementTree.py
i = "\n" + level * "\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
for e in elem:
indent(e, level + 1)
if not e.tail or not e.tail.strip():
e.tail = i
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# ---------------------------------
# Support: Reporters and HTML Stuff
# ---------------------------------
class TestResultGroup(list):
def __init__(self, title):
super(TestResultGroup, self).__init__()
self.title = title
def _haveType(self, tp):
for data in self:
if data["type"] == tp:
return True
return False
def haveNote(self):
return self._haveType("NOTE")
def haveWarning(self):
return self._haveType("WARNING")
def haveError(self):
return self._haveType("ERROR")
def havePass(self):
return self._haveType("PASS")
def haveTraceback(self):
return self._haveType("TRACEBACK")
class BaseReporter(object):
"""
Base reporter. This establishes the required API for reporters.
"""
def __init__(self):
self.title = ""
self.fileInfo = []
self.metadata = None
self.testResults = []
self.haveReadError = False
def logTitle(self, title):
self.title = title
def logFileInfo(self, title, value):
self.fileInfo.append((title, value))
def logMetadata(self, data):
self.metadata = data
def logTableInfo(self, tag=None, offset=None, compLength=None, origLength=None, origChecksum=None):
self.tableInfo.append((tag, offset, compLength, origLength, origChecksum))
def logTestTitle(self, title):
self.testResults.append(TestResultGroup(title))
def logNote(self, message, information=""):
d = dict(type="NOTE", message=message, information=information)
self.testResults[-1].append(d)
def logWarning(self, message, information=""):
d = dict(type="WARNING", message=message, information=information)
self.testResults[-1].append(d)
def logError(self, message, information=""):
d = dict(type="ERROR", message=message, information=information)
self.testResults[-1].append(d)
def logPass(self, message, information=""):
d = dict(type="PASS", message=message, information=information)
self.testResults[-1].append(d)
def logTraceback(self, text):
d = dict(type="TRACEBACK", message=text, information="")
self.testResults[-1].append(d)
def getReport(self, *args, **kwargs):
raise NotImplementedError
class TextReporter(BaseReporter):
"""
Plain text reporter.
"""
def getReport(self, reportNote=True, reportWarning=True, reportError=True, reportPass=True):
report = []
if self.metadata is not None:
report.append("METADATA DISPLAY")
for group in self.testResults:
for result in group:
typ = result["type"]
if typ == "NOTE" and not reportNote:
continue
elif typ == "WARNING" and not reportWarning:
continue
elif typ == "ERROR" and not reportError:
continue
elif typ == "PASS" and not reportPass:
continue
t = "%s - %s: %s" % (result["type"], group.title, result["message"])
report.append(t)
return "\n".join(report)
class HTMLReporter(BaseReporter):
def getReport(self):
writer = startHTML(title=self.title)
# write the file info
self._writeFileInfo(writer)
# write major error alert
if self.haveReadError:
self._writeMajorError(writer)
# write the metadata
if self.metadata:
self._writeMetadata(writer)
# write the test overview
self._writeTestResultsOverview(writer)
# write the test groups
self._writeTestResults(writer)
# close the html
text = finishHTML(writer)
# done
return text
def _writeFileInfo(self, writer):
# write the font info
writer.begintag("div", c_l_a_s_s="infoBlock")
## title
writer.begintag("h3", c_l_a_s_s="infoBlockTitle")
writer.write("File Information")
writer.endtag("h3")
## table
writer.begintag("table", c_l_a_s_s="report")
## items
for title, value in self.fileInfo:
# row
writer.begintag("tr")
# title
writer.begintag("td", c_l_a_s_s="title")
writer.write(title)
writer.endtag("td")
# message
writer.begintag("td")
writer.write(value)
writer.endtag("td")
# close row
writer.endtag("tr")
writer.endtag("table")
## close the container
writer.endtag("div")
def _writeMajorError(self, writer):
writer.begintag("h2", c_l_a_s_s="readError")
writer.write("The file contains major structural errors!")
writer.endtag("h2")
def _writeMetadata(self, writer):
# start the block
writer.begintag("div", c_l_a_s_s="infoBlock")
# title
writer.begintag("h3", c_l_a_s_s="infoBlockTitle")
writer.write("Metadata ")
writer.endtag("h3")
# content
for element in self.metadata:
self._writeMetadataElement(element, writer)
# close the block
writer.endtag("div")
def _writeMetadataElement(self, element, writer):
writer.begintag("div", c_l_a_s_s="metadataElement")
# tag
writer.begintag("h5", c_l_a_s_s="metadata")
writer.write(element["tag"])
writer.endtag("h5")
# attributes
attributes = element["attributes"]
if len(attributes):
writer.begintag("h6", c_l_a_s_s="metadata")
writer.write("Attributes:")
writer.endtag("h6")
# key, value pairs
writer.begintag("table", c_l_a_s_s="metadata")
for key, value in sorted(attributes.items()):
writer.begintag("tr")
writer.begintag("td", c_l_a_s_s="key")
writer.write(key)
writer.endtag("td")
writer.begintag("td", c_l_a_s_s="value")
writer.write(value)
writer.endtag("td")
writer.endtag("tr")
writer.endtag("table")
# text
text = element["text"]
if text is not None and text.strip():
writer.begintag("h6", c_l_a_s_s="metadata")
writer.write("Text:")
writer.endtag("h6")
writer.begintag("p", c_l_a_s_s="metadata")
writer.write(text)
writer.endtag("p")
# child elements
children = element["children"]
if len(children):
writer.begintag("h6", c_l_a_s_s="metadata")
writer.write("Child Elements:")
writer.endtag("h6")
for child in children:
self._writeMetadataElement(child, writer)
# close
writer.endtag("div")
def _writeTestResultsOverview(self, writer):
## tabulate
notes = 0
passes = 0
errors = 0
warnings = 0
for group in self.testResults:
for data in group:
tp = data["type"]
if tp == "NOTE":
notes += 1
elif tp == "PASS":
passes += 1
elif tp == "ERROR":
errors += 1
else:
warnings += 1
total = sum((notes, passes, errors, warnings))
## container
writer.begintag("div", c_l_a_s_s="infoBlock")
## header
writer.begintag("h3", c_l_a_s_s="infoBlockTitle")
writer.write("Results for %d Tests" % total)
writer.endtag("h3")
## results
results = [
("PASS", passes),
("WARNING", warnings),
("ERROR", errors),
("NOTE", notes),
]
writer.begintag("table", c_l_a_s_s="report")
for tp, value in results:
# title
writer.begintag("tr", c_l_a_s_s="testReport%s" % tp.title())
writer.begintag("td", c_l_a_s_s="title")
writer.write(tp)
writer.endtag("td")
# count
writer.begintag("td", c_l_a_s_s="testReportResultCount")
writer.write(str(value))
writer.endtag("td")
# empty
writer.begintag("td")
writer.endtag("td")
# toggle button
buttonID = "testResult%sToggleButton" % tp
writer.begintag("td",
id=buttonID, c_l_a_s_s="toggleButton",
onclick="testResultToggleButtonHit(a_p_o_s_t_r_o_p_h_e%sa_p_o_s_t_r_o_p_h_e, a_p_o_s_t_r_o_p_h_e%sa_p_o_s_t_r_o_p_h_e);" % (buttonID, "test%s" % tp.title()))
writer.write("Hide")
writer.endtag("td")
# close the row
writer.endtag("tr")
writer.endtag("table")
## close the container
writer.endtag("div")
def _writeTestResults(self, writer):
for infoBlock in self.testResults:
# container
writer.begintag("div", c_l_a_s_s="infoBlock")
# header
writer.begintag("h4", c_l_a_s_s="infoBlockTitle")
writer.write(infoBlock.title)
writer.endtag("h4")
# individual reports
writer.begintag("table", c_l_a_s_s="report")
for data in infoBlock:
tp = data["type"]
message = data["message"]
information = data["information"]
# row
writer.begintag("tr", c_l_a_s_s="test%s" % tp.title())
# title
writer.begintag("td", c_l_a_s_s="title")
writer.write(tp)
writer.endtag("td")
# message
writer.begintag("td")
writer.write(message)
## info
if information:
writer.begintag("p", c_l_a_s_s="info")
writer.write(information)
writer.endtag("p")
writer.endtag("td")
# close row
writer.endtag("tr")
writer.endtag("table")
# close container
writer.endtag("div")
defaultCSS = """
body {
background-color: #e5e5e5;
padding: 15px 15px 0px 15px;
margin: 0px;
font-family: Helvetica, Verdana, Arial, sans-serif;
}
h2.readError {
background-color: red;
color: white;
margin: 20px 15px 20px 15px;
padding: 10px;
border-radius: 5px;
font-size: 25px;
}
/* info blocks */
.infoBlock {
background-color: white;
margin: 0px 0px 15px 0px;
padding: 15px;
border-radius: 5px;
}
h3.infoBlockTitle {
font-size: 20px;
margin: 0px 0px 15px 0px;
padding: 0px 0px 10px 0px;
border-bottom: 1px solid #e5e5e5;
}
h4.infoBlockTitle {
font-size: 17px;
margin: 0px 0px 15px 0px;
padding: 0px 0px 10px 0px;
border-bottom: 1px solid #e5e5e5;
}
table.report {
border-collapse: collapse;
width: 100%;
font-size: 14px;
}
table.report tr {
border-top: 1px solid white;
}
table.report tr.testPass, table.report tr.testReportPass {
background-color: #c8ffaf;
}
table.report tr.testError, table.report tr.testReportError {
background-color: #ffc3af;
}
table.report tr.testWarning, table.report tr.testReportWarning {
background-color: #ffe1af;
}
table.report tr.testNote, table.report tr.testReportNote {
background-color: #96e1ff;
}
table.report tr.testTraceback, table.report tr.testReportTraceback {
background-color: red;
color: white;
}
table.report td {
padding: 7px 5px 7px 5px;
vertical-align: top;
}
table.report td.title {
width: 80px;
text-align: right;
font-weight: bold;
text-transform: uppercase;
}
table.report td.testReportResultCount {
width: 100px;
}
table.report td.toggleButton {
text-align: center;
width: 50px;
border-left: 1px solid white;
cursor: pointer;
}
.infoBlock td p.info {
font-size: 12px;
font-style: italic;
margin: 5px 0px 0px 0px;
}
/* SFNT table */
table.sfntTableData {
font-size: 14px;
width: 100%;
border-collapse: collapse;
padding: 0px;
}
table.sfntTableData th {
padding: 5px 0px 5px 0px;
text-align: left
}
table.sfntTableData tr.uncompressed {
background-color: #ffc3af;
}
table.sfntTableData td {
width: 20%;
padding: 5px 0px 5px 0px;
border: 1px solid #e5e5e5;
border-left: none;
border-right: none;
font-family: Consolas, Menlo, "Vera Mono", Monaco, monospace;
}
pre {
font-size: 12px;
font-family: Consolas, Menlo, "Vera Mono", Monaco, monospace;
margin: 0px;
padding: 0px;
}
/* Metadata */
.metadataElement {
background: rgba(0, 0, 0, 0.03);
margin: 10px 0px 10px 0px;
border: 2px solid #d8d8d8;
padding: 10px;
}
h5.metadata {
font-size: 14px;
margin: 5px 0px 10px 0px;
padding: 0px 0px 5px 0px;
border-bottom: 1px solid #d8d8d8;
}
h6.metadata {
font-size: 12px;
font-weight: normal;
margin: 10px 0px 10px 0px;
padding: 0px 0px 5px 0px;
border-bottom: 1px solid #d8d8d8;
}
table.metadata {
font-size: 12px;
width: 100%;
border-collapse: collapse;
padding: 0px;
}
table.metadata td.key {
width: 5em;
padding: 5px 5px 5px 0px;
border-right: 1px solid #d8d8d8;
text-align: right;
vertical-align: top;
}
table.metadata td.value {
padding: 5px 0px 5px 5px;
border-left: 1px solid #d8d8d8;
text-align: left;
vertical-align: top;
}
p.metadata {
font-size: 12px;
font-style: italic;
}
}
"""
defaultJavascript = """
//<![CDATA[
function testResultToggleButtonHit(buttonID, className) {
// change the button title
var element = document.getElementById(buttonID);
if (element.innerHTML == "Show" ) {
element.innerHTML = "Hide";
}
else {
element.innerHTML = "Show";
}
// toggle the elements
var elements = getTestResults(className);
for (var e = 0; e < elements.length; ++e) {
toggleElement(elements[e]);
}
// toggle the info blocks
toggleInfoBlocks();
}
function getTestResults(className) {
var rows = document.getElementsByTagName("tr");
var found = Array();
for (var r = 0; r < rows.length; ++r) {
var row = rows[r];
if (row.className == className) {
found[found.length] = row;
}
}
return found;
}
function toggleElement(element) {
if (element.style.display != "none" ) {
element.style.display = "none";
}
else {
element.style.display = "";
}
}
function toggleInfoBlocks() {
var tables = document.getElementsByTagName("table")
for (var t = 0; t < tables.length; ++t) {
var table = tables[t];
if (table.className == "report") {
var haveVisibleRow = false;
var rows = table.rows;
for (var r = 0; r < rows.length; ++r) {
var row = rows[r];
if (row.style.display == "none") {
var i = 0;
}
else {
haveVisibleRow = true;
}
}
var div = table.parentNode;
if (haveVisibleRow == true) {
div.style.display = "";
}
else {
div.style.display = "none";
}
}
}
}
//]]>
"""
def startHTML(title=None, cssReplacements={}):
writer = XMLWriter()
# start the html
writer.begintag("html", xmlns="http://www.w3.org/1999/xhtml", lang="en")
# start the head
writer.begintag("head")
writer.simpletag("meta", http_equiv="Content-Type", content="text/html; charset=utf-8")
# title
if title is not None:
writer.begintag("title")
writer.write(title)
writer.endtag("title")
# write the css
writer.begintag("style", type="text/css")
css = defaultCSS
for before, after in cssReplacements.items():
css = css.replace(before, after)
writer.write(css)
writer.endtag("style")
# write the javascript
writer.begintag("script", type="text/javascript")
javascript = defaultJavascript
## hack around some ElementTree escaping
javascript = javascript.replace("<", "l_e_s_s")
javascript = javascript.replace(">", "g_r_e_a_t_e_r")
writer.write(javascript)
writer.endtag("script")
# close the head
writer.endtag("head")
# start the body
writer.begintag("body")
# return the writer
return writer
def finishHTML(writer):
# close the body
writer.endtag("body")
# close the html
writer.endtag("html")
# get the text
text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
text += writer.compile()
text = text.replace("c_l_a_s_s", "class")
text = text.replace("a_p_o_s_t_r_o_p_h_e", "'")
text = text.replace("l_e_s_s", "<")
text = text.replace("g_r_e_a_t_e_r", ">")
text = text.replace("http_equiv", "http-equiv")
# return
return text
# ------------------
# Support: Unpackers
# ------------------
def unpackHeader(data):
return structUnpack(headerFormat, data)[0]
def unpackDirectory(data):
header = unpackHeader(data)
numTables = header["numTables"]
data = data[headerSize:]
directory = []
for index in range(numTables):
table, data = structUnpack(directoryFormat, data)
directory.append(table)
return directory
def unpackTableData(data):
directory = unpackDirectory(data)
tables = {}
for entry in directory:
tag = entry["tag"]
offset = entry["offset"]
origLength = entry["origLength"]
compLength = entry["compLength"]
if offset > len(data) or offset < 0 or (offset + compLength) < 0:
tableData = ""
elif offset + compLength > len(data):
tableData = data[offset:]
else:
tableData = data[offset:offset+compLength]
if compLength < origLength:
try:
td = zlib.decompress(tableData)
tableData = td
except zlib.error:
tableData = None
tables[tag] = tableData
return tables
def unpackMetadata(data, decompress=True, parse=True):
header = unpackHeader(data)
data = data[header["metaOffset"]:header["metaOffset"]+header["metaLength"]]
if decompress and data:
data = zlib.decompress(data)
if parse and data:
data = ElementTree.fromstring(data)
return data
def unpackPrivateData(data):
header = unpackHeader(data)
data = data[header["privOffset"]:header["privOffset"]+header["privLength"]]
return data
# -----------------------
# Support: Report Helpers
# -----------------------
def findUniqueFileName(path):
if not os.path.exists(path):
return path
folder = os.path.dirname(path)
fileName = os.path.basename(path)
fileName, extension = os.path.splitext(fileName)
stamp = time.strftime("%Y-%m-%d %H-%M-%S %Z")
newFileName = "%s (%s)%s" % (fileName, stamp, extension)
newPath = os.path.join(folder, newFileName)
# intentionally break to prevent a file overwrite.
# this could happen if the user has a directory full
# of files with future time stamped file names.
# not likely, but avoid it all the same.
assert not os.path.exists(newPath)
return newPath
# ---------------
# Public Function
# ---------------
def validateFont(path, options, writeFile=True):
# start the reporter
if options.outputFormat == "html":
reporter = HTMLReporter()
elif options.outputFormat == "text":
reporter = TextReporter()
else:
raise NotImplementedError
# log the title
reporter.logTitle("Report: %s" % os.path.basename(path))
# log fileinfo
reporter.logFileInfo("FILE", os.path.basename(path))
reporter.logFileInfo("DIRECTORY", os.path.dirname(path))
# run tests and log results
f = open(path, "rb")
data = f.read()
f.close()
haveReadError = False
canDisplayMetadata = True
while 1:
# the goal here is to locate as many errors as possible in
# one session, rather than stopping validation at the first
# instance of an error. to do this, each test function returns
# two booleans indicating the following:
# 1. errors were found that should cease all further tests.
# 2. errors were found, but futurther tests can proceed.
# this is important because displaying metadata for a file
# with errors must not happen.
# header
reporter.logTestTitle("Header")
stoppingError, nonStoppingError = testHeader(data, reporter)
if nonStoppingError:
canDisplayMetadata = False
if stoppingError:
haveReadError = True
break
# data blocks
reporter.logTestTitle("Data Blocks")
stoppingError, nonStoppingError = testDataBlocks(data, reporter)
if nonStoppingError:
canDisplayMetadata = False
if stoppingError:
haveReadError = True
break
# table directory
reporter.logTestTitle("Table Directory")
stoppingError, nonStoppingError = testTableDirectory(data, reporter)
if nonStoppingError:
canDisplayMetadata = False
if stoppingError:
haveReadError = True
break
# table data
reporter.logTestTitle("Table Data")
stoppingError, nonStoppingError = testTableData(data, reporter)
if nonStoppingError:
canDisplayMetadata = False
if stoppingError:
haveReadError = True
break
# metadata
reporter.logTestTitle("Metadata")
stoppingError, nonStoppingError = testMetadata(data, reporter)
if nonStoppingError:
canDisplayMetadata = False
if stoppingError:
haveReadError = True
break
# done
break
reporter.haveReadError = haveReadError
# report the metadata
if not haveReadError and canDisplayMetadata:
metadata = getMetadataForDisplay(data)
reporter.logMetadata(metadata)
# get the report
report = reporter.getReport()
# write
reportPath = None
if writeFile:
# make the output file name
if options.outputFileName is not None:
fileName = options.outputFileName
else:
fileName = os.path.splitext(os.path.basename(path))[0]
fileName += "_validate"
if options.outputFormat == "html":
fileName += ".html"
else:
fileName += ".txt"
# make the output directory
if options.outputDirectory is not None:
directory = options.outputDirectory
else:
directory = os.path.dirname(path)
# write the file
reportPath = os.path.join(directory, fileName)
reportPath = findUniqueFileName(reportPath)
f = open(reportPath, "wb")
f.write(report)
f.close()
return reportPath, report
################################################################################
############################### Fontforge test #################################
################################################################################
# Generate a WOFF file with fontforge.
fontname=sys.argv[1]
woffname = "%s.woff" % os.path.splitext(os.path.basename(fontname))[0]
font=fontforge.open(fontname)
font.generate(woffname)
font.close()
# Use the W3C's validator (code above) on the output WOFF file.
class defaultOptions(object):
def __init__( self ):
self.outputFormat = "text"
reportPath, report = validateFont(woffname, defaultOptions(), False)
os.remove(woffname)
# Check the validation report and raise assertion if an ERROR is found.
for line in report.split(os.linesep):
if line.startswith("ERROR"):
raise Exception(line)
print(line)
|