1 /***************************************************************************/ 2 /* */ 3 /* ttsbit0.c */ 4 /* */ 5 /* TrueType and OpenType embedded bitmap support (body). */ 6 /* This is a heap-optimized version. */ 7 /* */ 8 /* Copyright 2005, 2006, 2007, 2008, 2009 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 /* This file is included by ttsbit.c */ 21 22 23 #include <ft2build.h> 24 #include FT_INTERNAL_DEBUG_H 25 #include FT_INTERNAL_STREAM_H 26 #include FT_TRUETYPE_TAGS_H 27 #include "ttsbit.h" 28 29 #include "sferrors.h" 30 31 32 /*************************************************************************/ 33 /* */ 34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 36 /* messages during execution. */ 37 /* */ 38 #undef FT_COMPONENT 39 #define FT_COMPONENT trace_ttsbit 40 41 42 FT_LOCAL_DEF( FT_Error ) tt_face_load_eblc(TT_Face face,FT_Stream stream)43 tt_face_load_eblc( TT_Face face, 44 FT_Stream stream ) 45 { 46 FT_Error error = SFNT_Err_Ok; 47 FT_Fixed version; 48 FT_ULong num_strikes, table_size; 49 FT_Byte* p; 50 FT_Byte* p_limit; 51 FT_UInt count; 52 53 54 face->sbit_num_strikes = 0; 55 56 /* this table is optional */ 57 error = face->goto_table( face, TTAG_EBLC, stream, &table_size ); 58 if ( error ) 59 error = face->goto_table( face, TTAG_bloc, stream, &table_size ); 60 if ( error ) 61 goto Exit; 62 63 if ( table_size < 8 ) 64 { 65 FT_ERROR(( "tt_face_load_sbit_strikes: table too short!\n" )); 66 error = SFNT_Err_Invalid_File_Format; 67 goto Exit; 68 } 69 70 if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) ) 71 goto Exit; 72 73 face->sbit_table_size = table_size; 74 75 p = face->sbit_table; 76 p_limit = p + table_size; 77 78 version = FT_NEXT_ULONG( p ); 79 num_strikes = FT_NEXT_ULONG( p ); 80 81 if ( version != 0x00020000UL || num_strikes >= 0x10000UL ) 82 { 83 FT_ERROR(( "tt_face_load_sbit_strikes: invalid table version!\n" )); 84 error = SFNT_Err_Invalid_File_Format; 85 goto Fail; 86 } 87 88 /* 89 * Count the number of strikes available in the table. We are a bit 90 * paranoid there and don't trust the data. 91 */ 92 count = (FT_UInt)num_strikes; 93 if ( 8 + 48UL * count > table_size ) 94 count = (FT_UInt)( ( p_limit - p ) / 48 ); 95 96 face->sbit_num_strikes = count; 97 98 FT_TRACE3(( "sbit_num_strikes: %u\n", count )); 99 Exit: 100 return error; 101 102 Fail: 103 FT_FRAME_RELEASE( face->sbit_table ); 104 face->sbit_table_size = 0; 105 goto Exit; 106 } 107 108 109 FT_LOCAL_DEF( void ) tt_face_free_eblc(TT_Face face)110 tt_face_free_eblc( TT_Face face ) 111 { 112 FT_Stream stream = face->root.stream; 113 114 115 FT_FRAME_RELEASE( face->sbit_table ); 116 face->sbit_table_size = 0; 117 face->sbit_num_strikes = 0; 118 } 119 120 121 FT_LOCAL_DEF( FT_Error ) tt_face_set_sbit_strike(TT_Face face,FT_Size_Request req,FT_ULong * astrike_index)122 tt_face_set_sbit_strike( TT_Face face, 123 FT_Size_Request req, 124 FT_ULong* astrike_index ) 125 { 126 return FT_Match_Size( (FT_Face)face, req, 0, astrike_index ); 127 } 128 129 130 FT_LOCAL_DEF( FT_Error ) tt_face_load_strike_metrics(TT_Face face,FT_ULong strike_index,FT_Size_Metrics * metrics)131 tt_face_load_strike_metrics( TT_Face face, 132 FT_ULong strike_index, 133 FT_Size_Metrics* metrics ) 134 { 135 FT_Byte* strike; 136 137 138 if ( strike_index >= (FT_ULong)face->sbit_num_strikes ) 139 return SFNT_Err_Invalid_Argument; 140 141 strike = face->sbit_table + 8 + strike_index * 48; 142 143 metrics->x_ppem = (FT_UShort)strike[44]; 144 metrics->y_ppem = (FT_UShort)strike[45]; 145 146 metrics->ascender = (FT_Char)strike[16] << 6; /* hori.ascender */ 147 metrics->descender = (FT_Char)strike[17] << 6; /* hori.descender */ 148 metrics->height = metrics->ascender - metrics->descender; 149 150 /* XXX: Is this correct? */ 151 metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB */ 152 strike[18] + /* max_width */ 153 (FT_Char)strike[23] /* min_advance_SB */ 154 ) << 6; 155 156 return SFNT_Err_Ok; 157 } 158 159 160 typedef struct TT_SBitDecoderRec_ 161 { 162 TT_Face face; 163 FT_Stream stream; 164 FT_Bitmap* bitmap; 165 TT_SBit_Metrics metrics; 166 FT_Bool metrics_loaded; 167 FT_Bool bitmap_allocated; 168 FT_Byte bit_depth; 169 170 FT_ULong ebdt_start; 171 FT_ULong ebdt_size; 172 173 FT_ULong strike_index_array; 174 FT_ULong strike_index_count; 175 FT_Byte* eblc_base; 176 FT_Byte* eblc_limit; 177 178 } TT_SBitDecoderRec, *TT_SBitDecoder; 179 180 181 static FT_Error tt_sbit_decoder_init(TT_SBitDecoder decoder,TT_Face face,FT_ULong strike_index,TT_SBit_MetricsRec * metrics)182 tt_sbit_decoder_init( TT_SBitDecoder decoder, 183 TT_Face face, 184 FT_ULong strike_index, 185 TT_SBit_MetricsRec* metrics ) 186 { 187 FT_Error error; 188 FT_Stream stream = face->root.stream; 189 FT_ULong ebdt_size; 190 191 192 error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size ); 193 if ( error ) 194 error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size ); 195 if ( error ) 196 goto Exit; 197 198 decoder->face = face; 199 decoder->stream = stream; 200 decoder->bitmap = &face->root.glyph->bitmap; 201 decoder->metrics = metrics; 202 203 decoder->metrics_loaded = 0; 204 decoder->bitmap_allocated = 0; 205 206 decoder->ebdt_start = FT_STREAM_POS(); 207 decoder->ebdt_size = ebdt_size; 208 209 decoder->eblc_base = face->sbit_table; 210 decoder->eblc_limit = face->sbit_table + face->sbit_table_size; 211 212 /* now find the strike corresponding to the index */ 213 { 214 FT_Byte* p; 215 216 217 if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size ) 218 { 219 error = SFNT_Err_Invalid_File_Format; 220 goto Exit; 221 } 222 223 p = decoder->eblc_base + 8 + 48 * strike_index; 224 225 decoder->strike_index_array = FT_NEXT_ULONG( p ); 226 p += 4; 227 decoder->strike_index_count = FT_NEXT_ULONG( p ); 228 p += 34; 229 decoder->bit_depth = *p; 230 231 if ( decoder->strike_index_array > face->sbit_table_size || 232 decoder->strike_index_array + 8 * decoder->strike_index_count > 233 face->sbit_table_size ) 234 error = SFNT_Err_Invalid_File_Format; 235 } 236 237 Exit: 238 return error; 239 } 240 241 242 static void tt_sbit_decoder_done(TT_SBitDecoder decoder)243 tt_sbit_decoder_done( TT_SBitDecoder decoder ) 244 { 245 FT_UNUSED( decoder ); 246 } 247 248 249 static FT_Error tt_sbit_decoder_alloc_bitmap(TT_SBitDecoder decoder)250 tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder ) 251 { 252 FT_Error error = SFNT_Err_Ok; 253 FT_UInt width, height; 254 FT_Bitmap* map = decoder->bitmap; 255 FT_Long size; 256 257 258 if ( !decoder->metrics_loaded ) 259 { 260 error = SFNT_Err_Invalid_Argument; 261 goto Exit; 262 } 263 264 width = decoder->metrics->width; 265 height = decoder->metrics->height; 266 267 map->width = (int)width; 268 map->rows = (int)height; 269 270 switch ( decoder->bit_depth ) 271 { 272 case 1: 273 map->pixel_mode = FT_PIXEL_MODE_MONO; 274 map->pitch = ( map->width + 7 ) >> 3; 275 break; 276 277 case 2: 278 map->pixel_mode = FT_PIXEL_MODE_GRAY2; 279 map->pitch = ( map->width + 3 ) >> 2; 280 break; 281 282 case 4: 283 map->pixel_mode = FT_PIXEL_MODE_GRAY4; 284 map->pitch = ( map->width + 1 ) >> 1; 285 break; 286 287 case 8: 288 map->pixel_mode = FT_PIXEL_MODE_GRAY; 289 map->pitch = map->width; 290 break; 291 292 default: 293 error = SFNT_Err_Invalid_File_Format; 294 goto Exit; 295 } 296 297 size = map->rows * map->pitch; 298 299 /* check that there is no empty image */ 300 if ( size == 0 ) 301 goto Exit; /* exit successfully! */ 302 303 error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size ); 304 if ( error ) 305 goto Exit; 306 307 decoder->bitmap_allocated = 1; 308 309 Exit: 310 return error; 311 } 312 313 314 static FT_Error tt_sbit_decoder_load_metrics(TT_SBitDecoder decoder,FT_Byte ** pp,FT_Byte * limit,FT_Bool big)315 tt_sbit_decoder_load_metrics( TT_SBitDecoder decoder, 316 FT_Byte* *pp, 317 FT_Byte* limit, 318 FT_Bool big ) 319 { 320 FT_Byte* p = *pp; 321 TT_SBit_Metrics metrics = decoder->metrics; 322 323 324 if ( p + 5 > limit ) 325 goto Fail; 326 327 metrics->height = p[0]; 328 metrics->width = p[1]; 329 metrics->horiBearingX = (FT_Char)p[2]; 330 metrics->horiBearingY = (FT_Char)p[3]; 331 metrics->horiAdvance = p[4]; 332 333 p += 5; 334 if ( big ) 335 { 336 if ( p + 3 > limit ) 337 goto Fail; 338 339 metrics->vertBearingX = (FT_Char)p[0]; 340 metrics->vertBearingY = (FT_Char)p[1]; 341 metrics->vertAdvance = p[2]; 342 343 p += 3; 344 } 345 346 decoder->metrics_loaded = 1; 347 *pp = p; 348 return SFNT_Err_Ok; 349 350 Fail: 351 return SFNT_Err_Invalid_Argument; 352 } 353 354 355 /* forward declaration */ 356 static FT_Error 357 tt_sbit_decoder_load_image( TT_SBitDecoder decoder, 358 FT_UInt glyph_index, 359 FT_Int x_pos, 360 FT_Int y_pos ); 361 362 typedef FT_Error (*TT_SBitDecoder_LoadFunc)( TT_SBitDecoder decoder, 363 FT_Byte* p, 364 FT_Byte* plimit, 365 FT_Int x_pos, 366 FT_Int y_pos ); 367 368 369 static FT_Error tt_sbit_decoder_load_byte_aligned(TT_SBitDecoder decoder,FT_Byte * p,FT_Byte * limit,FT_Int x_pos,FT_Int y_pos)370 tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder decoder, 371 FT_Byte* p, 372 FT_Byte* limit, 373 FT_Int x_pos, 374 FT_Int y_pos ) 375 { 376 FT_Error error = SFNT_Err_Ok; 377 FT_Byte* line; 378 FT_Int bit_height, bit_width, pitch, width, height, h; 379 FT_Bitmap* bitmap; 380 381 382 if ( !decoder->bitmap_allocated ) 383 { 384 error = tt_sbit_decoder_alloc_bitmap( decoder ); 385 if ( error ) 386 goto Exit; 387 } 388 389 /* check that we can write the glyph into the bitmap */ 390 bitmap = decoder->bitmap; 391 bit_width = bitmap->width; 392 bit_height = bitmap->rows; 393 pitch = bitmap->pitch; 394 line = bitmap->buffer; 395 396 width = decoder->metrics->width; 397 height = decoder->metrics->height; 398 399 if ( x_pos < 0 || x_pos + width > bit_width || 400 y_pos < 0 || y_pos + height > bit_height ) 401 { 402 error = SFNT_Err_Invalid_File_Format; 403 goto Exit; 404 } 405 406 if ( p + ( ( width + 7 ) >> 3 ) * height > limit ) 407 { 408 error = SFNT_Err_Invalid_File_Format; 409 goto Exit; 410 } 411 412 /* now do the blit */ 413 line += y_pos * pitch + ( x_pos >> 3 ); 414 x_pos &= 7; 415 416 if ( x_pos == 0 ) /* the easy one */ 417 { 418 for ( h = height; h > 0; h--, line += pitch ) 419 { 420 FT_Byte* write = line; 421 FT_Int w; 422 423 424 for ( w = width; w >= 8; w -= 8 ) 425 { 426 write[0] = (FT_Byte)( write[0] | *p++ ); 427 write += 1; 428 } 429 430 if ( w > 0 ) 431 write[0] = (FT_Byte)( write[0] | ( *p++ & ( 0xFF00U >> w ) ) ); 432 } 433 } 434 else /* x_pos > 0 */ 435 { 436 for ( h = height; h > 0; h--, line += pitch ) 437 { 438 FT_Byte* write = line; 439 FT_Int w; 440 FT_UInt wval = 0; 441 442 443 for ( w = width; w >= 8; w -= 8 ) 444 { 445 wval = (FT_UInt)( wval | *p++ ); 446 write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) ); 447 write += 1; 448 wval <<= 8; 449 } 450 451 if ( w > 0 ) 452 wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) ); 453 454 /* all bits read and there are `x_pos + w' bits to be written */ 455 456 write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) ); 457 458 if ( x_pos + w > 8 ) 459 { 460 write++; 461 wval <<= 8; 462 write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) ); 463 } 464 } 465 } 466 467 Exit: 468 return error; 469 } 470 471 472 static FT_Error tt_sbit_decoder_load_bit_aligned(TT_SBitDecoder decoder,FT_Byte * p,FT_Byte * limit,FT_Int x_pos,FT_Int y_pos)473 tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder, 474 FT_Byte* p, 475 FT_Byte* limit, 476 FT_Int x_pos, 477 FT_Int y_pos ) 478 { 479 FT_Error error = SFNT_Err_Ok; 480 FT_Byte* line; 481 FT_Int bit_height, bit_width, pitch, width, height, h, nbits; 482 FT_Bitmap* bitmap; 483 FT_UShort rval; 484 485 486 if ( !decoder->bitmap_allocated ) 487 { 488 error = tt_sbit_decoder_alloc_bitmap( decoder ); 489 if ( error ) 490 goto Exit; 491 } 492 493 /* check that we can write the glyph into the bitmap */ 494 bitmap = decoder->bitmap; 495 bit_width = bitmap->width; 496 bit_height = bitmap->rows; 497 pitch = bitmap->pitch; 498 line = bitmap->buffer; 499 500 width = decoder->metrics->width; 501 height = decoder->metrics->height; 502 503 if ( x_pos < 0 || x_pos + width > bit_width || 504 y_pos < 0 || y_pos + height > bit_height ) 505 { 506 error = SFNT_Err_Invalid_File_Format; 507 goto Exit; 508 } 509 510 if ( p + ( ( width * height + 7 ) >> 3 ) > limit ) 511 { 512 error = SFNT_Err_Invalid_File_Format; 513 goto Exit; 514 } 515 516 /* now do the blit */ 517 line += y_pos * pitch + ( x_pos >> 3 ); 518 x_pos &= 7; 519 520 /* the higher byte of `rval' is used as a buffer */ 521 rval = 0; 522 nbits = 0; 523 524 for ( h = height; h > 0; h--, line += pitch ) 525 { 526 FT_Byte* write = line; 527 FT_Int w = width; 528 529 530 if ( x_pos ) 531 { 532 w = ( width < 8 - x_pos ) ? width : 8 - x_pos; 533 534 if ( h == height ) 535 { 536 rval |= *p++; 537 nbits += x_pos; 538 } 539 else if ( nbits < w ) 540 { 541 rval |= *p++; 542 nbits += 8 - w; 543 } 544 else 545 { 546 rval >>= 8; 547 nbits -= w; 548 } 549 550 *write++ |= ( ( rval >> nbits ) & 0xFF ) & 551 ( ~( 0xFF << w ) << ( 8 - w - x_pos ) ); 552 rval <<= 8; 553 554 w = width - w; 555 } 556 557 for ( ; w >= 8; w -= 8 ) 558 { 559 rval |= *p++; 560 *write++ |= ( rval >> nbits ) & 0xFF; 561 562 rval <<= 8; 563 } 564 565 if ( w > 0 ) 566 { 567 if ( nbits < w ) 568 { 569 rval |= *p++; 570 *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w ); 571 nbits += 8 - w; 572 573 rval <<= 8; 574 } 575 else 576 { 577 *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w ); 578 nbits -= w; 579 } 580 } 581 } 582 583 Exit: 584 return error; 585 } 586 587 588 static FT_Error tt_sbit_decoder_load_compound(TT_SBitDecoder decoder,FT_Byte * p,FT_Byte * limit,FT_Int x_pos,FT_Int y_pos)589 tt_sbit_decoder_load_compound( TT_SBitDecoder decoder, 590 FT_Byte* p, 591 FT_Byte* limit, 592 FT_Int x_pos, 593 FT_Int y_pos ) 594 { 595 FT_Error error = SFNT_Err_Ok; 596 FT_UInt num_components, nn; 597 598 FT_Char horiBearingX = decoder->metrics->horiBearingX; 599 FT_Char horiBearingY = decoder->metrics->horiBearingY; 600 FT_Byte horiAdvance = decoder->metrics->horiAdvance; 601 FT_Char vertBearingX = decoder->metrics->vertBearingX; 602 FT_Char vertBearingY = decoder->metrics->vertBearingY; 603 FT_Byte vertAdvance = decoder->metrics->vertAdvance; 604 605 606 if ( p + 2 > limit ) 607 goto Fail; 608 609 num_components = FT_NEXT_USHORT( p ); 610 if ( p + 4 * num_components > limit ) 611 goto Fail; 612 613 if ( !decoder->bitmap_allocated ) 614 { 615 error = tt_sbit_decoder_alloc_bitmap( decoder ); 616 if ( error ) 617 goto Exit; 618 } 619 620 for ( nn = 0; nn < num_components; nn++ ) 621 { 622 FT_UInt gindex = FT_NEXT_USHORT( p ); 623 FT_Byte dx = FT_NEXT_BYTE( p ); 624 FT_Byte dy = FT_NEXT_BYTE( p ); 625 626 627 /* NB: a recursive call */ 628 error = tt_sbit_decoder_load_image( decoder, gindex, 629 x_pos + dx, y_pos + dy ); 630 if ( error ) 631 break; 632 } 633 634 decoder->metrics->horiBearingX = horiBearingX; 635 decoder->metrics->horiBearingY = horiBearingY; 636 decoder->metrics->horiAdvance = horiAdvance; 637 decoder->metrics->vertBearingX = vertBearingX; 638 decoder->metrics->vertBearingY = vertBearingY; 639 decoder->metrics->vertAdvance = vertAdvance; 640 decoder->metrics->width = (FT_UInt)decoder->bitmap->width; 641 decoder->metrics->height = (FT_UInt)decoder->bitmap->rows; 642 643 Exit: 644 return error; 645 646 Fail: 647 error = SFNT_Err_Invalid_File_Format; 648 goto Exit; 649 } 650 651 652 static FT_Error tt_sbit_decoder_load_bitmap(TT_SBitDecoder decoder,FT_UInt glyph_format,FT_ULong glyph_start,FT_ULong glyph_size,FT_Int x_pos,FT_Int y_pos)653 tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder, 654 FT_UInt glyph_format, 655 FT_ULong glyph_start, 656 FT_ULong glyph_size, 657 FT_Int x_pos, 658 FT_Int y_pos ) 659 { 660 FT_Error error; 661 FT_Stream stream = decoder->stream; 662 FT_Byte* p; 663 FT_Byte* p_limit; 664 FT_Byte* data; 665 666 667 /* seek into the EBDT table now */ 668 if ( glyph_start + glyph_size > decoder->ebdt_size ) 669 { 670 error = SFNT_Err_Invalid_Argument; 671 goto Exit; 672 } 673 674 if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) || 675 FT_FRAME_EXTRACT( glyph_size, data ) ) 676 goto Exit; 677 678 p = data; 679 p_limit = p + glyph_size; 680 681 /* read the data, depending on the glyph format */ 682 switch ( glyph_format ) 683 { 684 case 1: 685 case 2: 686 case 8: 687 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 ); 688 break; 689 690 case 6: 691 case 7: 692 case 9: 693 error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ); 694 break; 695 696 default: 697 error = SFNT_Err_Ok; 698 } 699 700 if ( error ) 701 goto Fail; 702 703 { 704 TT_SBitDecoder_LoadFunc loader; 705 706 707 switch ( glyph_format ) 708 { 709 case 1: 710 case 6: 711 loader = tt_sbit_decoder_load_byte_aligned; 712 break; 713 714 case 2: 715 case 5: 716 case 7: 717 loader = tt_sbit_decoder_load_bit_aligned; 718 break; 719 720 case 8: 721 if ( p + 1 > p_limit ) 722 goto Fail; 723 724 p += 1; /* skip padding */ 725 /* fall-through */ 726 727 case 9: 728 loader = tt_sbit_decoder_load_compound; 729 break; 730 731 default: 732 goto Fail; 733 } 734 735 error = loader( decoder, p, p_limit, x_pos, y_pos ); 736 } 737 738 Fail: 739 FT_FRAME_RELEASE( data ); 740 741 Exit: 742 return error; 743 } 744 745 746 static FT_Error tt_sbit_decoder_load_image(TT_SBitDecoder decoder,FT_UInt glyph_index,FT_Int x_pos,FT_Int y_pos)747 tt_sbit_decoder_load_image( TT_SBitDecoder decoder, 748 FT_UInt glyph_index, 749 FT_Int x_pos, 750 FT_Int y_pos ) 751 { 752 /* 753 * First, we find the correct strike range that applies to this 754 * glyph index. 755 */ 756 757 FT_Byte* p = decoder->eblc_base + decoder->strike_index_array; 758 FT_Byte* p_limit = decoder->eblc_limit; 759 FT_ULong num_ranges = decoder->strike_index_count; 760 FT_UInt start, end, index_format, image_format; 761 FT_ULong image_start = 0, image_end = 0, image_offset; 762 763 764 for ( ; num_ranges > 0; num_ranges-- ) 765 { 766 start = FT_NEXT_USHORT( p ); 767 end = FT_NEXT_USHORT( p ); 768 769 if ( glyph_index >= start && glyph_index <= end ) 770 goto FoundRange; 771 772 p += 4; /* ignore index offset */ 773 } 774 goto NoBitmap; 775 776 FoundRange: 777 image_offset = FT_NEXT_ULONG( p ); 778 779 /* overflow check */ 780 if ( decoder->eblc_base + decoder->strike_index_array + image_offset < 781 decoder->eblc_base ) 782 goto Failure; 783 784 p = decoder->eblc_base + decoder->strike_index_array + image_offset; 785 if ( p + 8 > p_limit ) 786 goto NoBitmap; 787 788 /* now find the glyph's location and extend within the ebdt table */ 789 index_format = FT_NEXT_USHORT( p ); 790 image_format = FT_NEXT_USHORT( p ); 791 image_offset = FT_NEXT_ULONG ( p ); 792 793 switch ( index_format ) 794 { 795 case 1: /* 4-byte offsets relative to `image_offset' */ 796 { 797 p += 4 * ( glyph_index - start ); 798 if ( p + 8 > p_limit ) 799 goto NoBitmap; 800 801 image_start = FT_NEXT_ULONG( p ); 802 image_end = FT_NEXT_ULONG( p ); 803 804 if ( image_start == image_end ) /* missing glyph */ 805 goto NoBitmap; 806 } 807 break; 808 809 case 2: /* big metrics, constant image size */ 810 { 811 FT_ULong image_size; 812 813 814 if ( p + 12 > p_limit ) 815 goto NoBitmap; 816 817 image_size = FT_NEXT_ULONG( p ); 818 819 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) ) 820 goto NoBitmap; 821 822 image_start = image_size * ( glyph_index - start ); 823 image_end = image_start + image_size; 824 } 825 break; 826 827 case 3: /* 2-byte offsets relative to 'image_offset' */ 828 { 829 p += 2 * ( glyph_index - start ); 830 if ( p + 4 > p_limit ) 831 goto NoBitmap; 832 833 image_start = FT_NEXT_USHORT( p ); 834 image_end = FT_NEXT_USHORT( p ); 835 836 if ( image_start == image_end ) /* missing glyph */ 837 goto NoBitmap; 838 } 839 break; 840 841 case 4: /* sparse glyph array with (glyph,offset) pairs */ 842 { 843 FT_ULong mm, num_glyphs; 844 845 846 if ( p + 4 > p_limit ) 847 goto NoBitmap; 848 849 num_glyphs = FT_NEXT_ULONG( p ); 850 851 /* overflow check */ 852 if ( p + ( num_glyphs + 1 ) * 4 < p ) 853 goto Failure; 854 855 if ( p + ( num_glyphs + 1 ) * 4 > p_limit ) 856 goto NoBitmap; 857 858 for ( mm = 0; mm < num_glyphs; mm++ ) 859 { 860 FT_UInt gindex = FT_NEXT_USHORT( p ); 861 862 863 if ( gindex == glyph_index ) 864 { 865 image_start = FT_NEXT_USHORT( p ); 866 p += 2; 867 image_end = FT_PEEK_USHORT( p ); 868 break; 869 } 870 p += 2; 871 } 872 873 if ( mm >= num_glyphs ) 874 goto NoBitmap; 875 } 876 break; 877 878 case 5: /* constant metrics with sparse glyph codes */ 879 { 880 FT_ULong image_size, mm, num_glyphs; 881 882 883 if ( p + 16 > p_limit ) 884 goto NoBitmap; 885 886 image_size = FT_NEXT_ULONG( p ); 887 888 if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) ) 889 goto NoBitmap; 890 891 num_glyphs = FT_NEXT_ULONG( p ); 892 893 /* overflow check */ 894 if ( p + 2 * num_glyphs < p ) 895 goto Failure; 896 897 if ( p + 2 * num_glyphs > p_limit ) 898 goto NoBitmap; 899 900 for ( mm = 0; mm < num_glyphs; mm++ ) 901 { 902 FT_UInt gindex = FT_NEXT_USHORT( p ); 903 904 905 if ( gindex == glyph_index ) 906 break; 907 } 908 909 if ( mm >= num_glyphs ) 910 goto NoBitmap; 911 912 image_start = image_size * mm; 913 image_end = image_start + image_size; 914 } 915 break; 916 917 default: 918 goto NoBitmap; 919 } 920 921 if ( image_start > image_end ) 922 goto NoBitmap; 923 924 image_end -= image_start; 925 image_start = image_offset + image_start; 926 927 return tt_sbit_decoder_load_bitmap( decoder, 928 image_format, 929 image_start, 930 image_end, 931 x_pos, 932 y_pos ); 933 934 Failure: 935 return SFNT_Err_Invalid_Table; 936 937 NoBitmap: 938 return SFNT_Err_Invalid_Argument; 939 } 940 941 942 FT_LOCAL( FT_Error ) tt_face_load_sbit_image(TT_Face face,FT_ULong strike_index,FT_UInt glyph_index,FT_UInt load_flags,FT_Stream stream,FT_Bitmap * map,TT_SBit_MetricsRec * metrics)943 tt_face_load_sbit_image( TT_Face face, 944 FT_ULong strike_index, 945 FT_UInt glyph_index, 946 FT_UInt load_flags, 947 FT_Stream stream, 948 FT_Bitmap *map, 949 TT_SBit_MetricsRec *metrics ) 950 { 951 TT_SBitDecoderRec decoder[1]; 952 FT_Error error; 953 954 FT_UNUSED( load_flags ); 955 FT_UNUSED( stream ); 956 FT_UNUSED( map ); 957 958 959 error = tt_sbit_decoder_init( decoder, face, strike_index, metrics ); 960 if ( !error ) 961 { 962 error = tt_sbit_decoder_load_image( decoder, glyph_index, 0, 0 ); 963 tt_sbit_decoder_done( decoder ); 964 } 965 966 return error; 967 } 968 969 /* EOF */ 970