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
|
#ifdef pdfTeX /* writet1 used with pdfTeX */
#include "ptexlib.h"
#define t1_log(s) tex_printf(s)
#define t1_open() \
open_input(&t1_file, kpse_type1_format, FOPEN_RBIN_MODE)
#define enc_open() \
open_input(&enc_file, kpse_tex_ps_header_format, FOPEN_RBIN_MODE)
#define external_enc() enc_tab[fm_cur->encoding].glyph_names
#define full_file_name() nameoffile + 1
#define get_length1() t1_length1 = t1_offset() - t1_save_offset
#define get_length2() t1_length2 = t1_offset() - t1_save_offset
#define get_length3() t1_length3 = t1_offset() - t1_save_offset
#define is_used_char(c) pdfcharmarked(tex_font, c)
#define t1_putchar ff_putchar
#define t1_offset ff_offset
#define out_eexec_char t1_putchar
#define save_offset() t1_save_offset = t1_offset()
#define end_last_eexec_line() \
t1_eexec_encrypt = false
#define update_builtin_enc(font, glyph_names) update_enc(font, glyph_names)
#define t1_char(c) c
#define embed_all_glyphs(tex_font) fm_cur->all_glyphs
#define extra_charset() fm_cur->charset
integer t1_length1, t1_length2, t1_length3;
static integer t1_save_offset;
static integer t1_fontname_offset;
#else /* writet1 used with dvips */
#include "ptexmac.h"
#include "dvips.h"
#include "protos.h"
#undef fm_extend
#define fm_extend(f) 0
#undef fm_slant
#define fm_slant(f) 0
#undef is_reencoded
#define is_reencoded(f) (cur_enc_name != 0)
#undef is_subsetted
#define is_subsetted(f) true
#undef is_included
#define is_included(f) true
#undef set_cur_file_name
#define set_cur_file_name(s) cur_file_name = s
#define t1_open() \
((t1_file = search(headerpath, cur_file_name, FOPEN_RBIN_MODE)) != NULL)
#define enc_open() \
((enc_file = search(headerpath, cur_file_name, FOPEN_RBIN_MODE)) != NULL)
#define external_enc() ext_glyph_names
#define full_file_name() cur_file_name
#define get_length1()
#define get_length2()
#define get_length3()
#define is_used_char(c) (grid[c] == 1)
#define out_eexec_char t1_outhex
#define save_offset()
#define end_last_eexec_line() \
hexline_length = HEXLINE_WIDTH; \
end_hexline(); \
t1_eexec_encrypt = false
#define t1_log(s)
#define t1_scan_only()
#define t1_include()
#define t1_putchar(c) fputc(c, bitfile)
#define t1_scan_keys()
#define update_builtin_enc(font, glyph_names)
#define embed_all_glyphs(tex_font) false
#ifdef SHIFTLOWCHARS
#define t1_char(c) T1Char(c)
#define dvips_pdfmovechars 1
#else /* SHIFTLOWCHARS */
#define t1_char(c) c
#define dvips_pdfmovechars 0
#endif /* SHIFTLOWCHARS */
#define extra_charset() dvips_extra_charset
#define make_subset_tag(a, b)
static char *dvips_extra_charset ;
extern FILE *bitfile ;
extern FILE *search();
static char *cur_file_name;
static char *cur_enc_name;
static unsigned char *grid;
static char *ext_glyph_names[MAX_CHAR_CODE + 1];
static char print_buf[PRINTF_BUF_SIZE];
static int hexline_length;
static char notdef[] = ".notdef";
#endif /* pdfTeX */
#include <kpathsea/c-vararg.h>
#include <kpathsea/c-proto.h>
#define t1_getchar() getc(t1_file)
#define t1_ungetchar(c) ungetc(c, t1_file)
#define t1_eof() feof(t1_file)
#define t1_close() xfclose(t1_file, cur_file_name)
#define enc_getchar() getc(enc_file)
#define enc_eof() feof(enc_file)
#define enc_close() xfclose(enc_file, cur_file_name)
#define valid_code(c) (c >= 0 && c <= MAX_CHAR_CODE)
static char *standard_glyph_names[MAX_CHAR_CODE + 1] = {
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, "space", "exclam", "quotedbl",
"numbersign", "dollar", "percent", "ampersand", "quoteright", "parenleft",
"parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash",
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "colon", "semicolon", "less", "equal", "greater", "question", "at",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft",
"backslash", "bracketright", "asciicircum", "underscore", "quoteleft", "a",
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar",
"braceright", "asciitilde", notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, "exclamdown", "cent", "sterling", "fraction", "yen", "florin",
"section", "currency", "quotesingle", "quotedblleft", "guillemotleft",
"guilsinglleft", "guilsinglright", "fi", "fl", notdef, "endash", "dagger",
"daggerdbl", "periodcentered", notdef, "paragraph", "bullet",
"quotesinglbase", "quotedblbase", "quotedblright", "guillemotright",
"ellipsis", "perthousand", notdef, "questiondown", notdef, "grave", "acute",
"circumflex", "tilde", "macron", "breve", "dotaccent", "dieresis", notdef,
"ring", "cedilla", notdef, "hungarumlaut", "ogonek", "caron", "emdash",
notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
notdef, notdef, notdef, notdef, notdef, notdef, notdef, "AE", notdef,
"ordfeminine", notdef, notdef, notdef, notdef, "Lslash", "Oslash", "OE",
"ordmasculine", notdef, notdef, notdef, notdef, notdef, "ae", notdef, notdef,
notdef, "dotlessi", notdef, notdef, "lslash", "oslash", "oe", "germandbls",
notdef, notdef, notdef, notdef
};
char **t1_glyph_names;
char *t1_builtin_glyph_names[MAX_CHAR_CODE + 1];
static boolean read_encoding_only;
static int t1_encoding;
/*
typedef char *extra_glyphs_entry;
static char **extra_glyphs_ptr, **extra_glyphs_tab;
static int extra_glyphs_max;
*/
#define T1_BUF_SIZE 0x4000
#define ENC_BUF_SIZE 1024
#define ENC_STANDARD 0
#define ENC_BUILTIN 1
#define CS_HSTEM 1
#define CS_VSTEM 3
#define CS_VMOVETO 4
#define CS_RLINETO 5
#define CS_HLINETO 6
#define CS_VLINETO 7
#define CS_RRCURVETO 8
#define CS_CLOSEPATH 9
#define CS_CALLSUBR 10
#define CS_RETURN 11
#define CS_ESCAPE 12
#define CS_HSBW 13
#define CS_ENDCHAR 14
#define CS_RMOVETO 21
#define CS_HMOVETO 22
#define CS_VHCURVETO 30
#define CS_HVCURVETO 31
#define CS_1BYTE_MAX (CS_HVCURVETO + 1)
#define CS_DOTSECTION CS_1BYTE_MAX + 0
#define CS_VSTEM3 CS_1BYTE_MAX + 1
#define CS_HSTEM3 CS_1BYTE_MAX + 2
#define CS_SEAC CS_1BYTE_MAX + 6
#define CS_SBW CS_1BYTE_MAX + 7
#define CS_DIV CS_1BYTE_MAX + 12
#define CS_CALLOTHERSUBR CS_1BYTE_MAX + 16
#define CS_POP CS_1BYTE_MAX + 17
#define CS_SETCURRENTPOINT CS_1BYTE_MAX + 33
#define CS_2BYTE_MAX (CS_SETCURRENTPOINT + 1)
#define CS_MAX CS_2BYTE_MAX
typedef unsigned char byte;
typedef struct {
byte nargs; /* number of arguments */
boolean bottom; /* take arguments from bottom of stack? */
boolean clear; /* clear stack? */
boolean valid;
} cc_entry; /* CharString Command */
typedef struct {
char *name; /* glyph name (or notdef for Subrs entry) */
byte *data;
unsigned short len; /* length of the whole string */
unsigned short cslen; /* length of the encoded part of the string */
boolean used;
boolean valid;
} cs_entry;
static unsigned short t1_dr, t1_er;
static unsigned short t1_c1 = 52845, t1_c2 = 22719;
static unsigned short t1_cslen;
static short t1_lenIV;
static char t1_line[T1_BUF_SIZE], t1_buf[T1_BUF_SIZE], *t1_line_ptr;
static char enc_line[ENC_BUF_SIZE];
static char *cs_start;
static cs_entry *cs_tab, *cs_ptr, *cs_notdef;
static char *cs_dict_start, *cs_dict_end;
static int cs_count, cs_size, cs_size_pos;
static cs_entry *subr_tab;
static char *subr_array_start, *subr_array_end;
static int subr_max, subr_size, subr_size_pos;
/* This array contains the begin/end tokens commonly used in the */
/* /Subrs array of a Type 1 font. */
static char *cs_token_pairs[4][2] = {
{"RD", "NP"},
{"-|", "|"},
{"RD", "noaccess put"},
{"-|", "noaccess put"}
};
/* Which begin/end token set do we use for the font? */
static int cs_token_choice = -1;
static boolean cs_tokens_found = false;
static boolean t1_pfa, t1_cs, t1_scan, t1_eexec_encrypt, t1_synthetic;
static int t1_in_eexec; /* 0 before eexec-encrypted, 1 during, 2 after */
static long t1_block_length;
static int last_hexbyte;
static FILE *t1_file;
static FILE *enc_file;
#define str_prefix(s1, s2) (strncmp(s1, s2, strlen(s2)) == 0)
#define t1_prefix(s) str_prefix(t1_line, s)
#define t1_buf_prefix(s) str_prefix(t1_buf, s)
#define t1_charstrings() strstr(t1_line, "/CharStrings")
#define t1_subrs() t1_prefix("/Subrs")
#define t1_end_eexec() t1_suffix("mark currentfile closefile")
#define t1_cleartomark() t1_prefix("cleartomark")
#ifndef pdfTeX
static void pdftex_fail(char *fmt,...)
{
va_list args;
va_start(args, fmt);
fputs("\nError: module writet1", stderr);
if (cur_file_name)
fprintf(stderr, " (file %s)", cur_file_name);
fputs(": ", stderr);
vsprintf(print_buf, fmt, args);
fputs(print_buf, stderr);
fputs("\n", stderr);
va_end(args);
exit(-1);
}
static void pdftex_warn(char *fmt,...)
{
va_list args;
va_start(args, fmt);
fputs("\nWarning: module writet1 of dvips", stderr);
if (cur_file_name)
fprintf(stderr, " (file %s)", cur_file_name);
fputs(": ", stderr);
vsprintf(print_buf, fmt, args);
fputs(print_buf, stderr);
fputs("\n", stderr);
va_end(args);
}
#define HEXLINE_WIDTH 64
static void end_hexline()
{
if (hexline_length == HEXLINE_WIDTH) {
fputs("\n", bitfile);
hexline_length = 0;
}
}
static void t1_outhex(byte b)
{
static char *hexdigits = "0123456789ABCDEF";
t1_putchar(hexdigits[b/16]);
t1_putchar(hexdigits[b%16]);
hexline_length += 2;
end_hexline();
}
#endif /* pdfTeX */
static void enc_getline(void)
{
char *p;
int c;
restart:
if (enc_eof())
pdftex_fail("unexpected end of file");
p = enc_line;
do {
c = enc_getchar();
append_char_to_buf(c, p, enc_line, ENC_BUF_SIZE);
} while (c != 10);
append_eol(p, enc_line, ENC_BUF_SIZE);
if (p - enc_line <= 2 || *enc_line == '%')
goto restart;
}
void load_enc(char *enc_name, char **glyph_names)
{
char buf[ENC_BUF_SIZE], *p, *r;
int names_count;
set_cur_file_name(enc_name);
if (!enc_open()) {
pdftex_warn("cannot open encoding file for reading");
cur_file_name = 0;
return;
}
t1_log("{");
t1_log(cur_file_name = full_file_name());
enc_getline();
if (*enc_line != '/' || (r = strchr(enc_line, '[')) == NULL) {
remove_eol(r, enc_line);
pdftex_fail("invalid encoding vector (a name or `[' missing): `%s'", enc_line);
}
names_count = 0;
r++; /* skip '[' */
skip(r, ' ');
for (;;) {
while (*r == '/') {
for (p = buf, r++; *r != ' ' && *r != 10 && *r != ']' && *r != '/'; *p++ = *r++);
*p = 0;
skip(r, ' ');
if (names_count > MAX_CHAR_CODE)
pdftex_fail("encoding vector contains more than %i names",
(int)(MAX_CHAR_CODE + 1));
if (strcmp(buf, notdef) != 0)
glyph_names[names_count] = xstrdup(buf);
names_count++;
}
if (*r != 10 && *r != '%') {
if (strncmp(r, "] def", strlen("] def")) == 0)
goto done;
else {
remove_eol(r, enc_line);
pdftex_fail("invalid encoding vector: a name or `] def' expected: `%s'", enc_line);
}
}
enc_getline();
r = enc_line;
}
done:
enc_close();
t1_log("}");
cur_file_name = 0;
}
static void t1_check_pfa(void)
{
int c = t1_getchar();
if (c != 128)
t1_pfa = true;
else
t1_pfa = false;
t1_ungetchar(c);
}
static int t1_getbyte(void)
{
int c = t1_getchar();
if (t1_pfa)
return c;
if (t1_block_length == 0) {
if (c != 128)
pdftex_fail("invalid marker");
c = t1_getchar();
if (c == 3) {
while (!t1_eof())
t1_getchar();
return EOF;
}
t1_block_length = t1_getchar() & 0xff;
t1_block_length |= (t1_getchar() & 0xff) << 8;
t1_block_length |= (t1_getchar() & 0xff) << 16;
t1_block_length |= (t1_getchar() & 0xff) << 24;
c = t1_getchar();
}
t1_block_length--;
return c;
}
static int hexval(int c)
{
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else if (c >= '0' && c <= '9')
return c - '0';
else
return -1;
}
static byte edecrypt(byte cipher)
{
byte plain;
if (t1_pfa) {
while (cipher == 10 || cipher == 13)
cipher = t1_getbyte();
last_hexbyte = cipher = (hexval(cipher) << 4) + hexval(t1_getbyte());
}
plain = (cipher^(t1_dr >> 8));
t1_dr = (cipher + t1_dr)*t1_c1 + t1_c2;
return plain;
}
static byte cdecrypt(byte cipher, unsigned short *cr)
{
byte plain = (cipher^(*cr >> 8));
*cr = (cipher + *cr)*t1_c1 + t1_c2;
return plain;
}
static byte eencrypt(byte plain)
{
byte cipher = (plain^(t1_er >> 8));
t1_er = (cipher + t1_er)*t1_c1 + t1_c2;
return cipher;
}
static byte cencrypt(byte plain, unsigned short *cr)
{
byte cipher = (plain^(*cr >> 8));
*cr = (cipher + *cr)*t1_c1 + t1_c2;
return cipher;
}
static char *eol(char *s)
{
char *p = strend(s);
if (p - s > 1 && p[-1] != 10) {
*p++ = 10;
*p = 0;
}
return p;
}
static float t1_scan_num(char *p, char **r)
{
float f;
skip(p, ' ');
if (sscanf(p, "%g", &f) != 1) {
remove_eol(p, t1_line);
pdftex_fail("a number expected: `%s'", t1_line);
}
if (r != 0) {
for (; isdigit(*p) || *p == '.' ||
*p == 'e' || *p == 'E' || *p == '+' || *p == '-'; p++);
*r = p;
}
return f;
}
static boolean t1_suffix(char *s)
{
char *s1 = t1_line_ptr - 1,
*s2 = strend(s) - 1;
if (*s1 == 10)
s1--;
while (s1 >= t1_line && s2 >= s) {
if (*s1-- != *s2--)
return false;
}
return s1 >= t1_line - 1;
}
static boolean t1_buf_suffix(char *s, char *r)
{
char *s1 = r - 1,
*s2 = strend(s) - 1;
if (*s1 == 10)
s1--;
while (s1 >= t1_buf && s2 >= s) {
if (*s1-- != *s2--)
return false;
}
return s1 >= t1_buf - 1;
}
static void t1_getline(void)
{
int c, l, eexec_scan;
char *p;
static char eexec_str[] = "currentfile eexec";
static int eexec_len = 17; /* strlen(eexec_str) */
restart:
if (t1_eof())
pdftex_fail("unexpected end of file");
t1_line_ptr = t1_line;
t1_cslen = 0;
eexec_scan = 0;
c = t1_getbyte();
if (c == EOF)
return;
while (!t1_eof()) {
if (t1_in_eexec == 1)
c = edecrypt(c);
append_char_to_buf(c, t1_line_ptr, t1_line, T1_BUF_SIZE);
if (t1_in_eexec == 0 && eexec_scan >= 0 && eexec_scan < eexec_len) {
if (t1_line[eexec_scan] == eexec_str[eexec_scan])
eexec_scan++;
else
eexec_scan = -1;
}
if (c == 10 || (t1_pfa && eexec_scan == eexec_len && c == 32))
break;
if (t1_cs && t1_cslen == 0 && (t1_line_ptr - t1_line > 4) &&
(t1_suffix(" RD ") || t1_suffix(" -| "))) {
p = t1_line_ptr - 5;
while (*p != ' ')
p--;
t1_cslen = l = (int) t1_scan_num(p + 1, 0);
cs_start = t1_line_ptr;
check_buf(t1_line_ptr - t1_line + l, T1_BUF_SIZE);
while (l-- > 0)
*t1_line_ptr++ = edecrypt(t1_getbyte());
}
c = t1_getbyte();
}
append_eol(t1_line_ptr, t1_line, T1_BUF_SIZE);
if (t1_line_ptr - t1_line <= 1)
goto restart;
if (eexec_scan == eexec_len)
t1_in_eexec = 1;
}
static void t1_putline(void)
{
char *p = t1_line;
if (t1_line_ptr - t1_line <= 1)
return;
if (t1_eexec_encrypt) {
while (p < t1_line_ptr)
out_eexec_char(eencrypt(*p++));
}
else
while (p < t1_line_ptr)
t1_putchar(*p++);
}
static void t1_puts(char *s)
{
if (s != t1_line)
strcpy(t1_line, s);
t1_line_ptr = strend(t1_line);
t1_putline();
}
static void t1_printf(char *fmt,...)
{
va_list args;
va_start(args, fmt);
vsprintf(t1_line, fmt, args);
t1_puts(t1_line);
va_end(args);
}
static void t1_init_params(char *open_name_prefix)
{
t1_log(open_name_prefix);
t1_log(cur_file_name);
t1_lenIV = 4;
t1_dr = 55665;
t1_er = 55665;
t1_in_eexec = 0;
t1_cs = false;
t1_scan = true;
t1_synthetic = false;
t1_eexec_encrypt = false;
t1_block_length = 0;
t1_check_pfa();
}
static void t1_close_font_file(char *close_name_suffix)
{
t1_log(close_name_suffix);
t1_close();
cur_file_name = 0;
}
static void t1_check_block_len(boolean decrypt)
{
int l, c;
if (t1_block_length == 0)
return;
c = t1_getbyte();
if (decrypt)
c = edecrypt(c);
l = t1_block_length;
if (!(l == 0 && (c == 10 || c == 13))) {
pdftex_warn("%i bytes more than expected were ignored", l + 1);
while (l-- > 0)
t1_getbyte();
}
}
static void t1_start_eexec(void)
{
int i;
if (is_included(fm_cur)) {
get_length1();
save_offset();
}
if (!t1_pfa)
t1_check_block_len(false);
for (t1_line_ptr = t1_line, i = 0; i < 4; i++) {
edecrypt(t1_getbyte());
*t1_line_ptr++ = 0;
}
t1_eexec_encrypt = true;
if (is_included(fm_cur))
t1_putline(); /* to put the first four bytes */
}
static void t1_stop_eexec(void)
{
int c;
if (is_included(fm_cur)) {
get_length2();
save_offset();
}
end_last_eexec_line();
if (!t1_pfa)
t1_check_block_len(true);
else {
c = edecrypt(t1_getbyte());
if (!(c == 10 || c == 13)) {
if (last_hexbyte == 0)
t1_puts("00");
else
pdftex_warn("unexpected data after eexec");
}
}
t1_cs = false;
t1_in_eexec = 2;
}
#ifdef pdfTeX
static void t1_modify_fm(void)
{
/*
* font matrix is given as six numbers a0..a5, which stands for the matrix
*
* a0 a1 0
* M = a2 a3 0
* a4 a5 1
*
* ExtendFont is given as
*
* e 0 0
* E = 0 1 0
* 0 0 1
*
* SlantFont is given as
*
* 1 0 0
* S = s 1 0
* 0 0 1
*
* and the final transformation is
*
* e*a0 e*a1 0
* F = E.S.M = s*e*a0+a2 s*e*a1+a3 0
* a4 a5 1
*/
float e, s, a[6], b[6];
int i, c;
char *p, *q, *r;
if ((p = strchr(t1_line, '[')) == 0)
if ((p = strchr(t1_line, '{')) == 0) {
remove_eol(p, t1_line);
pdftex_fail("FontMatrix: an array expected: `%s'", t1_line);
}
c = *p++; /* save the character '[' resp. '{' */
strncpy(t1_buf, t1_line, (unsigned)(p - t1_line));
r = t1_buf + (p - t1_line);
for (i = 0; i < 6; i++) {
a[i] = t1_scan_num(p, &q);
p = q;
}
if (fm_extend(fm_cur) != 0)
e = fm_extend(fm_cur)*1E-3;
else
e = 1;
s = fm_slant(fm_cur)*1E-3;
b[0] = e*a[0];
b[1] = e*a[1];
b[2] = s*e*a[0] + a[2];
b[3] = s*e*a[1] + a[3];
b[4] = a[4];
b[5] = a[5];
for (i = 0; i < 6; i++) {
sprintf(r, "%G ", b[i]);
r = strend(r);
}
if (c == '[') {
while (*p != ']' && *p != 0)
p++;
}
else {
while (*p != '}' && *p != 0)
p++;
}
if (*p == 0) {
remove_eol(p, t1_line);
pdftex_fail("FontMatrix: cannot find the corresponding character to '%c': `%s'", c, t1_line);
}
strcpy(r, p);
strcpy(t1_line, t1_buf);
t1_line_ptr = eol(t1_line);
}
static void t1_modify_italic(void)
{
float a;
char *p, *r;
if (fm_slant(fm_cur) == 0)
return;
p = strchr(t1_line, ' ');
strncpy(t1_buf, t1_line, (unsigned)(p - t1_line + 1));
a = t1_scan_num(p + 1, &r);
a -= atan(fm_slant(fm_cur)*1E-3)*(180/M_PI);
sprintf(t1_buf + (p - t1_line + 1), "%.2g", a);
strcpy(strend(t1_buf), r);
strcpy(t1_line, t1_buf);
t1_line_ptr = eol(t1_line);
font_keys[ITALIC_ANGLE_CODE].value.num = round(a);
font_keys[ITALIC_ANGLE_CODE].valid = true;
}
static void t1_scan_keys(void)
{
int i, k;
char *p, *q, *r;
key_entry *key;
if (fm_extend(fm_cur) != 0 || fm_slant(fm_cur) != 0) {
if (strncmp(t1_line + 1, "FontMatrix", strlen("FontMatrix")) == 0) {
t1_modify_fm();
return;
}
if (strncmp(t1_line + 1, "ItalicAngle", strlen("ItalicAngle")) == 0) {
t1_modify_italic();
return;
}
}
for (key = font_keys; key - font_keys < MAX_KEY_CODE; key++)
if (strncmp(t1_line + 1, key->t1name, strlen(key->t1name)) == 0)
break;
if (key - font_keys == MAX_KEY_CODE)
return;
key->valid = true;
p = t1_line + strlen(key->t1name) + 1;
skip(p, ' ');
if ((k = key - font_keys) == FONTNAME_CODE) {
if (*p != '/') {
remove_eol(p, t1_line);
pdftex_fail("a name expected: `%s'", t1_line);
}
r = ++p; /* skip the slash */
for (q = t1_buf; *p != ' ' && *p != 10; *q++ = *p++);
*q = 0;
if (fm_extend(fm_cur) != 0) {
sprintf(q, "-Extend_%i", (int)fm_extend(fm_cur));
}
if (fm_slant(fm_cur) != 0) {
sprintf(q, "-Slant_%i", (int)fm_slant(fm_cur));
}
key->value.string = xstrdup(t1_buf);
if (is_included(fm_cur) && is_subsetted(fm_cur)) {
t1_fontname_offset = ff_offset() + (r - t1_line);
strcpy(t1_buf, p);
sprintf(r, "******+%s%s", key->value.string, t1_buf);
t1_line_ptr = eol(r);
}
return;
}
if ((k == STEMV_CODE || k == FONTBBOX1_CODE) &&
(*p == '[' || *p == '{'))
p++;
if (k == FONTBBOX1_CODE) {
for (i = 0; i < 4; i++) {
key[i].value.num = t1_scan_num(p, &r);
p = r;
}
return;
}
key->value.num = t1_scan_num(p, 0);
}
#endif /* pdfTeX */
static void t1_scan_param(void)
{
static char *lenIV = "/lenIV";
if (!t1_scan || *t1_line != '/')
return;
if (t1_prefix(lenIV)) {
t1_lenIV = (int) t1_scan_num(t1_line + strlen(lenIV), 0);
return;
}
t1_scan_keys();
}
static void copy_glyph_names(char **glyph_names, int a, int b)
{
if (glyph_names[b] != notdef) {
free(glyph_names[b]);
glyph_names[b] = notdef;
}
if (glyph_names[a] != notdef) {
glyph_names[b] = xstrdup(glyph_names[a]);
}
}
static void t1_builtin_enc(void)
{
int i, a, b, c, counter = 0;
char *r, *p;
/*
* At this moment "/Encoding" is the prefix of t1_line
*/
if (t1_suffix("def")) { /* predefined encoding */
sscanf(t1_line + strlen("/Encoding"), "%256s", t1_buf);
if (strcmp(t1_buf, "StandardEncoding") == 0) {
for (i = 0; i <= MAX_CHAR_CODE; i++)
if (standard_glyph_names[i] == notdef)
t1_builtin_glyph_names[i] = notdef;
else
t1_builtin_glyph_names[i] = xstrdup(standard_glyph_names[i]);
t1_encoding = ENC_STANDARD;
}
else
pdftex_fail("cannot subset font (unknown predefined encoding `%s')",
t1_buf);
return;
}
t1_encoding = ENC_BUILTIN;
/*
* At this moment "/Encoding" is the prefix of t1_line, and the encoding is
* not a predefined encoding
*
* We have two possible forms of Encoding vector. The first case is
*
* /Encoding [/a /b /c...] readonly def
*
* and the second case can look like
*
* /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for
* dup 0 /x put
* dup 1 /y put
* ...
* readonly def
*/
for (i = 0; i <= MAX_CHAR_CODE; i++)
t1_builtin_glyph_names[i] = notdef;
if (t1_prefix("/Encoding [") || t1_prefix("/Encoding[")) { /* the first case */
r = strchr(t1_line, '[') + 1;
skip(r, ' ');
for(;;) {
while (*r == '/') {
for (p = t1_buf, r++;
*r != 32 && *r != 10 && *r != ']' && *r != '/';
*p++ = *r++);
*p = 0;
skip(r, ' ');
if (counter > MAX_CHAR_CODE)
pdftex_fail("encoding vector contains more than %i names",
(int)(MAX_CHAR_CODE + 1));
if (strcmp(t1_buf, notdef) != 0)
t1_builtin_glyph_names[counter] = xstrdup(t1_buf);
counter++;
}
if (*r != 10 && *r != '%') {
if (str_prefix(r, "] def") || str_prefix(r, "] readonly def"))
break;
else {
remove_eol(r, t1_line);
pdftex_fail("a name or `] def' or `] readonly def' expected: `%s'",
t1_line);
}
}
t1_getline();
r = t1_line;
}
}
else { /* the second case */
p = strchr(t1_line, 10);
for (;;) {
if (*p == 10) {
t1_getline();
p = t1_line;
}
/*
check for `dup <index> <glyph> put'
*/
if (sscanf(p, "dup %i%256s put", &i, t1_buf) == 2 &&
*t1_buf == '/' && valid_code(i)) {
if (strcmp(t1_buf + 1, notdef) != 0)
t1_builtin_glyph_names[i] = xstrdup(t1_buf + 1);
p = strstr(p, " put") + strlen(" put");
skip(p, ' ');
}
/*
check for `dup dup <to> exch <from> get put'
*/
else if (sscanf(p, "dup dup %i exch %i get put", &b, &a) == 2 &&
valid_code(a) && valid_code(b)) {
copy_glyph_names(t1_builtin_glyph_names, a, b);
p = strstr(p, " get put") + strlen(" get put");
skip(p, ' ');
}
/*
check for `dup dup <from> <size> getinterval <to> exch putinterval'
*/
else if (sscanf(p, "dup dup %i %i getinterval %i exch putinterval",
&a, &c, &b) == 3 &&
valid_code(a) && valid_code(b) && valid_code(c)) {
for (i = 0; i < c; i++)
copy_glyph_names(t1_builtin_glyph_names, a + i, b + i);
p = strstr(p, " putinterval") + strlen(" putinterval");
skip(p, ' ');
}
/*
check for `def' or `readonly def'
*/
else if ((p == t1_line || (p > t1_line && p[-1] == ' ')) &&
strcmp(p, "def\n") == 0)
return;
/*
skip an unrecognizable word
*/
else {
while (*p != ' ' && *p != 10)
p++;
skip(p, ' ');
}
}
}
}
static void t1_check_predef_enc(void)
{
}
static void t1_check_end(void)
{
if (t1_eof())
return;
t1_getline();
if (t1_prefix("{restore}"))
t1_putline();
}
#ifdef pdfTeX
static boolean t1_open_fontfile(char *open_name_prefix)
{
char *ex_ffname = 0;
if (fm_cur->expansion != 0) {
ex_ffname = mk_exname(fm_cur->ff_name, fm_cur->expansion);
set_cur_file_name(ex_ffname);
if (t1_open()) { /* found mm instance */
cur_file_name = full_file_name();
goto open_ok;
}
}
if (fm_cur->found)
t1_file = xfopen(cur_file_name = fm_cur->ff_name, FOPEN_RBIN_MODE);
else {
set_cur_file_name(fm_cur->ff_name);
if (!t1_open()) {
pdftex_warn("cannot open Type 1 font file for reading");
return false;
}
fix_ffname(fm_cur, cur_file_name = full_file_name());
}
if (fm_cur->expansion != 0 && is_included(fm_cur)) { /* use ExtendFont to simulate MM instance */
if (fm_extend(fm_cur) == 0)
fm_extend(fm_cur) = 1000;
fm_extend(fm_cur) =
roundxnoverd(fm_extend(fm_cur), 1000 + fm_cur->expansion, 1000);
}
open_ok:
t1_init_params(open_name_prefix);
fontfile_found = true;
xfree(ex_ffname);
return true;
}
boolean t1_read_enc(fm_entry *fm)
{
read_encoding_only = true;
fm_cur = fm;
if (!t1_open_fontfile("{"))
return false;
fix_ffname(fm_cur, cur_file_name = full_file_name());
while (!t1_prefix("/Encoding"))
t1_getline();
t1_builtin_enc();
t1_close_font_file("}");
return true;
}
static void t1_scan_only(void)
{
do {
t1_getline();
t1_scan_param();
} while (t1_in_eexec == 0);
t1_start_eexec();
do {
t1_getline();
t1_scan_param();
} while (!(t1_charstrings() || t1_subrs()));
}
static void t1_include(void)
{
save_offset();
do {
t1_getline();
t1_scan_param();
t1_putline();
} while (t1_in_eexec == 0);
t1_start_eexec();
do {
t1_getline();
t1_scan_param();
t1_putline();
} while (!(t1_charstrings() || t1_subrs()));
t1_cs = true;
do {
t1_getline();
t1_putline();
} while (!t1_end_eexec());
t1_stop_eexec();
do {
t1_getline();
t1_putline();
} while (!t1_cleartomark());
t1_check_end(); /* write "{restore}if" if found */
get_length3();
}
#else /* not pdfTeX */
static boolean t1_open_fontfile(char *open_name_prefix)
{
if (!t1_open())
return false;
t1_init_params(open_name_prefix);
return true;
}
#endif /* pdfTeX */
#define check_subr(subr) \
if (subr >= subr_size || subr < 0) \
pdftex_fail("Subrs array: entry index out of range (%i)", subr);
static void cs_store(boolean is_subr)
{
char *p, *q;
cs_entry *ptr;
int subr;
for (p = t1_line, q = t1_buf; *p != ' '; *q++ = *p++);
*q = 0;
if (is_subr) {
subr = (int) t1_scan_num(p + 1, 0);
check_subr(subr);
ptr = subr_tab + subr;
}
else {
ptr = cs_ptr++;
if (cs_ptr - cs_tab > cs_size)
pdftex_fail("CharStrings dict: more entries than dict size (%i)", cs_size);
if (strcmp(t1_buf + 1, notdef) == 0) /* skip the slash */
ptr->name = notdef;
else
ptr->name = xstrdup(t1_buf + 1);
}
/* copy " RD " + cs data to t1_buf */
memcpy(t1_buf, cs_start - 4, (unsigned)(t1_cslen + 4));
/* copy the end of cs data to t1_buf */
for (p = cs_start + t1_cslen, q = t1_buf + t1_cslen + 4; *p != 10; *q++ = *p++);
*q++ = 10;
/* get the begin/end token pairs. Modify this as necessary for other token pairs. */
if (is_subr && !cs_tokens_found) {
if (t1_buf_prefix(" RD") && t1_buf_suffix("NP", q))
{ cs_token_choice = 0; cs_tokens_found = true; }
else if (t1_buf_prefix(" -|") && t1_buf_suffix("|", q))
{ cs_token_choice = 1; cs_tokens_found = true; }
else if (t1_buf_prefix(" RD") && t1_buf_suffix("noaccess put", q))
{ cs_token_choice = 2; cs_tokens_found = true; }
else if (t1_buf_prefix(" -|") && t1_buf_suffix("noaccess put", q))
{ cs_token_choice = 3; cs_tokens_found = true; }
}
ptr->len = q - t1_buf;
ptr->cslen = t1_cslen;
ptr->data = xtalloc(ptr->len, byte);
memcpy(ptr->data, t1_buf, ptr->len);
ptr->valid = true;
}
#define store_subr() cs_store(true)
#define store_cs() cs_store(false)
#define CC_STACK_SIZE 24
static integer cc_stack[CC_STACK_SIZE], *stack_ptr = cc_stack;
static cc_entry cc_tab[CS_MAX];
static boolean is_cc_init = false;
#define cc_pop(N) \
if (stack_ptr - cc_stack < (N)) \
stack_error(N); \
stack_ptr -= N
#define stack_error(N) { \
pdftex_warn("CharString: invalid access (%i) to stack (%i entries)", \
N, stack_ptr - cc_stack); \
goto cs_error; \
}
/*
static integer cc_get(integer index)
{
if (index < 0) {
if (stack_ptr + index < cc_stack )
stack_error(stack_ptr - cc_stack + index);
return *(stack_ptr + index);
}
else {
if (cc_stack + index >= stack_ptr)
stack_error(index);
return cc_stack[index];
}
}
*/
#define cc_get(N) ((N) < 0 ? *(stack_ptr + (N)) : *(cc_stack + (N)))
#define cc_push(V) *stack_ptr++ = V
#define cc_clear() stack_ptr = cc_stack
#define set_cc(N, B, A, C) \
cc_tab[N].nargs = A; \
cc_tab[N].bottom = B; \
cc_tab[N].clear = C; \
cc_tab[N].valid = true
static void cc_init(void)
{
int i;
if (is_cc_init)
return;
for (i = 0; i < CS_MAX; i++)
cc_tab[i].valid = false;
set_cc(CS_HSTEM, true, 2, true);
set_cc(CS_VSTEM, true, 2, true);
set_cc(CS_VMOVETO, true, 1, true);
set_cc(CS_RLINETO, true, 2, true);
set_cc(CS_HLINETO, true, 1, true);
set_cc(CS_VLINETO, true, 1, true);
set_cc(CS_RRCURVETO, true, 6, true);
set_cc(CS_CLOSEPATH, false, 0, true);
set_cc(CS_CALLSUBR, false, 1, false);
set_cc(CS_RETURN, false, 0, false);
/*
set_cc(CS_ESCAPE, false, 0, false);
*/
set_cc(CS_HSBW, true, 2, true);
set_cc(CS_ENDCHAR, false, 0, true);
set_cc(CS_RMOVETO, true, 2, true);
set_cc(CS_HMOVETO, true, 1, true);
set_cc(CS_VHCURVETO, true, 4, true);
set_cc(CS_HVCURVETO, true, 4, true);
set_cc(CS_DOTSECTION, false, 0, true);
set_cc(CS_VSTEM3, true, 6, true);
set_cc(CS_HSTEM3, true, 6, true);
set_cc(CS_SEAC, true, 5, true);
set_cc(CS_SBW, true, 4, true);
set_cc(CS_DIV, false, 2, false);
set_cc(CS_CALLOTHERSUBR, false, 0, false);
set_cc(CS_POP, false, 0, false);
set_cc(CS_SETCURRENTPOINT, true, 2, true);
is_cc_init = true;
}
#define cs_getchar() cdecrypt(*data++, &cr)
#define mark_subr(n) cs_mark(0, n)
#define mark_cs(s) cs_mark(s, 0)
static void cs_warn(char *cs_name, int subr, char *fmt,...)
{
char buf[SMALL_BUF_SIZE];
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
if (cs_name == 0)
pdftex_warn("Subr (%i): %s", (int)subr, buf);
else
pdftex_warn("CharString (/%s): %s", cs_name, buf);
}
static void cs_mark(char *cs_name, int subr)
{
byte *data;
int i, b, cs_len;
integer a, a1, a2;
unsigned short cr;
static integer lastargOtherSubr3 = 3; /* the argument of last call to
OtherSubrs[3] */
cs_entry *ptr;
cc_entry *cc;
if (cs_name == 0) {
check_subr(subr);
ptr = subr_tab + subr;
if (!ptr->valid)
return;
}
else {
if (cs_notdef != 0 &&
(cs_name == notdef || strcmp(cs_name, notdef) == 0))
ptr = cs_notdef;
else {
for (ptr = cs_tab; ptr < cs_ptr; ptr++)
if (strcmp(ptr->name, cs_name) == 0)
break;
if (ptr == cs_ptr) {
pdftex_warn("glyph `%s' undefined", cs_name);
return;
}
if (ptr->name == notdef)
cs_notdef = ptr;
}
}
/* only marked CharString entries and invalid entries can be skipped;
valid marked subrs must be parsed to keep the stack */
if (!ptr->valid || (ptr->used && cs_name != 0))
return;
ptr->used = true;
cr = 4330;
cs_len = ptr->cslen;
data = ptr->data + 4;
for (i = 0; i < t1_lenIV; i++, cs_len--)
cs_getchar();
while (cs_len > 0) {
--cs_len;
b = cs_getchar();
if (b >= 32) {
if (b <= 246)
a = b - 139;
else if (b <= 250) {
--cs_len;
a = ((b - 247) << 8) + 108 + cs_getchar();
}
else if (b <= 254) {
--cs_len;
a = -((b - 251) << 8) - 108 - cs_getchar();
}
else {
cs_len -= 4;
a = (cs_getchar() & 0xff) << 24;
a |= (cs_getchar() & 0xff) << 16;
a |= (cs_getchar() & 0xff) << 8;
a |= (cs_getchar() & 0xff) << 0;
if (sizeof(integer) > 4 && (a & 0x80000000))
a |= ~0x7FFFFFFF;
}
cc_push(a);
}
else {
if (b == CS_ESCAPE) {
b = cs_getchar() + CS_1BYTE_MAX;
cs_len--;
}
if (b >= CS_MAX) {
cs_warn(cs_name, subr, "command value out of range: %i", (int)b);
goto cs_error;
}
cc = cc_tab + b;
if (!cc->valid) {
cs_warn(cs_name, subr, "command not valid: %i", (int)b);
goto cs_error;
}
if (cc->bottom) {
if (stack_ptr - cc_stack < cc->nargs)
cs_warn(cs_name, subr,
"less arguments on stack (%i) than required (%i)",
(int)(stack_ptr - cc_stack), (int)cc->nargs);
else if (stack_ptr - cc_stack > cc->nargs)
cs_warn(cs_name, subr,
"more arguments on stack (%i) than required (%i)",
(int)(stack_ptr - cc_stack), (int)cc->nargs);
}
switch (cc - cc_tab) {
case CS_CALLSUBR:
a1 = cc_get(-1);
cc_pop(1);
mark_subr(a1);
if (!subr_tab[a1].valid) {
cs_warn(cs_name, subr,
"cannot call subr (%i)", (int)a1);
goto cs_error;
}
break;
case CS_DIV:
cc_pop(2);
cc_push(0);
break;
case CS_CALLOTHERSUBR:
if (cc_get(-1) == 3)
lastargOtherSubr3 = cc_get(-3);
a1 = cc_get(-2) + 2;
cc_pop(a1);
break;
case CS_POP:
cc_push(lastargOtherSubr3);
/* the only case when we care about the value being pushed onto
stack is when POP follows CALLOTHERSUBR (changing hints by
OtherSubrs[3])
*/
break;
case CS_SEAC:
a1 = cc_get(3);
a2 = cc_get(4);
cc_clear();
mark_cs(standard_glyph_names[a1]);
mark_cs(standard_glyph_names[a2]);
break;
default:
if (cc->clear)
cc_clear();
}
}
}
return;
cs_error: /* an error occured during parsing */
cc_clear();
ptr->valid = false;
ptr->used = false;
}
static void t1_subset_ascii_part(void)
{
int i, j;
save_offset();
t1_getline();
while (!t1_prefix("/Encoding")) {
t1_scan_param();
t1_putline();
t1_getline();
}
t1_builtin_enc();
if (is_reencoded(fm_cur))
t1_glyph_names = external_enc();
else {
t1_glyph_names = t1_builtin_glyph_names;
update_builtin_enc(tex_font, t1_glyph_names);
}
if (is_included(fm_cur) && is_subsetted(fm_cur))
make_subset_tag(fm_cur, t1_fontname_offset);
if (dvips_pdfmovechars == 0 && t1_encoding == ENC_STANDARD)
t1_puts("/Encoding StandardEncoding def\n");
else {
t1_puts("/Encoding 256 array\n0 1 255 {1 index exch /.notdef put} for\n");
for (i = 0, j = 0; i <= MAX_CHAR_CODE; i++) {
if (is_used_char(i) && t1_glyph_names[i] != notdef) {
j++;
t1_printf("dup %i /%s put\n", (int)t1_char(i), t1_glyph_names[i]);
}
}
/* We didn't mark anything for the Encoding array. */
/* We add "dup 0 /.notdef put" for compatibility */
/* with Acrobat 5.0. */
if (j == 0)
t1_puts("dup 0 /.notdef put\n");
t1_puts("readonly def\n");
}
do {
t1_getline();
t1_scan_param();
t1_putline();
} while (t1_in_eexec == 0);
}
#define t1_subr_flush() t1_flush_cs(true)
#define t1_cs_flush() t1_flush_cs(false)
static void t1_flush_cs(boolean);
static void cs_init(void)
{
cs_ptr = cs_tab = 0;
cs_dict_start = cs_dict_end = 0;
cs_count = cs_size = cs_size_pos = 0;
subr_tab = 0;
subr_array_start = subr_array_end = 0;
subr_max = subr_size = subr_size_pos = 0;
}
static void init_cs_entry(cs_entry *cs)
{
cs->data = 0;
cs->name = 0;
cs->len = 0;
cs->cslen = 0;
cs->used = false;
cs->valid = false;
}
static void t1_mark_glyphs(void);
static void t1_read_subrs(void)
{
int i;
char *s;
cs_entry *ptr;
t1_getline();
while (!(t1_charstrings() || t1_subrs())) {
t1_scan_param();
t1_putline();
t1_getline();
}
found:
t1_cs = true;
t1_scan = false;
if (!t1_subrs())
return;
subr_size_pos = strlen("/Subrs") + 1;
/* subr_size_pos points to the number indicating dict size after "/Subrs" */
subr_size = (int) t1_scan_num(t1_line + subr_size_pos, 0);
if (subr_size == 0) {
while (!t1_charstrings())
t1_getline();
return;
}
subr_tab = xtalloc(subr_size, cs_entry);
for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
init_cs_entry(ptr);
subr_array_start = xstrdup(t1_line);
t1_getline();
while (t1_cslen) {
store_subr();
t1_getline();
}
/* mark the first four entries without parsing */
for (i = 0; i < subr_size && i < 4; i++)
subr_tab[i].used = true;
/* the end of the Subrs array might have more than one line so we need to
concatnate them to subr_array_end. Unfortunately some fonts don't have
the Subrs array followed by the CharStrings dict immediately (synthetic
fonts). If we cannot find CharStrings in next POST_SUBRS_SCAN lines then
we will treat the font as synthetic and ignore everything until next
Subrs is found
*/
#define POST_SUBRS_SCAN 5
s = t1_buf;
*t1_buf = 0;
for (i = 0; i < POST_SUBRS_SCAN; i++) {
if (t1_charstrings())
break;
check_buf((t1_line_ptr - t1_line) + (s - t1_buf), T1_BUF_SIZE);
strcat(t1_buf, t1_line);
s += t1_line_ptr - t1_line;
t1_getline();
}
subr_array_end = xstrdup(t1_buf);
if (i == POST_SUBRS_SCAN) { /* CharStrings not found;
suppose synthetic font */
for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
if (ptr->valid)
xfree(ptr->data);
xfree(subr_tab);
xfree(subr_array_start);
xfree(subr_array_end);
cs_init();
t1_cs = false;
t1_synthetic = true;
while (!(t1_charstrings() || t1_subrs()))
t1_getline();
goto found;
}
}
static void t1_flush_cs(boolean is_subr)
{
char *p;
byte *r, return_cs[T1_BUF_SIZE];
cs_entry *tab, *end_tab, *ptr;
char *start_line, *line_end;
int count, size_pos;
unsigned short cr, cs_len;
if (is_subr) {
start_line = subr_array_start;
line_end = subr_array_end;
size_pos = subr_size_pos;
tab = subr_tab;
count = subr_max + 1;
end_tab = subr_tab + count;
}
else {
start_line = cs_dict_start;
line_end = cs_dict_end;
size_pos = cs_size_pos;
tab = cs_tab;
end_tab = cs_ptr;
count = cs_count;
}
t1_line_ptr = t1_line;
for (p = start_line; p - start_line < size_pos;)
*t1_line_ptr++ = *p++;
while (isdigit(*p))
p++;
sprintf(t1_line_ptr, "%u", count);
strcat(t1_line_ptr, p);
t1_line_ptr = eol(t1_line);
t1_putline();
if (is_subr) {
cr = 4330;
cs_len = 0;
if (t1_lenIV >= 0) {
for (cs_len = 0, r = return_cs; cs_len < t1_lenIV; cs_len++, r++)
*r = cencrypt(0x00, &cr);
*r = cencrypt(CS_RETURN, &cr);
}
else {
*return_cs = CS_RETURN;
}
cs_len++;
}
for (ptr = tab; ptr < end_tab; ptr++) {
if (ptr->used) {
if (is_subr)
sprintf(t1_line, "dup %u %u", ptr - tab, ptr->cslen);
else
sprintf(t1_line, "/%s %u", ptr->name, ptr->cslen);
p = strend(t1_line);
memcpy(p, ptr->data, ptr->len);
t1_line_ptr = p + ptr->len;
t1_putline();
}
else {
if (is_subr) {
sprintf(t1_line, "dup %u %u %s ", ptr - tab, cs_len,
cs_token_pairs[cs_token_choice][0]);
p = strend(t1_line);
memcpy(p, return_cs, cs_len);
t1_line_ptr = p + cs_len;
t1_putline();
sprintf(t1_line, " %s", cs_token_pairs[cs_token_choice][1]);
t1_line_ptr = eol(t1_line);
t1_putline();
}
}
xfree(ptr->data);
if (ptr->name != 0 && ptr->name != notdef)
xfree(ptr->name);
}
sprintf(t1_line, "%s", line_end);
t1_line_ptr = eol(t1_line);
t1_putline();
if (is_subr) {
cs_token_choice = -1;
cs_tokens_found = false;
}
xfree(tab);
xfree(start_line);
xfree(line_end);
}
static void t1_mark_glyphs(void)
{
int i;
char *charset = extra_charset();
char *g, *s, *r;
cs_entry *ptr;
if (t1_synthetic || embed_all_glyphs(tex_font)) { /* mark everything */
if (cs_tab != 0)
for (ptr = cs_tab; ptr < cs_ptr; ptr++)
if (ptr->valid)
ptr->used = true;
if (subr_tab != 0) {
for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
if (ptr->valid)
ptr->used = true;
subr_max = subr_size - 1;
}
return;
}
mark_cs(notdef);
for (i = 0; i <= MAX_CHAR_CODE; i++)
if (is_used_char(i)) {
if (t1_glyph_names[i] == notdef)
pdftex_warn("character %i is mapped to %s", i, notdef);
else
mark_cs(t1_glyph_names[i]);
}
if (charset == 0)
goto set_subr_max;
g = s = charset + 1; /* skip the first '/' */
r = strend(g);
while (g < r) {
while (*s != '/' && s < r)
s++;
*s = 0; /* terminate g by rewriting '/' to 0 */
mark_cs(g);
g = s + 1;
}
set_subr_max:
if (subr_tab != 0)
for (subr_max = -1, ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
if (ptr->used && ptr - subr_tab > subr_max)
subr_max = ptr - subr_tab;
}
static void t1_subset_charstrings(void)
{
cs_entry *ptr;
cs_size_pos = strstr(t1_line, "/CharStrings") + strlen("/CharStrings")
- t1_line + 1;
/* cs_size_pos points to the number indicating
dict size after "/CharStrings" */
cs_size = (int) t1_scan_num(t1_line + cs_size_pos, 0);
cs_ptr = cs_tab = xtalloc(cs_size, cs_entry);
for (ptr = cs_tab; ptr - cs_tab < cs_size; ptr++)
init_cs_entry(ptr);
cs_notdef = 0;
cs_dict_start = xstrdup(t1_line);
t1_getline();
while (t1_cslen) {
store_cs();
t1_getline();
}
cs_dict_end = xstrdup(t1_line);
t1_mark_glyphs();
if (subr_tab != 0) {
if (cs_token_choice == -1)
pdftex_fail("This Type 1 font uses mismatched subroutine begin/end token pairs.");
t1_subr_flush();
}
for (cs_count = 0, ptr = cs_tab; ptr < cs_ptr; ptr++)
if (ptr->used)
cs_count++;
t1_cs_flush();
}
static void t1_subset_end(void)
{
if (t1_synthetic) { /* copy to "dup /FontName get exch definefont pop" */
while (!strstr(t1_line, "definefont")) {
t1_getline();
t1_putline();
}
while (!t1_end_eexec())
t1_getline(); /* ignore the rest */
t1_putline(); /* write "mark currentfile closefile" */
}
else while (!t1_end_eexec()) { /* copy to "mark currentfile closefile" */
t1_getline();
t1_putline();
}
t1_stop_eexec();
while (!t1_cleartomark()) {
t1_getline();
t1_putline();
}
if (!t1_synthetic) /* don't check "{restore}if" for synthetic fonts */
t1_check_end(); /* write "{restore}if" if found */
get_length3();
}
void writet1(void)
{
read_encoding_only = false;
#ifdef pdfTeX
if (strcasecmp(strend(fm_fontfile(fm_cur)) - 4, ".otf") == 0) {
if (!is_included(fm_cur) || is_subsetted(fm_cur))
pdftex_fail("OTF fonts must be included entirely");
writeotf();
is_otf_font = true;
return;
}
#endif
if (!is_included(fm_cur)) { /* scan parameters from font file */
if (!t1_open_fontfile("{"))
return;
t1_scan_only();
t1_close_font_file("}");
return;
}
if (!is_subsetted(fm_cur)) { /* include entire font */
if (!t1_open_fontfile("<<"))
return;
t1_include();
t1_close_font_file(">>");
return;
}
/* partial downloading */
if (!t1_open_fontfile("<"))
return;
t1_subset_ascii_part();
t1_start_eexec();
cc_init();
cs_init();
t1_read_subrs();
t1_subset_charstrings();
t1_subset_end();
t1_close_font_file(">");
}
#ifndef pdfTeX
boolean t1_subset(char *fontfile, char *encfile, unsigned char *g)
{
int i;
cur_enc_name = encfile;
for (i = 0; i <= MAX_CHAR_CODE; i++)
ext_glyph_names[i] = notdef;
if (cur_enc_name != 0)
load_enc(cur_enc_name, ext_glyph_names);
grid = g;
cur_file_name = fontfile;
hexline_length = 0;
writet1();
for (i = 0; i <= MAX_CHAR_CODE; i++)
if (ext_glyph_names[i] != notdef)
free(ext_glyph_names[i]);
return 1 ; /* note: there *is* no unsuccessful return */
}
boolean t1_subset_2(char *fontfile, unsigned char *g, char *extraGlyphs)
{
int i;
for (i = 0; i <= MAX_CHAR_CODE; i++)
ext_glyph_names[i] = notdef;
grid = g;
cur_file_name = fontfile;
hexline_length = 0;
dvips_extra_charset = extraGlyphs ;
writet1();
for (i = 0; i <= MAX_CHAR_CODE; i++)
if (ext_glyph_names[i] != notdef)
free(ext_glyph_names[i]);
return 1 ; /* note: there *is* no unsuccessful return */
}
#endif /* not pdfTeX */
|