Hash :
1937b557
Author :
Date :
2018-08-14T07:54:25
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
/****************************************************************************
*
* cffparse.c
*
* CFF token stream parser (body)
*
* Copyright 1996-2018 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
#include <ft2build.h>
#include "cffparse.h"
#include FT_INTERNAL_STREAM_H
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_CALC_H
#include FT_INTERNAL_POSTSCRIPT_AUX_H
#include "cfferrs.h"
#include "cffload.h"
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT trace_cffparse
FT_LOCAL_DEF( FT_Error )
cff_parser_init( CFF_Parser parser,
FT_UInt code,
void* object,
FT_Library library,
FT_UInt stackSize,
FT_UShort num_designs,
FT_UShort num_axes )
{
FT_Memory memory = library->memory; /* for FT_NEW_ARRAY */
FT_Error error; /* for FT_NEW_ARRAY */
FT_ZERO( parser );
#if 0
parser->top = parser->stack;
#endif
parser->object_code = code;
parser->object = object;
parser->library = library;
parser->num_designs = num_designs;
parser->num_axes = num_axes;
/* allocate the stack buffer */
if ( FT_NEW_ARRAY( parser->stack, stackSize ) )
{
FT_FREE( parser->stack );
goto Exit;
}
parser->stackSize = stackSize;
parser->top = parser->stack; /* empty stack */
Exit:
return error;
}
FT_LOCAL_DEF( void )
cff_parser_done( CFF_Parser parser )
{
FT_Memory memory = parser->library->memory; /* for FT_FREE */
FT_FREE( parser->stack );
}
/* read an integer */
static FT_Long
cff_parse_integer( FT_Byte* start,
FT_Byte* limit )
{
FT_Byte* p = start;
FT_Int v = *p++;
FT_Long val = 0;
if ( v == 28 )
{
if ( p + 2 > limit )
goto Bad;
val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) | p[1] );
}
else if ( v == 29 )
{
if ( p + 4 > limit )
goto Bad;
val = (FT_Long)( ( (FT_ULong)p[0] << 24 ) |
( (FT_ULong)p[1] << 16 ) |
( (FT_ULong)p[2] << 8 ) |
(FT_ULong)p[3] );
}
else if ( v < 247 )
{
val = v - 139;
}
else if ( v < 251 )
{
if ( p + 1 > limit )
goto Bad;
val = ( v - 247 ) * 256 + p[0] + 108;
}
else
{
if ( p + 1 > limit )
goto Bad;
val = -( v - 251 ) * 256 - p[0] - 108;
}
Exit:
return val;
Bad:
val = 0;
FT_TRACE4(( "!!!END OF DATA:!!!" ));
goto Exit;
}
static const FT_Long power_tens[] =
{
1L,
10L,
100L,
1000L,
10000L,
100000L,
1000000L,
10000000L,
100000000L,
1000000000L
};
/* maximum values allowed for multiplying */
/* with the corresponding `power_tens' element */
static const FT_Long power_ten_limits[] =
{
FT_LONG_MAX / 1L,
FT_LONG_MAX / 10L,
FT_LONG_MAX / 100L,
FT_LONG_MAX / 1000L,
FT_LONG_MAX / 10000L,
FT_LONG_MAX / 100000L,
FT_LONG_MAX / 1000000L,
FT_LONG_MAX / 10000000L,
FT_LONG_MAX / 100000000L,
FT_LONG_MAX / 1000000000L,
};
/* read a real */
static FT_Fixed
cff_parse_real( FT_Byte* start,
FT_Byte* limit,
FT_Long power_ten,
FT_Long* scaling )
{
FT_Byte* p = start;
FT_Int nib;
FT_UInt phase;
FT_Long result, number, exponent;
FT_Int sign = 0, exponent_sign = 0, have_overflow = 0;
FT_Long exponent_add, integer_length, fraction_length;
if ( scaling )
*scaling = 0;
result = 0;
number = 0;
exponent = 0;
exponent_add = 0;
integer_length = 0;
fraction_length = 0;
/* First of all, read the integer part. */
phase = 4;
for (;;)
{
/* If we entered this iteration with phase == 4, we need to */
/* read a new byte. This also skips past the initial 0x1E. */
if ( phase )
{
p++;
/* Make sure we don't read past the end. */
if ( p >= limit )
goto Bad;
}
/* Get the nibble. */
nib = (FT_Int)( p[0] >> phase ) & 0xF;
phase = 4 - phase;
if ( nib == 0xE )
sign = 1;
else if ( nib > 9 )
break;
else
{
/* Increase exponent if we can't add the digit. */
if ( number >= 0xCCCCCCCL )
exponent_add++;
/* Skip leading zeros. */
else if ( nib || number )
{
integer_length++;
number = number * 10 + nib;
}
}
}
/* Read fraction part, if any. */
if ( nib == 0xA )
for (;;)
{
/* If we entered this iteration with phase == 4, we need */
/* to read a new byte. */
if ( phase )
{
p++;
/* Make sure we don't read past the end. */
if ( p >= limit )
goto Bad;
}
/* Get the nibble. */
nib = ( p[0] >> phase ) & 0xF;
phase = 4 - phase;
if ( nib >= 10 )
break;
/* Skip leading zeros if possible. */
if ( !nib && !number )
exponent_add--;
/* Only add digit if we don't overflow. */
else if ( number < 0xCCCCCCCL && fraction_length < 9 )
{
fraction_length++;
number = number * 10 + nib;
}
}
/* Read exponent, if any. */
if ( nib == 12 )
{
exponent_sign = 1;
nib = 11;
}
if ( nib == 11 )
{
for (;;)
{
/* If we entered this iteration with phase == 4, */
/* we need to read a new byte. */
if ( phase )
{
p++;
/* Make sure we don't read past the end. */
if ( p >= limit )
goto Bad;
}
/* Get the nibble. */
nib = ( p[0] >> phase ) & 0xF;
phase = 4 - phase;
if ( nib >= 10 )
break;
/* Arbitrarily limit exponent. */
if ( exponent > 1000 )
have_overflow = 1;
else
exponent = exponent * 10 + nib;
}
if ( exponent_sign )
exponent = -exponent;
}
if ( !number )
goto Exit;
if ( have_overflow )
{
if ( exponent_sign )
goto Underflow;
else
goto Overflow;
}
/* We don't check `power_ten' and `exponent_add'. */
exponent += power_ten + exponent_add;
if ( scaling )
{
/* Only use `fraction_length'. */
fraction_length += integer_length;
exponent += integer_length;
if ( fraction_length <= 5 )
{
if ( number > 0x7FFFL )
{
result = FT_DivFix( number, 10 );
*scaling = exponent - fraction_length + 1;
}
else
{
if ( exponent > 0 )
{
FT_Long new_fraction_length, shift;
/* Make `scaling' as small as possible. */
new_fraction_length = FT_MIN( exponent, 5 );
shift = new_fraction_length - fraction_length;
if ( shift > 0 )
{
exponent -= new_fraction_length;
number *= power_tens[shift];
if ( number > 0x7FFFL )
{
number /= 10;
exponent += 1;
}
}
else
exponent -= fraction_length;
}
else
exponent -= fraction_length;
result = (FT_Long)( (FT_ULong)number << 16 );
*scaling = exponent;
}
}
else
{
if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
{
result = FT_DivFix( number, power_tens[fraction_length - 4] );
*scaling = exponent - 4;
}
else
{
result = FT_DivFix( number, power_tens[fraction_length - 5] );
*scaling = exponent - 5;
}
}
}
else
{
integer_length += exponent;
fraction_length -= exponent;
if ( integer_length > 5 )
goto Overflow;
if ( integer_length < -5 )
goto Underflow;
/* Remove non-significant digits. */
if ( integer_length < 0 )
{
number /= power_tens[-integer_length];
fraction_length += integer_length;
}
/* this can only happen if exponent was non-zero */
if ( fraction_length == 10 )
{
number /= 10;
fraction_length -= 1;
}
/* Convert into 16.16 format. */
if ( fraction_length > 0 )
{
if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
goto Exit;
result = FT_DivFix( number, power_tens[fraction_length] );
}
else
{
number *= power_tens[-fraction_length];
if ( number > 0x7FFFL )
goto Overflow;
result = (FT_Long)( (FT_ULong)number << 16 );
}
}
Exit:
if ( sign )
result = -result;
return result;
Overflow:
result = 0x7FFFFFFFL;
FT_TRACE4(( "!!!OVERFLOW:!!!" ));
goto Exit;
Underflow:
result = 0;
FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
goto Exit;
Bad:
result = 0;
FT_TRACE4(( "!!!END OF DATA:!!!" ));
goto Exit;
}
/* read a number, either integer or real */
FT_LOCAL_DEF( FT_Long )
cff_parse_num( CFF_Parser parser,
FT_Byte** d )
{
if ( **d == 30 )
{
/* binary-coded decimal is truncated to integer */
return cff_parse_real( *d, parser->limit, 0, NULL ) >> 16;
}
else if ( **d == 255 )
{
/* 16.16 fixed point is used internally for CFF2 blend results. */
/* Since these are trusted values, a limit check is not needed. */
/* After the 255, 4 bytes give the number. */
/* The blend value is converted to integer, with rounding; */
/* due to the right-shift we don't need the lowest byte. */
#if 0
return (FT_Short)(
( ( ( (FT_UInt32)*( d[0] + 1 ) << 24 ) |
( (FT_UInt32)*( d[0] + 2 ) << 16 ) |
( (FT_UInt32)*( d[0] + 3 ) << 8 ) |
(FT_UInt32)*( d[0] + 4 ) ) + 0x8000U ) >> 16 );
#else
return (FT_Short)(
( ( ( (FT_UInt32)*( d[0] + 1 ) << 16 ) |
( (FT_UInt32)*( d[0] + 2 ) << 8 ) |
(FT_UInt32)*( d[0] + 3 ) ) + 0x80U ) >> 8 );
#endif
}
else
return cff_parse_integer( *d, parser->limit );
}
/* read a floating point number, either integer or real */
static FT_Fixed
do_fixed( CFF_Parser parser,
FT_Byte** d,
FT_Long scaling )
{
if ( **d == 30 )
return cff_parse_real( *d, parser->limit, scaling, NULL );
else
{
FT_Long val = cff_parse_integer( *d, parser->limit );
if ( scaling )
{
if ( FT_ABS( val ) > power_ten_limits[scaling] )
{
val = val > 0 ? 0x7FFFFFFFL : -0x7FFFFFFFL;
goto Overflow;
}
val *= power_tens[scaling];
}
if ( val > 0x7FFF )
{
val = 0x7FFFFFFFL;
goto Overflow;
}
else if ( val < -0x7FFF )
{
val = -0x7FFFFFFFL;
goto Overflow;
}
return (FT_Long)( (FT_ULong)val << 16 );
Overflow:
FT_TRACE4(( "!!!OVERFLOW:!!!" ));
return val;
}
}
/* read a floating point number, either integer or real */
static FT_Fixed
cff_parse_fixed( CFF_Parser parser,
FT_Byte** d )
{
return do_fixed( parser, d, 0 );
}
/* read a floating point number, either integer or real, */
/* but return `10^scaling' times the number read in */
static FT_Fixed
cff_parse_fixed_scaled( CFF_Parser parser,
FT_Byte** d,
FT_Long scaling )
{
return do_fixed( parser, d, scaling );
}
/* read a floating point number, either integer or real, */
/* and return it as precise as possible -- `scaling' returns */
/* the scaling factor (as a power of 10) */
static FT_Fixed
cff_parse_fixed_dynamic( CFF_Parser parser,
FT_Byte** d,
FT_Long* scaling )
{
FT_ASSERT( scaling );
if ( **d == 30 )
return cff_parse_real( *d, parser->limit, 0, scaling );
else
{
FT_Long number;
FT_Int integer_length;
number = cff_parse_integer( d[0], d[1] );
if ( number > 0x7FFFL )
{
for ( integer_length = 5; integer_length < 10; integer_length++ )
if ( number < power_tens[integer_length] )
break;
if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
{
*scaling = integer_length - 4;
return FT_DivFix( number, power_tens[integer_length - 4] );
}
else
{
*scaling = integer_length - 5;
return FT_DivFix( number, power_tens[integer_length - 5] );
}
}
else
{
*scaling = 0;
return (FT_Long)( (FT_ULong)number << 16 );
}
}
}
static FT_Error
cff_parse_font_matrix( CFF_Parser parser )
{
CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
FT_Matrix* matrix = &dict->font_matrix;
FT_Vector* offset = &dict->font_offset;
FT_ULong* upm = &dict->units_per_em;
FT_Byte** data = parser->stack;
if ( parser->top >= parser->stack + 6 )
{
FT_Fixed values[6];
FT_Long scalings[6];
FT_Long min_scaling, max_scaling;
int i;
dict->has_font_matrix = TRUE;
/* We expect a well-formed font matrix, this is, the matrix elements */
/* `xx' and `yy' are of approximately the same magnitude. To avoid */
/* loss of precision, we use the magnitude of the largest matrix */
/* element to scale all other elements. The scaling factor is then */
/* contained in the `units_per_em' value. */
max_scaling = FT_LONG_MIN;
min_scaling = FT_LONG_MAX;
for ( i = 0; i < 6; i++ )
{
values[i] = cff_parse_fixed_dynamic( parser, data++, &scalings[i] );
if ( values[i] )
{
if ( scalings[i] > max_scaling )
max_scaling = scalings[i];
if ( scalings[i] < min_scaling )
min_scaling = scalings[i];
}
}
if ( max_scaling < -9 ||
max_scaling > 0 ||
( max_scaling - min_scaling ) < 0 ||
( max_scaling - min_scaling ) > 9 )
{
FT_TRACE1(( "cff_parse_font_matrix:"
" strange scaling values (minimum %d, maximum %d),\n"
" "
" using default matrix\n", min_scaling, max_scaling ));
goto Unlikely;
}
for ( i = 0; i < 6; i++ )
{
FT_Fixed value = values[i];
FT_Long divisor, half_divisor;
if ( !value )
continue;
divisor = power_tens[max_scaling - scalings[i]];
half_divisor = divisor >> 1;
if ( value < 0 )
{
if ( FT_LONG_MIN + half_divisor < value )
values[i] = ( value - half_divisor ) / divisor;
else
values[i] = FT_LONG_MIN / divisor;
}
else
{
if ( FT_LONG_MAX - half_divisor > value )
values[i] = ( value + half_divisor ) / divisor;
else
values[i] = FT_LONG_MAX / divisor;
}
}
matrix->xx = values[0];
matrix->yx = values[1];
matrix->xy = values[2];
matrix->yy = values[3];
offset->x = values[4];
offset->y = values[5];
*upm = (FT_ULong)power_tens[-max_scaling];
FT_TRACE4(( " [%f %f %f %f %f %f]\n",
(double)matrix->xx / *upm / 65536,
(double)matrix->xy / *upm / 65536,
(double)matrix->yx / *upm / 65536,
(double)matrix->yy / *upm / 65536,
(double)offset->x / *upm / 65536,
(double)offset->y / *upm / 65536 ));
if ( !FT_Matrix_Check( matrix ) )
{
FT_TRACE1(( "cff_parse_font_matrix:"
" degenerate values, using default matrix\n" ));
goto Unlikely;
}
return FT_Err_Ok;
}
else
return FT_THROW( Stack_Underflow );
Unlikely:
/* Return default matrix in case of unlikely values. */
matrix->xx = 0x10000L;
matrix->yx = 0;
matrix->xy = 0;
matrix->yy = 0x10000L;
offset->x = 0;
offset->y = 0;
*upm = 1;
return FT_Err_Ok;
}
static FT_Error
cff_parse_font_bbox( CFF_Parser parser )
{
CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
FT_BBox* bbox = &dict->font_bbox;
FT_Byte** data = parser->stack;
FT_Error error;
error = FT_ERR( Stack_Underflow );
if ( parser->top >= parser->stack + 4 )
{
bbox->xMin = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
bbox->yMin = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
bbox->xMax = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
bbox->yMax = FT_RoundFix( cff_parse_fixed( parser, data ) );
error = FT_Err_Ok;
FT_TRACE4(( " [%d %d %d %d]\n",
bbox->xMin / 65536,
bbox->yMin / 65536,
bbox->xMax / 65536,
bbox->yMax / 65536 ));
}
return error;
}
static FT_Error
cff_parse_private_dict( CFF_Parser parser )
{
CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
FT_Byte** data = parser->stack;
FT_Error error;
error = FT_ERR( Stack_Underflow );
if ( parser->top >= parser->stack + 2 )
{
FT_Long tmp;
tmp = cff_parse_num( parser, data++ );
if ( tmp < 0 )
{
FT_ERROR(( "cff_parse_private_dict: Invalid dictionary size\n" ));
error = FT_THROW( Invalid_File_Format );
goto Fail;
}
dict->private_size = (FT_ULong)tmp;
tmp = cff_parse_num( parser, data );
if ( tmp < 0 )
{
FT_ERROR(( "cff_parse_private_dict: Invalid dictionary offset\n" ));
error = FT_THROW( Invalid_File_Format );
goto Fail;
}
dict->private_offset = (FT_ULong)tmp;
FT_TRACE4(( " %lu %lu\n",
dict->private_size, dict->private_offset ));
error = FT_Err_Ok;
}
Fail:
return error;
}
/* The `MultipleMaster' operator comes before any */
/* top DICT operators that contain T2 charstrings. */
static FT_Error
cff_parse_multiple_master( CFF_Parser parser )
{
CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
FT_Error error;
#ifdef FT_DEBUG_LEVEL_TRACE
/* beautify tracing message */
if ( ft_trace_levels[FT_COMPONENT] < 4 )
FT_TRACE1(( "Multiple Master CFFs not supported yet,"
" handling first master design only\n" ));
else
FT_TRACE1(( " (not supported yet,"
" handling first master design only)\n" ));
#endif
error = FT_ERR( Stack_Underflow );
/* currently, we handle only the first argument */
if ( parser->top >= parser->stack + 5 )
{
FT_Long num_designs = cff_parse_num( parser, parser->stack );
if ( num_designs > 16 || num_designs < 2 )
{
FT_ERROR(( "cff_parse_multiple_master:"
" Invalid number of designs\n" ));
error = FT_THROW( Invalid_File_Format );
}
else
{
dict->num_designs = (FT_UShort)num_designs;
dict->num_axes = (FT_UShort)( parser->top - parser->stack - 4 );
parser->num_designs = dict->num_designs;
parser->num_axes = dict->num_axes;
error = FT_Err_Ok;
}
}
return error;
}
static FT_Error
cff_parse_cid_ros( CFF_Parser parser )
{
CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
FT_Byte** data = parser->stack;
FT_Error error;
error = FT_ERR( Stack_Underflow );
if ( parser->top >= parser->stack + 3 )
{
dict->cid_registry = (FT_UInt)cff_parse_num( parser, data++ );
dict->cid_ordering = (FT_UInt)cff_parse_num( parser, data++ );
if ( **data == 30 )
FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
dict->cid_supplement = cff_parse_num( parser, data );
if ( dict->cid_supplement < 0 )
FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
dict->cid_supplement ));
error = FT_Err_Ok;
FT_TRACE4(( " %d %d %d\n",
dict->cid_registry,
dict->cid_ordering,
dict->cid_supplement ));
}
return error;
}
static FT_Error
cff_parse_vsindex( CFF_Parser parser )
{
/* vsindex operator can only be used in a Private DICT */
CFF_Private priv = (CFF_Private)parser->object;
FT_Byte** data = parser->stack;
CFF_Blend blend;
FT_Error error;
if ( !priv || !priv->subfont )
{
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
blend = &priv->subfont->blend;
if ( blend->usedBV )
{
FT_ERROR(( " cff_parse_vsindex: vsindex not allowed after blend\n" ));
error = FT_THROW( Syntax_Error );
goto Exit;
}
priv->vsindex = (FT_UInt)cff_parse_num( parser, data++ );
FT_TRACE4(( " %d\n", priv->vsindex ));
error = FT_Err_Ok;
Exit:
return error;
}
static FT_Error
cff_parse_blend( CFF_Parser parser )
{
/* blend operator can only be used in a Private DICT */
CFF_Private priv = (CFF_Private)parser->object;
CFF_SubFont subFont;
CFF_Blend blend;
FT_UInt numBlends;
FT_Error error;
if ( !priv || !priv->subfont )
{
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
subFont = priv->subfont;
blend = &subFont->blend;
if ( cff_blend_check_vector( blend,
priv->vsindex,
subFont->lenNDV,
subFont->NDV ) )
{
error = cff_blend_build_vector( blend,
priv->vsindex,
subFont->lenNDV,
subFont->NDV );
if ( error )
goto Exit;
}
numBlends = (FT_UInt)cff_parse_num( parser, parser->top - 1 );
if ( numBlends > parser->stackSize )
{
FT_ERROR(( "cff_parse_blend: Invalid number of blends\n" ));
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
FT_TRACE4(( " %d value%s blended\n",
numBlends,
numBlends == 1 ? "" : "s" ));
error = cff_blend_doBlend( subFont, parser, numBlends );
blend->usedBV = TRUE;
Exit:
return error;
}
/* maxstack operator increases parser and operand stacks for CFF2 */
static FT_Error
cff_parse_maxstack( CFF_Parser parser )
{
/* maxstack operator can only be used in a Top DICT */
CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
FT_Byte** data = parser->stack;
FT_Error error = FT_Err_Ok;
if ( !dict )
{
error = FT_THROW( Invalid_File_Format );
goto Exit;
}
dict->maxstack = (FT_UInt)cff_parse_num( parser, data++ );
if ( dict->maxstack > CFF2_MAX_STACK )
dict->maxstack = CFF2_MAX_STACK;
if ( dict->maxstack < CFF2_DEFAULT_STACK )
dict->maxstack = CFF2_DEFAULT_STACK;
FT_TRACE4(( " %d\n", dict->maxstack ));
Exit:
return error;
}
#define CFF_FIELD_NUM( code, name, id ) \
CFF_FIELD( code, name, id, cff_kind_num )
#define CFF_FIELD_FIXED( code, name, id ) \
CFF_FIELD( code, name, id, cff_kind_fixed )
#define CFF_FIELD_FIXED_1000( code, name, id ) \
CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
#define CFF_FIELD_STRING( code, name, id ) \
CFF_FIELD( code, name, id, cff_kind_string )
#define CFF_FIELD_BOOL( code, name, id ) \
CFF_FIELD( code, name, id, cff_kind_bool )
#undef CFF_FIELD
#undef CFF_FIELD_DELTA
#ifndef FT_DEBUG_LEVEL_TRACE
#define CFF_FIELD_CALLBACK( code, name, id ) \
{ \
cff_kind_callback, \
code | CFFCODE, \
0, 0, \
cff_parse_ ## name, \
0, 0 \
},
#define CFF_FIELD_BLEND( code, id ) \
{ \
cff_kind_blend, \
code | CFFCODE, \
0, 0, \
cff_parse_blend, \
0, 0 \
},
#define CFF_FIELD( code, name, id, kind ) \
{ \
kind, \
code | CFFCODE, \
FT_FIELD_OFFSET( name ), \
FT_FIELD_SIZE( name ), \
0, 0, 0 \
},
#define CFF_FIELD_DELTA( code, name, max, id ) \
{ \
cff_kind_delta, \
code | CFFCODE, \
FT_FIELD_OFFSET( name ), \
FT_FIELD_SIZE_DELTA( name ), \
0, \
max, \
FT_FIELD_OFFSET( num_ ## name ) \
},
static const CFF_Field_Handler cff_field_handlers[] =
{
#include "cfftoken.h"
{ 0, 0, 0, 0, 0, 0, 0 }
};
#else /* FT_DEBUG_LEVEL_TRACE */
#define CFF_FIELD_CALLBACK( code, name, id ) \
{ \
cff_kind_callback, \
code | CFFCODE, \
0, 0, \
cff_parse_ ## name, \
0, 0, \
id \
},
#define CFF_FIELD_BLEND( code, id ) \
{ \
cff_kind_blend, \
code | CFFCODE, \
0, 0, \
cff_parse_blend, \
0, 0, \
id \
},
#define CFF_FIELD( code, name, id, kind ) \
{ \
kind, \
code | CFFCODE, \
FT_FIELD_OFFSET( name ), \
FT_FIELD_SIZE( name ), \
0, 0, 0, \
id \
},
#define CFF_FIELD_DELTA( code, name, max, id ) \
{ \
cff_kind_delta, \
code | CFFCODE, \
FT_FIELD_OFFSET( name ), \
FT_FIELD_SIZE_DELTA( name ), \
0, \
max, \
FT_FIELD_OFFSET( num_ ## name ), \
id \
},
static const CFF_Field_Handler cff_field_handlers[] =
{
#include "cfftoken.h"
{ 0, 0, 0, 0, 0, 0, 0, 0 }
};
#endif /* FT_DEBUG_LEVEL_TRACE */
FT_LOCAL_DEF( FT_Error )
cff_parser_run( CFF_Parser parser,
FT_Byte* start,
FT_Byte* limit )
{
#ifdef CFF_CONFIG_OPTION_OLD_ENGINE
PSAux_Service psaux;
#endif
FT_Byte* p = start;
FT_Error error = FT_Err_Ok;
FT_Library library = parser->library;
FT_UNUSED( library );
parser->top = parser->stack;
parser->start = start;
parser->limit = limit;
parser->cursor = start;
while ( p < limit )
{
FT_UInt v = *p;
/* Opcode 31 is legacy MM T2 operator, not a number. */
/* Opcode 255 is reserved and should not appear in fonts; */
/* it is used internally for CFF2 blends. */
if ( v >= 27 && v != 31 && v != 255 )
{
/* it's a number; we will push its position on the stack */
if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
goto Stack_Overflow;
*parser->top++ = p;
/* now, skip it */
if ( v == 30 )
{
/* skip real number */
p++;
for (;;)
{
/* An unterminated floating point number at the */
/* end of a dictionary is invalid but harmless. */
if ( p >= limit )
goto Exit;
v = p[0] >> 4;
if ( v == 15 )
break;
v = p[0] & 0xF;
if ( v == 15 )
break;
p++;
}
}
else if ( v == 28 )
p += 2;
else if ( v == 29 )
p += 4;
else if ( v > 246 )
p += 1;
}
#ifdef CFF_CONFIG_OPTION_OLD_ENGINE
else if ( v == 31 )
{
/* a Type 2 charstring */
CFF_Decoder decoder;
CFF_FontRec cff_rec;
FT_Byte* charstring_base;
FT_ULong charstring_len;
FT_Fixed* stack;
FT_Byte* q;
charstring_base = ++p;
/* search `endchar' operator */
for (;;)
{
if ( p >= limit )
goto Exit;
if ( *p == 14 )
break;
p++;
}
charstring_len = (FT_ULong)( p - charstring_base ) + 1;
/* construct CFF_Decoder object */
FT_ZERO( &decoder );
FT_ZERO( &cff_rec );
cff_rec.top_font.font_dict.num_designs = parser->num_designs;
cff_rec.top_font.font_dict.num_axes = parser->num_axes;
decoder.cff = &cff_rec;
psaux = (PSAux_Service)FT_Get_Module_Interface( library, "psaux" );
if ( !psaux )
{
FT_ERROR(( "cff_parser_run: cannot access `psaux' module\n" ));
error = FT_THROW( Missing_Module );
goto Exit;
}
error = psaux->cff_decoder_funcs->parse_charstrings_old(
&decoder, charstring_base, charstring_len, 1 );
if ( error )
goto Exit;
/* Now copy the stack data in the temporary decoder object, */
/* converting it back to charstring number representations */
/* (this is ugly, I know). */
/* */
/* We overwrite the original top DICT charstring under the */
/* assumption that the charstring representation of the result */
/* of `cff_decoder_parse_charstrings' is shorter, which should */
/* be always true. */
q = charstring_base - 1;
stack = decoder.stack;
while ( stack < decoder.top )
{
FT_ULong num;
FT_Bool neg;
if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
goto Stack_Overflow;
*parser->top++ = q;
if ( *stack < 0 )
{
num = (FT_ULong)-*stack;
neg = 1;
}
else
{
num = (FT_ULong)*stack;
neg = 0;
}
if ( num & 0xFFFFU )
{
if ( neg )
num = (FT_ULong)-num;
*q++ = 255;
*q++ = ( num & 0xFF000000U ) >> 24;
*q++ = ( num & 0x00FF0000U ) >> 16;
*q++ = ( num & 0x0000FF00U ) >> 8;
*q++ = num & 0x000000FFU;
}
else
{
num >>= 16;
if ( neg )
{
if ( num <= 107 )
*q++ = (FT_Byte)( 139 - num );
else if ( num <= 1131 )
{
*q++ = (FT_Byte)( ( ( num - 108 ) >> 8 ) + 251 );
*q++ = (FT_Byte)( ( num - 108 ) & 0xFF );
}
else
{
num = (FT_ULong)-num;
*q++ = 28;
*q++ = (FT_Byte)( num >> 8 );
*q++ = (FT_Byte)( num & 0xFF );
}
}
else
{
if ( num <= 107 )
*q++ = (FT_Byte)( num + 139 );
else if ( num <= 1131 )
{
*q++ = (FT_Byte)( ( ( num - 108 ) >> 8 ) + 247 );
*q++ = (FT_Byte)( ( num - 108 ) & 0xFF );
}
else
{
*q++ = 28;
*q++ = (FT_Byte)( num >> 8 );
*q++ = (FT_Byte)( num & 0xFF );
}
}
}
stack++;
}
}
#endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
else
{
/* This is not a number, hence it's an operator. Compute its code */
/* and look for it in our current list. */
FT_UInt code;
FT_UInt num_args;
const CFF_Field_Handler* field;
if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
goto Stack_Overflow;
num_args = (FT_UInt)( parser->top - parser->stack );
*parser->top = p;
code = v;
if ( v == 12 )
{
/* two byte operator */
p++;
if ( p >= limit )
goto Syntax_Error;
code = 0x100 | p[0];
}
code = code | parser->object_code;
for ( field = cff_field_handlers; field->kind; field++ )
{
if ( field->code == (FT_Int)code )
{
/* we found our field's handler; read it */
FT_Long val;
FT_Byte* q = (FT_Byte*)parser->object + field->offset;
#ifdef FT_DEBUG_LEVEL_TRACE
FT_TRACE4(( " %s", field->id ));
#endif
/* check that we have enough arguments -- except for */
/* delta encoded arrays, which can be empty */
if ( field->kind != cff_kind_delta && num_args < 1 )
goto Stack_Underflow;
switch ( field->kind )
{
case cff_kind_bool:
case cff_kind_string:
case cff_kind_num:
val = cff_parse_num( parser, parser->stack );
goto Store_Number;
case cff_kind_fixed:
val = cff_parse_fixed( parser, parser->stack );
goto Store_Number;
case cff_kind_fixed_thousand:
val = cff_parse_fixed_scaled( parser, parser->stack, 3 );
Store_Number:
switch ( field->size )
{
case (8 / FT_CHAR_BIT):
*(FT_Byte*)q = (FT_Byte)val;
break;
case (16 / FT_CHAR_BIT):
*(FT_Short*)q = (FT_Short)val;
break;
case (32 / FT_CHAR_BIT):
*(FT_Int32*)q = (FT_Int)val;
break;
default: /* for 64-bit systems */
*(FT_Long*)q = val;
}
#ifdef FT_DEBUG_LEVEL_TRACE
switch ( field->kind )
{
case cff_kind_bool:
FT_TRACE4(( " %s\n", val ? "true" : "false" ));
break;
case cff_kind_string:
FT_TRACE4(( " %ld (SID)\n", val ));
break;
case cff_kind_num:
FT_TRACE4(( " %ld\n", val ));
break;
case cff_kind_fixed:
FT_TRACE4(( " %f\n", (double)val / 65536 ));
break;
case cff_kind_fixed_thousand:
FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
default:
; /* never reached */
}
#endif
break;
case cff_kind_delta:
{
FT_Byte* qcount = (FT_Byte*)parser->object +
field->count_offset;
FT_Byte** data = parser->stack;
if ( num_args > field->array_max )
num_args = field->array_max;
FT_TRACE4(( " [" ));
/* store count */
*qcount = (FT_Byte)num_args;
val = 0;
while ( num_args > 0 )
{
val = ADD_LONG( val, cff_parse_num( parser, data++ ) );
switch ( field->size )
{
case (8 / FT_CHAR_BIT):
*(FT_Byte*)q = (FT_Byte)val;
break;
case (16 / FT_CHAR_BIT):
*(FT_Short*)q = (FT_Short)val;
break;
case (32 / FT_CHAR_BIT):
*(FT_Int32*)q = (FT_Int)val;
break;
default: /* for 64-bit systems */
*(FT_Long*)q = val;
}
FT_TRACE4(( " %ld", val ));
q += field->size;
num_args--;
}
FT_TRACE4(( "]\n" ));
}
break;
default: /* callback or blend */
error = field->reader( parser );
if ( error )
goto Exit;
}
goto Found;
}
}
/* this is an unknown operator, or it is unsupported; */
/* we will ignore it for now. */
Found:
/* clear stack */
/* TODO: could clear blend stack here, */
/* but we don't have access to subFont */
if ( field->kind != cff_kind_blend )
parser->top = parser->stack;
}
p++;
}
Exit:
return error;
Stack_Overflow:
error = FT_THROW( Invalid_Argument );
goto Exit;
Stack_Underflow:
error = FT_THROW( Invalid_Argument );
goto Exit;
Syntax_Error:
error = FT_THROW( Invalid_Argument );
goto Exit;
}
/* END */