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
|
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
This software is licensed as OpenSource, under the Apache License, Version 2.0.
This license is available at: http://opensource.org/licenses/Apache-2.0. */
/*
* Compact Font Embedding library.
*/
#include "dynarr.h"
#include "cfembed.h"
#include "t1read.h"
#include "cffread.h"
#include "ttread.h"
#include "cffwrite.h"
#include "sfntread.h"
#include "svgwrite.h"
#include "supportexcept.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
#ifndef ABS
#define ABS(v) (((v) < 0) ? -(v) : (v))
#endif
typedef unsigned short GID; /* Glyph Index */
enum /* Source font types */
{
SRC_TYPE1, /* PFA, PFB, POST, GX Typ1, naked CID, GX CID*/
SRC_CFF, /* OpenType CFF, naked CFF */
SRC_TRUETYPE /* TrueType sfnt */
};
/* Predefined tags */
#define CFF__tag CTL_TAG('C', 'F', 'F', ' ') /* OTF CFF table */
#define CID__tag CTL_TAG('C', 'I', 'D', ' ') /* sfnt-wrapped CID table */
#define DFLT_tag CTL_TAG('D', 'F', 'L', 'T') /* Default GPOS script */
#define GPOS_tag CTL_TAG('G', 'P', 'O', 'S') /* OTF GPOS table */
#define POST_tag CTL_TAG('P', 'O', 'S', 'T') /* Macintosh POST resource */
#define sfnt_tag CTL_TAG('s', 'f', 'n', 't') /* Macintosh sfnt resource */
#define TYP1_tag CTL_TAG('T', 'Y', 'P', '1') /* GX Type 1 sfnt table */
#define cmap_tag CTL_TAG('c', 'm', 'a', 'p') /* OTF cmap table */
#define kern_tag CTL_TAG('k', 'e', 'r', 'n') /* OTF kern feature */
/* File signatures */
#define sig_PostScript CTL_TAG('%', '!', 0x00, 0x00)
#define sig_PFB CTL_TAG(0x80, 0x01, 0x00, 0x00)
#define sig_CFF CTL_TAG(0x01, 0x00, 0x00, 0x00)
#define sig_MacResource CTL_TAG(0x00, 0x00, 0x01, 0x00)
/* ------------------------------- cmap Table ------------------------------ */
typedef struct cmapSeg4_ /* Format 4 segment */
{
unsigned short endCode;
unsigned short startCode;
short idDelta;
unsigned short idRangeOffset;
} cmapSeg4;
#define CMAP_SEG4_SIZE (4 * 2)
typedef struct cmapFmt4_ /* Format 4 cmap subtable */
{
unsigned short format;
unsigned short length;
unsigned short languageId; /* Documented as revision */
unsigned short segCountX2;
unsigned short searchRange;
unsigned short entrySelector;
unsigned short rangeShift;
/* unsigned short *endCode; */ /* written here, held in segment */
unsigned short password;
/* unsigned short *startCode; */ /*written here, held in segment */
/* short *idDelta; */ /* written here, held in segment */
/* unsigned short *idRangeOffset; */ /* written here, held in segment */
dnaDCL(cmapSeg4, segment);
dnaDCL(GID, glyphId);
} cmapFmt4;
#define CMAP_FMT4_SIZE(segs, glyphs) (8 * 2 + (segs)*CMAP_SEG4_SIZE + (glyphs)*2)
typedef struct cmapGroup12_ {
unsigned long startcode;
unsigned long endcode;
unsigned long startGID;
} cmapGroup12;
#define CMAP_GROUP12_SIZE (3 * 4)
typedef struct cmapFmt12_ {
unsigned short format; /* 12 */
unsigned short reserved; /* 0 */
unsigned long length; /* len of subtable, including the header */
unsigned long languageId;
/* unsigned long nGroups; */
dnaDCL(cmapGroup12, group);
} cmapFmt12;
#define CMAP_FMT12_SIZE(groups) (2 * 2 + 3 * 4 + (groups)*CMAP_GROUP12_SIZE)
typedef struct cmapEncoding_ {
unsigned short platformId;
unsigned short encodingID;
unsigned long offset;
cmapFmt4 subtable4;
cmapFmt12 subtable12;
} cmapEncoding;
#define CMAP_ENC_SIZE (2 * 2 + 4)
typedef struct
{
unsigned short version;
unsigned short nEncodings; /* =1 if all unicode values are in BMP, else =2 */
unsigned long _flags;
#define FILLEDFMT4 0x1
#define FILLEDFMT12 0x2
cmapEncoding encoding[2];
} cmapTbl;
#define CMAP_HDR_SIZE (2 * 2)
/* Code to glyph mapping */
typedef struct
{
unsigned long code; /* was: short */
GID glyphId;
unsigned short span; /* # of mappings in this range */
unsigned short ordered;
unsigned short flags;
#define CMAP_CODE_BREAK (1 << 0) /* Broke range because of code discontinuity */
#define CMAP_GLYPH_BREAK (1 << 1) /* broke because of glyphid discontinuity */
} Mapping;
/* ----------------------------- Coverage Table ---------------------------- */
typedef struct /* Coverage format 1 */
{
unsigned short CoverageFormat; /* =1 */
unsigned short GlyphCount;
GID *GlyphArray; /* [GlyphCount] */
} CoverageFormat1;
#define COVERAGE1_SIZE(nGlyphs) (2 * 2 + (nGlyphs)*2)
typedef struct
{
GID Start;
GID End;
unsigned short StartCoverageIndex;
} RangeRecord;
#define RANGE_RECORD_SIZE (3 * 2)
typedef struct /* Coverage format 2 */
{
unsigned short CoverageFormat; /* =2 */
unsigned short RangeCount;
dnaDCL(RangeRecord, RangeRecord); /* [RangeCount] */
} CoverageFormat2;
#define COVERAGE2_SIZE(nRanges) (2 * 2 + (nRanges)*RANGE_RECORD_SIZE)
/* ------------------------------- GPOS Table ------------------------------ */
/* Pair Adjustment Subtable Definition */
typedef unsigned short Offset;
typedef short ValueRecord;
typedef struct
{
GID SecondGlyph;
ValueRecord Value1;
/* ValueRecord Value2; */ /* not used in this implementation */
} PairValueRecord;
#define PAIR_VALUE_REC_SIZE (2 * 2)
typedef struct
{
Offset offset; /* Offset to this PairSet */
unsigned short PairValueCount;
} PairSet;
#define PAIR_SET_SIZE(nRecs, nValues) (2 + (nRecs)*PAIR_VALUE_REC_SIZE)
typedef struct
{
unsigned short PosFormat; /* =1 */
Offset Coverage;
unsigned short ValueFormat1;
unsigned short ValueFormat2;
unsigned short PairSetCount;
dnaDCL(PairSet, PairSet); /* [PairSetCount] */
} PairPosFormat1;
#define PAIR_POS1_SIZE(nPairSets) (5 * 2 + (nPairSets)*2)
/* ---------------------------- Library context ---------------------------- */
typedef struct /* Client id to CFF gid remapping */
{
char *gname; /* subset.names[id] (Type 1 only) */
unsigned short id; /* Client nid/cid/gid */
unsigned long uv; /* Unicode value */
GID gid; /* Final CFF glyph index */
} GlyphMap;
/* Format-specific font data refill callback */
typedef size_t (*FmtSpecRefill)(cefCtx h, char **ptr);
struct cefCtx_ {
long flags; /* Status flags */
#define DO_NEW_TABLES (1 << 0) /* Call new table functions */
#define CLOSE_DST_STREAM (1 << 1) /* Close destination stream */
long svwFlags; /* svgwrite library flags */
cefEmbedSpec *spec; /* Client's embedding spec */
struct /* Streams */
{
void *src;
void *dst;
ctlStreamCallbacks cb;
} stm;
struct /* Source stream */
{
int type; /* Font type */
long origin; /* Offset to start of font data in stream */
long offset; /* Buffer offset */
size_t length; /* Buffer length */
char *buf; /* Buffer beginning */
char *end; /* Buffer end */
char *next; /* Next byte available (buf <= next < end) */
} src;
struct /* Font data segment */
{
FmtSpecRefill refill; /* Format-specific callback */
size_t left; /* Bytes remaining in segment */
} seg;
struct /* cmap table data */
{
dnaDCL(Mapping, mapping); /* Code/glyph mapping accumulator */
cmapTbl tbl; /* cmap table */
} cmap;
struct /* GPOS kern feature data */
{
dnaDCL(cefKernPair, pairs); /* Kern pair accumulator */
dnaDCL(PairValueRecord, pvrecs); /* PairValueRecord accumulator */
PairPosFormat1 subtbl; /* PairPos format subtable */
} GPOS;
struct /* Coverage table (used for GPOS) */
{
dnaDCL(GID, gids); /* Covered glyphs */
short format; /* Format selector */
CoverageFormat1 fmt1; /* Format 1 data */
CoverageFormat2 fmt2; /* Format 2 data */
} coverage;
dnaDCL(GlyphMap, subset); /* Working subset specification */
struct /* Client callbacks */
{
ctlMemoryCallbacks mem;
ctlStreamCallbacks stm;
abfGlyphCallbacks glyph;
cefMapCallback *map;
} cb;
abfTopDict *top;
struct /* Service library contexts */
{
dnaCtx dna;
t1rCtx t1r;
cfrCtx cfr;
ttrCtx ttr;
cfwCtx cfw;
sfrCtx sfr;
sfwCtx sfw;
} ctx;
struct /* Error handling */
{
_Exc_Buf env;
} err;
};
/* ----------------------------- Error Handling ---------------------------- */
/* Handle fatal error */
static void fatal(cefCtx h, int err_code) {
RAISE(&h->err.env, err_code, NULL);
}
/* ----------------------------- Source Stream ----------------------------- */
/* Fill source buffer. */
static void srcFillBuf(cefCtx h, long offset) {
h->src.length = h->cb.stm.read(&h->cb.stm, h->stm.src, &h->src.buf);
if (h->src.length == 0)
fatal(h, cefErrSrcStream);
h->src.offset = offset;
h->src.next = h->src.buf;
h->src.end = h->src.buf + h->src.length;
}
/* Open source and destination streams. */
static void stmsOpen(cefCtx h) {
h->stm.src = h->cb.stm.open(&h->cb.stm, CEF_SRC_STREAM_ID, 0);
if (h->stm.src == NULL)
fatal(h, cefErrSrcStream);
srcFillBuf(h, 0);
h->stm.dst = h->cb.stm.open(&h->cb.stm, CEF_DST_STREAM_ID, 0);
if (h->stm.dst == NULL)
fatal(h, cefErrDstStream);
}
/* Close source and destination streams. */
static void stmsClose(cefCtx h) {
if (h->cb.stm.close(&h->cb.stm, h->stm.src))
fatal(h, cefErrSrcStream);
if (h->cb.stm.close(&h->cb.stm, h->stm.dst))
fatal(h, cefErrDstStream);
}
/* Read next sequential source buffer; update offset and return first byte. */
static char srcNextBuf(cefCtx h) {
/* 64-bit warning fixed by cast here */
srcFillBuf(h, (long)(h->src.offset + h->src.length));
return *h->src.next++;
}
/* Seek to specified offset. */
static void srcSeek(cefCtx h, long offset) {
long delta = offset - h->src.offset;
if (delta >= 0 && (size_t)delta < h->src.length)
/* Offset within current buffer; reposition next byte */
h->src.next = h->src.buf + delta;
else {
/* Offset outside current buffer; seek to offset and fill buffer */
if (h->cb.stm.seek(&h->cb.stm, h->stm.src, offset))
fatal(h, cefErrSrcStream);
srcFillBuf(h, offset);
}
}
/* Read 1-byte unsigned number from source stream. */
#define srcRead1(h) \
((unsigned char)((h->src.next == h->src.end) ? srcNextBuf(h) : *h->src.next++))
/* Read 2-byte number from source stream. */
static unsigned short srcRead2(cefCtx h) {
unsigned short value = (unsigned short)srcRead1(h) << 8;
return value | (unsigned short)srcRead1(h);
}
/* Read 4-byte number from source stream. */
static unsigned long srcRead4(cefCtx h) {
unsigned long value = (unsigned long)srcRead1(h) << 24;
value |= (unsigned long)srcRead1(h) << 16;
value |= (unsigned long)srcRead1(h) << 8;
return value | (unsigned long)srcRead1(h);
}
/* --------------------------- Destination Stream -------------------------- */
/* Write 2-byte number. */
static void dstWrite2(cefCtx h, unsigned short value) {
unsigned char buf[2];
buf[0] = (unsigned char)(value >> 8);
buf[1] = (unsigned char)value;
if (h->cb.stm.write(&h->cb.stm, h->stm.dst, sizeof(buf), (char *)buf) !=
sizeof(buf))
fatal(h, cefErrDstStream);
}
/* Write 4-byte number. */
static void dstWrite4(cefCtx h, unsigned long value) {
unsigned char buf[4];
buf[0] = (unsigned char)(value >> 24);
buf[1] = (unsigned char)(value >> 16);
buf[2] = (unsigned char)(value >> 8);
buf[3] = (unsigned char)value;
if (h->cb.stm.write(&h->cb.stm, h->stm.dst, sizeof(buf), (char *)buf) !=
sizeof(buf))
fatal(h, cefErrDstStream);
}
/* ---------------------------- Stream Callbacks --------------------------- */
/* Open stream. */
static void *stm_open(ctlStreamCallbacks *cb, int id, size_t size) {
cefCtx h = cb->indirect_ctx;
switch (id) {
case T1R_SRC_STREAM_ID:
case CFR_SRC_STREAM_ID:
case TTR_SRC_STREAM_ID:
/* Hand back already opened source stream */
return h->stm.src;
case CFW_DST_STREAM_ID:
case SFW_DST_STREAM_ID:
/* Hand back already opened destination stream */
return h->stm.dst;
case T1R_TMP_STREAM_ID:
/* Convert id and pass to client */
return h->cb.stm.open(&h->cb.stm, CEF_TMP0_STREAM_ID, size);
case CFW_TMP_STREAM_ID:
/* Convert id and pass to client */
return h->cb.stm.open(&h->cb.stm, CEF_TMP1_STREAM_ID, size);
default:
/* Ignore debug calls */
break;
}
return NULL;
}
/* Read from stream. */
static size_t stm_read(ctlStreamCallbacks *cb, void *stream, char **ptr) {
cefCtx h = cb->indirect_ctx;
if (stream == h->stm.src && h->seg.refill != NULL)
/* Segmented */
return h->seg.refill(h, ptr);
else
return h->cb.stm.read(&h->cb.stm, stream, ptr);
}
/* Close stream. */
static int stm_close(ctlStreamCallbacks *cb, void *stream) {
cefCtx h = cb->indirect_ctx;
if (stream == h->stm.src || stream == h->stm.dst)
return 0; /* These streams are closed later */
return h->cb.stm.close(&h->cb.stm, stream);
}
/* Initialize stream callbacks. */
static void stmInit(cefCtx h) {
h->stm.cb = h->cb.stm; /* Copy client stream callbacks */
/* Patch client callbacks */
h->stm.cb.indirect_ctx = h; /* Provide cfembed context */
h->stm.cb.open = stm_open;
h->stm.cb.close = stm_close;
}
/* ---------------- Platform-Specific Font Wrapper Callbacks --------------- */
/* Return file offset of first POST/sfnt resource data. */
static long POST_sfnt_Seek(cefCtx h, unsigned long *rsrctypefound) {
/* Macintosh resource structures */
struct
{
unsigned long dataOffset;
unsigned long mapOffset;
unsigned long dataLength;
unsigned long mapLength;
} header;
enum { HEADER_SIZE = 256 }; /* Resource header size */
struct
{
char reserved1[16];
unsigned long reserved2;
unsigned short reserved3;
unsigned short attrs;
unsigned short typeListOffset;
unsigned short nameListOffset;
} map;
enum { MAP_SKIP_SIZE = 16 + 4 + 2 * 2 }; /* Skip to typeListOffset */
struct
{
unsigned long type;
unsigned short cnt;
unsigned short refListOffset;
} type;
long typeListCnt;
#if 0
/* Included for reference only */
struct
{
unsigned short id;
unsigned short nameOffset;
char attrs;
char dataOffset[3];
unsigned long reserved;
} refList;
#endif
enum { REFLIST_SKIP_SIZE = 2 * 2 + 1 }; /* Skip to dataOffset */
long dataOffset;
long i;
/* Read header (4-byte header.dataOffset already read) */
header.mapOffset = srcRead4(h);
/* Read map */
srcSeek(h, header.mapOffset + MAP_SKIP_SIZE);
map.typeListOffset = srcRead2(h);
map.nameListOffset = srcRead2(h);
/* Find POST resource or sfnt resource in type list */
typeListCnt = srcRead2(h);
for (i = 0; i <= typeListCnt; i++) {
type.type = srcRead4(h);
type.cnt = srcRead2(h);
type.refListOffset = srcRead2(h);
switch (type.type) {
case sfnt_tag:
case POST_tag:
*rsrctypefound = type.type;
/* Read first resource data offset from reference list */
srcSeek(h, (header.mapOffset +
map.typeListOffset +
type.refListOffset +
REFLIST_SKIP_SIZE));
dataOffset = (unsigned long)srcRead1(h) << 16;
dataOffset |= srcRead1(h) << 8;
dataOffset |= srcRead1(h);
/* Return offset */
return dataOffset + HEADER_SIZE;
break;
}
}
fatal(h, cefErrNoPOST);
return 0; /* Suppress compiler warning */
}
/* Prepare next segment of font data. */
static size_t nextseg(cefCtx h, char **ptr) {
size_t srcleft = h->src.end - h->src.next;
if (srcleft == 0) {
/* Refill empty source buffer */
/* 64-bit warning fixed by cast here */
srcFillBuf(h, (long)(h->src.offset + h->src.length));
srcleft = h->src.length;
}
*ptr = h->src.next;
if (srcleft <= h->seg.left) {
/* Return full buffer */
h->seg.left -= srcleft;
h->src.next += srcleft;
} else {
/* Return partial buffer */
srcleft = h->seg.left;
h->src.next += h->seg.left;
h->seg.left = 0;
}
return srcleft;
}
/* Refill input buffer from PFB stream. */
static size_t PFBRefill(cefCtx h, char **ptr) {
while (h->seg.left == 0) {
/* New segment; read segment header */
int escape = srcRead1(h);
int type = srcRead1(h);
/* Check segment header */
if (escape != 128 || (type != 1 && type != 2 && type != 3))
fatal(h, cefErrBadPFB);
if (type == 3) {
/* EOF */
*ptr = NULL;
return 0;
} else {
/* Read segment length (little endian) */
h->seg.left = srcRead1(h);
h->seg.left |= srcRead1(h) << 8;
h->seg.left |= (long)srcRead1(h) << 16;
h->seg.left |= (long)srcRead1(h) << 24;
}
}
return nextseg(h, ptr);
}
/* Refill input buffer from POST resource stream. */
static size_t POSTRefill(cefCtx h, char **ptr) {
while (h->seg.left == 0) {
/* New POST resource */
int type;
/* Read length and type */
h->seg.left = srcRead4(h) - 2;
type = srcRead1(h);
(void)srcRead1(h); /* Discard padding byte (not documented) */
/* Process resource data */
switch (type) {
case 0:
/* Comment; skip data */
/* 64-bit warning fixed by cast here */
srcSeek(h, (long)(h->src.offset + h->seg.left));
h->seg.left = 0;
break;
case 1: /* ASCII */
case 2: /* Binary */
break;
case 3: /* End-of-file */
case 5: /* End-of-data */
*ptr = NULL;
return 0;
case 4: /* Data in data fork; unsupported */
default: /* Unknown POST type */
fatal(h, cefErrBadPOST);
}
}
return nextseg(h, ptr);
}
/* Read 4-byte signature and try to match against sfnt. */
static int readsfnt(cefCtx h, long origin) {
enum {
CID__HDR_SIZE = 4 * 4 + 3 * 2, /* 'CID ' table header size */
TYP1_HDR_SIZE = 5 * 4 + 2 * 2 /* 'TYP1' table header size */
};
ctlTag Asfnt_tag;
sfrTable *table;
int type = 0; /* Suppress optimizer warning */
if (h->ctx.sfr == NULL) {
/* Initialize library */
h->ctx.sfr =
sfrNew(&h->cb.mem, &h->cb.stm, SFR_CHECK_ARGS);
if (h->ctx.sfr == NULL)
fatal(h, cefErrSfntreadInit);
}
switch (sfrBegFont(h->ctx.sfr, h->stm.src, origin /* was: 0 */, &Asfnt_tag)) {
case sfrSuccess:
switch (Asfnt_tag) {
case sfr_v1_0_tag:
case sfr_true_tag:
/* TrueType */
h->src.origin = origin; /* was: 0 */
type = SRC_TRUETYPE;
break;
case sfr_OTTO_tag:
/* OTF */
h->src.origin = origin; /* was: 0 */
type = SRC_CFF;
break;
case sfr_typ1_tag:
/* GX or sfnt-wrapped CID font */
table = sfrGetTableByTag(h->ctx.sfr, CID__tag);
if (table != NULL) {
h->src.origin = table->offset + CID__HDR_SIZE;
type = SRC_TYPE1;
break;
} else {
table = sfrGetTableByTag(h->ctx.sfr, TYP1_tag);
if (table != NULL) {
h->src.origin = table->offset + TYP1_HDR_SIZE;
type = SRC_TYPE1;
break;
}
}
fatal(h, cefErrBadSfnt);
case sfr_ttcf_tag:
fatal(h, cefErrTTCNoSupport);
}
break;
case sfrErrBadSfnt:
fatal(h, cefErrUnrecFont);
default:
fatal(h, cefErrSfntParse);
}
if (sfrEndFont(h->ctx.sfr))
fatal(h, cefErrSfntParse);
return type;
}
/* Determine input font type and setup callbacks (assumes start of file). */
static int getFontType(cefCtx h) {
ctlTag sig;
int type = 0; /* Suppress optimizer warning */
/* Initialize segment */
h->seg.refill = NULL;
/* Determine font data type */
sig = (ctlTag)srcRead1(h) << 24;
sig |= (ctlTag)srcRead1(h) << 16;
switch (sig) {
case sig_PostScript:
type = SRC_TYPE1;
h->src.origin = 0;
break;
case sig_PFB:
type = SRC_TYPE1;
h->seg.refill = PFBRefill;
h->src.origin = 0;
break;
case sig_CFF:
type = SRC_CFF;
h->src.origin = 0;
break;
default:
/* Make 4-byte signature */
sig |= srcRead1(h) << 8;
sig |= srcRead1(h);
switch (sig) {
case sig_MacResource: {
ctlTag whichkind = 0;
long off = 0;
off = POST_sfnt_Seek(h, &whichkind);
switch (whichkind) {
case POST_tag:
type = SRC_TYPE1;
h->seg.refill = POSTRefill;
h->src.origin = off;
break;
case sfnt_tag:
type = readsfnt(h, off + 4);
break;
}
} break;
default:
type = readsfnt(h, 0);
}
}
if (h->seg.refill != NULL) {
/* Prep source filter */
h->seg.left = 0;
h->src.next = h->src.end;
}
return type;
}
/* ------------------------ Miscellaneous Functions ------------------------ */
/* Calculate values of binary search table parameters */
static void setSearchParams(unsigned unitSize, long nUnits,
unsigned short *searchRange,
unsigned short *entrySelector,
unsigned short *rangeShift) {
long nextPwr;
int pwr2;
int log2;
nextPwr = 2;
for (log2 = 0; nextPwr <= nUnits; log2++)
nextPwr *= 2;
pwr2 = nextPwr / 2;
*searchRange = unitSize * pwr2;
*entrySelector = log2;
*rangeShift = (unsigned short)(unitSize * (nUnits - pwr2));
}
/* Match id. */
static int CTL_CDECL matchId(const void *key, const void *value) {
unsigned short a = *(unsigned short *)key;
unsigned short b = ((GlyphMap *)value)->id;
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
/* Map client id to final gid. */
static GID mapId2GID(cefCtx h, unsigned short id) {
if (id >= CEF_VID_BEGIN)
return id; /* Return VID as VGID */
else {
GlyphMap *map = (GlyphMap *)bsearch(&id, h->subset.array,
h->subset.cnt,
sizeof(GlyphMap), matchId);
if (map == NULL)
fatal(h, cefErrBadSpec);
return map->gid;
}
}
/* ------------------------------- cmap Table ------------------------------ */
/* Safety initialization. */
static void cmapInit(cefCtx h) {
cmapFmt4 *fmt4 = &h->cmap.tbl.encoding[0].subtable4;
cmapFmt12 *fmt12 = &h->cmap.tbl.encoding[1].subtable12;
fmt4->segment.size = 0;
fmt4->glyphId.size = 0;
fmt12->group.size = 0;
h->cmap.mapping.size = 0;
}
/* Initialize cmap-specific data. */
static int cmapNew(sfwTableCallbacks *cb) {
cefCtx h = cb->ctx;
cmapFmt4 *fmt4 = &h->cmap.tbl.encoding[0].subtable4;
cmapFmt12 *fmt12 = &h->cmap.tbl.encoding[1].subtable12;
dnaINIT(h->ctx.dna, fmt4->segment, 64, 64);
dnaINIT(h->ctx.dna, fmt4->glyphId, 128, 128);
dnaINIT(h->ctx.dna, fmt12->group, 64, 64);
dnaINIT(h->ctx.dna, h->cmap.mapping, 256, 256);
return 0;
}
/* Free cmap-specific data. */
static int cmapFree(sfwTableCallbacks *cb) {
cefCtx h = cb->ctx;
cmapFmt4 *fmt4 = &h->cmap.tbl.encoding[0].subtable4;
cmapFmt12 *fmt12 = &h->cmap.tbl.encoding[1].subtable12;
dnaFREE(fmt4->segment);
dnaFREE(fmt4->glyphId);
dnaFREE(fmt12->group);
dnaFREE(h->cmap.mapping);
return 0;
}
/* Partition mappings optimally for format 4 and 12. Assumes mapping in code order. */
static int partitionRanges4_12(long nMaps, Mapping *mapping, int isforformat4) {
int span;
int ordered = 0;
int total = 0;
int i;
int nSegments = 0;
int beyondbmp_4 = 0;
/* Create ordered partitioning */
span = 1;
for (i = 1; i < nMaps; i++) {
int codeBreak = (mapping[i - 1].code + 1 != mapping[i].code);
int glyphBreak = (mapping[i - 1].glyphId + 1 != mapping[i].glyphId);
if (((mapping[i - 1].code > 0xFFFF) || (mapping[i].code > 0xFFFF)) &&
isforformat4) { /* not handled in format4 */
beyondbmp_4 = 1;
mapping[i - span].span = span;
mapping[i].flags |= CMAP_CODE_BREAK;
span = 1;
break;
}
if (codeBreak || glyphBreak) {
mapping[i - span].span = span;
if (codeBreak)
mapping[i].flags |= CMAP_CODE_BREAK;
if (glyphBreak)
mapping[i].flags |= CMAP_GLYPH_BREAK;
span = 1;
} else
span++;
}
mapping[i - span].span = span;
/* Coalesce ranges */
nSegments = 0;
total = ordered = span = mapping[0].span;
for (i = span; (i < nMaps) && (span > 0); i += span) {
span = mapping[i].span;
if (!(mapping[i].flags & CMAP_CODE_BREAK) &&
!(mapping[i].flags & CMAP_GLYPH_BREAK) &&
(ordered + span < 4)) {
total += span;
ordered = 0;
} else {
mapping[i - total].span = total;
mapping[i - total].ordered = ordered;
total = ordered = span;
nSegments++;
}
}
mapping[i - total].span = total;
mapping[i - total].ordered = ordered;
nSegments += 1; /* Add last segment */
return nSegments;
}
/* Fill format 4 subtable. */
static void cmapFillFmt4(cefCtx h) {
int iStart;
cmapSeg4 *segment;
Mapping *mapping = h->cmap.mapping.array;
long nMaps = h->cmap.mapping.cnt;
cmapFmt4 *fmt = &h->cmap.tbl.encoding[0].subtable4;
int nSegments = partitionRanges4_12(nMaps, mapping, 0x1);
nSegments += 1; /* add sentinel */
/* Add segments */
fmt->segment.cnt = 0;
iStart = 0;
while (iStart < nMaps) {
int j;
Mapping *startmap = &mapping[iStart];
int iEnd = iStart + startmap->span - 1;
long iSegment = fmt->segment.cnt;
if (mapping[iEnd].code > 0xFFFF) {
nSegments -= (nMaps - iStart); /* cannot include beyond this segment */
break;
}
if (mapping[iStart].code > 0xFFFF)
break; /* cannot use this segment in format4 */
segment = dnaNEXT(fmt->segment);
segment->endCode = (unsigned short)(mapping[iEnd].code & 0xFFFF);
segment->startCode = (unsigned short)(startmap->code & 0xFFFF);
if (startmap->ordered) {
segment->idDelta = (unsigned short)(startmap->glyphId - startmap->code);
segment->idRangeOffset = 0;
} else {
segment->idDelta = 0;
segment->idRangeOffset = (unsigned short)((nSegments - iSegment) * 2 + fmt->glyphId.cnt * 2);
for (j = iStart; j <= iEnd; j++)
*dnaNEXT(fmt->glyphId) = mapping[j].glyphId;
}
iStart = iEnd + 1;
}
if ((0 == nSegments) || (0 == fmt->segment.cnt))
return;
/* Add sentinel segment */
segment = dnaNEXT(fmt->segment);
segment->endCode = 0xffff;
segment->startCode = 0xffff;
segment->idDelta = 1;
segment->idRangeOffset = 0;
nSegments = fmt->segment.cnt;
fmt->segCountX2 = nSegments * 2;
fmt->password = 0;
setSearchParams(2, nSegments,
&fmt->searchRange,
&fmt->entrySelector,
&fmt->rangeShift);
fmt->format = 4;
fmt->languageId = 0;
fmt->length = (unsigned short)CMAP_FMT4_SIZE(nSegments, fmt->glyphId.cnt);
h->cmap.tbl._flags |= FILLEDFMT4;
}
/* Fill format 12 subtable. */
static void cmapFillFmt12(cefCtx h) {
Mapping *mapping = h->cmap.mapping.array;
long nMaps = h->cmap.mapping.cnt;
cmapFmt12 *fmt = &h->cmap.tbl.encoding[1].subtable12;
int iStart;
partitionRanges4_12(nMaps, mapping, 0x0);
/* add groups */
fmt->group.cnt = 0;
iStart = 0;
while (iStart < nMaps /* yes: nMaps, not nGroups */) {
cmapGroup12 *group = NULL;
Mapping *startmap = &mapping[iStart];
int iEnd = iStart + startmap->span - 1;
group = dnaNEXT(fmt->group);
group->startcode = startmap->code;
group->endcode = mapping[iEnd].code;
group->startGID = startmap->glyphId;
iStart = iEnd + 1;
}
fmt->format = 12;
fmt->reserved = 0;
fmt->languageId = 0;
fmt->length = (unsigned short)CMAP_FMT12_SIZE(fmt->group.cnt);
h->cmap.tbl._flags |= FILLEDFMT12;
}
/* Compare character codes. */
static int CTL_CDECL cmpCodes(const void *first, const void *second) {
const Mapping *a = first;
const Mapping *b = second;
if (a->code < b->code)
return -1;
else if (a->code > b->code)
return 1;
else
return 0;
}
/* Fill cmap table. */
static int cmapFill(sfwTableCallbacks *cb, int *dont_write) {
cefCtx h = cb->ctx;
long i;
cmapEncoding *encoding;
unsigned long lastcode;
unsigned char sawbeyondBMP = 0;
/* Copy subset spec. to mapping array */
h->cmap.mapping.cnt = 0;
for (i = 0; i < h->spec->subset.cnt; i++) {
cefSubsetGlyph *glyph = &h->spec->subset.array[i];
Mapping *mapping = dnaNEXT(h->cmap.mapping);
mapping->code = glyph->uv;
if (glyph->uv > 0xFFFF)
sawbeyondBMP = 1;
mapping->glyphId = mapId2GID(h, glyph->id);
mapping->span = 0;
mapping->ordered = 0;
mapping->flags = 0;
}
/* Sort mappings by encoding */
qsort(h->cmap.mapping.array, h->cmap.mapping.cnt,
sizeof(Mapping), cmpCodes);
/* Check for duplicate encodings */
lastcode = h->cmap.mapping.array[0].code;
for (i = 1; i < h->cmap.mapping.cnt; i++) {
unsigned long thiscode = h->cmap.mapping.array[i].code;
if (lastcode == thiscode)
fatal(h, cefErrBadSpec);
lastcode = thiscode;
}
h->cmap.tbl._flags = 0;
/* Fill subtable */
cmapFillFmt4(h);
/* Fill header */
h->cmap.tbl.version = 0;
h->cmap.tbl.nEncodings = 0;
if (h->cmap.tbl._flags & FILLEDFMT4)
h->cmap.tbl.nEncodings++;
if (sawbeyondBMP) { /* Fill subtable with values > 0xFFFF */
cmapFillFmt12(h);
}
if (h->cmap.tbl._flags & FILLEDFMT12)
h->cmap.tbl.nEncodings++;
/* Fill encoding records */
encoding = &h->cmap.tbl.encoding[0];
encoding->platformId = 0; /* Unicode */
encoding->encodingID = 3; /* Unicode */
encoding->offset = CMAP_HDR_SIZE + CMAP_ENC_SIZE * h->cmap.tbl.nEncodings;
encoding = &h->cmap.tbl.encoding[1];
encoding->platformId = 0; /* Unicode */
encoding->encodingID = 4; /* UTF-32 */
encoding->offset = 0xFFFF; /* not yet settable */
*dont_write = 0;
return 0;
}
/* Write format 4 subtable. */
static void cmapWriteFmt4(cefCtx h) {
long i;
cmapFmt4 *fmt = &h->cmap.tbl.encoding[0].subtable4;
dstWrite2(h, fmt->format);
dstWrite2(h, fmt->length);
dstWrite2(h, fmt->languageId);
dstWrite2(h, fmt->segCountX2);
dstWrite2(h, fmt->searchRange);
dstWrite2(h, fmt->entrySelector);
dstWrite2(h, fmt->rangeShift);
for (i = 0; i < fmt->segment.cnt; i++)
dstWrite2(h, fmt->segment.array[i].endCode);
dstWrite2(h, fmt->password);
for (i = 0; i < fmt->segment.cnt; i++)
dstWrite2(h, fmt->segment.array[i].startCode);
for (i = 0; i < fmt->segment.cnt; i++)
dstWrite2(h, fmt->segment.array[i].idDelta);
for (i = 0; i < fmt->segment.cnt; i++)
dstWrite2(h, fmt->segment.array[i].idRangeOffset);
for (i = 0; i < fmt->glyphId.cnt; i++)
dstWrite2(h, fmt->glyphId.array[i]);
}
static void cmapWriteFmt12(cefCtx h) {
long i;
cmapFmt12 *fmt = &h->cmap.tbl.encoding[1].subtable12;
dstWrite2(h, fmt->format);
dstWrite2(h, fmt->reserved);
dstWrite4(h, fmt->length);
dstWrite4(h, fmt->languageId);
dstWrite4(h, fmt->group.cnt);
for (i = 0; i < fmt->group.cnt; i++) {
cmapGroup12 *grp = &(fmt->group.array[i]);
dstWrite4(h, grp->startcode);
dstWrite4(h, grp->endcode);
dstWrite4(h, grp->startGID);
}
}
/* Write cmap table. */
static int cmapWrite(sfwTableCallbacks *cb,
ctlStreamCallbacks *stm_cb, void *stm,
int *use_checksum, unsigned long *checksum) {
cefCtx h = cb->ctx;
cmapEncoding *encoding;
unsigned long fmt4len = 0;
unsigned long fmt12len = 0;
cmapFmt4 *fmt4;
cmapFmt12 *fmt12;
/* Write header */
dstWrite2(h, h->cmap.tbl.version);
dstWrite2(h, h->cmap.tbl.nEncodings);
/* Write encoding records */
fmt4 = &h->cmap.tbl.encoding[0].subtable4;
if (h->cmap.tbl._flags & FILLEDFMT4) {
fmt4len = fmt4->length;
encoding = &h->cmap.tbl.encoding[0];
dstWrite2(h, encoding->platformId);
dstWrite2(h, encoding->encodingID);
dstWrite4(h, encoding->offset);
}
if (h->cmap.tbl._flags & FILLEDFMT12) {
fmt12 = &h->cmap.tbl.encoding[1].subtable12;
fmt12len = fmt12->length;
encoding = &h->cmap.tbl.encoding[1];
dstWrite2(h, encoding->platformId);
dstWrite2(h, encoding->encodingID);
encoding->offset = CMAP_HDR_SIZE + CMAP_ENC_SIZE * h->cmap.tbl.nEncodings + fmt4len;
dstWrite4(h, encoding->offset);
}
/* Write subtables */
if (h->cmap.tbl._flags & FILLEDFMT4)
cmapWriteFmt4(h);
if (h->cmap.tbl._flags & FILLEDFMT12)
cmapWriteFmt12(h);
return 0;
}
/* Register cmap table. */
static void cmapRegisterTable(cefCtx h) {
sfwTableCallbacks cb;
cb.ctx = h;
cb.table_tag = cmap_tag;
cb.new_table = cmapNew;
cb.fill_table = cmapFill;
cb.write_table = cmapWrite;
cb.reuse_table = NULL;
cb.free_table = cmapFree;
cb.fill_seq = cef_cmap_FILL_SEQ;
cb.write_seq = cef_cmap_WRITE_SEQ;
if (sfwRegisterTable(h->ctx.sfw, &cb))
fatal(h, cefErrCantRegister);
}
/* ------------------------------- CFF Table ------------------------------- */
/* Safety initialization. */
static void CFF_Init(cefCtx h) {
h->ctx.t1r = NULL;
h->ctx.cfr = NULL;
h->ctx.ttr = NULL;
h->ctx.cfw = NULL;
}
/* Initialize CFF-specific data. */
static int CFF_New(sfwTableCallbacks *cb) {
cefCtx h = cb->ctx;
h->ctx.cfw = cfwNew(&h->cb.mem, &h->stm.cb, CFW_CHECK_ARGS);
if (h->ctx.cfw == NULL)
fatal(h, cefErrCffwriteInit);
h->cb.glyph.direct_ctx = h->ctx.cfw;
return 0;
}
/* Free CFF-specific data. */
static int CFF_Free(sfwTableCallbacks *cb) {
cefCtx h = cb->ctx;
t1rFree(h->ctx.t1r);
cfrFree(h->ctx.cfr);
ttrFree(h->ctx.ttr);
cfwFree(h->ctx.cfw);
return 0;
}
/* Compare glyph names. */
static int CTL_CDECL cmpNames(const void *first, const void *second) {
return strcmp(((GlyphMap *)first)->gname, ((GlyphMap *)second)->gname);
}
/* Call t1read library to subset and convert Type 1 font data. */
static void t1Parse(cefCtx h) {
long i;
/* Patch in filtered stream read */
h->stm.cb.read = stm_read;
if (h->ctx.t1r == NULL) {
/* Initialize library */
h->ctx.t1r = t1rNew(&h->cb.mem, &h->stm.cb, T1R_CHECK_ARGS);
if (h->ctx.t1r == NULL)
fatal(h, cefErrT1readInit);
}
/* Parse font */
if (t1rBegFont(h->ctx.t1r,
T1R_UPDATE_OPS, h->src.origin, &h->top, h->spec->UDV))
fatal(h, cefErrT1Parse);
if (h->top->sup.flags & ABF_CID_FONT) {
/* CID font; check name override */
if (h->spec->newFontName != NULL)
h->top->cid.CIDFontName.ptr = h->spec->newFontName;
/* Subset by CID */
for (i = 0; i < h->subset.cnt; i++)
if (t1rGetGlyphByCID(h->ctx.t1r,
h->subset.array[i].id, &h->cb.glyph))
fatal(h, cefErrNoGlyph);
} else {
/* Name-keyed font; check name override */
if (h->spec->newFontName != NULL)
h->top->FDArray.array[0].FontName.ptr = h->spec->newFontName;
/* Validate subsetting method */
if (h->spec->subset.names == NULL)
fatal(h, cefErrBadSpec);
/* Subset by glyph name */
for (i = 0; i < h->subset.cnt; i++)
if (t1rGetGlyphByName(h->ctx.t1r,
h->subset.array[i].gname, &h->cb.glyph))
fatal(h, cefErrNoGlyph);
}
/* Restore client stream read */
h->stm.cb.read = h->cb.stm.read;
}
/* Call cffread library to subset CFF data. */
static void cffParse(cefCtx h) {
long i;
if (h->ctx.cfr == NULL) {
/* Initialize library */
h->ctx.cfr = cfrNew(&h->cb.mem, &h->stm.cb, CFR_CHECK_ARGS);
if (h->ctx.cfr == NULL)
fatal(h, cefErrCffreadInit);
}
/* Parse font */
if (cfrBegFont(h->ctx.cfr, CFR_UPDATE_OPS, h->src.origin, 0, &h->top, NULL))
fatal(h, cefErrCFFParse);
if (h->top->sup.flags & ABF_CID_FONT) {
/* CID font; check name override */
if (h->spec->newFontName != NULL)
h->top->cid.CIDFontName.ptr = h->spec->newFontName;
/* Subset by CID */
for (i = 0; i < h->subset.cnt; i++)
if (cfrGetGlyphByCID(h->ctx.cfr,
h->subset.array[i].id, &h->cb.glyph))
fatal(h, cefErrNoGlyph);
} else {
/* Name-keyed font; check name override */
if (h->spec->newFontName != NULL)
h->top->FDArray.array[0].FontName.ptr = h->spec->newFontName;
/* Validate subsetting method */
if (h->spec->subset.names != NULL) {
/* Subset by glyph name */
for (i = 0; i < h->subset.cnt; i++)
if (cfrGetGlyphByName(h->ctx.cfr,
h->subset.array[i].gname, &h->cb.glyph))
fatal(h, cefErrNoGlyph);
} else
/* Subset by GID */
for (i = 0; i < h->subset.cnt; i++)
if (cfrGetGlyphByTag(h->ctx.cfr,
h->subset.array[i].id, &h->cb.glyph))
fatal(h, cefErrNoGlyph);
}
}
/* Call ttread library to subset TrueType data. */
static void ttParse(cefCtx h) {
long i;
if (h->ctx.ttr == NULL) {
/* Initialize library */
h->ctx.ttr = ttrNew(&h->cb.mem, &h->stm.cb, TTR_CHECK_ARGS);
if (h->ctx.ttr == NULL)
fatal(h, cefErrTtreadInit);
}
/* Parse font */
if (ttrBegFont(h->ctx.ttr, TTR_EXACT_PATH, h->src.origin, 0, &h->top, 0))
fatal(h, cefErrTTParse);
/* Check name override */
if (h->spec->newFontName != NULL)
h->top->FDArray.array[0].FontName.ptr = h->spec->newFontName;
/* Validate subsetting method */
if (h->spec->subset.names != NULL)
fatal(h, cefErrBadSpec);
/* Subset by GID */
for (i = 0; i < h->subset.cnt; i++)
if (ttrGetGlyphByTag(h->ctx.ttr, h->subset.array[i].id, &h->cb.glyph))
fatal(h, cefErrNoGlyph);
}
/* Match glyph name in subset. */
static int CTL_CDECL matchName(const void *key, const void *value) {
return strcmp((char *)key, ((GlyphMap *)value)->gname);
}
/* GID to glyph mapping callback. */
static void glyphmap(cfwMapCallback *cb,
unsigned short gid, abfGlyphInfo *info) {
cefCtx h = cb->ctx;
GlyphMap *map;
if (h->spec->subset.names == NULL) {
/* Match cid or tag to subset id */
unsigned short key =
(info->flags & ABF_CID_FONT) ? info->cid : info->tag;
map = (GlyphMap *)bsearch(&key, h->subset.array, h->subset.cnt,
sizeof(GlyphMap), matchId);
} else
/* Match glyph names */
map = (GlyphMap *)bsearch(info->gname.ptr, h->subset.array,
h->subset.cnt, sizeof(GlyphMap), matchName);
if (map == NULL)
fatal(h, cefErrCantHappen);
map->gid = gid;
if (h->cb.map != NULL)
/* Callback mapping to client */
h->cb.map->glyphmap(h->cb.map, gid, info);
}
/* Compare client ids. */
static int CTL_CDECL cmpIds(const void *first, const void *second) {
unsigned short a = ((GlyphMap *)first)->id;
unsigned short b = ((GlyphMap *)second)->id;
if (a < b)
return -1;
else if (a > b)
return 1;
return 0;
}
/* Fill CFF table. */
static int CFF_Fill(sfwTableCallbacks *cb, int *dont_write) {
cefCtx h = cb->ctx;
cfwMapCallback map_cb;
/* Initialize callback */
map_cb.ctx = h;
map_cb.glyphmap = glyphmap;
/* Prepare to add CFF data */
if (cfwBegSet(h->ctx.cfw, CFW_EMBED_OPT) ||
cfwBegFont(h->ctx.cfw, &map_cb, 0))
fatal(h, cefErrCffwriteFont);
switch (h->src.type) {
case SRC_TYPE1:
t1Parse(h);
break;
case SRC_CFF:
cffParse(h);
break;
case SRC_TRUETYPE:
ttParse(h);
break;
}
if (h->spec->flags & CEF_FORCE_LANG_1) {
/* Force LanguageGroup 1 in all Private DICTs */
long i;
for (i = 0; i < h->top->FDArray.cnt; i++)
h->top->FDArray.array[i].Private.LanguageGroup = 1;
}
if (h->spec->flags & CEF_FORCE_IDENTITY_ROS) {
h->top->cid.Registry.ptr = "Adobe";
h->top->cid.Ordering.ptr = "Identity";
h->top->cid.Supplement = 0;
}
/* Write CFF data */
if (cfwEndFont(h->ctx.cfw, h->top))
fatal(h, cefErrCffwriteFont);
/* End parse */
switch (h->src.type) {
case SRC_TYPE1:
if (t1rEndFont(h->ctx.t1r))
fatal(h, cefErrT1Parse);
break;
case SRC_CFF:
if (cfrEndFont(h->ctx.cfr))
fatal(h, cefErrCFFParse);
break;
case SRC_TRUETYPE:
if (ttrEndFont(h->ctx.ttr))
fatal(h, cefErrTTParse);
break;
}
if (h->spec->subset.names != NULL)
/* Re-sort subset by id */
qsort(h->subset.array, h->subset.cnt, sizeof(GlyphMap), cmpIds);
*dont_write = 0;
return 0;
}
/* Write CFF table. */
static int CFF_Write(sfwTableCallbacks *cb,
ctlStreamCallbacks *stm_cb, void *stm,
int *use_checksum, unsigned long *checksum)
{
cefCtx h = cb->ctx;
/* Write CFF data */
if (cfwEndSet(h->ctx.cfw))
fatal(h, cefErrCffwriteFont);
return 0;
}
/* Register CFF table. */
static void CFF_RegisterTable(cefCtx h) {
sfwTableCallbacks cb;
cb.ctx = h;
cb.table_tag = CFF__tag;
cb.new_table = CFF_New;
cb.fill_table = CFF_Fill;
cb.write_table = CFF_Write;
cb.reuse_table = NULL;
cb.free_table = CFF_Free;
cb.fill_seq = cef_CFF__FILL_SEQ;
cb.write_seq = cef_CFF__WRITE_SEQ;
if (sfwRegisterTable(h->ctx.sfw, &cb))
fatal(h, cefErrCantRegister);
}
/* ----------------------------- Coverage Table ---------------------------- */
/* Safety initialization. */
static void coverageSafe(cefCtx h) {
h->coverage.gids.size = 0;
h->coverage.fmt2.RangeRecord.size = 0;
}
/* Initialize coverage-specific data. */
static void coverageNew(cefCtx h) {
dnaINIT(h->ctx.dna, h->coverage.gids, 100, 200);
dnaINIT(h->ctx.dna, h->coverage.fmt2.RangeRecord, 50, 100);
}
/* Free coverage-specific data. */
static void coverageFree(cefCtx h) {
dnaFREE(h->coverage.gids);
dnaFREE(h->coverage.fmt2.RangeRecord);
}
/* Write format 1 coverage table. */
static void writeCoverage1(cefCtx h) {
int i;
CoverageFormat1 *fmt = &h->coverage.fmt1;
dstWrite2(h, fmt->CoverageFormat);
dstWrite2(h, fmt->GlyphCount);
for (i = 0; i < fmt->GlyphCount; i++)
dstWrite2(h, fmt->GlyphArray[i]);
}
/* Write format 2 coverage table. */
static void writeCoverage2(cefCtx h) {
int i;
CoverageFormat2 *fmt = &h->coverage.fmt2;
dstWrite2(h, fmt->CoverageFormat);
dstWrite2(h, fmt->RangeCount);
for (i = 0; i < fmt->RangeCount; i++) {
RangeRecord *rec = &fmt->RangeRecord.array[i];
dstWrite2(h, rec->Start);
dstWrite2(h, rec->End);
dstWrite2(h, rec->StartCoverageIndex);
}
}
/* Write coverage table. */
static void writeCoverage(cefCtx h) {
switch (h->coverage.format) {
case 1:
writeCoverage1(h);
break;
case 2:
writeCoverage2(h);
break;
}
}
/* Fill coverage format 1 table. */
static void fillCoverage1(cefCtx h, long nGlyphs, GID *glyphs) {
CoverageFormat1 *fmt = &h->coverage.fmt1;
fmt->CoverageFormat = 1;
fmt->GlyphCount = (unsigned short)nGlyphs;
fmt->GlyphArray = glyphs;
}
/* Fill coverage format 2 table. */
static void fillCoverage2(cefCtx h, int nRanges, long nGlyphs, GID *glyphs) {
int i;
int iRange;
int iStart;
CoverageFormat2 *fmt = &h->coverage.fmt2;
fmt->CoverageFormat = 2;
fmt->RangeCount = nRanges;
dnaSET_CNT(fmt->RangeRecord, nRanges);
iRange = 0;
iStart = 0;
for (i = 1; i <= nGlyphs; i++)
if (i == nGlyphs || glyphs[i - 1] != glyphs[i] - 1) {
RangeRecord *rec = &fmt->RangeRecord.array[iRange++];
rec->Start = glyphs[iStart];
rec->End = glyphs[i - 1];
rec->StartCoverageIndex = iStart;
iStart = i;
}
}
/* Fill coverage table. */
static unsigned short fillCoverage(cefCtx h) {
int i;
GID first;
int nRanges;
unsigned size1;
unsigned size2;
long nGlyphs;
GID *glyphs;
/* Make list of unique first glyphs */
h->coverage.gids.cnt = 0;
first = h->GPOS.pairs.array[0].first;
for (i = 1; i < h->GPOS.pairs.cnt; i++)
if (h->GPOS.pairs.array[i].first != first) {
*dnaNEXT(h->coverage.gids) = first;
first = h->GPOS.pairs.array[i].first;
}
*dnaNEXT(h->coverage.gids) = first;
/* Count ranges (assumes glyphs sorted by increasing GID) */
nGlyphs = h->coverage.gids.cnt;
glyphs = h->coverage.gids.array;
nRanges = 1;
for (i = 1; i < nGlyphs; i++)
if (glyphs[i - 1] != glyphs[i] - 1)
nRanges++;
/* Choose format with smallest representation */
size1 = COVERAGE1_SIZE(nGlyphs);
size2 = COVERAGE2_SIZE(nRanges);
if (size1 < size2) {
h->coverage.format = 1;
fillCoverage1(h, nGlyphs, glyphs);
} else {
h->coverage.format = 2;
fillCoverage2(h, nRanges, nGlyphs, glyphs);
}
return (unsigned short)h->coverage.gids.cnt;
}
/* ------------------------------- GPOS Table ------------------------------ */
/* Safety initialization. */
static void GPOSInit(cefCtx h) {
h->GPOS.pairs.size = 0;
h->GPOS.subtbl.PairSet.size = 0;
h->GPOS.pvrecs.size = 0;
coverageSafe(h);
}
/* Initialize GPOS-specific data. */
static int GPOSNew(sfwTableCallbacks *cb) {
cefCtx h = cb->ctx;
dnaINIT(h->ctx.dna, h->GPOS.pairs, 1000, 2000);
dnaINIT(h->ctx.dna, h->GPOS.subtbl.PairSet, 100, 200);
dnaINIT(h->ctx.dna, h->GPOS.pvrecs, 1000, 2000);
coverageNew(h);
return 0;
}
/* Free GPOS-specific data. */
static int GPOSFree(sfwTableCallbacks *cb) {
cefCtx h = cb->ctx;
dnaFREE(h->GPOS.pairs);
dnaFREE(h->GPOS.subtbl.PairSet);
dnaFREE(h->GPOS.pvrecs);
coverageFree(h);
return 0;
}
/* Compare kern pairs. */
static int CTL_CDECL cmpKernPairs(const void *first, const void *second) {
const cefKernPair *a = first;
const cefKernPair *b = second;
if (a->first < b->first) /* Compare first glyphs */
return -1;
else if (a->first > b->first)
return 1;
else if (a->second < b->second) /* Compare second glyphs */
return -1;
else if (a->second > b->second)
return 1;
else if (ABS(a->value) < ABS(b->value)) /* Smallest absolute value first */
return -1;
else if (ABS(a->value) > ABS(b->value))
return 1;
else if (a->value < b->value) /* Same absolute value; negative first */
return -1;
else if (a->value > b->value)
return 1;
else
return 0;
}
/* Fill format 1 pair positioning subtable. */
static void fillPairPos1(cefCtx h) {
int i;
int j;
int iFirst;
GID first;
Offset offset;
PairPosFormat1 *fmt = &h->GPOS.subtbl;
/* Fill format 1 header */
fmt->PosFormat = 1;
fmt->ValueFormat1 = 1 << 2; /* ValueXAdvance */
fmt->ValueFormat2 = 0;
fmt->PairSetCount = fillCoverage(h);
/* Allocate/size pair set array */
dnaSET_CNT(fmt->PairSet, fmt->PairSetCount);
/* Fill pair sets */
j = 0;
iFirst = 0;
offset = PAIR_POS1_SIZE(fmt->PairSetCount);
first = h->GPOS.pairs.array[0].first;
for (i = 1; i <= h->GPOS.pairs.cnt; i++)
if (i == h->GPOS.pairs.cnt ||
h->GPOS.pairs.array[i].first != h->GPOS.pairs.array[iFirst].first) {
int k;
PairValueRecord *dst;
cefKernPair *src = &h->GPOS.pairs.array[iFirst];
PairSet *ps = &fmt->PairSet.array[j++];
/* Fill PairSet */
ps->offset = offset;
ps->PairValueCount = i - iFirst;
dst = dnaEXTEND(h->GPOS.pvrecs, ps->PairValueCount);
for (k = 0; k < ps->PairValueCount; k++) {
dst->SecondGlyph = src->second;
dst->Value1 = src->value;
src++;
dst++;
}
offset += PAIR_SET_SIZE(ps->PairValueCount, 1);
iFirst = i;
}
/* Fill coverage table offset */
fmt->Coverage = offset;
}
/* Fill GPOS table. */
static int GPOSFill(sfwTableCallbacks *cb, int *dont_write) {
cefCtx h = cb->ctx;
long i;
cefKernPair *last;
if (h->spec->kern.cnt == 0) {
/* Nothing to do */
*dont_write = 1;
return 0;
}
/* Copy pairs to local accumulator */
dnaSET_CNT(h->GPOS.pairs, h->spec->kern.cnt);
for (i = 0; i < h->GPOS.pairs.cnt; i++) {
cefKernPair *src = &h->spec->kern.array[i];
cefKernPair *dst = &h->GPOS.pairs.array[i];
dst->first = mapId2GID(h, src->first);
dst->second = mapId2GID(h, src->second);
dst->value = src->value;
}
/* Sort pairs */
qsort(h->GPOS.pairs.array, h->GPOS.pairs.cnt, sizeof(cefKernPair),
cmpKernPairs);
/* Remove duplicates. A pair is considered duplicate if the first and
second glyphs are identical. If the value associated with identical
pairs is the same either one can be discarded. If the value is different
the pair with the smallest absolute value is retained. The rational is
that one of the duplicates is mostly likely correct and the other is
wrong but we have no way of knowing which. Therefore, we choose the
smallest adjustment value because it will be the least obtrusive even if
it is wrong choice. */
last = &h->GPOS.pairs.array[0];
for (i = 1; i < h->GPOS.pairs.cnt; i++) {
cefKernPair *curr = &h->GPOS.pairs.array[i];
if (curr->first != last->first || curr->second != last->second) {
if (++last != curr)
*last = *curr;
}
}
/* 64-bit warning fixed by cast here */
h->GPOS.pairs.cnt = (long)(last - h->GPOS.pairs.array + 1);
fillPairPos1(h);
*dont_write = 0;
return 0;
}
/* Write format 1 pair positioning table */
static void writePairPos1(cefCtx h) {
int i;
PairValueRecord *pvrec;
PairPosFormat1 *fmt = &h->GPOS.subtbl;
/* Write header */
dstWrite2(h, fmt->PosFormat);
dstWrite2(h, fmt->Coverage);
dstWrite2(h, fmt->ValueFormat1);
dstWrite2(h, fmt->ValueFormat2);
dstWrite2(h, fmt->PairSetCount);
/* Write pair sets */
for (i = 0; i < fmt->PairSetCount; i++)
dstWrite2(h, fmt->PairSet.array[i].offset);
pvrec = h->GPOS.pvrecs.array;
for (i = 0; i < fmt->PairSetCount; i++) {
int j;
PairSet *ps = &fmt->PairSet.array[i];
/* Write pair value records */
dstWrite2(h, ps->PairValueCount);
for (j = 0; j < ps->PairValueCount; j++) {
dstWrite2(h, pvrec->SecondGlyph);
dstWrite2(h, pvrec->Value1);
pvrec++;
}
}
}
/* Write GPOS table. */
static int GPOSWrite(sfwTableCallbacks *cb,
ctlStreamCallbacks *stm_cb, void *stm,
int *use_checksum, unsigned long *checksum) {
cefCtx h = cb->ctx;
/* An offset field specifies a byte offset of data relative to some format
component, normally the beginning of the record it belongs to. In the
following comments, the notation <component> indicates an offset field
is relative to <component>. Offsets, both absolute and relative, within
the GPOS table are shown in parentheses. */
/* Write GPOS header */
dstWrite4(h, 0x00010000); /* Version */
dstWrite2(h, 0x000a); /* ScriptList <Header> */
dstWrite2(h, 0x001e); /* FeatureList <Header> */
dstWrite2(h, 0x002c); /* LookupList <Header> */
/* Write ScriptList (000a) */
dstWrite2(h, 0x0001); /* ScriptCount */
/* Write ScriptRecord */
dstWrite4(h, DFLT_tag); /* ScriptTag */
dstWrite2(h, 0x0008); /* Script <ScriptList> */
/* Write Script (0008) */
dstWrite2(h, 0x0004); /* DefaultLangSys <Script> */
dstWrite2(h, 0x0000); /* LangSys */
/* Write LangSys (0004) */
dstWrite2(h, 0x0000); /* LookupOrder */
dstWrite2(h, 0xffff); /* ReqFeatureIndex */
dstWrite2(h, 0x0001); /* FeatureCount */
dstWrite2(h, 0x0000); /* FeatureIndex */
/* Write FeatureList (001e) */
dstWrite2(h, 0x0001); /* FeatureCount */
/* Write FeatureRecord */
dstWrite4(h, kern_tag); /* FeatureTag */
dstWrite2(h, 0x0008); /* Feature <FeatureList> */
/* Write FeatureTable (0008) */
dstWrite2(h, 0x0000); /* FeatureParam */
dstWrite2(h, 0x0001); /* LookupCount */
dstWrite2(h, 0x0000); /* LookupListIndex[0] */
/* Write LookupList (002c) */
dstWrite2(h, 0x0001); /* LookupCount */
dstWrite2(h, 0x0004); /* Lookup[0] <LookupList> */
/* Write Lookup (0004) */
dstWrite2(h, 0x0002); /* LookupType (GPOS KernPair) */
dstWrite2(h, 0x0000); /* LookupFlag */
dstWrite2(h, 0x0001); /* SubTableCount */
dstWrite2(h, 0x0008); /* SubTable[0] <Lookup> */
writePairPos1(h);
writeCoverage(h);
return 0;
}
/* Register GPOS table. */
static void GPOSRegisterTable(cefCtx h) {
sfwTableCallbacks cb;
cb.ctx = h;
cb.table_tag = GPOS_tag;
cb.new_table = GPOSNew;
cb.fill_table = GPOSFill;
cb.write_table = GPOSWrite;
cb.reuse_table = NULL;
cb.free_table = GPOSFree;
cb.fill_seq = cef_GPOS_FILL_SEQ;
cb.write_seq = cef_GPOS_WRITE_SEQ;
if (sfwRegisterTable(h->ctx.sfw, &cb))
fatal(h, cefErrCantRegister);
}
/* ----------------------------- Font Embedding ---------------------------- */
/* This hook determines the Unicode value for the glyph, which is required for
SVG. */
static int svg_GlyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info) {
cefCtx h = cb->indirect_ctx;
GlyphMap *map;
if (h->spec->subset.names == NULL) {
/* Match cid or tag to subset id */
unsigned short key =
(info->flags & ABF_CID_FONT) ? info->cid : info->tag;
map = (GlyphMap *)bsearch(&key, h->subset.array, h->subset.cnt,
sizeof(GlyphMap), matchId);
} else
/* Match glyph names */
map = (GlyphMap *)bsearch(info->gname.ptr, h->subset.array,
h->subset.cnt, sizeof(GlyphMap), matchName);
if (map == NULL)
fatal(h, cefErrCantHappen);
info->flags |= ABF_GLYPH_UNICODE;
info->encoding.code = map->uv;
return svwGlyphCallbacks.beg(cb, info);
}
/* Make embedding font. */
int cefMakeEmbeddingFont(cefCtx h, cefEmbedSpec *spec, cefMapCallback *map) {
long i;
long j;
long namecnt;
unsigned short notdef;
if (spec->URL != NULL)
return cefErrNotImpl;
if ((spec->flags & CEF_FORCE_LANG_1) && (spec->flags & CEF_WRITE_SVG))
return cefErrBadSpec;
if (spec->subset.cnt <= 0 || spec->kern.cnt < 0)
return cefErrBadSpec;
/* Set error handler */
DURING_EX(h->err.env)
namecnt = 0; /* Suppress optimizer warning */
/* Make copy of client's subset id list and insert .notdef */
h->subset.cnt = 0;
dnaSET_CNT(h->subset, spec->subset.cnt + 1);
if (spec->subset.names != NULL) {
/* Subset glyph names specified; count them and find .notdef */
int seen_notdef = 0;
for (namecnt = 0;; namecnt++) {
char *gname = spec->subset.names[namecnt];
if (gname == NULL)
break;
else if (!seen_notdef && strcmp(gname, ".notdef") == 0) {
h->subset.array[0].id = (unsigned short)namecnt;
h->subset.array[0].uv = 0xFFFF; /* Signifies .notdef */
seen_notdef = 1; /* Ignore redefinitions! */
}
}
if (!seen_notdef)
return cefErrBadSpec;
} else {
h->subset.array[0].id = 0;
h->subset.array[0].uv = 0xFFFF; /* Signifies .notdef */
}
/* Save the .notdef id */
notdef = h->subset.array[0].id;
for (i = 0; i < spec->subset.cnt; i++) {
h->subset.array[i + 1].id = spec->subset.array[i].id;
h->subset.array[i + 1].uv = spec->subset.array[i].uv;
}
/* Sort id list (including vids) and remove duplicates */
qsort(h->subset.array, h->subset.cnt, sizeof(GlyphMap), cmpIds);
j = 0; /* target index */
for (i = 1; i < h->subset.cnt; i++) /* source index */
{
if (h->subset.array[i].id >= CEF_VID_BEGIN)
break; /* Exclude VIDs from subset */
else if (h->subset.array[i].id == h->subset.array[j].id) /* Duplicate ids */
{
if (h->subset.array[j].id != notdef) /* Target id is not .notdef */
{
if (h->subset.array[i].uv < h->subset.array[j].uv) /* Current target value is not the minimum for this id */
h->subset.array[j].uv = h->subset.array[i].uv; /* Keep the minimum */
} else /* The .notdef id is a special case */
h->subset.array[j].uv = 0xFFFF; /* Must have the value signifying .notdef */
} else /* Source and target have different ids */
{
/* Copy source to target to collapse any sequence of duplicate ids dealt with above */
++j;
h->subset.array[j].id = h->subset.array[i].id;
h->subset.array[j].uv = h->subset.array[i].uv;
}
}
h->subset.cnt = j + 1; /* Total count is now number of target values written */
if (spec->subset.names != NULL) {
/* Validate glyph/name indexes */
if (h->subset.array[h->subset.cnt - 1].id > namecnt)
fatal(h, cefErrBadSpec);
/* Add glyph names to subset */
for (i = 0; i < h->subset.cnt; i++) {
GlyphMap *glyph_map = &h->subset.array[i];
glyph_map->gname = spec->subset.names[glyph_map->id];
}
/* Sort subset by glyph name */
qsort(h->subset.array, h->subset.cnt, sizeof(GlyphMap), cmpNames);
}
h->spec = spec;
h->cb.map = map;
stmsOpen(h);
h->src.type = getFontType(h);
if (spec->flags & CEF_WRITE_SVG) /* Write SVG font */
{
abfGlyphCallbacks cb = h->cb.glyph;
svwCtx hSvw = svwNew(&h->cb.mem, &h->cb.stm, SVW_CHECK_ARGS);
if (NULL == hSvw)
fatal(h, cefErrSvgwriteInit);
h->cb.glyph = svwGlyphCallbacks;
h->cb.glyph.beg = svg_GlyphBeg;
h->cb.glyph.indirect_ctx = h;
h->cb.glyph.direct_ctx = hSvw;
if (svwBegFont(hSvw, h->svwFlags))
fatal(h, cefErrCffwriteFont);
switch (h->src.type) {
case SRC_TYPE1:
t1Parse(h);
break;
case SRC_CFF:
cffParse(h);
break;
case SRC_TRUETYPE:
ttParse(h);
break;
}
/* Write the SVG font */
if (svwEndFont(hSvw, h->top))
fatal(h, cefErrCffwriteFont);
/* End parse */
switch (h->src.type) {
case SRC_TYPE1:
if (t1rEndFont(h->ctx.t1r))
fatal(h, cefErrT1Parse);
break;
case SRC_CFF:
if (cfrEndFont(h->ctx.cfr))
fatal(h, cefErrCFFParse);
break;
case SRC_TRUETYPE:
if (ttrEndFont(h->ctx.ttr))
fatal(h, cefErrTTParse);
break;
}
svwFree(hSvw);
h->cb.glyph = cb;
} else /* Write CEF font */
{
if (h->flags & DO_NEW_TABLES) {
if (sfwNewTables(h->ctx.sfw))
fatal(h, cefErrSfntwrite);
h->flags &= ~DO_NEW_TABLES;
}
if (sfwFillTables(h->ctx.sfw) ||
sfwWriteTables(h->ctx.sfw, NULL, sfr_OTTO_tag))
fatal(h, cefErrSfntwrite);
}
stmsClose(h);
HANDLER
return Exception.Code;
END_HANDLER
return cefSuccess;
}
/* -------------------------- Safe dynarr Callbacks ------------------------ */
/* Manage memory. */
static void *dna_manage(ctlMemoryCallbacks *cb, void *old, size_t size) {
cefCtx h = cb->ctx;
void *ptr = h->cb.mem.manage(&h->cb.mem, old, size);
if (size > 0 && ptr == NULL)
fatal(h, cefErrNoMemory);
return ptr;
}
/* Initialize error handling dynarr context. */
static void dna_init(cefCtx h) {
ctlMemoryCallbacks cb;
cb.ctx = h;
cb.manage = dna_manage;
h->ctx.dna = dnaNew(&cb, DNA_CHECK_ARGS);
}
/* --------------------------- Context Management -------------------------- */
/* Validate client and create context. */
cefCtx cefNew(ctlMemoryCallbacks *mem_cb, ctlStreamCallbacks *stm_cb,
CTL_CHECK_ARGS_DCL) {
cefCtx h;
/* Check client/library compatibility */
if (CTL_CHECK_ARGS_TEST(CEF_VERSION))
return NULL;
/* Allocate context */
h = mem_cb->manage(mem_cb, NULL, sizeof(struct cefCtx_));
if (h == NULL)
return NULL;
/* Safety initialization */
cmapInit(h);
CFF_Init(h);
GPOSInit(h);
h->stm.src = NULL;
h->stm.dst = NULL;
h->subset.size = 0;
h->ctx.dna = NULL;
/* Copy callbacks */
h->cb.mem = *mem_cb;
h->cb.stm = *stm_cb;
/* Set error handler */
DURING_EX(h->err.env)
/* Initialize stream patches */
stmInit(h);
/* Initialize service libraries */
dna_init(h);
h->ctx.sfr = sfrNew(mem_cb, &h->stm.cb, SFR_CHECK_ARGS);
if (h->ctx.sfr == NULL)
fatal(h, ttrErrSfntread);
h->ctx.sfw = sfwNew(mem_cb, &h->stm.cb, SFW_CHECK_ARGS);
if (h->ctx.sfw == NULL)
fatal(h, cefErrSfntwrite);
/* Initialize context */
h->flags = DO_NEW_TABLES;
h->svwFlags = 0;
dnaINIT(h->ctx.dna, h->subset, 256, 128);
h->cb.glyph = cfwGlyphCallbacks;
/* This keeps these callbacks from being used when
writing a regular CFF, and avoids the overhead of processing the
source CFF2 blend args */
h->cb.glyph.moveVF = NULL;
h->cb.glyph.lineVF = NULL;
h->cb.glyph.curveVF = NULL;
/* Register tables */
cmapRegisterTable(h);
CFF_RegisterTable(h);
GPOSRegisterTable(h);
HANDLER
/* Initialization failed */
cefFree(h);
h = NULL;
END_HANDLER
return h;
}
/* Pass svgwrite flags through cfembed context. */
int cefSetSvwFlags(cefCtx h, long flags) {
h->svwFlags = flags;
return cefSuccess;
}
/* Register client table. */
int cefRegisterTable(cefCtx h, sfwTableCallbacks *tbl_cb) {
if (sfwRegisterTable(h->ctx.sfw, tbl_cb))
fatal(h, cefErrCantRegister);
return cefSuccess;
}
/* Free context. */
void cefFree(cefCtx h) {
if (h == NULL)
return;
dnaFREE(h->subset);
/* Free sfnt tables */
if (h->ctx.sfw != NULL)
(void)sfwFreeTables(h->ctx.sfw);
dnaFree(h->ctx.dna);
sfrFree(h->ctx.sfr);
sfwFree(h->ctx.sfw);
/* Free context */
(void)h->cb.mem.manage(&h->cb.mem, h, 0);
}
/* Get version numbers of libraries. */
void cefGetVersion(ctlVersionCallbacks *cb) {
if (cb->called & 1 << CEF_LIB_ID)
return; /* Already enumerated */
/* Support libraries */
cfrGetVersion(cb);
cfwGetVersion(cb);
dnaGetVersion(cb);
sfrGetVersion(cb);
sfwGetVersion(cb);
t1rGetVersion(cb);
ttrGetVersion(cb);
/* This library */
cb->getversion(cb, CEF_VERSION, "cfembed");
/* Record this call */
cb->called |= 1 << CEF_LIB_ID;
}
/* Map error code to error string. */
char *cefErrStr(int err_code) {
static char *errstrs[] =
{
#undef CTL_DCL_ERR
#define CTL_DCL_ERR(name, string) string,
#include "ceferr.h"
};
return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code];
}
#ifdef CEF_DEBUG
static void dbkern(cefCtx h) {
long i;
printf("--- kerning [nGlyphs=%ld]\n", h->subset.cnt);
printf("--- pairs[index]={first,second,value}\n");
for (i = 0; i < h->GPOS.pairs.cnt; i++) {
cefKernPair *pair = &h->GPOS.pairs.array[i];
printf("[%ld]={%hu,%hu,%hd} ",
i, pair->first, pair->second, pair->value);
}
printf("\n");
}
/* This function just serves to suppress annoying "defined but not used"
compiler messages when debugging is enabled. */
static void CTL_CDECL dbuse(int arg, ...) {
dbuse(0, dbuse, dbkern);
}
#endif /* CEF_DEBUG */
|