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