1 /***************************************************************************/ 2 /* */ 3 /* cidload.c */ 4 /* */ 5 /* CID-keyed Type1 font loader (body). */ 6 /* */ 7 /* Copyright 1996-2015 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 10 /* This file is part of the FreeType project, and may only be used, */ 11 /* modified, and distributed under the terms of the FreeType project */ 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13 /* this file you indicate that you have read the license and */ 14 /* understand and accept it fully. */ 15 /* */ 16 /***************************************************************************/ 17 18 19 #include <ft2build.h> 20 #include FT_INTERNAL_DEBUG_H 21 #include FT_CONFIG_CONFIG_H 22 #include FT_MULTIPLE_MASTERS_H 23 #include FT_INTERNAL_TYPE1_TYPES_H 24 25 #include "cidload.h" 26 27 #include "ciderrs.h" 28 29 30 /*************************************************************************/ 31 /* */ 32 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 33 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 34 /* messages during execution. */ 35 /* */ 36 #undef FT_COMPONENT 37 #define FT_COMPONENT trace_cidload 38 39 40 /* read a single offset */ 41 FT_LOCAL_DEF( FT_ULong ) 42 cid_get_offset( FT_Byte* *start, 43 FT_Byte offsize ) 44 { 45 FT_ULong result; 46 FT_Byte* p = *start; 47 48 49 for ( result = 0; offsize > 0; offsize-- ) 50 { 51 result <<= 8; 52 result |= *p++; 53 } 54 55 *start = p; 56 return result; 57 } 58 59 60 /*************************************************************************/ 61 /*************************************************************************/ 62 /***** *****/ 63 /***** TYPE 1 SYMBOL PARSING *****/ 64 /***** *****/ 65 /*************************************************************************/ 66 /*************************************************************************/ 67 68 69 static FT_Error 70 cid_load_keyword( CID_Face face, 71 CID_Loader* loader, 72 const T1_Field keyword ) 73 { 74 FT_Error error; 75 CID_Parser* parser = &loader->parser; 76 FT_Byte* object; 77 void* dummy_object; 78 CID_FaceInfo cid = &face->cid; 79 80 81 /* if the keyword has a dedicated callback, call it */ 82 if ( keyword->type == T1_FIELD_TYPE_CALLBACK ) 83 { 84 keyword->reader( (FT_Face)face, parser ); 85 error = parser->root.error; 86 goto Exit; 87 } 88 89 /* we must now compute the address of our target object */ 90 switch ( keyword->location ) 91 { 92 case T1_FIELD_LOCATION_CID_INFO: 93 object = (FT_Byte*)cid; 94 break; 95 96 case T1_FIELD_LOCATION_FONT_INFO: 97 object = (FT_Byte*)&cid->font_info; 98 break; 99 100 case T1_FIELD_LOCATION_FONT_EXTRA: 101 object = (FT_Byte*)&face->font_extra; 102 break; 103 104 case T1_FIELD_LOCATION_BBOX: 105 object = (FT_Byte*)&cid->font_bbox; 106 break; 107 108 default: 109 { 110 CID_FaceDict dict; 111 112 113 if ( parser->num_dict < 0 || parser->num_dict >= cid->num_dicts ) 114 { 115 FT_ERROR(( "cid_load_keyword: invalid use of `%s'\n", 116 keyword->ident )); 117 error = FT_THROW( Syntax_Error ); 118 goto Exit; 119 } 120 121 dict = cid->font_dicts + parser->num_dict; 122 switch ( keyword->location ) 123 { 124 case T1_FIELD_LOCATION_PRIVATE: 125 object = (FT_Byte*)&dict->private_dict; 126 break; 127 128 default: 129 object = (FT_Byte*)dict; 130 } 131 } 132 } 133 134 dummy_object = object; 135 136 /* now, load the keyword data in the object's field(s) */ 137 if ( keyword->type == T1_FIELD_TYPE_INTEGER_ARRAY || 138 keyword->type == T1_FIELD_TYPE_FIXED_ARRAY ) 139 error = cid_parser_load_field_table( &loader->parser, keyword, 140 &dummy_object ); 141 else 142 error = cid_parser_load_field( &loader->parser, 143 keyword, &dummy_object ); 144 Exit: 145 return error; 146 } 147 148 149 FT_CALLBACK_DEF( FT_Error ) 150 cid_parse_font_matrix( CID_Face face, 151 CID_Parser* parser ) 152 { 153 CID_FaceDict dict; 154 FT_Face root = (FT_Face)&face->root; 155 FT_Fixed temp[6]; 156 FT_Fixed temp_scale; 157 158 159 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts ) 160 { 161 FT_Matrix* matrix; 162 FT_Vector* offset; 163 FT_Int result; 164 165 166 dict = face->cid.font_dicts + parser->num_dict; 167 matrix = &dict->font_matrix; 168 offset = &dict->font_offset; 169 170 /* input is scaled by 1000 to accommodate default FontMatrix */ 171 result = cid_parser_to_fixed_array( parser, 6, temp, 3 ); 172 173 if ( result < 6 ) 174 return FT_THROW( Invalid_File_Format ); 175 176 temp_scale = FT_ABS( temp[3] ); 177 178 if ( temp_scale == 0 ) 179 { 180 FT_ERROR(( "cid_parse_font_matrix: invalid font matrix\n" )); 181 return FT_THROW( Invalid_File_Format ); 182 } 183 184 /* atypical case */ 185 if ( temp_scale != 0x10000L ) 186 { 187 /* set units per EM based on FontMatrix values */ 188 root->units_per_EM = (FT_UShort)FT_DivFix( 1000, temp_scale ); 189 190 temp[0] = FT_DivFix( temp[0], temp_scale ); 191 temp[1] = FT_DivFix( temp[1], temp_scale ); 192 temp[2] = FT_DivFix( temp[2], temp_scale ); 193 temp[4] = FT_DivFix( temp[4], temp_scale ); 194 temp[5] = FT_DivFix( temp[5], temp_scale ); 195 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L; 196 } 197 198 matrix->xx = temp[0]; 199 matrix->yx = temp[1]; 200 matrix->xy = temp[2]; 201 matrix->yy = temp[3]; 202 203 /* note that the font offsets are expressed in integer font units */ 204 offset->x = temp[4] >> 16; 205 offset->y = temp[5] >> 16; 206 } 207 208 return FT_Err_Ok; 209 } 210 211 212 FT_CALLBACK_DEF( FT_Error ) 213 parse_fd_array( CID_Face face, 214 CID_Parser* parser ) 215 { 216 CID_FaceInfo cid = &face->cid; 217 FT_Memory memory = face->root.memory; 218 FT_Error error = FT_Err_Ok; 219 FT_Long num_dicts; 220 221 222 num_dicts = cid_parser_to_int( parser ); 223 if ( num_dicts < 0 ) 224 { 225 FT_ERROR(( "parse_fd_array: invalid number of dictionaries\n" )); 226 error = FT_THROW( Invalid_File_Format ); 227 goto Exit; 228 } 229 230 if ( !cid->font_dicts ) 231 { 232 FT_Int n; 233 234 235 if ( FT_NEW_ARRAY( cid->font_dicts, num_dicts ) ) 236 goto Exit; 237 238 cid->num_dicts = num_dicts; 239 240 /* don't forget to set a few defaults */ 241 for ( n = 0; n < cid->num_dicts; n++ ) 242 { 243 CID_FaceDict dict = cid->font_dicts + n; 244 245 246 /* default value for lenIV */ 247 dict->private_dict.lenIV = 4; 248 } 249 } 250 251 Exit: 252 return error; 253 } 254 255 256 /* by mistake, `expansion_factor' appears both in PS_PrivateRec */ 257 /* and CID_FaceDictRec (both are public header files and can't */ 258 /* changed); we simply copy the value */ 259 260 FT_CALLBACK_DEF( FT_Error ) 261 parse_expansion_factor( CID_Face face, 262 CID_Parser* parser ) 263 { 264 CID_FaceDict dict; 265 266 267 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts ) 268 { 269 dict = face->cid.font_dicts + parser->num_dict; 270 271 dict->expansion_factor = cid_parser_to_fixed( parser, 0 ); 272 dict->private_dict.expansion_factor = dict->expansion_factor; 273 } 274 275 return FT_Err_Ok; 276 } 277 278 279 static 280 const T1_FieldRec cid_field_records[] = 281 { 282 283 #include "cidtoken.h" 284 285 T1_FIELD_CALLBACK( "FDArray", parse_fd_array, 0 ) 286 T1_FIELD_CALLBACK( "FontMatrix", cid_parse_font_matrix, 0 ) 287 T1_FIELD_CALLBACK( "ExpansionFactor", parse_expansion_factor, 0 ) 288 289 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 } 290 }; 291 292 293 static FT_Error 294 cid_parse_dict( CID_Face face, 295 CID_Loader* loader, 296 FT_Byte* base, 297 FT_ULong size ) 298 { 299 CID_Parser* parser = &loader->parser; 300 301 302 parser->root.cursor = base; 303 parser->root.limit = base + size; 304 parser->root.error = FT_Err_Ok; 305 306 { 307 FT_Byte* cur = base; 308 FT_Byte* limit = cur + size; 309 310 311 for (;;) 312 { 313 FT_Byte* newlimit; 314 315 316 parser->root.cursor = cur; 317 cid_parser_skip_spaces( parser ); 318 319 if ( parser->root.cursor >= limit ) 320 newlimit = limit - 1 - 17; 321 else 322 newlimit = parser->root.cursor - 17; 323 324 /* look for `%ADOBeginFontDict' */ 325 for ( ; cur < newlimit; cur++ ) 326 { 327 if ( *cur == '%' && 328 ft_strncmp( (char*)cur, "%ADOBeginFontDict", 17 ) == 0 ) 329 { 330 /* if /FDArray was found, then cid->num_dicts is > 0, and */ 331 /* we can start increasing parser->num_dict */ 332 if ( face->cid.num_dicts > 0 ) 333 parser->num_dict++; 334 } 335 } 336 337 cur = parser->root.cursor; 338 /* no error can occur in cid_parser_skip_spaces */ 339 if ( cur >= limit ) 340 break; 341 342 cid_parser_skip_PS_token( parser ); 343 if ( parser->root.cursor >= limit || parser->root.error ) 344 break; 345 346 /* look for immediates */ 347 if ( *cur == '/' && cur + 2 < limit ) 348 { 349 FT_UInt len; 350 351 352 cur++; 353 len = (FT_UInt)( parser->root.cursor - cur ); 354 355 if ( len > 0 && len < 22 ) 356 { 357 /* now compare the immediate name to the keyword table */ 358 T1_Field keyword = (T1_Field)cid_field_records; 359 360 361 for (;;) 362 { 363 FT_Byte* name; 364 365 366 name = (FT_Byte*)keyword->ident; 367 if ( !name ) 368 break; 369 370 if ( cur[0] == name[0] && 371 len == ft_strlen( (const char*)name ) ) 372 { 373 FT_UInt n; 374 375 376 for ( n = 1; n < len; n++ ) 377 if ( cur[n] != name[n] ) 378 break; 379 380 if ( n >= len ) 381 { 382 /* we found it - run the parsing callback */ 383 parser->root.error = cid_load_keyword( face, 384 loader, 385 keyword ); 386 if ( parser->root.error ) 387 return parser->root.error; 388 break; 389 } 390 } 391 keyword++; 392 } 393 } 394 } 395 396 cur = parser->root.cursor; 397 } 398 } 399 return parser->root.error; 400 } 401 402 403 /* read the subrmap and the subrs of each font dict */ 404 static FT_Error 405 cid_read_subrs( CID_Face face ) 406 { 407 CID_FaceInfo cid = &face->cid; 408 FT_Memory memory = face->root.memory; 409 FT_Stream stream = face->cid_stream; 410 FT_Error error; 411 FT_Int n; 412 CID_Subrs subr; 413 FT_UInt max_offsets = 0; 414 FT_ULong* offsets = NULL; 415 PSAux_Service psaux = (PSAux_Service)face->psaux; 416 417 418 if ( FT_NEW_ARRAY( face->subrs, cid->num_dicts ) ) 419 goto Exit; 420 421 subr = face->subrs; 422 for ( n = 0; n < cid->num_dicts; n++, subr++ ) 423 { 424 CID_FaceDict dict = cid->font_dicts + n; 425 FT_Int lenIV = dict->private_dict.lenIV; 426 FT_UInt count, num_subrs = dict->num_subrs; 427 FT_ULong data_len; 428 FT_Byte* p; 429 430 431 /* Check for possible overflow. */ 432 if ( num_subrs == FT_UINT_MAX ) 433 { 434 error = FT_THROW( Syntax_Error ); 435 goto Fail; 436 } 437 438 /* reallocate offsets array if needed */ 439 if ( num_subrs + 1 > max_offsets ) 440 { 441 FT_UInt new_max = FT_PAD_CEIL( num_subrs + 1, 4 ); 442 443 444 if ( new_max <= max_offsets ) 445 { 446 error = FT_THROW( Syntax_Error ); 447 goto Fail; 448 } 449 450 if ( FT_RENEW_ARRAY( offsets, max_offsets, new_max ) ) 451 goto Fail; 452 453 max_offsets = new_max; 454 } 455 456 /* read the subrmap's offsets */ 457 if ( FT_STREAM_SEEK( cid->data_offset + dict->subrmap_offset ) || 458 FT_FRAME_ENTER( ( num_subrs + 1 ) * (FT_UInt)dict->sd_bytes ) ) 459 goto Fail; 460 461 p = (FT_Byte*)stream->cursor; 462 for ( count = 0; count <= num_subrs; count++ ) 463 offsets[count] = cid_get_offset( &p, (FT_Byte)dict->sd_bytes ); 464 465 FT_FRAME_EXIT(); 466 467 /* offsets must be ordered */ 468 for ( count = 1; count <= num_subrs; count++ ) 469 if ( offsets[count - 1] > offsets[count] ) 470 goto Fail; 471 472 /* now, compute the size of subrs charstrings, */ 473 /* allocate, and read them */ 474 data_len = offsets[num_subrs] - offsets[0]; 475 476 if ( FT_NEW_ARRAY( subr->code, num_subrs + 1 ) || 477 FT_ALLOC( subr->code[0], data_len ) ) 478 goto Fail; 479 480 if ( FT_STREAM_SEEK( cid->data_offset + offsets[0] ) || 481 FT_STREAM_READ( subr->code[0], data_len ) ) 482 goto Fail; 483 484 /* set up pointers */ 485 for ( count = 1; count <= num_subrs; count++ ) 486 { 487 FT_ULong len; 488 489 490 len = offsets[count] - offsets[count - 1]; 491 subr->code[count] = subr->code[count - 1] + len; 492 } 493 494 /* decrypt subroutines, but only if lenIV >= 0 */ 495 if ( lenIV >= 0 ) 496 { 497 for ( count = 0; count < num_subrs; count++ ) 498 { 499 FT_ULong len; 500 501 502 len = offsets[count + 1] - offsets[count]; 503 psaux->t1_decrypt( subr->code[count], len, 4330 ); 504 } 505 } 506 507 subr->num_subrs = (FT_Int)num_subrs; 508 } 509 510 Exit: 511 FT_FREE( offsets ); 512 return error; 513 514 Fail: 515 if ( face->subrs ) 516 { 517 for ( n = 0; n < cid->num_dicts; n++ ) 518 { 519 if ( face->subrs[n].code ) 520 FT_FREE( face->subrs[n].code[0] ); 521 522 FT_FREE( face->subrs[n].code ); 523 } 524 FT_FREE( face->subrs ); 525 } 526 goto Exit; 527 } 528 529 530 static void 531 cid_init_loader( CID_Loader* loader, 532 CID_Face face ) 533 { 534 FT_UNUSED( face ); 535 536 FT_MEM_ZERO( loader, sizeof ( *loader ) ); 537 } 538 539 540 static void 541 cid_done_loader( CID_Loader* loader ) 542 { 543 CID_Parser* parser = &loader->parser; 544 545 546 /* finalize parser */ 547 cid_parser_done( parser ); 548 } 549 550 551 static FT_Error 552 cid_hex_to_binary( FT_Byte* data, 553 FT_ULong data_len, 554 FT_ULong offset, 555 CID_Face face ) 556 { 557 FT_Stream stream = face->root.stream; 558 FT_Error error; 559 560 FT_Byte buffer[256]; 561 FT_Byte *p, *plimit; 562 FT_Byte *d, *dlimit; 563 FT_Byte val; 564 565 FT_Bool upper_nibble, done; 566 567 568 if ( FT_STREAM_SEEK( offset ) ) 569 goto Exit; 570 571 d = data; 572 dlimit = d + data_len; 573 p = buffer; 574 plimit = p; 575 576 upper_nibble = 1; 577 done = 0; 578 579 while ( d < dlimit ) 580 { 581 if ( p >= plimit ) 582 { 583 FT_ULong oldpos = FT_STREAM_POS(); 584 FT_ULong size = stream->size - oldpos; 585 586 587 if ( size == 0 ) 588 { 589 error = FT_THROW( Syntax_Error ); 590 goto Exit; 591 } 592 593 if ( FT_STREAM_READ( buffer, 256 > size ? size : 256 ) ) 594 goto Exit; 595 p = buffer; 596 plimit = p + FT_STREAM_POS() - oldpos; 597 } 598 599 if ( ft_isdigit( *p ) ) 600 val = (FT_Byte)( *p - '0' ); 601 else if ( *p >= 'a' && *p <= 'f' ) 602 val = (FT_Byte)( *p - 'a' ); 603 else if ( *p >= 'A' && *p <= 'F' ) 604 val = (FT_Byte)( *p - 'A' + 10 ); 605 else if ( *p == ' ' || 606 *p == '\t' || 607 *p == '\r' || 608 *p == '\n' || 609 *p == '\f' || 610 *p == '\0' ) 611 { 612 p++; 613 continue; 614 } 615 else if ( *p == '>' ) 616 { 617 val = 0; 618 done = 1; 619 } 620 else 621 { 622 error = FT_THROW( Syntax_Error ); 623 goto Exit; 624 } 625 626 if ( upper_nibble ) 627 *d = (FT_Byte)( val << 4 ); 628 else 629 { 630 *d = (FT_Byte)( *d + val ); 631 d++; 632 } 633 634 upper_nibble = (FT_Byte)( 1 - upper_nibble ); 635 636 if ( done ) 637 break; 638 639 p++; 640 } 641 642 error = FT_Err_Ok; 643 644 Exit: 645 return error; 646 } 647 648 649 FT_LOCAL_DEF( FT_Error ) 650 cid_face_open( CID_Face face, 651 FT_Int face_index ) 652 { 653 CID_Loader loader; 654 CID_Parser* parser; 655 FT_Memory memory = face->root.memory; 656 FT_Error error; 657 658 659 cid_init_loader( &loader, face ); 660 661 parser = &loader.parser; 662 error = cid_parser_new( parser, face->root.stream, face->root.memory, 663 (PSAux_Service)face->psaux ); 664 if ( error ) 665 goto Exit; 666 667 error = cid_parse_dict( face, &loader, 668 parser->postscript, 669 parser->postscript_len ); 670 if ( error ) 671 goto Exit; 672 673 if ( face_index < 0 ) 674 goto Exit; 675 676 if ( FT_NEW( face->cid_stream ) ) 677 goto Exit; 678 679 if ( parser->binary_length ) 680 { 681 /* we must convert the data section from hexadecimal to binary */ 682 if ( FT_ALLOC( face->binary_data, parser->binary_length ) || 683 cid_hex_to_binary( face->binary_data, parser->binary_length, 684 parser->data_offset, face ) ) 685 goto Exit; 686 687 FT_Stream_OpenMemory( face->cid_stream, 688 face->binary_data, parser->binary_length ); 689 face->cid.data_offset = 0; 690 } 691 else 692 { 693 *face->cid_stream = *face->root.stream; 694 face->cid.data_offset = loader.parser.data_offset; 695 } 696 697 error = cid_read_subrs( face ); 698 699 Exit: 700 cid_done_loader( &loader ); 701 return error; 702 } 703 704 705 /* END */ 706