1 /***************************************************************************/ 2 /* */ 3 /* ttsbit.c */ 4 /* */ 5 /* TrueType and OpenType embedded bitmap support (body). */ 6 /* */ 7 /* Copyright 2005-2017 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 10 /* Copyright 2013 by Google, Inc. */ 11 /* Google Author(s): Behdad Esfahbod. */ 12 /* */ 13 /* This file is part of the FreeType project, and may only be used, */ 14 /* modified, and distributed under the terms of the FreeType project */ 15 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 16 /* this file you indicate that you have read the license and */ 17 /* understand and accept it fully. */ 18 /* */ 19 /***************************************************************************/ 20 21 22 #include <ft2build.h> 23 #include FT_INTERNAL_DEBUG_H 24 #include FT_INTERNAL_STREAM_H 25 #include FT_TRUETYPE_TAGS_H 26 #include FT_BITMAP_H 27 #include "ttsbit.h" 28 29 #include "sferrors.h" 30 31 #include "ttmtx.h" 32 #include "pngshim.h" 33 34 35 /*************************************************************************/ 36 /* */ 37 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 38 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 39 /* messages during execution. */ 40 /* */ 41 #undef FT_COMPONENT 42 #define FT_COMPONENT trace_ttsbit 43 44 45 FT_LOCAL_DEF( FT_Error ) 46 tt_face_load_sbit( TT_Face face, 47 FT_Stream stream ) 48 { 49 FT_Error error; 50 FT_ULong table_size; 51 FT_ULong table_start; 52 53 54 face->sbit_table = NULL; 55 face->sbit_table_size = 0; 56 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE; 57 face->sbit_num_strikes = 0; 58 59 error = face->goto_table( face, TTAG_CBLC, stream, &table_size ); 60 if ( !error ) 61 face->sbit_table_type = TT_SBIT_TABLE_TYPE_CBLC; 62 else 63 { 64 error = face->goto_table( face, TTAG_EBLC, stream, &table_size ); 65 if ( error ) 66 error = face->goto_table( face, TTAG_bloc, stream, &table_size ); 67 if ( !error ) 68 face->sbit_table_type = TT_SBIT_TABLE_TYPE_EBLC; 69 } 70 71 if ( error ) 72 { 73 error = face->goto_table( face, TTAG_sbix, stream, &table_size ); 74 if ( !error ) 75 face->sbit_table_type = TT_SBIT_TABLE_TYPE_SBIX; 76 } 77 if ( error ) 78 goto Exit; 79 80 if ( table_size < 8 ) 81 { 82 FT_ERROR(( "tt_face_load_sbit_strikes: table too short\n" )); 83 error = FT_THROW( Invalid_File_Format ); 84 goto Exit; 85 } 86 87 table_start = FT_STREAM_POS(); 88 89 switch ( (FT_UInt)face->sbit_table_type ) 90 { 91 case TT_SBIT_TABLE_TYPE_EBLC: 92 case TT_SBIT_TABLE_TYPE_CBLC: 93 { 94 FT_Byte* p; 95 FT_Fixed version; 96 FT_ULong num_strikes; 97 FT_UInt count; 98 99 100 if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) ) 101 goto Exit; 102 103 face->sbit_table_size = table_size; 104 105 p = face->sbit_table; 106 107 version = FT_NEXT_LONG( p ); 108 num_strikes = FT_NEXT_ULONG( p ); 109 110 /* there's at least one font (FZShuSong-Z01, version 3) */ 111 /* that uses the wrong byte order for the `version' field */ 112 if ( ( (FT_ULong)version & 0xFFFF0000UL ) != 0x00020000UL && 113 ( (FT_ULong)version & 0x0000FFFFUL ) != 0x00000200UL && 114 ( (FT_ULong)version & 0xFFFF0000UL ) != 0x00030000UL && 115 ( (FT_ULong)version & 0x0000FFFFUL ) != 0x00000300UL ) 116 { 117 error = FT_THROW( Unknown_File_Format ); 118 goto Exit; 119 } 120 121 if ( num_strikes >= 0x10000UL ) 122 { 123 error = FT_THROW( Invalid_File_Format ); 124 goto Exit; 125 } 126 127 /* 128 * Count the number of strikes available in the table. We are a bit 129 * paranoid there and don't trust the data. 130 */ 131 count = (FT_UInt)num_strikes; 132 if ( 8 + 48UL * count > table_size ) 133 count = (FT_UInt)( ( table_size - 8 ) / 48 ); 134 135 face->sbit_num_strikes = count; 136 } 137 break; 138 139 case TT_SBIT_TABLE_TYPE_SBIX: 140 { 141 FT_UShort version; 142 FT_UShort flags; 143 FT_ULong num_strikes; 144 FT_UInt count; 145 146 147 if ( FT_FRAME_ENTER( 8 ) ) 148 goto Exit; 149 150 version = FT_GET_USHORT(); 151 flags = FT_GET_USHORT(); 152 num_strikes = FT_GET_ULONG(); 153 154 FT_FRAME_EXIT(); 155 156 if ( version < 1 ) 157 { 158 error = FT_THROW( Unknown_File_Format ); 159 goto Exit; 160 } 161 162 /* Bit 0 must always be `1'. */ 163 /* Bit 1 controls the overlay of bitmaps with outlines. */ 164 /* All other bits should be zero. */ 165 if ( !( flags == 1 || flags == 3 ) || 166 num_strikes >= 0x10000UL ) 167 { 168 error = FT_THROW( Invalid_File_Format ); 169 goto Exit; 170 } 171 172 /* we currently don't support bit 1; however, it is better to */ 173 /* draw at least something... */ 174 if ( flags == 3 ) 175 FT_TRACE1(( "tt_face_load_sbit_strikes:" 176 " sbix overlay not supported yet\n" 177 " " 178 " expect bad rendering results\n" )); 179 180 /* 181 * Count the number of strikes available in the table. We are a bit 182 * paranoid there and don't trust the data. 183 */ 184 count = (FT_UInt)num_strikes; 185 if ( 8 + 4UL * count > table_size ) 186 count = (FT_UInt)( ( table_size - 8 ) / 4 ); 187 188 if ( FT_STREAM_SEEK( FT_STREAM_POS() - 8 ) ) 189 goto Exit; 190 191 face->sbit_table_size = 8 + count * 4; 192 if ( FT_FRAME_EXTRACT( face->sbit_table_size, face->sbit_table ) ) 193 goto Exit; 194 195 face->sbit_num_strikes = count; 196 } 197 break; 198 199 default: 200 /* we ignore unknown table formats */ 201 error = FT_THROW( Unknown_File_Format ); 202 break; 203 } 204 205 if ( !error ) 206 FT_TRACE3(( "tt_face_load_sbit_strikes: found %u strikes\n", 207 face->sbit_num_strikes )); 208 209 face->ebdt_start = 0; 210 face->ebdt_size = 0; 211 212 if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX ) 213 { 214 /* the `sbix' table is self-contained; */ 215 /* it has no associated data table */ 216 face->ebdt_start = table_start; 217 face->ebdt_size = table_size; 218 } 219 else if ( face->sbit_table_type != TT_SBIT_TABLE_TYPE_NONE ) 220 { 221 FT_ULong ebdt_size; 222 223 224 error = face->goto_table( face, TTAG_CBDT, stream, &ebdt_size ); 225 if ( error ) 226 error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size ); 227 if ( error ) 228 error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size ); 229 230 if ( !error ) 231 { 232 face->ebdt_start = FT_STREAM_POS(); 233 face->ebdt_size = ebdt_size; 234 } 235 } 236 237 if ( !face->ebdt_size ) 238 { 239 FT_TRACE2(( "tt_face_load_sbit_strikes:" 240 " no embedded bitmap data table found;\n" 241 " " 242 " resetting number of strikes to zero\n" )); 243 face->sbit_num_strikes = 0; 244 } 245 246 return FT_Err_Ok; 247 248 Exit: 249 if ( error ) 250 { 251 if ( face->sbit_table ) 252 FT_FRAME_RELEASE( face->sbit_table ); 253 face->sbit_table_size = 0; 254 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE; 255 } 256 257 return error; 258 } 259 260 261 FT_LOCAL_DEF( void ) 262 tt_face_free_sbit( TT_Face face ) 263 { 264 FT_Stream stream = face->root.stream; 265 266 267 FT_FRAME_RELEASE( face->sbit_table ); 268 face->sbit_table_size = 0; 269 face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE; 270 face->sbit_num_strikes = 0; 271 } 272 273 274 FT_LOCAL_DEF( FT_Error ) 275 tt_face_set_sbit_strike( TT_Face face, 276 FT_Size_Request req, 277 FT_ULong* astrike_index ) 278 { 279 return FT_Match_Size( (FT_Face)face, req, 0, astrike_index ); 280 } 281 282 283 FT_LOCAL_DEF( FT_Error ) 284 tt_face_load_strike_metrics( TT_Face face, 285 FT_ULong strike_index, 286 FT_Size_Metrics* metrics ) 287 { 288 /* we have to test for the existence of `sbit_strike_map' */ 289 /* because the function gets also used at the very beginning */ 290 /* to construct `sbit_strike_map' itself */ 291 if ( face->sbit_strike_map ) 292 { 293 if ( strike_index >= (FT_ULong)face->root.num_fixed_sizes ) 294 return FT_THROW( Invalid_Argument ); 295 296 /* map to real index */ 297 strike_index = face->sbit_strike_map[strike_index]; 298 } 299 else 300 { 301 if ( strike_index >= (FT_ULong)face->sbit_num_strikes ) 302 return FT_THROW( Invalid_Argument ); 303 } 304 305 switch ( (FT_UInt)face->sbit_table_type ) 306 { 307 case TT_SBIT_TABLE_TYPE_EBLC: 308 case TT_SBIT_TABLE_TYPE_CBLC: 309 { 310 FT_Byte* strike; 311 FT_Char max_before_bl; 312 FT_Char min_after_bl; 313 314 315 strike = face->sbit_table + 8 + strike_index * 48; 316 317 metrics->x_ppem = (FT_UShort)strike[44]; 318 metrics->y_ppem = (FT_UShort)strike[45]; 319 320 metrics->ascender = (FT_Char)strike[16] * 64; /* hori.ascender */ 321 metrics->descender = (FT_Char)strike[17] * 64; /* hori.descender */ 322 323 /* Due to fuzzy wording in the EBLC documentation, we find both */ 324 /* positive and negative values for `descender'. Additionally, */ 325 /* many fonts have both `ascender' and `descender' set to zero */ 326 /* (which is definitely wrong). MS Windows simply ignores all */ 327 /* those values... For these reasons we apply some heuristics */ 328 /* to get a reasonable, non-zero value for the height. */ 329 330 max_before_bl = (FT_Char)strike[24]; 331 min_after_bl = (FT_Char)strike[25]; 332 333 if ( metrics->descender > 0 ) 334 { 335 /* compare sign of descender with `min_after_bl' */ 336 if ( min_after_bl < 0 ) 337 metrics->descender = -metrics->descender; 338 } 339 340 else if ( metrics->descender == 0 ) 341 { 342 if ( metrics->ascender == 0 ) 343 { 344 FT_TRACE2(( "tt_face_load_strike_metrics:" 345 " sanitizing invalid ascender and descender\n" 346 " " 347 " values for strike %d (%dppem, %dppem)\n", 348 strike_index, 349 metrics->x_ppem, metrics->y_ppem )); 350 351 /* sanitize buggy ascender and descender values */ 352 if ( max_before_bl || min_after_bl ) 353 { 354 metrics->ascender = max_before_bl * 64; 355 metrics->descender = min_after_bl * 64; 356 } 357 else 358 { 359 metrics->ascender = metrics->y_ppem * 64; 360 metrics->descender = 0; 361 } 362 } 363 } 364 365 #if 0 366 else 367 ; /* if we have a negative descender, simply use it */ 368 #endif 369 370 metrics->height = metrics->ascender - metrics->descender; 371 if ( metrics->height == 0 ) 372 { 373 FT_TRACE2(( "tt_face_load_strike_metrics:" 374 " sanitizing invalid height value\n" 375 " " 376 " for strike (%d, %d)\n", 377 metrics->x_ppem, metrics->y_ppem )); 378 metrics->height = metrics->y_ppem * 64; 379 metrics->descender = metrics->ascender - metrics->height; 380 } 381 382 /* Is this correct? */ 383 metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB */ 384 strike[18] + /* max_width */ 385 (FT_Char)strike[23] /* min_advance_SB */ 386 ) * 64; 387 388 /* set the scale values (in 16.16 units) so advances */ 389 /* from the hmtx and vmtx table are scaled correctly */ 390 metrics->x_scale = FT_MulDiv( metrics->x_ppem, 391 64 * 0x10000, 392 face->header.Units_Per_EM ); 393 metrics->y_scale = FT_MulDiv( metrics->y_ppem, 394 64 * 0x10000, 395 face->header.Units_Per_EM ); 396 397 return FT_Err_Ok; 398 } 399 400 case TT_SBIT_TABLE_TYPE_SBIX: 401 { 402 FT_Stream stream = face->root.stream; 403 FT_UInt offset; 404 FT_UShort upem, ppem, resolution; 405 TT_HoriHeader *hori; 406 FT_Pos ppem_; /* to reduce casts */ 407 408 FT_Error error; 409 FT_Byte* p; 410 411 412 p = face->sbit_table + 8 + 4 * strike_index; 413 offset = FT_NEXT_ULONG( p ); 414 415 if ( offset + 4 > face->ebdt_size ) 416 return FT_THROW( Invalid_File_Format ); 417 418 if ( FT_STREAM_SEEK( face->ebdt_start + offset ) || 419 FT_FRAME_ENTER( 4 ) ) 420 return error; 421 422 ppem = FT_GET_USHORT(); 423 resolution = FT_GET_USHORT(); 424 425 FT_UNUSED( resolution ); /* What to do with this? */ 426 427 FT_FRAME_EXIT(); 428 429 upem = face->header.Units_Per_EM; 430 hori = &face->horizontal; 431 432 metrics->x_ppem = ppem; 433 metrics->y_ppem = ppem; 434 435 ppem_ = (FT_Pos)ppem; 436 437 metrics->ascender = 438 FT_MulDiv( hori->Ascender, ppem_ * 64, upem ); 439 metrics->descender = 440 FT_MulDiv( hori->Descender, ppem_ * 64, upem ); 441 metrics->height = 442 FT_MulDiv( hori->Ascender - hori->Descender + hori->Line_Gap, 443 ppem_ * 64, upem ); 444 metrics->max_advance = 445 FT_MulDiv( hori->advance_Width_Max, ppem_ * 64, upem ); 446 447 return error; 448 } 449 450 default: 451 return FT_THROW( Unknown_File_Format ); 452 } 453 } 454 455 456 typedef struct TT_SBitDecoderRec_ 457 { 458 TT_Face face; 459 FT_Stream stream; 460 FT_Bitmap* bitmap; 461 TT_SBit_Metrics metrics; 462 FT_Bool metrics_loaded; 463 FT_Bool bitmap_allocated; 464 FT_Byte bit_depth; 465 466 FT_ULong ebdt_start; 467 FT_ULong ebdt_size; 468 469 FT_ULong strike_index_array; 470 FT_ULong strike_index_count; 471 FT_Byte* eblc_base; 472 FT_Byte* eblc_limit; 473 474 } TT_SBitDecoderRec, *TT_SBitDecoder; 475 476 477 static FT_Error 478 tt_sbit_decoder_init( TT_SBitDecoder decoder, 479 TT_Face face, 480 FT_ULong strike_index, 481 TT_SBit_MetricsRec* metrics ) 482 { 483 FT_Error error = FT_ERR( Table_Missing ); 484 FT_Stream stream = face->root.stream; 485 486 487 strike_index = face->sbit_strike_map[strike_index]; 488 489 if ( !face->ebdt_size ) 490 goto Exit; 491 if ( FT_STREAM_SEEK( face->ebdt_start ) ) 492 goto Exit; 493 494 decoder->face = face; 495 decoder->stream = stream; 496 decoder->bitmap = &face->root.glyph->bitmap; 497 decoder->metrics = metrics; 498 499 decoder->metrics_loaded = 0; 500 decoder->bitmap_allocated = 0; 501 502 decoder->ebdt_start = face->ebdt_start; 503 decoder->ebdt_size = face->ebdt_size; 504 505 decoder->eblc_base = face->sbit_table; 506 decoder->eblc_limit = face->sbit_table + face->sbit_table_size; 507 508 /* now find the strike corresponding to the index */ 509 { 510 FT_Byte* p; 511 512 513 if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size ) 514 { 515 error = FT_THROW( Invalid_File_Format ); 516 goto Exit; 517 } 518 519 p = decoder->eblc_base + 8 + 48 * strike_index; 520 521 decoder->strike_index_array = FT_NEXT_ULONG( p ); 522 p += 4; 523 decoder->strike_index_count = FT_NEXT_ULONG( p ); 524 p += 34; 525 decoder->bit_depth = *p; 526 527 /* decoder->strike_index_array + */ 528 /* 8 * decoder->strike_index_count > face->sbit_table_size ? */ 529 if ( decoder->strike_index_array > face->sbit_table_size || 530 decoder->strike_index_count > 531 ( face->sbit_table_size - decoder->strike_index_array ) / 8 ) 532 error = FT_THROW( Invalid_File_Format ); 533 } 534 535 Exit: 536 return error; 537 } 538 539 540 static void 541 tt_sbit_decoder_done( TT_SBitDecoder decoder ) 542 { 543 FT_UNUSED( decoder ); 544 } 545 546 547 static FT_Error 548 tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder, 549 FT_Bool metrics_only ) 550 { 551 FT_Error error = FT_Err_Ok; 552 FT_UInt width, height; 553 FT_Bitmap* map = decoder->bitmap; 554 FT_ULong size; 555 556 557 if ( !decoder->metrics_loaded ) 558 { 559 error = FT_THROW( Invalid_Argument ); 560 goto Exit; 561 } 562 563 width = decoder->metrics->width; 564 height = decoder->metrics->height; 565 566 map->width = width; 567 map->rows = height; 568 569 switch ( decoder->bit_depth ) 570 { 571 case 1: 572 map->pixel_mode = FT_PIXEL_MODE_MONO; 573 map->pitch = (int)( ( map->width + 7 ) >> 3 ); 574 map->num_grays = 2; 575 break; 576 577 case 2: 578 map->pixel_mode = FT_PIXEL_MODE_GRAY2; 579 map->pitch = (int)( ( map->width + 3 ) >> 2 ); 580 map->num_grays = 4; 581 break; 582 583 case 4: 584 map->pixel_mode = FT_PIXEL_MODE_GRAY4; 585 map->pitch = (int)( ( map->width + 1 ) >> 1 ); 586 map->num_grays = 16; 587 break; 588 589 case 8: 590 map->pixel_mode = FT_PIXEL_MODE_GRAY; 591 map->pitch = (int)( map->width ); 592 map->num_grays = 256; 593 break; 594 595 case 32: 596 map->pixel_mode = FT_PIXEL_MODE_BGRA; 597 map->pitch = (int)( map->width * 4 ); 598 map->num_grays = 256; 599 break; 600 601 default: 602 error = FT_THROW( Invalid_File_Format ); 603 goto Exit; 604 } 605 606 size = map->rows * (FT_ULong)map->pitch; 607 608 /* check that there is no empty image */ 609 if ( size == 0 ) 610 goto Exit; /* exit successfully! */ 611 612 if ( metrics_only ) 613 goto Exit; /* only metrics are requested */ 614 615 error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size ); 616 if ( error ) 617 goto Exit; 618 619 decoder->bitmap_allocated = 1; 620 621 Exit: 622 return error; 623 } 624 625 626 static FT_Error 627 tt_sbit_decoder_load_metrics( TT_SBitDecoder decoder, 628 FT_Byte* *pp, 629 FT_Byte* limit, 630 FT_Bool big ) 631 { 632 FT_Byte* p = *pp; 633 TT_SBit_Metrics metrics = decoder->metrics; 634 635 636 if ( p + 5 > limit ) 637 goto Fail; 638 639 metrics->height = p[0]; 640 metrics->width = p[1]; 641 metrics->horiBearingX = (FT_Char)p[2]; 642 metrics->horiBearingY = (FT_Char)p[3]; 643 metrics->horiAdvance = p[4]; 644 645 p += 5; 646 if ( big ) 647 { 648 if ( p + 3 > limit ) 649 goto Fail; 650 651 metrics->vertBearingX = (FT_Char)p[0]; 652 metrics->vertBearingY = (FT_Char)p[1]; 653 metrics->vertAdvance = p[2]; 654 655 p += 3; 656 } 657 else 658 { 659 /* avoid uninitialized data in case there is no vertical info -- */ 660 metrics->vertBearingX = 0; 661 metrics->vertBearingY = 0; 662 metrics->vertAdvance = 0; 663 } 664 665 decoder->metrics_loaded = 1; 666 *pp = p; 667 return FT_Err_Ok; 668 669 Fail: 670 FT_TRACE1(( "tt_sbit_decoder_load_metrics: broken table\n" )); 671 return FT_THROW( Invalid_Argument ); 672 } 673 674 675 /* forward declaration */ 676 static FT_Error 677 tt_sbit_decoder_load_image( TT_SBitDecoder decoder, 678 FT_UInt glyph_index, 679 FT_Int x_pos, 680 FT_Int y_pos, 681 FT_UInt recurse_count, 682 FT_Bool metrics_only ); 683 684 typedef FT_Error (*TT_SBitDecoder_LoadFunc)( 685 TT_SBitDecoder decoder, 686 FT_Byte* p, 687 FT_Byte* plimit, 688 FT_Int x_pos, 689 FT_Int y_pos, 690 FT_UInt recurse_count ); 691 692 693 static FT_Error 694 tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder decoder, 695 FT_Byte* p, 696 FT_Byte* limit, 697 FT_Int x_pos, 698 FT_Int y_pos, 699 FT_UInt recurse_count ) 700 { 701 FT_Error error = FT_Err_Ok; 702 FT_Byte* line; 703 FT_Int pitch, width, height, line_bits, h; 704 FT_UInt bit_height, bit_width; 705 FT_Bitmap* bitmap; 706 707 FT_UNUSED( recurse_count ); 708 709 710 /* check that we can write the glyph into the bitmap */ 711 bitmap = decoder->bitmap; 712 bit_width = bitmap->width; 713 bit_height = bitmap->rows; 714 pitch = bitmap->pitch; 715 line = bitmap->buffer; 716 717 width = decoder->metrics->width; 718 height = decoder->metrics->height; 719 720 line_bits = width * decoder->bit_depth; 721 722 if ( x_pos < 0 || (FT_UInt)( x_pos + width ) > bit_width || 723 y_pos < 0 || (FT_UInt)( y_pos + height ) > bit_height ) 724 { 725 FT_TRACE1(( "tt_sbit_decoder_load_byte_aligned:" 726 " invalid bitmap dimensions\n" )); 727 error = FT_THROW( Invalid_File_Format ); 728 goto Exit; 729 } 730 731 if ( p + ( ( line_bits + 7 ) >> 3 ) * height > limit ) 732 { 733 FT_TRACE1(( "tt_sbit_decoder_load_byte_aligned: broken bitmap\n" )); 734 error = FT_THROW( Invalid_File_Format ); 735 goto Exit; 736 } 737 738 /* now do the blit */ 739 line += y_pos * pitch + ( x_pos >> 3 ); 740 x_pos &= 7; 741 742 if ( x_pos == 0 ) /* the easy one */ 743 { 744 for ( h = height; h > 0; h--, line += pitch ) 745 { 746 FT_Byte* pwrite = line; 747 FT_Int w; 748 749 750 for ( w = line_bits; w >= 8; w -= 8 ) 751 { 752 pwrite[0] = (FT_Byte)( pwrite[0] | *p++ ); 753 pwrite += 1; 754 } 755 756 if ( w > 0 ) 757 pwrite[0] = (FT_Byte)( pwrite[0] | ( *p++ & ( 0xFF00U >> w ) ) ); 758 } 759 } 760 else /* x_pos > 0 */ 761 { 762 for ( h = height; h > 0; h--, line += pitch ) 763 { 764 FT_Byte* pwrite = line; 765 FT_Int w; 766 FT_UInt wval = 0; 767 768 769 for ( w = line_bits; w >= 8; w -= 8 ) 770 { 771 wval = (FT_UInt)( wval | *p++ ); 772 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) ); 773 pwrite += 1; 774 wval <<= 8; 775 } 776 777 if ( w > 0 ) 778 wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) ); 779 780 /* all bits read and there are `x_pos + w' bits to be written */ 781 782 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) ); 783 784 if ( x_pos + w > 8 ) 785 { 786 pwrite++; 787 wval <<= 8; 788 pwrite[0] = (FT_Byte)( pwrite[0] | ( wval >> x_pos ) ); 789 } 790 } 791 } 792 793 Exit: 794 if ( !error ) 795 FT_TRACE3(( "tt_sbit_decoder_load_byte_aligned: loaded\n" )); 796 return error; 797 } 798 799 800 /* 801 * Load a bit-aligned bitmap (with pointer `p') into a line-aligned bitmap 802 * (with pointer `pwrite'). In the example below, the width is 3 pixel, 803 * and `x_pos' is 1 pixel. 804 * 805 * p p+1 806 * | | | 807 * | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |... 808 * | | | 809 * +-------+ +-------+ +-------+ ... 810 * . . . 811 * . . . 812 * v . . 813 * +-------+ . . 814 * | | . 815 * | 7 6 5 4 3 2 1 0 | . 816 * | | . 817 * pwrite . . 818 * . . 819 * v . 820 * +-------+ . 821 * | | 822 * | 7 6 5 4 3 2 1 0 | 823 * | | 824 * pwrite+1 . 825 * . 826 * v 827 * +-------+ 828 * | | 829 * | 7 6 5 4 3 2 1 0 | 830 * | | 831 * pwrite+2 832 * 833 */ 834 835 static FT_Error 836 tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder, 837 FT_Byte* p, 838 FT_Byte* limit, 839 FT_Int x_pos, 840 FT_Int y_pos, 841 FT_UInt recurse_count ) 842 { 843 FT_Error error = FT_Err_Ok; 844 FT_Byte* line; 845 FT_Int pitch, width, height, line_bits, h, nbits; 846 FT_UInt bit_height, bit_width; 847 FT_Bitmap* bitmap; 848 FT_UShort rval; 849 850 FT_UNUSED( recurse_count ); 851 852 853 /* check that we can write the glyph into the bitmap */ 854 bitmap = decoder->bitmap; 855 bit_width = bitmap->width; 856 bit_height = bitmap->rows; 857 pitch = bitmap->pitch; 858 line = bitmap->buffer; 859 860 width = decoder->metrics->width; 861 height = decoder->metrics->height; 862 863 line_bits = width * decoder->bit_depth; 864 865 if ( x_pos < 0 || (FT_UInt)( x_pos + width ) > bit_width || 866 y_pos < 0 || (FT_UInt)( y_pos + height ) > bit_height ) 867 { 868 FT_TRACE1(( "tt_sbit_decoder_load_bit_aligned:" 869 " invalid bitmap dimensions\n" )); 870 error = FT_THROW( Invalid_File_Format ); 871 goto Exit; 872 } 873 874 if ( p + ( ( line_bits * height + 7 ) >> 3 ) > limit ) 875 { 876 FT_TRACE1(( "tt_sbit_decoder_load_bit_aligned: broken bitmap\n" )); 877 error = FT_THROW( Invalid_File_Format ); 878 goto Exit; 879 } 880 881 if ( !line_bits || !height ) 882 { 883 /* nothing to do */ 884 goto Exit; 885 } 886 887 /* now do the blit */ 888 889 /* adjust `line' to point to the first byte of the bitmap */ 890 line += y_pos * pitch + ( x_pos >> 3 ); 891 x_pos &= 7; 892 893 /* the higher byte of `rval' is used as a buffer */ 894 rval = 0; 895 nbits = 0; 896 897 for ( h = height; h > 0; h--, line += pitch ) 898 { 899 FT_Byte* pwrite = line; 900 FT_Int w = line_bits; 901 902 903 /* handle initial byte (in target bitmap) specially if necessary */ 904 if ( x_pos ) 905 { 906 w = ( line_bits < 8 - x_pos ) ? line_bits : 8 - x_pos; 907 908 if ( h == height ) 909 { 910 rval = *p++; 911 nbits = x_pos; 912 } 913 else if ( nbits < w ) 914 { 915 if ( p < limit ) 916 rval |= *p++; 917 nbits += 8 - w; 918 } 919 else 920 { 921 rval >>= 8; 922 nbits -= w; 923 } 924 925 *pwrite++ |= ( ( rval >> nbits ) & 0xFF ) & 926 ( ~( 0xFFU << w ) << ( 8 - w - x_pos ) ); 927 rval <<= 8; 928 929 w = line_bits - w; 930 } 931 932 /* handle medial bytes */ 933 for ( ; w >= 8; w -= 8 ) 934 { 935 rval |= *p++; 936 *pwrite++ |= ( rval >> nbits ) & 0xFF; 937 938 rval <<= 8; 939 } 940 941 /* handle final byte if necessary */ 942 if ( w > 0 ) 943 { 944 if ( nbits < w ) 945 { 946 if ( p < limit ) 947 rval |= *p++; 948 *pwrite |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w ); 949 nbits += 8 - w; 950 951 rval <<= 8; 952 } 953 else 954 { 955 *pwrite |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w ); 956 nbits -= w; 957 } 958 } 959 } 960 961 Exit: 962 if ( !error ) 963 FT_TRACE3(( "tt_sbit_decoder_load_bit_aligned: loaded\n" )); 964 return error; 965 } 966 967 968 static FT_Error 969 tt_sbit_decoder_load_compound( TT_SBitDecoder decoder, 970 FT_Byte* p, 971 FT_Byte* limit, 972 FT_Int x_pos, 973 FT_Int y_pos, 974 FT_UInt recurse_count ) 975 { 976 FT_Error error = FT_Err_Ok; 977 FT_UInt num_components, nn; 978 979 FT_Char horiBearingX = (FT_Char)decoder->metrics->horiBearingX; 980 FT_Char horiBearingY = (FT_Char)decoder->metrics->horiBearingY; 981 FT_Byte horiAdvance = (FT_Byte)decoder->metrics->horiAdvance; 982 FT_Char vertBearingX = (FT_Char)decoder->metrics->vertBearingX; 983 FT_Char vertBearingY = (FT_Char)decoder->metrics->vertBearingY; 984 FT_Byte vertAdvance = (FT_Byte)decoder->metrics->vertAdvance; 985 986 987 if ( p + 2 > limit ) 988 goto Fail; 989 990 num_components = FT_NEXT_USHORT( p ); 991 if ( p + 4 * num_components > limit ) 992 { 993 FT_TRACE1(( "tt_sbit_decoder_load_compound: broken table\n" )); 994 goto Fail; 995 } 996 997 FT_TRACE3(( "tt_sbit_decoder_load_compound: loading %d components\n", 998 num_components )); 999 1000 for ( nn = 0; nn < num_components; nn++ ) 1001 { 1002 FT_UInt gindex = FT_NEXT_USHORT( p ); 1003 FT_Byte dx = FT_NEXT_BYTE( p ); 1004 FT_Byte dy = FT_NEXT_BYTE( p ); 1005 1006 1007 /* NB: a recursive call */ 1008 error = tt_sbit_decoder_load_image( decoder, 1009 gindex, 1010 x_pos + dx, 1011 y_pos + dy, 1012 recurse_count + 1, 1013 /* request full bitmap image */ 1014 FALSE ); 1015 if ( error ) 1016 break; 1017 } 1018 1019 FT_TRACE3(( "tt_sbit_decoder_load_compound: done\n" )); 1020 1021 decoder->metrics->horiBearingX = horiBearingX; 1022 decoder->metrics->horiBearingY = horiBearingY; 1023 decoder->metrics->horiAdvance = horiAdvance; 1024 decoder->metrics->vertBearingX = vertBearingX; 1025 decoder->metrics->vertBearingY = vertBearingY; 1026 decoder->metrics->vertAdvance = vertAdvance; 1027 decoder->metrics->width = (FT_Byte)decoder->bitmap->width; 1028 decoder->metrics->height = (FT_Byte)decoder->bitmap->rows; 1029 1030 Exit: 1031 return error; 1032 1033 Fail: 1034 error = FT_THROW( Invalid_File_Format ); 1035 goto Exit; 1036 } 1037 1038 1039 #ifdef FT_CONFIG_OPTION_USE_PNG 1040 1041 static FT_Error 1042 tt_sbit_decoder_load_png( TT_SBitDecoder decoder, 1043 FT_Byte* p, 1044 FT_Byte* limit, 1045 FT_Int x_pos, 1046 FT_Int y_pos, 1047 FT_UInt recurse_count ) 1048 { 1049 FT_Error error = FT_Err_Ok; 1050 FT_ULong png_len; 1051 1052 FT_UNUSED( recurse_count ); 1053 1054 1055 if ( limit - p < 4 ) 1056 { 1057 FT_TRACE1(( "tt_sbit_decoder_load_png: broken bitmap\n" )); 1058 error = FT_THROW( Invalid_File_Format ); 1059 goto Exit; 1060 } 1061 1062 png_len = FT_NEXT_ULONG( p ); 1063 if ( (FT_ULong)( limit - p ) < png_len ) 1064 { 1065 FT_TRACE1(( "tt_sbit_decoder_load_png: broken bitmap\n" )); 1066 error = FT_THROW( Invalid_File_Format ); 1067 goto Exit; 1068 } 1069 1070 error = Load_SBit_Png( decoder->face->root.glyph, 1071 x_pos, 1072 y_pos, 1073 decoder->bit_depth, 1074 decoder->metrics, 1075 decoder->stream->memory, 1076 p, 1077 png_len, 1078 FALSE, 1079 FALSE ); 1080 1081 Exit: 1082 if ( !error ) 1083 FT_TRACE3(( "tt_sbit_decoder_load_png: loaded\n" )); 1084 return error; 1085 } 1086 1087 #endif /* FT_CONFIG_OPTION_USE_PNG */ 1088 1089 1090 static FT_Error 1091 tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder, 1092 FT_UInt glyph_format, 1093 FT_ULong glyph_start, 1094 FT_ULong glyph_size, 1095 FT_Int x_pos, 1096 FT_Int y_pos, 1097 FT_UInt recurse_count, 1098 FT_Bool metrics_only ) 1099 { 1100 FT_Error error; 1101 FT_Stream stream = decoder->stream; 1102 FT_Byte* p; 1103 FT_Byte* p_limit; 1104 FT_Byte* data; 1105 1106 1107 /* seek into the EBDT table now */ 1108 if ( !glyph_size || 1109 glyph_start + glyph_size > decoder->ebdt_size ) 1110 { 1111 error = FT_THROW( Invalid_Argument ); 1112 goto Exit; 1113 } 1114 1115 if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) || 1116 FT_FRAME_EXTRACT( glyph_size, data ) ) 1117 goto Exit; 1118 1119 p = data; 1120 p_limit = p + glyph_size; 1121 1122 /* read the data, depending on the glyph format */ 1123 switch ( glyph_format ) 1124 { 1125 case 1: 1126 case 2: 1127 case 8: 1128 case 17: 1129 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 ); 1130 break; 1131 1132 case 6: 1133 case 7: 1134 case 9: 1135 case 18: 1136 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ); 1137 break; 1138 1139 default: 1140 error = FT_Err_Ok; 1141 } 1142 1143 if ( error ) 1144 goto Fail; 1145 1146 { 1147 TT_SBitDecoder_LoadFunc loader; 1148 1149 1150 switch ( glyph_format ) 1151 { 1152 case 1: 1153 case 6: 1154 loader = tt_sbit_decoder_load_byte_aligned; 1155 break; 1156 1157 case 2: 1158 case 7: 1159 { 1160 /* Don't trust `glyph_format'. For example, Apple's main Korean */ 1161 /* system font, `AppleMyungJo.ttf' (version 7.0d2e6), uses glyph */ 1162 /* format 7, but the data is format 6. We check whether we have */ 1163 /* an excessive number of bytes in the image: If it is equal to */ 1164 /* the value for a byte-aligned glyph, use the other loading */ 1165 /* routine. */ 1166 /* */ 1167 /* Note that for some (width,height) combinations, where the */ 1168 /* width is not a multiple of 8, the sizes for bit- and */ 1169 /* byte-aligned data are equal, for example (7,7) or (15,6). We */ 1170 /* then prefer what `glyph_format' specifies. */ 1171 1172 FT_UInt width = decoder->metrics->width; 1173 FT_UInt height = decoder->metrics->height; 1174 1175 FT_UInt bit_size = ( width * height + 7 ) >> 3; 1176 FT_UInt byte_size = height * ( ( width + 7 ) >> 3 ); 1177 1178 1179 if ( bit_size < byte_size && 1180 byte_size == (FT_UInt)( p_limit - p ) ) 1181 loader = tt_sbit_decoder_load_byte_aligned; 1182 else 1183 loader = tt_sbit_decoder_load_bit_aligned; 1184 } 1185 break; 1186 1187 case 5: 1188 loader = tt_sbit_decoder_load_bit_aligned; 1189 break; 1190 1191 case 8: 1192 if ( p + 1 > p_limit ) 1193 goto Fail; 1194 1195 p += 1; /* skip padding */ 1196 /* fall-through */ 1197 1198 case 9: 1199 loader = tt_sbit_decoder_load_compound; 1200 break; 1201 1202 case 17: /* small metrics, PNG image data */ 1203 case 18: /* big metrics, PNG image data */ 1204 case 19: /* metrics in EBLC, PNG image data */ 1205 #ifdef FT_CONFIG_OPTION_USE_PNG 1206 loader = tt_sbit_decoder_load_png; 1207 break; 1208 #else 1209 error = FT_THROW( Unimplemented_Feature ); 1210 goto Fail; 1211 #endif /* FT_CONFIG_OPTION_USE_PNG */ 1212 1213 default: 1214 error = FT_THROW( Invalid_Table ); 1215 goto Fail; 1216 } 1217 1218 if ( !decoder->bitmap_allocated ) 1219 { 1220 error = tt_sbit_decoder_alloc_bitmap( decoder, metrics_only ); 1221 1222 if ( error ) 1223 goto Fail; 1224 } 1225 1226 if ( metrics_only ) 1227 goto Fail; /* this is not an error */ 1228 1229 error = loader( decoder, p, p_limit, x_pos, y_pos, recurse_count ); 1230 } 1231 1232 Fail: 1233 FT_FRAME_RELEASE( data ); 1234 1235 Exit: 1236 return error; 1237 } 1238 1239 1240 static FT_Error 1241 tt_sbit_decoder_load_image( TT_SBitDecoder decoder, 1242 FT_UInt glyph_index, 1243 FT_Int x_pos, 1244 FT_Int y_pos, 1245 FT_UInt recurse_count, 1246 FT_Bool metrics_only ) 1247 { 1248 FT_Byte* p = decoder->eblc_base + decoder->strike_index_array; 1249 FT_Byte* p_limit = decoder->eblc_limit; 1250 FT_ULong num_ranges = decoder->strike_index_count; 1251 FT_UInt start, end, index_format, image_format; 1252 FT_ULong image_start = 0, image_end = 0, image_offset; 1253 1254 1255 /* arbitrary recursion limit */ 1256 if ( recurse_count > 100 ) 1257 { 1258 FT_TRACE4(( "tt_sbit_decoder_load_image:" 1259 " recursion depth exceeded\n" )); 1260 goto Failure; 1261 } 1262 1263 1264 /* First, we find the correct strike range that applies to this */ 1265 /* glyph index. */ 1266 for ( ; num_ranges > 0; num_ranges-- ) 1267 { 1268 start = FT_NEXT_USHORT( p ); 1269 end = FT_NEXT_USHORT( p ); 1270 1271 if ( glyph_index >= start && glyph_index <= end ) 1272 goto FoundRange; 1273 1274 p += 4; /* ignore index offset */ 1275 } 1276 goto NoBitmap; 1277 1278 FoundRange: 1279 image_offset = FT_NEXT_ULONG( p ); 1280 1281 /* overflow check */ 1282 p = decoder->eblc_base + decoder->strike_index_array; 1283 if ( image_offset > (FT_ULong)( p_limit - p ) ) 1284 goto Failure; 1285 1286 p += image_offset; 1287 if ( p + 8 > p_limit ) 1288 goto NoBitmap; 1289 1290 /* now find the glyph's location and extend within the ebdt table */ 1291 index_format = FT_NEXT_USHORT( p ); 1292 image_format = FT_NEXT_USHORT( p ); 1293 image_offset = FT_NEXT_ULONG ( p ); 1294 1295 switch ( index_format ) 1296 { 1297 case 1: /* 4-byte offsets relative to `image_offset' */ 1298 p += 4 * ( glyph_index - start ); 1299 if ( p + 8 > p_limit ) 1300 goto NoBitmap; 1301 1302 image_start = FT_NEXT_ULONG( p ); 1303 image_end = FT_NEXT_ULONG( p ); 1304 1305 if ( image_start == image_end ) /* missing glyph */ 1306 goto NoBitmap; 1307 break; 1308 1309 case 2: /* big metrics, constant image size */ 1310 { 1311 FT_ULong image_size; 1312 1313 1314 if ( p + 12 > p_limit ) 1315 goto NoBitmap; 1316 1317 image_size = FT_NEXT_ULONG( p ); 1318 1319 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) ) 1320 goto NoBitmap; 1321 1322 image_start = image_size * ( glyph_index - start ); 1323 image_end = image_start + image_size; 1324 } 1325 break; 1326 1327 case 3: /* 2-byte offsets relative to 'image_offset' */ 1328 p += 2 * ( glyph_index - start ); 1329 if ( p + 4 > p_limit ) 1330 goto NoBitmap; 1331 1332 image_start = FT_NEXT_USHORT( p ); 1333 image_end = FT_NEXT_USHORT( p ); 1334 1335 if ( image_start == image_end ) /* missing glyph */ 1336 goto NoBitmap; 1337 break; 1338 1339 case 4: /* sparse glyph array with (glyph,offset) pairs */ 1340 { 1341 FT_ULong mm, num_glyphs; 1342 1343 1344 if ( p + 4 > p_limit ) 1345 goto NoBitmap; 1346 1347 num_glyphs = FT_NEXT_ULONG( p ); 1348 1349 /* overflow check for p + ( num_glyphs + 1 ) * 4 */ 1350 if ( p + 4 > p_limit || 1351 num_glyphs > (FT_ULong)( ( ( p_limit - p ) >> 2 ) - 1 ) ) 1352 goto NoBitmap; 1353 1354 for ( mm = 0; mm < num_glyphs; mm++ ) 1355 { 1356 FT_UInt gindex = FT_NEXT_USHORT( p ); 1357 1358 1359 if ( gindex == glyph_index ) 1360 { 1361 image_start = FT_NEXT_USHORT( p ); 1362 p += 2; 1363 image_end = FT_PEEK_USHORT( p ); 1364 break; 1365 } 1366 p += 2; 1367 } 1368 1369 if ( mm >= num_glyphs ) 1370 goto NoBitmap; 1371 } 1372 break; 1373 1374 case 5: /* constant metrics with sparse glyph codes */ 1375 case 19: 1376 { 1377 FT_ULong image_size, mm, num_glyphs; 1378 1379 1380 if ( p + 16 > p_limit ) 1381 goto NoBitmap; 1382 1383 image_size = FT_NEXT_ULONG( p ); 1384 1385 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) ) 1386 goto NoBitmap; 1387 1388 num_glyphs = FT_NEXT_ULONG( p ); 1389 1390 /* overflow check for p + 2 * num_glyphs */ 1391 if ( num_glyphs > (FT_ULong)( ( p_limit - p ) >> 1 ) ) 1392 goto NoBitmap; 1393 1394 for ( mm = 0; mm < num_glyphs; mm++ ) 1395 { 1396 FT_UInt gindex = FT_NEXT_USHORT( p ); 1397 1398 1399 if ( gindex == glyph_index ) 1400 break; 1401 } 1402 1403 if ( mm >= num_glyphs ) 1404 goto NoBitmap; 1405 1406 image_start = image_size * mm; 1407 image_end = image_start + image_size; 1408 } 1409 break; 1410 1411 default: 1412 goto NoBitmap; 1413 } 1414 1415 if ( image_start > image_end ) 1416 goto NoBitmap; 1417 1418 image_end -= image_start; 1419 image_start = image_offset + image_start; 1420 1421 FT_TRACE3(( "tt_sbit_decoder_load_image:" 1422 " found sbit (format %d) for glyph index %d\n", 1423 image_format, glyph_index )); 1424 1425 return tt_sbit_decoder_load_bitmap( decoder, 1426 image_format, 1427 image_start, 1428 image_end, 1429 x_pos, 1430 y_pos, 1431 recurse_count, 1432 metrics_only ); 1433 1434 Failure: 1435 return FT_THROW( Invalid_Table ); 1436 1437 NoBitmap: 1438 FT_TRACE4(( "tt_sbit_decoder_load_image:" 1439 " no sbit found for glyph index %d\n", glyph_index )); 1440 1441 return FT_THROW( Invalid_Argument ); 1442 } 1443 1444 1445 static FT_Error 1446 tt_face_load_sbix_image( TT_Face face, 1447 FT_ULong strike_index, 1448 FT_UInt glyph_index, 1449 FT_Stream stream, 1450 FT_Bitmap *map, 1451 TT_SBit_MetricsRec *metrics, 1452 FT_Bool metrics_only ) 1453 { 1454 FT_UInt strike_offset, glyph_start, glyph_end; 1455 FT_Int originOffsetX, originOffsetY; 1456 FT_Tag graphicType; 1457 FT_Int recurse_depth = 0; 1458 1459 FT_Error error; 1460 FT_Byte* p; 1461 1462 FT_UNUSED( map ); 1463 1464 1465 strike_index = face->sbit_strike_map[strike_index]; 1466 1467 metrics->width = 0; 1468 metrics->height = 0; 1469 1470 p = face->sbit_table + 8 + 4 * strike_index; 1471 strike_offset = FT_NEXT_ULONG( p ); 1472 1473 retry: 1474 if ( glyph_index > (FT_UInt)face->root.num_glyphs ) 1475 return FT_THROW( Invalid_Argument ); 1476 1477 if ( strike_offset >= face->ebdt_size || 1478 face->ebdt_size - strike_offset < 4 + glyph_index * 4 + 8 ) 1479 return FT_THROW( Invalid_File_Format ); 1480 1481 if ( FT_STREAM_SEEK( face->ebdt_start + 1482 strike_offset + 4 + 1483 glyph_index * 4 ) || 1484 FT_FRAME_ENTER( 8 ) ) 1485 return error; 1486 1487 glyph_start = FT_GET_ULONG(); 1488 glyph_end = FT_GET_ULONG(); 1489 1490 FT_FRAME_EXIT(); 1491 1492 if ( glyph_start == glyph_end ) 1493 return FT_THROW( Invalid_Argument ); 1494 if ( glyph_start > glyph_end || 1495 glyph_end - glyph_start < 8 || 1496 face->ebdt_size - strike_offset < glyph_end ) 1497 return FT_THROW( Invalid_File_Format ); 1498 1499 if ( FT_STREAM_SEEK( face->ebdt_start + strike_offset + glyph_start ) || 1500 FT_FRAME_ENTER( glyph_end - glyph_start ) ) 1501 return error; 1502 1503 originOffsetX = FT_GET_SHORT(); 1504 originOffsetY = FT_GET_SHORT(); 1505 1506 graphicType = FT_GET_TAG4(); 1507 1508 switch ( graphicType ) 1509 { 1510 case FT_MAKE_TAG( 'd', 'u', 'p', 'e' ): 1511 if ( recurse_depth < 4 ) 1512 { 1513 glyph_index = FT_GET_USHORT(); 1514 FT_FRAME_EXIT(); 1515 recurse_depth++; 1516 goto retry; 1517 } 1518 error = FT_THROW( Invalid_File_Format ); 1519 break; 1520 1521 case FT_MAKE_TAG( 'p', 'n', 'g', ' ' ): 1522 #ifdef FT_CONFIG_OPTION_USE_PNG 1523 error = Load_SBit_Png( face->root.glyph, 1524 0, 1525 0, 1526 32, 1527 metrics, 1528 stream->memory, 1529 stream->cursor, 1530 glyph_end - glyph_start - 8, 1531 TRUE, 1532 metrics_only ); 1533 #else 1534 error = FT_THROW( Unimplemented_Feature ); 1535 #endif 1536 break; 1537 1538 case FT_MAKE_TAG( 'j', 'p', 'g', ' ' ): 1539 case FT_MAKE_TAG( 't', 'i', 'f', 'f' ): 1540 case FT_MAKE_TAG( 'r', 'g', 'b', 'l' ): /* used on iOS 7.1 */ 1541 error = FT_THROW( Unknown_File_Format ); 1542 break; 1543 1544 default: 1545 error = FT_THROW( Unimplemented_Feature ); 1546 break; 1547 } 1548 1549 FT_FRAME_EXIT(); 1550 1551 if ( !error ) 1552 { 1553 FT_Short abearing; 1554 FT_UShort aadvance; 1555 1556 1557 tt_face_get_metrics( face, FALSE, glyph_index, &abearing, &aadvance ); 1558 1559 metrics->horiBearingX = (FT_Short)originOffsetX; 1560 metrics->horiBearingY = (FT_Short)( -originOffsetY + metrics->height ); 1561 metrics->horiAdvance = (FT_UShort)( aadvance * 1562 face->root.size->metrics.x_ppem / 1563 face->header.Units_Per_EM ); 1564 } 1565 1566 return error; 1567 } 1568 1569 FT_LOCAL( FT_Error ) 1570 tt_face_load_sbit_image( TT_Face face, 1571 FT_ULong strike_index, 1572 FT_UInt glyph_index, 1573 FT_UInt load_flags, 1574 FT_Stream stream, 1575 FT_Bitmap *map, 1576 TT_SBit_MetricsRec *metrics ) 1577 { 1578 FT_Error error = FT_Err_Ok; 1579 1580 1581 switch ( (FT_UInt)face->sbit_table_type ) 1582 { 1583 case TT_SBIT_TABLE_TYPE_EBLC: 1584 case TT_SBIT_TABLE_TYPE_CBLC: 1585 { 1586 TT_SBitDecoderRec decoder[1]; 1587 1588 1589 error = tt_sbit_decoder_init( decoder, face, strike_index, metrics ); 1590 if ( !error ) 1591 { 1592 error = tt_sbit_decoder_load_image( 1593 decoder, 1594 glyph_index, 1595 0, 1596 0, 1597 0, 1598 ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 ); 1599 tt_sbit_decoder_done( decoder ); 1600 } 1601 } 1602 break; 1603 1604 case TT_SBIT_TABLE_TYPE_SBIX: 1605 error = tt_face_load_sbix_image( 1606 face, 1607 strike_index, 1608 glyph_index, 1609 stream, 1610 map, 1611 metrics, 1612 ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 ); 1613 break; 1614 1615 default: 1616 error = FT_THROW( Unknown_File_Format ); 1617 break; 1618 } 1619 1620 /* Flatten color bitmaps if color was not requested. */ 1621 if ( !error && 1622 !( load_flags & FT_LOAD_COLOR ) && 1623 !( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) && 1624 map->pixel_mode == FT_PIXEL_MODE_BGRA ) 1625 { 1626 FT_Bitmap new_map; 1627 FT_Library library = face->root.glyph->library; 1628 1629 1630 FT_Bitmap_Init( &new_map ); 1631 1632 /* Convert to 8bit grayscale. */ 1633 error = FT_Bitmap_Convert( library, map, &new_map, 1 ); 1634 if ( error ) 1635 FT_Bitmap_Done( library, &new_map ); 1636 else 1637 { 1638 map->pixel_mode = new_map.pixel_mode; 1639 map->pitch = new_map.pitch; 1640 map->num_grays = new_map.num_grays; 1641 1642 ft_glyphslot_set_bitmap( face->root.glyph, new_map.buffer ); 1643 face->root.glyph->internal->flags |= FT_GLYPH_OWN_BITMAP; 1644 } 1645 } 1646 1647 return error; 1648 } 1649 1650 1651 /* EOF */ 1652