1 /**************************************************************************** 2 * 3 * ftobjs.c 4 * 5 * The FreeType private base classes (body). 6 * 7 * Copyright (C) 1996-2021 by 8 * David Turner, Robert Wilhelm, and Werner Lemberg. 9 * 10 * This file is part of the FreeType project, and may only be used, 11 * modified, and distributed under the terms of the FreeType project 12 * license, LICENSE.TXT. By continuing to use, modify, or distribute 13 * this file you indicate that you have read the license and 14 * understand and accept it fully. 15 * 16 */ 17 18 19 #include <freetype/ftlist.h> 20 #include <freetype/ftoutln.h> 21 #include <freetype/ftfntfmt.h> 22 23 #include <freetype/internal/ftvalid.h> 24 #include <freetype/internal/ftobjs.h> 25 #include <freetype/internal/ftdebug.h> 26 #include <freetype/internal/ftrfork.h> 27 #include <freetype/internal/ftstream.h> 28 #include <freetype/internal/sfnt.h> /* for SFNT_Load_Table_Func */ 29 #include <freetype/internal/psaux.h> /* for PS_Driver */ 30 31 #include <freetype/tttables.h> 32 #include <freetype/tttags.h> 33 #include <freetype/ttnameid.h> 34 35 #include <freetype/internal/services/svprop.h> 36 #include <freetype/internal/services/svsfnt.h> 37 #include <freetype/internal/services/svpostnm.h> 38 #include <freetype/internal/services/svgldict.h> 39 #include <freetype/internal/services/svttcmap.h> 40 #include <freetype/internal/services/svkern.h> 41 #include <freetype/internal/services/svtteng.h> 42 43 #include <freetype/ftdriver.h> 44 45 #ifdef FT_CONFIG_OPTION_MAC_FONTS 46 #include "ftbase.h" 47 #endif 48 49 50 #ifdef FT_DEBUG_LEVEL_TRACE 51 52 #include <freetype/ftbitmap.h> 53 54 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */ 55 /* We disable the warning `conversion from XXX to YYY, */ 56 /* possible loss of data' in order to compile cleanly with */ 57 /* the maximum level of warnings: `md5.c' is non-FreeType */ 58 /* code, and it gets used during development builds only. */ 59 #pragma warning( push ) 60 #pragma warning( disable : 4244 ) 61 #endif /* _MSC_VER */ 62 63 /* It's easiest to include `md5.c' directly. However, since OpenSSL */ 64 /* also provides the same functions, there might be conflicts if */ 65 /* both FreeType and OpenSSL are built as static libraries. For */ 66 /* this reason, we put the MD5 stuff into the `FT_' namespace. */ 67 #define MD5_u32plus FT_MD5_u32plus 68 #define MD5_CTX FT_MD5_CTX 69 #define MD5_Init FT_MD5_Init 70 #define MD5_Update FT_MD5_Update 71 #define MD5_Final FT_MD5_Final 72 73 #undef HAVE_OPENSSL 74 75 #include "md5.c" 76 77 #if defined( _MSC_VER ) 78 #pragma warning( pop ) 79 #endif 80 81 /* This array must stay in sync with the @FT_Pixel_Mode enumeration */ 82 /* (in file `ftimage.h`). */ 83 84 static const char* const pixel_modes[] = 85 { 86 "none", 87 "monochrome bitmap", 88 "gray 8-bit bitmap", 89 "gray 2-bit bitmap", 90 "gray 4-bit bitmap", 91 "LCD 8-bit bitmap", 92 "vertical LCD 8-bit bitmap", 93 "BGRA 32-bit color image bitmap", 94 "SDF 8-bit bitmap" 95 }; 96 97 #endif /* FT_DEBUG_LEVEL_TRACE */ 98 99 100 #define GRID_FIT_METRICS 101 102 103 /* forward declaration */ 104 static FT_Error 105 ft_open_face_internal( FT_Library library, 106 const FT_Open_Args* args, 107 FT_Long face_index, 108 FT_Face *aface, 109 FT_Bool test_mac_fonts ); 110 111 112 FT_BASE_DEF( FT_Pointer ) ft_service_list_lookup(FT_ServiceDesc service_descriptors,const char * service_id)113 ft_service_list_lookup( FT_ServiceDesc service_descriptors, 114 const char* service_id ) 115 { 116 FT_Pointer result = NULL; 117 FT_ServiceDesc desc = service_descriptors; 118 119 120 if ( desc && service_id ) 121 { 122 for ( ; desc->serv_id != NULL; desc++ ) 123 { 124 if ( ft_strcmp( desc->serv_id, service_id ) == 0 ) 125 { 126 result = (FT_Pointer)desc->serv_data; 127 break; 128 } 129 } 130 } 131 132 return result; 133 } 134 135 136 FT_BASE_DEF( void ) ft_validator_init(FT_Validator valid,const FT_Byte * base,const FT_Byte * limit,FT_ValidationLevel level)137 ft_validator_init( FT_Validator valid, 138 const FT_Byte* base, 139 const FT_Byte* limit, 140 FT_ValidationLevel level ) 141 { 142 valid->base = base; 143 valid->limit = limit; 144 valid->level = level; 145 valid->error = FT_Err_Ok; 146 } 147 148 149 FT_BASE_DEF( FT_Int ) ft_validator_run(FT_Validator valid)150 ft_validator_run( FT_Validator valid ) 151 { 152 /* This function doesn't work! None should call it. */ 153 FT_UNUSED( valid ); 154 155 return -1; 156 } 157 158 159 FT_BASE_DEF( void ) ft_validator_error(FT_Validator valid,FT_Error error)160 ft_validator_error( FT_Validator valid, 161 FT_Error error ) 162 { 163 /* since the cast below also disables the compiler's */ 164 /* type check, we introduce a dummy variable, which */ 165 /* will be optimized away */ 166 volatile ft_jmp_buf* jump_buffer = &valid->jump_buffer; 167 168 169 valid->error = error; 170 171 /* throw away volatileness; use `jump_buffer' or the */ 172 /* compiler may warn about an unused local variable */ 173 ft_longjmp( *(ft_jmp_buf*) jump_buffer, 1 ); 174 } 175 176 177 /*************************************************************************/ 178 /*************************************************************************/ 179 /*************************************************************************/ 180 /**** ****/ 181 /**** ****/ 182 /**** S T R E A M ****/ 183 /**** ****/ 184 /**** ****/ 185 /*************************************************************************/ 186 /*************************************************************************/ 187 /*************************************************************************/ 188 189 190 /* create a new input stream from an FT_Open_Args structure */ 191 /* */ 192 FT_BASE_DEF( FT_Error ) FT_Stream_New(FT_Library library,const FT_Open_Args * args,FT_Stream * astream)193 FT_Stream_New( FT_Library library, 194 const FT_Open_Args* args, 195 FT_Stream *astream ) 196 { 197 FT_Error error; 198 FT_Memory memory; 199 FT_Stream stream = NULL; 200 FT_UInt mode; 201 202 203 *astream = NULL; 204 205 if ( !library ) 206 return FT_THROW( Invalid_Library_Handle ); 207 208 if ( !args ) 209 return FT_THROW( Invalid_Argument ); 210 211 memory = library->memory; 212 mode = args->flags & 213 ( FT_OPEN_MEMORY | FT_OPEN_STREAM | FT_OPEN_PATHNAME ); 214 215 if ( mode == FT_OPEN_MEMORY ) 216 { 217 /* create a memory-based stream */ 218 if ( FT_NEW( stream ) ) 219 goto Exit; 220 221 FT_Stream_OpenMemory( stream, 222 (const FT_Byte*)args->memory_base, 223 (FT_ULong)args->memory_size ); 224 } 225 226 #ifndef FT_CONFIG_OPTION_DISABLE_STREAM_SUPPORT 227 228 else if ( mode == FT_OPEN_PATHNAME ) 229 { 230 /* create a normal system stream */ 231 if ( FT_NEW( stream ) ) 232 goto Exit; 233 234 error = FT_Stream_Open( stream, args->pathname ); 235 if ( error ) 236 FT_FREE( stream ); 237 } 238 else if ( ( mode == FT_OPEN_STREAM ) && args->stream ) 239 { 240 /* use an existing, user-provided stream */ 241 242 /* in this case, we do not need to allocate a new stream object */ 243 /* since the caller is responsible for closing it himself */ 244 stream = args->stream; 245 error = FT_Err_Ok; 246 } 247 248 #endif 249 250 else 251 { 252 error = FT_THROW( Invalid_Argument ); 253 if ( ( args->flags & FT_OPEN_STREAM ) && args->stream ) 254 FT_Stream_Close( args->stream ); 255 } 256 257 if ( !error ) 258 { 259 stream->memory = memory; 260 *astream = stream; 261 } 262 263 Exit: 264 return error; 265 } 266 267 268 FT_BASE_DEF( void ) FT_Stream_Free(FT_Stream stream,FT_Int external)269 FT_Stream_Free( FT_Stream stream, 270 FT_Int external ) 271 { 272 if ( stream ) 273 { 274 FT_Memory memory = stream->memory; 275 276 277 FT_Stream_Close( stream ); 278 279 if ( !external ) 280 FT_FREE( stream ); 281 } 282 } 283 284 285 /************************************************************************** 286 * 287 * The macro FT_COMPONENT is used in trace mode. It is an implicit 288 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log 289 * messages during execution. 290 */ 291 #undef FT_COMPONENT 292 #define FT_COMPONENT objs 293 294 295 /*************************************************************************/ 296 /*************************************************************************/ 297 /*************************************************************************/ 298 /**** ****/ 299 /**** ****/ 300 /**** FACE, SIZE & GLYPH SLOT OBJECTS ****/ 301 /**** ****/ 302 /**** ****/ 303 /*************************************************************************/ 304 /*************************************************************************/ 305 /*************************************************************************/ 306 307 308 static FT_Error ft_glyphslot_init(FT_GlyphSlot slot)309 ft_glyphslot_init( FT_GlyphSlot slot ) 310 { 311 FT_Driver driver = slot->face->driver; 312 FT_Driver_Class clazz = driver->clazz; 313 FT_Memory memory = driver->root.memory; 314 FT_Error error = FT_Err_Ok; 315 FT_Slot_Internal internal = NULL; 316 317 318 slot->library = driver->root.library; 319 320 if ( FT_NEW( internal ) ) 321 goto Exit; 322 323 slot->internal = internal; 324 325 if ( FT_DRIVER_USES_OUTLINES( driver ) ) 326 error = FT_GlyphLoader_New( memory, &internal->loader ); 327 328 if ( !error && clazz->init_slot ) 329 error = clazz->init_slot( slot ); 330 331 Exit: 332 return error; 333 } 334 335 336 FT_BASE_DEF( void ) ft_glyphslot_free_bitmap(FT_GlyphSlot slot)337 ft_glyphslot_free_bitmap( FT_GlyphSlot slot ) 338 { 339 if ( slot->internal && ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) ) 340 { 341 FT_Memory memory = FT_FACE_MEMORY( slot->face ); 342 343 344 FT_FREE( slot->bitmap.buffer ); 345 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP; 346 } 347 else 348 { 349 /* assume that the bitmap buffer was stolen or not */ 350 /* allocated from the heap */ 351 slot->bitmap.buffer = NULL; 352 } 353 } 354 355 356 /* overflow-resistant presetting of bitmap position and dimensions; */ 357 /* also check whether the size is too large for rendering */ 358 FT_BASE_DEF( FT_Bool ) ft_glyphslot_preset_bitmap(FT_GlyphSlot slot,FT_Render_Mode mode,const FT_Vector * origin)359 ft_glyphslot_preset_bitmap( FT_GlyphSlot slot, 360 FT_Render_Mode mode, 361 const FT_Vector* origin ) 362 { 363 FT_Outline* outline = &slot->outline; 364 FT_Bitmap* bitmap = &slot->bitmap; 365 366 FT_Pixel_Mode pixel_mode; 367 368 FT_BBox cbox, pbox; 369 FT_Pos x_shift = 0; 370 FT_Pos y_shift = 0; 371 FT_Pos x_left, y_top; 372 FT_Pos width, height, pitch; 373 374 375 if ( slot->format != FT_GLYPH_FORMAT_OUTLINE ) 376 return 1; 377 378 if ( origin ) 379 { 380 x_shift = origin->x; 381 y_shift = origin->y; 382 } 383 384 /* compute the control box, and grid-fit it, */ 385 /* taking into account the origin shift */ 386 FT_Outline_Get_CBox( outline, &cbox ); 387 388 /* rough estimate of pixel box */ 389 pbox.xMin = ( cbox.xMin >> 6 ) + ( x_shift >> 6 ); 390 pbox.yMin = ( cbox.yMin >> 6 ) + ( y_shift >> 6 ); 391 pbox.xMax = ( cbox.xMax >> 6 ) + ( x_shift >> 6 ); 392 pbox.yMax = ( cbox.yMax >> 6 ) + ( y_shift >> 6 ); 393 394 /* tiny remainder box */ 395 cbox.xMin = ( cbox.xMin & 63 ) + ( x_shift & 63 ); 396 cbox.yMin = ( cbox.yMin & 63 ) + ( y_shift & 63 ); 397 cbox.xMax = ( cbox.xMax & 63 ) + ( x_shift & 63 ); 398 cbox.yMax = ( cbox.yMax & 63 ) + ( y_shift & 63 ); 399 400 switch ( mode ) 401 { 402 case FT_RENDER_MODE_MONO: 403 pixel_mode = FT_PIXEL_MODE_MONO; 404 #if 1 405 /* x */ 406 407 /* undocumented but confirmed: bbox values get rounded; */ 408 /* we do asymmetric rounding so that the center of a pixel */ 409 /* gets always included */ 410 411 pbox.xMin += ( cbox.xMin + 31 ) >> 6; 412 pbox.xMax += ( cbox.xMax + 32 ) >> 6; 413 414 /* if the bbox collapsed, we add a pixel based on the total */ 415 /* rounding remainder to cover most of the original cbox */ 416 417 if ( pbox.xMin == pbox.xMax ) 418 { 419 if ( ( ( cbox.xMin + 31 ) & 63 ) - 31 + 420 ( ( cbox.xMax + 32 ) & 63 ) - 32 < 0 ) 421 pbox.xMin -= 1; 422 else 423 pbox.xMax += 1; 424 } 425 426 /* y */ 427 428 pbox.yMin += ( cbox.yMin + 31 ) >> 6; 429 pbox.yMax += ( cbox.yMax + 32 ) >> 6; 430 431 if ( pbox.yMin == pbox.yMax ) 432 { 433 if ( ( ( cbox.yMin + 31 ) & 63 ) - 31 + 434 ( ( cbox.yMax + 32 ) & 63 ) - 32 < 0 ) 435 pbox.yMin -= 1; 436 else 437 pbox.yMax += 1; 438 } 439 440 break; 441 #else 442 goto Adjust; 443 #endif 444 445 case FT_RENDER_MODE_LCD: 446 pixel_mode = FT_PIXEL_MODE_LCD; 447 ft_lcd_padding( &cbox, slot, mode ); 448 goto Adjust; 449 450 case FT_RENDER_MODE_LCD_V: 451 pixel_mode = FT_PIXEL_MODE_LCD_V; 452 ft_lcd_padding( &cbox, slot, mode ); 453 goto Adjust; 454 455 case FT_RENDER_MODE_NORMAL: 456 case FT_RENDER_MODE_LIGHT: 457 default: 458 pixel_mode = FT_PIXEL_MODE_GRAY; 459 Adjust: 460 pbox.xMin += cbox.xMin >> 6; 461 pbox.yMin += cbox.yMin >> 6; 462 pbox.xMax += ( cbox.xMax + 63 ) >> 6; 463 pbox.yMax += ( cbox.yMax + 63 ) >> 6; 464 } 465 466 x_left = pbox.xMin; 467 y_top = pbox.yMax; 468 469 width = pbox.xMax - pbox.xMin; 470 height = pbox.yMax - pbox.yMin; 471 472 switch ( pixel_mode ) 473 { 474 case FT_PIXEL_MODE_MONO: 475 pitch = ( ( width + 15 ) >> 4 ) << 1; 476 break; 477 478 case FT_PIXEL_MODE_LCD: 479 width *= 3; 480 pitch = FT_PAD_CEIL( width, 4 ); 481 break; 482 483 case FT_PIXEL_MODE_LCD_V: 484 height *= 3; 485 /* fall through */ 486 487 case FT_PIXEL_MODE_GRAY: 488 default: 489 pitch = width; 490 } 491 492 slot->bitmap_left = (FT_Int)x_left; 493 slot->bitmap_top = (FT_Int)y_top; 494 495 bitmap->pixel_mode = (unsigned char)pixel_mode; 496 bitmap->num_grays = 256; 497 bitmap->width = (unsigned int)width; 498 bitmap->rows = (unsigned int)height; 499 bitmap->pitch = pitch; 500 501 if ( pbox.xMin < -0x8000 || pbox.xMax > 0x7FFF || 502 pbox.yMin < -0x8000 || pbox.yMax > 0x7FFF ) 503 { 504 FT_TRACE3(( "ft_glyphslot_preset_bitmap: [%ld %ld %ld %ld]\n", 505 pbox.xMin, pbox.yMin, pbox.xMax, pbox.yMax )); 506 return 1; 507 } 508 509 return 0; 510 } 511 512 513 FT_BASE_DEF( void ) ft_glyphslot_set_bitmap(FT_GlyphSlot slot,FT_Byte * buffer)514 ft_glyphslot_set_bitmap( FT_GlyphSlot slot, 515 FT_Byte* buffer ) 516 { 517 ft_glyphslot_free_bitmap( slot ); 518 519 slot->bitmap.buffer = buffer; 520 521 FT_ASSERT( (slot->internal->flags & FT_GLYPH_OWN_BITMAP) == 0 ); 522 } 523 524 525 FT_BASE_DEF( FT_Error ) ft_glyphslot_alloc_bitmap(FT_GlyphSlot slot,FT_ULong size)526 ft_glyphslot_alloc_bitmap( FT_GlyphSlot slot, 527 FT_ULong size ) 528 { 529 FT_Memory memory = FT_FACE_MEMORY( slot->face ); 530 FT_Error error; 531 532 533 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) 534 FT_FREE( slot->bitmap.buffer ); 535 else 536 slot->internal->flags |= FT_GLYPH_OWN_BITMAP; 537 538 (void)FT_ALLOC( slot->bitmap.buffer, size ); 539 return error; 540 } 541 542 543 static void ft_glyphslot_clear(FT_GlyphSlot slot)544 ft_glyphslot_clear( FT_GlyphSlot slot ) 545 { 546 /* free bitmap if needed */ 547 ft_glyphslot_free_bitmap( slot ); 548 549 /* clear all public fields in the glyph slot */ 550 slot->glyph_index = 0; 551 552 FT_ZERO( &slot->metrics ); 553 FT_ZERO( &slot->outline ); 554 555 slot->bitmap.width = 0; 556 slot->bitmap.rows = 0; 557 slot->bitmap.pitch = 0; 558 slot->bitmap.pixel_mode = 0; 559 /* `slot->bitmap.buffer' has been handled by ft_glyphslot_free_bitmap */ 560 561 slot->bitmap_left = 0; 562 slot->bitmap_top = 0; 563 slot->num_subglyphs = 0; 564 slot->subglyphs = NULL; 565 slot->control_data = NULL; 566 slot->control_len = 0; 567 slot->other = NULL; 568 slot->format = FT_GLYPH_FORMAT_NONE; 569 570 slot->linearHoriAdvance = 0; 571 slot->linearVertAdvance = 0; 572 slot->advance.x = 0; 573 slot->advance.y = 0; 574 slot->lsb_delta = 0; 575 slot->rsb_delta = 0; 576 } 577 578 579 static void ft_glyphslot_done(FT_GlyphSlot slot)580 ft_glyphslot_done( FT_GlyphSlot slot ) 581 { 582 FT_Driver driver = slot->face->driver; 583 FT_Driver_Class clazz = driver->clazz; 584 FT_Memory memory = driver->root.memory; 585 586 587 if ( clazz->done_slot ) 588 clazz->done_slot( slot ); 589 590 /* free bitmap buffer if needed */ 591 ft_glyphslot_free_bitmap( slot ); 592 593 /* slot->internal might be NULL in out-of-memory situations */ 594 if ( slot->internal ) 595 { 596 /* free glyph loader */ 597 if ( FT_DRIVER_USES_OUTLINES( driver ) ) 598 { 599 FT_GlyphLoader_Done( slot->internal->loader ); 600 slot->internal->loader = NULL; 601 } 602 603 FT_FREE( slot->internal ); 604 } 605 } 606 607 608 /* documentation is in ftobjs.h */ 609 610 FT_BASE_DEF( FT_Error ) FT_New_GlyphSlot(FT_Face face,FT_GlyphSlot * aslot)611 FT_New_GlyphSlot( FT_Face face, 612 FT_GlyphSlot *aslot ) 613 { 614 FT_Error error; 615 FT_Driver driver; 616 FT_Driver_Class clazz; 617 FT_Memory memory; 618 FT_GlyphSlot slot = NULL; 619 620 621 if ( !face ) 622 return FT_THROW( Invalid_Face_Handle ); 623 624 if ( !face->driver ) 625 return FT_THROW( Invalid_Argument ); 626 627 driver = face->driver; 628 clazz = driver->clazz; 629 memory = driver->root.memory; 630 631 FT_TRACE4(( "FT_New_GlyphSlot: Creating new slot object\n" )); 632 if ( !FT_ALLOC( slot, clazz->slot_object_size ) ) 633 { 634 slot->face = face; 635 636 error = ft_glyphslot_init( slot ); 637 if ( error ) 638 { 639 ft_glyphslot_done( slot ); 640 FT_FREE( slot ); 641 goto Exit; 642 } 643 644 slot->next = face->glyph; 645 face->glyph = slot; 646 647 if ( aslot ) 648 *aslot = slot; 649 } 650 else if ( aslot ) 651 *aslot = NULL; 652 653 654 Exit: 655 FT_TRACE4(( "FT_New_GlyphSlot: Return 0x%x\n", error )); 656 657 return error; 658 } 659 660 661 /* documentation is in ftobjs.h */ 662 663 FT_BASE_DEF( void ) FT_Done_GlyphSlot(FT_GlyphSlot slot)664 FT_Done_GlyphSlot( FT_GlyphSlot slot ) 665 { 666 if ( slot ) 667 { 668 FT_Driver driver = slot->face->driver; 669 FT_Memory memory = driver->root.memory; 670 FT_GlyphSlot prev; 671 FT_GlyphSlot cur; 672 673 674 /* Remove slot from its parent face's list */ 675 prev = NULL; 676 cur = slot->face->glyph; 677 678 while ( cur ) 679 { 680 if ( cur == slot ) 681 { 682 if ( !prev ) 683 slot->face->glyph = cur->next; 684 else 685 prev->next = cur->next; 686 687 /* finalize client-specific data */ 688 if ( slot->generic.finalizer ) 689 slot->generic.finalizer( slot ); 690 691 ft_glyphslot_done( slot ); 692 FT_FREE( slot ); 693 break; 694 } 695 prev = cur; 696 cur = cur->next; 697 } 698 } 699 } 700 701 702 /* documentation is in freetype.h */ 703 704 FT_EXPORT_DEF( void ) FT_Set_Transform(FT_Face face,FT_Matrix * matrix,FT_Vector * delta)705 FT_Set_Transform( FT_Face face, 706 FT_Matrix* matrix, 707 FT_Vector* delta ) 708 { 709 FT_Face_Internal internal; 710 711 712 if ( !face ) 713 return; 714 715 internal = face->internal; 716 717 internal->transform_flags = 0; 718 719 if ( !matrix ) 720 { 721 internal->transform_matrix.xx = 0x10000L; 722 internal->transform_matrix.xy = 0; 723 internal->transform_matrix.yx = 0; 724 internal->transform_matrix.yy = 0x10000L; 725 726 matrix = &internal->transform_matrix; 727 } 728 else 729 internal->transform_matrix = *matrix; 730 731 /* set transform_flags bit flag 0 if `matrix' isn't the identity */ 732 if ( ( matrix->xy | matrix->yx ) || 733 matrix->xx != 0x10000L || 734 matrix->yy != 0x10000L ) 735 internal->transform_flags |= 1; 736 737 if ( !delta ) 738 { 739 internal->transform_delta.x = 0; 740 internal->transform_delta.y = 0; 741 742 delta = &internal->transform_delta; 743 } 744 else 745 internal->transform_delta = *delta; 746 747 /* set transform_flags bit flag 1 if `delta' isn't the null vector */ 748 if ( delta->x | delta->y ) 749 internal->transform_flags |= 2; 750 } 751 752 753 /* documentation is in freetype.h */ 754 755 FT_EXPORT_DEF( void ) FT_Get_Transform(FT_Face face,FT_Matrix * matrix,FT_Vector * delta)756 FT_Get_Transform( FT_Face face, 757 FT_Matrix* matrix, 758 FT_Vector* delta ) 759 { 760 FT_Face_Internal internal; 761 762 763 if ( !face ) 764 return; 765 766 internal = face->internal; 767 768 if ( matrix ) 769 *matrix = internal->transform_matrix; 770 771 if ( delta ) 772 *delta = internal->transform_delta; 773 } 774 775 776 static FT_Renderer 777 ft_lookup_glyph_renderer( FT_GlyphSlot slot ); 778 779 780 #ifdef GRID_FIT_METRICS 781 static void ft_glyphslot_grid_fit_metrics(FT_GlyphSlot slot,FT_Bool vertical)782 ft_glyphslot_grid_fit_metrics( FT_GlyphSlot slot, 783 FT_Bool vertical ) 784 { 785 FT_Glyph_Metrics* metrics = &slot->metrics; 786 FT_Pos right, bottom; 787 788 789 if ( vertical ) 790 { 791 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX ); 792 metrics->horiBearingY = FT_PIX_CEIL_LONG( metrics->horiBearingY ); 793 794 right = FT_PIX_CEIL_LONG( ADD_LONG( metrics->vertBearingX, 795 metrics->width ) ); 796 bottom = FT_PIX_CEIL_LONG( ADD_LONG( metrics->vertBearingY, 797 metrics->height ) ); 798 799 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX ); 800 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY ); 801 802 metrics->width = SUB_LONG( right, 803 metrics->vertBearingX ); 804 metrics->height = SUB_LONG( bottom, 805 metrics->vertBearingY ); 806 } 807 else 808 { 809 metrics->vertBearingX = FT_PIX_FLOOR( metrics->vertBearingX ); 810 metrics->vertBearingY = FT_PIX_FLOOR( metrics->vertBearingY ); 811 812 right = FT_PIX_CEIL_LONG( ADD_LONG( metrics->horiBearingX, 813 metrics->width ) ); 814 bottom = FT_PIX_FLOOR( SUB_LONG( metrics->horiBearingY, 815 metrics->height ) ); 816 817 metrics->horiBearingX = FT_PIX_FLOOR( metrics->horiBearingX ); 818 metrics->horiBearingY = FT_PIX_CEIL_LONG( metrics->horiBearingY ); 819 820 metrics->width = SUB_LONG( right, 821 metrics->horiBearingX ); 822 metrics->height = SUB_LONG( metrics->horiBearingY, 823 bottom ); 824 } 825 826 metrics->horiAdvance = FT_PIX_ROUND_LONG( metrics->horiAdvance ); 827 metrics->vertAdvance = FT_PIX_ROUND_LONG( metrics->vertAdvance ); 828 } 829 #endif /* GRID_FIT_METRICS */ 830 831 832 /* documentation is in freetype.h */ 833 834 FT_EXPORT_DEF( FT_Error ) FT_Load_Glyph(FT_Face face,FT_UInt glyph_index,FT_Int32 load_flags)835 FT_Load_Glyph( FT_Face face, 836 FT_UInt glyph_index, 837 FT_Int32 load_flags ) 838 { 839 FT_Error error; 840 FT_Driver driver; 841 FT_GlyphSlot slot; 842 FT_Library library; 843 FT_Bool autohint = FALSE; 844 FT_Module hinter; 845 TT_Face ttface = (TT_Face)face; 846 847 848 if ( !face || !face->size || !face->glyph ) 849 return FT_THROW( Invalid_Face_Handle ); 850 851 /* The validity test for `glyph_index' is performed by the */ 852 /* font drivers. */ 853 854 slot = face->glyph; 855 ft_glyphslot_clear( slot ); 856 857 driver = face->driver; 858 library = driver->root.library; 859 hinter = library->auto_hinter; 860 861 /* resolve load flags dependencies */ 862 863 if ( load_flags & FT_LOAD_NO_RECURSE ) 864 load_flags |= FT_LOAD_NO_SCALE | 865 FT_LOAD_IGNORE_TRANSFORM; 866 867 if ( load_flags & FT_LOAD_NO_SCALE ) 868 { 869 load_flags |= FT_LOAD_NO_HINTING | 870 FT_LOAD_NO_BITMAP; 871 872 load_flags &= ~FT_LOAD_RENDER; 873 } 874 875 if ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) 876 load_flags &= ~FT_LOAD_RENDER; 877 878 /* 879 * Determine whether we need to auto-hint or not. 880 * The general rules are: 881 * 882 * - Do only auto-hinting if we have 883 * 884 * - a hinter module, 885 * - a scalable font, 886 * - not a tricky font, and 887 * - no transforms except simple slants and/or rotations by 888 * integer multiples of 90 degrees. 889 * 890 * - Then, auto-hint if FT_LOAD_FORCE_AUTOHINT is set or if we don't 891 * have a native font hinter. 892 * 893 * - Otherwise, auto-hint for LIGHT hinting mode or if there isn't 894 * any hinting bytecode in the TrueType/OpenType font. 895 * 896 * - Exception: The font is `tricky' and requires the native hinter to 897 * load properly. 898 */ 899 900 if ( hinter && 901 !( load_flags & FT_LOAD_NO_HINTING ) && 902 !( load_flags & FT_LOAD_NO_AUTOHINT ) && 903 FT_IS_SCALABLE( face ) && 904 !FT_IS_TRICKY( face ) && 905 ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) || 906 ( face->internal->transform_matrix.yx == 0 && 907 face->internal->transform_matrix.xx != 0 ) || 908 ( face->internal->transform_matrix.xx == 0 && 909 face->internal->transform_matrix.yx != 0 ) ) ) 910 { 911 if ( ( load_flags & FT_LOAD_FORCE_AUTOHINT ) || 912 !FT_DRIVER_HAS_HINTER( driver ) ) 913 autohint = TRUE; 914 else 915 { 916 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags ); 917 FT_Bool is_light_type1; 918 919 920 /* only the new Adobe engine (for both CFF and Type 1) is `light'; */ 921 /* we use `strstr' to catch both `Type 1' and `CID Type 1' */ 922 is_light_type1 = 923 ft_strstr( FT_Get_Font_Format( face ), "Type 1" ) != NULL && 924 ((PS_Driver)driver)->hinting_engine == FT_HINTING_ADOBE; 925 926 /* the check for `num_locations' assures that we actually */ 927 /* test for instructions in a TTF and not in a CFF-based OTF */ 928 /* */ 929 /* since `maxSizeOfInstructions' might be unreliable, we */ 930 /* check the size of the `fpgm' and `prep' tables, too -- */ 931 /* the assumption is that there don't exist real TTFs where */ 932 /* both `fpgm' and `prep' tables are missing */ 933 if ( ( mode == FT_RENDER_MODE_LIGHT && 934 ( !FT_DRIVER_HINTS_LIGHTLY( driver ) && 935 !is_light_type1 ) ) || 936 ( FT_IS_SFNT( face ) && 937 ttface->num_locations && 938 ttface->max_profile.maxSizeOfInstructions == 0 && 939 ttface->font_program_size == 0 && 940 ttface->cvt_program_size == 0 ) ) 941 autohint = TRUE; 942 } 943 } 944 945 if ( autohint ) 946 { 947 FT_AutoHinter_Interface hinting; 948 949 950 /* try to load embedded bitmaps first if available */ 951 /* */ 952 /* XXX: This is really a temporary hack that should disappear */ 953 /* promptly with FreeType 2.1! */ 954 /* */ 955 if ( FT_HAS_FIXED_SIZES( face ) && 956 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) 957 { 958 error = driver->clazz->load_glyph( slot, face->size, 959 glyph_index, 960 load_flags | FT_LOAD_SBITS_ONLY ); 961 962 if ( !error && slot->format == FT_GLYPH_FORMAT_BITMAP ) 963 goto Load_Ok; 964 } 965 966 { 967 FT_Face_Internal internal = face->internal; 968 FT_Int transform_flags = internal->transform_flags; 969 970 971 /* since the auto-hinter calls FT_Load_Glyph by itself, */ 972 /* make sure that glyphs aren't transformed */ 973 internal->transform_flags = 0; 974 975 /* load auto-hinted outline */ 976 hinting = (FT_AutoHinter_Interface)hinter->clazz->module_interface; 977 978 error = hinting->load_glyph( (FT_AutoHinter)hinter, 979 slot, face->size, 980 glyph_index, load_flags ); 981 982 internal->transform_flags = transform_flags; 983 } 984 } 985 else 986 { 987 error = driver->clazz->load_glyph( slot, 988 face->size, 989 glyph_index, 990 load_flags ); 991 if ( error ) 992 goto Exit; 993 994 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE ) 995 { 996 /* check that the loaded outline is correct */ 997 error = FT_Outline_Check( &slot->outline ); 998 if ( error ) 999 goto Exit; 1000 1001 #ifdef GRID_FIT_METRICS 1002 if ( !( load_flags & FT_LOAD_NO_HINTING ) ) 1003 ft_glyphslot_grid_fit_metrics( 1004 slot, 1005 FT_BOOL( load_flags & FT_LOAD_VERTICAL_LAYOUT ) ); 1006 #endif 1007 } 1008 } 1009 1010 Load_Ok: 1011 /* compute the advance */ 1012 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 1013 { 1014 slot->advance.x = 0; 1015 slot->advance.y = slot->metrics.vertAdvance; 1016 } 1017 else 1018 { 1019 slot->advance.x = slot->metrics.horiAdvance; 1020 slot->advance.y = 0; 1021 } 1022 1023 /* compute the linear advance in 16.16 pixels */ 1024 if ( ( load_flags & FT_LOAD_LINEAR_DESIGN ) == 0 && 1025 FT_IS_SCALABLE( face ) ) 1026 { 1027 FT_Size_Metrics* metrics = &face->size->metrics; 1028 1029 1030 /* it's tricky! */ 1031 slot->linearHoriAdvance = FT_MulDiv( slot->linearHoriAdvance, 1032 metrics->x_scale, 64 ); 1033 1034 slot->linearVertAdvance = FT_MulDiv( slot->linearVertAdvance, 1035 metrics->y_scale, 64 ); 1036 } 1037 1038 if ( ( load_flags & FT_LOAD_IGNORE_TRANSFORM ) == 0 ) 1039 { 1040 FT_Face_Internal internal = face->internal; 1041 1042 1043 /* now, transform the glyph image if needed */ 1044 if ( internal->transform_flags ) 1045 { 1046 /* get renderer */ 1047 FT_Renderer renderer = ft_lookup_glyph_renderer( slot ); 1048 1049 1050 if ( renderer ) 1051 error = renderer->clazz->transform_glyph( 1052 renderer, slot, 1053 &internal->transform_matrix, 1054 &internal->transform_delta ); 1055 else if ( slot->format == FT_GLYPH_FORMAT_OUTLINE ) 1056 { 1057 /* apply `standard' transformation if no renderer is available */ 1058 if ( internal->transform_flags & 1 ) 1059 FT_Outline_Transform( &slot->outline, 1060 &internal->transform_matrix ); 1061 1062 if ( internal->transform_flags & 2 ) 1063 FT_Outline_Translate( &slot->outline, 1064 internal->transform_delta.x, 1065 internal->transform_delta.y ); 1066 } 1067 1068 /* transform advance */ 1069 FT_Vector_Transform( &slot->advance, &internal->transform_matrix ); 1070 } 1071 } 1072 1073 slot->glyph_index = glyph_index; 1074 slot->internal->load_flags = load_flags; 1075 1076 /* do we need to render the image or preset the bitmap now? */ 1077 if ( !error && 1078 ( load_flags & FT_LOAD_NO_SCALE ) == 0 && 1079 slot->format != FT_GLYPH_FORMAT_BITMAP && 1080 slot->format != FT_GLYPH_FORMAT_COMPOSITE ) 1081 { 1082 FT_Render_Mode mode = FT_LOAD_TARGET_MODE( load_flags ); 1083 1084 1085 if ( mode == FT_RENDER_MODE_NORMAL && 1086 load_flags & FT_LOAD_MONOCHROME ) 1087 mode = FT_RENDER_MODE_MONO; 1088 1089 if ( load_flags & FT_LOAD_RENDER ) 1090 error = FT_Render_Glyph( slot, mode ); 1091 else 1092 ft_glyphslot_preset_bitmap( slot, mode, NULL ); 1093 } 1094 1095 #ifdef FT_DEBUG_LEVEL_TRACE 1096 FT_TRACE5(( "FT_Load_Glyph: index %d, flags 0x%x\n", 1097 glyph_index, load_flags )); 1098 FT_TRACE5(( " bitmap %dx%d %s, %s (mode %d)\n", 1099 slot->bitmap.width, 1100 slot->bitmap.rows, 1101 slot->outline.points ? 1102 slot->bitmap.buffer ? "rendered" 1103 : "preset" 1104 : 1105 slot->internal->flags & FT_GLYPH_OWN_BITMAP ? "owned" 1106 : "unowned", 1107 pixel_modes[slot->bitmap.pixel_mode], 1108 slot->bitmap.pixel_mode )); 1109 FT_TRACE5(( "\n" )); 1110 FT_TRACE5(( " x advance: %f\n", slot->advance.x / 64.0 )); 1111 FT_TRACE5(( " y advance: %f\n", slot->advance.y / 64.0 )); 1112 FT_TRACE5(( " linear x advance: %f\n", 1113 slot->linearHoriAdvance / 65536.0 )); 1114 FT_TRACE5(( " linear y advance: %f\n", 1115 slot->linearVertAdvance / 65536.0 )); 1116 1117 { 1118 FT_Glyph_Metrics* metrics = &slot->metrics; 1119 1120 1121 FT_TRACE5(( " metrics:\n" )); 1122 FT_TRACE5(( " width: %f\n", metrics->width / 64.0 )); 1123 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 1124 FT_TRACE5(( "\n" )); 1125 FT_TRACE5(( " horiBearingX: %f\n", metrics->horiBearingX / 64.0 )); 1126 FT_TRACE5(( " horiBearingY: %f\n", metrics->horiBearingY / 64.0 )); 1127 FT_TRACE5(( " horiAdvance: %f\n", metrics->horiAdvance / 64.0 )); 1128 FT_TRACE5(( "\n" )); 1129 FT_TRACE5(( " vertBearingX: %f\n", metrics->vertBearingX / 64.0 )); 1130 FT_TRACE5(( " vertBearingY: %f\n", metrics->vertBearingY / 64.0 )); 1131 FT_TRACE5(( " vertAdvance: %f\n", metrics->vertAdvance / 64.0 )); 1132 } 1133 #endif 1134 1135 Exit: 1136 return error; 1137 } 1138 1139 1140 /* documentation is in freetype.h */ 1141 1142 FT_EXPORT_DEF( FT_Error ) FT_Load_Char(FT_Face face,FT_ULong char_code,FT_Int32 load_flags)1143 FT_Load_Char( FT_Face face, 1144 FT_ULong char_code, 1145 FT_Int32 load_flags ) 1146 { 1147 FT_UInt glyph_index; 1148 1149 1150 if ( !face ) 1151 return FT_THROW( Invalid_Face_Handle ); 1152 1153 glyph_index = (FT_UInt)char_code; 1154 if ( face->charmap ) 1155 glyph_index = FT_Get_Char_Index( face, char_code ); 1156 1157 return FT_Load_Glyph( face, glyph_index, load_flags ); 1158 } 1159 1160 1161 /* destructor for sizes list */ 1162 static void destroy_size(FT_Memory memory,FT_Size size,FT_Driver driver)1163 destroy_size( FT_Memory memory, 1164 FT_Size size, 1165 FT_Driver driver ) 1166 { 1167 /* finalize client-specific data */ 1168 if ( size->generic.finalizer ) 1169 size->generic.finalizer( size ); 1170 1171 /* finalize format-specific stuff */ 1172 if ( driver->clazz->done_size ) 1173 driver->clazz->done_size( size ); 1174 1175 FT_FREE( size->internal ); 1176 FT_FREE( size ); 1177 } 1178 1179 1180 static void 1181 ft_cmap_done_internal( FT_CMap cmap ); 1182 1183 1184 static void destroy_charmaps(FT_Face face,FT_Memory memory)1185 destroy_charmaps( FT_Face face, 1186 FT_Memory memory ) 1187 { 1188 FT_Int n; 1189 1190 1191 if ( !face ) 1192 return; 1193 1194 for ( n = 0; n < face->num_charmaps; n++ ) 1195 { 1196 FT_CMap cmap = FT_CMAP( face->charmaps[n] ); 1197 1198 1199 ft_cmap_done_internal( cmap ); 1200 1201 face->charmaps[n] = NULL; 1202 } 1203 1204 FT_FREE( face->charmaps ); 1205 face->num_charmaps = 0; 1206 } 1207 1208 1209 /* destructor for faces list */ 1210 static void destroy_face(FT_Memory memory,FT_Face face,FT_Driver driver)1211 destroy_face( FT_Memory memory, 1212 FT_Face face, 1213 FT_Driver driver ) 1214 { 1215 FT_Driver_Class clazz = driver->clazz; 1216 1217 1218 /* discard auto-hinting data */ 1219 if ( face->autohint.finalizer ) 1220 face->autohint.finalizer( face->autohint.data ); 1221 1222 /* Discard glyph slots for this face. */ 1223 /* Beware! FT_Done_GlyphSlot() changes the field `face->glyph' */ 1224 while ( face->glyph ) 1225 FT_Done_GlyphSlot( face->glyph ); 1226 1227 /* discard all sizes for this face */ 1228 FT_List_Finalize( &face->sizes_list, 1229 (FT_List_Destructor)destroy_size, 1230 memory, 1231 driver ); 1232 face->size = NULL; 1233 1234 /* now discard client data */ 1235 if ( face->generic.finalizer ) 1236 face->generic.finalizer( face ); 1237 1238 /* discard charmaps */ 1239 destroy_charmaps( face, memory ); 1240 1241 /* finalize format-specific stuff */ 1242 if ( clazz->done_face ) 1243 clazz->done_face( face ); 1244 1245 /* close the stream for this face if needed */ 1246 FT_Stream_Free( 1247 face->stream, 1248 ( face->face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 ); 1249 1250 face->stream = NULL; 1251 1252 /* get rid of it */ 1253 if ( face->internal ) 1254 { 1255 FT_FREE( face->internal ); 1256 } 1257 FT_FREE( face ); 1258 } 1259 1260 1261 static void Destroy_Driver(FT_Driver driver)1262 Destroy_Driver( FT_Driver driver ) 1263 { 1264 FT_List_Finalize( &driver->faces_list, 1265 (FT_List_Destructor)destroy_face, 1266 driver->root.memory, 1267 driver ); 1268 } 1269 1270 1271 /************************************************************************** 1272 * 1273 * @Function: 1274 * find_unicode_charmap 1275 * 1276 * @Description: 1277 * This function finds a Unicode charmap, if there is one. 1278 * And if there is more than one, it tries to favour the more 1279 * extensive one, i.e., one that supports UCS-4 against those which 1280 * are limited to the BMP (said UCS-2 encoding.) 1281 * 1282 * This function is called from open_face() (just below), and also 1283 * from FT_Select_Charmap( ..., FT_ENCODING_UNICODE ). 1284 */ 1285 static FT_Error find_unicode_charmap(FT_Face face)1286 find_unicode_charmap( FT_Face face ) 1287 { 1288 FT_CharMap* first; 1289 FT_CharMap* cur; 1290 1291 1292 /* caller should have already checked that `face' is valid */ 1293 FT_ASSERT( face ); 1294 1295 first = face->charmaps; 1296 1297 if ( !first ) 1298 return FT_THROW( Invalid_CharMap_Handle ); 1299 1300 /* 1301 * The original TrueType specification(s) only specified charmap 1302 * formats that are capable of mapping 8 or 16 bit character codes to 1303 * glyph indices. 1304 * 1305 * However, recent updates to the Apple and OpenType specifications 1306 * introduced new formats that are capable of mapping 32-bit character 1307 * codes as well. And these are already used on some fonts, mainly to 1308 * map non-BMP Asian ideographs as defined in Unicode. 1309 * 1310 * For compatibility purposes, these fonts generally come with 1311 * *several* Unicode charmaps: 1312 * 1313 * - One of them in the "old" 16-bit format, that cannot access 1314 * all glyphs in the font. 1315 * 1316 * - Another one in the "new" 32-bit format, that can access all 1317 * the glyphs. 1318 * 1319 * This function has been written to always favor a 32-bit charmap 1320 * when found. Otherwise, a 16-bit one is returned when found. 1321 */ 1322 1323 /* Since the `interesting' table, with IDs (3,10), is normally the */ 1324 /* last one, we loop backwards. This loses with type1 fonts with */ 1325 /* non-BMP characters (<.0001%), this wins with .ttf with non-BMP */ 1326 /* chars (.01% ?), and this is the same about 99.99% of the time! */ 1327 1328 cur = first + face->num_charmaps; /* points after the last one */ 1329 1330 for ( ; --cur >= first; ) 1331 { 1332 if ( cur[0]->encoding == FT_ENCODING_UNICODE ) 1333 { 1334 /* XXX If some new encodings to represent UCS-4 are added, */ 1335 /* they should be added here. */ 1336 if ( ( cur[0]->platform_id == TT_PLATFORM_MICROSOFT && 1337 cur[0]->encoding_id == TT_MS_ID_UCS_4 ) || 1338 ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE && 1339 cur[0]->encoding_id == TT_APPLE_ID_UNICODE_32 ) ) 1340 { 1341 face->charmap = cur[0]; 1342 return FT_Err_Ok; 1343 } 1344 } 1345 } 1346 1347 /* We do not have any UCS-4 charmap. */ 1348 /* Do the loop again and search for UCS-2 charmaps. */ 1349 cur = first + face->num_charmaps; 1350 1351 for ( ; --cur >= first; ) 1352 { 1353 if ( cur[0]->encoding == FT_ENCODING_UNICODE ) 1354 { 1355 face->charmap = cur[0]; 1356 return FT_Err_Ok; 1357 } 1358 } 1359 1360 return FT_THROW( Invalid_CharMap_Handle ); 1361 } 1362 1363 1364 /************************************************************************** 1365 * 1366 * @Function: 1367 * find_variant_selector_charmap 1368 * 1369 * @Description: 1370 * This function finds the variant selector charmap, if there is one. 1371 * There can only be one (platform=0, specific=5, format=14). 1372 */ 1373 static FT_CharMap find_variant_selector_charmap(FT_Face face)1374 find_variant_selector_charmap( FT_Face face ) 1375 { 1376 FT_CharMap* first; 1377 FT_CharMap* end; 1378 FT_CharMap* cur; 1379 1380 1381 /* caller should have already checked that `face' is valid */ 1382 FT_ASSERT( face ); 1383 1384 first = face->charmaps; 1385 1386 if ( !first ) 1387 return NULL; 1388 1389 end = first + face->num_charmaps; /* points after the last one */ 1390 1391 for ( cur = first; cur < end; cur++ ) 1392 { 1393 if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE && 1394 cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR && 1395 FT_Get_CMap_Format( cur[0] ) == 14 ) 1396 return cur[0]; 1397 } 1398 1399 return NULL; 1400 } 1401 1402 1403 /************************************************************************** 1404 * 1405 * @Function: 1406 * open_face 1407 * 1408 * @Description: 1409 * This function does some work for FT_Open_Face(). 1410 */ 1411 static FT_Error open_face(FT_Driver driver,FT_Stream * astream,FT_Bool external_stream,FT_Long face_index,FT_Int num_params,FT_Parameter * params,FT_Face * aface)1412 open_face( FT_Driver driver, 1413 FT_Stream *astream, 1414 FT_Bool external_stream, 1415 FT_Long face_index, 1416 FT_Int num_params, 1417 FT_Parameter* params, 1418 FT_Face *aface ) 1419 { 1420 FT_Memory memory; 1421 FT_Driver_Class clazz; 1422 FT_Face face = NULL; 1423 FT_Face_Internal internal = NULL; 1424 1425 FT_Error error, error2; 1426 1427 1428 clazz = driver->clazz; 1429 memory = driver->root.memory; 1430 1431 /* allocate the face object and perform basic initialization */ 1432 if ( FT_ALLOC( face, clazz->face_object_size ) ) 1433 goto Fail; 1434 1435 face->driver = driver; 1436 face->memory = memory; 1437 face->stream = *astream; 1438 1439 /* set the FT_FACE_FLAG_EXTERNAL_STREAM bit for FT_Done_Face */ 1440 if ( external_stream ) 1441 face->face_flags |= FT_FACE_FLAG_EXTERNAL_STREAM; 1442 1443 if ( FT_NEW( internal ) ) 1444 goto Fail; 1445 1446 face->internal = internal; 1447 1448 #ifdef FT_CONFIG_OPTION_INCREMENTAL 1449 { 1450 int i; 1451 1452 1453 face->internal->incremental_interface = NULL; 1454 for ( i = 0; i < num_params && !face->internal->incremental_interface; 1455 i++ ) 1456 if ( params[i].tag == FT_PARAM_TAG_INCREMENTAL ) 1457 face->internal->incremental_interface = 1458 (FT_Incremental_Interface)params[i].data; 1459 } 1460 #endif 1461 1462 face->internal->random_seed = -1; 1463 1464 if ( clazz->init_face ) 1465 error = clazz->init_face( *astream, 1466 face, 1467 (FT_Int)face_index, 1468 num_params, 1469 params ); 1470 *astream = face->stream; /* Stream may have been changed. */ 1471 if ( error ) 1472 goto Fail; 1473 1474 /* select Unicode charmap by default */ 1475 error2 = find_unicode_charmap( face ); 1476 1477 /* if no Unicode charmap can be found, FT_Err_Invalid_CharMap_Handle */ 1478 /* is returned. */ 1479 1480 /* no error should happen, but we want to play safe */ 1481 if ( error2 && FT_ERR_NEQ( error2, Invalid_CharMap_Handle ) ) 1482 { 1483 error = error2; 1484 goto Fail; 1485 } 1486 1487 *aface = face; 1488 1489 Fail: 1490 if ( error ) 1491 { 1492 destroy_charmaps( face, memory ); 1493 if ( clazz->done_face ) 1494 clazz->done_face( face ); 1495 FT_FREE( internal ); 1496 FT_FREE( face ); 1497 *aface = NULL; 1498 } 1499 1500 return error; 1501 } 1502 1503 1504 /* there's a Mac-specific extended implementation of FT_New_Face() */ 1505 /* in src/base/ftmac.c */ 1506 1507 #ifndef FT_MACINTOSH 1508 1509 /* documentation is in freetype.h */ 1510 1511 FT_EXPORT_DEF( FT_Error ) FT_New_Face(FT_Library library,const char * pathname,FT_Long face_index,FT_Face * aface)1512 FT_New_Face( FT_Library library, 1513 const char* pathname, 1514 FT_Long face_index, 1515 FT_Face *aface ) 1516 { 1517 FT_Open_Args args; 1518 1519 1520 /* test for valid `library' and `aface' delayed to `FT_Open_Face' */ 1521 if ( !pathname ) 1522 return FT_THROW( Invalid_Argument ); 1523 1524 args.flags = FT_OPEN_PATHNAME; 1525 args.pathname = (char*)pathname; 1526 args.stream = NULL; 1527 1528 return ft_open_face_internal( library, &args, face_index, aface, 1 ); 1529 } 1530 1531 #endif 1532 1533 1534 /* documentation is in freetype.h */ 1535 1536 FT_EXPORT_DEF( FT_Error ) FT_New_Memory_Face(FT_Library library,const FT_Byte * file_base,FT_Long file_size,FT_Long face_index,FT_Face * aface)1537 FT_New_Memory_Face( FT_Library library, 1538 const FT_Byte* file_base, 1539 FT_Long file_size, 1540 FT_Long face_index, 1541 FT_Face *aface ) 1542 { 1543 FT_Open_Args args; 1544 1545 1546 /* test for valid `library' and `face' delayed to `FT_Open_Face' */ 1547 if ( !file_base ) 1548 return FT_THROW( Invalid_Argument ); 1549 1550 args.flags = FT_OPEN_MEMORY; 1551 args.memory_base = file_base; 1552 args.memory_size = file_size; 1553 args.stream = NULL; 1554 1555 return ft_open_face_internal( library, &args, face_index, aface, 1 ); 1556 } 1557 1558 1559 #ifdef FT_CONFIG_OPTION_MAC_FONTS 1560 1561 /* The behavior here is very similar to that in base/ftmac.c, but it */ 1562 /* is designed to work on non-mac systems, so no mac specific calls. */ 1563 /* */ 1564 /* We look at the file and determine if it is a mac dfont file or a mac */ 1565 /* resource file, or a macbinary file containing a mac resource file. */ 1566 /* */ 1567 /* Unlike ftmac I'm not going to look at a `FOND'. I don't really see */ 1568 /* the point, especially since there may be multiple `FOND' resources. */ 1569 /* Instead I'll just look for `sfnt' and `POST' resources, ordered as */ 1570 /* they occur in the file. */ 1571 /* */ 1572 /* Note that multiple `POST' resources do not mean multiple postscript */ 1573 /* fonts; they all get jammed together to make what is essentially a */ 1574 /* pfb file. */ 1575 /* */ 1576 /* We aren't interested in `NFNT' or `FONT' bitmap resources. */ 1577 /* */ 1578 /* As soon as we get an `sfnt' load it into memory and pass it off to */ 1579 /* FT_Open_Face. */ 1580 /* */ 1581 /* If we have a (set of) `POST' resources, massage them into a (memory) */ 1582 /* pfb file and pass that to FT_Open_Face. (As with ftmac.c I'm not */ 1583 /* going to try to save the kerning info. After all that lives in the */ 1584 /* `FOND' which isn't in the file containing the `POST' resources so */ 1585 /* we don't really have access to it. */ 1586 1587 1588 /* Finalizer for a memory stream; gets called by FT_Done_Face(). */ 1589 /* It frees the memory it uses. */ 1590 /* From `ftmac.c'. */ 1591 static void memory_stream_close(FT_Stream stream)1592 memory_stream_close( FT_Stream stream ) 1593 { 1594 FT_Memory memory = stream->memory; 1595 1596 1597 FT_FREE( stream->base ); 1598 1599 stream->size = 0; 1600 stream->base = NULL; 1601 stream->close = NULL; 1602 } 1603 1604 1605 /* Create a new memory stream from a buffer and a size. */ 1606 /* From `ftmac.c'. */ 1607 static FT_Error new_memory_stream(FT_Library library,FT_Byte * base,FT_ULong size,FT_Stream_CloseFunc close,FT_Stream * astream)1608 new_memory_stream( FT_Library library, 1609 FT_Byte* base, 1610 FT_ULong size, 1611 FT_Stream_CloseFunc close, 1612 FT_Stream *astream ) 1613 { 1614 FT_Error error; 1615 FT_Memory memory; 1616 FT_Stream stream = NULL; 1617 1618 1619 if ( !library ) 1620 return FT_THROW( Invalid_Library_Handle ); 1621 1622 if ( !base ) 1623 return FT_THROW( Invalid_Argument ); 1624 1625 *astream = NULL; 1626 memory = library->memory; 1627 if ( FT_NEW( stream ) ) 1628 goto Exit; 1629 1630 FT_Stream_OpenMemory( stream, base, size ); 1631 1632 stream->close = close; 1633 1634 *astream = stream; 1635 1636 Exit: 1637 return error; 1638 } 1639 1640 1641 /* Create a new FT_Face given a buffer and a driver name. */ 1642 /* From `ftmac.c'. */ 1643 FT_LOCAL_DEF( FT_Error ) open_face_from_buffer(FT_Library library,FT_Byte * base,FT_ULong size,FT_Long face_index,const char * driver_name,FT_Face * aface)1644 open_face_from_buffer( FT_Library library, 1645 FT_Byte* base, 1646 FT_ULong size, 1647 FT_Long face_index, 1648 const char* driver_name, 1649 FT_Face *aface ) 1650 { 1651 FT_Open_Args args; 1652 FT_Error error; 1653 FT_Stream stream = NULL; 1654 FT_Memory memory = library->memory; 1655 1656 1657 error = new_memory_stream( library, 1658 base, 1659 size, 1660 memory_stream_close, 1661 &stream ); 1662 if ( error ) 1663 { 1664 FT_FREE( base ); 1665 return error; 1666 } 1667 1668 args.flags = FT_OPEN_STREAM; 1669 args.stream = stream; 1670 if ( driver_name ) 1671 { 1672 args.flags = args.flags | FT_OPEN_DRIVER; 1673 args.driver = FT_Get_Module( library, driver_name ); 1674 } 1675 1676 #ifdef FT_MACINTOSH 1677 /* At this point, the face index has served its purpose; */ 1678 /* whoever calls this function has already used it to */ 1679 /* locate the correct font data. We should not propagate */ 1680 /* this index to FT_Open_Face() (unless it is negative). */ 1681 1682 if ( face_index > 0 ) 1683 face_index &= 0x7FFF0000L; /* retain GX data */ 1684 #endif 1685 1686 error = ft_open_face_internal( library, &args, face_index, aface, 0 ); 1687 1688 if ( !error ) 1689 (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM; 1690 else 1691 #ifdef FT_MACINTOSH 1692 FT_Stream_Free( stream, 0 ); 1693 #else 1694 { 1695 FT_Stream_Close( stream ); 1696 FT_FREE( stream ); 1697 } 1698 #endif 1699 1700 return error; 1701 } 1702 1703 1704 /* Look up `TYP1' or `CID ' table from sfnt table directory. */ 1705 /* `offset' and `length' must exclude the binary header in tables. */ 1706 1707 /* Type 1 and CID-keyed font drivers should recognize sfnt-wrapped */ 1708 /* format too. Here, since we can't expect that the TrueType font */ 1709 /* driver is loaded unconditionally, we must parse the font by */ 1710 /* ourselves. We are only interested in the name of the table and */ 1711 /* the offset. */ 1712 1713 static FT_Error ft_lookup_PS_in_sfnt_stream(FT_Stream stream,FT_Long face_index,FT_ULong * offset,FT_ULong * length,FT_Bool * is_sfnt_cid)1714 ft_lookup_PS_in_sfnt_stream( FT_Stream stream, 1715 FT_Long face_index, 1716 FT_ULong* offset, 1717 FT_ULong* length, 1718 FT_Bool* is_sfnt_cid ) 1719 { 1720 FT_Error error; 1721 FT_UShort numTables; 1722 FT_Long pstable_index; 1723 FT_ULong tag; 1724 int i; 1725 1726 1727 *offset = 0; 1728 *length = 0; 1729 *is_sfnt_cid = FALSE; 1730 1731 /* TODO: support for sfnt-wrapped PS/CID in TTC format */ 1732 1733 /* version check for 'typ1' (should be ignored?) */ 1734 if ( FT_READ_ULONG( tag ) ) 1735 return error; 1736 if ( tag != TTAG_typ1 ) 1737 return FT_THROW( Unknown_File_Format ); 1738 1739 if ( FT_READ_USHORT( numTables ) ) 1740 return error; 1741 if ( FT_STREAM_SKIP( 2 * 3 ) ) /* skip binary search header */ 1742 return error; 1743 1744 pstable_index = -1; 1745 *is_sfnt_cid = FALSE; 1746 1747 for ( i = 0; i < numTables; i++ ) 1748 { 1749 if ( FT_READ_ULONG( tag ) || FT_STREAM_SKIP( 4 ) || 1750 FT_READ_ULONG( *offset ) || FT_READ_ULONG( *length ) ) 1751 return error; 1752 1753 if ( tag == TTAG_CID ) 1754 { 1755 pstable_index++; 1756 *offset += 22; 1757 *length -= 22; 1758 *is_sfnt_cid = TRUE; 1759 if ( face_index < 0 ) 1760 return FT_Err_Ok; 1761 } 1762 else if ( tag == TTAG_TYP1 ) 1763 { 1764 pstable_index++; 1765 *offset += 24; 1766 *length -= 24; 1767 *is_sfnt_cid = FALSE; 1768 if ( face_index < 0 ) 1769 return FT_Err_Ok; 1770 } 1771 if ( face_index >= 0 && pstable_index == face_index ) 1772 return FT_Err_Ok; 1773 } 1774 1775 return FT_THROW( Table_Missing ); 1776 } 1777 1778 1779 FT_LOCAL_DEF( FT_Error ) open_face_PS_from_sfnt_stream(FT_Library library,FT_Stream stream,FT_Long face_index,FT_Int num_params,FT_Parameter * params,FT_Face * aface)1780 open_face_PS_from_sfnt_stream( FT_Library library, 1781 FT_Stream stream, 1782 FT_Long face_index, 1783 FT_Int num_params, 1784 FT_Parameter *params, 1785 FT_Face *aface ) 1786 { 1787 FT_Error error; 1788 FT_Memory memory = library->memory; 1789 FT_ULong offset, length; 1790 FT_ULong pos; 1791 FT_Bool is_sfnt_cid; 1792 FT_Byte* sfnt_ps = NULL; 1793 1794 FT_UNUSED( num_params ); 1795 FT_UNUSED( params ); 1796 1797 1798 /* ignore GX stuff */ 1799 if ( face_index > 0 ) 1800 face_index &= 0xFFFFL; 1801 1802 pos = FT_STREAM_POS(); 1803 1804 error = ft_lookup_PS_in_sfnt_stream( stream, 1805 face_index, 1806 &offset, 1807 &length, 1808 &is_sfnt_cid ); 1809 if ( error ) 1810 goto Exit; 1811 1812 if ( offset > stream->size ) 1813 { 1814 FT_TRACE2(( "open_face_PS_from_sfnt_stream: invalid table offset\n" )); 1815 error = FT_THROW( Invalid_Table ); 1816 goto Exit; 1817 } 1818 else if ( length > stream->size - offset ) 1819 { 1820 FT_TRACE2(( "open_face_PS_from_sfnt_stream: invalid table length\n" )); 1821 error = FT_THROW( Invalid_Table ); 1822 goto Exit; 1823 } 1824 1825 error = FT_Stream_Seek( stream, pos + offset ); 1826 if ( error ) 1827 goto Exit; 1828 1829 if ( FT_QALLOC( sfnt_ps, (FT_Long)length ) ) 1830 goto Exit; 1831 1832 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_ps, length ); 1833 if ( error ) 1834 { 1835 FT_FREE( sfnt_ps ); 1836 goto Exit; 1837 } 1838 1839 error = open_face_from_buffer( library, 1840 sfnt_ps, 1841 length, 1842 FT_MIN( face_index, 0 ), 1843 is_sfnt_cid ? "cid" : "type1", 1844 aface ); 1845 Exit: 1846 { 1847 FT_Error error1; 1848 1849 1850 if ( FT_ERR_EQ( error, Unknown_File_Format ) ) 1851 { 1852 error1 = FT_Stream_Seek( stream, pos ); 1853 if ( error1 ) 1854 return error1; 1855 } 1856 1857 return error; 1858 } 1859 } 1860 1861 1862 #ifndef FT_MACINTOSH 1863 1864 /* The resource header says we've got resource_cnt `POST' (type1) */ 1865 /* resources in this file. They all need to be coalesced into */ 1866 /* one lump which gets passed on to the type1 driver. */ 1867 /* Here can be only one PostScript font in a file so face_index */ 1868 /* must be 0 (or -1). */ 1869 /* */ 1870 static FT_Error Mac_Read_POST_Resource(FT_Library library,FT_Stream stream,FT_Long * offsets,FT_Long resource_cnt,FT_Long face_index,FT_Face * aface)1871 Mac_Read_POST_Resource( FT_Library library, 1872 FT_Stream stream, 1873 FT_Long *offsets, 1874 FT_Long resource_cnt, 1875 FT_Long face_index, 1876 FT_Face *aface ) 1877 { 1878 FT_Error error = FT_ERR( Cannot_Open_Resource ); 1879 FT_Memory memory = library->memory; 1880 1881 FT_Byte* pfb_data = NULL; 1882 int i, type, flags; 1883 FT_ULong len; 1884 FT_ULong pfb_len, pfb_pos, pfb_lenpos; 1885 FT_ULong rlen, temp; 1886 1887 1888 if ( face_index == -1 ) 1889 face_index = 0; 1890 if ( face_index != 0 ) 1891 return error; 1892 1893 /* Find the length of all the POST resources, concatenated. Assume */ 1894 /* worst case (each resource in its own section). */ 1895 pfb_len = 0; 1896 for ( i = 0; i < resource_cnt; i++ ) 1897 { 1898 error = FT_Stream_Seek( stream, (FT_ULong)offsets[i] ); 1899 if ( error ) 1900 goto Exit; 1901 if ( FT_READ_ULONG( temp ) ) /* actually LONG */ 1902 goto Exit; 1903 1904 /* FT2 allocator takes signed long buffer length, 1905 * too large value causing overflow should be checked 1906 */ 1907 FT_TRACE4(( " POST fragment #%d: length=0x%08lx" 1908 " total pfb_len=0x%08lx\n", 1909 i, temp, pfb_len + temp + 6 )); 1910 1911 if ( FT_MAC_RFORK_MAX_LEN < temp || 1912 FT_MAC_RFORK_MAX_LEN - temp < pfb_len + 6 ) 1913 { 1914 FT_TRACE2(( " MacOS resource length cannot exceed" 1915 " 0x%08lx\n", 1916 FT_MAC_RFORK_MAX_LEN )); 1917 1918 error = FT_THROW( Invalid_Offset ); 1919 goto Exit; 1920 } 1921 1922 pfb_len += temp + 6; 1923 } 1924 1925 FT_TRACE2(( " total buffer size to concatenate" 1926 " %ld POST fragments: 0x%08lx\n", 1927 resource_cnt, pfb_len + 2 )); 1928 1929 if ( pfb_len + 2 < 6 ) 1930 { 1931 FT_TRACE2(( " too long fragment length makes" 1932 " pfb_len confused: pfb_len=0x%08lx\n", 1933 pfb_len )); 1934 1935 error = FT_THROW( Array_Too_Large ); 1936 goto Exit; 1937 } 1938 1939 if ( FT_QALLOC( pfb_data, (FT_Long)pfb_len + 2 ) ) 1940 goto Exit; 1941 1942 pfb_data[0] = 0x80; 1943 pfb_data[1] = 1; /* Ascii section */ 1944 pfb_data[2] = 0; /* 4-byte length, fill in later */ 1945 pfb_data[3] = 0; 1946 pfb_data[4] = 0; 1947 pfb_data[5] = 0; 1948 pfb_pos = 6; 1949 pfb_lenpos = 2; 1950 1951 len = 0; 1952 type = 1; 1953 1954 for ( i = 0; i < resource_cnt; i++ ) 1955 { 1956 error = FT_Stream_Seek( stream, (FT_ULong)offsets[i] ); 1957 if ( error ) 1958 goto Exit2; 1959 if ( FT_READ_ULONG( rlen ) ) 1960 goto Exit2; 1961 1962 /* FT2 allocator takes signed long buffer length, 1963 * too large fragment length causing overflow should be checked 1964 */ 1965 if ( 0x7FFFFFFFUL < rlen ) 1966 { 1967 error = FT_THROW( Invalid_Offset ); 1968 goto Exit2; 1969 } 1970 1971 if ( FT_READ_USHORT( flags ) ) 1972 goto Exit2; 1973 1974 FT_TRACE3(( "POST fragment[%d]:" 1975 " offsets=0x%08lx, rlen=0x%08lx, flags=0x%04x\n", 1976 i, offsets[i], rlen, flags )); 1977 1978 error = FT_ERR( Array_Too_Large ); 1979 1980 /* postpone the check of `rlen longer than buffer' */ 1981 /* until `FT_Stream_Read' */ 1982 1983 if ( ( flags >> 8 ) == 0 ) /* Comment, should not be loaded */ 1984 { 1985 FT_TRACE3(( " Skip POST fragment #%d because it is a comment\n", 1986 i )); 1987 continue; 1988 } 1989 1990 /* the flags are part of the resource, so rlen >= 2, */ 1991 /* but some fonts declare rlen = 0 for empty fragment */ 1992 if ( rlen > 2 ) 1993 rlen -= 2; 1994 else 1995 rlen = 0; 1996 1997 if ( ( flags >> 8 ) == type ) 1998 len += rlen; 1999 else 2000 { 2001 FT_TRACE3(( " Write POST fragment #%d header (4-byte) to buffer" 2002 " %p + 0x%08lx\n", 2003 i, (void*)pfb_data, pfb_lenpos )); 2004 2005 if ( pfb_lenpos + 3 > pfb_len + 2 ) 2006 goto Exit2; 2007 2008 pfb_data[pfb_lenpos ] = (FT_Byte)( len ); 2009 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 ); 2010 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 ); 2011 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 ); 2012 2013 if ( ( flags >> 8 ) == 5 ) /* End of font mark */ 2014 break; 2015 2016 FT_TRACE3(( " Write POST fragment #%d header (6-byte) to buffer" 2017 " %p + 0x%08lx\n", 2018 i, (void*)pfb_data, pfb_pos )); 2019 2020 if ( pfb_pos + 6 > pfb_len + 2 ) 2021 goto Exit2; 2022 2023 pfb_data[pfb_pos++] = 0x80; 2024 2025 type = flags >> 8; 2026 len = rlen; 2027 2028 pfb_data[pfb_pos++] = (FT_Byte)type; 2029 pfb_lenpos = pfb_pos; 2030 pfb_data[pfb_pos++] = 0; /* 4-byte length, fill in later */ 2031 pfb_data[pfb_pos++] = 0; 2032 pfb_data[pfb_pos++] = 0; 2033 pfb_data[pfb_pos++] = 0; 2034 } 2035 2036 if ( pfb_pos > pfb_len || pfb_pos + rlen > pfb_len ) 2037 goto Exit2; 2038 2039 FT_TRACE3(( " Load POST fragment #%d (%ld byte) to buffer" 2040 " %p + 0x%08lx\n", 2041 i, rlen, (void*)pfb_data, pfb_pos )); 2042 2043 error = FT_Stream_Read( stream, (FT_Byte *)pfb_data + pfb_pos, rlen ); 2044 if ( error ) 2045 goto Exit2; 2046 2047 pfb_pos += rlen; 2048 } 2049 2050 error = FT_ERR( Array_Too_Large ); 2051 2052 if ( pfb_pos + 2 > pfb_len + 2 ) 2053 goto Exit2; 2054 pfb_data[pfb_pos++] = 0x80; 2055 pfb_data[pfb_pos++] = 3; 2056 2057 if ( pfb_lenpos + 3 > pfb_len + 2 ) 2058 goto Exit2; 2059 pfb_data[pfb_lenpos ] = (FT_Byte)( len ); 2060 pfb_data[pfb_lenpos + 1] = (FT_Byte)( len >> 8 ); 2061 pfb_data[pfb_lenpos + 2] = (FT_Byte)( len >> 16 ); 2062 pfb_data[pfb_lenpos + 3] = (FT_Byte)( len >> 24 ); 2063 2064 return open_face_from_buffer( library, 2065 pfb_data, 2066 pfb_pos, 2067 face_index, 2068 "type1", 2069 aface ); 2070 2071 Exit2: 2072 if ( FT_ERR_EQ( error, Array_Too_Large ) ) 2073 FT_TRACE2(( " Abort due to too-short buffer to store" 2074 " all POST fragments\n" )); 2075 else if ( FT_ERR_EQ( error, Invalid_Offset ) ) 2076 FT_TRACE2(( " Abort due to invalid offset in a POST fragment\n" )); 2077 2078 if ( error ) 2079 error = FT_ERR( Cannot_Open_Resource ); 2080 FT_FREE( pfb_data ); 2081 2082 Exit: 2083 return error; 2084 } 2085 2086 2087 /* The resource header says we've got resource_cnt `sfnt' */ 2088 /* (TrueType/OpenType) resources in this file. Look through */ 2089 /* them for the one indicated by face_index, load it into mem, */ 2090 /* pass it on to the truetype driver, and return it. */ 2091 /* */ 2092 static FT_Error Mac_Read_sfnt_Resource(FT_Library library,FT_Stream stream,FT_Long * offsets,FT_Long resource_cnt,FT_Long face_index,FT_Face * aface)2093 Mac_Read_sfnt_Resource( FT_Library library, 2094 FT_Stream stream, 2095 FT_Long *offsets, 2096 FT_Long resource_cnt, 2097 FT_Long face_index, 2098 FT_Face *aface ) 2099 { 2100 FT_Memory memory = library->memory; 2101 FT_Byte* sfnt_data = NULL; 2102 FT_Error error; 2103 FT_ULong flag_offset; 2104 FT_Long rlen; 2105 int is_cff; 2106 FT_Long face_index_in_resource = 0; 2107 2108 2109 if ( face_index < 0 ) 2110 face_index = -face_index - 1; 2111 if ( face_index >= resource_cnt ) 2112 return FT_THROW( Cannot_Open_Resource ); 2113 2114 flag_offset = (FT_ULong)offsets[face_index]; 2115 error = FT_Stream_Seek( stream, flag_offset ); 2116 if ( error ) 2117 goto Exit; 2118 2119 if ( FT_READ_LONG( rlen ) ) 2120 goto Exit; 2121 if ( rlen < 1 ) 2122 return FT_THROW( Cannot_Open_Resource ); 2123 if ( (FT_ULong)rlen > FT_MAC_RFORK_MAX_LEN ) 2124 return FT_THROW( Invalid_Offset ); 2125 2126 error = open_face_PS_from_sfnt_stream( library, 2127 stream, 2128 face_index, 2129 0, NULL, 2130 aface ); 2131 if ( !error ) 2132 goto Exit; 2133 2134 /* rewind sfnt stream before open_face_PS_from_sfnt_stream() */ 2135 error = FT_Stream_Seek( stream, flag_offset + 4 ); 2136 if ( error ) 2137 goto Exit; 2138 2139 if ( FT_QALLOC( sfnt_data, rlen ) ) 2140 return error; 2141 error = FT_Stream_Read( stream, (FT_Byte *)sfnt_data, (FT_ULong)rlen ); 2142 if ( error ) { 2143 FT_FREE( sfnt_data ); 2144 goto Exit; 2145 } 2146 2147 is_cff = rlen > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 ); 2148 error = open_face_from_buffer( library, 2149 sfnt_data, 2150 (FT_ULong)rlen, 2151 face_index_in_resource, 2152 is_cff ? "cff" : "truetype", 2153 aface ); 2154 2155 Exit: 2156 return error; 2157 } 2158 2159 2160 /* Check for a valid resource fork header, or a valid dfont */ 2161 /* header. In a resource fork the first 16 bytes are repeated */ 2162 /* at the location specified by bytes 4-7. In a dfont bytes */ 2163 /* 4-7 point to 16 bytes of zeroes instead. */ 2164 /* */ 2165 static FT_Error IsMacResource(FT_Library library,FT_Stream stream,FT_Long resource_offset,FT_Long face_index,FT_Face * aface)2166 IsMacResource( FT_Library library, 2167 FT_Stream stream, 2168 FT_Long resource_offset, 2169 FT_Long face_index, 2170 FT_Face *aface ) 2171 { 2172 FT_Memory memory = library->memory; 2173 FT_Error error; 2174 FT_Long map_offset, rdata_pos; 2175 FT_Long *data_offsets; 2176 FT_Long count; 2177 2178 2179 error = FT_Raccess_Get_HeaderInfo( library, stream, resource_offset, 2180 &map_offset, &rdata_pos ); 2181 if ( error ) 2182 return error; 2183 2184 /* POST resources must be sorted to concatenate properly */ 2185 error = FT_Raccess_Get_DataOffsets( library, stream, 2186 map_offset, rdata_pos, 2187 TTAG_POST, TRUE, 2188 &data_offsets, &count ); 2189 if ( !error ) 2190 { 2191 error = Mac_Read_POST_Resource( library, stream, data_offsets, count, 2192 face_index, aface ); 2193 FT_FREE( data_offsets ); 2194 /* POST exists in an LWFN providing a single face */ 2195 if ( !error ) 2196 (*aface)->num_faces = 1; 2197 return error; 2198 } 2199 2200 /* sfnt resources should not be sorted to preserve the face order by 2201 QuickDraw API */ 2202 error = FT_Raccess_Get_DataOffsets( library, stream, 2203 map_offset, rdata_pos, 2204 TTAG_sfnt, FALSE, 2205 &data_offsets, &count ); 2206 if ( !error ) 2207 { 2208 FT_Long face_index_internal = face_index % count; 2209 2210 2211 error = Mac_Read_sfnt_Resource( library, stream, data_offsets, count, 2212 face_index_internal, aface ); 2213 FT_FREE( data_offsets ); 2214 if ( !error ) 2215 (*aface)->num_faces = count; 2216 } 2217 2218 return error; 2219 } 2220 2221 2222 /* Check for a valid macbinary header, and if we find one */ 2223 /* check that the (flattened) resource fork in it is valid. */ 2224 /* */ 2225 static FT_Error IsMacBinary(FT_Library library,FT_Stream stream,FT_Long face_index,FT_Face * aface)2226 IsMacBinary( FT_Library library, 2227 FT_Stream stream, 2228 FT_Long face_index, 2229 FT_Face *aface ) 2230 { 2231 unsigned char header[128]; 2232 FT_Error error; 2233 FT_Long dlen, offset; 2234 2235 2236 if ( !stream ) 2237 return FT_THROW( Invalid_Stream_Operation ); 2238 2239 error = FT_Stream_Seek( stream, 0 ); 2240 if ( error ) 2241 goto Exit; 2242 2243 error = FT_Stream_Read( stream, (FT_Byte*)header, 128 ); 2244 if ( error ) 2245 goto Exit; 2246 2247 if ( header[ 0] != 0 || 2248 header[74] != 0 || 2249 header[82] != 0 || 2250 header[ 1] == 0 || 2251 header[ 1] > 33 || 2252 header[63] != 0 || 2253 header[2 + header[1]] != 0 || 2254 header[0x53] > 0x7F ) 2255 return FT_THROW( Unknown_File_Format ); 2256 2257 dlen = ( header[0x53] << 24 ) | 2258 ( header[0x54] << 16 ) | 2259 ( header[0x55] << 8 ) | 2260 header[0x56]; 2261 #if 0 2262 rlen = ( header[0x57] << 24 ) | 2263 ( header[0x58] << 16 ) | 2264 ( header[0x59] << 8 ) | 2265 header[0x5A]; 2266 #endif /* 0 */ 2267 offset = 128 + ( ( dlen + 127 ) & ~127 ); 2268 2269 return IsMacResource( library, stream, offset, face_index, aface ); 2270 2271 Exit: 2272 return error; 2273 } 2274 2275 2276 static FT_Error load_face_in_embedded_rfork(FT_Library library,FT_Stream stream,FT_Long face_index,FT_Face * aface,const FT_Open_Args * args)2277 load_face_in_embedded_rfork( FT_Library library, 2278 FT_Stream stream, 2279 FT_Long face_index, 2280 FT_Face *aface, 2281 const FT_Open_Args *args ) 2282 { 2283 2284 #undef FT_COMPONENT 2285 #define FT_COMPONENT raccess 2286 2287 FT_Memory memory = library->memory; 2288 FT_Error error = FT_ERR( Unknown_File_Format ); 2289 FT_UInt i; 2290 2291 char* file_names[FT_RACCESS_N_RULES]; 2292 FT_Long offsets[FT_RACCESS_N_RULES]; 2293 FT_Error errors[FT_RACCESS_N_RULES]; 2294 FT_Bool is_darwin_vfs, vfs_rfork_has_no_font = FALSE; /* not tested */ 2295 2296 FT_Open_Args args2; 2297 FT_Stream stream2 = NULL; 2298 2299 2300 FT_Raccess_Guess( library, stream, 2301 args->pathname, file_names, offsets, errors ); 2302 2303 for ( i = 0; i < FT_RACCESS_N_RULES; i++ ) 2304 { 2305 is_darwin_vfs = ft_raccess_rule_by_darwin_vfs( library, i ); 2306 if ( is_darwin_vfs && vfs_rfork_has_no_font ) 2307 { 2308 FT_TRACE3(( "Skip rule %d: darwin vfs resource fork" 2309 " is already checked and" 2310 " no font is found\n", 2311 i )); 2312 continue; 2313 } 2314 2315 if ( errors[i] ) 2316 { 2317 FT_TRACE3(( "Error 0x%x has occurred in rule %d\n", 2318 errors[i], i )); 2319 continue; 2320 } 2321 2322 args2.flags = FT_OPEN_PATHNAME; 2323 args2.pathname = file_names[i] ? file_names[i] : args->pathname; 2324 2325 FT_TRACE3(( "Try rule %d: %s (offset=%ld) ...", 2326 i, args2.pathname, offsets[i] )); 2327 2328 error = FT_Stream_New( library, &args2, &stream2 ); 2329 if ( is_darwin_vfs && FT_ERR_EQ( error, Cannot_Open_Stream ) ) 2330 vfs_rfork_has_no_font = TRUE; 2331 2332 if ( error ) 2333 { 2334 FT_TRACE3(( "failed\n" )); 2335 continue; 2336 } 2337 2338 error = IsMacResource( library, stream2, offsets[i], 2339 face_index, aface ); 2340 FT_Stream_Free( stream2, 0 ); 2341 2342 FT_TRACE3(( "%s\n", error ? "failed": "successful" )); 2343 2344 if ( !error ) 2345 break; 2346 else if ( is_darwin_vfs ) 2347 vfs_rfork_has_no_font = TRUE; 2348 } 2349 2350 for (i = 0; i < FT_RACCESS_N_RULES; i++) 2351 { 2352 if ( file_names[i] ) 2353 FT_FREE( file_names[i] ); 2354 } 2355 2356 /* Caller (load_mac_face) requires FT_Err_Unknown_File_Format. */ 2357 if ( error ) 2358 error = FT_ERR( Unknown_File_Format ); 2359 2360 return error; 2361 2362 #undef FT_COMPONENT 2363 #define FT_COMPONENT objs 2364 2365 } 2366 2367 2368 /* Check for some macintosh formats without Carbon framework. */ 2369 /* Is this a macbinary file? If so look at the resource fork. */ 2370 /* Is this a mac dfont file? */ 2371 /* Is this an old style resource fork? (in data) */ 2372 /* Else call load_face_in_embedded_rfork to try extra rules */ 2373 /* (defined in `ftrfork.c'). */ 2374 /* */ 2375 static FT_Error load_mac_face(FT_Library library,FT_Stream stream,FT_Long face_index,FT_Face * aface,const FT_Open_Args * args)2376 load_mac_face( FT_Library library, 2377 FT_Stream stream, 2378 FT_Long face_index, 2379 FT_Face *aface, 2380 const FT_Open_Args *args ) 2381 { 2382 FT_Error error; 2383 FT_UNUSED( args ); 2384 2385 2386 error = IsMacBinary( library, stream, face_index, aface ); 2387 if ( FT_ERR_EQ( error, Unknown_File_Format ) ) 2388 { 2389 2390 #undef FT_COMPONENT 2391 #define FT_COMPONENT raccess 2392 2393 #ifdef FT_DEBUG_LEVEL_TRACE 2394 FT_TRACE3(( "Try as dfont: " )); 2395 if ( !( args->flags & FT_OPEN_MEMORY ) ) 2396 FT_TRACE3(( "%s ...", args->pathname )); 2397 #endif 2398 2399 error = IsMacResource( library, stream, 0, face_index, aface ); 2400 2401 FT_TRACE3(( "%s\n", error ? "failed" : "successful" )); 2402 2403 #undef FT_COMPONENT 2404 #define FT_COMPONENT objs 2405 2406 } 2407 2408 if ( ( FT_ERR_EQ( error, Unknown_File_Format ) || 2409 FT_ERR_EQ( error, Invalid_Stream_Operation ) ) && 2410 ( args->flags & FT_OPEN_PATHNAME ) ) 2411 error = load_face_in_embedded_rfork( library, stream, 2412 face_index, aface, args ); 2413 return error; 2414 } 2415 #endif 2416 2417 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */ 2418 2419 2420 /* documentation is in freetype.h */ 2421 2422 FT_EXPORT_DEF( FT_Error ) FT_Open_Face(FT_Library library,const FT_Open_Args * args,FT_Long face_index,FT_Face * aface)2423 FT_Open_Face( FT_Library library, 2424 const FT_Open_Args* args, 2425 FT_Long face_index, 2426 FT_Face *aface ) 2427 { 2428 return ft_open_face_internal( library, args, face_index, aface, 1 ); 2429 } 2430 2431 2432 static FT_Error ft_open_face_internal(FT_Library library,const FT_Open_Args * args,FT_Long face_index,FT_Face * aface,FT_Bool test_mac_fonts)2433 ft_open_face_internal( FT_Library library, 2434 const FT_Open_Args* args, 2435 FT_Long face_index, 2436 FT_Face *aface, 2437 FT_Bool test_mac_fonts ) 2438 { 2439 FT_Error error; 2440 FT_Driver driver = NULL; 2441 FT_Memory memory = NULL; 2442 FT_Stream stream = NULL; 2443 FT_Face face = NULL; 2444 FT_ListNode node = NULL; 2445 FT_Bool external_stream; 2446 FT_Module* cur; 2447 FT_Module* limit; 2448 2449 #ifndef FT_CONFIG_OPTION_MAC_FONTS 2450 FT_UNUSED( test_mac_fonts ); 2451 #endif 2452 2453 2454 #ifdef FT_DEBUG_LEVEL_TRACE 2455 FT_TRACE3(( "FT_Open_Face: " )); 2456 if ( face_index < 0 ) 2457 FT_TRACE3(( "Requesting number of faces and named instances\n")); 2458 else 2459 { 2460 FT_TRACE3(( "Requesting face %ld", face_index & 0xFFFFL )); 2461 if ( face_index & 0x7FFF0000L ) 2462 FT_TRACE3(( ", named instance %ld", face_index >> 16 )); 2463 FT_TRACE3(( "\n" )); 2464 } 2465 #endif 2466 2467 /* test for valid `library' delayed to `FT_Stream_New' */ 2468 2469 if ( ( !aface && face_index >= 0 ) || !args ) 2470 return FT_THROW( Invalid_Argument ); 2471 2472 external_stream = FT_BOOL( ( args->flags & FT_OPEN_STREAM ) && 2473 args->stream ); 2474 2475 /* create input stream */ 2476 error = FT_Stream_New( library, args, &stream ); 2477 if ( error ) 2478 goto Fail3; 2479 2480 memory = library->memory; 2481 2482 /* If the font driver is specified in the `args' structure, use */ 2483 /* it. Otherwise, we scan the list of registered drivers. */ 2484 if ( ( args->flags & FT_OPEN_DRIVER ) && args->driver ) 2485 { 2486 driver = FT_DRIVER( args->driver ); 2487 2488 /* not all modules are drivers, so check... */ 2489 if ( FT_MODULE_IS_DRIVER( driver ) ) 2490 { 2491 FT_Int num_params = 0; 2492 FT_Parameter* params = NULL; 2493 2494 2495 if ( args->flags & FT_OPEN_PARAMS ) 2496 { 2497 num_params = args->num_params; 2498 params = args->params; 2499 } 2500 2501 error = open_face( driver, &stream, external_stream, face_index, 2502 num_params, params, &face ); 2503 if ( !error ) 2504 goto Success; 2505 } 2506 else 2507 error = FT_THROW( Invalid_Handle ); 2508 2509 FT_Stream_Free( stream, external_stream ); 2510 goto Fail; 2511 } 2512 else 2513 { 2514 error = FT_ERR( Missing_Module ); 2515 2516 /* check each font driver for an appropriate format */ 2517 cur = library->modules; 2518 limit = cur + library->num_modules; 2519 2520 for ( ; cur < limit; cur++ ) 2521 { 2522 /* not all modules are font drivers, so check... */ 2523 if ( FT_MODULE_IS_DRIVER( cur[0] ) ) 2524 { 2525 FT_Int num_params = 0; 2526 FT_Parameter* params = NULL; 2527 2528 2529 driver = FT_DRIVER( cur[0] ); 2530 2531 if ( args->flags & FT_OPEN_PARAMS ) 2532 { 2533 num_params = args->num_params; 2534 params = args->params; 2535 } 2536 2537 error = open_face( driver, &stream, external_stream, face_index, 2538 num_params, params, &face ); 2539 if ( !error ) 2540 goto Success; 2541 2542 #ifdef FT_CONFIG_OPTION_MAC_FONTS 2543 if ( test_mac_fonts && 2544 ft_strcmp( cur[0]->clazz->module_name, "truetype" ) == 0 && 2545 FT_ERR_EQ( error, Table_Missing ) ) 2546 { 2547 /* TrueType but essential tables are missing */ 2548 error = FT_Stream_Seek( stream, 0 ); 2549 if ( error ) 2550 break; 2551 2552 error = open_face_PS_from_sfnt_stream( library, 2553 stream, 2554 face_index, 2555 num_params, 2556 params, 2557 aface ); 2558 if ( !error ) 2559 { 2560 FT_Stream_Free( stream, external_stream ); 2561 return error; 2562 } 2563 } 2564 #endif 2565 2566 if ( FT_ERR_NEQ( error, Unknown_File_Format ) ) 2567 goto Fail3; 2568 } 2569 } 2570 2571 Fail3: 2572 /* If we are on the mac, and we get an */ 2573 /* FT_Err_Invalid_Stream_Operation it may be because we have an */ 2574 /* empty data fork, so we need to check the resource fork. */ 2575 if ( FT_ERR_NEQ( error, Cannot_Open_Stream ) && 2576 FT_ERR_NEQ( error, Unknown_File_Format ) && 2577 FT_ERR_NEQ( error, Invalid_Stream_Operation ) ) 2578 goto Fail2; 2579 2580 #if !defined( FT_MACINTOSH ) && defined( FT_CONFIG_OPTION_MAC_FONTS ) 2581 if ( test_mac_fonts ) 2582 { 2583 error = load_mac_face( library, stream, face_index, aface, args ); 2584 if ( !error ) 2585 { 2586 /* We don't want to go to Success here. We've already done */ 2587 /* that. On the other hand, if we succeeded we still need to */ 2588 /* close this stream (we opened a different stream which */ 2589 /* extracted the interesting information out of this stream */ 2590 /* here. That stream will still be open and the face will */ 2591 /* point to it). */ 2592 FT_Stream_Free( stream, external_stream ); 2593 return error; 2594 } 2595 } 2596 2597 if ( FT_ERR_NEQ( error, Unknown_File_Format ) ) 2598 goto Fail2; 2599 #endif /* !FT_MACINTOSH && FT_CONFIG_OPTION_MAC_FONTS */ 2600 2601 /* no driver is able to handle this format */ 2602 error = FT_THROW( Unknown_File_Format ); 2603 2604 Fail2: 2605 FT_Stream_Free( stream, external_stream ); 2606 goto Fail; 2607 } 2608 2609 Success: 2610 FT_TRACE4(( "FT_Open_Face: New face object, adding to list\n" )); 2611 2612 /* add the face object to its driver's list */ 2613 if ( FT_NEW( node ) ) 2614 goto Fail; 2615 2616 node->data = face; 2617 /* don't assume driver is the same as face->driver, so use */ 2618 /* face->driver instead. */ 2619 FT_List_Add( &face->driver->faces_list, node ); 2620 2621 /* now allocate a glyph slot object for the face */ 2622 FT_TRACE4(( "FT_Open_Face: Creating glyph slot\n" )); 2623 2624 if ( face_index >= 0 ) 2625 { 2626 error = FT_New_GlyphSlot( face, NULL ); 2627 if ( error ) 2628 goto Fail; 2629 2630 /* finally, allocate a size object for the face */ 2631 { 2632 FT_Size size; 2633 2634 2635 FT_TRACE4(( "FT_Open_Face: Creating size object\n" )); 2636 2637 error = FT_New_Size( face, &size ); 2638 if ( error ) 2639 goto Fail; 2640 2641 face->size = size; 2642 } 2643 } 2644 2645 /* some checks */ 2646 2647 if ( FT_IS_SCALABLE( face ) ) 2648 { 2649 if ( face->height < 0 ) 2650 face->height = (FT_Short)-face->height; 2651 2652 if ( !FT_HAS_VERTICAL( face ) ) 2653 face->max_advance_height = (FT_Short)face->height; 2654 } 2655 2656 if ( FT_HAS_FIXED_SIZES( face ) ) 2657 { 2658 FT_Int i; 2659 2660 2661 for ( i = 0; i < face->num_fixed_sizes; i++ ) 2662 { 2663 FT_Bitmap_Size* bsize = face->available_sizes + i; 2664 2665 2666 if ( bsize->height < 0 ) 2667 bsize->height = -bsize->height; 2668 if ( bsize->x_ppem < 0 ) 2669 bsize->x_ppem = -bsize->x_ppem; 2670 if ( bsize->y_ppem < 0 ) 2671 bsize->y_ppem = -bsize->y_ppem; 2672 2673 /* check whether negation actually has worked */ 2674 if ( bsize->height < 0 || bsize->x_ppem < 0 || bsize->y_ppem < 0 ) 2675 { 2676 FT_TRACE0(( "FT_Open_Face:" 2677 " Invalid bitmap dimensions for strike %d," 2678 " now disabled\n", i )); 2679 bsize->width = 0; 2680 bsize->height = 0; 2681 bsize->size = 0; 2682 bsize->x_ppem = 0; 2683 bsize->y_ppem = 0; 2684 } 2685 } 2686 } 2687 2688 /* initialize internal face data */ 2689 { 2690 FT_Face_Internal internal = face->internal; 2691 2692 2693 internal->transform_matrix.xx = 0x10000L; 2694 internal->transform_matrix.xy = 0; 2695 internal->transform_matrix.yx = 0; 2696 internal->transform_matrix.yy = 0x10000L; 2697 2698 internal->transform_delta.x = 0; 2699 internal->transform_delta.y = 0; 2700 2701 internal->refcount = 1; 2702 2703 internal->no_stem_darkening = -1; 2704 2705 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING 2706 /* Per-face filtering can only be set up by FT_Face_Properties */ 2707 internal->lcd_filter_func = NULL; 2708 #endif 2709 } 2710 2711 if ( aface ) 2712 *aface = face; 2713 else 2714 FT_Done_Face( face ); 2715 2716 goto Exit; 2717 2718 Fail: 2719 if ( node ) 2720 FT_Done_Face( face ); /* face must be in the driver's list */ 2721 else if ( face ) 2722 destroy_face( memory, face, driver ); 2723 2724 Exit: 2725 #ifdef FT_DEBUG_LEVEL_TRACE 2726 if ( !error && face_index < 0 ) 2727 { 2728 FT_TRACE3(( "FT_Open_Face: The font has %ld face%s\n", 2729 face->num_faces, 2730 face->num_faces == 1 ? "" : "s" )); 2731 FT_TRACE3(( " and %ld named instance%s for face %ld\n", 2732 face->style_flags >> 16, 2733 ( face->style_flags >> 16 ) == 1 ? "" : "s", 2734 -face_index - 1 )); 2735 } 2736 #endif 2737 2738 FT_TRACE4(( "FT_Open_Face: Return 0x%x\n", error )); 2739 2740 return error; 2741 } 2742 2743 2744 /* documentation is in freetype.h */ 2745 2746 FT_EXPORT_DEF( FT_Error ) FT_Attach_File(FT_Face face,const char * filepathname)2747 FT_Attach_File( FT_Face face, 2748 const char* filepathname ) 2749 { 2750 FT_Open_Args open; 2751 2752 2753 /* test for valid `face' delayed to `FT_Attach_Stream' */ 2754 2755 if ( !filepathname ) 2756 return FT_THROW( Invalid_Argument ); 2757 2758 open.stream = NULL; 2759 open.flags = FT_OPEN_PATHNAME; 2760 open.pathname = (char*)filepathname; 2761 2762 return FT_Attach_Stream( face, &open ); 2763 } 2764 2765 2766 /* documentation is in freetype.h */ 2767 2768 FT_EXPORT_DEF( FT_Error ) FT_Attach_Stream(FT_Face face,FT_Open_Args * parameters)2769 FT_Attach_Stream( FT_Face face, 2770 FT_Open_Args* parameters ) 2771 { 2772 FT_Stream stream; 2773 FT_Error error; 2774 FT_Driver driver; 2775 2776 FT_Driver_Class clazz; 2777 2778 2779 /* test for valid `parameters' delayed to `FT_Stream_New' */ 2780 2781 if ( !face ) 2782 return FT_THROW( Invalid_Face_Handle ); 2783 2784 driver = face->driver; 2785 if ( !driver ) 2786 return FT_THROW( Invalid_Driver_Handle ); 2787 2788 error = FT_Stream_New( driver->root.library, parameters, &stream ); 2789 if ( error ) 2790 goto Exit; 2791 2792 /* we implement FT_Attach_Stream in each driver through the */ 2793 /* `attach_file' interface */ 2794 2795 error = FT_ERR( Unimplemented_Feature ); 2796 clazz = driver->clazz; 2797 if ( clazz->attach_file ) 2798 error = clazz->attach_file( face, stream ); 2799 2800 /* close the attached stream */ 2801 FT_Stream_Free( stream, 2802 FT_BOOL( parameters->stream && 2803 ( parameters->flags & FT_OPEN_STREAM ) ) ); 2804 2805 Exit: 2806 return error; 2807 } 2808 2809 2810 /* documentation is in freetype.h */ 2811 2812 FT_EXPORT_DEF( FT_Error ) FT_Reference_Face(FT_Face face)2813 FT_Reference_Face( FT_Face face ) 2814 { 2815 if ( !face ) 2816 return FT_THROW( Invalid_Face_Handle ); 2817 2818 face->internal->refcount++; 2819 2820 return FT_Err_Ok; 2821 } 2822 2823 2824 /* documentation is in freetype.h */ 2825 2826 FT_EXPORT_DEF( FT_Error ) FT_Done_Face(FT_Face face)2827 FT_Done_Face( FT_Face face ) 2828 { 2829 FT_Error error; 2830 FT_Driver driver; 2831 FT_Memory memory; 2832 FT_ListNode node; 2833 2834 2835 error = FT_ERR( Invalid_Face_Handle ); 2836 if ( face && face->driver ) 2837 { 2838 face->internal->refcount--; 2839 if ( face->internal->refcount > 0 ) 2840 error = FT_Err_Ok; 2841 else 2842 { 2843 driver = face->driver; 2844 memory = driver->root.memory; 2845 2846 /* find face in driver's list */ 2847 node = FT_List_Find( &driver->faces_list, face ); 2848 if ( node ) 2849 { 2850 /* remove face object from the driver's list */ 2851 FT_List_Remove( &driver->faces_list, node ); 2852 FT_FREE( node ); 2853 2854 /* now destroy the object proper */ 2855 destroy_face( memory, face, driver ); 2856 error = FT_Err_Ok; 2857 } 2858 } 2859 } 2860 2861 return error; 2862 } 2863 2864 2865 /* documentation is in ftobjs.h */ 2866 2867 FT_EXPORT_DEF( FT_Error ) FT_New_Size(FT_Face face,FT_Size * asize)2868 FT_New_Size( FT_Face face, 2869 FT_Size *asize ) 2870 { 2871 FT_Error error; 2872 FT_Memory memory; 2873 FT_Driver driver; 2874 FT_Driver_Class clazz; 2875 2876 FT_Size size = NULL; 2877 FT_ListNode node = NULL; 2878 2879 FT_Size_Internal internal = NULL; 2880 2881 2882 if ( !face ) 2883 return FT_THROW( Invalid_Face_Handle ); 2884 2885 if ( !asize ) 2886 return FT_THROW( Invalid_Argument ); 2887 2888 if ( !face->driver ) 2889 return FT_THROW( Invalid_Driver_Handle ); 2890 2891 *asize = NULL; 2892 2893 driver = face->driver; 2894 clazz = driver->clazz; 2895 memory = face->memory; 2896 2897 /* Allocate new size object and perform basic initialisation */ 2898 if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) ) 2899 goto Exit; 2900 2901 size->face = face; 2902 2903 if ( FT_NEW( internal ) ) 2904 goto Exit; 2905 2906 size->internal = internal; 2907 2908 if ( clazz->init_size ) 2909 error = clazz->init_size( size ); 2910 2911 /* in case of success, add to the face's list */ 2912 if ( !error ) 2913 { 2914 *asize = size; 2915 node->data = size; 2916 FT_List_Add( &face->sizes_list, node ); 2917 } 2918 2919 Exit: 2920 if ( error ) 2921 { 2922 FT_FREE( node ); 2923 if ( size ) 2924 FT_FREE( size->internal ); 2925 FT_FREE( size ); 2926 } 2927 2928 return error; 2929 } 2930 2931 2932 /* documentation is in ftobjs.h */ 2933 2934 FT_EXPORT_DEF( FT_Error ) FT_Done_Size(FT_Size size)2935 FT_Done_Size( FT_Size size ) 2936 { 2937 FT_Error error; 2938 FT_Driver driver; 2939 FT_Memory memory; 2940 FT_Face face; 2941 FT_ListNode node; 2942 2943 2944 if ( !size ) 2945 return FT_THROW( Invalid_Size_Handle ); 2946 2947 face = size->face; 2948 if ( !face ) 2949 return FT_THROW( Invalid_Face_Handle ); 2950 2951 driver = face->driver; 2952 if ( !driver ) 2953 return FT_THROW( Invalid_Driver_Handle ); 2954 2955 memory = driver->root.memory; 2956 2957 error = FT_Err_Ok; 2958 node = FT_List_Find( &face->sizes_list, size ); 2959 if ( node ) 2960 { 2961 FT_List_Remove( &face->sizes_list, node ); 2962 FT_FREE( node ); 2963 2964 if ( face->size == size ) 2965 { 2966 face->size = NULL; 2967 if ( face->sizes_list.head ) 2968 face->size = (FT_Size)(face->sizes_list.head->data); 2969 } 2970 2971 destroy_size( memory, size, driver ); 2972 } 2973 else 2974 error = FT_THROW( Invalid_Size_Handle ); 2975 2976 return error; 2977 } 2978 2979 2980 /* documentation is in ftobjs.h */ 2981 2982 FT_BASE_DEF( FT_Error ) FT_Match_Size(FT_Face face,FT_Size_Request req,FT_Bool ignore_width,FT_ULong * size_index)2983 FT_Match_Size( FT_Face face, 2984 FT_Size_Request req, 2985 FT_Bool ignore_width, 2986 FT_ULong* size_index ) 2987 { 2988 FT_Int i; 2989 FT_Long w, h; 2990 2991 2992 if ( !FT_HAS_FIXED_SIZES( face ) ) 2993 return FT_THROW( Invalid_Face_Handle ); 2994 2995 /* FT_Bitmap_Size doesn't provide enough info... */ 2996 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL ) 2997 return FT_THROW( Unimplemented_Feature ); 2998 2999 w = FT_REQUEST_WIDTH ( req ); 3000 h = FT_REQUEST_HEIGHT( req ); 3001 3002 if ( req->width && !req->height ) 3003 h = w; 3004 else if ( !req->width && req->height ) 3005 w = h; 3006 3007 w = FT_PIX_ROUND( w ); 3008 h = FT_PIX_ROUND( h ); 3009 3010 if ( !w || !h ) 3011 return FT_THROW( Invalid_Pixel_Size ); 3012 3013 for ( i = 0; i < face->num_fixed_sizes; i++ ) 3014 { 3015 FT_Bitmap_Size* bsize = face->available_sizes + i; 3016 3017 3018 if ( h != FT_PIX_ROUND( bsize->y_ppem ) ) 3019 continue; 3020 3021 if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width ) 3022 { 3023 FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i )); 3024 3025 if ( size_index ) 3026 *size_index = (FT_ULong)i; 3027 3028 return FT_Err_Ok; 3029 } 3030 } 3031 3032 FT_TRACE3(( "FT_Match_Size: no matching bitmap strike\n" )); 3033 3034 return FT_THROW( Invalid_Pixel_Size ); 3035 } 3036 3037 3038 /* documentation is in ftobjs.h */ 3039 3040 FT_BASE_DEF( void ) ft_synthesize_vertical_metrics(FT_Glyph_Metrics * metrics,FT_Pos advance)3041 ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics, 3042 FT_Pos advance ) 3043 { 3044 FT_Pos height = metrics->height; 3045 3046 3047 /* compensate for glyph with bbox above/below the baseline */ 3048 if ( metrics->horiBearingY < 0 ) 3049 { 3050 if ( height < metrics->horiBearingY ) 3051 height = metrics->horiBearingY; 3052 } 3053 else if ( metrics->horiBearingY > 0 ) 3054 height -= metrics->horiBearingY; 3055 3056 /* the factor 1.2 is a heuristical value */ 3057 if ( !advance ) 3058 advance = height * 12 / 10; 3059 3060 metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2; 3061 metrics->vertBearingY = ( advance - height ) / 2; 3062 metrics->vertAdvance = advance; 3063 } 3064 3065 3066 static void ft_recompute_scaled_metrics(FT_Face face,FT_Size_Metrics * metrics)3067 ft_recompute_scaled_metrics( FT_Face face, 3068 FT_Size_Metrics* metrics ) 3069 { 3070 /* Compute root ascender, descender, test height, and max_advance */ 3071 3072 #ifdef GRID_FIT_METRICS 3073 metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender, 3074 metrics->y_scale ) ); 3075 3076 metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender, 3077 metrics->y_scale ) ); 3078 3079 metrics->height = FT_PIX_ROUND( FT_MulFix( face->height, 3080 metrics->y_scale ) ); 3081 3082 metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width, 3083 metrics->x_scale ) ); 3084 #else /* !GRID_FIT_METRICS */ 3085 metrics->ascender = FT_MulFix( face->ascender, 3086 metrics->y_scale ); 3087 3088 metrics->descender = FT_MulFix( face->descender, 3089 metrics->y_scale ); 3090 3091 metrics->height = FT_MulFix( face->height, 3092 metrics->y_scale ); 3093 3094 metrics->max_advance = FT_MulFix( face->max_advance_width, 3095 metrics->x_scale ); 3096 #endif /* !GRID_FIT_METRICS */ 3097 } 3098 3099 3100 FT_BASE_DEF( void ) FT_Select_Metrics(FT_Face face,FT_ULong strike_index)3101 FT_Select_Metrics( FT_Face face, 3102 FT_ULong strike_index ) 3103 { 3104 FT_Size_Metrics* metrics; 3105 FT_Bitmap_Size* bsize; 3106 3107 3108 metrics = &face->size->metrics; 3109 bsize = face->available_sizes + strike_index; 3110 3111 metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 ); 3112 metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 ); 3113 3114 if ( FT_IS_SCALABLE( face ) ) 3115 { 3116 metrics->x_scale = FT_DivFix( bsize->x_ppem, 3117 face->units_per_EM ); 3118 metrics->y_scale = FT_DivFix( bsize->y_ppem, 3119 face->units_per_EM ); 3120 3121 ft_recompute_scaled_metrics( face, metrics ); 3122 } 3123 else 3124 { 3125 metrics->x_scale = 1L << 16; 3126 metrics->y_scale = 1L << 16; 3127 metrics->ascender = bsize->y_ppem; 3128 metrics->descender = 0; 3129 metrics->height = bsize->height << 6; 3130 metrics->max_advance = bsize->x_ppem; 3131 } 3132 } 3133 3134 3135 FT_BASE_DEF( void ) FT_Request_Metrics(FT_Face face,FT_Size_Request req)3136 FT_Request_Metrics( FT_Face face, 3137 FT_Size_Request req ) 3138 { 3139 FT_Size_Metrics* metrics; 3140 3141 3142 metrics = &face->size->metrics; 3143 3144 if ( FT_IS_SCALABLE( face ) ) 3145 { 3146 FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0; 3147 3148 3149 switch ( req->type ) 3150 { 3151 case FT_SIZE_REQUEST_TYPE_NOMINAL: 3152 w = h = face->units_per_EM; 3153 break; 3154 3155 case FT_SIZE_REQUEST_TYPE_REAL_DIM: 3156 w = h = face->ascender - face->descender; 3157 break; 3158 3159 case FT_SIZE_REQUEST_TYPE_BBOX: 3160 w = face->bbox.xMax - face->bbox.xMin; 3161 h = face->bbox.yMax - face->bbox.yMin; 3162 break; 3163 3164 case FT_SIZE_REQUEST_TYPE_CELL: 3165 w = face->max_advance_width; 3166 h = face->ascender - face->descender; 3167 break; 3168 3169 case FT_SIZE_REQUEST_TYPE_SCALES: 3170 metrics->x_scale = (FT_Fixed)req->width; 3171 metrics->y_scale = (FT_Fixed)req->height; 3172 if ( !metrics->x_scale ) 3173 metrics->x_scale = metrics->y_scale; 3174 else if ( !metrics->y_scale ) 3175 metrics->y_scale = metrics->x_scale; 3176 goto Calculate_Ppem; 3177 3178 case FT_SIZE_REQUEST_TYPE_MAX: 3179 break; 3180 } 3181 3182 /* to be on the safe side */ 3183 if ( w < 0 ) 3184 w = -w; 3185 3186 if ( h < 0 ) 3187 h = -h; 3188 3189 scaled_w = FT_REQUEST_WIDTH ( req ); 3190 scaled_h = FT_REQUEST_HEIGHT( req ); 3191 3192 /* determine scales */ 3193 if ( req->width ) 3194 { 3195 metrics->x_scale = FT_DivFix( scaled_w, w ); 3196 3197 if ( req->height ) 3198 { 3199 metrics->y_scale = FT_DivFix( scaled_h, h ); 3200 3201 if ( req->type == FT_SIZE_REQUEST_TYPE_CELL ) 3202 { 3203 if ( metrics->y_scale > metrics->x_scale ) 3204 metrics->y_scale = metrics->x_scale; 3205 else 3206 metrics->x_scale = metrics->y_scale; 3207 } 3208 } 3209 else 3210 { 3211 metrics->y_scale = metrics->x_scale; 3212 scaled_h = FT_MulDiv( scaled_w, h, w ); 3213 } 3214 } 3215 else 3216 { 3217 metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h ); 3218 scaled_w = FT_MulDiv( scaled_h, w, h ); 3219 } 3220 3221 Calculate_Ppem: 3222 /* calculate the ppems */ 3223 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL ) 3224 { 3225 scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale ); 3226 scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale ); 3227 } 3228 3229 metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 ); 3230 metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 ); 3231 3232 ft_recompute_scaled_metrics( face, metrics ); 3233 } 3234 else 3235 { 3236 FT_ZERO( metrics ); 3237 metrics->x_scale = 1L << 16; 3238 metrics->y_scale = 1L << 16; 3239 } 3240 } 3241 3242 3243 /* documentation is in freetype.h */ 3244 3245 FT_EXPORT_DEF( FT_Error ) FT_Select_Size(FT_Face face,FT_Int strike_index)3246 FT_Select_Size( FT_Face face, 3247 FT_Int strike_index ) 3248 { 3249 FT_Error error = FT_Err_Ok; 3250 FT_Driver_Class clazz; 3251 3252 3253 if ( !face || !FT_HAS_FIXED_SIZES( face ) ) 3254 return FT_THROW( Invalid_Face_Handle ); 3255 3256 if ( strike_index < 0 || strike_index >= face->num_fixed_sizes ) 3257 return FT_THROW( Invalid_Argument ); 3258 3259 clazz = face->driver->clazz; 3260 3261 if ( clazz->select_size ) 3262 { 3263 error = clazz->select_size( face->size, (FT_ULong)strike_index ); 3264 3265 FT_TRACE5(( "FT_Select_Size (%s driver):\n", 3266 face->driver->root.clazz->module_name )); 3267 } 3268 else 3269 { 3270 FT_Select_Metrics( face, (FT_ULong)strike_index ); 3271 3272 FT_TRACE5(( "FT_Select_Size:\n" )); 3273 } 3274 3275 #ifdef FT_DEBUG_LEVEL_TRACE 3276 { 3277 FT_Size_Metrics* metrics = &face->size->metrics; 3278 3279 3280 FT_TRACE5(( " x scale: %ld (%f)\n", 3281 metrics->x_scale, metrics->x_scale / 65536.0 )); 3282 FT_TRACE5(( " y scale: %ld (%f)\n", 3283 metrics->y_scale, metrics->y_scale / 65536.0 )); 3284 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 )); 3285 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 )); 3286 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 3287 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 )); 3288 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem )); 3289 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem )); 3290 } 3291 #endif 3292 3293 return error; 3294 } 3295 3296 3297 /* documentation is in freetype.h */ 3298 3299 FT_EXPORT_DEF( FT_Error ) FT_Request_Size(FT_Face face,FT_Size_Request req)3300 FT_Request_Size( FT_Face face, 3301 FT_Size_Request req ) 3302 { 3303 FT_Error error = FT_Err_Ok; 3304 FT_Driver_Class clazz; 3305 FT_ULong strike_index; 3306 3307 3308 if ( !face ) 3309 return FT_THROW( Invalid_Face_Handle ); 3310 3311 if ( !req || req->width < 0 || req->height < 0 || 3312 req->type >= FT_SIZE_REQUEST_TYPE_MAX ) 3313 return FT_THROW( Invalid_Argument ); 3314 3315 /* signal the auto-hinter to recompute its size metrics */ 3316 /* (if requested) */ 3317 face->size->internal->autohint_metrics.x_scale = 0; 3318 3319 clazz = face->driver->clazz; 3320 3321 if ( clazz->request_size ) 3322 { 3323 error = clazz->request_size( face->size, req ); 3324 3325 FT_TRACE5(( "FT_Request_Size (%s driver):\n", 3326 face->driver->root.clazz->module_name )); 3327 } 3328 else if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) ) 3329 { 3330 /* 3331 * The reason that a driver doesn't have `request_size' defined is 3332 * either that the scaling here suffices or that the supported formats 3333 * are bitmap-only and size matching is not implemented. 3334 * 3335 * In the latter case, a simple size matching is done. 3336 */ 3337 error = FT_Match_Size( face, req, 0, &strike_index ); 3338 if ( error ) 3339 return error; 3340 3341 return FT_Select_Size( face, (FT_Int)strike_index ); 3342 } 3343 else 3344 { 3345 FT_Request_Metrics( face, req ); 3346 3347 FT_TRACE5(( "FT_Request_Size:\n" )); 3348 } 3349 3350 #ifdef FT_DEBUG_LEVEL_TRACE 3351 { 3352 FT_Size_Metrics* metrics = &face->size->metrics; 3353 3354 3355 FT_TRACE5(( " x scale: %ld (%f)\n", 3356 metrics->x_scale, metrics->x_scale / 65536.0 )); 3357 FT_TRACE5(( " y scale: %ld (%f)\n", 3358 metrics->y_scale, metrics->y_scale / 65536.0 )); 3359 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 )); 3360 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 )); 3361 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 3362 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 )); 3363 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem )); 3364 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem )); 3365 } 3366 #endif 3367 3368 return error; 3369 } 3370 3371 3372 /* documentation is in freetype.h */ 3373 3374 FT_EXPORT_DEF( FT_Error ) FT_Set_Char_Size(FT_Face face,FT_F26Dot6 char_width,FT_F26Dot6 char_height,FT_UInt horz_resolution,FT_UInt vert_resolution)3375 FT_Set_Char_Size( FT_Face face, 3376 FT_F26Dot6 char_width, 3377 FT_F26Dot6 char_height, 3378 FT_UInt horz_resolution, 3379 FT_UInt vert_resolution ) 3380 { 3381 FT_Size_RequestRec req; 3382 3383 3384 /* check of `face' delayed to `FT_Request_Size' */ 3385 3386 if ( !char_width ) 3387 char_width = char_height; 3388 else if ( !char_height ) 3389 char_height = char_width; 3390 3391 if ( !horz_resolution ) 3392 horz_resolution = vert_resolution; 3393 else if ( !vert_resolution ) 3394 vert_resolution = horz_resolution; 3395 3396 if ( char_width < 1 * 64 ) 3397 char_width = 1 * 64; 3398 if ( char_height < 1 * 64 ) 3399 char_height = 1 * 64; 3400 3401 if ( !horz_resolution ) 3402 horz_resolution = vert_resolution = 72; 3403 3404 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL; 3405 req.width = char_width; 3406 req.height = char_height; 3407 req.horiResolution = horz_resolution; 3408 req.vertResolution = vert_resolution; 3409 3410 return FT_Request_Size( face, &req ); 3411 } 3412 3413 3414 /* documentation is in freetype.h */ 3415 3416 FT_EXPORT_DEF( FT_Error ) FT_Set_Pixel_Sizes(FT_Face face,FT_UInt pixel_width,FT_UInt pixel_height)3417 FT_Set_Pixel_Sizes( FT_Face face, 3418 FT_UInt pixel_width, 3419 FT_UInt pixel_height ) 3420 { 3421 FT_Size_RequestRec req; 3422 3423 3424 /* check of `face' delayed to `FT_Request_Size' */ 3425 3426 if ( pixel_width == 0 ) 3427 pixel_width = pixel_height; 3428 else if ( pixel_height == 0 ) 3429 pixel_height = pixel_width; 3430 3431 if ( pixel_width < 1 ) 3432 pixel_width = 1; 3433 if ( pixel_height < 1 ) 3434 pixel_height = 1; 3435 3436 /* use `>=' to avoid potential compiler warning on 16bit platforms */ 3437 if ( pixel_width >= 0xFFFFU ) 3438 pixel_width = 0xFFFFU; 3439 if ( pixel_height >= 0xFFFFU ) 3440 pixel_height = 0xFFFFU; 3441 3442 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL; 3443 req.width = (FT_Long)( pixel_width << 6 ); 3444 req.height = (FT_Long)( pixel_height << 6 ); 3445 req.horiResolution = 0; 3446 req.vertResolution = 0; 3447 3448 return FT_Request_Size( face, &req ); 3449 } 3450 3451 3452 /* documentation is in freetype.h */ 3453 3454 FT_EXPORT_DEF( FT_Error ) FT_Get_Kerning(FT_Face face,FT_UInt left_glyph,FT_UInt right_glyph,FT_UInt kern_mode,FT_Vector * akerning)3455 FT_Get_Kerning( FT_Face face, 3456 FT_UInt left_glyph, 3457 FT_UInt right_glyph, 3458 FT_UInt kern_mode, 3459 FT_Vector *akerning ) 3460 { 3461 FT_Error error = FT_Err_Ok; 3462 FT_Driver driver; 3463 3464 3465 if ( !face ) 3466 return FT_THROW( Invalid_Face_Handle ); 3467 3468 if ( !akerning ) 3469 return FT_THROW( Invalid_Argument ); 3470 3471 driver = face->driver; 3472 3473 akerning->x = 0; 3474 akerning->y = 0; 3475 3476 if ( driver->clazz->get_kerning ) 3477 { 3478 error = driver->clazz->get_kerning( face, 3479 left_glyph, 3480 right_glyph, 3481 akerning ); 3482 if ( !error ) 3483 { 3484 if ( kern_mode != FT_KERNING_UNSCALED ) 3485 { 3486 akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale ); 3487 akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale ); 3488 3489 if ( kern_mode != FT_KERNING_UNFITTED ) 3490 { 3491 FT_Pos orig_x = akerning->x; 3492 FT_Pos orig_y = akerning->y; 3493 3494 3495 /* we scale down kerning values for small ppem values */ 3496 /* to avoid that rounding makes them too big. */ 3497 /* `25' has been determined heuristically. */ 3498 if ( face->size->metrics.x_ppem < 25 ) 3499 akerning->x = FT_MulDiv( orig_x, 3500 face->size->metrics.x_ppem, 25 ); 3501 if ( face->size->metrics.y_ppem < 25 ) 3502 akerning->y = FT_MulDiv( orig_y, 3503 face->size->metrics.y_ppem, 25 ); 3504 3505 akerning->x = FT_PIX_ROUND( akerning->x ); 3506 akerning->y = FT_PIX_ROUND( akerning->y ); 3507 3508 #ifdef FT_DEBUG_LEVEL_TRACE 3509 { 3510 FT_Pos orig_x_rounded = FT_PIX_ROUND( orig_x ); 3511 FT_Pos orig_y_rounded = FT_PIX_ROUND( orig_y ); 3512 3513 3514 if ( akerning->x != orig_x_rounded || 3515 akerning->y != orig_y_rounded ) 3516 FT_TRACE5(( "FT_Get_Kerning: horizontal kerning" 3517 " (%ld, %ld) scaled down to (%ld, %ld) pixels\n", 3518 orig_x_rounded / 64, orig_y_rounded / 64, 3519 akerning->x / 64, akerning->y / 64 )); 3520 } 3521 #endif 3522 } 3523 } 3524 } 3525 } 3526 3527 return error; 3528 } 3529 3530 3531 /* documentation is in freetype.h */ 3532 3533 FT_EXPORT_DEF( FT_Error ) FT_Get_Track_Kerning(FT_Face face,FT_Fixed point_size,FT_Int degree,FT_Fixed * akerning)3534 FT_Get_Track_Kerning( FT_Face face, 3535 FT_Fixed point_size, 3536 FT_Int degree, 3537 FT_Fixed* akerning ) 3538 { 3539 FT_Service_Kerning service; 3540 FT_Error error = FT_Err_Ok; 3541 3542 3543 if ( !face ) 3544 return FT_THROW( Invalid_Face_Handle ); 3545 3546 if ( !akerning ) 3547 return FT_THROW( Invalid_Argument ); 3548 3549 FT_FACE_FIND_SERVICE( face, service, KERNING ); 3550 if ( !service ) 3551 return FT_THROW( Unimplemented_Feature ); 3552 3553 error = service->get_track( face, 3554 point_size, 3555 degree, 3556 akerning ); 3557 3558 return error; 3559 } 3560 3561 3562 /* documentation is in freetype.h */ 3563 3564 FT_EXPORT_DEF( FT_Error ) FT_Select_Charmap(FT_Face face,FT_Encoding encoding)3565 FT_Select_Charmap( FT_Face face, 3566 FT_Encoding encoding ) 3567 { 3568 FT_CharMap* cur; 3569 FT_CharMap* limit; 3570 3571 3572 if ( !face ) 3573 return FT_THROW( Invalid_Face_Handle ); 3574 3575 /* FT_ENCODING_NONE is a valid encoding for BDF, PCF, and Windows FNT */ 3576 if ( encoding == FT_ENCODING_NONE && !face->num_charmaps ) 3577 return FT_THROW( Invalid_Argument ); 3578 3579 /* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */ 3580 /* charmap available, i.e., one with UCS-4 characters, if possible. */ 3581 /* */ 3582 /* This is done by find_unicode_charmap() above, to share code. */ 3583 if ( encoding == FT_ENCODING_UNICODE ) 3584 return find_unicode_charmap( face ); 3585 3586 cur = face->charmaps; 3587 if ( !cur ) 3588 return FT_THROW( Invalid_CharMap_Handle ); 3589 3590 limit = cur + face->num_charmaps; 3591 3592 for ( ; cur < limit; cur++ ) 3593 { 3594 if ( cur[0]->encoding == encoding ) 3595 { 3596 face->charmap = cur[0]; 3597 return FT_Err_Ok; 3598 } 3599 } 3600 3601 return FT_THROW( Invalid_Argument ); 3602 } 3603 3604 3605 /* documentation is in freetype.h */ 3606 3607 FT_EXPORT_DEF( FT_Error ) FT_Set_Charmap(FT_Face face,FT_CharMap charmap)3608 FT_Set_Charmap( FT_Face face, 3609 FT_CharMap charmap ) 3610 { 3611 FT_CharMap* cur; 3612 FT_CharMap* limit; 3613 3614 3615 if ( !face ) 3616 return FT_THROW( Invalid_Face_Handle ); 3617 3618 cur = face->charmaps; 3619 if ( !cur || !charmap ) 3620 return FT_THROW( Invalid_CharMap_Handle ); 3621 3622 limit = cur + face->num_charmaps; 3623 3624 for ( ; cur < limit; cur++ ) 3625 { 3626 if ( cur[0] == charmap && 3627 FT_Get_CMap_Format ( charmap ) != 14 ) 3628 { 3629 face->charmap = cur[0]; 3630 return FT_Err_Ok; 3631 } 3632 } 3633 3634 return FT_THROW( Invalid_Argument ); 3635 } 3636 3637 3638 /* documentation is in freetype.h */ 3639 3640 FT_EXPORT_DEF( FT_Int ) FT_Get_Charmap_Index(FT_CharMap charmap)3641 FT_Get_Charmap_Index( FT_CharMap charmap ) 3642 { 3643 FT_Int i; 3644 3645 3646 if ( !charmap || !charmap->face ) 3647 return -1; 3648 3649 for ( i = 0; i < charmap->face->num_charmaps; i++ ) 3650 if ( charmap->face->charmaps[i] == charmap ) 3651 break; 3652 3653 FT_ASSERT( i < charmap->face->num_charmaps ); 3654 3655 return i; 3656 } 3657 3658 3659 static void ft_cmap_done_internal(FT_CMap cmap)3660 ft_cmap_done_internal( FT_CMap cmap ) 3661 { 3662 FT_CMap_Class clazz = cmap->clazz; 3663 FT_Face face = cmap->charmap.face; 3664 FT_Memory memory = FT_FACE_MEMORY( face ); 3665 3666 3667 if ( clazz->done ) 3668 clazz->done( cmap ); 3669 3670 FT_FREE( cmap ); 3671 } 3672 3673 3674 FT_BASE_DEF( void ) FT_CMap_Done(FT_CMap cmap)3675 FT_CMap_Done( FT_CMap cmap ) 3676 { 3677 if ( cmap ) 3678 { 3679 FT_Face face = cmap->charmap.face; 3680 FT_Memory memory = FT_FACE_MEMORY( face ); 3681 FT_Error error; 3682 FT_Int i, j; 3683 3684 3685 for ( i = 0; i < face->num_charmaps; i++ ) 3686 { 3687 if ( (FT_CMap)face->charmaps[i] == cmap ) 3688 { 3689 FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1]; 3690 3691 3692 if ( FT_RENEW_ARRAY( face->charmaps, 3693 face->num_charmaps, 3694 face->num_charmaps - 1 ) ) 3695 return; 3696 3697 /* remove it from our list of charmaps */ 3698 for ( j = i + 1; j < face->num_charmaps; j++ ) 3699 { 3700 if ( j == face->num_charmaps - 1 ) 3701 face->charmaps[j - 1] = last_charmap; 3702 else 3703 face->charmaps[j - 1] = face->charmaps[j]; 3704 } 3705 3706 face->num_charmaps--; 3707 3708 if ( (FT_CMap)face->charmap == cmap ) 3709 face->charmap = NULL; 3710 3711 ft_cmap_done_internal( cmap ); 3712 3713 break; 3714 } 3715 } 3716 } 3717 } 3718 3719 3720 FT_BASE_DEF( FT_Error ) FT_CMap_New(FT_CMap_Class clazz,FT_Pointer init_data,FT_CharMap charmap,FT_CMap * acmap)3721 FT_CMap_New( FT_CMap_Class clazz, 3722 FT_Pointer init_data, 3723 FT_CharMap charmap, 3724 FT_CMap *acmap ) 3725 { 3726 FT_Error error = FT_Err_Ok; 3727 FT_Face face; 3728 FT_Memory memory; 3729 FT_CMap cmap = NULL; 3730 3731 3732 if ( !clazz || !charmap || !charmap->face ) 3733 return FT_THROW( Invalid_Argument ); 3734 3735 face = charmap->face; 3736 memory = FT_FACE_MEMORY( face ); 3737 3738 if ( !FT_ALLOC( cmap, clazz->size ) ) 3739 { 3740 cmap->charmap = *charmap; 3741 cmap->clazz = clazz; 3742 3743 if ( clazz->init ) 3744 { 3745 error = clazz->init( cmap, init_data ); 3746 if ( error ) 3747 goto Fail; 3748 } 3749 3750 /* add it to our list of charmaps */ 3751 if ( FT_RENEW_ARRAY( face->charmaps, 3752 face->num_charmaps, 3753 face->num_charmaps + 1 ) ) 3754 goto Fail; 3755 3756 face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap; 3757 } 3758 3759 Exit: 3760 if ( acmap ) 3761 *acmap = cmap; 3762 3763 return error; 3764 3765 Fail: 3766 ft_cmap_done_internal( cmap ); 3767 cmap = NULL; 3768 goto Exit; 3769 } 3770 3771 3772 /* documentation is in freetype.h */ 3773 3774 FT_EXPORT_DEF( FT_UInt ) FT_Get_Char_Index(FT_Face face,FT_ULong charcode)3775 FT_Get_Char_Index( FT_Face face, 3776 FT_ULong charcode ) 3777 { 3778 FT_UInt result = 0; 3779 3780 3781 if ( face && face->charmap ) 3782 { 3783 FT_CMap cmap = FT_CMAP( face->charmap ); 3784 3785 3786 if ( charcode > 0xFFFFFFFFUL ) 3787 { 3788 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" )); 3789 FT_TRACE1(( " 0x%lx is truncated\n", charcode )); 3790 } 3791 3792 result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode ); 3793 if ( result >= (FT_UInt)face->num_glyphs ) 3794 result = 0; 3795 } 3796 3797 return result; 3798 } 3799 3800 3801 /* documentation is in freetype.h */ 3802 3803 FT_EXPORT_DEF( FT_ULong ) FT_Get_First_Char(FT_Face face,FT_UInt * agindex)3804 FT_Get_First_Char( FT_Face face, 3805 FT_UInt *agindex ) 3806 { 3807 FT_ULong result = 0; 3808 FT_UInt gindex = 0; 3809 3810 3811 /* only do something if we have a charmap, and we have glyphs at all */ 3812 if ( face && face->charmap && face->num_glyphs ) 3813 { 3814 gindex = FT_Get_Char_Index( face, 0 ); 3815 if ( gindex == 0 ) 3816 result = FT_Get_Next_Char( face, 0, &gindex ); 3817 } 3818 3819 if ( agindex ) 3820 *agindex = gindex; 3821 3822 return result; 3823 } 3824 3825 3826 /* documentation is in freetype.h */ 3827 3828 FT_EXPORT_DEF( FT_ULong ) FT_Get_Next_Char(FT_Face face,FT_ULong charcode,FT_UInt * agindex)3829 FT_Get_Next_Char( FT_Face face, 3830 FT_ULong charcode, 3831 FT_UInt *agindex ) 3832 { 3833 FT_ULong result = 0; 3834 FT_UInt gindex = 0; 3835 3836 3837 if ( face && face->charmap && face->num_glyphs ) 3838 { 3839 FT_UInt32 code = (FT_UInt32)charcode; 3840 FT_CMap cmap = FT_CMAP( face->charmap ); 3841 3842 3843 do 3844 { 3845 gindex = cmap->clazz->char_next( cmap, &code ); 3846 3847 } while ( gindex >= (FT_UInt)face->num_glyphs ); 3848 3849 result = ( gindex == 0 ) ? 0 : code; 3850 } 3851 3852 if ( agindex ) 3853 *agindex = gindex; 3854 3855 return result; 3856 } 3857 3858 3859 /* documentation is in freetype.h */ 3860 3861 FT_EXPORT_DEF( FT_Error ) FT_Face_Properties(FT_Face face,FT_UInt num_properties,FT_Parameter * properties)3862 FT_Face_Properties( FT_Face face, 3863 FT_UInt num_properties, 3864 FT_Parameter* properties ) 3865 { 3866 FT_Error error = FT_Err_Ok; 3867 3868 3869 if ( num_properties > 0 && !properties ) 3870 { 3871 error = FT_THROW( Invalid_Argument ); 3872 goto Exit; 3873 } 3874 3875 for ( ; num_properties > 0; num_properties-- ) 3876 { 3877 if ( properties->tag == FT_PARAM_TAG_STEM_DARKENING ) 3878 { 3879 if ( properties->data ) 3880 { 3881 if ( *( (FT_Bool*)properties->data ) == TRUE ) 3882 face->internal->no_stem_darkening = FALSE; 3883 else 3884 face->internal->no_stem_darkening = TRUE; 3885 } 3886 else 3887 { 3888 /* use module default */ 3889 face->internal->no_stem_darkening = -1; 3890 } 3891 } 3892 else if ( properties->tag == FT_PARAM_TAG_LCD_FILTER_WEIGHTS ) 3893 { 3894 #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING 3895 if ( properties->data ) 3896 { 3897 ft_memcpy( face->internal->lcd_weights, 3898 properties->data, 3899 FT_LCD_FILTER_FIVE_TAPS ); 3900 face->internal->lcd_filter_func = ft_lcd_filter_fir; 3901 } 3902 #else 3903 error = FT_THROW( Unimplemented_Feature ); 3904 goto Exit; 3905 #endif 3906 } 3907 else if ( properties->tag == FT_PARAM_TAG_RANDOM_SEED ) 3908 { 3909 if ( properties->data ) 3910 { 3911 face->internal->random_seed = *( (FT_Int32*)properties->data ); 3912 if ( face->internal->random_seed < 0 ) 3913 face->internal->random_seed = 0; 3914 } 3915 else 3916 { 3917 /* use module default */ 3918 face->internal->random_seed = -1; 3919 } 3920 } 3921 else 3922 { 3923 error = FT_THROW( Invalid_Argument ); 3924 goto Exit; 3925 } 3926 3927 if ( error ) 3928 break; 3929 3930 properties++; 3931 } 3932 3933 Exit: 3934 return error; 3935 } 3936 3937 3938 /* documentation is in freetype.h */ 3939 3940 FT_EXPORT_DEF( FT_UInt ) FT_Face_GetCharVariantIndex(FT_Face face,FT_ULong charcode,FT_ULong variantSelector)3941 FT_Face_GetCharVariantIndex( FT_Face face, 3942 FT_ULong charcode, 3943 FT_ULong variantSelector ) 3944 { 3945 FT_UInt result = 0; 3946 3947 3948 if ( face && 3949 face->charmap && 3950 face->charmap->encoding == FT_ENCODING_UNICODE ) 3951 { 3952 FT_CharMap charmap = find_variant_selector_charmap( face ); 3953 FT_CMap ucmap = FT_CMAP( face->charmap ); 3954 3955 3956 if ( charmap ) 3957 { 3958 FT_CMap vcmap = FT_CMAP( charmap ); 3959 3960 3961 if ( charcode > 0xFFFFFFFFUL ) 3962 { 3963 FT_TRACE1(( "FT_Face_GetCharVariantIndex:" 3964 " too large charcode" )); 3965 FT_TRACE1(( " 0x%lx is truncated\n", charcode )); 3966 } 3967 if ( variantSelector > 0xFFFFFFFFUL ) 3968 { 3969 FT_TRACE1(( "FT_Face_GetCharVariantIndex:" 3970 " too large variantSelector" )); 3971 FT_TRACE1(( " 0x%lx is truncated\n", variantSelector )); 3972 } 3973 3974 result = vcmap->clazz->char_var_index( vcmap, ucmap, 3975 (FT_UInt32)charcode, 3976 (FT_UInt32)variantSelector ); 3977 } 3978 } 3979 3980 return result; 3981 } 3982 3983 3984 /* documentation is in freetype.h */ 3985 3986 FT_EXPORT_DEF( FT_Int ) FT_Face_GetCharVariantIsDefault(FT_Face face,FT_ULong charcode,FT_ULong variantSelector)3987 FT_Face_GetCharVariantIsDefault( FT_Face face, 3988 FT_ULong charcode, 3989 FT_ULong variantSelector ) 3990 { 3991 FT_Int result = -1; 3992 3993 3994 if ( face ) 3995 { 3996 FT_CharMap charmap = find_variant_selector_charmap( face ); 3997 3998 3999 if ( charmap ) 4000 { 4001 FT_CMap vcmap = FT_CMAP( charmap ); 4002 4003 4004 if ( charcode > 0xFFFFFFFFUL ) 4005 { 4006 FT_TRACE1(( "FT_Face_GetCharVariantIsDefault:" 4007 " too large charcode" )); 4008 FT_TRACE1(( " 0x%lx is truncated\n", charcode )); 4009 } 4010 if ( variantSelector > 0xFFFFFFFFUL ) 4011 { 4012 FT_TRACE1(( "FT_Face_GetCharVariantIsDefault:" 4013 " too large variantSelector" )); 4014 FT_TRACE1(( " 0x%lx is truncated\n", variantSelector )); 4015 } 4016 4017 result = vcmap->clazz->char_var_default( vcmap, 4018 (FT_UInt32)charcode, 4019 (FT_UInt32)variantSelector ); 4020 } 4021 } 4022 4023 return result; 4024 } 4025 4026 4027 /* documentation is in freetype.h */ 4028 4029 FT_EXPORT_DEF( FT_UInt32* ) FT_Face_GetVariantSelectors(FT_Face face)4030 FT_Face_GetVariantSelectors( FT_Face face ) 4031 { 4032 FT_UInt32 *result = NULL; 4033 4034 4035 if ( face ) 4036 { 4037 FT_CharMap charmap = find_variant_selector_charmap( face ); 4038 4039 4040 if ( charmap ) 4041 { 4042 FT_CMap vcmap = FT_CMAP( charmap ); 4043 FT_Memory memory = FT_FACE_MEMORY( face ); 4044 4045 4046 result = vcmap->clazz->variant_list( vcmap, memory ); 4047 } 4048 } 4049 4050 return result; 4051 } 4052 4053 4054 /* documentation is in freetype.h */ 4055 4056 FT_EXPORT_DEF( FT_UInt32* ) FT_Face_GetVariantsOfChar(FT_Face face,FT_ULong charcode)4057 FT_Face_GetVariantsOfChar( FT_Face face, 4058 FT_ULong charcode ) 4059 { 4060 FT_UInt32 *result = NULL; 4061 4062 4063 if ( face ) 4064 { 4065 FT_CharMap charmap = find_variant_selector_charmap( face ); 4066 4067 4068 if ( charmap ) 4069 { 4070 FT_CMap vcmap = FT_CMAP( charmap ); 4071 FT_Memory memory = FT_FACE_MEMORY( face ); 4072 4073 4074 if ( charcode > 0xFFFFFFFFUL ) 4075 { 4076 FT_TRACE1(( "FT_Face_GetVariantsOfChar: too large charcode" )); 4077 FT_TRACE1(( " 0x%lx is truncated\n", charcode )); 4078 } 4079 4080 result = vcmap->clazz->charvariant_list( vcmap, memory, 4081 (FT_UInt32)charcode ); 4082 } 4083 } 4084 return result; 4085 } 4086 4087 4088 /* documentation is in freetype.h */ 4089 4090 FT_EXPORT_DEF( FT_UInt32* ) FT_Face_GetCharsOfVariant(FT_Face face,FT_ULong variantSelector)4091 FT_Face_GetCharsOfVariant( FT_Face face, 4092 FT_ULong variantSelector ) 4093 { 4094 FT_UInt32 *result = NULL; 4095 4096 4097 if ( face ) 4098 { 4099 FT_CharMap charmap = find_variant_selector_charmap( face ); 4100 4101 4102 if ( charmap ) 4103 { 4104 FT_CMap vcmap = FT_CMAP( charmap ); 4105 FT_Memory memory = FT_FACE_MEMORY( face ); 4106 4107 4108 if ( variantSelector > 0xFFFFFFFFUL ) 4109 { 4110 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" )); 4111 FT_TRACE1(( " 0x%lx is truncated\n", variantSelector )); 4112 } 4113 4114 result = vcmap->clazz->variantchar_list( vcmap, memory, 4115 (FT_UInt32)variantSelector ); 4116 } 4117 } 4118 4119 return result; 4120 } 4121 4122 4123 /* documentation is in freetype.h */ 4124 4125 FT_EXPORT_DEF( FT_UInt ) FT_Get_Name_Index(FT_Face face,const FT_String * glyph_name)4126 FT_Get_Name_Index( FT_Face face, 4127 const FT_String* glyph_name ) 4128 { 4129 FT_UInt result = 0; 4130 4131 4132 if ( face && 4133 FT_HAS_GLYPH_NAMES( face ) && 4134 glyph_name ) 4135 { 4136 FT_Service_GlyphDict service; 4137 4138 4139 FT_FACE_LOOKUP_SERVICE( face, 4140 service, 4141 GLYPH_DICT ); 4142 4143 if ( service && service->name_index ) 4144 result = service->name_index( face, glyph_name ); 4145 } 4146 4147 return result; 4148 } 4149 4150 4151 /* documentation is in freetype.h */ 4152 4153 FT_EXPORT_DEF( FT_Error ) FT_Get_Glyph_Name(FT_Face face,FT_UInt glyph_index,FT_Pointer buffer,FT_UInt buffer_max)4154 FT_Get_Glyph_Name( FT_Face face, 4155 FT_UInt glyph_index, 4156 FT_Pointer buffer, 4157 FT_UInt buffer_max ) 4158 { 4159 FT_Error error; 4160 FT_Service_GlyphDict service; 4161 4162 4163 if ( !face ) 4164 return FT_THROW( Invalid_Face_Handle ); 4165 4166 if ( !buffer || buffer_max == 0 ) 4167 return FT_THROW( Invalid_Argument ); 4168 4169 /* clean up buffer */ 4170 ((FT_Byte*)buffer)[0] = '\0'; 4171 4172 if ( (FT_Long)glyph_index >= face->num_glyphs ) 4173 return FT_THROW( Invalid_Glyph_Index ); 4174 4175 if ( !FT_HAS_GLYPH_NAMES( face ) ) 4176 return FT_THROW( Invalid_Argument ); 4177 4178 FT_FACE_LOOKUP_SERVICE( face, service, GLYPH_DICT ); 4179 if ( service && service->get_name ) 4180 error = service->get_name( face, glyph_index, buffer, buffer_max ); 4181 else 4182 error = FT_THROW( Invalid_Argument ); 4183 4184 return error; 4185 } 4186 4187 4188 /* documentation is in freetype.h */ 4189 4190 FT_EXPORT_DEF( const char* ) FT_Get_Postscript_Name(FT_Face face)4191 FT_Get_Postscript_Name( FT_Face face ) 4192 { 4193 const char* result = NULL; 4194 4195 4196 if ( !face ) 4197 goto Exit; 4198 4199 if ( !result ) 4200 { 4201 FT_Service_PsFontName service; 4202 4203 4204 FT_FACE_LOOKUP_SERVICE( face, 4205 service, 4206 POSTSCRIPT_FONT_NAME ); 4207 4208 if ( service && service->get_ps_font_name ) 4209 result = service->get_ps_font_name( face ); 4210 } 4211 4212 Exit: 4213 return result; 4214 } 4215 4216 4217 /* documentation is in tttables.h */ 4218 4219 FT_EXPORT_DEF( void* ) FT_Get_Sfnt_Table(FT_Face face,FT_Sfnt_Tag tag)4220 FT_Get_Sfnt_Table( FT_Face face, 4221 FT_Sfnt_Tag tag ) 4222 { 4223 void* table = NULL; 4224 FT_Service_SFNT_Table service; 4225 4226 4227 if ( face && FT_IS_SFNT( face ) ) 4228 { 4229 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE ); 4230 if ( service ) 4231 table = service->get_table( face, tag ); 4232 } 4233 4234 return table; 4235 } 4236 4237 4238 /* documentation is in tttables.h */ 4239 4240 FT_EXPORT_DEF( FT_Error ) FT_Load_Sfnt_Table(FT_Face face,FT_ULong tag,FT_Long offset,FT_Byte * buffer,FT_ULong * length)4241 FT_Load_Sfnt_Table( FT_Face face, 4242 FT_ULong tag, 4243 FT_Long offset, 4244 FT_Byte* buffer, 4245 FT_ULong* length ) 4246 { 4247 FT_Service_SFNT_Table service; 4248 4249 4250 if ( !face || !FT_IS_SFNT( face ) ) 4251 return FT_THROW( Invalid_Face_Handle ); 4252 4253 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE ); 4254 if ( !service ) 4255 return FT_THROW( Unimplemented_Feature ); 4256 4257 return service->load_table( face, tag, offset, buffer, length ); 4258 } 4259 4260 4261 /* documentation is in tttables.h */ 4262 4263 FT_EXPORT_DEF( FT_Error ) FT_Sfnt_Table_Info(FT_Face face,FT_UInt table_index,FT_ULong * tag,FT_ULong * length)4264 FT_Sfnt_Table_Info( FT_Face face, 4265 FT_UInt table_index, 4266 FT_ULong *tag, 4267 FT_ULong *length ) 4268 { 4269 FT_Service_SFNT_Table service; 4270 FT_ULong offset; 4271 4272 4273 /* test for valid `length' delayed to `service->table_info' */ 4274 4275 if ( !face || !FT_IS_SFNT( face ) ) 4276 return FT_THROW( Invalid_Face_Handle ); 4277 4278 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE ); 4279 if ( !service ) 4280 return FT_THROW( Unimplemented_Feature ); 4281 4282 return service->table_info( face, table_index, tag, &offset, length ); 4283 } 4284 4285 4286 /* documentation is in tttables.h */ 4287 4288 FT_EXPORT_DEF( FT_ULong ) FT_Get_CMap_Language_ID(FT_CharMap charmap)4289 FT_Get_CMap_Language_ID( FT_CharMap charmap ) 4290 { 4291 FT_Service_TTCMaps service; 4292 FT_Face face; 4293 TT_CMapInfo cmap_info; 4294 4295 4296 if ( !charmap || !charmap->face ) 4297 return 0; 4298 4299 face = charmap->face; 4300 FT_FACE_FIND_SERVICE( face, service, TT_CMAP ); 4301 if ( !service ) 4302 return 0; 4303 if ( service->get_cmap_info( charmap, &cmap_info )) 4304 return 0; 4305 4306 return cmap_info.language; 4307 } 4308 4309 4310 /* documentation is in tttables.h */ 4311 4312 FT_EXPORT_DEF( FT_Long ) FT_Get_CMap_Format(FT_CharMap charmap)4313 FT_Get_CMap_Format( FT_CharMap charmap ) 4314 { 4315 FT_Service_TTCMaps service; 4316 FT_Face face; 4317 TT_CMapInfo cmap_info; 4318 4319 4320 if ( !charmap || !charmap->face ) 4321 return -1; 4322 4323 face = charmap->face; 4324 FT_FACE_FIND_SERVICE( face, service, TT_CMAP ); 4325 if ( !service ) 4326 return -1; 4327 if ( service->get_cmap_info( charmap, &cmap_info )) 4328 return -1; 4329 4330 return cmap_info.format; 4331 } 4332 4333 4334 /* documentation is in ftsizes.h */ 4335 4336 FT_EXPORT_DEF( FT_Error ) FT_Activate_Size(FT_Size size)4337 FT_Activate_Size( FT_Size size ) 4338 { 4339 FT_Face face; 4340 4341 4342 if ( !size ) 4343 return FT_THROW( Invalid_Size_Handle ); 4344 4345 face = size->face; 4346 if ( !face || !face->driver ) 4347 return FT_THROW( Invalid_Face_Handle ); 4348 4349 /* we don't need anything more complex than that; all size objects */ 4350 /* are already listed by the face */ 4351 face->size = size; 4352 4353 return FT_Err_Ok; 4354 } 4355 4356 4357 /*************************************************************************/ 4358 /*************************************************************************/ 4359 /*************************************************************************/ 4360 /**** ****/ 4361 /**** ****/ 4362 /**** R E N D E R E R S ****/ 4363 /**** ****/ 4364 /**** ****/ 4365 /*************************************************************************/ 4366 /*************************************************************************/ 4367 /*************************************************************************/ 4368 4369 /* lookup a renderer by glyph format in the library's list */ 4370 FT_BASE_DEF( FT_Renderer ) FT_Lookup_Renderer(FT_Library library,FT_Glyph_Format format,FT_ListNode * node)4371 FT_Lookup_Renderer( FT_Library library, 4372 FT_Glyph_Format format, 4373 FT_ListNode* node ) 4374 { 4375 FT_ListNode cur; 4376 FT_Renderer result = NULL; 4377 4378 4379 if ( !library ) 4380 goto Exit; 4381 4382 cur = library->renderers.head; 4383 4384 if ( node ) 4385 { 4386 if ( *node ) 4387 cur = (*node)->next; 4388 *node = NULL; 4389 } 4390 4391 while ( cur ) 4392 { 4393 FT_Renderer renderer = FT_RENDERER( cur->data ); 4394 4395 4396 if ( renderer->glyph_format == format ) 4397 { 4398 if ( node ) 4399 *node = cur; 4400 4401 result = renderer; 4402 break; 4403 } 4404 cur = cur->next; 4405 } 4406 4407 Exit: 4408 return result; 4409 } 4410 4411 4412 static FT_Renderer ft_lookup_glyph_renderer(FT_GlyphSlot slot)4413 ft_lookup_glyph_renderer( FT_GlyphSlot slot ) 4414 { 4415 FT_Face face = slot->face; 4416 FT_Library library = FT_FACE_LIBRARY( face ); 4417 FT_Renderer result = library->cur_renderer; 4418 4419 4420 if ( !result || result->glyph_format != slot->format ) 4421 result = FT_Lookup_Renderer( library, slot->format, 0 ); 4422 4423 return result; 4424 } 4425 4426 4427 static void ft_set_current_renderer(FT_Library library)4428 ft_set_current_renderer( FT_Library library ) 4429 { 4430 FT_Renderer renderer; 4431 4432 4433 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 ); 4434 library->cur_renderer = renderer; 4435 } 4436 4437 4438 static FT_Error ft_add_renderer(FT_Module module)4439 ft_add_renderer( FT_Module module ) 4440 { 4441 FT_Library library = module->library; 4442 FT_Memory memory = library->memory; 4443 FT_Error error; 4444 FT_ListNode node = NULL; 4445 4446 4447 if ( FT_NEW( node ) ) 4448 goto Exit; 4449 4450 { 4451 FT_Renderer render = FT_RENDERER( module ); 4452 FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz; 4453 4454 4455 render->clazz = clazz; 4456 render->glyph_format = clazz->glyph_format; 4457 4458 /* allocate raster object if needed */ 4459 if ( clazz->raster_class->raster_new ) 4460 { 4461 error = clazz->raster_class->raster_new( memory, &render->raster ); 4462 if ( error ) 4463 goto Fail; 4464 4465 render->raster_render = clazz->raster_class->raster_render; 4466 render->render = clazz->render_glyph; 4467 } 4468 4469 /* add to list */ 4470 node->data = module; 4471 FT_List_Add( &library->renderers, node ); 4472 4473 ft_set_current_renderer( library ); 4474 } 4475 4476 Fail: 4477 if ( error ) 4478 FT_FREE( node ); 4479 4480 Exit: 4481 return error; 4482 } 4483 4484 4485 static void ft_remove_renderer(FT_Module module)4486 ft_remove_renderer( FT_Module module ) 4487 { 4488 FT_Library library; 4489 FT_Memory memory; 4490 FT_ListNode node; 4491 4492 4493 library = module->library; 4494 if ( !library ) 4495 return; 4496 4497 memory = library->memory; 4498 4499 node = FT_List_Find( &library->renderers, module ); 4500 if ( node ) 4501 { 4502 FT_Renderer render = FT_RENDERER( module ); 4503 4504 4505 /* release raster object, if any */ 4506 if ( render->raster ) 4507 render->clazz->raster_class->raster_done( render->raster ); 4508 4509 /* remove from list */ 4510 FT_List_Remove( &library->renderers, node ); 4511 FT_FREE( node ); 4512 4513 ft_set_current_renderer( library ); 4514 } 4515 } 4516 4517 4518 /* documentation is in ftrender.h */ 4519 4520 FT_EXPORT_DEF( FT_Renderer ) FT_Get_Renderer(FT_Library library,FT_Glyph_Format format)4521 FT_Get_Renderer( FT_Library library, 4522 FT_Glyph_Format format ) 4523 { 4524 /* test for valid `library' delayed to `FT_Lookup_Renderer' */ 4525 4526 return FT_Lookup_Renderer( library, format, 0 ); 4527 } 4528 4529 4530 /* documentation is in ftrender.h */ 4531 4532 FT_EXPORT_DEF( FT_Error ) FT_Set_Renderer(FT_Library library,FT_Renderer renderer,FT_UInt num_params,FT_Parameter * parameters)4533 FT_Set_Renderer( FT_Library library, 4534 FT_Renderer renderer, 4535 FT_UInt num_params, 4536 FT_Parameter* parameters ) 4537 { 4538 FT_ListNode node; 4539 FT_Error error = FT_Err_Ok; 4540 4541 FT_Renderer_SetModeFunc set_mode; 4542 4543 4544 if ( !library ) 4545 { 4546 error = FT_THROW( Invalid_Library_Handle ); 4547 goto Exit; 4548 } 4549 4550 if ( !renderer ) 4551 { 4552 error = FT_THROW( Invalid_Argument ); 4553 goto Exit; 4554 } 4555 4556 if ( num_params > 0 && !parameters ) 4557 { 4558 error = FT_THROW( Invalid_Argument ); 4559 goto Exit; 4560 } 4561 4562 node = FT_List_Find( &library->renderers, renderer ); 4563 if ( !node ) 4564 { 4565 error = FT_THROW( Invalid_Argument ); 4566 goto Exit; 4567 } 4568 4569 FT_List_Up( &library->renderers, node ); 4570 4571 if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE ) 4572 library->cur_renderer = renderer; 4573 4574 set_mode = renderer->clazz->set_mode; 4575 4576 for ( ; num_params > 0; num_params-- ) 4577 { 4578 error = set_mode( renderer, parameters->tag, parameters->data ); 4579 if ( error ) 4580 break; 4581 parameters++; 4582 } 4583 4584 Exit: 4585 return error; 4586 } 4587 4588 4589 FT_BASE_DEF( FT_Error ) FT_Render_Glyph_Internal(FT_Library library,FT_GlyphSlot slot,FT_Render_Mode render_mode)4590 FT_Render_Glyph_Internal( FT_Library library, 4591 FT_GlyphSlot slot, 4592 FT_Render_Mode render_mode ) 4593 { 4594 FT_Error error = FT_Err_Ok; 4595 FT_Face face = slot->face; 4596 FT_Renderer renderer; 4597 4598 4599 switch ( slot->format ) 4600 { 4601 default: 4602 if ( slot->internal->load_flags & FT_LOAD_COLOR ) 4603 { 4604 FT_LayerIterator iterator; 4605 4606 FT_UInt base_glyph = slot->glyph_index; 4607 4608 FT_Bool have_layers; 4609 FT_UInt glyph_index; 4610 FT_UInt color_index; 4611 4612 4613 /* check whether we have colored glyph layers */ 4614 iterator.p = NULL; 4615 have_layers = FT_Get_Color_Glyph_Layer( face, 4616 base_glyph, 4617 &glyph_index, 4618 &color_index, 4619 &iterator ); 4620 if ( have_layers ) 4621 { 4622 error = FT_New_GlyphSlot( face, NULL ); 4623 if ( !error ) 4624 { 4625 TT_Face ttface = (TT_Face)face; 4626 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt; 4627 4628 4629 do 4630 { 4631 FT_Int32 load_flags = slot->internal->load_flags; 4632 4633 4634 /* disable the `FT_LOAD_COLOR' flag to avoid recursion */ 4635 /* right here in this function */ 4636 load_flags &= ~FT_LOAD_COLOR; 4637 4638 /* render into the new `face->glyph' glyph slot */ 4639 load_flags |= FT_LOAD_RENDER; 4640 4641 error = FT_Load_Glyph( face, glyph_index, load_flags ); 4642 if ( error ) 4643 break; 4644 4645 /* blend new `face->glyph' into old `slot'; */ 4646 /* at the first call, `slot' is still empty */ 4647 error = sfnt->colr_blend( ttface, 4648 color_index, 4649 slot, 4650 face->glyph ); 4651 if ( error ) 4652 break; 4653 4654 } while ( FT_Get_Color_Glyph_Layer( face, 4655 base_glyph, 4656 &glyph_index, 4657 &color_index, 4658 &iterator ) ); 4659 4660 if ( !error ) 4661 slot->format = FT_GLYPH_FORMAT_BITMAP; 4662 4663 /* this call also restores `slot' as the glyph slot */ 4664 FT_Done_GlyphSlot( face->glyph ); 4665 } 4666 4667 if ( !error ) 4668 return error; 4669 4670 /* Failed to do the colored layer. Draw outline instead. */ 4671 slot->format = FT_GLYPH_FORMAT_OUTLINE; 4672 } 4673 } 4674 4675 { 4676 FT_ListNode node = NULL; 4677 4678 4679 /* small shortcut for the very common case */ 4680 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE ) 4681 { 4682 renderer = library->cur_renderer; 4683 node = library->renderers.head; 4684 } 4685 else 4686 renderer = FT_Lookup_Renderer( library, slot->format, &node ); 4687 4688 error = FT_ERR( Unimplemented_Feature ); 4689 while ( renderer ) 4690 { 4691 error = renderer->render( renderer, slot, render_mode, NULL ); 4692 if ( !error || 4693 FT_ERR_NEQ( error, Cannot_Render_Glyph ) ) 4694 break; 4695 4696 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */ 4697 /* is unsupported by the current renderer for this glyph image */ 4698 /* format. */ 4699 4700 /* now, look for another renderer that supports the same */ 4701 /* format. */ 4702 renderer = FT_Lookup_Renderer( library, slot->format, &node ); 4703 } 4704 } 4705 } 4706 4707 #ifdef FT_DEBUG_LEVEL_TRACE 4708 4709 #undef FT_COMPONENT 4710 #define FT_COMPONENT checksum 4711 4712 /* 4713 * Computing the MD5 checksum is expensive, unnecessarily distorting a 4714 * possible profiling of FreeType if compiled with tracing support. For 4715 * this reason, we execute the following code only if explicitly 4716 * requested. 4717 */ 4718 4719 /* we use FT_TRACE3 in this block */ 4720 if ( !error && 4721 ft_trace_levels[trace_checksum] >= 3 && 4722 slot->bitmap.buffer ) 4723 { 4724 FT_Bitmap bitmap; 4725 FT_Error err; 4726 4727 4728 FT_Bitmap_Init( &bitmap ); 4729 4730 /* we convert to a single bitmap format for computing the checksum */ 4731 /* this also converts the bitmap flow to `down' (i.e., pitch > 0) */ 4732 err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 ); 4733 if ( !err ) 4734 { 4735 MD5_CTX ctx; 4736 unsigned char md5[16]; 4737 unsigned long coverage = 0; 4738 int i, j; 4739 int rows = (int)bitmap.rows; 4740 int pitch = bitmap.pitch; 4741 4742 4743 FT_TRACE3(( "FT_Render_Glyph: bitmap %dx%d, %s (mode %d)\n", 4744 pitch, 4745 rows, 4746 pixel_modes[slot->bitmap.pixel_mode], 4747 slot->bitmap.pixel_mode )); 4748 4749 for ( i = 0; i < rows; i++ ) 4750 for ( j = 0; j < pitch; j++ ) 4751 coverage += bitmap.buffer[i * pitch + j]; 4752 4753 FT_TRACE3(( " Total coverage: %lu\n", coverage )); 4754 4755 MD5_Init( &ctx ); 4756 if ( bitmap.buffer ) 4757 MD5_Update( &ctx, bitmap.buffer, 4758 (unsigned long)rows * (unsigned long)pitch ); 4759 MD5_Final( md5, &ctx ); 4760 4761 FT_TRACE3(( " MD5 checksum: " )); 4762 for ( i = 0; i < 16; i++ ) 4763 FT_TRACE3(( "%02X", md5[i] )); 4764 FT_TRACE3(( "\n" )); 4765 } 4766 4767 FT_Bitmap_Done( library, &bitmap ); 4768 } 4769 4770 /* 4771 * Dump bitmap in Netpbm format (PBM or PGM). 4772 */ 4773 4774 /* we use FT_TRACE7 in this block */ 4775 if ( !error && 4776 ft_trace_levels[trace_checksum] >= 7 && 4777 slot->bitmap.buffer ) 4778 { 4779 if ( slot->bitmap.rows < 128U && 4780 slot->bitmap.width < 128U ) 4781 { 4782 int rows = (int)slot->bitmap.rows; 4783 int width = (int)slot->bitmap.width; 4784 int pitch = slot->bitmap.pitch; 4785 int i, j, m; 4786 4787 unsigned char* topleft = slot->bitmap.buffer; 4788 4789 4790 if ( pitch < 0 ) 4791 topleft -= pitch * ( rows - 1 ); 4792 4793 FT_TRACE7(( "Netpbm image: start\n" )); 4794 switch ( slot->bitmap.pixel_mode ) 4795 { 4796 case FT_PIXEL_MODE_MONO: 4797 FT_TRACE7(( "P1 %d %d\n", width, rows )); 4798 for ( i = 0; i < rows; i++ ) 4799 { 4800 for ( j = 0; j < width; ) 4801 for ( m = 128; m > 0 && j < width; m >>= 1, j++ ) 4802 FT_TRACE7(( " %d", 4803 ( topleft[i * pitch + j / 8] & m ) != 0 )); 4804 FT_TRACE7(( "\n" )); 4805 } 4806 break; 4807 4808 default: 4809 FT_TRACE7(( "P2 %d %d 255\n", width, rows )); 4810 for ( i = 0; i < rows; i++ ) 4811 { 4812 for ( j = 0; j < width; j += 1 ) 4813 FT_TRACE7(( " %3u", topleft[i * pitch + j] )); 4814 FT_TRACE7(( "\n" )); 4815 } 4816 } 4817 FT_TRACE7(( "Netpbm image: end\n" )); 4818 } 4819 else 4820 FT_TRACE7(( "Netpbm image: too large, omitted\n" )); 4821 } 4822 4823 #undef FT_COMPONENT 4824 #define FT_COMPONENT objs 4825 4826 #endif /* FT_DEBUG_LEVEL_TRACE */ 4827 4828 return error; 4829 } 4830 4831 4832 /* documentation is in freetype.h */ 4833 4834 FT_EXPORT_DEF( FT_Error ) FT_Render_Glyph(FT_GlyphSlot slot,FT_Render_Mode render_mode)4835 FT_Render_Glyph( FT_GlyphSlot slot, 4836 FT_Render_Mode render_mode ) 4837 { 4838 FT_Library library; 4839 4840 4841 if ( !slot || !slot->face ) 4842 return FT_THROW( Invalid_Argument ); 4843 4844 library = FT_FACE_LIBRARY( slot->face ); 4845 4846 return FT_Render_Glyph_Internal( library, slot, render_mode ); 4847 } 4848 4849 4850 /*************************************************************************/ 4851 /*************************************************************************/ 4852 /*************************************************************************/ 4853 /**** ****/ 4854 /**** ****/ 4855 /**** M O D U L E S ****/ 4856 /**** ****/ 4857 /**** ****/ 4858 /*************************************************************************/ 4859 /*************************************************************************/ 4860 /*************************************************************************/ 4861 4862 4863 /************************************************************************** 4864 * 4865 * @Function: 4866 * Destroy_Module 4867 * 4868 * @Description: 4869 * Destroys a given module object. For drivers, this also destroys 4870 * all child faces. 4871 * 4872 * @InOut: 4873 * module :: 4874 * A handle to the target driver object. 4875 * 4876 * @Note: 4877 * The driver _must_ be LOCKED! 4878 */ 4879 static void Destroy_Module(FT_Module module)4880 Destroy_Module( FT_Module module ) 4881 { 4882 FT_Memory memory = module->memory; 4883 FT_Module_Class* clazz = module->clazz; 4884 FT_Library library = module->library; 4885 4886 4887 if ( library && library->auto_hinter == module ) 4888 library->auto_hinter = NULL; 4889 4890 /* if the module is a renderer */ 4891 if ( FT_MODULE_IS_RENDERER( module ) ) 4892 ft_remove_renderer( module ); 4893 4894 /* if the module is a font driver, add some steps */ 4895 if ( FT_MODULE_IS_DRIVER( module ) ) 4896 Destroy_Driver( FT_DRIVER( module ) ); 4897 4898 /* finalize the module object */ 4899 if ( clazz->module_done ) 4900 clazz->module_done( module ); 4901 4902 /* discard it */ 4903 FT_FREE( module ); 4904 } 4905 4906 4907 /* documentation is in ftmodapi.h */ 4908 4909 FT_EXPORT_DEF( FT_Error ) FT_Add_Module(FT_Library library,const FT_Module_Class * clazz)4910 FT_Add_Module( FT_Library library, 4911 const FT_Module_Class* clazz ) 4912 { 4913 FT_Error error; 4914 FT_Memory memory; 4915 FT_Module module = NULL; 4916 FT_UInt nn; 4917 4918 4919 #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \ 4920 FREETYPE_MINOR ) 4921 4922 if ( !library ) 4923 return FT_THROW( Invalid_Library_Handle ); 4924 4925 if ( !clazz ) 4926 return FT_THROW( Invalid_Argument ); 4927 4928 /* check FreeType version */ 4929 if ( clazz->module_requires > FREETYPE_VER_FIXED ) 4930 return FT_THROW( Invalid_Version ); 4931 4932 /* look for a module with the same name in the library's table */ 4933 for ( nn = 0; nn < library->num_modules; nn++ ) 4934 { 4935 module = library->modules[nn]; 4936 if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 ) 4937 { 4938 /* this installed module has the same name, compare their versions */ 4939 if ( clazz->module_version <= module->clazz->module_version ) 4940 return FT_THROW( Lower_Module_Version ); 4941 4942 /* remove the module from our list, then exit the loop to replace */ 4943 /* it by our new version.. */ 4944 FT_Remove_Module( library, module ); 4945 break; 4946 } 4947 } 4948 4949 memory = library->memory; 4950 error = FT_Err_Ok; 4951 4952 if ( library->num_modules >= FT_MAX_MODULES ) 4953 { 4954 error = FT_THROW( Too_Many_Drivers ); 4955 goto Exit; 4956 } 4957 4958 /* allocate module object */ 4959 if ( FT_ALLOC( module, clazz->module_size ) ) 4960 goto Exit; 4961 4962 /* base initialization */ 4963 module->library = library; 4964 module->memory = memory; 4965 module->clazz = (FT_Module_Class*)clazz; 4966 4967 /* check whether the module is a renderer - this must be performed */ 4968 /* before the normal module initialization */ 4969 if ( FT_MODULE_IS_RENDERER( module ) ) 4970 { 4971 /* add to the renderers list */ 4972 error = ft_add_renderer( module ); 4973 if ( error ) 4974 goto Fail; 4975 } 4976 4977 /* is the module a auto-hinter? */ 4978 if ( FT_MODULE_IS_HINTER( module ) ) 4979 library->auto_hinter = module; 4980 4981 /* if the module is a font driver */ 4982 if ( FT_MODULE_IS_DRIVER( module ) ) 4983 { 4984 FT_Driver driver = FT_DRIVER( module ); 4985 4986 4987 driver->clazz = (FT_Driver_Class)module->clazz; 4988 } 4989 4990 if ( clazz->module_init ) 4991 { 4992 error = clazz->module_init( module ); 4993 if ( error ) 4994 goto Fail; 4995 } 4996 4997 /* add module to the library's table */ 4998 library->modules[library->num_modules++] = module; 4999 5000 Exit: 5001 return error; 5002 5003 Fail: 5004 if ( FT_MODULE_IS_RENDERER( module ) ) 5005 { 5006 FT_Renderer renderer = FT_RENDERER( module ); 5007 5008 5009 if ( renderer->clazz && 5010 renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE && 5011 renderer->raster ) 5012 renderer->clazz->raster_class->raster_done( renderer->raster ); 5013 } 5014 5015 FT_FREE( module ); 5016 goto Exit; 5017 } 5018 5019 5020 /* documentation is in ftmodapi.h */ 5021 5022 FT_EXPORT_DEF( FT_Module ) FT_Get_Module(FT_Library library,const char * module_name)5023 FT_Get_Module( FT_Library library, 5024 const char* module_name ) 5025 { 5026 FT_Module result = NULL; 5027 FT_Module* cur; 5028 FT_Module* limit; 5029 5030 5031 if ( !library || !module_name ) 5032 return result; 5033 5034 cur = library->modules; 5035 limit = cur + library->num_modules; 5036 5037 for ( ; cur < limit; cur++ ) 5038 if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 ) 5039 { 5040 result = cur[0]; 5041 break; 5042 } 5043 5044 return result; 5045 } 5046 5047 5048 /* documentation is in ftobjs.h */ 5049 5050 FT_BASE_DEF( const void* ) FT_Get_Module_Interface(FT_Library library,const char * mod_name)5051 FT_Get_Module_Interface( FT_Library library, 5052 const char* mod_name ) 5053 { 5054 FT_Module module; 5055 5056 5057 /* test for valid `library' delayed to FT_Get_Module() */ 5058 5059 module = FT_Get_Module( library, mod_name ); 5060 5061 return module ? module->clazz->module_interface : 0; 5062 } 5063 5064 5065 FT_BASE_DEF( FT_Pointer ) ft_module_get_service(FT_Module module,const char * service_id,FT_Bool global)5066 ft_module_get_service( FT_Module module, 5067 const char* service_id, 5068 FT_Bool global ) 5069 { 5070 FT_Pointer result = NULL; 5071 5072 5073 if ( module ) 5074 { 5075 FT_ASSERT( module->clazz && module->clazz->get_interface ); 5076 5077 /* first, look for the service in the module */ 5078 if ( module->clazz->get_interface ) 5079 result = module->clazz->get_interface( module, service_id ); 5080 5081 if ( global && !result ) 5082 { 5083 /* we didn't find it, look in all other modules then */ 5084 FT_Library library = module->library; 5085 FT_Module* cur = library->modules; 5086 FT_Module* limit = cur + library->num_modules; 5087 5088 5089 for ( ; cur < limit; cur++ ) 5090 { 5091 if ( cur[0] != module ) 5092 { 5093 FT_ASSERT( cur[0]->clazz ); 5094 5095 if ( cur[0]->clazz->get_interface ) 5096 { 5097 result = cur[0]->clazz->get_interface( cur[0], service_id ); 5098 if ( result ) 5099 break; 5100 } 5101 } 5102 } 5103 } 5104 } 5105 5106 return result; 5107 } 5108 5109 5110 /* documentation is in ftmodapi.h */ 5111 5112 FT_EXPORT_DEF( FT_Error ) FT_Remove_Module(FT_Library library,FT_Module module)5113 FT_Remove_Module( FT_Library library, 5114 FT_Module module ) 5115 { 5116 /* try to find the module from the table, then remove it from there */ 5117 5118 if ( !library ) 5119 return FT_THROW( Invalid_Library_Handle ); 5120 5121 if ( module ) 5122 { 5123 FT_Module* cur = library->modules; 5124 FT_Module* limit = cur + library->num_modules; 5125 5126 5127 for ( ; cur < limit; cur++ ) 5128 { 5129 if ( cur[0] == module ) 5130 { 5131 /* remove it from the table */ 5132 library->num_modules--; 5133 limit--; 5134 while ( cur < limit ) 5135 { 5136 cur[0] = cur[1]; 5137 cur++; 5138 } 5139 limit[0] = NULL; 5140 5141 /* destroy the module */ 5142 Destroy_Module( module ); 5143 5144 return FT_Err_Ok; 5145 } 5146 } 5147 } 5148 return FT_THROW( Invalid_Driver_Handle ); 5149 } 5150 5151 5152 static FT_Error ft_property_do(FT_Library library,const FT_String * module_name,const FT_String * property_name,void * value,FT_Bool set,FT_Bool value_is_string)5153 ft_property_do( FT_Library library, 5154 const FT_String* module_name, 5155 const FT_String* property_name, 5156 void* value, 5157 FT_Bool set, 5158 FT_Bool value_is_string ) 5159 { 5160 FT_Module* cur; 5161 FT_Module* limit; 5162 FT_Module_Interface interface; 5163 5164 FT_Service_Properties service; 5165 5166 #ifdef FT_DEBUG_LEVEL_ERROR 5167 const FT_String* set_name = "FT_Property_Set"; 5168 const FT_String* get_name = "FT_Property_Get"; 5169 const FT_String* func_name = set ? set_name : get_name; 5170 #endif 5171 5172 FT_Bool missing_func; 5173 5174 5175 if ( !library ) 5176 return FT_THROW( Invalid_Library_Handle ); 5177 5178 if ( !module_name || !property_name || !value ) 5179 return FT_THROW( Invalid_Argument ); 5180 5181 cur = library->modules; 5182 limit = cur + library->num_modules; 5183 5184 /* search module */ 5185 for ( ; cur < limit; cur++ ) 5186 if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) ) 5187 break; 5188 5189 if ( cur == limit ) 5190 { 5191 FT_TRACE2(( "%s: can't find module `%s'\n", 5192 func_name, module_name )); 5193 return FT_THROW( Missing_Module ); 5194 } 5195 5196 /* check whether we have a service interface */ 5197 if ( !cur[0]->clazz->get_interface ) 5198 { 5199 FT_TRACE2(( "%s: module `%s' doesn't support properties\n", 5200 func_name, module_name )); 5201 return FT_THROW( Unimplemented_Feature ); 5202 } 5203 5204 /* search property service */ 5205 interface = cur[0]->clazz->get_interface( cur[0], 5206 FT_SERVICE_ID_PROPERTIES ); 5207 if ( !interface ) 5208 { 5209 FT_TRACE2(( "%s: module `%s' doesn't support properties\n", 5210 func_name, module_name )); 5211 return FT_THROW( Unimplemented_Feature ); 5212 } 5213 5214 service = (FT_Service_Properties)interface; 5215 5216 if ( set ) 5217 missing_func = FT_BOOL( !service->set_property ); 5218 else 5219 missing_func = FT_BOOL( !service->get_property ); 5220 5221 if ( missing_func ) 5222 { 5223 FT_TRACE2(( "%s: property service of module `%s' is broken\n", 5224 func_name, module_name )); 5225 return FT_THROW( Unimplemented_Feature ); 5226 } 5227 5228 return set ? service->set_property( cur[0], 5229 property_name, 5230 value, 5231 value_is_string ) 5232 : service->get_property( cur[0], 5233 property_name, 5234 value ); 5235 } 5236 5237 5238 /* documentation is in ftmodapi.h */ 5239 5240 FT_EXPORT_DEF( FT_Error ) FT_Property_Set(FT_Library library,const FT_String * module_name,const FT_String * property_name,const void * value)5241 FT_Property_Set( FT_Library library, 5242 const FT_String* module_name, 5243 const FT_String* property_name, 5244 const void* value ) 5245 { 5246 return ft_property_do( library, 5247 module_name, 5248 property_name, 5249 (void*)value, 5250 TRUE, 5251 FALSE ); 5252 } 5253 5254 5255 /* documentation is in ftmodapi.h */ 5256 5257 FT_EXPORT_DEF( FT_Error ) FT_Property_Get(FT_Library library,const FT_String * module_name,const FT_String * property_name,void * value)5258 FT_Property_Get( FT_Library library, 5259 const FT_String* module_name, 5260 const FT_String* property_name, 5261 void* value ) 5262 { 5263 return ft_property_do( library, 5264 module_name, 5265 property_name, 5266 value, 5267 FALSE, 5268 FALSE ); 5269 } 5270 5271 5272 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES 5273 5274 /* this variant is used for handling the FREETYPE_PROPERTIES */ 5275 /* environment variable */ 5276 5277 FT_BASE_DEF( FT_Error ) ft_property_string_set(FT_Library library,const FT_String * module_name,const FT_String * property_name,FT_String * value)5278 ft_property_string_set( FT_Library library, 5279 const FT_String* module_name, 5280 const FT_String* property_name, 5281 FT_String* value ) 5282 { 5283 return ft_property_do( library, 5284 module_name, 5285 property_name, 5286 (void*)value, 5287 TRUE, 5288 TRUE ); 5289 } 5290 5291 #endif 5292 5293 5294 /*************************************************************************/ 5295 /*************************************************************************/ 5296 /*************************************************************************/ 5297 /**** ****/ 5298 /**** ****/ 5299 /**** L I B R A R Y ****/ 5300 /**** ****/ 5301 /**** ****/ 5302 /*************************************************************************/ 5303 /*************************************************************************/ 5304 /*************************************************************************/ 5305 5306 5307 /* documentation is in ftmodapi.h */ 5308 5309 FT_EXPORT_DEF( FT_Error ) FT_Reference_Library(FT_Library library)5310 FT_Reference_Library( FT_Library library ) 5311 { 5312 if ( !library ) 5313 return FT_THROW( Invalid_Library_Handle ); 5314 5315 library->refcount++; 5316 5317 return FT_Err_Ok; 5318 } 5319 5320 5321 /* documentation is in ftmodapi.h */ 5322 5323 FT_EXPORT_DEF( FT_Error ) FT_New_Library(FT_Memory memory,FT_Library * alibrary)5324 FT_New_Library( FT_Memory memory, 5325 FT_Library *alibrary ) 5326 { 5327 FT_Library library = NULL; 5328 FT_Error error; 5329 5330 5331 if ( !memory || !alibrary ) 5332 return FT_THROW( Invalid_Argument ); 5333 5334 #ifndef FT_DEBUG_LOGGING 5335 #ifdef FT_DEBUG_LEVEL_ERROR 5336 /* init debugging support */ 5337 ft_debug_init(); 5338 #endif /* FT_DEBUG_LEVEL_ERROR */ 5339 #endif /* !FT_DEBUG_LOGGING */ 5340 5341 /* first of all, allocate the library object */ 5342 if ( FT_NEW( library ) ) 5343 return error; 5344 5345 library->memory = memory; 5346 5347 library->version_major = FREETYPE_MAJOR; 5348 library->version_minor = FREETYPE_MINOR; 5349 library->version_patch = FREETYPE_PATCH; 5350 5351 library->refcount = 1; 5352 5353 /* That's ok now */ 5354 *alibrary = library; 5355 5356 return FT_Err_Ok; 5357 } 5358 5359 5360 /* documentation is in freetype.h */ 5361 5362 FT_EXPORT_DEF( void ) FT_Library_Version(FT_Library library,FT_Int * amajor,FT_Int * aminor,FT_Int * apatch)5363 FT_Library_Version( FT_Library library, 5364 FT_Int *amajor, 5365 FT_Int *aminor, 5366 FT_Int *apatch ) 5367 { 5368 FT_Int major = 0; 5369 FT_Int minor = 0; 5370 FT_Int patch = 0; 5371 5372 5373 if ( library ) 5374 { 5375 major = library->version_major; 5376 minor = library->version_minor; 5377 patch = library->version_patch; 5378 } 5379 5380 if ( amajor ) 5381 *amajor = major; 5382 5383 if ( aminor ) 5384 *aminor = minor; 5385 5386 if ( apatch ) 5387 *apatch = patch; 5388 } 5389 5390 5391 /* documentation is in ftmodapi.h */ 5392 5393 FT_EXPORT_DEF( FT_Error ) FT_Done_Library(FT_Library library)5394 FT_Done_Library( FT_Library library ) 5395 { 5396 FT_Memory memory; 5397 5398 5399 if ( !library ) 5400 return FT_THROW( Invalid_Library_Handle ); 5401 5402 library->refcount--; 5403 if ( library->refcount > 0 ) 5404 goto Exit; 5405 5406 memory = library->memory; 5407 5408 /* 5409 * Close all faces in the library. If we don't do this, we can have 5410 * some subtle memory leaks. 5411 * 5412 * Example: 5413 * 5414 * - the cff font driver uses the pshinter module in cff_size_done 5415 * - if the pshinter module is destroyed before the cff font driver, 5416 * opened FT_Face objects managed by the driver are not properly 5417 * destroyed, resulting in a memory leak 5418 * 5419 * Some faces are dependent on other faces, like Type42 faces that 5420 * depend on TrueType faces synthesized internally. 5421 * 5422 * The order of drivers should be specified in driver_name[]. 5423 */ 5424 { 5425 FT_UInt m, n; 5426 const char* driver_name[] = { "type42", NULL }; 5427 5428 5429 for ( m = 0; 5430 m < sizeof ( driver_name ) / sizeof ( driver_name[0] ); 5431 m++ ) 5432 { 5433 for ( n = 0; n < library->num_modules; n++ ) 5434 { 5435 FT_Module module = library->modules[n]; 5436 const char* module_name = module->clazz->module_name; 5437 FT_List faces; 5438 5439 5440 if ( driver_name[m] && 5441 ft_strcmp( module_name, driver_name[m] ) != 0 ) 5442 continue; 5443 5444 if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 ) 5445 continue; 5446 5447 FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name )); 5448 5449 faces = &FT_DRIVER( module )->faces_list; 5450 while ( faces->head ) 5451 { 5452 FT_Done_Face( FT_FACE( faces->head->data ) ); 5453 if ( faces->head ) 5454 FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" )); 5455 } 5456 } 5457 } 5458 } 5459 5460 /* Close all other modules in the library */ 5461 #if 1 5462 /* XXX Modules are removed in the reversed order so that */ 5463 /* type42 module is removed before truetype module. This */ 5464 /* avoids double free in some occasions. It is a hack. */ 5465 while ( library->num_modules > 0 ) 5466 FT_Remove_Module( library, 5467 library->modules[library->num_modules - 1] ); 5468 #else 5469 { 5470 FT_UInt n; 5471 5472 5473 for ( n = 0; n < library->num_modules; n++ ) 5474 { 5475 FT_Module module = library->modules[n]; 5476 5477 5478 if ( module ) 5479 { 5480 Destroy_Module( module ); 5481 library->modules[n] = NULL; 5482 } 5483 } 5484 } 5485 #endif 5486 5487 FT_FREE( library ); 5488 5489 Exit: 5490 return FT_Err_Ok; 5491 } 5492 5493 5494 /* documentation is in ftmodapi.h */ 5495 5496 FT_EXPORT_DEF( void ) FT_Set_Debug_Hook(FT_Library library,FT_UInt hook_index,FT_DebugHook_Func debug_hook)5497 FT_Set_Debug_Hook( FT_Library library, 5498 FT_UInt hook_index, 5499 FT_DebugHook_Func debug_hook ) 5500 { 5501 if ( library && debug_hook && 5502 hook_index < 5503 ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) ) 5504 library->debug_hooks[hook_index] = debug_hook; 5505 } 5506 5507 5508 /* documentation is in ftmodapi.h */ 5509 5510 FT_EXPORT_DEF( FT_TrueTypeEngineType ) FT_Get_TrueType_Engine_Type(FT_Library library)5511 FT_Get_TrueType_Engine_Type( FT_Library library ) 5512 { 5513 FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE; 5514 5515 5516 if ( library ) 5517 { 5518 FT_Module module = FT_Get_Module( library, "truetype" ); 5519 5520 5521 if ( module ) 5522 { 5523 FT_Service_TrueTypeEngine service; 5524 5525 5526 service = (FT_Service_TrueTypeEngine) 5527 ft_module_get_service( module, 5528 FT_SERVICE_ID_TRUETYPE_ENGINE, 5529 0 ); 5530 if ( service ) 5531 result = service->engine_type; 5532 } 5533 } 5534 5535 return result; 5536 } 5537 5538 5539 /* documentation is in freetype.h */ 5540 5541 FT_EXPORT_DEF( FT_Error ) FT_Get_SubGlyph_Info(FT_GlyphSlot glyph,FT_UInt sub_index,FT_Int * p_index,FT_UInt * p_flags,FT_Int * p_arg1,FT_Int * p_arg2,FT_Matrix * p_transform)5542 FT_Get_SubGlyph_Info( FT_GlyphSlot glyph, 5543 FT_UInt sub_index, 5544 FT_Int *p_index, 5545 FT_UInt *p_flags, 5546 FT_Int *p_arg1, 5547 FT_Int *p_arg2, 5548 FT_Matrix *p_transform ) 5549 { 5550 FT_Error error = FT_ERR( Invalid_Argument ); 5551 5552 5553 if ( glyph && 5554 glyph->subglyphs && 5555 glyph->format == FT_GLYPH_FORMAT_COMPOSITE && 5556 sub_index < glyph->num_subglyphs ) 5557 { 5558 FT_SubGlyph subg = glyph->subglyphs + sub_index; 5559 5560 5561 *p_index = subg->index; 5562 *p_flags = subg->flags; 5563 *p_arg1 = subg->arg1; 5564 *p_arg2 = subg->arg2; 5565 *p_transform = subg->transform; 5566 5567 error = FT_Err_Ok; 5568 } 5569 5570 return error; 5571 } 5572 5573 5574 /* documentation is in freetype.h */ 5575 5576 FT_EXPORT_DEF( FT_Bool ) FT_Get_Color_Glyph_Layer(FT_Face face,FT_UInt base_glyph,FT_UInt * aglyph_index,FT_UInt * acolor_index,FT_LayerIterator * iterator)5577 FT_Get_Color_Glyph_Layer( FT_Face face, 5578 FT_UInt base_glyph, 5579 FT_UInt *aglyph_index, 5580 FT_UInt *acolor_index, 5581 FT_LayerIterator* iterator ) 5582 { 5583 TT_Face ttface; 5584 SFNT_Service sfnt; 5585 5586 5587 if ( !face || 5588 !aglyph_index || 5589 !acolor_index || 5590 !iterator || 5591 base_glyph >= (FT_UInt)face->num_glyphs ) 5592 return 0; 5593 5594 if ( !FT_IS_SFNT( face ) ) 5595 return 0; 5596 5597 ttface = (TT_Face)face; 5598 sfnt = (SFNT_Service)ttface->sfnt; 5599 5600 if ( sfnt->get_colr_layer ) 5601 return sfnt->get_colr_layer( ttface, 5602 base_glyph, 5603 aglyph_index, 5604 acolor_index, 5605 iterator ); 5606 else 5607 return 0; 5608 } 5609 5610 5611 /* documentation is in freetype.h */ 5612 5613 FT_EXPORT_DEF( FT_Bool ) FT_Get_Color_Glyph_Paint(FT_Face face,FT_UInt base_glyph,FT_Color_Root_Transform root_transform,FT_OpaquePaint * paint)5614 FT_Get_Color_Glyph_Paint( FT_Face face, 5615 FT_UInt base_glyph, 5616 FT_Color_Root_Transform root_transform, 5617 FT_OpaquePaint* paint ) 5618 { 5619 TT_Face ttface; 5620 SFNT_Service sfnt; 5621 5622 5623 if ( !face || !paint ) 5624 return 0; 5625 5626 if ( !FT_IS_SFNT( face ) ) 5627 return 0; 5628 5629 ttface = (TT_Face)face; 5630 sfnt = (SFNT_Service)ttface->sfnt; 5631 5632 if ( sfnt->get_colr_layer ) 5633 return sfnt->get_colr_glyph_paint( ttface, 5634 base_glyph, 5635 root_transform, 5636 paint ); 5637 else 5638 return 0; 5639 } 5640 5641 5642 /* documentation is in freetype.h */ 5643 5644 FT_EXPORT_DEF( FT_Bool ) FT_Get_Paint_Layers(FT_Face face,FT_LayerIterator * layer_iterator,FT_OpaquePaint * paint)5645 FT_Get_Paint_Layers( FT_Face face, 5646 FT_LayerIterator* layer_iterator, 5647 FT_OpaquePaint* paint ) 5648 { 5649 TT_Face ttface; 5650 SFNT_Service sfnt; 5651 5652 5653 if ( !face || !paint || !layer_iterator ) 5654 return 0; 5655 5656 if ( !FT_IS_SFNT( face ) ) 5657 return 0; 5658 5659 ttface = (TT_Face)face; 5660 sfnt = (SFNT_Service)ttface->sfnt; 5661 5662 if ( sfnt->get_paint_layers ) 5663 return sfnt->get_paint_layers( ttface, layer_iterator, paint ); 5664 else 5665 return 0; 5666 } 5667 5668 5669 /* documentation is in freetype.h */ 5670 5671 FT_EXPORT_DEF( FT_Bool ) FT_Get_Paint(FT_Face face,FT_OpaquePaint opaque_paint,FT_COLR_Paint * paint)5672 FT_Get_Paint( FT_Face face, 5673 FT_OpaquePaint opaque_paint, 5674 FT_COLR_Paint* paint ) 5675 { 5676 TT_Face ttface; 5677 SFNT_Service sfnt; 5678 5679 5680 if ( !face || !paint || !paint ) 5681 return 0; 5682 5683 if ( !FT_IS_SFNT( face ) ) 5684 return 0; 5685 5686 ttface = (TT_Face)face; 5687 sfnt = (SFNT_Service)ttface->sfnt; 5688 5689 if ( sfnt->get_paint ) 5690 return sfnt->get_paint( ttface, opaque_paint, paint ); 5691 else 5692 return 0; 5693 } 5694 5695 5696 /* documentation is in freetype.h */ 5697 5698 FT_EXPORT_DEF( FT_Bool ) FT_Get_Colorline_Stops(FT_Face face,FT_ColorStop * color_stop,FT_ColorStopIterator * iterator)5699 FT_Get_Colorline_Stops ( FT_Face face, 5700 FT_ColorStop * color_stop, 5701 FT_ColorStopIterator *iterator ) 5702 { 5703 TT_Face ttface; 5704 SFNT_Service sfnt; 5705 5706 5707 if ( !face || !color_stop || !iterator ) 5708 return 0; 5709 5710 if ( !FT_IS_SFNT( face ) ) 5711 return 0; 5712 5713 ttface = (TT_Face)face; 5714 sfnt = (SFNT_Service)ttface->sfnt; 5715 5716 if ( sfnt->get_colorline_stops ) 5717 return sfnt->get_colorline_stops ( ttface, color_stop, iterator ); 5718 else 5719 return 0; 5720 } 5721 5722 5723 /* END */ 5724