1 /**************************************************************************** 2 * 3 * ttload.c 4 * 5 * Load the basic TrueType tables, i.e., tables that can be either in 6 * TTF or OTF fonts (body). 7 * 8 * Copyright (C) 1996-2020 by 9 * David Turner, Robert Wilhelm, and Werner Lemberg. 10 * 11 * This file is part of the FreeType project, and may only be used, 12 * modified, and distributed under the terms of the FreeType project 13 * license, LICENSE.TXT. By continuing to use, modify, or distribute 14 * this file you indicate that you have read the license and 15 * understand and accept it fully. 16 * 17 */ 18 19 20 #include <freetype/internal/ftdebug.h> 21 #include <freetype/internal/ftstream.h> 22 #include <freetype/tttags.h> 23 #include "ttload.h" 24 25 #include "sferrors.h" 26 27 28 /************************************************************************** 29 * 30 * The macro FT_COMPONENT is used in trace mode. It is an implicit 31 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log 32 * messages during execution. 33 */ 34 #undef FT_COMPONENT 35 #define FT_COMPONENT ttload 36 37 38 /************************************************************************** 39 * 40 * @Function: 41 * tt_face_lookup_table 42 * 43 * @Description: 44 * Looks for a TrueType table by name. 45 * 46 * @Input: 47 * face :: 48 * A face object handle. 49 * 50 * tag :: 51 * The searched tag. 52 * 53 * @Return: 54 * A pointer to the table directory entry. 0 if not found. 55 */ 56 FT_LOCAL_DEF( TT_Table ) tt_face_lookup_table(TT_Face face,FT_ULong tag)57 tt_face_lookup_table( TT_Face face, 58 FT_ULong tag ) 59 { 60 TT_Table entry; 61 TT_Table limit; 62 #ifdef FT_DEBUG_LEVEL_TRACE 63 FT_Bool zero_length = FALSE; 64 #endif 65 66 67 FT_TRACE4(( "tt_face_lookup_table: %p, `%c%c%c%c' -- ", 68 (void *)face, 69 (FT_Char)( tag >> 24 ), 70 (FT_Char)( tag >> 16 ), 71 (FT_Char)( tag >> 8 ), 72 (FT_Char)( tag ) )); 73 74 entry = face->dir_tables; 75 limit = entry + face->num_tables; 76 77 for ( ; entry < limit; entry++ ) 78 { 79 /* For compatibility with Windows, we consider */ 80 /* zero-length tables the same as missing tables. */ 81 if ( entry->Tag == tag ) 82 { 83 if ( entry->Length != 0 ) 84 { 85 FT_TRACE4(( "found table.\n" )); 86 return entry; 87 } 88 #ifdef FT_DEBUG_LEVEL_TRACE 89 zero_length = TRUE; 90 #endif 91 } 92 } 93 94 #ifdef FT_DEBUG_LEVEL_TRACE 95 if ( zero_length ) 96 FT_TRACE4(( "ignoring empty table\n" )); 97 else 98 FT_TRACE4(( "could not find table\n" )); 99 #endif 100 101 return NULL; 102 } 103 104 105 /************************************************************************** 106 * 107 * @Function: 108 * tt_face_goto_table 109 * 110 * @Description: 111 * Looks for a TrueType table by name, then seek a stream to it. 112 * 113 * @Input: 114 * face :: 115 * A face object handle. 116 * 117 * tag :: 118 * The searched tag. 119 * 120 * stream :: 121 * The stream to seek when the table is found. 122 * 123 * @Output: 124 * length :: 125 * The length of the table if found, undefined otherwise. 126 * 127 * @Return: 128 * FreeType error code. 0 means success. 129 */ 130 FT_LOCAL_DEF( FT_Error ) tt_face_goto_table(TT_Face face,FT_ULong tag,FT_Stream stream,FT_ULong * length)131 tt_face_goto_table( TT_Face face, 132 FT_ULong tag, 133 FT_Stream stream, 134 FT_ULong* length ) 135 { 136 TT_Table table; 137 FT_Error error; 138 139 140 table = tt_face_lookup_table( face, tag ); 141 if ( table ) 142 { 143 if ( length ) 144 *length = table->Length; 145 146 if ( FT_STREAM_SEEK( table->Offset ) ) 147 goto Exit; 148 } 149 else 150 error = FT_THROW( Table_Missing ); 151 152 Exit: 153 return error; 154 } 155 156 157 /* Here, we */ 158 /* */ 159 /* - check that `num_tables' is valid (and adjust it if necessary); */ 160 /* also return the number of valid table entries */ 161 /* */ 162 /* - look for a `head' table, check its size, and parse it to check */ 163 /* whether its `magic' field is correctly set */ 164 /* */ 165 /* - errors (except errors returned by stream handling) */ 166 /* */ 167 /* SFNT_Err_Unknown_File_Format: */ 168 /* no table is defined in directory, it is not sfnt-wrapped */ 169 /* data */ 170 /* SFNT_Err_Table_Missing: */ 171 /* table directory is valid, but essential tables */ 172 /* (head/bhed/SING) are missing */ 173 /* */ 174 static FT_Error check_table_dir(SFNT_Header sfnt,FT_Stream stream,FT_UShort * valid)175 check_table_dir( SFNT_Header sfnt, 176 FT_Stream stream, 177 FT_UShort* valid ) 178 { 179 FT_Error error; 180 FT_UShort nn, valid_entries = 0; 181 FT_UInt has_head = 0, has_sing = 0, has_meta = 0; 182 FT_ULong offset = sfnt->offset + 12; 183 184 static const FT_Frame_Field table_dir_entry_fields[] = 185 { 186 #undef FT_STRUCTURE 187 #define FT_STRUCTURE TT_TableRec 188 189 FT_FRAME_START( 16 ), 190 FT_FRAME_ULONG( Tag ), 191 FT_FRAME_ULONG( CheckSum ), 192 FT_FRAME_ULONG( Offset ), 193 FT_FRAME_ULONG( Length ), 194 FT_FRAME_END 195 }; 196 197 198 if ( FT_STREAM_SEEK( offset ) ) 199 goto Exit; 200 201 for ( nn = 0; nn < sfnt->num_tables; nn++ ) 202 { 203 TT_TableRec table; 204 205 206 if ( FT_STREAM_READ_FIELDS( table_dir_entry_fields, &table ) ) 207 { 208 nn--; 209 FT_TRACE2(( "check_table_dir:" 210 " can read only %d table%s in font (instead of %d)\n", 211 nn, nn == 1 ? "" : "s", sfnt->num_tables )); 212 sfnt->num_tables = nn; 213 break; 214 } 215 216 /* we ignore invalid tables */ 217 218 if ( table.Offset > stream->size ) 219 { 220 FT_TRACE2(( "check_table_dir: table entry %d invalid\n", nn )); 221 continue; 222 } 223 else if ( table.Length > stream->size - table.Offset ) 224 { 225 /* Some tables have such a simple structure that clipping its */ 226 /* contents is harmless. This also makes FreeType less sensitive */ 227 /* to invalid table lengths (which programs like Acroread seem to */ 228 /* ignore in general). */ 229 230 if ( table.Tag == TTAG_hmtx || 231 table.Tag == TTAG_vmtx ) 232 valid_entries++; 233 else 234 { 235 FT_TRACE2(( "check_table_dir: table entry %d invalid\n", nn )); 236 continue; 237 } 238 } 239 else 240 valid_entries++; 241 242 if ( table.Tag == TTAG_head || table.Tag == TTAG_bhed ) 243 { 244 FT_UInt32 magic; 245 246 247 #ifndef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 248 if ( table.Tag == TTAG_head ) 249 #endif 250 has_head = 1; 251 252 /* 253 * The table length should be 0x36, but certain font tools make it 254 * 0x38, so we will just check that it is greater. 255 * 256 * Note that according to the specification, the table must be 257 * padded to 32-bit lengths, but this doesn't apply to the value of 258 * its `Length' field! 259 * 260 */ 261 if ( table.Length < 0x36 ) 262 { 263 FT_TRACE2(( "check_table_dir:" 264 " `head' or `bhed' table too small\n" )); 265 error = FT_THROW( Table_Missing ); 266 goto Exit; 267 } 268 269 if ( FT_STREAM_SEEK( table.Offset + 12 ) || 270 FT_READ_ULONG( magic ) ) 271 goto Exit; 272 273 if ( magic != 0x5F0F3CF5UL ) 274 FT_TRACE2(( "check_table_dir:" 275 " invalid magic number in `head' or `bhed' table\n")); 276 277 if ( FT_STREAM_SEEK( offset + ( nn + 1 ) * 16 ) ) 278 goto Exit; 279 } 280 else if ( table.Tag == TTAG_SING ) 281 has_sing = 1; 282 else if ( table.Tag == TTAG_META ) 283 has_meta = 1; 284 } 285 286 *valid = valid_entries; 287 288 if ( !valid_entries ) 289 { 290 FT_TRACE2(( "check_table_dir: no valid tables found\n" )); 291 error = FT_THROW( Unknown_File_Format ); 292 goto Exit; 293 } 294 295 /* if `sing' and `meta' tables are present, there is no `head' table */ 296 if ( has_head || ( has_sing && has_meta ) ) 297 { 298 error = FT_Err_Ok; 299 goto Exit; 300 } 301 else 302 { 303 FT_TRACE2(( "check_table_dir:" )); 304 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 305 FT_TRACE2(( " neither `head', `bhed', nor `sing' table found\n" )); 306 #else 307 FT_TRACE2(( " neither `head' nor `sing' table found\n" )); 308 #endif 309 error = FT_THROW( Table_Missing ); 310 } 311 312 Exit: 313 return error; 314 } 315 316 317 /************************************************************************** 318 * 319 * @Function: 320 * tt_face_load_font_dir 321 * 322 * @Description: 323 * Loads the header of a SFNT font file. 324 * 325 * @Input: 326 * face :: 327 * A handle to the target face object. 328 * 329 * stream :: 330 * The input stream. 331 * 332 * @Output: 333 * sfnt :: 334 * The SFNT header. 335 * 336 * @Return: 337 * FreeType error code. 0 means success. 338 * 339 * @Note: 340 * The stream cursor must be at the beginning of the font directory. 341 */ 342 FT_LOCAL_DEF( FT_Error ) tt_face_load_font_dir(TT_Face face,FT_Stream stream)343 tt_face_load_font_dir( TT_Face face, 344 FT_Stream stream ) 345 { 346 SFNT_HeaderRec sfnt; 347 FT_Error error; 348 FT_Memory memory = stream->memory; 349 FT_UShort nn, valid_entries = 0; 350 351 static const FT_Frame_Field offset_table_fields[] = 352 { 353 #undef FT_STRUCTURE 354 #define FT_STRUCTURE SFNT_HeaderRec 355 356 FT_FRAME_START( 8 ), 357 FT_FRAME_USHORT( num_tables ), 358 FT_FRAME_USHORT( search_range ), 359 FT_FRAME_USHORT( entry_selector ), 360 FT_FRAME_USHORT( range_shift ), 361 FT_FRAME_END 362 }; 363 364 365 FT_TRACE2(( "tt_face_load_font_dir: %p\n", (void *)face )); 366 367 /* read the offset table */ 368 369 sfnt.offset = FT_STREAM_POS(); 370 371 if ( FT_READ_ULONG( sfnt.format_tag ) || 372 FT_STREAM_READ_FIELDS( offset_table_fields, &sfnt ) ) 373 goto Exit; 374 375 /* many fonts don't have these fields set correctly */ 376 #if 0 377 if ( sfnt.search_range != 1 << ( sfnt.entry_selector + 4 ) || 378 sfnt.search_range + sfnt.range_shift != sfnt.num_tables << 4 ) 379 return FT_THROW( Unknown_File_Format ); 380 #endif 381 382 /* load the table directory */ 383 384 FT_TRACE2(( "-- Number of tables: %10u\n", sfnt.num_tables )); 385 FT_TRACE2(( "-- Format version: 0x%08lx\n", sfnt.format_tag )); 386 387 if ( sfnt.format_tag != TTAG_OTTO ) 388 { 389 /* check first */ 390 error = check_table_dir( &sfnt, stream, &valid_entries ); 391 if ( error ) 392 { 393 FT_TRACE2(( "tt_face_load_font_dir:" 394 " invalid table directory for TrueType\n" )); 395 goto Exit; 396 } 397 } 398 else 399 { 400 valid_entries = sfnt.num_tables; 401 if ( !valid_entries ) 402 { 403 FT_TRACE2(( "tt_face_load_font_dir: no valid tables found\n" )); 404 error = FT_THROW( Unknown_File_Format ); 405 goto Exit; 406 } 407 } 408 409 face->num_tables = valid_entries; 410 face->format_tag = sfnt.format_tag; 411 412 if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) ) 413 goto Exit; 414 415 if ( FT_STREAM_SEEK( sfnt.offset + 12 ) || 416 FT_FRAME_ENTER( sfnt.num_tables * 16L ) ) 417 goto Exit; 418 419 FT_TRACE2(( "\n" 420 " tag offset length checksum\n" 421 " ----------------------------------\n" )); 422 423 valid_entries = 0; 424 for ( nn = 0; nn < sfnt.num_tables; nn++ ) 425 { 426 TT_TableRec entry; 427 FT_UShort i; 428 FT_Bool duplicate; 429 430 431 entry.Tag = FT_GET_TAG4(); 432 entry.CheckSum = FT_GET_ULONG(); 433 entry.Offset = FT_GET_ULONG(); 434 entry.Length = FT_GET_ULONG(); 435 436 /* ignore invalid tables that can't be sanitized */ 437 438 if ( entry.Offset > stream->size ) 439 continue; 440 else if ( entry.Length > stream->size - entry.Offset ) 441 { 442 if ( entry.Tag == TTAG_hmtx || 443 entry.Tag == TTAG_vmtx ) 444 { 445 #ifdef FT_DEBUG_LEVEL_TRACE 446 FT_ULong old_length = entry.Length; 447 #endif 448 449 450 /* make metrics table length a multiple of 4 */ 451 entry.Length = ( stream->size - entry.Offset ) & ~3U; 452 453 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx" 454 " (sanitized; original length %08lx)", 455 (FT_Char)( entry.Tag >> 24 ), 456 (FT_Char)( entry.Tag >> 16 ), 457 (FT_Char)( entry.Tag >> 8 ), 458 (FT_Char)( entry.Tag ), 459 entry.Offset, 460 entry.Length, 461 entry.CheckSum, 462 old_length )); 463 } 464 else 465 continue; 466 } 467 #ifdef FT_DEBUG_LEVEL_TRACE 468 else 469 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx", 470 (FT_Char)( entry.Tag >> 24 ), 471 (FT_Char)( entry.Tag >> 16 ), 472 (FT_Char)( entry.Tag >> 8 ), 473 (FT_Char)( entry.Tag ), 474 entry.Offset, 475 entry.Length, 476 entry.CheckSum )); 477 #endif 478 479 /* ignore duplicate tables – the first one wins */ 480 duplicate = 0; 481 for ( i = 0; i < valid_entries; i++ ) 482 { 483 if ( face->dir_tables[i].Tag == entry.Tag ) 484 { 485 duplicate = 1; 486 break; 487 } 488 } 489 if ( duplicate ) 490 { 491 FT_TRACE2(( " (duplicate, ignored)\n" )); 492 continue; 493 } 494 else 495 { 496 FT_TRACE2(( "\n" )); 497 498 /* we finally have a valid entry */ 499 face->dir_tables[valid_entries++] = entry; 500 } 501 } 502 503 /* final adjustment to number of tables */ 504 face->num_tables = valid_entries; 505 506 FT_FRAME_EXIT(); 507 508 FT_TRACE2(( "table directory loaded\n\n" )); 509 510 Exit: 511 return error; 512 } 513 514 515 /************************************************************************** 516 * 517 * @Function: 518 * tt_face_load_any 519 * 520 * @Description: 521 * Loads any font table into client memory. 522 * 523 * @Input: 524 * face :: 525 * The face object to look for. 526 * 527 * tag :: 528 * The tag of table to load. Use the value 0 if you want 529 * to access the whole font file, else set this parameter 530 * to a valid TrueType table tag that you can forge with 531 * the MAKE_TT_TAG macro. 532 * 533 * offset :: 534 * The starting offset in the table (or the file if 535 * tag == 0). 536 * 537 * length :: 538 * The address of the decision variable: 539 * 540 * If length == NULL: 541 * Loads the whole table. Returns an error if 542 * `offset' == 0! 543 * 544 * If *length == 0: 545 * Exits immediately; returning the length of the given 546 * table or of the font file, depending on the value of 547 * `tag'. 548 * 549 * If *length != 0: 550 * Loads the next `length' bytes of table or font, 551 * starting at offset `offset' (in table or font too). 552 * 553 * @Output: 554 * buffer :: 555 * The address of target buffer. 556 * 557 * @Return: 558 * FreeType error code. 0 means success. 559 */ 560 FT_LOCAL_DEF( FT_Error ) tt_face_load_any(TT_Face face,FT_ULong tag,FT_Long offset,FT_Byte * buffer,FT_ULong * length)561 tt_face_load_any( TT_Face face, 562 FT_ULong tag, 563 FT_Long offset, 564 FT_Byte* buffer, 565 FT_ULong* length ) 566 { 567 FT_Error error; 568 FT_Stream stream; 569 TT_Table table; 570 FT_ULong size; 571 572 573 if ( tag != 0 ) 574 { 575 /* look for tag in font directory */ 576 table = tt_face_lookup_table( face, tag ); 577 if ( !table ) 578 { 579 error = FT_THROW( Table_Missing ); 580 goto Exit; 581 } 582 583 offset += table->Offset; 584 size = table->Length; 585 } 586 else 587 /* tag == 0 -- the user wants to access the font file directly */ 588 size = face->root.stream->size; 589 590 if ( length && *length == 0 ) 591 { 592 *length = size; 593 594 return FT_Err_Ok; 595 } 596 597 if ( length ) 598 size = *length; 599 600 stream = face->root.stream; 601 /* the `if' is syntactic sugar for picky compilers */ 602 if ( FT_STREAM_READ_AT( offset, buffer, size ) ) 603 goto Exit; 604 605 Exit: 606 return error; 607 } 608 609 610 /************************************************************************** 611 * 612 * @Function: 613 * tt_face_load_generic_header 614 * 615 * @Description: 616 * Loads the TrueType table `head' or `bhed'. 617 * 618 * @Input: 619 * face :: 620 * A handle to the target face object. 621 * 622 * stream :: 623 * The input stream. 624 * 625 * @Return: 626 * FreeType error code. 0 means success. 627 */ 628 static FT_Error tt_face_load_generic_header(TT_Face face,FT_Stream stream,FT_ULong tag)629 tt_face_load_generic_header( TT_Face face, 630 FT_Stream stream, 631 FT_ULong tag ) 632 { 633 FT_Error error; 634 TT_Header* header; 635 636 static const FT_Frame_Field header_fields[] = 637 { 638 #undef FT_STRUCTURE 639 #define FT_STRUCTURE TT_Header 640 641 FT_FRAME_START( 54 ), 642 FT_FRAME_ULONG ( Table_Version ), 643 FT_FRAME_ULONG ( Font_Revision ), 644 FT_FRAME_LONG ( CheckSum_Adjust ), 645 FT_FRAME_LONG ( Magic_Number ), 646 FT_FRAME_USHORT( Flags ), 647 FT_FRAME_USHORT( Units_Per_EM ), 648 FT_FRAME_ULONG ( Created[0] ), 649 FT_FRAME_ULONG ( Created[1] ), 650 FT_FRAME_ULONG ( Modified[0] ), 651 FT_FRAME_ULONG ( Modified[1] ), 652 FT_FRAME_SHORT ( xMin ), 653 FT_FRAME_SHORT ( yMin ), 654 FT_FRAME_SHORT ( xMax ), 655 FT_FRAME_SHORT ( yMax ), 656 FT_FRAME_USHORT( Mac_Style ), 657 FT_FRAME_USHORT( Lowest_Rec_PPEM ), 658 FT_FRAME_SHORT ( Font_Direction ), 659 FT_FRAME_SHORT ( Index_To_Loc_Format ), 660 FT_FRAME_SHORT ( Glyph_Data_Format ), 661 FT_FRAME_END 662 }; 663 664 665 error = face->goto_table( face, tag, stream, 0 ); 666 if ( error ) 667 goto Exit; 668 669 header = &face->header; 670 671 if ( FT_STREAM_READ_FIELDS( header_fields, header ) ) 672 goto Exit; 673 674 FT_TRACE3(( "Units per EM: %4u\n", header->Units_Per_EM )); 675 FT_TRACE3(( "IndexToLoc: %4d\n", header->Index_To_Loc_Format )); 676 677 Exit: 678 return error; 679 } 680 681 682 FT_LOCAL_DEF( FT_Error ) tt_face_load_head(TT_Face face,FT_Stream stream)683 tt_face_load_head( TT_Face face, 684 FT_Stream stream ) 685 { 686 return tt_face_load_generic_header( face, stream, TTAG_head ); 687 } 688 689 690 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 691 692 FT_LOCAL_DEF( FT_Error ) tt_face_load_bhed(TT_Face face,FT_Stream stream)693 tt_face_load_bhed( TT_Face face, 694 FT_Stream stream ) 695 { 696 return tt_face_load_generic_header( face, stream, TTAG_bhed ); 697 } 698 699 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 700 701 702 /************************************************************************** 703 * 704 * @Function: 705 * tt_face_load_maxp 706 * 707 * @Description: 708 * Loads the maximum profile into a face object. 709 * 710 * @Input: 711 * face :: 712 * A handle to the target face object. 713 * 714 * stream :: 715 * The input stream. 716 * 717 * @Return: 718 * FreeType error code. 0 means success. 719 */ 720 FT_LOCAL_DEF( FT_Error ) tt_face_load_maxp(TT_Face face,FT_Stream stream)721 tt_face_load_maxp( TT_Face face, 722 FT_Stream stream ) 723 { 724 FT_Error error; 725 TT_MaxProfile* maxProfile = &face->max_profile; 726 727 static const FT_Frame_Field maxp_fields[] = 728 { 729 #undef FT_STRUCTURE 730 #define FT_STRUCTURE TT_MaxProfile 731 732 FT_FRAME_START( 6 ), 733 FT_FRAME_LONG ( version ), 734 FT_FRAME_USHORT( numGlyphs ), 735 FT_FRAME_END 736 }; 737 738 static const FT_Frame_Field maxp_fields_extra[] = 739 { 740 FT_FRAME_START( 26 ), 741 FT_FRAME_USHORT( maxPoints ), 742 FT_FRAME_USHORT( maxContours ), 743 FT_FRAME_USHORT( maxCompositePoints ), 744 FT_FRAME_USHORT( maxCompositeContours ), 745 FT_FRAME_USHORT( maxZones ), 746 FT_FRAME_USHORT( maxTwilightPoints ), 747 FT_FRAME_USHORT( maxStorage ), 748 FT_FRAME_USHORT( maxFunctionDefs ), 749 FT_FRAME_USHORT( maxInstructionDefs ), 750 FT_FRAME_USHORT( maxStackElements ), 751 FT_FRAME_USHORT( maxSizeOfInstructions ), 752 FT_FRAME_USHORT( maxComponentElements ), 753 FT_FRAME_USHORT( maxComponentDepth ), 754 FT_FRAME_END 755 }; 756 757 758 error = face->goto_table( face, TTAG_maxp, stream, 0 ); 759 if ( error ) 760 goto Exit; 761 762 if ( FT_STREAM_READ_FIELDS( maxp_fields, maxProfile ) ) 763 goto Exit; 764 765 maxProfile->maxPoints = 0; 766 maxProfile->maxContours = 0; 767 maxProfile->maxCompositePoints = 0; 768 maxProfile->maxCompositeContours = 0; 769 maxProfile->maxZones = 0; 770 maxProfile->maxTwilightPoints = 0; 771 maxProfile->maxStorage = 0; 772 maxProfile->maxFunctionDefs = 0; 773 maxProfile->maxInstructionDefs = 0; 774 maxProfile->maxStackElements = 0; 775 maxProfile->maxSizeOfInstructions = 0; 776 maxProfile->maxComponentElements = 0; 777 maxProfile->maxComponentDepth = 0; 778 779 if ( maxProfile->version >= 0x10000L ) 780 { 781 if ( FT_STREAM_READ_FIELDS( maxp_fields_extra, maxProfile ) ) 782 goto Exit; 783 784 /* XXX: an adjustment that is necessary to load certain */ 785 /* broken fonts like `Keystrokes MT' :-( */ 786 /* */ 787 /* We allocate 64 function entries by default when */ 788 /* the maxFunctionDefs value is smaller. */ 789 790 if ( maxProfile->maxFunctionDefs < 64 ) 791 maxProfile->maxFunctionDefs = 64; 792 793 /* we add 4 phantom points later */ 794 if ( maxProfile->maxTwilightPoints > ( 0xFFFFU - 4 ) ) 795 { 796 FT_TRACE0(( "tt_face_load_maxp:" 797 " too much twilight points in `maxp' table;\n" 798 " " 799 " some glyphs might be rendered incorrectly\n" )); 800 801 maxProfile->maxTwilightPoints = 0xFFFFU - 4; 802 } 803 } 804 805 FT_TRACE3(( "numGlyphs: %u\n", maxProfile->numGlyphs )); 806 807 Exit: 808 return error; 809 } 810 811 812 /************************************************************************** 813 * 814 * @Function: 815 * tt_face_load_name 816 * 817 * @Description: 818 * Loads the name records. 819 * 820 * @Input: 821 * face :: 822 * A handle to the target face object. 823 * 824 * stream :: 825 * The input stream. 826 * 827 * @Return: 828 * FreeType error code. 0 means success. 829 */ 830 FT_LOCAL_DEF( FT_Error ) tt_face_load_name(TT_Face face,FT_Stream stream)831 tt_face_load_name( TT_Face face, 832 FT_Stream stream ) 833 { 834 FT_Error error; 835 FT_Memory memory = stream->memory; 836 FT_ULong table_pos, table_len; 837 FT_ULong storage_start, storage_limit; 838 TT_NameTable table; 839 840 static const FT_Frame_Field name_table_fields[] = 841 { 842 #undef FT_STRUCTURE 843 #define FT_STRUCTURE TT_NameTableRec 844 845 FT_FRAME_START( 6 ), 846 FT_FRAME_USHORT( format ), 847 FT_FRAME_USHORT( numNameRecords ), 848 FT_FRAME_USHORT( storageOffset ), 849 FT_FRAME_END 850 }; 851 852 static const FT_Frame_Field name_record_fields[] = 853 { 854 #undef FT_STRUCTURE 855 #define FT_STRUCTURE TT_NameRec 856 857 /* no FT_FRAME_START */ 858 FT_FRAME_USHORT( platformID ), 859 FT_FRAME_USHORT( encodingID ), 860 FT_FRAME_USHORT( languageID ), 861 FT_FRAME_USHORT( nameID ), 862 FT_FRAME_USHORT( stringLength ), 863 FT_FRAME_USHORT( stringOffset ), 864 FT_FRAME_END 865 }; 866 867 static const FT_Frame_Field langTag_record_fields[] = 868 { 869 #undef FT_STRUCTURE 870 #define FT_STRUCTURE TT_LangTagRec 871 872 /* no FT_FRAME_START */ 873 FT_FRAME_USHORT( stringLength ), 874 FT_FRAME_USHORT( stringOffset ), 875 FT_FRAME_END 876 }; 877 878 879 table = &face->name_table; 880 table->stream = stream; 881 882 error = face->goto_table( face, TTAG_name, stream, &table_len ); 883 if ( error ) 884 goto Exit; 885 886 table_pos = FT_STREAM_POS(); 887 888 if ( FT_STREAM_READ_FIELDS( name_table_fields, table ) ) 889 goto Exit; 890 891 /* Some popular Asian fonts have an invalid `storageOffset' value (it */ 892 /* should be at least `6 + 12*numNameRecords'). However, the string */ 893 /* offsets, computed as `storageOffset + entry->stringOffset', are */ 894 /* valid pointers within the name table... */ 895 /* */ 896 /* We thus can't check `storageOffset' right now. */ 897 /* */ 898 storage_start = table_pos + 6 + 12 * table->numNameRecords; 899 storage_limit = table_pos + table_len; 900 901 if ( storage_start > storage_limit ) 902 { 903 FT_ERROR(( "tt_face_load_name: invalid `name' table\n" )); 904 error = FT_THROW( Name_Table_Missing ); 905 goto Exit; 906 } 907 908 /* `name' format 1 contains additional language tag records, */ 909 /* which we load first */ 910 if ( table->format == 1 ) 911 { 912 if ( FT_STREAM_SEEK( storage_start ) || 913 FT_READ_USHORT( table->numLangTagRecords ) ) 914 goto Exit; 915 916 storage_start += 2 + 4 * table->numLangTagRecords; 917 918 /* allocate language tag records array */ 919 if ( FT_NEW_ARRAY( table->langTags, table->numLangTagRecords ) || 920 FT_FRAME_ENTER( table->numLangTagRecords * 4 ) ) 921 goto Exit; 922 923 /* load language tags */ 924 { 925 TT_LangTag entry = table->langTags; 926 TT_LangTag limit = FT_OFFSET( entry, table->numLangTagRecords ); 927 928 929 for ( ; entry < limit; entry++ ) 930 { 931 (void)FT_STREAM_READ_FIELDS( langTag_record_fields, entry ); 932 933 /* check that the langTag string is within the table */ 934 entry->stringOffset += table_pos + table->storageOffset; 935 if ( entry->stringOffset < storage_start || 936 entry->stringOffset + entry->stringLength > storage_limit ) 937 { 938 /* invalid entry; ignore it */ 939 entry->stringLength = 0; 940 } 941 } 942 } 943 944 FT_FRAME_EXIT(); 945 946 (void)FT_STREAM_SEEK( table_pos + 6 ); 947 } 948 949 /* allocate name records array */ 950 if ( FT_NEW_ARRAY( table->names, table->numNameRecords ) || 951 FT_FRAME_ENTER( table->numNameRecords * 12 ) ) 952 goto Exit; 953 954 /* load name records */ 955 { 956 TT_Name entry = table->names; 957 FT_UInt count = table->numNameRecords; 958 959 960 for ( ; count > 0; count-- ) 961 { 962 if ( FT_STREAM_READ_FIELDS( name_record_fields, entry ) ) 963 continue; 964 965 /* check that the name is not empty */ 966 if ( entry->stringLength == 0 ) 967 continue; 968 969 /* check that the name string is within the table */ 970 entry->stringOffset += table_pos + table->storageOffset; 971 if ( entry->stringOffset < storage_start || 972 entry->stringOffset + entry->stringLength > storage_limit ) 973 { 974 /* invalid entry; ignore it */ 975 continue; 976 } 977 978 /* assure that we have a valid language tag ID, and */ 979 /* that the corresponding langTag entry is valid, too */ 980 if ( table->format == 1 && entry->languageID >= 0x8000U ) 981 { 982 if ( entry->languageID - 0x8000U >= table->numLangTagRecords || 983 !table->langTags[entry->languageID - 0x8000U].stringLength ) 984 { 985 /* invalid entry; ignore it */ 986 continue; 987 } 988 } 989 990 entry++; 991 } 992 993 /* reduce array size to the actually used elements */ 994 count = (FT_UInt)( entry - table->names ); 995 (void)FT_RENEW_ARRAY( table->names, 996 table->numNameRecords, 997 count ); 998 table->numNameRecords = count; 999 } 1000 1001 FT_FRAME_EXIT(); 1002 1003 /* everything went well, update face->num_names */ 1004 face->num_names = (FT_UShort)table->numNameRecords; 1005 1006 Exit: 1007 return error; 1008 } 1009 1010 1011 /************************************************************************** 1012 * 1013 * @Function: 1014 * tt_face_free_name 1015 * 1016 * @Description: 1017 * Frees the name records. 1018 * 1019 * @Input: 1020 * face :: 1021 * A handle to the target face object. 1022 */ 1023 FT_LOCAL_DEF( void ) tt_face_free_name(TT_Face face)1024 tt_face_free_name( TT_Face face ) 1025 { 1026 FT_Memory memory = face->root.driver->root.memory; 1027 TT_NameTable table = &face->name_table; 1028 1029 1030 if ( table->names ) 1031 { 1032 TT_Name entry = table->names; 1033 TT_Name limit = entry + table->numNameRecords; 1034 1035 1036 for ( ; entry < limit; entry++ ) 1037 FT_FREE( entry->string ); 1038 1039 FT_FREE( table->names ); 1040 } 1041 1042 if ( table->langTags ) 1043 { 1044 TT_LangTag entry = table->langTags; 1045 TT_LangTag limit = entry + table->numLangTagRecords; 1046 1047 1048 for ( ; entry < limit; entry++ ) 1049 FT_FREE( entry->string ); 1050 1051 FT_FREE( table->langTags ); 1052 } 1053 1054 table->numNameRecords = 0; 1055 table->numLangTagRecords = 0; 1056 table->format = 0; 1057 table->storageOffset = 0; 1058 } 1059 1060 1061 /************************************************************************** 1062 * 1063 * @Function: 1064 * tt_face_load_cmap 1065 * 1066 * @Description: 1067 * Loads the cmap directory in a face object. The cmaps themselves 1068 * are loaded on demand in the `ttcmap.c' module. 1069 * 1070 * @Input: 1071 * face :: 1072 * A handle to the target face object. 1073 * 1074 * stream :: 1075 * A handle to the input stream. 1076 * 1077 * @Return: 1078 * FreeType error code. 0 means success. 1079 */ 1080 1081 FT_LOCAL_DEF( FT_Error ) tt_face_load_cmap(TT_Face face,FT_Stream stream)1082 tt_face_load_cmap( TT_Face face, 1083 FT_Stream stream ) 1084 { 1085 FT_Error error; 1086 1087 1088 error = face->goto_table( face, TTAG_cmap, stream, &face->cmap_size ); 1089 if ( error ) 1090 goto Exit; 1091 1092 if ( FT_FRAME_EXTRACT( face->cmap_size, face->cmap_table ) ) 1093 face->cmap_size = 0; 1094 1095 Exit: 1096 return error; 1097 } 1098 1099 1100 1101 /************************************************************************** 1102 * 1103 * @Function: 1104 * tt_face_load_os2 1105 * 1106 * @Description: 1107 * Loads the OS2 table. 1108 * 1109 * @Input: 1110 * face :: 1111 * A handle to the target face object. 1112 * 1113 * stream :: 1114 * A handle to the input stream. 1115 * 1116 * @Return: 1117 * FreeType error code. 0 means success. 1118 */ 1119 FT_LOCAL_DEF( FT_Error ) tt_face_load_os2(TT_Face face,FT_Stream stream)1120 tt_face_load_os2( TT_Face face, 1121 FT_Stream stream ) 1122 { 1123 FT_Error error; 1124 TT_OS2* os2; 1125 1126 static const FT_Frame_Field os2_fields[] = 1127 { 1128 #undef FT_STRUCTURE 1129 #define FT_STRUCTURE TT_OS2 1130 1131 FT_FRAME_START( 78 ), 1132 FT_FRAME_USHORT( version ), 1133 FT_FRAME_SHORT ( xAvgCharWidth ), 1134 FT_FRAME_USHORT( usWeightClass ), 1135 FT_FRAME_USHORT( usWidthClass ), 1136 FT_FRAME_SHORT ( fsType ), 1137 FT_FRAME_SHORT ( ySubscriptXSize ), 1138 FT_FRAME_SHORT ( ySubscriptYSize ), 1139 FT_FRAME_SHORT ( ySubscriptXOffset ), 1140 FT_FRAME_SHORT ( ySubscriptYOffset ), 1141 FT_FRAME_SHORT ( ySuperscriptXSize ), 1142 FT_FRAME_SHORT ( ySuperscriptYSize ), 1143 FT_FRAME_SHORT ( ySuperscriptXOffset ), 1144 FT_FRAME_SHORT ( ySuperscriptYOffset ), 1145 FT_FRAME_SHORT ( yStrikeoutSize ), 1146 FT_FRAME_SHORT ( yStrikeoutPosition ), 1147 FT_FRAME_SHORT ( sFamilyClass ), 1148 FT_FRAME_BYTE ( panose[0] ), 1149 FT_FRAME_BYTE ( panose[1] ), 1150 FT_FRAME_BYTE ( panose[2] ), 1151 FT_FRAME_BYTE ( panose[3] ), 1152 FT_FRAME_BYTE ( panose[4] ), 1153 FT_FRAME_BYTE ( panose[5] ), 1154 FT_FRAME_BYTE ( panose[6] ), 1155 FT_FRAME_BYTE ( panose[7] ), 1156 FT_FRAME_BYTE ( panose[8] ), 1157 FT_FRAME_BYTE ( panose[9] ), 1158 FT_FRAME_ULONG ( ulUnicodeRange1 ), 1159 FT_FRAME_ULONG ( ulUnicodeRange2 ), 1160 FT_FRAME_ULONG ( ulUnicodeRange3 ), 1161 FT_FRAME_ULONG ( ulUnicodeRange4 ), 1162 FT_FRAME_BYTE ( achVendID[0] ), 1163 FT_FRAME_BYTE ( achVendID[1] ), 1164 FT_FRAME_BYTE ( achVendID[2] ), 1165 FT_FRAME_BYTE ( achVendID[3] ), 1166 1167 FT_FRAME_USHORT( fsSelection ), 1168 FT_FRAME_USHORT( usFirstCharIndex ), 1169 FT_FRAME_USHORT( usLastCharIndex ), 1170 FT_FRAME_SHORT ( sTypoAscender ), 1171 FT_FRAME_SHORT ( sTypoDescender ), 1172 FT_FRAME_SHORT ( sTypoLineGap ), 1173 FT_FRAME_USHORT( usWinAscent ), 1174 FT_FRAME_USHORT( usWinDescent ), 1175 FT_FRAME_END 1176 }; 1177 1178 /* `OS/2' version 1 and newer */ 1179 static const FT_Frame_Field os2_fields_extra1[] = 1180 { 1181 FT_FRAME_START( 8 ), 1182 FT_FRAME_ULONG( ulCodePageRange1 ), 1183 FT_FRAME_ULONG( ulCodePageRange2 ), 1184 FT_FRAME_END 1185 }; 1186 1187 /* `OS/2' version 2 and newer */ 1188 static const FT_Frame_Field os2_fields_extra2[] = 1189 { 1190 FT_FRAME_START( 10 ), 1191 FT_FRAME_SHORT ( sxHeight ), 1192 FT_FRAME_SHORT ( sCapHeight ), 1193 FT_FRAME_USHORT( usDefaultChar ), 1194 FT_FRAME_USHORT( usBreakChar ), 1195 FT_FRAME_USHORT( usMaxContext ), 1196 FT_FRAME_END 1197 }; 1198 1199 /* `OS/2' version 5 and newer */ 1200 static const FT_Frame_Field os2_fields_extra5[] = 1201 { 1202 FT_FRAME_START( 4 ), 1203 FT_FRAME_USHORT( usLowerOpticalPointSize ), 1204 FT_FRAME_USHORT( usUpperOpticalPointSize ), 1205 FT_FRAME_END 1206 }; 1207 1208 1209 /* We now support old Mac fonts where the OS/2 table doesn't */ 1210 /* exist. Simply put, we set the `version' field to 0xFFFF */ 1211 /* and test this value each time we need to access the table. */ 1212 error = face->goto_table( face, TTAG_OS2, stream, 0 ); 1213 if ( error ) 1214 goto Exit; 1215 1216 os2 = &face->os2; 1217 1218 if ( FT_STREAM_READ_FIELDS( os2_fields, os2 ) ) 1219 goto Exit; 1220 1221 os2->ulCodePageRange1 = 0; 1222 os2->ulCodePageRange2 = 0; 1223 os2->sxHeight = 0; 1224 os2->sCapHeight = 0; 1225 os2->usDefaultChar = 0; 1226 os2->usBreakChar = 0; 1227 os2->usMaxContext = 0; 1228 os2->usLowerOpticalPointSize = 0; 1229 os2->usUpperOpticalPointSize = 0xFFFF; 1230 1231 if ( os2->version >= 0x0001 ) 1232 { 1233 /* only version 1 tables */ 1234 if ( FT_STREAM_READ_FIELDS( os2_fields_extra1, os2 ) ) 1235 goto Exit; 1236 1237 if ( os2->version >= 0x0002 ) 1238 { 1239 /* only version 2 tables */ 1240 if ( FT_STREAM_READ_FIELDS( os2_fields_extra2, os2 ) ) 1241 goto Exit; 1242 1243 if ( os2->version >= 0x0005 ) 1244 { 1245 /* only version 5 tables */ 1246 if ( FT_STREAM_READ_FIELDS( os2_fields_extra5, os2 ) ) 1247 goto Exit; 1248 } 1249 } 1250 } 1251 1252 FT_TRACE3(( "sTypoAscender: %4d\n", os2->sTypoAscender )); 1253 FT_TRACE3(( "sTypoDescender: %4d\n", os2->sTypoDescender )); 1254 FT_TRACE3(( "usWinAscent: %4u\n", os2->usWinAscent )); 1255 FT_TRACE3(( "usWinDescent: %4u\n", os2->usWinDescent )); 1256 FT_TRACE3(( "fsSelection: 0x%2x\n", os2->fsSelection )); 1257 1258 Exit: 1259 return error; 1260 } 1261 1262 1263 /************************************************************************** 1264 * 1265 * @Function: 1266 * tt_face_load_postscript 1267 * 1268 * @Description: 1269 * Loads the Postscript table. 1270 * 1271 * @Input: 1272 * face :: 1273 * A handle to the target face object. 1274 * 1275 * stream :: 1276 * A handle to the input stream. 1277 * 1278 * @Return: 1279 * FreeType error code. 0 means success. 1280 */ 1281 FT_LOCAL_DEF( FT_Error ) tt_face_load_post(TT_Face face,FT_Stream stream)1282 tt_face_load_post( TT_Face face, 1283 FT_Stream stream ) 1284 { 1285 FT_Error error; 1286 TT_Postscript* post = &face->postscript; 1287 1288 static const FT_Frame_Field post_fields[] = 1289 { 1290 #undef FT_STRUCTURE 1291 #define FT_STRUCTURE TT_Postscript 1292 1293 FT_FRAME_START( 32 ), 1294 FT_FRAME_LONG ( FormatType ), 1295 FT_FRAME_LONG ( italicAngle ), 1296 FT_FRAME_SHORT( underlinePosition ), 1297 FT_FRAME_SHORT( underlineThickness ), 1298 FT_FRAME_ULONG( isFixedPitch ), 1299 FT_FRAME_ULONG( minMemType42 ), 1300 FT_FRAME_ULONG( maxMemType42 ), 1301 FT_FRAME_ULONG( minMemType1 ), 1302 FT_FRAME_ULONG( maxMemType1 ), 1303 FT_FRAME_END 1304 }; 1305 1306 1307 error = face->goto_table( face, TTAG_post, stream, 0 ); 1308 if ( error ) 1309 return error; 1310 1311 if ( FT_STREAM_READ_FIELDS( post_fields, post ) ) 1312 return error; 1313 1314 /* we don't load the glyph names, we do that in another */ 1315 /* module (ttpost). */ 1316 1317 FT_TRACE3(( "FormatType: 0x%lx\n", post->FormatType )); 1318 FT_TRACE3(( "isFixedPitch: %s\n", post->isFixedPitch 1319 ? " yes" : " no" )); 1320 1321 return FT_Err_Ok; 1322 } 1323 1324 1325 /************************************************************************** 1326 * 1327 * @Function: 1328 * tt_face_load_pclt 1329 * 1330 * @Description: 1331 * Loads the PCL 5 Table. 1332 * 1333 * @Input: 1334 * face :: 1335 * A handle to the target face object. 1336 * 1337 * stream :: 1338 * A handle to the input stream. 1339 * 1340 * @Return: 1341 * FreeType error code. 0 means success. 1342 */ 1343 FT_LOCAL_DEF( FT_Error ) tt_face_load_pclt(TT_Face face,FT_Stream stream)1344 tt_face_load_pclt( TT_Face face, 1345 FT_Stream stream ) 1346 { 1347 static const FT_Frame_Field pclt_fields[] = 1348 { 1349 #undef FT_STRUCTURE 1350 #define FT_STRUCTURE TT_PCLT 1351 1352 FT_FRAME_START( 54 ), 1353 FT_FRAME_ULONG ( Version ), 1354 FT_FRAME_ULONG ( FontNumber ), 1355 FT_FRAME_USHORT( Pitch ), 1356 FT_FRAME_USHORT( xHeight ), 1357 FT_FRAME_USHORT( Style ), 1358 FT_FRAME_USHORT( TypeFamily ), 1359 FT_FRAME_USHORT( CapHeight ), 1360 FT_FRAME_USHORT( SymbolSet ), 1361 FT_FRAME_BYTES ( TypeFace, 16 ), 1362 FT_FRAME_BYTES ( CharacterComplement, 8 ), 1363 FT_FRAME_BYTES ( FileName, 6 ), 1364 FT_FRAME_CHAR ( StrokeWeight ), 1365 FT_FRAME_CHAR ( WidthType ), 1366 FT_FRAME_BYTE ( SerifStyle ), 1367 FT_FRAME_BYTE ( Reserved ), 1368 FT_FRAME_END 1369 }; 1370 1371 FT_Error error; 1372 TT_PCLT* pclt = &face->pclt; 1373 1374 1375 /* optional table */ 1376 error = face->goto_table( face, TTAG_PCLT, stream, 0 ); 1377 if ( error ) 1378 goto Exit; 1379 1380 if ( FT_STREAM_READ_FIELDS( pclt_fields, pclt ) ) 1381 goto Exit; 1382 1383 Exit: 1384 return error; 1385 } 1386 1387 1388 /************************************************************************** 1389 * 1390 * @Function: 1391 * tt_face_load_gasp 1392 * 1393 * @Description: 1394 * Loads the `gasp' table into a face object. 1395 * 1396 * @Input: 1397 * face :: 1398 * A handle to the target face object. 1399 * 1400 * stream :: 1401 * The input stream. 1402 * 1403 * @Return: 1404 * FreeType error code. 0 means success. 1405 */ 1406 FT_LOCAL_DEF( FT_Error ) tt_face_load_gasp(TT_Face face,FT_Stream stream)1407 tt_face_load_gasp( TT_Face face, 1408 FT_Stream stream ) 1409 { 1410 FT_Error error; 1411 FT_Memory memory = stream->memory; 1412 1413 FT_UInt j,num_ranges; 1414 TT_GaspRange gaspranges = NULL; 1415 1416 1417 /* the gasp table is optional */ 1418 error = face->goto_table( face, TTAG_gasp, stream, 0 ); 1419 if ( error ) 1420 goto Exit; 1421 1422 if ( FT_FRAME_ENTER( 4L ) ) 1423 goto Exit; 1424 1425 face->gasp.version = FT_GET_USHORT(); 1426 face->gasp.numRanges = FT_GET_USHORT(); 1427 1428 FT_FRAME_EXIT(); 1429 1430 /* only support versions 0 and 1 of the table */ 1431 if ( face->gasp.version >= 2 ) 1432 { 1433 face->gasp.numRanges = 0; 1434 error = FT_THROW( Invalid_Table ); 1435 goto Exit; 1436 } 1437 1438 num_ranges = face->gasp.numRanges; 1439 FT_TRACE3(( "numRanges: %u\n", num_ranges )); 1440 1441 if ( FT_QNEW_ARRAY( face->gasp.gaspRanges, num_ranges ) || 1442 FT_FRAME_ENTER( num_ranges * 4L ) ) 1443 goto Exit; 1444 1445 gaspranges = face->gasp.gaspRanges; 1446 1447 for ( j = 0; j < num_ranges; j++ ) 1448 { 1449 gaspranges[j].maxPPEM = FT_GET_USHORT(); 1450 gaspranges[j].gaspFlag = FT_GET_USHORT(); 1451 1452 FT_TRACE3(( "gaspRange %d: rangeMaxPPEM %5d, rangeGaspBehavior 0x%x\n", 1453 j, 1454 gaspranges[j].maxPPEM, 1455 gaspranges[j].gaspFlag )); 1456 } 1457 1458 FT_FRAME_EXIT(); 1459 1460 Exit: 1461 return error; 1462 } 1463 1464 1465 /* END */ 1466