Hash :
04aa800c
Author :
Date :
2000-06-01T03:27:48
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
#include <freetype/config/ftconfig.h>
#include <freetype/internal/ftdebug.h>
#include <freetype/internal/t1types.h>
#include <t1tokens.h>
#include <t1parse.h>
#include <stdio.h>
#undef FT_COMPONENT
#define FT_COMPONENT trace_t1load
typedef T1_Error (*T1_Parse_Func)( T1_Parser* parser );
/*************************************************************************/
/* */
/* <Function> Init_T1_Parser */
/* */
/* <Description> */
/* Initialise a given parser object to build a given T1_Face */
/* */
/* <Input> */
/* parser :: handle to the newly built parser object */
/* face :: handle to target T1 face object */
/* */
LOCAL_FUNC
void Init_T1_Parser( T1_Parser* parser,
T1_Face face,
T1_Tokenizer tokenizer )
{
parser->error = 0;
parser->face = face;
parser->tokenizer = tokenizer;
parser->top = parser->stack;
parser->limit = parser->stack + T1_MAX_STACK_DEPTH;
parser->state_index = 0;
parser->state_stack[0] = dict_none;
parser->encoding_type = t1_encoding_none;
parser->encoding_names = 0;
parser->encoding_offsets = 0;
parser->encoding_lengths = 0;
parser->dump_tokens = 0;
face->type1.private_dict.lenIV = 4; /* XXX : is it sure ?? */
}
/*************************************************************************/
/* */
/* <Function> Next_T1_Token */
/* */
/* <Description> */
/* grabs the next significant token from a parser's input stream. */
/* this function ignores a number of tokens, and translates */
/* alternate forms into their common ones.. */
/* */
/* <Input> */
/* parser :: handle to source parser */
/* */
/* <Output> */
/* token :: the extracted token descriptor */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
LOCAL_FUNC
T1_Error Next_T1_Token( T1_Parser* parser,
T1_Token* token )
{
T1_Error error;
T1_Tokenizer tokzer = parser->tokenizer;
L1:
error = Read_Token( tokzer );
if (error) return error;
/* We now must ignore a number of tokens like "dup", "executeonly", */
/* "readonly", etc... */
*token = tokzer->token;
if ( token->kind == tok_keyword )
switch( token->kind2 )
{
case key_dup:
case key_execonly:
case key_readonly:
case key_noaccess:
case key_userdict:
/* do nothing - loop */
goto L1;
/* We also translate some other keywords from their alternative */
/* to their "normal" form.. */
case key_NP_alternate:
token->kind2 = key_NP;
break;
case key_RD_alternate:
token->kind2 = key_RD;
break;
case key_ND_alternate:
token->kind2 = key_ND;
break;
default:
;
}
/* Dump the token when requested. This feature is only available */
/* in the 'error' and 'trace' debug levels.. */
#if defined( FT_DEBUG_LEVEL_ERROR ) || defined( FT_DEBUG_LEVEL_TRACE )
if ( parser->dump_tokens )
{
T1_String temp_string[128];
T1_Int len;
len = token->len;
if ( len > 127 ) len = 127;
strncpy( temp_string,
(T1_String*)tokzer->base + token->start,
len );
temp_string[len] = '\0';
FT_ERROR(( "%s\n", temp_string ));
}
#endif
return T1_Err_Ok;
}
static
T1_Error Expect_Keyword( T1_Parser* parser,
T1_TokenType keyword )
{
T1_Token token;
T1_Error error;
error = Next_T1_Token( parser, &token );
if (error) goto Exit;
if ( token.kind != tok_keyword ||
token.kind2 != keyword )
{
error = T1_Err_Syntax_Error;
FT_ERROR(( "T1.Parse: keyword '%s' expected.\n",
t1_keywords[ keyword - key_first_ ] ));
}
Exit:
return error;
}
static
T1_Error Expect_Keyword2( T1_Parser* parser,
T1_TokenType keyword1,
T1_TokenType keyword2 )
{
T1_Token token;
T1_Error error;
error = Next_T1_Token( parser, &token );
if (error) goto Exit;
if ( token.kind != tok_keyword ||
( token.kind2 != keyword1 &&
token.kind2 != keyword2 ) )
{
error = T1_Err_Syntax_Error;
FT_ERROR(( "T1.Parse: keyword '%s' or '%s' expected.\n",
t1_keywords[ keyword1 - key_first_ ],
t1_keywords[ keyword2 - key_first_ ] ));
}
Exit:
return error;
}
static
void Parse_Encoding( T1_Parser* parser )
{
T1_Token* token = parser->top+1;
FT_Memory memory = parser->face->root.memory;
T1_Encoding* encode = &parser->face->type1.encoding;
T1_Error error = 0;
if (token->kind == tok_keyword &&
(token->kind2 == key_StandardEncoding ||
token->kind2 == key_ExpertEncoding ) )
{
encode->num_chars = 256;
encode->code_first = 32;
encode->code_last = 255;
if ( ALLOC_ARRAY( encode->char_index, 256, T1_Short ) )
goto Exit;
encode->char_name = 0; /* no need to store glyph names */
/* Now copy the encoding */
switch (token->kind2)
{
case key_ExpertEncoding : parser->encoding_type = t1_encoding_expert;
default : parser->encoding_type = t1_encoding_standard; break;
}
}
else
{
FT_ERROR(( "T1.Parse_Encoding: invalid encoding type\n" ));
error = T1_Err_Syntax_Error;
}
Exit:
parser->error = error;
}
/**********************************************************************/
/* */
/* */
/* IMPLEMENTATION OF THE "DEF" KEYWORD DEPENDING ON */
/* CURRENT DICTIONARY STATE */
/* */
/* */
/**********************************************************************/
/**************************************************************************/
/* */
/* <Function> Do_Def_Font */
/* */
/* <Description> */
/* This function performs a 'def' when in the Font dictionary */
/* Its purpose is to build the T1_Face attributes directly from */
/* the stream.. */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_Def_Font( T1_Parser* parser )
{
T1_Token* top = parser->top;
T1_Face face = parser->face;
T1_Font* type1 = &face->type1;
switch ( top[0].kind2 )
{
case imm_FontName:
/* in some cases, the /FontName is an immediate like */
/* /TimesNewRoman. In this case, we simply copy the */
/* token string (without the /).. */
if (top[1].kind == tok_immediate)
{
FT_Memory memory = parser->tokenizer->memory;
T1_Error error;
T1_Int len = top[1].len;
if ( ALLOC( type1->font_name, len+1 ) )
{
parser->error = error;
return error;
}
MEM_Copy( type1->font_name,
parser->tokenizer->base + top[1].start,
len );
type1->font_name[len] = '\0';
}
else
type1->font_name = CopyString( parser );
break;
case imm_Encoding:
Parse_Encoding( parser );
break;
case imm_PaintType:
type1->paint_type = (T1_Byte)CopyInteger( parser );
break;
case imm_FontType:
type1->font_type = (T1_Byte)CopyInteger( parser );
break;
case imm_FontMatrix:
CopyMatrix( parser, &type1->font_matrix );
break;
case imm_FontBBox:
CopyBBox( parser, &type1->font_bbox );
break;
case imm_UniqueID:
type1->private_dict.unique_id = CopyInteger( parser );
break;
case imm_StrokeWidth:
type1->stroke_width = CopyInteger( parser );
break;
case imm_FontID:
type1->font_id = CopyInteger( parser );
break;
default:
/* ignore all other things */
parser->error = T1_Err_Ok;
}
return parser->error;
}
/**************************************************************************/
/* */
/* <Function> Do_Def_FontInfo */
/* */
/* <Description> */
/* This function performs a 'def' when in the FontInfo dictionary */
/* Its purpose is to build the T1_FontInfo structure directly from */
/* the stream.. */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_Def_FontInfo( T1_Parser* parser )
{
T1_Token* top = parser->top;
T1_FontInfo* info = &parser->face->type1.font_info;
switch ( top[0].kind2 )
{
case imm_version:
info->version = CopyString( parser );
break;
case imm_Notice:
info->notice = CopyString( parser );
break;
case imm_FullName:
info->full_name = CopyString( parser );
break;
case imm_FamilyName:
info->family_name = CopyString( parser );
break;
case imm_Weight:
info->weight = CopyString( parser );
break;
case imm_ItalicAngle:
info->italic_angle = CopyInteger( parser );
break;
case imm_isFixedPitch:
info->is_fixed_pitch = CopyBoolean( parser );
break;
case imm_UnderlinePosition:
info->underline_position = (T1_Short)CopyInteger( parser );
break;
case imm_UnderlineThickness:
info->underline_thickness = (T1_Short)CopyInteger( parser );
break;
default:
/* ignore all other things */
parser->error = T1_Err_Ok;
}
return parser->error;
}
/**************************************************************************/
/* */
/* <Function> Do_Def_Private */
/* */
/* <Description> */
/* This function performs a 'def' when in the Private dictionary */
/* Its purpose is to build the T1_Private structure directly from */
/* the stream.. */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_Def_Private( T1_Parser* parser )
{
T1_Token* top = parser->top;
T1_Private* priv = &parser->face->type1.private_dict;
switch ( top[0].kind2 )
{
case imm_RD: case imm_RD_alternate: /* Ignore the definitions */
case imm_ND: case imm_ND_alternate: /* of RD, NP, ND and their */
case imm_NP: case imm_NP_alternate: /* alternate forms ... */
parser->error = T1_Err_Ok;
break;
case imm_BlueValues:
CopyArray( parser, &priv->num_blue_values,
priv->blue_values, 14 );
break;
case imm_OtherBlues:
CopyArray( parser, &priv->num_other_blues,
priv->other_blues, 10 );
break;
case imm_FamilyBlues:
CopyArray( parser, &priv->num_family_blues,
priv->family_blues, 14 );
break;
case imm_FamilyOtherBlues:
CopyArray( parser, &priv->num_family_other_blues,
priv->family_other_blues, 10 );
break;
case imm_BlueScale:
priv->blue_scale = CopyFloat( parser, 0x10000 );
break;
case imm_BlueShift:
priv->blue_shift = CopyInteger( parser );
break;
case imm_BlueFuzz:
priv->blue_fuzz = CopyInteger( parser );
break;
case imm_StdHW:
CopyArray( parser, 0, (T1_Short*)&priv->standard_width, 1 );
break;
case imm_StdVW:
CopyArray( parser, 0, (T1_Short*)&priv->standard_height, 1 );
break;
case imm_StemSnapH:
CopyArray( parser, &priv->num_snap_widths,
priv->snap_widths, 12 );
break;
case imm_StemSnapV:
CopyArray( parser, &priv->num_snap_heights,
priv->snap_heights, 12 );
break;
case imm_ForceBold:
priv->force_bold = CopyBoolean( parser );
break;
case imm_LanguageGroup:
priv->language_group = CopyInteger( parser );
break;
case imm_password:
priv->password = CopyInteger( parser );
break;
case imm_UniqueID:
priv->unique_id = CopyInteger( parser );
break;
case imm_lenIV:
priv->lenIV = CopyInteger( parser );
break;
case imm_MinFeature:
CopyArray( parser, 0, priv->min_feature, 2 );
break;
default:
/* ignore all other things */
parser->error = T1_Err_Ok;
}
return parser->error;
}
/**************************************************************************/
/* */
/* <Function> Do_Def_Error */
/* */
/* <Description> */
/* This function returns a simple syntax error when invoked. It is */
/* ued for the "def" keyword when in the "encoding", "subrs", */
/* "othersubrs" and "charstrings" dictionary states.. */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_Def_Error( T1_Parser* parser )
{
FT_ERROR(( "T1.Load : 'def' keyword encountered in bad dictionary/array\n" ));
parser->error = T1_Err_Syntax_Error;
return parser->error;
}
static
T1_Error Do_Def_Ignore( T1_Parser* parser )
{
UNUSED(parser);
return T1_Err_Ok;
}
static
T1_Parse_Func def_funcs[ dict_max ] =
{
Do_Def_Error,
Do_Def_Font,
Do_Def_FontInfo,
Do_Def_Ignore,
Do_Def_Private,
Do_Def_Ignore,
Do_Def_Ignore,
Do_Def_Ignore,
Do_Def_Ignore,
Do_Def_Ignore,
Do_Def_Ignore,
};
/**********************************************************************/
/* */
/* */
/* IMPLEMENTATION OF THE "PUT" KEYWORD DEPENDING ON */
/* CURRENT DICTIONARY STATE */
/* */
/* */
/**********************************************************************/
/**************************************************************************/
/* */
/* <Function> Do_Put_Encoding */
/* */
/* <Description> */
/* This function performs a 'put' when in the Encoding array */
/* The glyph name is copied into the T1 recorder, and the charcode */
/* and glyph name pointer are written into the face object encoding */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_Put_Encoding( T1_Parser* parser )
{
T1_Error error = T1_Err_Ok;
T1_Face face = parser->face;
T1_Token* top = parser->top;
T1_Encoding* encode = &face->type1.encoding;
T1_Int index;
/* record and check the character code */
if ( top[0].kind != tok_number )
{
FT_TRACE4(( "T1.Parse.put: number expected\n" ));
goto Syntax_Error;
}
index = (T1_Int)CopyInteger( parser );
if (parser->error) return parser->error;
if ( index < 0 || index >= encode->num_chars )
{
FT_TRACE4(( "T1.Parse.put: invalid character code\n" ));
goto Syntax_Error;
}
/* record the immediate name */
if ( top[1].kind != tok_immediate )
{
FT_TRACE4(( "T1.Parse.put: immediate name expected\n" ));
goto Syntax_Error;
}
/* if the glyph name is '.notdef', store a NULL char name */
/* otherwise, record the glyph name.. */
if ( top[1].kind == imm_notdef )
{
parser->table.elements[ index ] = 0;
parser->table.lengths [ index ] = 0;
}
else
{
T1_String temp_name[128];
T1_Token* token = top+1;
T1_Int len = token->len-1;
/* copy immediate name */
if (len > 127) len = 127;
MEM_Copy( temp_name, parser->tokenizer->base + token->start+1, len );
temp_name[len] = '\0';
error = T1_Add_Table( &parser->table, index, (T1_Byte*)temp_name, len+1 );
/* adjust code_first and code_last */
if ( index < encode->code_first ) encode->code_first = index;
if ( index > encode->code_last ) encode->code_last = index;
}
return error;
Syntax_Error:
/* ignore the error, and simply clear the stack */
FT_TRACE4(( "T1.Put.Encoding: invalid syntax encountered\n" ));
parser->top = parser->stack;
return T1_Err_Ok;
}
/**********************************************************************/
/* */
/* */
/* IMPLEMENTATION OF THE "RD" KEYWORD DEPENDING ON */
/* CURRENT DICTIONARY STATE */
/* */
/* */
/**********************************************************************/
/**************************************************************************/
/* */
/* <Function> Do_RD_Subrs */
/* */
/* <Description> */
/* This function performs a 'RD' when in the Subrs dictionary */
/* It simply records the array of bytecodes/charstrings corresponding */
/* to the sub-routine.. */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_RD_Subrs( T1_Parser* parser )
{
T1_Error error = T1_Err_Ok;
T1_Face face = parser->face;
T1_Token* top = parser->top;
T1_Tokenizer tokzer = parser->tokenizer;
T1_Int index, count;
/* record and check the character code */
if ( top[0].kind != tok_number ||
top[1].kind != tok_number )
{
FT_ERROR(( "T1.Parse.put: number expected\n" ));
goto Syntax_Error;
}
index = (T1_Int)CopyInteger( parser );
error = parser->error; if (error) goto Exit;
count = (T1_Int)CopyInteger( parser );
error = parser->error; if (error) goto Exit;
if ( index < 0 || index >= face->type1.num_subrs )
{
FT_ERROR(( "T1.Parse.put: invalid character code\n" ));
goto Syntax_Error;
}
/* decrypt charstring and skip them */
{
T1_Byte* base = tokzer->base + tokzer->cursor;
t1_decrypt( base, count, 4330 );
tokzer->cursor += count;
base += face->type1.private_dict.lenIV;
count -= face->type1.private_dict.lenIV;
error = T1_Add_Table( &parser->table, index, base, count );
}
/* consume the closing NP or 'put' */
error = Expect_Keyword2( parser, key_NP, key_put );
Exit:
return error;
Syntax_Error:
return T1_Err_Syntax_Error;
}
/**************************************************************************/
/* */
/* <Function> Do_RD_CharStrings */
/* */
/* <Description> */
/* This function performs a 'RD' when in the CharStrings dictionary */
/* It simply records the array of bytecodes/charstrings corresponding */
/* to the glyph program string. */
/* */
/* <Input> */
/* parser :: handle to current parser. */
/* */
/* <Return> */
/* Error code. 0 means success */
/* */
static
T1_Error Do_RD_Charstrings( T1_Parser* parser )
{
T1_Error error = T1_Err_Ok;
T1_Face face = parser->face;
T1_Token* top = parser->top;
T1_Tokenizer tokzer = parser->tokenizer;
T1_Int index, count;
/* check the character name argument */
if ( top[0].kind != tok_immediate )
{
FT_ERROR(( "T1.Parse.RD: immediate character name expected\n" ));
goto Syntax_Error;
}
/* check the count argument */
if ( top[1].kind != tok_number )
{
FT_ERROR(( "T1.Parse.put: number expected\n" ));
goto Syntax_Error;
}
parser->args++;
count = (T1_Int)CopyInteger( parser );
error = parser->error; if (error) goto Exit;
/* record the glyph name and get the corresponding glyph index */
if ( top[0].kind2 == imm_notdef )
index = 0;
else
{
T1_String temp_name[128];
T1_Token* token = top;
T1_Int len = token->len-1;
/* copy immediate name */
if (len > 127) len = 127;
MEM_Copy( temp_name, parser->tokenizer->base + token->start+1, len );
temp_name[len] = '\0';
index = parser->cur_name++;
error = T1_Add_Table( &parser->table, index*2, (T1_Byte*)temp_name, len+1 );
if (error) goto Exit;
}
/* decrypt and record charstring, then skip them */
{
T1_Byte* base = tokzer->base + tokzer->cursor;
t1_decrypt( base, count, 4330 );
tokzer->cursor += count; /* skip */
base += face->type1.private_dict.lenIV;
count -= face->type1.private_dict.lenIV;
error = T1_Add_Table( &parser->table, index*2+1, base, count );
}
/* consume the closing ND */
if (!error)
error = Expect_Keyword( parser, key_ND );
Exit:
return error;
Syntax_Error:
return T1_Err_Syntax_Error;
}
static
T1_Error Expect_Dict_Arguments( T1_Parser* parser,
T1_Int num_args,
T1_TokenType immediate,
T1_DictState new_state,
T1_Int *count )
{
/* check that we have enough arguments in the stack, including */
/* the 'dict' keyword.. */
if ( parser->top - parser->stack < num_args )
{
FT_ERROR(( "T1.Parse.Dict : expecting at least %d arguments",
num_args ));
goto Syntax_Error;
}
/* check that we have the correct immediate, if needed */
if ( num_args == 2 )
{
if ( parser->top[-2].kind != tok_immediate ||
parser->top[-2].kind2 != immediate )
{
FT_ERROR(( "T1.Parse.Dict : expecting '/%s' dictionary\n",
t1_immediates[ immediate - imm_first_ ] ));
goto Syntax_Error;
}
}
parser->args = parser->top-1;
/* check that the count argument is a number */
if ( parser->args->kind != tok_number )
{
FT_ERROR(( "T1.Parse.Dict : expecting numerical count argument for 'dict'\n" ));
goto Syntax_Error;
}
if (count)
{
*count = CopyInteger( parser );
if (parser->error) return parser->error;
}
/* save the dictionary state */
parser->state_stack[ ++parser->state_index ] = new_state;
/* consume the 'begin' keyword, and clear the stack */
parser->top -= num_args;
return Expect_Keyword( parser, key_begin );
Syntax_Error:
return T1_Err_Syntax_Error;
}
static
T1_Error Expect_Array_Arguments( T1_Parser* parser )
{
T1_Token* top = parser->top;
T1_Error error = T1_Err_Ok;
T1_DictState new_state;
T1_Int count;
T1_Face face = parser->face;
FT_Memory memory = face->root.memory;
/* Check arguments format */
if ( top - parser->stack < 2 )
{
FT_ERROR(( "T1.Parse.array: two arguments expected\n" ));
error = T1_Err_Stack_Underflow;
goto Exit;
}
parser->top -= 2;
top -= 2;
parser->args = top + 1;
if ( top[0].kind != tok_immediate )
{
FT_ERROR(( "T1.Parse.array: first argument must be an immediate name\n" ));
goto Syntax_Error;
}
if ( top[1].kind != tok_number )
{
FT_ERROR(( "T1.Parse.array: second argument must be a number\n" ));
goto Syntax_Error;
}
count = (T1_Int)CopyInteger( parser );
/* Is this an array we know about ?? */
switch ( top[0].kind2 )
{
case imm_Encoding:
{
T1_Encoding* encode = &face->type1.encoding;
new_state = dict_encoding;
encode->code_first = count;
encode->code_last = 0;
encode->num_chars = count;
/* allocate the table of character indexes. The table of */
/* character names is allocated through init_t1_recorder */
if ( ALLOC_ARRAY( encode->char_index, count, T1_Short ) )
return error;
error = T1_New_Table( &parser->table, count, memory );
if (error) goto Exit;
parser->encoding_type = t1_encoding_array;
}
break;
case imm_Subrs:
{
new_state = dict_subrs;
face->type1.num_subrs = count;
error = T1_New_Table( &parser->table, count, memory );
if (error) goto Exit;
}
break;
case imm_CharStrings:
new_state = dict_charstrings;
break;
default:
new_state = dict_unknown_array;
}
parser->state_stack[ ++parser->state_index ] = new_state;
Exit:
return error;
Syntax_Error:
return T1_Err_Syntax_Error;
}
static
T1_Error Finalise_Parsing( T1_Parser* parser )
{
T1_Face face = parser->face;
T1_Font* type1 = &face->type1;
FT_Memory memory = face->root.memory;
T1_Table* strings = &parser->table;
PSNames_Interface* psnames = (PSNames_Interface*)face->psnames;
T1_Int num_glyphs;
T1_Int n;
T1_Error error;
num_glyphs = type1->num_glyphs = parser->cur_name;
/* allocate glyph names and charstrings arrays */
if ( ALLOC_ARRAY( type1->glyph_names , num_glyphs, T1_String* ) ||
ALLOC_ARRAY( type1->charstrings , num_glyphs, T1_Byte* ) ||
ALLOC_ARRAY( type1->charstrings_len, num_glyphs, T1_Int* ) )
return error;
/* copy glyph names and charstrings offsets and lengths */
type1->charstrings_block = strings->block;
for ( n = 0; n < num_glyphs; n++ )
{
type1->glyph_names[n] = (T1_String*)strings->elements[2*n];
type1->charstrings[n] = strings->elements[2*n+1];
type1->charstrings_len[n] = strings->lengths [2*n+1];
}
/* now free the old tables */
FREE( strings->elements );
FREE( strings->lengths );
if (!psnames)
{
FT_ERROR(( "T1.Parse.Finalise : PSNames module missing !!\n" ));
return T1_Err_Unimplemented_Feature;
}
/* Compute encoding if required. */
if (parser->encoding_type == t1_encoding_none)
{
FT_ERROR(( "T1.Parse.Finalise : no encoding specified in font file\n" ));
return T1_Err_Syntax_Error;
}
{
T1_Int n;
T1_Encoding* encode = &type1->encoding;
encode->code_first = encode->num_chars-1;
encode->code_last = 0;
for ( n = 0; n < encode->num_chars; n++ )
{
T1_String** names;
T1_Int index;
T1_Int m;
switch (parser->encoding_type)
{
case t1_encoding_standard:
index = psnames->adobe_std_encoding[n];
names = 0;
break;
case t1_encoding_expert:
index = psnames->adobe_expert_encoding[n];
names = 0;
break;
default:
index = n;
names = (T1_String**)parser->encoding_offsets;
}
encode->char_index[n] = 0;
if (index)
{
T1_String* name;
if (names)
name = names[index];
else
name = (T1_String*)psnames->adobe_std_strings(index);
if ( name )
{
T1_Int len = strlen(name);
/* lookup glyph index from name */
for ( m = 0; m < num_glyphs; m++ )
{
if ( strncmp( type1->glyph_names[m], name, len ) == 0 )
{
encode->char_index[n] = m;
break;
}
}
if ( n < encode->code_first ) encode->code_first = n;
if ( n > encode->code_last ) encode->code_last = n;
}
}
}
parser->encoding_type = t1_encoding_none;
FREE( parser->encoding_names );
FREE( parser->encoding_lengths );
FREE( parser->encoding_offsets );
}
return T1_Err_Ok;
}
LOCAL_FUNC
T1_Error Parse_T1_FontProgram( T1_Parser* parser )
{
T1_Error error;
T1_Font* type1 = &parser->face->type1;
for (;;)
{
T1_Token token;
T1_Token* top;
T1_DictState dict_state;
T1_Int dict_index;
error = Next_T1_Token( parser, &token );
top = parser->top;
dict_index = parser->state_index;
dict_state = parser->state_stack[ dict_index ];
switch ( token.kind )
{
/* A keyword was detected */
case tok_keyword:
switch (token.kind2)
{
case key_dict:
switch (dict_state)
{
case dict_none:
/* All right, we're beggining the font dictionary */
/* check that we only have one number argument, then */
/* consume the 'begin' and change to 'dict_font' */
/* state.. */
error = Expect_Dict_Arguments( parser, 1, tok_error,
dict_font, 0 );
if (error) goto Exit;
/* clear stack from all the previous content. This */
/* could be some stupid Postscript code ... */
parser->top = parser->stack;
break;
case dict_font:
/* This must be the /FontInfo dictionary, so check */
/* That we have at least two arguments, that they */
/* are "/FontInfo" and a number, then change the */
/* dictionary state.. */
error = Expect_Dict_Arguments( parser, 2, imm_FontInfo,
dict_fontinfo, 0 );
if (error) goto Exit;
break;
case dict_none2:
error = Expect_Dict_Arguments( parser, 2, imm_Private,
dict_private, 0 );
if (error) goto Exit;
break;
case dict_private:
{
T1_Face face = parser->face;
T1_Int count;
error = Expect_Dict_Arguments( parser, 2, imm_CharStrings,
dict_charstrings, &count );
if (error) goto Exit;
type1->num_glyphs = count;
error = T1_New_Table( &parser->table, count*2, face->root.memory );
if (error) goto Exit;
/* record '.notdef' as the first glyph in the font */
error = T1_Add_Table( &parser->table, 0, (T1_Byte*)".notdef", 8 );
parser->cur_name = 1;
/* XXXXX : DO SOMETHING HERE */
}
break;
default:
/* All other uses are invalid */
FT_ERROR(( "T1.Parse: invalid use of the 'dict' keyword\n" ));
goto Syntax_Error;
}
break;
case key_array:
/* Are we in an array yet ? Is so, raise an error */
switch (dict_state)
{
case dict_encoding: case dict_subrs:
case dict_othersubrs: case dict_charstrings:
case dict_unknown_array:
FT_ERROR(( "T1.Parse.array: nested array definitions\n" ));
goto Syntax_Error;
default:
;
}
error = Expect_Array_Arguments( parser );
if (error) goto Exit;
break;
case key_ND:
case key_NP:
case key_def:
/* Are we in an array ? If so, finalise it.. */
switch ( dict_state )
{
case dict_encoding: /* finish encoding array */
{
/* copy table names to the face object */
T1_Done_Table( &parser->table );
parser->encoding_names = parser->table.block;
parser->encoding_lengths = parser->table.lengths;
parser->encoding_offsets = parser->table.elements;
parser->state_index--;
}
break;
case dict_subrs:
{
/* copy recorder sub-routines */
T1_Done_Table( &parser->table );
parser->subrs = parser->table.block;
type1->subrs = parser->table.elements;
type1->subrs_len = parser->table.lengths;
parser->state_index--;
}
break;
case dict_charstrings:
case dict_othersubrs:
case dict_unknown_array:
FT_ERROR(( "T1.Parser.def: unsupported array\n" ));
goto Syntax_Error;
break;
default: /* normal 'def' processing */
{
/* Check that we have sufficient operands in the stack */
if ( top >= parser->stack+2 )
{
/* Now check that the first operand is an immediate */
/* If so, call the appropriate "def" routine based */
/* on the current parser state.. */
if ( top[-2].kind == tok_immediate )
{
parser->top -= 2;
parser->args = parser->top + 1;
error = def_funcs[dict_state](parser);
}
else
{
/* This is an error, but some fonts contain some */
/* stupid Postscript code. We simply ignore */
/* an invalid 'def' by clearing the stack */
#if 0
FT_ERROR(( "T1.Parse.def: immediate expected\n" ));
goto Syntax_Error;
#else
parser->top = parser->stack;
#endif
}
}
else
{
FT_ERROR(( "T1.Parse.def: not enough arguments\n" ));
goto Stack_Underflow;
}
}
}
break;
case key_index:
if ( top <= parser->stack )
{
FT_ERROR(( "T1.Parse.index: not enough arguments\n" ));
goto Stack_Underflow;
}
/* simply ignore ?? */
parser->top --;
break;
case key_put:
/* Check that we have sufficient operands in stack */
if ( top < parser->stack+2 )
{
FT_ERROR(( "T1.Parse.put: not enough arguments\n" ));
goto Stack_Underflow;
}
parser->top -= 2;
parser->args = parser->top;
switch (dict_state)
{
case dict_encoding:
error = Do_Put_Encoding( parser );
if (error) goto Exit;
break;
case dict_unknown_array: /* ignore the put */
break;
default:
#if 0
FT_ERROR(( "T1.Parse.put: invalid context\n" ));
goto Syntax_Error;
#else
/* invalid context, simply ignore the put and */
/* clear the stack (stupid Postscript code..) */
FT_TRACE4(( "T1.Parse.put: invalid context. ignored.\n" ));
parser->top = parser->stack;
#endif
}
break;
case key_RD:
/* Check that we have sufficient operands in stack */
if ( top < parser->stack+2 )
{
FT_ERROR(( "T1.Parse.RD: not enough arguments\n" ));
goto Stack_Underflow;
}
parser->top -= 2;
parser->args = parser->top;
switch (dict_state)
{
case dict_subrs:
error = Do_RD_Subrs( parser );
if (error) goto Exit;
break;
case dict_charstrings:
error = Do_RD_Charstrings( parser );
if (error) goto Exit;
break;
default:
FT_ERROR(( "T1.Parse.RD: invalid context\n" ));
goto Syntax_Error;
}
break;
case key_end:
/* Were we in a dictionary or in an array ? */
if ( dict_index <= 0 )
{
FT_ERROR(( "T1.Parse.end: no dictionary defined\n" ));
goto Syntax_Error;
}
switch (dict_state)
{
/* Jump to the private dictionary if we're closing the */
/* /Font dictionary.. */
case dict_font:
goto Open_Private;
/* Exit the parser when closing the CharStrings dictionary */
case dict_charstrings:
return Finalise_Parsing( parser );
default:
/* Pop the current dictionary state and return to previous */
/* one. Consume the "def".. */
/* Because some buggy fonts (BitStream) have incorrect */
/* syntax, we never escape from the private dictionary */
if (dict_state != dict_private)
parser->state_index--;
/* many fonts use a NP instead of def or put, so */
/* we simply ignore the nest token.. */
#if 0
error = Expect_Keyword2( parser, key_def, key_put );
if (error) goto Exit;
#else
(void)Expect_Keyword2( parser, key_def, key_put );
#endif
}
break;
case key_for:
/* check that we have four arguments, and simply */
/* ignore them.. */
if ( top - parser->stack < 4 )
{
FT_ERROR(( "T1.Parse.for: not enough arguments\n" ));
goto Stack_Underflow;
}
parser->top -= 4;
break;
case key_currentdict:
Open_Private:
parser->state_index = 0;
parser->state_stack[0] = dict_none2;
error = Open_PrivateDict( parser->tokenizer );
if (error) goto Exit;
break;
case key_true:
case key_false:
case key_StandardEncoding:
case key_ExpertEncoding:
goto Push_Element;
default:
FT_ERROR(( "T1.Parser: invalid keyword in context\n" ));
error = T1_Err_Syntax_Error;
}
break;
/* check for the presence of /BlendAxisTypes, we cannot deal with */
/* multiple master fonts, so we must return a correct error code */
/* to allow another driver to load them.. */
case tok_immediate:
if (token.kind2 == imm_BlendAxisTypes)
{
error = FT_Err_Unknown_File_Format;
goto Exit;
}
/* fallthrough */
/* A number was detected */
case tok_string:
case tok_program:
case tok_array:
case tok_hexarray:
case tok_any:
case tok_number: /* push number on stack */
Push_Element:
if ( top >= parser->limit )
{
error = T1_Err_Stack_Overflow;
goto Exit;
}
else
*parser->top++ = token;
break;
/* anything else is an error per se the spec, but we */
/* frequently encountre stupid postscript code in fonts, */
/* so just ignore them.. */
default:
error = T1_Err_Ok; /* ignore token */
}
if (error)
return error;
}
Exit:
return error;
Syntax_Error:
return T1_Err_Syntax_Error;
Stack_Underflow:
return T1_Err_Stack_Underflow;
}