1 /***************************************************************************/ 2 /* */ 3 /* sfobjs.c */ 4 /* */ 5 /* SFNT object management (base). */ 6 /* */ 7 /* Copyright 1996-2017 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 "sfobjs.h" 21 #include "ttload.h" 22 #include "ttcmap.h" 23 #include "ttkern.h" 24 #include FT_INTERNAL_SFNT_H 25 #include FT_INTERNAL_DEBUG_H 26 #include FT_TRUETYPE_IDS_H 27 #include FT_TRUETYPE_TAGS_H 28 #include FT_SERVICE_POSTSCRIPT_CMAPS_H 29 #include FT_SFNT_NAMES_H 30 #include FT_GZIP_H 31 32 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 33 #include FT_SERVICE_MULTIPLE_MASTERS_H 34 #include FT_SERVICE_METRICS_VARIATIONS_H 35 #endif 36 37 #include "sferrors.h" 38 39 #ifdef TT_CONFIG_OPTION_BDF 40 #include "ttbdf.h" 41 #endif 42 43 44 /*************************************************************************/ 45 /* */ 46 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 47 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 48 /* messages during execution. */ 49 /* */ 50 #undef FT_COMPONENT 51 #define FT_COMPONENT trace_sfobjs 52 53 54 55 /* convert a UTF-16 name entry to ASCII */ 56 static FT_String* tt_name_ascii_from_utf16(TT_Name entry,FT_Memory memory)57 tt_name_ascii_from_utf16( TT_Name entry, 58 FT_Memory memory ) 59 { 60 FT_String* string = NULL; 61 FT_UInt len, code, n; 62 FT_Byte* read = (FT_Byte*)entry->string; 63 FT_Error error; 64 65 66 len = (FT_UInt)entry->stringLength / 2; 67 68 if ( FT_NEW_ARRAY( string, len + 1 ) ) 69 return NULL; 70 71 for ( n = 0; n < len; n++ ) 72 { 73 code = FT_NEXT_USHORT( read ); 74 75 if ( code == 0 ) 76 break; 77 78 if ( code < 32 || code > 127 ) 79 code = '?'; 80 81 string[n] = (char)code; 82 } 83 84 string[n] = 0; 85 86 return string; 87 } 88 89 90 /* convert an Apple Roman or symbol name entry to ASCII */ 91 static FT_String* tt_name_ascii_from_other(TT_Name entry,FT_Memory memory)92 tt_name_ascii_from_other( TT_Name entry, 93 FT_Memory memory ) 94 { 95 FT_String* string = NULL; 96 FT_UInt len, code, n; 97 FT_Byte* read = (FT_Byte*)entry->string; 98 FT_Error error; 99 100 101 len = (FT_UInt)entry->stringLength; 102 103 if ( FT_NEW_ARRAY( string, len + 1 ) ) 104 return NULL; 105 106 for ( n = 0; n < len; n++ ) 107 { 108 code = *read++; 109 110 if ( code == 0 ) 111 break; 112 113 if ( code < 32 || code > 127 ) 114 code = '?'; 115 116 string[n] = (char)code; 117 } 118 119 string[n] = 0; 120 121 return string; 122 } 123 124 125 typedef FT_String* (*TT_Name_ConvertFunc)( TT_Name entry, 126 FT_Memory memory ); 127 128 129 /* documentation is in sfnt.h */ 130 131 FT_LOCAL_DEF( FT_Error ) tt_face_get_name(TT_Face face,FT_UShort nameid,FT_String ** name)132 tt_face_get_name( TT_Face face, 133 FT_UShort nameid, 134 FT_String** name ) 135 { 136 FT_Memory memory = face->root.memory; 137 FT_Error error = FT_Err_Ok; 138 FT_String* result = NULL; 139 FT_UShort n; 140 TT_Name rec; 141 142 FT_Int found_apple = -1; 143 FT_Int found_apple_roman = -1; 144 FT_Int found_apple_english = -1; 145 FT_Int found_win = -1; 146 FT_Int found_unicode = -1; 147 148 FT_Bool is_english = 0; 149 150 TT_Name_ConvertFunc convert; 151 152 153 FT_ASSERT( name ); 154 155 rec = face->name_table.names; 156 for ( n = 0; n < face->num_names; n++, rec++ ) 157 { 158 /* According to the OpenType 1.3 specification, only Microsoft or */ 159 /* Apple platform IDs might be used in the `name' table. The */ 160 /* `Unicode' platform is reserved for the `cmap' table, and the */ 161 /* `ISO' one is deprecated. */ 162 /* */ 163 /* However, the Apple TrueType specification doesn't say the same */ 164 /* thing and goes to suggest that all Unicode `name' table entries */ 165 /* should be coded in UTF-16 (in big-endian format I suppose). */ 166 /* */ 167 if ( rec->nameID == nameid && rec->stringLength > 0 ) 168 { 169 switch ( rec->platformID ) 170 { 171 case TT_PLATFORM_APPLE_UNICODE: 172 case TT_PLATFORM_ISO: 173 /* there is `languageID' to check there. We should use this */ 174 /* field only as a last solution when nothing else is */ 175 /* available. */ 176 /* */ 177 found_unicode = n; 178 break; 179 180 case TT_PLATFORM_MACINTOSH: 181 /* This is a bit special because some fonts will use either */ 182 /* an English language id, or a Roman encoding id, to indicate */ 183 /* the English version of its font name. */ 184 /* */ 185 if ( rec->languageID == TT_MAC_LANGID_ENGLISH ) 186 found_apple_english = n; 187 else if ( rec->encodingID == TT_MAC_ID_ROMAN ) 188 found_apple_roman = n; 189 break; 190 191 case TT_PLATFORM_MICROSOFT: 192 /* we only take a non-English name when there is nothing */ 193 /* else available in the font */ 194 /* */ 195 if ( found_win == -1 || ( rec->languageID & 0x3FF ) == 0x009 ) 196 { 197 switch ( rec->encodingID ) 198 { 199 case TT_MS_ID_SYMBOL_CS: 200 case TT_MS_ID_UNICODE_CS: 201 case TT_MS_ID_UCS_4: 202 is_english = FT_BOOL( ( rec->languageID & 0x3FF ) == 0x009 ); 203 found_win = n; 204 break; 205 206 default: 207 ; 208 } 209 } 210 break; 211 212 default: 213 ; 214 } 215 } 216 } 217 218 found_apple = found_apple_roman; 219 if ( found_apple_english >= 0 ) 220 found_apple = found_apple_english; 221 222 /* some fonts contain invalid Unicode or Macintosh formatted entries; */ 223 /* we will thus favor names encoded in Windows formats if available */ 224 /* (provided it is an English name) */ 225 /* */ 226 convert = NULL; 227 if ( found_win >= 0 && !( found_apple >= 0 && !is_english ) ) 228 { 229 rec = face->name_table.names + found_win; 230 switch ( rec->encodingID ) 231 { 232 /* all Unicode strings are encoded using UTF-16BE */ 233 case TT_MS_ID_UNICODE_CS: 234 case TT_MS_ID_SYMBOL_CS: 235 convert = tt_name_ascii_from_utf16; 236 break; 237 238 case TT_MS_ID_UCS_4: 239 /* Apparently, if this value is found in a name table entry, it is */ 240 /* documented as `full Unicode repertoire'. Experience with the */ 241 /* MsGothic font shipped with Windows Vista shows that this really */ 242 /* means UTF-16 encoded names (UCS-4 values are only used within */ 243 /* charmaps). */ 244 convert = tt_name_ascii_from_utf16; 245 break; 246 247 default: 248 ; 249 } 250 } 251 else if ( found_apple >= 0 ) 252 { 253 rec = face->name_table.names + found_apple; 254 convert = tt_name_ascii_from_other; 255 } 256 else if ( found_unicode >= 0 ) 257 { 258 rec = face->name_table.names + found_unicode; 259 convert = tt_name_ascii_from_utf16; 260 } 261 262 if ( rec && convert ) 263 { 264 if ( !rec->string ) 265 { 266 FT_Stream stream = face->name_table.stream; 267 268 269 if ( FT_QNEW_ARRAY ( rec->string, rec->stringLength ) || 270 FT_STREAM_SEEK( rec->stringOffset ) || 271 FT_STREAM_READ( rec->string, rec->stringLength ) ) 272 { 273 FT_FREE( rec->string ); 274 rec->stringLength = 0; 275 result = NULL; 276 goto Exit; 277 } 278 } 279 280 result = convert( rec, memory ); 281 } 282 283 Exit: 284 *name = result; 285 return error; 286 } 287 288 289 static FT_Encoding sfnt_find_encoding(int platform_id,int encoding_id)290 sfnt_find_encoding( int platform_id, 291 int encoding_id ) 292 { 293 typedef struct TEncoding_ 294 { 295 int platform_id; 296 int encoding_id; 297 FT_Encoding encoding; 298 299 } TEncoding; 300 301 static 302 const TEncoding tt_encodings[] = 303 { 304 { TT_PLATFORM_ISO, -1, FT_ENCODING_UNICODE }, 305 306 { TT_PLATFORM_APPLE_UNICODE, -1, FT_ENCODING_UNICODE }, 307 308 { TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, FT_ENCODING_APPLE_ROMAN }, 309 310 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, FT_ENCODING_MS_SYMBOL }, 311 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UCS_4, FT_ENCODING_UNICODE }, 312 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, FT_ENCODING_UNICODE }, 313 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS, FT_ENCODING_SJIS }, 314 { TT_PLATFORM_MICROSOFT, TT_MS_ID_PRC, FT_ENCODING_PRC }, 315 { TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5, FT_ENCODING_BIG5 }, 316 { TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG, FT_ENCODING_WANSUNG }, 317 { TT_PLATFORM_MICROSOFT, TT_MS_ID_JOHAB, FT_ENCODING_JOHAB } 318 }; 319 320 const TEncoding *cur, *limit; 321 322 323 cur = tt_encodings; 324 limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] ); 325 326 for ( ; cur < limit; cur++ ) 327 { 328 if ( cur->platform_id == platform_id ) 329 { 330 if ( cur->encoding_id == encoding_id || 331 cur->encoding_id == -1 ) 332 return cur->encoding; 333 } 334 } 335 336 return FT_ENCODING_NONE; 337 } 338 339 340 #define WRITE_USHORT( p, v ) \ 341 do \ 342 { \ 343 *(p)++ = (FT_Byte)( (v) >> 8 ); \ 344 *(p)++ = (FT_Byte)( (v) >> 0 ); \ 345 \ 346 } while ( 0 ) 347 348 #define WRITE_ULONG( p, v ) \ 349 do \ 350 { \ 351 *(p)++ = (FT_Byte)( (v) >> 24 ); \ 352 *(p)++ = (FT_Byte)( (v) >> 16 ); \ 353 *(p)++ = (FT_Byte)( (v) >> 8 ); \ 354 *(p)++ = (FT_Byte)( (v) >> 0 ); \ 355 \ 356 } while ( 0 ) 357 358 359 static void sfnt_stream_close(FT_Stream stream)360 sfnt_stream_close( FT_Stream stream ) 361 { 362 FT_Memory memory = stream->memory; 363 364 365 FT_FREE( stream->base ); 366 367 stream->size = 0; 368 stream->base = NULL; 369 stream->close = NULL; 370 } 371 372 373 FT_CALLBACK_DEF( int ) compare_offsets(const void * a,const void * b)374 compare_offsets( const void* a, 375 const void* b ) 376 { 377 WOFF_Table table1 = *(WOFF_Table*)a; 378 WOFF_Table table2 = *(WOFF_Table*)b; 379 380 FT_ULong offset1 = table1->Offset; 381 FT_ULong offset2 = table2->Offset; 382 383 384 if ( offset1 > offset2 ) 385 return 1; 386 else if ( offset1 < offset2 ) 387 return -1; 388 else 389 return 0; 390 } 391 392 393 /* Replace `face->root.stream' with a stream containing the extracted */ 394 /* SFNT of a WOFF font. */ 395 396 static FT_Error woff_open_font(FT_Stream stream,TT_Face face)397 woff_open_font( FT_Stream stream, 398 TT_Face face ) 399 { 400 FT_Memory memory = stream->memory; 401 FT_Error error = FT_Err_Ok; 402 403 WOFF_HeaderRec woff; 404 WOFF_Table tables = NULL; 405 WOFF_Table* indices = NULL; 406 407 FT_ULong woff_offset; 408 409 FT_Byte* sfnt = NULL; 410 FT_Stream sfnt_stream = NULL; 411 412 FT_Byte* sfnt_header; 413 FT_ULong sfnt_offset; 414 415 FT_Int nn; 416 FT_ULong old_tag = 0; 417 418 static const FT_Frame_Field woff_header_fields[] = 419 { 420 #undef FT_STRUCTURE 421 #define FT_STRUCTURE WOFF_HeaderRec 422 423 FT_FRAME_START( 44 ), 424 FT_FRAME_ULONG ( signature ), 425 FT_FRAME_ULONG ( flavor ), 426 FT_FRAME_ULONG ( length ), 427 FT_FRAME_USHORT( num_tables ), 428 FT_FRAME_USHORT( reserved ), 429 FT_FRAME_ULONG ( totalSfntSize ), 430 FT_FRAME_USHORT( majorVersion ), 431 FT_FRAME_USHORT( minorVersion ), 432 FT_FRAME_ULONG ( metaOffset ), 433 FT_FRAME_ULONG ( metaLength ), 434 FT_FRAME_ULONG ( metaOrigLength ), 435 FT_FRAME_ULONG ( privOffset ), 436 FT_FRAME_ULONG ( privLength ), 437 FT_FRAME_END 438 }; 439 440 441 FT_ASSERT( stream == face->root.stream ); 442 FT_ASSERT( FT_STREAM_POS() == 0 ); 443 444 if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) ) 445 return error; 446 447 /* Make sure we don't recurse back here or hit TTC code. */ 448 if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf ) 449 return FT_THROW( Invalid_Table ); 450 451 /* Miscellaneous checks. */ 452 if ( woff.length != stream->size || 453 woff.num_tables == 0 || 454 44 + woff.num_tables * 20UL >= woff.length || 455 12 + woff.num_tables * 16UL >= woff.totalSfntSize || 456 ( woff.totalSfntSize & 3 ) != 0 || 457 ( woff.metaOffset == 0 && ( woff.metaLength != 0 || 458 woff.metaOrigLength != 0 ) ) || 459 ( woff.metaLength != 0 && woff.metaOrigLength == 0 ) || 460 ( woff.privOffset == 0 && woff.privLength != 0 ) ) 461 { 462 FT_ERROR(( "woff_font_open: invalid WOFF header\n" )); 463 return FT_THROW( Invalid_Table ); 464 } 465 466 /* Don't trust `totalSfntSize' before thorough checks. */ 467 if ( FT_ALLOC( sfnt, 12 + woff.num_tables * 16UL ) || 468 FT_NEW( sfnt_stream ) ) 469 goto Exit; 470 471 sfnt_header = sfnt; 472 473 /* Write sfnt header. */ 474 { 475 FT_UInt searchRange, entrySelector, rangeShift, x; 476 477 478 x = woff.num_tables; 479 entrySelector = 0; 480 while ( x ) 481 { 482 x >>= 1; 483 entrySelector += 1; 484 } 485 entrySelector--; 486 487 searchRange = ( 1 << entrySelector ) * 16; 488 rangeShift = woff.num_tables * 16 - searchRange; 489 490 WRITE_ULONG ( sfnt_header, woff.flavor ); 491 WRITE_USHORT( sfnt_header, woff.num_tables ); 492 WRITE_USHORT( sfnt_header, searchRange ); 493 WRITE_USHORT( sfnt_header, entrySelector ); 494 WRITE_USHORT( sfnt_header, rangeShift ); 495 } 496 497 /* While the entries in the sfnt header must be sorted by the */ 498 /* tag value, the tables themselves are not. We thus have to */ 499 /* sort them by offset and check that they don't overlap. */ 500 501 if ( FT_NEW_ARRAY( tables, woff.num_tables ) || 502 FT_NEW_ARRAY( indices, woff.num_tables ) ) 503 goto Exit; 504 505 FT_TRACE2(( "\n" 506 " tag offset compLen origLen checksum\n" 507 " -------------------------------------------\n" )); 508 509 if ( FT_FRAME_ENTER( 20L * woff.num_tables ) ) 510 goto Exit; 511 512 for ( nn = 0; nn < woff.num_tables; nn++ ) 513 { 514 WOFF_Table table = tables + nn; 515 516 table->Tag = FT_GET_TAG4(); 517 table->Offset = FT_GET_ULONG(); 518 table->CompLength = FT_GET_ULONG(); 519 table->OrigLength = FT_GET_ULONG(); 520 table->CheckSum = FT_GET_ULONG(); 521 522 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx %08lx\n", 523 (FT_Char)( table->Tag >> 24 ), 524 (FT_Char)( table->Tag >> 16 ), 525 (FT_Char)( table->Tag >> 8 ), 526 (FT_Char)( table->Tag ), 527 table->Offset, 528 table->CompLength, 529 table->OrigLength, 530 table->CheckSum )); 531 532 if ( table->Tag <= old_tag ) 533 { 534 FT_FRAME_EXIT(); 535 536 FT_ERROR(( "woff_font_open: table tags are not sorted\n" )); 537 error = FT_THROW( Invalid_Table ); 538 goto Exit; 539 } 540 541 old_tag = table->Tag; 542 indices[nn] = table; 543 } 544 545 FT_FRAME_EXIT(); 546 547 /* Sort by offset. */ 548 549 ft_qsort( indices, 550 woff.num_tables, 551 sizeof ( WOFF_Table ), 552 compare_offsets ); 553 554 /* Check offsets and lengths. */ 555 556 woff_offset = 44 + woff.num_tables * 20L; 557 sfnt_offset = 12 + woff.num_tables * 16L; 558 559 for ( nn = 0; nn < woff.num_tables; nn++ ) 560 { 561 WOFF_Table table = indices[nn]; 562 563 564 if ( table->Offset != woff_offset || 565 table->CompLength > woff.length || 566 table->Offset > woff.length - table->CompLength || 567 table->OrigLength > woff.totalSfntSize || 568 sfnt_offset > woff.totalSfntSize - table->OrigLength || 569 table->CompLength > table->OrigLength ) 570 { 571 FT_ERROR(( "woff_font_open: invalid table offsets\n" )); 572 error = FT_THROW( Invalid_Table ); 573 goto Exit; 574 } 575 576 table->OrigOffset = sfnt_offset; 577 578 /* The offsets must be multiples of 4. */ 579 woff_offset += ( table->CompLength + 3 ) & ~3U; 580 sfnt_offset += ( table->OrigLength + 3 ) & ~3U; 581 } 582 583 /* 584 * Final checks! 585 * 586 * We don't decode and check the metadata block. 587 * We don't check table checksums either. 588 * But other than those, I think we implement all 589 * `MUST' checks from the spec. 590 */ 591 592 if ( woff.metaOffset ) 593 { 594 if ( woff.metaOffset != woff_offset || 595 woff.metaOffset + woff.metaLength > woff.length ) 596 { 597 FT_ERROR(( "woff_font_open:" 598 " invalid `metadata' offset or length\n" )); 599 error = FT_THROW( Invalid_Table ); 600 goto Exit; 601 } 602 603 /* We have padding only ... */ 604 woff_offset += woff.metaLength; 605 } 606 607 if ( woff.privOffset ) 608 { 609 /* ... if it isn't the last block. */ 610 woff_offset = ( woff_offset + 3 ) & ~3U; 611 612 if ( woff.privOffset != woff_offset || 613 woff.privOffset + woff.privLength > woff.length ) 614 { 615 FT_ERROR(( "woff_font_open: invalid `private' offset or length\n" )); 616 error = FT_THROW( Invalid_Table ); 617 goto Exit; 618 } 619 620 /* No padding for the last block. */ 621 woff_offset += woff.privLength; 622 } 623 624 if ( sfnt_offset != woff.totalSfntSize || 625 woff_offset != woff.length ) 626 { 627 FT_ERROR(( "woff_font_open: invalid `sfnt' table structure\n" )); 628 error = FT_THROW( Invalid_Table ); 629 goto Exit; 630 } 631 632 /* Now use `totalSfntSize'. */ 633 if ( FT_REALLOC( sfnt, 634 12 + woff.num_tables * 16UL, 635 woff.totalSfntSize ) ) 636 goto Exit; 637 638 sfnt_header = sfnt + 12; 639 640 /* Write the tables. */ 641 642 for ( nn = 0; nn < woff.num_tables; nn++ ) 643 { 644 WOFF_Table table = tables + nn; 645 646 647 /* Write SFNT table entry. */ 648 WRITE_ULONG( sfnt_header, table->Tag ); 649 WRITE_ULONG( sfnt_header, table->CheckSum ); 650 WRITE_ULONG( sfnt_header, table->OrigOffset ); 651 WRITE_ULONG( sfnt_header, table->OrigLength ); 652 653 /* Write table data. */ 654 if ( FT_STREAM_SEEK( table->Offset ) || 655 FT_FRAME_ENTER( table->CompLength ) ) 656 goto Exit; 657 658 if ( table->CompLength == table->OrigLength ) 659 { 660 /* Uncompressed data; just copy. */ 661 ft_memcpy( sfnt + table->OrigOffset, 662 stream->cursor, 663 table->OrigLength ); 664 } 665 else 666 { 667 #ifdef FT_CONFIG_OPTION_USE_ZLIB 668 669 /* Uncompress with zlib. */ 670 FT_ULong output_len = table->OrigLength; 671 672 673 error = FT_Gzip_Uncompress( memory, 674 sfnt + table->OrigOffset, &output_len, 675 stream->cursor, table->CompLength ); 676 if ( error ) 677 goto Exit; 678 if ( output_len != table->OrigLength ) 679 { 680 FT_ERROR(( "woff_font_open: compressed table length mismatch\n" )); 681 error = FT_THROW( Invalid_Table ); 682 goto Exit; 683 } 684 685 #else /* !FT_CONFIG_OPTION_USE_ZLIB */ 686 687 error = FT_THROW( Unimplemented_Feature ); 688 goto Exit; 689 690 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */ 691 } 692 693 FT_FRAME_EXIT(); 694 695 /* We don't check whether the padding bytes in the WOFF file are */ 696 /* actually '\0'. For the output, however, we do set them properly. */ 697 sfnt_offset = table->OrigOffset + table->OrigLength; 698 while ( sfnt_offset & 3 ) 699 { 700 sfnt[sfnt_offset] = '\0'; 701 sfnt_offset++; 702 } 703 } 704 705 /* Ok! Finally ready. Swap out stream and return. */ 706 FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize ); 707 sfnt_stream->memory = stream->memory; 708 sfnt_stream->close = sfnt_stream_close; 709 710 FT_Stream_Free( 711 face->root.stream, 712 ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 ); 713 714 face->root.stream = sfnt_stream; 715 716 face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM; 717 718 Exit: 719 FT_FREE( tables ); 720 FT_FREE( indices ); 721 722 if ( error ) 723 { 724 FT_FREE( sfnt ); 725 FT_Stream_Close( sfnt_stream ); 726 FT_FREE( sfnt_stream ); 727 } 728 729 return error; 730 } 731 732 733 #undef WRITE_USHORT 734 #undef WRITE_ULONG 735 736 737 /* Fill in face->ttc_header. If the font is not a TTC, it is */ 738 /* synthesized into a TTC with one offset table. */ 739 static FT_Error sfnt_open_font(FT_Stream stream,TT_Face face)740 sfnt_open_font( FT_Stream stream, 741 TT_Face face ) 742 { 743 FT_Memory memory = stream->memory; 744 FT_Error error; 745 FT_ULong tag, offset; 746 747 static const FT_Frame_Field ttc_header_fields[] = 748 { 749 #undef FT_STRUCTURE 750 #define FT_STRUCTURE TTC_HeaderRec 751 752 FT_FRAME_START( 8 ), 753 FT_FRAME_LONG( version ), 754 FT_FRAME_LONG( count ), /* this is ULong in the specs */ 755 FT_FRAME_END 756 }; 757 758 759 face->ttc_header.tag = 0; 760 face->ttc_header.version = 0; 761 face->ttc_header.count = 0; 762 763 retry: 764 offset = FT_STREAM_POS(); 765 766 if ( FT_READ_ULONG( tag ) ) 767 return error; 768 769 if ( tag == TTAG_wOFF ) 770 { 771 FT_TRACE2(( "sfnt_open_font: file is a WOFF; synthesizing SFNT\n" )); 772 773 if ( FT_STREAM_SEEK( offset ) ) 774 return error; 775 776 error = woff_open_font( stream, face ); 777 if ( error ) 778 return error; 779 780 /* Swap out stream and retry! */ 781 stream = face->root.stream; 782 goto retry; 783 } 784 785 if ( tag != 0x00010000UL && 786 tag != TTAG_ttcf && 787 tag != TTAG_OTTO && 788 tag != TTAG_true && 789 tag != TTAG_typ1 && 790 tag != 0x00020000UL ) 791 { 792 FT_TRACE2(( " not a font using the SFNT container format\n" )); 793 return FT_THROW( Unknown_File_Format ); 794 } 795 796 face->ttc_header.tag = TTAG_ttcf; 797 798 if ( tag == TTAG_ttcf ) 799 { 800 FT_Int n; 801 802 803 FT_TRACE3(( "sfnt_open_font: file is a collection\n" )); 804 805 if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) ) 806 return error; 807 808 FT_TRACE3(( " with %ld subfonts\n", 809 face->ttc_header.count )); 810 811 if ( face->ttc_header.count == 0 ) 812 return FT_THROW( Invalid_Table ); 813 814 /* a rough size estimate: let's conservatively assume that there */ 815 /* is just a single table info in each subfont header (12 + 16*1 = */ 816 /* 28 bytes), thus we have (at least) `12 + 4*count' bytes for the */ 817 /* size of the TTC header plus `28*count' bytes for all subfont */ 818 /* headers */ 819 if ( (FT_ULong)face->ttc_header.count > stream->size / ( 28 + 4 ) ) 820 return FT_THROW( Array_Too_Large ); 821 822 /* now read the offsets of each font in the file */ 823 if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) ) 824 return error; 825 826 if ( FT_FRAME_ENTER( face->ttc_header.count * 4L ) ) 827 return error; 828 829 for ( n = 0; n < face->ttc_header.count; n++ ) 830 face->ttc_header.offsets[n] = FT_GET_ULONG(); 831 832 FT_FRAME_EXIT(); 833 } 834 else 835 { 836 FT_TRACE3(( "sfnt_open_font: synthesize TTC\n" )); 837 838 face->ttc_header.version = 1 << 16; 839 face->ttc_header.count = 1; 840 841 if ( FT_NEW( face->ttc_header.offsets ) ) 842 return error; 843 844 face->ttc_header.offsets[0] = offset; 845 } 846 847 return error; 848 } 849 850 851 FT_LOCAL_DEF( FT_Error ) sfnt_init_face(FT_Stream stream,TT_Face face,FT_Int face_instance_index,FT_Int num_params,FT_Parameter * params)852 sfnt_init_face( FT_Stream stream, 853 TT_Face face, 854 FT_Int face_instance_index, 855 FT_Int num_params, 856 FT_Parameter* params ) 857 { 858 FT_Error error; 859 FT_Library library = face->root.driver->root.library; 860 SFNT_Service sfnt; 861 FT_Int face_index; 862 863 864 /* for now, parameters are unused */ 865 FT_UNUSED( num_params ); 866 FT_UNUSED( params ); 867 868 869 sfnt = (SFNT_Service)face->sfnt; 870 if ( !sfnt ) 871 { 872 sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" ); 873 if ( !sfnt ) 874 { 875 FT_ERROR(( "sfnt_init_face: cannot access `sfnt' module\n" )); 876 return FT_THROW( Missing_Module ); 877 } 878 879 face->sfnt = sfnt; 880 face->goto_table = sfnt->goto_table; 881 } 882 883 FT_FACE_FIND_GLOBAL_SERVICE( face, face->psnames, POSTSCRIPT_CMAPS ); 884 885 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 886 if ( !face->mm ) 887 { 888 /* we want the MM interface from the `truetype' module only */ 889 FT_Module tt_module = FT_Get_Module( library, "truetype" ); 890 891 892 face->mm = ft_module_get_service( tt_module, 893 FT_SERVICE_ID_MULTI_MASTERS, 894 0 ); 895 } 896 897 if ( !face->var ) 898 { 899 /* we want the metrics variations interface */ 900 /* from the `truetype' module only */ 901 FT_Module tt_module = FT_Get_Module( library, "truetype" ); 902 903 904 face->var = ft_module_get_service( tt_module, 905 FT_SERVICE_ID_METRICS_VARIATIONS, 906 0 ); 907 } 908 #endif 909 910 FT_TRACE2(( "SFNT driver\n" )); 911 912 error = sfnt_open_font( stream, face ); 913 if ( error ) 914 return error; 915 916 /* Stream may have changed in sfnt_open_font. */ 917 stream = face->root.stream; 918 919 FT_TRACE2(( "sfnt_init_face: %08p, %d\n", face, face_instance_index )); 920 921 face_index = FT_ABS( face_instance_index ) & 0xFFFF; 922 923 /* value -(N+1) requests information on index N */ 924 if ( face_instance_index < 0 ) 925 face_index--; 926 927 if ( face_index >= face->ttc_header.count ) 928 { 929 if ( face_instance_index >= 0 ) 930 return FT_THROW( Invalid_Argument ); 931 else 932 face_index = 0; 933 } 934 935 if ( FT_STREAM_SEEK( face->ttc_header.offsets[face_index] ) ) 936 return error; 937 938 /* check whether we have a valid TrueType file */ 939 error = sfnt->load_font_dir( face, stream ); 940 if ( error ) 941 return error; 942 943 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 944 { 945 FT_ULong fvar_len; 946 947 FT_ULong version; 948 FT_ULong offset; 949 950 FT_UShort num_axes; 951 FT_UShort axis_size; 952 FT_UShort num_instances; 953 FT_UShort instance_size; 954 955 FT_Int instance_index; 956 957 958 face->is_default_instance = 1; 959 960 instance_index = FT_ABS( face_instance_index ) >> 16; 961 962 /* test whether current face is a GX font with named instances */ 963 if ( face->goto_table( face, TTAG_fvar, stream, &fvar_len ) || 964 fvar_len < 20 || 965 FT_READ_ULONG( version ) || 966 FT_READ_USHORT( offset ) || 967 FT_STREAM_SKIP( 2 ) /* count_size_pairs */ || 968 FT_READ_USHORT( num_axes ) || 969 FT_READ_USHORT( axis_size ) || 970 FT_READ_USHORT( num_instances ) || 971 FT_READ_USHORT( instance_size ) ) 972 { 973 version = 0; 974 offset = 0; 975 num_axes = 0; 976 axis_size = 0; 977 num_instances = 0; 978 instance_size = 0; 979 } 980 981 /* check that the data is bound by the table length */ 982 if ( version != 0x00010000UL || 983 #if 0 984 /* fonts like `JamRegular.ttf' have an incorrect value for */ 985 /* `count_size_pairs'; since value 2 is hard-coded in `fvar' */ 986 /* version 1.0, we simply ignore it */ 987 count_size_pairs != 2 || 988 #endif 989 axis_size != 20 || 990 num_axes == 0 || 991 /* `num_axes' limit implied by 16-bit `instance_size' */ 992 num_axes > 0x3FFE || 993 !( instance_size == 4 + 4 * num_axes || 994 instance_size == 6 + 4 * num_axes ) || 995 num_instances > 0x7EFF || 996 offset + 997 axis_size * num_axes + 998 instance_size * num_instances > fvar_len ) 999 num_instances = 0; 1000 else 1001 face->variation_support |= TT_FACE_FLAG_VAR_FVAR; 1002 1003 /* we don't support Multiple Master CFFs yet */ 1004 /* note that `glyf' or `CFF2' have precedence */ 1005 if ( face->goto_table( face, TTAG_glyf, stream, 0 ) && 1006 face->goto_table( face, TTAG_CFF2, stream, 0 ) && 1007 !face->goto_table( face, TTAG_CFF, stream, 0 ) ) 1008 num_instances = 0; 1009 1010 /* we support at most 2^15 - 1 instances */ 1011 if ( num_instances >= ( 1U << 15 ) - 1 ) 1012 { 1013 if ( face_instance_index >= 0 ) 1014 return FT_THROW( Invalid_Argument ); 1015 else 1016 num_instances = 0; 1017 } 1018 1019 /* instance indices in `face_instance_index' start with index 1, */ 1020 /* thus `>' and not `>=' */ 1021 if ( instance_index > num_instances ) 1022 { 1023 if ( face_instance_index >= 0 ) 1024 return FT_THROW( Invalid_Argument ); 1025 else 1026 num_instances = 0; 1027 } 1028 1029 face->root.style_flags = (FT_Long)num_instances << 16; 1030 } 1031 #endif 1032 1033 face->root.num_faces = face->ttc_header.count; 1034 face->root.face_index = face_instance_index; 1035 1036 return error; 1037 } 1038 1039 1040 #define LOAD_( x ) \ 1041 do \ 1042 { \ 1043 FT_TRACE2(( "`" #x "' " )); \ 1044 FT_TRACE3(( "-->\n" )); \ 1045 \ 1046 error = sfnt->load_ ## x( face, stream ); \ 1047 \ 1048 FT_TRACE2(( "%s\n", ( !error ) \ 1049 ? "loaded" \ 1050 : FT_ERR_EQ( error, Table_Missing ) \ 1051 ? "missing" \ 1052 : "failed to load" )); \ 1053 FT_TRACE3(( "\n" )); \ 1054 } while ( 0 ) 1055 1056 #define LOADM_( x, vertical ) \ 1057 do \ 1058 { \ 1059 FT_TRACE2(( "`%s" #x "' ", \ 1060 vertical ? "vertical " : "" )); \ 1061 FT_TRACE3(( "-->\n" )); \ 1062 \ 1063 error = sfnt->load_ ## x( face, stream, vertical ); \ 1064 \ 1065 FT_TRACE2(( "%s\n", ( !error ) \ 1066 ? "loaded" \ 1067 : FT_ERR_EQ( error, Table_Missing ) \ 1068 ? "missing" \ 1069 : "failed to load" )); \ 1070 FT_TRACE3(( "\n" )); \ 1071 } while ( 0 ) 1072 1073 #define GET_NAME( id, field ) \ 1074 do \ 1075 { \ 1076 error = tt_face_get_name( face, TT_NAME_ID_ ## id, field ); \ 1077 if ( error ) \ 1078 goto Exit; \ 1079 } while ( 0 ) 1080 1081 1082 FT_LOCAL_DEF( FT_Error ) sfnt_load_face(FT_Stream stream,TT_Face face,FT_Int face_instance_index,FT_Int num_params,FT_Parameter * params)1083 sfnt_load_face( FT_Stream stream, 1084 TT_Face face, 1085 FT_Int face_instance_index, 1086 FT_Int num_params, 1087 FT_Parameter* params ) 1088 { 1089 FT_Error error; 1090 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES 1091 FT_Error psnames_error; 1092 #endif 1093 FT_Bool has_outline; 1094 FT_Bool is_apple_sbit; 1095 FT_Bool is_apple_sbix; 1096 FT_Bool ignore_typographic_family = FALSE; 1097 FT_Bool ignore_typographic_subfamily = FALSE; 1098 1099 SFNT_Service sfnt = (SFNT_Service)face->sfnt; 1100 1101 FT_UNUSED( face_instance_index ); 1102 1103 1104 /* Check parameters */ 1105 1106 { 1107 FT_Int i; 1108 1109 1110 for ( i = 0; i < num_params; i++ ) 1111 { 1112 if ( params[i].tag == FT_PARAM_TAG_IGNORE_TYPOGRAPHIC_FAMILY ) 1113 ignore_typographic_family = TRUE; 1114 else if ( params[i].tag == FT_PARAM_TAG_IGNORE_TYPOGRAPHIC_SUBFAMILY ) 1115 ignore_typographic_subfamily = TRUE; 1116 } 1117 } 1118 1119 /* Load tables */ 1120 1121 /* We now support two SFNT-based bitmapped font formats. They */ 1122 /* are recognized easily as they do not include a `glyf' */ 1123 /* table. */ 1124 /* */ 1125 /* The first format comes from Apple, and uses a table named */ 1126 /* `bhed' instead of `head' to store the font header (using */ 1127 /* the same format). It also doesn't include horizontal and */ 1128 /* vertical metrics tables (i.e. `hhea' and `vhea' tables are */ 1129 /* missing). */ 1130 /* */ 1131 /* The other format comes from Microsoft, and is used with */ 1132 /* WinCE/PocketPC. It looks like a standard TTF, except that */ 1133 /* it doesn't contain outlines. */ 1134 /* */ 1135 1136 FT_TRACE2(( "sfnt_load_face: %08p\n\n", face )); 1137 1138 /* do we have outlines in there? */ 1139 #ifdef FT_CONFIG_OPTION_INCREMENTAL 1140 has_outline = FT_BOOL( face->root.internal->incremental_interface || 1141 tt_face_lookup_table( face, TTAG_glyf ) || 1142 tt_face_lookup_table( face, TTAG_CFF ) || 1143 tt_face_lookup_table( face, TTAG_CFF2 ) ); 1144 #else 1145 has_outline = FT_BOOL( tt_face_lookup_table( face, TTAG_glyf ) || 1146 tt_face_lookup_table( face, TTAG_CFF ) || 1147 tt_face_lookup_table( face, TTAG_CFF2 ) ); 1148 #endif 1149 1150 is_apple_sbit = 0; 1151 is_apple_sbix = !face->goto_table( face, TTAG_sbix, stream, 0 ); 1152 1153 /* Apple 'sbix' color bitmaps are rendered scaled and then the 'glyf' 1154 * outline rendered on top. We don't support that yet, so just ignore 1155 * the 'glyf' outline and advertise it as a bitmap-only font. */ 1156 if ( is_apple_sbix ) 1157 has_outline = FALSE; 1158 1159 /* if this font doesn't contain outlines, we try to load */ 1160 /* a `bhed' table */ 1161 if ( !has_outline && sfnt->load_bhed ) 1162 { 1163 LOAD_( bhed ); 1164 is_apple_sbit = FT_BOOL( !error ); 1165 } 1166 1167 /* load the font header (`head' table) if this isn't an Apple */ 1168 /* sbit font file */ 1169 if ( !is_apple_sbit || is_apple_sbix ) 1170 { 1171 LOAD_( head ); 1172 if ( error ) 1173 goto Exit; 1174 } 1175 1176 if ( face->header.Units_Per_EM == 0 ) 1177 { 1178 error = FT_THROW( Invalid_Table ); 1179 1180 goto Exit; 1181 } 1182 1183 /* the following tables are often not present in embedded TrueType */ 1184 /* fonts within PDF documents, so don't check for them. */ 1185 LOAD_( maxp ); 1186 LOAD_( cmap ); 1187 1188 /* the following tables are optional in PCL fonts -- */ 1189 /* don't check for errors */ 1190 LOAD_( name ); 1191 LOAD_( post ); 1192 1193 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES 1194 psnames_error = error; 1195 #endif 1196 1197 /* do not load the metrics headers and tables if this is an Apple */ 1198 /* sbit font file */ 1199 if ( !is_apple_sbit ) 1200 { 1201 /* load the `hhea' and `hmtx' tables */ 1202 LOADM_( hhea, 0 ); 1203 if ( !error ) 1204 { 1205 LOADM_( hmtx, 0 ); 1206 if ( FT_ERR_EQ( error, Table_Missing ) ) 1207 { 1208 error = FT_THROW( Hmtx_Table_Missing ); 1209 1210 #ifdef FT_CONFIG_OPTION_INCREMENTAL 1211 /* If this is an incrementally loaded font and there are */ 1212 /* overriding metrics, tolerate a missing `hmtx' table. */ 1213 if ( face->root.internal->incremental_interface && 1214 face->root.internal->incremental_interface->funcs-> 1215 get_glyph_metrics ) 1216 { 1217 face->horizontal.number_Of_HMetrics = 0; 1218 error = FT_Err_Ok; 1219 } 1220 #endif 1221 } 1222 } 1223 else if ( FT_ERR_EQ( error, Table_Missing ) ) 1224 { 1225 /* No `hhea' table necessary for SFNT Mac fonts. */ 1226 if ( face->format_tag == TTAG_true ) 1227 { 1228 FT_TRACE2(( "This is an SFNT Mac font.\n" )); 1229 1230 has_outline = 0; 1231 error = FT_Err_Ok; 1232 } 1233 else 1234 { 1235 error = FT_THROW( Horiz_Header_Missing ); 1236 1237 #ifdef FT_CONFIG_OPTION_INCREMENTAL 1238 /* If this is an incrementally loaded font and there are */ 1239 /* overriding metrics, tolerate a missing `hhea' table. */ 1240 if ( face->root.internal->incremental_interface && 1241 face->root.internal->incremental_interface->funcs-> 1242 get_glyph_metrics ) 1243 { 1244 face->horizontal.number_Of_HMetrics = 0; 1245 error = FT_Err_Ok; 1246 } 1247 #endif 1248 1249 } 1250 } 1251 1252 if ( error ) 1253 goto Exit; 1254 1255 /* try to load the `vhea' and `vmtx' tables */ 1256 LOADM_( hhea, 1 ); 1257 if ( !error ) 1258 { 1259 LOADM_( hmtx, 1 ); 1260 if ( !error ) 1261 face->vertical_info = 1; 1262 } 1263 1264 if ( error && FT_ERR_NEQ( error, Table_Missing ) ) 1265 goto Exit; 1266 1267 LOAD_( os2 ); 1268 if ( error ) 1269 { 1270 /* we treat the table as missing if there are any errors */ 1271 face->os2.version = 0xFFFFU; 1272 } 1273 } 1274 1275 /* the optional tables */ 1276 1277 /* embedded bitmap support */ 1278 if ( sfnt->load_eblc ) 1279 LOAD_( eblc ); 1280 1281 /* consider the pclt, kerning, and gasp tables as optional */ 1282 LOAD_( pclt ); 1283 LOAD_( gasp ); 1284 LOAD_( kern ); 1285 1286 face->root.num_glyphs = face->max_profile.numGlyphs; 1287 1288 /* Bit 8 of the `fsSelection' field in the `OS/2' table denotes */ 1289 /* a WWS-only font face. `WWS' stands for `weight', width', and */ 1290 /* `slope', a term used by Microsoft's Windows Presentation */ 1291 /* Foundation (WPF). This flag has been introduced in version */ 1292 /* 1.5 of the OpenType specification (May 2008). */ 1293 1294 face->root.family_name = NULL; 1295 face->root.style_name = NULL; 1296 if ( face->os2.version != 0xFFFFU && face->os2.fsSelection & 256 ) 1297 { 1298 if ( !ignore_typographic_family ) 1299 GET_NAME( TYPOGRAPHIC_FAMILY, &face->root.family_name ); 1300 if ( !face->root.family_name ) 1301 GET_NAME( FONT_FAMILY, &face->root.family_name ); 1302 1303 if ( !ignore_typographic_subfamily ) 1304 GET_NAME( TYPOGRAPHIC_SUBFAMILY, &face->root.style_name ); 1305 if ( !face->root.style_name ) 1306 GET_NAME( FONT_SUBFAMILY, &face->root.style_name ); 1307 } 1308 else 1309 { 1310 GET_NAME( WWS_FAMILY, &face->root.family_name ); 1311 if ( !face->root.family_name && !ignore_typographic_family ) 1312 GET_NAME( TYPOGRAPHIC_FAMILY, &face->root.family_name ); 1313 if ( !face->root.family_name ) 1314 GET_NAME( FONT_FAMILY, &face->root.family_name ); 1315 1316 GET_NAME( WWS_SUBFAMILY, &face->root.style_name ); 1317 if ( !face->root.style_name && !ignore_typographic_subfamily ) 1318 GET_NAME( TYPOGRAPHIC_SUBFAMILY, &face->root.style_name ); 1319 if ( !face->root.style_name ) 1320 GET_NAME( FONT_SUBFAMILY, &face->root.style_name ); 1321 } 1322 1323 /* now set up root fields */ 1324 { 1325 FT_Face root = &face->root; 1326 FT_Long flags = root->face_flags; 1327 1328 1329 /*********************************************************************/ 1330 /* */ 1331 /* Compute face flags. */ 1332 /* */ 1333 if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_CBLC || 1334 face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX ) 1335 flags |= FT_FACE_FLAG_COLOR; /* color glyphs */ 1336 1337 if ( has_outline == TRUE ) 1338 flags |= FT_FACE_FLAG_SCALABLE; /* scalable outlines */ 1339 1340 /* The sfnt driver only supports bitmap fonts natively, thus we */ 1341 /* don't set FT_FACE_FLAG_HINTER. */ 1342 flags |= FT_FACE_FLAG_SFNT | /* SFNT file format */ 1343 FT_FACE_FLAG_HORIZONTAL; /* horizontal data */ 1344 1345 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES 1346 if ( !psnames_error && 1347 face->postscript.FormatType != 0x00030000L ) 1348 flags |= FT_FACE_FLAG_GLYPH_NAMES; 1349 #endif 1350 1351 /* fixed width font? */ 1352 if ( face->postscript.isFixedPitch ) 1353 flags |= FT_FACE_FLAG_FIXED_WIDTH; 1354 1355 /* vertical information? */ 1356 if ( face->vertical_info ) 1357 flags |= FT_FACE_FLAG_VERTICAL; 1358 1359 /* kerning available ? */ 1360 if ( TT_FACE_HAS_KERNING( face ) ) 1361 flags |= FT_FACE_FLAG_KERNING; 1362 1363 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1364 /* Don't bother to load the tables unless somebody asks for them. */ 1365 /* No need to do work which will (probably) not be used. */ 1366 if ( face->variation_support & TT_FACE_FLAG_VAR_FVAR ) 1367 { 1368 if ( tt_face_lookup_table( face, TTAG_glyf ) != 0 && 1369 tt_face_lookup_table( face, TTAG_gvar ) != 0 ) 1370 flags |= FT_FACE_FLAG_MULTIPLE_MASTERS; 1371 if ( tt_face_lookup_table( face, TTAG_CFF2 ) != 0 ) 1372 flags |= FT_FACE_FLAG_MULTIPLE_MASTERS; 1373 } 1374 #endif 1375 1376 root->face_flags = flags; 1377 1378 /*********************************************************************/ 1379 /* */ 1380 /* Compute style flags. */ 1381 /* */ 1382 1383 flags = 0; 1384 if ( has_outline == TRUE && face->os2.version != 0xFFFFU ) 1385 { 1386 /* We have an OS/2 table; use the `fsSelection' field. Bit 9 */ 1387 /* indicates an oblique font face. This flag has been */ 1388 /* introduced in version 1.5 of the OpenType specification. */ 1389 1390 if ( face->os2.fsSelection & 512 ) /* bit 9 */ 1391 flags |= FT_STYLE_FLAG_ITALIC; 1392 else if ( face->os2.fsSelection & 1 ) /* bit 0 */ 1393 flags |= FT_STYLE_FLAG_ITALIC; 1394 1395 if ( face->os2.fsSelection & 32 ) /* bit 5 */ 1396 flags |= FT_STYLE_FLAG_BOLD; 1397 } 1398 else 1399 { 1400 /* this is an old Mac font, use the header field */ 1401 1402 if ( face->header.Mac_Style & 1 ) 1403 flags |= FT_STYLE_FLAG_BOLD; 1404 1405 if ( face->header.Mac_Style & 2 ) 1406 flags |= FT_STYLE_FLAG_ITALIC; 1407 } 1408 1409 root->style_flags |= flags; 1410 1411 /*********************************************************************/ 1412 /* */ 1413 /* Polish the charmaps. */ 1414 /* */ 1415 /* Try to set the charmap encoding according to the platform & */ 1416 /* encoding ID of each charmap. */ 1417 /* */ 1418 1419 tt_face_build_cmaps( face ); /* ignore errors */ 1420 1421 1422 /* set the encoding fields */ 1423 { 1424 FT_Int m; 1425 1426 1427 for ( m = 0; m < root->num_charmaps; m++ ) 1428 { 1429 FT_CharMap charmap = root->charmaps[m]; 1430 1431 1432 charmap->encoding = sfnt_find_encoding( charmap->platform_id, 1433 charmap->encoding_id ); 1434 1435 #if 0 1436 if ( !root->charmap && 1437 charmap->encoding == FT_ENCODING_UNICODE ) 1438 { 1439 /* set 'root->charmap' to the first Unicode encoding we find */ 1440 root->charmap = charmap; 1441 } 1442 #endif 1443 } 1444 } 1445 1446 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 1447 1448 /* 1449 * Now allocate the root array of FT_Bitmap_Size records and 1450 * populate them. Unfortunately, it isn't possible to indicate bit 1451 * depths in the FT_Bitmap_Size record. This is a design error. 1452 */ 1453 { 1454 FT_UInt count; 1455 1456 1457 count = face->sbit_num_strikes; 1458 1459 if ( count > 0 ) 1460 { 1461 FT_Memory memory = face->root.stream->memory; 1462 FT_UShort em_size = face->header.Units_Per_EM; 1463 FT_Short avgwidth = face->os2.xAvgCharWidth; 1464 FT_Size_Metrics metrics; 1465 1466 FT_UInt* sbit_strike_map = NULL; 1467 FT_UInt strike_idx, bsize_idx; 1468 1469 1470 if ( em_size == 0 || face->os2.version == 0xFFFFU ) 1471 { 1472 avgwidth = 1; 1473 em_size = 1; 1474 } 1475 1476 /* to avoid invalid strike data in the `available_sizes' field */ 1477 /* of `FT_Face', we map `available_sizes' indices to strike */ 1478 /* indices */ 1479 if ( FT_NEW_ARRAY( root->available_sizes, count ) || 1480 FT_NEW_ARRAY( sbit_strike_map, count ) ) 1481 goto Exit; 1482 1483 bsize_idx = 0; 1484 for ( strike_idx = 0; strike_idx < count; strike_idx++ ) 1485 { 1486 FT_Bitmap_Size* bsize = root->available_sizes + bsize_idx; 1487 1488 1489 error = sfnt->load_strike_metrics( face, strike_idx, &metrics ); 1490 if ( error ) 1491 continue; 1492 1493 bsize->height = (FT_Short)( metrics.height >> 6 ); 1494 bsize->width = (FT_Short)( 1495 ( avgwidth * metrics.x_ppem + em_size / 2 ) / em_size ); 1496 1497 bsize->x_ppem = metrics.x_ppem << 6; 1498 bsize->y_ppem = metrics.y_ppem << 6; 1499 1500 /* assume 72dpi */ 1501 bsize->size = metrics.y_ppem << 6; 1502 1503 /* only use strikes with valid PPEM values */ 1504 if ( bsize->x_ppem && bsize->y_ppem ) 1505 sbit_strike_map[bsize_idx++] = strike_idx; 1506 } 1507 1508 /* reduce array size to the actually used elements */ 1509 (void)FT_RENEW_ARRAY( sbit_strike_map, count, bsize_idx ); 1510 1511 /* from now on, all strike indices are mapped */ 1512 /* using `sbit_strike_map' */ 1513 if ( bsize_idx ) 1514 { 1515 face->sbit_strike_map = sbit_strike_map; 1516 1517 root->face_flags |= FT_FACE_FLAG_FIXED_SIZES; 1518 root->num_fixed_sizes = (FT_Int)bsize_idx; 1519 } 1520 } 1521 } 1522 1523 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 1524 1525 /* a font with no bitmaps and no outlines is scalable; */ 1526 /* it has only empty glyphs then */ 1527 if ( !FT_HAS_FIXED_SIZES( root ) && !FT_IS_SCALABLE( root ) ) 1528 root->face_flags |= FT_FACE_FLAG_SCALABLE; 1529 1530 1531 /*********************************************************************/ 1532 /* */ 1533 /* Set up metrics. */ 1534 /* */ 1535 if ( FT_IS_SCALABLE( root ) ) 1536 { 1537 /* XXX What about if outline header is missing */ 1538 /* (e.g. sfnt wrapped bitmap)? */ 1539 root->bbox.xMin = face->header.xMin; 1540 root->bbox.yMin = face->header.yMin; 1541 root->bbox.xMax = face->header.xMax; 1542 root->bbox.yMax = face->header.yMax; 1543 root->units_per_EM = face->header.Units_Per_EM; 1544 1545 1546 /* XXX: Computing the ascender/descender/height is very different */ 1547 /* from what the specification tells you. Apparently, we */ 1548 /* must be careful because */ 1549 /* */ 1550 /* - not all fonts have an OS/2 table; in this case, we take */ 1551 /* the values in the horizontal header. However, these */ 1552 /* values very often are not reliable. */ 1553 /* */ 1554 /* - otherwise, the correct typographic values are in the */ 1555 /* sTypoAscender, sTypoDescender & sTypoLineGap fields. */ 1556 /* */ 1557 /* However, certain fonts have these fields set to 0. */ 1558 /* Rather, they have usWinAscent & usWinDescent correctly */ 1559 /* set (but with different values). */ 1560 /* */ 1561 /* As an example, Arial Narrow is implemented through four */ 1562 /* files ARIALN.TTF, ARIALNI.TTF, ARIALNB.TTF & ARIALNBI.TTF */ 1563 /* */ 1564 /* Strangely, all fonts have the same values in their */ 1565 /* sTypoXXX fields, except ARIALNB which sets them to 0. */ 1566 /* */ 1567 /* On the other hand, they all have different */ 1568 /* usWinAscent/Descent values -- as a conclusion, the OS/2 */ 1569 /* table cannot be used to compute the text height reliably! */ 1570 /* */ 1571 1572 /* The ascender and descender are taken from the `hhea' table. */ 1573 /* If zero, they are taken from the `OS/2' table. */ 1574 1575 root->ascender = face->horizontal.Ascender; 1576 root->descender = face->horizontal.Descender; 1577 1578 root->height = root->ascender - root->descender + 1579 face->horizontal.Line_Gap; 1580 1581 if ( !( root->ascender || root->descender ) ) 1582 { 1583 if ( face->os2.version != 0xFFFFU ) 1584 { 1585 if ( face->os2.sTypoAscender || face->os2.sTypoDescender ) 1586 { 1587 root->ascender = face->os2.sTypoAscender; 1588 root->descender = face->os2.sTypoDescender; 1589 1590 root->height = root->ascender - root->descender + 1591 face->os2.sTypoLineGap; 1592 } 1593 else 1594 { 1595 root->ascender = (FT_Short)face->os2.usWinAscent; 1596 root->descender = -(FT_Short)face->os2.usWinDescent; 1597 1598 root->height = root->ascender - root->descender; 1599 } 1600 } 1601 } 1602 1603 root->max_advance_width = 1604 (FT_Short)face->horizontal.advance_Width_Max; 1605 root->max_advance_height = 1606 (FT_Short)( face->vertical_info ? face->vertical.advance_Height_Max 1607 : root->height ); 1608 1609 /* See http://www.microsoft.com/OpenType/OTSpec/post.htm -- */ 1610 /* Adjust underline position from top edge to centre of */ 1611 /* stroke to convert TrueType meaning to FreeType meaning. */ 1612 root->underline_position = face->postscript.underlinePosition - 1613 face->postscript.underlineThickness / 2; 1614 root->underline_thickness = face->postscript.underlineThickness; 1615 } 1616 1617 } 1618 1619 Exit: 1620 FT_TRACE2(( "sfnt_load_face: done\n" )); 1621 1622 return error; 1623 } 1624 1625 1626 #undef LOAD_ 1627 #undef LOADM_ 1628 #undef GET_NAME 1629 1630 1631 FT_LOCAL_DEF( void ) sfnt_done_face(TT_Face face)1632 sfnt_done_face( TT_Face face ) 1633 { 1634 FT_Memory memory; 1635 SFNT_Service sfnt; 1636 1637 1638 if ( !face ) 1639 return; 1640 1641 memory = face->root.memory; 1642 sfnt = (SFNT_Service)face->sfnt; 1643 1644 if ( sfnt ) 1645 { 1646 /* destroy the postscript names table if it is loaded */ 1647 if ( sfnt->free_psnames ) 1648 sfnt->free_psnames( face ); 1649 1650 /* destroy the embedded bitmaps table if it is loaded */ 1651 if ( sfnt->free_eblc ) 1652 sfnt->free_eblc( face ); 1653 } 1654 1655 #ifdef TT_CONFIG_OPTION_BDF 1656 /* freeing the embedded BDF properties */ 1657 tt_face_free_bdf_props( face ); 1658 #endif 1659 1660 /* freeing the kerning table */ 1661 tt_face_done_kern( face ); 1662 1663 /* freeing the collection table */ 1664 FT_FREE( face->ttc_header.offsets ); 1665 face->ttc_header.count = 0; 1666 1667 /* freeing table directory */ 1668 FT_FREE( face->dir_tables ); 1669 face->num_tables = 0; 1670 1671 { 1672 FT_Stream stream = FT_FACE_STREAM( face ); 1673 1674 1675 /* simply release the 'cmap' table frame */ 1676 FT_FRAME_RELEASE( face->cmap_table ); 1677 face->cmap_size = 0; 1678 } 1679 1680 face->horz_metrics_size = 0; 1681 face->vert_metrics_size = 0; 1682 1683 /* freeing vertical metrics, if any */ 1684 if ( face->vertical_info ) 1685 { 1686 FT_FREE( face->vertical.long_metrics ); 1687 FT_FREE( face->vertical.short_metrics ); 1688 face->vertical_info = 0; 1689 } 1690 1691 /* freeing the gasp table */ 1692 FT_FREE( face->gasp.gaspRanges ); 1693 face->gasp.numRanges = 0; 1694 1695 /* freeing the name table */ 1696 if ( sfnt ) 1697 sfnt->free_name( face ); 1698 1699 /* freeing family and style name */ 1700 FT_FREE( face->root.family_name ); 1701 FT_FREE( face->root.style_name ); 1702 1703 /* freeing sbit size table */ 1704 FT_FREE( face->root.available_sizes ); 1705 FT_FREE( face->sbit_strike_map ); 1706 face->root.num_fixed_sizes = 0; 1707 1708 FT_FREE( face->postscript_name ); 1709 1710 face->sfnt = NULL; 1711 } 1712 1713 1714 /* END */ 1715