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
/****************************************************************************
*
* cffload.c
*
* OpenType and CFF data/program tables loader (body).
*
* Copyright (C) 1996-2022 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
#include <freetype/internal/ftdebug.h>
#include <freetype/internal/ftobjs.h>
#include <freetype/internal/ftstream.h>
#include <freetype/tttags.h>
#include <freetype/t1tables.h>
#include <freetype/internal/psaux.h>
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
#include <freetype/ftmm.h>
#include <freetype/internal/services/svmm.h>
#endif
#include "cffload.h"
#include "cffparse.h"
#include "cfferrs.h"
#define FT_FIXED_ONE ( (FT_Fixed)0x10000 )
#if 1
static const FT_UShort cff_isoadobe_charset[229] =
{
0, 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
};
static const FT_UShort cff_expert_charset[166] =
{
0, 1, 229, 230, 231, 232, 233, 234,
235, 236, 237, 238, 13, 14, 15, 99,
239, 240, 241, 242, 243, 244, 245, 246,
247, 248, 27, 28, 249, 250, 251, 252,
253, 254, 255, 256, 257, 258, 259, 260,
261, 262, 263, 264, 265, 266, 109, 110,
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, 158, 155, 163, 319,
320, 321, 322, 323, 324, 325, 326, 150,
164, 169, 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
};
static const FT_UShort cff_expertsubset_charset[87] =
{
0, 1, 231, 232, 235, 236, 237, 238,
13, 14, 15, 99, 239, 240, 241, 242,
243, 244, 245, 246, 247, 248, 27, 28,
249, 250, 251, 253, 254, 255, 256, 257,
258, 259, 260, 261, 262, 263, 264, 265,
266, 109, 110, 267, 268, 269, 270, 272,
300, 301, 302, 305, 314, 315, 158, 155,
163, 320, 321, 322, 323, 324, 325, 326,
150, 164, 169, 327, 328, 329, 330, 331,
332, 333, 334, 335, 336, 337, 338, 339,
340, 341, 342, 343, 344, 345, 346
};
static const FT_UShort cff_standard_encoding[256] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
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, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 96, 97, 98, 99, 100, 101, 102,
103, 104, 105, 106, 107, 108, 109, 110,
0, 111, 112, 113, 114, 0, 115, 116,
117, 118, 119, 120, 121, 122, 0, 123,
0, 124, 125, 126, 127, 128, 129, 130,
131, 0, 132, 133, 0, 134, 135, 136,
137, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 138, 0, 139, 0, 0, 0, 0,
140, 141, 142, 143, 0, 0, 0, 0,
0, 144, 0, 0, 0, 145, 0, 0,
146, 147, 148, 149, 0, 0, 0, 0
};
static const FT_UShort cff_expert_encoding[256] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 229, 230, 0, 231, 232, 233, 234,
235, 236, 237, 238, 13, 14, 15, 99,
239, 240, 241, 242, 243, 244, 245, 246,
247, 248, 27, 28, 249, 250, 251, 252,
0, 253, 254, 255, 256, 257, 0, 0,
0, 258, 0, 0, 259, 260, 261, 262,
0, 0, 263, 264, 265, 0, 266, 109,
110, 267, 268, 269, 0, 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, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 304, 305, 306, 0, 0, 307, 308,
309, 310, 311, 0, 312, 0, 0, 312,
0, 0, 314, 315, 0, 0, 316, 317,
318, 0, 0, 0, 158, 155, 163, 319,
320, 321, 322, 323, 324, 325, 0, 0,
326, 150, 164, 169, 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
};
#endif /* 1 */
FT_LOCAL_DEF( FT_UShort )
cff_get_standard_encoding( FT_UInt charcode )
{
return (FT_UShort)( charcode < 256 ? cff_standard_encoding[charcode]
: 0 );
}
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT cffload
/* read an offset from the index's stream current position */
static FT_ULong
cff_index_read_offset( CFF_Index idx,
FT_Error *errorp )
{
FT_Error error;
FT_Stream stream = idx->stream;
FT_Byte tmp[4];
FT_ULong result = 0;
if ( !FT_STREAM_READ( tmp, idx->off_size ) )
{
FT_Int nn;
for ( nn = 0; nn < idx->off_size; nn++ )
result = ( result << 8 ) | tmp[nn];
}
*errorp = error;
return result;
}
static FT_Error
cff_index_init( CFF_Index idx,
FT_Stream stream,
FT_Bool load,
FT_Bool cff2 )
{
FT_Error error;
FT_Memory memory = stream->memory;
FT_UInt count;
FT_ZERO( idx );
idx->stream = stream;
idx->start = FT_STREAM_POS();
if ( cff2 )
{
if ( FT_READ_ULONG( count ) )
goto Exit;
idx->hdr_size = 5;
}
else
{
if ( FT_READ_USHORT( count ) )
goto Exit;
idx->hdr_size = 3;
}
if ( count > 0 )
{
FT_Byte offsize;
FT_ULong size;
/* there is at least one element; read the offset size, */
/* then access the offset table to compute the index's total size */
if ( FT_READ_BYTE( offsize ) )
goto Exit;
if ( offsize < 1 || offsize > 4 )
{
error = FT_THROW( Invalid_Table );
goto Exit;
}
idx->count = count;
idx->off_size = offsize;
size = (FT_ULong)( count + 1 ) * offsize;
idx->data_offset = idx->start + idx->hdr_size + size;
if ( FT_STREAM_SKIP( size - offsize ) )
goto Exit;
size = cff_index_read_offset( idx, &error );
if ( error )
goto Exit;
if ( size == 0 )
{
error = FT_THROW( Invalid_Table );
goto Exit;
}
idx->data_size = --size;
if ( load )
{
/* load the data */
if ( FT_FRAME_EXTRACT( size, idx->bytes ) )
goto Exit;
}
else
{
/* skip the data */
if ( FT_STREAM_SKIP( size ) )
goto Exit;
}
}
Exit:
if ( error )
FT_FREE( idx->offsets );
return error;
}
static void
cff_index_done( CFF_Index idx )
{
if ( idx->stream )
{
FT_Stream stream = idx->stream;
FT_Memory memory = stream->memory;
if ( idx->bytes )
FT_FRAME_RELEASE( idx->bytes );
FT_FREE( idx->offsets );
FT_ZERO( idx );
}
}
static FT_Error
cff_index_load_offsets( CFF_Index idx )
{
FT_Error error = FT_Err_Ok;
FT_Stream stream = idx->stream;
FT_Memory memory = stream->memory;
if ( idx->count > 0 && !idx->offsets )
{
FT_Byte offsize = idx->off_size;
FT_ULong data_size;
FT_Byte* p;
FT_Byte* p_end;
FT_ULong* poff;
data_size = (FT_ULong)( idx->count + 1 ) * offsize;
if ( FT_QNEW_ARRAY( idx->offsets, idx->count + 1 ) ||
FT_STREAM_SEEK( idx->start + idx->hdr_size ) ||
FT_FRAME_ENTER( data_size ) )
goto Exit;
poff = idx->offsets;
p = (FT_Byte*)stream->cursor;
p_end = p + data_size;
switch ( offsize )
{
case 1:
for ( ; p < p_end; p++, poff++ )
poff[0] = p[0];
break;
case 2:
for ( ; p < p_end; p += 2, poff++ )
poff[0] = FT_PEEK_USHORT( p );
break;
case 3:
for ( ; p < p_end; p += 3, poff++ )
poff[0] = FT_PEEK_UOFF3( p );
break;
default:
for ( ; p < p_end; p += 4, poff++ )
poff[0] = FT_PEEK_ULONG( p );
}
FT_FRAME_EXIT();
}
Exit:
if ( error )
FT_FREE( idx->offsets );
return error;
}
/* Allocate a table containing pointers to an index's elements. */
/* The `pool' argument makes this function convert the index */
/* entries to C-style strings (this is, null-terminated). */
static FT_Error
cff_index_get_pointers( CFF_Index idx,
FT_Byte*** table,
FT_Byte** pool,
FT_ULong* pool_size )
{
FT_Error error = FT_Err_Ok;
FT_Memory memory = idx->stream->memory;
FT_Byte** tbl = NULL;
FT_Byte* new_bytes = NULL;
FT_ULong new_size;
*table = NULL;
if ( !idx->offsets )
{
error = cff_index_load_offsets( idx );
if ( error )
goto Exit;
}
new_size = idx->data_size + idx->count;
if ( idx->count > 0 &&
!FT_QNEW_ARRAY( tbl, idx->count + 1 ) &&
( !pool || !FT_ALLOC( new_bytes, new_size ) ) )
{
FT_ULong n, cur_offset;
FT_ULong extra = 0;
FT_Byte* org_bytes = idx->bytes;
/* at this point, `idx->offsets' can't be NULL */
cur_offset = idx->offsets[0] - 1;
/* sanity check */
if ( cur_offset != 0 )
{
FT_TRACE0(( "cff_index_get_pointers:"
" invalid first offset value %ld set to zero\n",
cur_offset ));
cur_offset = 0;
}
if ( !pool )
tbl[0] = org_bytes + cur_offset;
else
tbl[0] = new_bytes + cur_offset;
for ( n = 1; n <= idx->count; n++ )
{
FT_ULong next_offset = idx->offsets[n] - 1;
/* two sanity checks for invalid offset tables */
if ( next_offset < cur_offset )
next_offset = cur_offset;
else if ( next_offset > idx->data_size )
next_offset = idx->data_size;
if ( !pool )
tbl[n] = org_bytes + next_offset;
else
{
tbl[n] = new_bytes + next_offset + extra;
if ( next_offset != cur_offset )
{
FT_MEM_COPY( tbl[n - 1],
org_bytes + cur_offset,
tbl[n] - tbl[n - 1] );
tbl[n][0] = '\0';
tbl[n] += 1;
extra++;
}
}
cur_offset = next_offset;
}
*table = tbl;
if ( pool )
*pool = new_bytes;
if ( pool_size )
*pool_size = new_size;
}
Exit:
if ( error && new_bytes )
FT_FREE( new_bytes );
if ( error && tbl )
FT_FREE( tbl );
return error;
}
FT_LOCAL_DEF( FT_Error )
cff_index_access_element( CFF_Index idx,
FT_UInt element,
FT_Byte** pbytes,
FT_ULong* pbyte_len )
{
FT_Error error = FT_Err_Ok;
if ( idx && idx->count > element )
{
/* compute start and end offsets */
FT_Stream stream = idx->stream;
FT_ULong off1, off2 = 0;
/* load offsets from file or the offset table */
if ( !idx->offsets )
{
FT_ULong pos = element * idx->off_size;
if ( FT_STREAM_SEEK( idx->start + idx->hdr_size + pos ) )
goto Exit;
off1 = cff_index_read_offset( idx, &error );
if ( error )
goto Exit;
if ( off1 != 0 )
{
do
{
element++;
off2 = cff_index_read_offset( idx, &error );
} while ( off2 == 0 && element < idx->count );
}
}
else /* use offsets table */
{
off1 = idx->offsets[element];
if ( off1 )
{
do
{
element++;
off2 = idx->offsets[element];
} while ( off2 == 0 && element < idx->count );
}
}
/* XXX: should check off2 does not exceed the end of this entry; */
/* at present, only truncate off2 at the end of this stream */
if ( off2 > stream->size + 1 ||
idx->data_offset > stream->size - off2 + 1 )
{
FT_ERROR(( "cff_index_access_element:"
" offset to next entry (%ld)"
" exceeds the end of stream (%ld)\n",
off2, stream->size - idx->data_offset + 1 ));
off2 = stream->size - idx->data_offset + 1;
}
/* access element */
if ( off1 && off2 > off1 )
{
*pbyte_len = off2 - off1;
if ( idx->bytes )
{
/* this index was completely loaded in memory, that's easy */
*pbytes = idx->bytes + off1 - 1;
}
else
{
/* this index is still on disk/file, access it through a frame */
if ( FT_STREAM_SEEK( idx->data_offset + off1 - 1 ) ||
FT_FRAME_EXTRACT( off2 - off1, *pbytes ) )
goto Exit;
}
}
else
{
/* empty index element */
*pbytes = 0;
*pbyte_len = 0;
}
}
else
error = FT_THROW( Invalid_Argument );
Exit:
return error;
}
FT_LOCAL_DEF( void )
cff_index_forget_element( CFF_Index idx,
FT_Byte** pbytes )
{
if ( idx->bytes == 0 )
{
FT_Stream stream = idx->stream;
FT_FRAME_RELEASE( *pbytes );
}
}
/* get an entry from Name INDEX */
FT_LOCAL_DEF( FT_String* )
cff_index_get_name( CFF_Font font,
FT_UInt element )
{
CFF_Index idx = &font->name_index;
FT_Memory memory;
FT_Byte* bytes;
FT_ULong byte_len;
FT_Error error;
FT_String* name = NULL;
if ( !idx->stream ) /* CFF2 does not include a name index */
goto Exit;
memory = idx->stream->memory;
error = cff_index_access_element( idx, element, &bytes, &byte_len );
if ( error )
goto Exit;
if ( !FT_QALLOC( name, byte_len + 1 ) )
{
FT_MEM_COPY( name, bytes, byte_len );
name[byte_len] = 0;
}
cff_index_forget_element( idx, &bytes );
Exit:
return name;
}
/* get an entry from String INDEX */
FT_LOCAL_DEF( FT_String* )
cff_index_get_string( CFF_Font font,
FT_UInt element )
{
return ( element < font->num_strings )
? (FT_String*)font->strings[element]
: NULL;
}
FT_LOCAL_DEF( FT_String* )
cff_index_get_sid_string( CFF_Font font,
FT_UInt sid )
{
/* value 0xFFFFU indicates a missing dictionary entry */
if ( sid == 0xFFFFU )
return NULL;
/* if it is not a standard string, return it */
if ( sid > 390 )
return cff_index_get_string( font, sid - 391 );
/* CID-keyed CFF fonts don't have glyph names */
if ( !font->psnames )
return NULL;
/* this is a standard string */
return (FT_String *)font->psnames->adobe_std_strings( sid );
}
/*************************************************************************/
/*************************************************************************/
/*** ***/
/*** FD Select table support ***/
/*** ***/
/*************************************************************************/
/*************************************************************************/
static void
CFF_Done_FD_Select( CFF_FDSelect fdselect,
FT_Stream stream )
{
if ( fdselect->data )
FT_FRAME_RELEASE( fdselect->data );
fdselect->data_size = 0;
fdselect->format = 0;
fdselect->range_count = 0;
}
static FT_Error
CFF_Load_FD_Select( CFF_FDSelect fdselect,
FT_UInt num_glyphs,
FT_Stream stream,
FT_ULong offset )
{
FT_Error error;
FT_Byte format;
FT_UInt num_ranges;
/* read format */
if ( FT_STREAM_SEEK( offset ) || FT_READ_BYTE( format ) )
goto Exit;
fdselect->format = format;
fdselect->cache_count = 0; /* clear cache */
switch ( format )
{
case 0: /* format 0, that's simple */
fdselect->data_size = num_glyphs;
goto Load_Data;
case 3: /* format 3, a tad more complex */
if ( FT_READ_USHORT( num_ranges ) )
goto Exit;
if ( !num_ranges )
{
FT_TRACE0(( "CFF_Load_FD_Select: empty FDSelect array\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
fdselect->data_size = num_ranges * 3 + 2;
Load_Data:
if ( FT_FRAME_EXTRACT( fdselect->data_size, fdselect->data ) )
goto Exit;
break;
default: /* hmm... that's wrong */
error = FT_THROW( Invalid_File_Format );
}
Exit:
return error;
}
FT_LOCAL_DEF( FT_Byte )
cff_fd_select_get( CFF_FDSelect fdselect,
FT_UInt glyph_index )
{
FT_Byte fd = 0;
/* if there is no FDSelect, return zero */
/* Note: CFF2 with just one Font Dict has no FDSelect */
if ( !fdselect->data )
goto Exit;
switch ( fdselect->format )
{
case 0:
fd = fdselect->data[glyph_index];
break;
case 3:
/* first, compare to the cache */
if ( glyph_index - fdselect->cache_first < fdselect->cache_count )
{
fd = fdselect->cache_fd;
break;
}
/* then, look up the ranges array */
{
FT_Byte* p = fdselect->data;
FT_Byte* p_limit = p + fdselect->data_size;
FT_Byte fd2;
FT_UInt first, limit;
first = FT_NEXT_USHORT( p );
do
{
if ( glyph_index < first )
break;
fd2 = *p++;
limit = FT_NEXT_USHORT( p );
if ( glyph_index < limit )
{
fd = fd2;
/* update cache */
fdselect->cache_first = first;
fdselect->cache_count = limit - first;
fdselect->cache_fd = fd2;
break;
}
first = limit;
} while ( p < p_limit );
}
break;
default:
;
}
Exit:
return fd;
}
/*************************************************************************/
/*************************************************************************/
/*** ***/
/*** CFF font support ***/
/*** ***/
/*************************************************************************/
/*************************************************************************/
static FT_Error
cff_charset_compute_cids( CFF_Charset charset,
FT_UInt num_glyphs,
FT_Memory memory )
{
FT_Error error = FT_Err_Ok;
FT_UInt i;
FT_UShort max_cid = 0;
if ( charset->max_cid > 0 )
goto Exit;
for ( i = 0; i < num_glyphs; i++ )
{
if ( charset->sids[i] > max_cid )
max_cid = charset->sids[i];
}
if ( FT_NEW_ARRAY( charset->cids, (FT_ULong)max_cid + 1 ) )
goto Exit;
/* When multiple GIDs map to the same CID, we choose the lowest */
/* GID. This is not described in any spec, but it matches the */
/* behaviour of recent Acroread versions. The loop stops when */
/* the unsigned index wraps around after reaching zero. */
for ( i = num_glyphs - 1; i < num_glyphs; i-- )
charset->cids[charset->sids[i]] = (FT_UShort)i;
charset->max_cid = max_cid;
charset->num_glyphs = num_glyphs;
Exit:
return error;
}
FT_LOCAL_DEF( FT_UInt )
cff_charset_cid_to_gindex( CFF_Charset charset,
FT_UInt cid )
{
FT_UInt result = 0;
if ( cid <= charset->max_cid )
result = charset->cids[cid];
return result;
}
static void
cff_charset_free_cids( CFF_Charset charset,
FT_Memory memory )
{
FT_FREE( charset->cids );
charset->max_cid = 0;
}
static void
cff_charset_done( CFF_Charset charset,
FT_Stream stream )
{
FT_Memory memory = stream->memory;
cff_charset_free_cids( charset, memory );
FT_FREE( charset->sids );
charset->format = 0;
charset->offset = 0;
}
static FT_Error
cff_charset_load( CFF_Charset charset,
FT_UInt num_glyphs,
FT_Stream stream,
FT_ULong base_offset,
FT_ULong offset,
FT_Bool invert )
{
FT_Memory memory = stream->memory;
FT_Error error = FT_Err_Ok;
FT_UShort glyph_sid;
/* If the offset is greater than 2, we have to parse the charset */
/* table. */
if ( offset > 2 )
{
FT_UInt j;
charset->offset = base_offset + offset;
/* Get the format of the table. */
if ( FT_STREAM_SEEK( charset->offset ) ||
FT_READ_BYTE( charset->format ) )
goto Exit;
/* Allocate memory for sids. */
if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
goto Exit;
/* assign the .notdef glyph */
charset->sids[0] = 0;
switch ( charset->format )
{
case 0:
if ( num_glyphs > 0 )
{
if ( FT_FRAME_ENTER( ( num_glyphs - 1 ) * 2 ) )
goto Exit;
for ( j = 1; j < num_glyphs; j++ )
charset->sids[j] = FT_GET_USHORT();
FT_FRAME_EXIT();
}
break;
case 1:
case 2:
{
FT_UInt nleft;
FT_UInt i;
j = 1;
while ( j < num_glyphs )
{
/* Read the first glyph sid of the range. */
if ( FT_READ_USHORT( glyph_sid ) )
goto Exit;
/* Read the number of glyphs in the range. */
if ( charset->format == 2 )
{
if ( FT_READ_USHORT( nleft ) )
goto Exit;
}
else
{
if ( FT_READ_BYTE( nleft ) )
goto Exit;
}
/* try to rescue some of the SIDs if `nleft' is too large */
if ( glyph_sid > 0xFFFFL - nleft )
{
FT_ERROR(( "cff_charset_load: invalid SID range trimmed"
" nleft=%d -> %ld\n", nleft, 0xFFFFL - glyph_sid ));
nleft = ( FT_UInt )( 0xFFFFL - glyph_sid );
}
/* Fill in the range of sids -- `nleft + 1' glyphs. */
for ( i = 0; j < num_glyphs && i <= nleft; i++, j++, glyph_sid++ )
charset->sids[j] = glyph_sid;
}
}
break;
default:
FT_ERROR(( "cff_charset_load: invalid table format\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
}
else
{
/* Parse default tables corresponding to offset == 0, 1, or 2. */
/* CFF specification intimates the following: */
/* */
/* In order to use a predefined charset, the following must be */
/* true: The charset constructed for the glyphs in the font's */
/* charstrings dictionary must match the predefined charset in */
/* the first num_glyphs. */
charset->offset = offset; /* record charset type */
switch ( (FT_UInt)offset )
{
case 0:
if ( num_glyphs > 229 )
{
FT_ERROR(( "cff_charset_load: implicit charset larger than\n" ));
FT_ERROR(( "predefined charset (Adobe ISO-Latin)\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* Allocate memory for sids. */
if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
goto Exit;
/* Copy the predefined charset into the allocated memory. */
FT_ARRAY_COPY( charset->sids, cff_isoadobe_charset, num_glyphs );
break;
case 1:
if ( num_glyphs > 166 )
{
FT_ERROR(( "cff_charset_load: implicit charset larger than\n" ));
FT_ERROR(( "predefined charset (Adobe Expert)\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* Allocate memory for sids. */
if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
goto Exit;
/* Copy the predefined charset into the allocated memory. */
FT_ARRAY_COPY( charset->sids, cff_expert_charset, num_glyphs );
break;
case 2:
if ( num_glyphs > 87 )
{
FT_ERROR(( "cff_charset_load: implicit charset larger than\n" ));
FT_ERROR(( "predefined charset (Adobe Expert Subset)\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* Allocate memory for sids. */
if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
goto Exit;
/* Copy the predefined charset into the allocated memory. */
FT_ARRAY_COPY( charset->sids, cff_expertsubset_charset, num_glyphs );
break;
default:
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
}
/* we have to invert the `sids' array for subsetted CID-keyed fonts */
if ( invert )
error = cff_charset_compute_cids( charset, num_glyphs, memory );
Exit:
/* Clean up if there was an error. */
if ( error )
{
FT_FREE( charset->sids );
FT_FREE( charset->cids );
charset->format = 0;
charset->offset = 0;
}
return error;
}
static void
cff_vstore_done( CFF_VStoreRec* vstore,
FT_Memory memory )
{
FT_UInt i;
/* free regionList and axisLists */
if ( vstore->varRegionList )
{
for ( i = 0; i < vstore->regionCount; i++ )
FT_FREE( vstore->varRegionList[i].axisList );
}
FT_FREE( vstore->varRegionList );
/* free varData and indices */
if ( vstore->varData )
{
for ( i = 0; i < vstore->dataCount; i++ )
FT_FREE( vstore->varData[i].regionIndices );
}
FT_FREE( vstore->varData );
}
/* convert 2.14 to Fixed */
#define FT_fdot14ToFixed( x ) ( (FT_Fixed)( (FT_ULong)(x) << 2 ) )
static FT_Error
cff_vstore_load( CFF_VStoreRec* vstore,
FT_Stream stream,
FT_ULong base_offset,
FT_ULong offset )
{
FT_Memory memory = stream->memory;
FT_Error error = FT_ERR( Invalid_File_Format );
FT_ULong* dataOffsetArray = NULL;
FT_UInt i, j;
/* no offset means no vstore to parse */
if ( offset )
{
FT_UInt vsOffset;
FT_UInt format;
FT_UInt dataCount;
FT_UInt regionCount;
FT_ULong regionListOffset;
/* we need to parse the table to determine its size; */
/* skip table length */
if ( FT_STREAM_SEEK( base_offset + offset ) ||
FT_STREAM_SKIP( 2 ) )
goto Exit;
/* actual variation store begins after the length */
vsOffset = FT_STREAM_POS();
/* check the header */
if ( FT_READ_USHORT( format ) )
goto Exit;
if ( format != 1 )
{
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* read top level fields */
if ( FT_READ_ULONG( regionListOffset ) ||
FT_READ_USHORT( dataCount ) )
goto Exit;
/* make temporary copy of item variation data offsets; */
/* we'll parse region list first, then come back */
if ( FT_QNEW_ARRAY( dataOffsetArray, dataCount ) )
goto Exit;
for ( i = 0; i < dataCount; i++ )
{
if ( FT_READ_ULONG( dataOffsetArray[i] ) )
goto Exit;
}
/* parse regionList and axisLists */
if ( FT_STREAM_SEEK( vsOffset + regionListOffset ) ||
FT_READ_USHORT( vstore->axisCount ) ||
FT_READ_USHORT( regionCount ) )
goto Exit;
vstore->regionCount = 0;
if ( FT_QNEW_ARRAY( vstore->varRegionList, regionCount ) )
goto Exit;
for ( i = 0; i < regionCount; i++ )
{
CFF_VarRegion* region = &vstore->varRegionList[i];
if ( FT_QNEW_ARRAY( region->axisList, vstore->axisCount ) )
goto Exit;
/* keep track of how many axisList to deallocate on error */
vstore->regionCount++;
for ( j = 0; j < vstore->axisCount; j++ )
{
CFF_AxisCoords* axis = ®ion->axisList[j];
FT_Int16 start14, peak14, end14;
if ( FT_READ_SHORT( start14 ) ||
FT_READ_SHORT( peak14 ) ||
FT_READ_SHORT( end14 ) )
goto Exit;
axis->startCoord = FT_fdot14ToFixed( start14 );
axis->peakCoord = FT_fdot14ToFixed( peak14 );
axis->endCoord = FT_fdot14ToFixed( end14 );
}
}
/* use dataOffsetArray now to parse varData items */
vstore->dataCount = 0;
if ( FT_QNEW_ARRAY( vstore->varData, dataCount ) )
goto Exit;
for ( i = 0; i < dataCount; i++ )
{
CFF_VarData* data = &vstore->varData[i];
if ( FT_STREAM_SEEK( vsOffset + dataOffsetArray[i] ) )
goto Exit;
/* ignore `itemCount' and `shortDeltaCount' */
/* because CFF2 has no delta sets */
if ( FT_STREAM_SKIP( 4 ) )
goto Exit;
/* Note: just record values; consistency is checked later */
/* by cff_blend_build_vector when it consumes `vstore' */
if ( FT_READ_USHORT( data->regionIdxCount ) )
goto Exit;
if ( FT_QNEW_ARRAY( data->regionIndices, data->regionIdxCount ) )
goto Exit;
/* keep track of how many regionIndices to deallocate on error */
vstore->dataCount++;
for ( j = 0; j < data->regionIdxCount; j++ )
{
if ( FT_READ_USHORT( data->regionIndices[j] ) )
goto Exit;
}
}
}
error = FT_Err_Ok;
Exit:
FT_FREE( dataOffsetArray );
if ( error )
cff_vstore_done( vstore, memory );
return error;
}
/* Clear blend stack (after blend values are consumed). */
/* */
/* TODO: Should do this in cff_run_parse, but subFont */
/* ref is not available there. */
/* */
/* Allocation is not changed when stack is cleared. */
FT_LOCAL_DEF( void )
cff_blend_clear( CFF_SubFont subFont )
{
subFont->blend_top = subFont->blend_stack;
subFont->blend_used = 0;
}
/* Blend numOperands on the stack, */
/* store results into the first numBlends values, */
/* then pop remaining arguments. */
/* */
/* This is comparable to `cf2_doBlend' but */
/* the cffparse stack is different and can't be written. */
/* Blended values are written to a different buffer, */
/* using reserved operator 255. */
/* */
/* Blend calculation is done in 16.16 fixed-point. */
FT_LOCAL_DEF( FT_Error )
cff_blend_doBlend( CFF_SubFont subFont,
CFF_Parser parser,
FT_UInt numBlends )
{
FT_UInt delta;
FT_UInt base;
FT_UInt i, j;
FT_UInt size;
CFF_Blend blend = &subFont->blend;
FT_Memory memory = subFont->blend.font->memory; /* for FT_REALLOC */
FT_Error error = FT_Err_Ok; /* for FT_REALLOC */
/* compute expected number of operands for this blend */
FT_UInt numOperands = (FT_UInt)( numBlends * blend->lenBV );
FT_UInt count = (FT_UInt)( parser->top - 1 - parser->stack );
if ( numOperands > count )
{
FT_TRACE4(( " cff_blend_doBlend: Stack underflow %d argument%s\n",
count,
count == 1 ? "" : "s" ));
error = FT_THROW( Stack_Underflow );
goto Exit;
}
/* check whether we have room for `numBlends' values at `blend_top' */
size = 5 * numBlends; /* add 5 bytes per entry */
if ( subFont->blend_used + size > subFont->blend_alloc )
{
FT_Byte* blend_stack_old = subFont->blend_stack;
FT_Byte* blend_top_old = subFont->blend_top;
/* increase or allocate `blend_stack' and reset `blend_top'; */
/* prepare to append `numBlends' values to the buffer */
if ( FT_QREALLOC( subFont->blend_stack,
subFont->blend_alloc,
subFont->blend_alloc + size ) )
goto Exit;
subFont->blend_top = subFont->blend_stack + subFont->blend_used;
subFont->blend_alloc += size;
/* iterate over the parser stack and adjust pointers */
/* if the reallocated buffer has a different address */
if ( blend_stack_old &&
subFont->blend_stack != blend_stack_old )
{
FT_PtrDist offset = subFont->blend_stack - blend_stack_old;
FT_Byte** p;
for ( p = parser->stack; p < parser->top; p++ )
{
if ( *p >= blend_stack_old && *p < blend_top_old )
*p += offset;
}
}
}
subFont->blend_used += size;
base = count - numOperands; /* index of first blend arg */
delta = base + numBlends; /* index of first delta arg */
for ( i = 0; i < numBlends; i++ )
{
const FT_Int32* weight = &blend->BV[1];
FT_UInt32 sum;
/* convert inputs to 16.16 fixed-point */
sum = cff_parse_num( parser, &parser->stack[i + base] ) * 0x10000;
for ( j = 1; j < blend->lenBV; j++ )
sum += cff_parse_num( parser, &parser->stack[delta++] ) * *weight++;
/* point parser stack to new value on blend_stack */
parser->stack[i + base] = subFont->blend_top;
/* Push blended result as Type 2 5-byte fixed-point number. This */
/* will not conflict with actual DICTs because 255 is a reserved */
/* opcode in both CFF and CFF2 DICTs. See `cff_parse_num' for */
/* decode of this, which rounds to an integer. */
*subFont->blend_top++ = 255;
*subFont->blend_top++ = (FT_Byte)( sum >> 24 );
*subFont->blend_top++ = (FT_Byte)( sum >> 16 );
*subFont->blend_top++ = (FT_Byte)( sum >> 8 );
*subFont->blend_top++ = (FT_Byte)sum;
}
/* leave only numBlends results on parser stack */
parser->top = &parser->stack[base + numBlends];
Exit:
return error;
}
/* Compute a blend vector from variation store index and normalized */
/* vector based on pseudo-code in OpenType Font Variations Overview. */
/* */
/* Note: lenNDV == 0 produces a default blend vector, (1,0,0,...). */
FT_LOCAL_DEF( FT_Error )
cff_blend_build_vector( CFF_Blend blend,
FT_UInt vsindex,
FT_UInt lenNDV,
FT_Fixed* NDV )
{
FT_Error error = FT_Err_Ok; /* for FT_REALLOC */
FT_Memory memory = blend->font->memory; /* for FT_REALLOC */
FT_UInt len;
CFF_VStore vs;
CFF_VarData* varData;
FT_UInt master;
/* protect against malformed fonts */
if ( !( lenNDV == 0 || NDV ) )
{
FT_TRACE4(( " cff_blend_build_vector:"
" Malformed Normalize Design Vector data\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
blend->builtBV = FALSE;
vs = &blend->font->vstore;
/* VStore and fvar must be consistent */
if ( lenNDV != 0 && lenNDV != vs->axisCount )
{
FT_TRACE4(( " cff_blend_build_vector: Axis count mismatch\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
if ( vsindex >= vs->dataCount )
{
FT_TRACE4(( " cff_blend_build_vector: vsindex out of range\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* select the item variation data structure */
varData = &vs->varData[vsindex];
/* prepare buffer for the blend vector */
len = varData->regionIdxCount + 1; /* add 1 for default component */
if ( FT_QRENEW_ARRAY( blend->BV, blend->lenBV, len ) )
goto Exit;
blend->lenBV = len;
/* outer loop steps through master designs to be blended */
for ( master = 0; master < len; master++ )
{
FT_UInt j;
FT_UInt idx;
CFF_VarRegion* varRegion;
/* default factor is always one */
if ( master == 0 )
{
blend->BV[master] = FT_FIXED_ONE;
FT_TRACE4(( " build blend vector len %d\n", len ));
FT_TRACE4(( " [ %f ", blend->BV[master] / 65536.0 ));
continue;
}
/* VStore array does not include default master, so subtract one */
idx = varData->regionIndices[master - 1];
varRegion = &vs->varRegionList[idx];
if ( idx >= vs->regionCount )
{
FT_TRACE4(( " cff_blend_build_vector:"
" region index out of range\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* Note: `lenNDV' could be zero. */
/* In that case, build default blend vector (1,0,0...). */
if ( !lenNDV )
{
blend->BV[master] = 0;
continue;
}
/* In the normal case, initialize each component to 1 */
/* before inner loop. */
blend->BV[master] = FT_FIXED_ONE; /* default */
/* inner loop steps through axes in this region */
for ( j = 0; j < lenNDV; j++ )
{
CFF_AxisCoords* axis = &varRegion->axisList[j];
FT_Fixed axisScalar;
/* compute the scalar contribution of this axis; */
/* ignore invalid ranges */
if ( axis->startCoord > axis->peakCoord ||
axis->peakCoord > axis->endCoord )
axisScalar = FT_FIXED_ONE;
else if ( axis->startCoord < 0 &&
axis->endCoord > 0 &&
axis->peakCoord != 0 )
axisScalar = FT_FIXED_ONE;
/* peak of 0 means ignore this axis */
else if ( axis->peakCoord == 0 )
axisScalar = FT_FIXED_ONE;
/* ignore this region if coords are out of range */
else if ( NDV[j] < axis->startCoord ||
NDV[j] > axis->endCoord )
axisScalar = 0;
/* calculate a proportional factor */
else
{
if ( NDV[j] == axis->peakCoord )
axisScalar = FT_FIXED_ONE;
else if ( NDV[j] < axis->peakCoord )
axisScalar = FT_DivFix( NDV[j] - axis->startCoord,
axis->peakCoord - axis->startCoord );
else
axisScalar = FT_DivFix( axis->endCoord - NDV[j],
axis->endCoord - axis->peakCoord );
}
/* take product of all the axis scalars */
blend->BV[master] = FT_MulFix( blend->BV[master], axisScalar );
}
FT_TRACE4(( ", %f ",
blend->BV[master] / 65536.0 ));
}
FT_TRACE4(( "]\n" ));
/* record the parameters used to build the blend vector */
blend->lastVsindex = vsindex;
if ( lenNDV != 0 )
{
/* user has set a normalized vector */
if ( FT_QRENEW_ARRAY( blend->lastNDV, blend->lenNDV, lenNDV ) )
goto Exit;
FT_MEM_COPY( blend->lastNDV,
NDV,
lenNDV * sizeof ( *NDV ) );
}
blend->lenNDV = lenNDV;
blend->builtBV = TRUE;
Exit:
return error;
}
/* `lenNDV' is zero for default vector; */
/* return TRUE if blend vector needs to be built. */
FT_LOCAL_DEF( FT_Bool )
cff_blend_check_vector( CFF_Blend blend,
FT_UInt vsindex,
FT_UInt lenNDV,
FT_Fixed* NDV )
{
if ( !blend->builtBV ||
blend->lastVsindex != vsindex ||
blend->lenNDV != lenNDV ||
( lenNDV &&
ft_memcmp( NDV,
blend->lastNDV,
lenNDV * sizeof ( *NDV ) ) != 0 ) )
{
/* need to build blend vector */
return TRUE;
}
return FALSE;
}
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
FT_LOCAL_DEF( FT_Error )
cff_get_var_blend( CFF_Face face,
FT_UInt *num_coords,
FT_Fixed* *coords,
FT_Fixed* *normalizedcoords,
FT_MM_Var* *mm_var )
{
FT_Service_MultiMasters mm = (FT_Service_MultiMasters)face->mm;
return mm->get_var_blend( FT_FACE( face ),
num_coords,
coords,
normalizedcoords,
mm_var );
}
FT_LOCAL_DEF( void )
cff_done_blend( CFF_Face face )
{
FT_Service_MultiMasters mm = (FT_Service_MultiMasters)face->mm;
if (mm)
mm->done_blend( FT_FACE( face ) );
}
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
static void
cff_encoding_done( CFF_Encoding encoding )
{
encoding->format = 0;
encoding->offset = 0;
encoding->count = 0;
}
static FT_Error
cff_encoding_load( CFF_Encoding encoding,
CFF_Charset charset,
FT_UInt num_glyphs,
FT_Stream stream,
FT_ULong base_offset,
FT_ULong offset )
{
FT_Error error = FT_Err_Ok;
FT_UInt count;
FT_UInt j;
FT_UShort glyph_sid;
FT_UInt glyph_code;
/* Check for charset->sids. If we do not have this, we fail. */
if ( !charset->sids )
{
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* Zero out the code to gid/sid mappings. */
for ( j = 0; j < 256; j++ )
{
encoding->sids [j] = 0;
encoding->codes[j] = 0;
}
/* Note: The encoding table in a CFF font is indexed by glyph index; */
/* the first encoded glyph index is 1. Hence, we read the character */
/* code (`glyph_code') at index j and make the assignment: */
/* */
/* encoding->codes[glyph_code] = j + 1 */
/* */
/* We also make the assignment: */
/* */
/* encoding->sids[glyph_code] = charset->sids[j + 1] */
/* */
/* This gives us both a code to GID and a code to SID mapping. */
if ( offset > 1 )
{
encoding->offset = base_offset + offset;
/* we need to parse the table to determine its size */
if ( FT_STREAM_SEEK( encoding->offset ) ||
FT_READ_BYTE( encoding->format ) ||
FT_READ_BYTE( count ) )
goto Exit;
switch ( encoding->format & 0x7F )
{
case 0:
{
FT_Byte* p;
/* By convention, GID 0 is always ".notdef" and is never */
/* coded in the font. Hence, the number of codes found */
/* in the table is `count+1'. */
/* */
encoding->count = count + 1;
if ( FT_FRAME_ENTER( count ) )
goto Exit;
p = (FT_Byte*)stream->cursor;
for ( j = 1; j <= count; j++ )
{
glyph_code = *p++;
/* Make sure j is not too big. */
if ( j < num_glyphs )
{
/* Assign code to GID mapping. */
encoding->codes[glyph_code] = (FT_UShort)j;
/* Assign code to SID mapping. */
encoding->sids[glyph_code] = charset->sids[j];
}
}
FT_FRAME_EXIT();
}
break;
case 1:
{
FT_UInt nleft;
FT_UInt i = 1;
FT_UInt k;
encoding->count = 0;
/* Parse the Format1 ranges. */
for ( j = 0; j < count; j++, i += nleft )
{
/* Read the first glyph code of the range. */
if ( FT_READ_BYTE( glyph_code ) )
goto Exit;
/* Read the number of codes in the range. */
if ( FT_READ_BYTE( nleft ) )
goto Exit;
/* Increment nleft, so we read `nleft + 1' codes/sids. */
nleft++;
/* compute max number of character codes */
if ( (FT_UInt)nleft > encoding->count )
encoding->count = nleft;
/* Fill in the range of codes/sids. */
for ( k = i; k < nleft + i; k++, glyph_code++ )
{
/* Make sure k is not too big. */
if ( k < num_glyphs && glyph_code < 256 )
{
/* Assign code to GID mapping. */
encoding->codes[glyph_code] = (FT_UShort)k;
/* Assign code to SID mapping. */
encoding->sids[glyph_code] = charset->sids[k];
}
}
}
/* simple check; one never knows what can be found in a font */
if ( encoding->count > 256 )
encoding->count = 256;
}
break;
default:
FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
/* Parse supplemental encodings, if any. */
if ( encoding->format & 0x80 )
{
FT_UInt gindex;
/* count supplements */
if ( FT_READ_BYTE( count ) )
goto Exit;
for ( j = 0; j < count; j++ )
{
/* Read supplemental glyph code. */
if ( FT_READ_BYTE( glyph_code ) )
goto Exit;
/* Read the SID associated with this glyph code. */
if ( FT_READ_USHORT( glyph_sid ) )
goto Exit;
/* Assign code to SID mapping. */
encoding->sids[glyph_code] = glyph_sid;
/* First, look up GID which has been assigned to */
/* SID glyph_sid. */
for ( gindex = 0; gindex < num_glyphs; gindex++ )
{
if ( charset->sids[gindex] == glyph_sid )
{
encoding->codes[glyph_code] = (FT_UShort)gindex;
break;
}
}
}
}
}
else
{
/* We take into account the fact a CFF font can use a predefined */
/* encoding without containing all of the glyphs encoded by this */
/* encoding (see the note at the end of section 12 in the CFF */
/* specification). */
switch ( (FT_UInt)offset )
{
case 0:
/* First, copy the code to SID mapping. */
FT_ARRAY_COPY( encoding->sids, cff_standard_encoding, 256 );
goto Populate;
case 1:
/* First, copy the code to SID mapping. */
FT_ARRAY_COPY( encoding->sids, cff_expert_encoding, 256 );
Populate:
/* Construct code to GID mapping from code to SID mapping */
/* and charset. */
encoding->offset = offset; /* used in cff_face_init */
encoding->count = 0;
error = cff_charset_compute_cids( charset, num_glyphs,
stream->memory );
if ( error )
goto Exit;
for ( j = 0; j < 256; j++ )
{
FT_UInt sid = encoding->sids[j];
FT_UInt gid = 0;
if ( sid )
gid = cff_charset_cid_to_gindex( charset, sid );
if ( gid != 0 )
{
encoding->codes[j] = (FT_UShort)gid;
encoding->count = j + 1;
}
else
{
encoding->codes[j] = 0;
encoding->sids [j] = 0;
}
}
break;
default:
FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
}
Exit:
/* Clean up if there was an error. */
return error;
}
/* Parse private dictionary; first call is always from `cff_face_init', */
/* so NDV has not been set for CFF2 variation. */
/* */
/* `cff_slot_load' must call this function each time NDV changes. */
FT_LOCAL_DEF( FT_Error )
cff_load_private_dict( CFF_Font font,
CFF_SubFont subfont,
FT_UInt lenNDV,
FT_Fixed* NDV )
{
FT_Error error = FT_Err_Ok;
CFF_ParserRec parser;
CFF_FontRecDict top = &subfont->font_dict;
CFF_Private priv = &subfont->private_dict;
FT_Stream stream = font->stream;
FT_UInt stackSize;
/* store handle needed to access memory, vstore for blend; */
/* we need this for clean-up even if there is no private DICT */
subfont->blend.font = font;
subfont->blend.usedBV = FALSE; /* clear state */
if ( !top->private_offset || !top->private_size )
goto Exit2; /* no private DICT, do nothing */
/* set defaults */
FT_ZERO( priv );
priv->blue_shift = 7;
priv->blue_fuzz = 1;
priv->lenIV = -1;
priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
/* provide inputs for blend calculations */
priv->subfont = subfont;
subfont->lenNDV = lenNDV;
subfont->NDV = NDV;
/* add 1 for the operator */
stackSize = font->cff2 ? font->top_font.font_dict.maxstack + 1
: CFF_MAX_STACK_DEPTH + 1;
if ( cff_parser_init( &parser,
font->cff2 ? CFF2_CODE_PRIVATE : CFF_CODE_PRIVATE,
priv,
font->library,
stackSize,
top->num_designs,
top->num_axes ) )
goto Exit;
if ( FT_STREAM_SEEK( font->base_offset + top->private_offset ) ||
FT_FRAME_ENTER( top->private_size ) )
goto Exit;
FT_TRACE4(( " private dictionary:\n" ));
error = cff_parser_run( &parser,
(FT_Byte*)stream->cursor,
(FT_Byte*)stream->limit );
FT_FRAME_EXIT();
if ( error )
goto Exit;
/* ensure that `num_blue_values' is even */
priv->num_blue_values &= ~1;
/* sanitize `initialRandomSeed' to be a positive value, if necessary; */
/* this is not mandated by the specification but by our implementation */
if ( priv->initial_random_seed < 0 )
priv->initial_random_seed = -priv->initial_random_seed;
else if ( priv->initial_random_seed == 0 )
priv->initial_random_seed = 987654321;
/* some sanitizing to avoid overflows later on; */
/* the upper limits are ad-hoc values */
if ( priv->blue_shift > 1000 || priv->blue_shift < 0 )
{
FT_TRACE2(( "cff_load_private_dict:"
" setting unlikely BlueShift value %ld to default (7)\n",
priv->blue_shift ));
priv->blue_shift = 7;
}
if ( priv->blue_fuzz > 1000 || priv->blue_fuzz < 0 )
{
FT_TRACE2(( "cff_load_private_dict:"
" setting unlikely BlueFuzz value %ld to default (1)\n",
priv->blue_fuzz ));
priv->blue_fuzz = 1;
}
Exit:
/* clean up */
cff_blend_clear( subfont ); /* clear blend stack */
cff_parser_done( &parser ); /* free parser stack */
Exit2:
/* no clean up (parser not initialized) */
return error;
}
/* There are 3 ways to call this function, distinguished by code. */
/* */
/* . CFF_CODE_TOPDICT for either a CFF Top DICT or a CFF Font DICT */
/* . CFF2_CODE_TOPDICT for CFF2 Top DICT */
/* . CFF2_CODE_FONTDICT for CFF2 Font DICT */
static FT_Error
cff_subfont_load( CFF_SubFont subfont,
CFF_Index idx,
FT_UInt font_index,
FT_Stream stream,
FT_ULong base_offset,
FT_UInt code,
CFF_Font font,
CFF_Face face )
{
FT_Error error;
CFF_ParserRec parser;
FT_Byte* dict = NULL;
FT_ULong dict_len;
CFF_FontRecDict top = &subfont->font_dict;
CFF_Private priv = &subfont->private_dict;
PSAux_Service psaux = (PSAux_Service)face->psaux;
FT_Bool cff2 = FT_BOOL( code == CFF2_CODE_TOPDICT ||
code == CFF2_CODE_FONTDICT );
FT_UInt stackSize = cff2 ? CFF2_DEFAULT_STACK
: CFF_MAX_STACK_DEPTH;
/* Note: We use default stack size for CFF2 Font DICT because */
/* Top and Font DICTs are not allowed to have blend operators. */
error = cff_parser_init( &parser,
code,
&subfont->font_dict,
font->library,
stackSize,
0,
0 );
if ( error )
goto Exit;
/* set defaults */
FT_ZERO( top );
top->underline_position = -( 100L << 16 );
top->underline_thickness = 50L << 16;
top->charstring_type = 2;
top->font_matrix.xx = 0x10000L;
top->font_matrix.yy = 0x10000L;
top->cid_count = 8720;
/* we use the implementation specific SID value 0xFFFF to indicate */
/* missing entries */
top->version = 0xFFFFU;
top->notice = 0xFFFFU;
top->copyright = 0xFFFFU;
top->full_name = 0xFFFFU;
top->family_name = 0xFFFFU;
top->weight = 0xFFFFU;
top->embedded_postscript = 0xFFFFU;
top->cid_registry = 0xFFFFU;
top->cid_ordering = 0xFFFFU;
top->cid_font_name = 0xFFFFU;
/* set default stack size */
top->maxstack = cff2 ? CFF2_DEFAULT_STACK : 48;
if ( idx->count ) /* count is nonzero for a real index */
error = cff_index_access_element( idx, font_index, &dict, &dict_len );
else
{
/* CFF2 has a fake top dict index; */
/* simulate `cff_index_access_element' */
/* Note: macros implicitly use `stream' and set `error' */
if ( FT_STREAM_SEEK( idx->data_offset ) ||
FT_FRAME_EXTRACT( idx->data_size, dict ) )
goto Exit;
dict_len = idx->data_size;
}
if ( !error )
{
FT_TRACE4(( " top dictionary:\n" ));
error = cff_parser_run( &parser, dict, FT_OFFSET( dict, dict_len ) );
}
/* clean up regardless of error */
if ( idx->count )
cff_index_forget_element( idx, &dict );
else
FT_FRAME_RELEASE( dict );
if ( error )
goto Exit;
/* if it is a CID font, we stop there */
if ( top->cid_registry != 0xFFFFU )
goto Exit;
/* Parse the private dictionary, if any. */
/* */
/* CFF2 does not have a private dictionary in the Top DICT */
/* but may have one in a Font DICT. We need to parse */
/* the latter here in order to load any local subrs. */
error = cff_load_private_dict( font, subfont, 0, 0 );
if ( error )
goto Exit;
if ( !cff2 )
{
/*
* Initialize the random number generator.
*
* - If we have a face-specific seed, use it.
* If non-zero, update it to a positive value.
*
* - Otherwise, use the seed from the CFF driver.
* If non-zero, update it to a positive value.
*
* - If the random value is zero, use the seed given by the subfont's
* `initialRandomSeed' value.
*
*/
if ( face->root.internal->random_seed == -1 )
{
PS_Driver driver = (PS_Driver)FT_FACE_DRIVER( face );
subfont->random = (FT_UInt32)driver->random_seed;
if ( driver->random_seed )
{
do
{
driver->random_seed =
(FT_Int32)psaux->cff_random( (FT_UInt32)driver->random_seed );
} while ( driver->random_seed < 0 );
}
}
else
{
subfont->random = (FT_UInt32)face->root.internal->random_seed;
if ( face->root.internal->random_seed )
{
do
{
face->root.internal->random_seed =
(FT_Int32)psaux->cff_random(
(FT_UInt32)face->root.internal->random_seed );
} while ( face->root.internal->random_seed < 0 );
}
}
if ( !subfont->random )
subfont->random = (FT_UInt32)priv->initial_random_seed;
}
/* read the local subrs, if any */
if ( priv->local_subrs_offset )
{
if ( FT_STREAM_SEEK( base_offset + top->private_offset +
priv->local_subrs_offset ) )
goto Exit;
error = cff_index_init( &subfont->local_subrs_index, stream, 1, cff2 );
if ( error )
goto Exit;
error = cff_index_get_pointers( &subfont->local_subrs_index,
&subfont->local_subrs, NULL, NULL );
if ( error )
goto Exit;
}
Exit:
cff_parser_done( &parser ); /* free parser stack */
return error;
}
static void
cff_subfont_done( FT_Memory memory,
CFF_SubFont subfont )
{
if ( subfont )
{
cff_index_done( &subfont->local_subrs_index );
FT_FREE( subfont->local_subrs );
FT_FREE( subfont->blend.lastNDV );
FT_FREE( subfont->blend.BV );
FT_FREE( subfont->blend_stack );
}
}
FT_LOCAL_DEF( FT_Error )
cff_font_load( FT_Library library,
FT_Stream stream,
FT_Int face_index,
CFF_Font font,
CFF_Face face,
FT_Bool pure_cff,
FT_Bool cff2 )
{
static const FT_Frame_Field cff_header_fields[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE CFF_FontRec
FT_FRAME_START( 3 ),
FT_FRAME_BYTE( version_major ),
FT_FRAME_BYTE( version_minor ),
FT_FRAME_BYTE( header_size ),
FT_FRAME_END
};
FT_Error error;
FT_Memory memory = stream->memory;
FT_ULong base_offset;
CFF_FontRecDict dict;
CFF_IndexRec string_index;
FT_UInt subfont_index;
FT_ZERO( font );
FT_ZERO( &string_index );
dict = &font->top_font.font_dict;
base_offset = FT_STREAM_POS();
font->library = library;
font->stream = stream;
font->memory = memory;
font->cff2 = cff2;
font->base_offset = base_offset;
/* read CFF font header */
if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) )
goto Exit;
if ( cff2 )
{
if ( font->version_major != 2 ||
font->header_size < 5 )
{
FT_TRACE2(( " not a CFF2 font header\n" ));
error = FT_THROW( Unknown_File_Format );
goto Exit;
}
if ( FT_READ_USHORT( font->top_dict_length ) )
goto Exit;
}
else
{
FT_Byte absolute_offset;
if ( FT_READ_BYTE( absolute_offset ) )
goto Exit;
if ( font->version_major != 1 ||
font->header_size < 4 ||
absolute_offset > 4 )
{
FT_TRACE2(( " not a CFF font header\n" ));
error = FT_THROW( Unknown_File_Format );
goto Exit;
}
}
/* skip the rest of the header */
if ( FT_STREAM_SEEK( base_offset + font->header_size ) )
{
/* For pure CFFs we have read only four bytes so far. Contrary to */
/* other formats like SFNT those bytes doesn't define a signature; */
/* it is thus possible that the font isn't a CFF at all. */
if ( pure_cff )
{
FT_TRACE2(( " not a CFF file\n" ));
error = FT_THROW( Unknown_File_Format );
}
goto Exit;
}
if ( cff2 )
{
/* For CFF2, the top dict data immediately follow the header */
/* and the length is stored in the header `offSize' field; */
/* there is no index for it. */
/* */
/* Use the `font_dict_index' to save the current position */
/* and length of data, but leave count at zero as an indicator. */
FT_ZERO( &font->font_dict_index );
font->font_dict_index.data_offset = FT_STREAM_POS();
font->font_dict_index.data_size = font->top_dict_length;
/* skip the top dict data for now, we will parse it later */
if ( FT_STREAM_SKIP( font->top_dict_length ) )
goto Exit;
/* next, read the global subrs index */
if ( FT_SET_ERROR( cff_index_init( &font->global_subrs_index,
stream, 1, cff2 ) ) )
goto Exit;
}
else
{
/* for CFF, read the name, top dict, string and global subrs index */
if ( FT_SET_ERROR( cff_index_init( &font->name_index,
stream, 0, cff2 ) ) )
{
if ( pure_cff )
{
FT_TRACE2(( " not a CFF file\n" ));
error = FT_THROW( Unknown_File_Format );
}
goto Exit;
}
/* if we have an empty font name, */
/* it must be the only font in the CFF */
if ( font->name_index.count > 1 &&
font->name_index.data_size < font->name_index.count )
{
/* for pure CFFs, we still haven't checked enough bytes */
/* to be sure that it is a CFF at all */
error = pure_cff ? FT_THROW( Unknown_File_Format )
: FT_THROW( Invalid_File_Format );
goto Exit;
}
if ( FT_SET_ERROR( cff_index_init( &font->font_dict_index,
stream, 0, cff2 ) ) ||
FT_SET_ERROR( cff_index_init( &string_index,
stream, 1, cff2 ) ) ||
FT_SET_ERROR( cff_index_init( &font->global_subrs_index,
stream, 1, cff2 ) ) ||
FT_SET_ERROR( cff_index_get_pointers( &string_index,
&font->strings,
&font->string_pool,
&font->string_pool_size ) ) )
goto Exit;
/* there must be a Top DICT index entry for each name index entry */
if ( font->name_index.count > font->font_dict_index.count )
{
FT_ERROR(( "cff_font_load:"
" not enough entries in Top DICT index\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
}
font->num_strings = string_index.count;
if ( pure_cff )
{
/* well, we don't really forget the `disabled' fonts... */
subfont_index = (FT_UInt)( face_index & 0xFFFF );
if ( face_index > 0 && subfont_index >= font->name_index.count )
{
FT_ERROR(( "cff_font_load:"
" invalid subfont index for pure CFF font (%d)\n",
subfont_index ));
error = FT_THROW( Invalid_Argument );
goto Exit;
}
font->num_faces = font->name_index.count;
}
else
{
subfont_index = 0;
if ( font->name_index.count > 1 )
{
FT_ERROR(( "cff_font_load:"
" invalid CFF font with multiple subfonts\n" ));
FT_ERROR(( " "
" in SFNT wrapper\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
}
/* in case of a font format check, simply exit now */
if ( face_index < 0 )
goto Exit;
/* now, parse the top-level font dictionary */
FT_TRACE4(( "parsing top-level\n" ));
error = cff_subfont_load( &font->top_font,
&font->font_dict_index,
subfont_index,
stream,
base_offset,
cff2 ? CFF2_CODE_TOPDICT : CFF_CODE_TOPDICT,
font,
face );
if ( error )
goto Exit;
if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) )
goto Exit;
error = cff_index_init( &font->charstrings_index, stream, 0, cff2 );
if ( error )
goto Exit;
/* now, check for a CID or CFF2 font */
if ( dict->cid_registry != 0xFFFFU ||
cff2 )
{
CFF_IndexRec fd_index;
CFF_SubFont sub = NULL;
FT_UInt idx;
/* for CFF2, read the Variation Store if available; */
/* this must follow the Top DICT parse and precede any Private DICT */
error = cff_vstore_load( &font->vstore,
stream,
base_offset,
dict->vstore_offset );
if ( error )
goto Exit;
/* this is a CID-keyed font, we must now allocate a table of */
/* sub-fonts, then load each of them separately */
if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) )
goto Exit;
error = cff_index_init( &fd_index, stream, 0, cff2 );
if ( error )
goto Exit;
/* Font Dicts are not limited to 256 for CFF2. */
/* TODO: support this for CFF2 */
if ( fd_index.count > CFF_MAX_CID_FONTS )
{
FT_TRACE0(( "cff_font_load: FD array too large in CID font\n" ));
goto Fail_CID;
}
/* allocate & read each font dict independently */
font->num_subfonts = fd_index.count;
if ( FT_NEW_ARRAY( sub, fd_index.count ) )
goto Fail_CID;
/* set up pointer table */
for ( idx = 0; idx < fd_index.count; idx++ )
font->subfonts[idx] = sub + idx;
/* now load each subfont independently */
for ( idx = 0; idx < fd_index.count; idx++ )
{
sub = font->subfonts[idx];
FT_TRACE4(( "parsing subfont %u\n", idx ));
error = cff_subfont_load( sub,
&fd_index,
idx,
stream,
base_offset,
cff2 ? CFF2_CODE_FONTDICT
: CFF_CODE_TOPDICT,
font,
face );
if ( error )
goto Fail_CID;
}
/* now load the FD Select array; */
/* CFF2 omits FDSelect if there is only one FD */
if ( !cff2 || fd_index.count > 1 )
error = CFF_Load_FD_Select( &font->fd_select,
font->charstrings_index.count,
stream,
base_offset + dict->cid_fd_select_offset );
Fail_CID:
cff_index_done( &fd_index );
if ( error )
goto Exit;
}
else
font->num_subfonts = 0;
/* read the charstrings index now */
if ( dict->charstrings_offset == 0 )
{
FT_ERROR(( "cff_font_load: no charstrings offset\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
font->num_glyphs = font->charstrings_index.count;
error = cff_index_get_pointers( &font->global_subrs_index,
&font->global_subrs, NULL, NULL );
if ( error )
goto Exit;
/* read the Charset and Encoding tables if available */
if ( !cff2 && font->num_glyphs > 0 )
{
FT_Bool invert = FT_BOOL( dict->cid_registry != 0xFFFFU && pure_cff );
error = cff_charset_load( &font->charset, font->num_glyphs, stream,
base_offset, dict->charset_offset, invert );
if ( error )
goto Exit;
/* CID-keyed CFFs don't have an encoding */
if ( dict->cid_registry == 0xFFFFU )
{
error = cff_encoding_load( &font->encoding,
&font->charset,
font->num_glyphs,
stream,
base_offset,
dict->encoding_offset );
if ( error )
goto Exit;
}
}
/* get the font name (/CIDFontName for CID-keyed fonts, */
/* /FontName otherwise) */
font->font_name = cff_index_get_name( font, subfont_index );
Exit:
cff_index_done( &string_index );
return error;
}
FT_LOCAL_DEF( void )
cff_font_done( CFF_Font font )
{
FT_Memory memory = font->memory;
FT_UInt idx;
cff_index_done( &font->global_subrs_index );
cff_index_done( &font->font_dict_index );
cff_index_done( &font->name_index );
cff_index_done( &font->charstrings_index );
/* release font dictionaries, but only if working with */
/* a CID keyed CFF font or a CFF2 font */
if ( font->num_subfonts > 0 )
{
for ( idx = 0; idx < font->num_subfonts; idx++ )
cff_subfont_done( memory, font->subfonts[idx] );
/* the subfonts array has been allocated as a single block */
FT_FREE( font->subfonts[0] );
}
cff_encoding_done( &font->encoding );
cff_charset_done( &font->charset, font->stream );
cff_vstore_done( &font->vstore, memory );
cff_subfont_done( memory, &font->top_font );
CFF_Done_FD_Select( &font->fd_select, font->stream );
FT_FREE( font->font_info );
FT_FREE( font->font_name );
FT_FREE( font->global_subrs );
FT_FREE( font->strings );
FT_FREE( font->string_pool );
if ( font->cf2_instance.finalizer )
{
font->cf2_instance.finalizer( font->cf2_instance.data );
FT_FREE( font->cf2_instance.data );
}
FT_FREE( font->font_extra );
}
/* END */