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 ) 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 ) 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 ) 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 ) 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 ) 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 ) 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 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 ) 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 ) 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 ) 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 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 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 ) 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 ) 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 ) 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 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 ) 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 ) 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 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 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 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 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 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 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 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 ) 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 ) 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 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 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 ) 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 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 ) 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 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 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 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 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 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 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 ) 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 = -bsize->height; 2318 if ( bsize->x_ppem < 0 ) 2319 bsize->x_ppem = -bsize->x_ppem; 2320 if ( bsize->y_ppem < 0 ) 2321 bsize->y_ppem = -bsize->y_ppem; 2322 2323 /* check whether negation actually has worked */ 2324 if ( bsize->height < 0 || bsize->x_ppem < 0 || bsize->y_ppem < 0 ) 2325 { 2326 FT_TRACE0(( "FT_Open_Face:" 2327 " Invalid bitmap dimensions for stroke %d," 2328 " now disabled\n", i )); 2329 bsize->width = 0; 2330 bsize->height = 0; 2331 bsize->size = 0; 2332 bsize->x_ppem = 0; 2333 bsize->y_ppem = 0; 2334 } 2335 } 2336 } 2337 2338 /* initialize internal face data */ 2339 { 2340 FT_Face_Internal internal = face->internal; 2341 2342 2343 internal->transform_matrix.xx = 0x10000L; 2344 internal->transform_matrix.xy = 0; 2345 internal->transform_matrix.yx = 0; 2346 internal->transform_matrix.yy = 0x10000L; 2347 2348 internal->transform_delta.x = 0; 2349 internal->transform_delta.y = 0; 2350 2351 internal->refcount = 1; 2352 } 2353 2354 if ( aface ) 2355 *aface = face; 2356 else 2357 FT_Done_Face( face ); 2358 2359 goto Exit; 2360 2361 Fail: 2362 if ( node ) 2363 FT_Done_Face( face ); /* face must be in the driver's list */ 2364 else if ( face ) 2365 destroy_face( memory, face, driver ); 2366 2367 Exit: 2368 FT_TRACE4(( "FT_Open_Face: Return %d\n", error )); 2369 2370 return error; 2371 } 2372 2373 2374 /* documentation is in freetype.h */ 2375 2376 FT_EXPORT_DEF( FT_Error ) 2377 FT_Attach_File( FT_Face face, 2378 const char* filepathname ) 2379 { 2380 FT_Open_Args open; 2381 2382 2383 /* test for valid `face' delayed to `FT_Attach_Stream' */ 2384 2385 if ( !filepathname ) 2386 return FT_THROW( Invalid_Argument ); 2387 2388 open.stream = NULL; 2389 open.flags = FT_OPEN_PATHNAME; 2390 open.pathname = (char*)filepathname; 2391 2392 return FT_Attach_Stream( face, &open ); 2393 } 2394 2395 2396 /* documentation is in freetype.h */ 2397 2398 FT_EXPORT_DEF( FT_Error ) 2399 FT_Attach_Stream( FT_Face face, 2400 FT_Open_Args* parameters ) 2401 { 2402 FT_Stream stream; 2403 FT_Error error; 2404 FT_Driver driver; 2405 2406 FT_Driver_Class clazz; 2407 2408 2409 /* test for valid `parameters' delayed to `FT_Stream_New' */ 2410 2411 if ( !face ) 2412 return FT_THROW( Invalid_Face_Handle ); 2413 2414 driver = face->driver; 2415 if ( !driver ) 2416 return FT_THROW( Invalid_Driver_Handle ); 2417 2418 error = FT_Stream_New( driver->root.library, parameters, &stream ); 2419 if ( error ) 2420 goto Exit; 2421 2422 /* we implement FT_Attach_Stream in each driver through the */ 2423 /* `attach_file' interface */ 2424 2425 error = FT_ERR( Unimplemented_Feature ); 2426 clazz = driver->clazz; 2427 if ( clazz->attach_file ) 2428 error = clazz->attach_file( face, stream ); 2429 2430 /* close the attached stream */ 2431 FT_Stream_Free( stream, 2432 (FT_Bool)( parameters->stream && 2433 ( parameters->flags & FT_OPEN_STREAM ) ) ); 2434 2435 Exit: 2436 return error; 2437 } 2438 2439 2440 /* documentation is in freetype.h */ 2441 2442 FT_EXPORT_DEF( FT_Error ) 2443 FT_Reference_Face( FT_Face face ) 2444 { 2445 if ( !face ) 2446 return FT_THROW( Invalid_Face_Handle ); 2447 2448 face->internal->refcount++; 2449 2450 return FT_Err_Ok; 2451 } 2452 2453 2454 /* documentation is in freetype.h */ 2455 2456 FT_EXPORT_DEF( FT_Error ) 2457 FT_Done_Face( FT_Face face ) 2458 { 2459 FT_Error error; 2460 FT_Driver driver; 2461 FT_Memory memory; 2462 FT_ListNode node; 2463 2464 2465 error = FT_ERR( Invalid_Face_Handle ); 2466 if ( face && face->driver ) 2467 { 2468 face->internal->refcount--; 2469 if ( face->internal->refcount > 0 ) 2470 error = FT_Err_Ok; 2471 else 2472 { 2473 driver = face->driver; 2474 memory = driver->root.memory; 2475 2476 /* find face in driver's list */ 2477 node = FT_List_Find( &driver->faces_list, face ); 2478 if ( node ) 2479 { 2480 /* remove face object from the driver's list */ 2481 FT_List_Remove( &driver->faces_list, node ); 2482 FT_FREE( node ); 2483 2484 /* now destroy the object proper */ 2485 destroy_face( memory, face, driver ); 2486 error = FT_Err_Ok; 2487 } 2488 } 2489 } 2490 2491 return error; 2492 } 2493 2494 2495 /* documentation is in ftobjs.h */ 2496 2497 FT_EXPORT_DEF( FT_Error ) 2498 FT_New_Size( FT_Face face, 2499 FT_Size *asize ) 2500 { 2501 FT_Error error; 2502 FT_Memory memory; 2503 FT_Driver driver; 2504 FT_Driver_Class clazz; 2505 2506 FT_Size size = NULL; 2507 FT_ListNode node = NULL; 2508 2509 2510 if ( !face ) 2511 return FT_THROW( Invalid_Face_Handle ); 2512 2513 if ( !asize ) 2514 return FT_THROW( Invalid_Argument ); 2515 2516 if ( !face->driver ) 2517 return FT_THROW( Invalid_Driver_Handle ); 2518 2519 *asize = NULL; 2520 2521 driver = face->driver; 2522 clazz = driver->clazz; 2523 memory = face->memory; 2524 2525 /* Allocate new size object and perform basic initialisation */ 2526 if ( FT_ALLOC( size, clazz->size_object_size ) || FT_NEW( node ) ) 2527 goto Exit; 2528 2529 size->face = face; 2530 2531 /* for now, do not use any internal fields in size objects */ 2532 size->internal = NULL; 2533 2534 if ( clazz->init_size ) 2535 error = clazz->init_size( size ); 2536 2537 /* in case of success, add to the face's list */ 2538 if ( !error ) 2539 { 2540 *asize = size; 2541 node->data = size; 2542 FT_List_Add( &face->sizes_list, node ); 2543 } 2544 2545 Exit: 2546 if ( error ) 2547 { 2548 FT_FREE( node ); 2549 FT_FREE( size ); 2550 } 2551 2552 return error; 2553 } 2554 2555 2556 /* documentation is in ftobjs.h */ 2557 2558 FT_EXPORT_DEF( FT_Error ) 2559 FT_Done_Size( FT_Size size ) 2560 { 2561 FT_Error error; 2562 FT_Driver driver; 2563 FT_Memory memory; 2564 FT_Face face; 2565 FT_ListNode node; 2566 2567 2568 if ( !size ) 2569 return FT_THROW( Invalid_Size_Handle ); 2570 2571 face = size->face; 2572 if ( !face ) 2573 return FT_THROW( Invalid_Face_Handle ); 2574 2575 driver = face->driver; 2576 if ( !driver ) 2577 return FT_THROW( Invalid_Driver_Handle ); 2578 2579 memory = driver->root.memory; 2580 2581 error = FT_Err_Ok; 2582 node = FT_List_Find( &face->sizes_list, size ); 2583 if ( node ) 2584 { 2585 FT_List_Remove( &face->sizes_list, node ); 2586 FT_FREE( node ); 2587 2588 if ( face->size == size ) 2589 { 2590 face->size = NULL; 2591 if ( face->sizes_list.head ) 2592 face->size = (FT_Size)(face->sizes_list.head->data); 2593 } 2594 2595 destroy_size( memory, size, driver ); 2596 } 2597 else 2598 error = FT_THROW( Invalid_Size_Handle ); 2599 2600 return error; 2601 } 2602 2603 2604 /* documentation is in ftobjs.h */ 2605 2606 FT_BASE_DEF( FT_Error ) 2607 FT_Match_Size( FT_Face face, 2608 FT_Size_Request req, 2609 FT_Bool ignore_width, 2610 FT_ULong* size_index ) 2611 { 2612 FT_Int i; 2613 FT_Long w, h; 2614 2615 2616 if ( !FT_HAS_FIXED_SIZES( face ) ) 2617 return FT_THROW( Invalid_Face_Handle ); 2618 2619 /* FT_Bitmap_Size doesn't provide enough info... */ 2620 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL ) 2621 return FT_THROW( Unimplemented_Feature ); 2622 2623 w = FT_REQUEST_WIDTH ( req ); 2624 h = FT_REQUEST_HEIGHT( req ); 2625 2626 if ( req->width && !req->height ) 2627 h = w; 2628 else if ( !req->width && req->height ) 2629 w = h; 2630 2631 w = FT_PIX_ROUND( w ); 2632 h = FT_PIX_ROUND( h ); 2633 2634 if ( !w || !h ) 2635 return FT_THROW( Invalid_Pixel_Size ); 2636 2637 for ( i = 0; i < face->num_fixed_sizes; i++ ) 2638 { 2639 FT_Bitmap_Size* bsize = face->available_sizes + i; 2640 2641 2642 if ( h != FT_PIX_ROUND( bsize->y_ppem ) ) 2643 continue; 2644 2645 if ( w == FT_PIX_ROUND( bsize->x_ppem ) || ignore_width ) 2646 { 2647 FT_TRACE3(( "FT_Match_Size: bitmap strike %d matches\n", i )); 2648 2649 if ( size_index ) 2650 *size_index = (FT_ULong)i; 2651 2652 return FT_Err_Ok; 2653 } 2654 } 2655 2656 FT_TRACE3(( "FT_Match_Size: no matching bitmap strike\n" )); 2657 2658 return FT_THROW( Invalid_Pixel_Size ); 2659 } 2660 2661 2662 /* documentation is in ftobjs.h */ 2663 2664 FT_BASE_DEF( void ) 2665 ft_synthesize_vertical_metrics( FT_Glyph_Metrics* metrics, 2666 FT_Pos advance ) 2667 { 2668 FT_Pos height = metrics->height; 2669 2670 2671 /* compensate for glyph with bbox above/below the baseline */ 2672 if ( metrics->horiBearingY < 0 ) 2673 { 2674 if ( height < metrics->horiBearingY ) 2675 height = metrics->horiBearingY; 2676 } 2677 else if ( metrics->horiBearingY > 0 ) 2678 height -= metrics->horiBearingY; 2679 2680 /* the factor 1.2 is a heuristical value */ 2681 if ( !advance ) 2682 advance = height * 12 / 10; 2683 2684 metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2; 2685 metrics->vertBearingY = ( advance - height ) / 2; 2686 metrics->vertAdvance = advance; 2687 } 2688 2689 2690 static void 2691 ft_recompute_scaled_metrics( FT_Face face, 2692 FT_Size_Metrics* metrics ) 2693 { 2694 /* Compute root ascender, descender, test height, and max_advance */ 2695 2696 #ifdef GRID_FIT_METRICS 2697 metrics->ascender = FT_PIX_CEIL( FT_MulFix( face->ascender, 2698 metrics->y_scale ) ); 2699 2700 metrics->descender = FT_PIX_FLOOR( FT_MulFix( face->descender, 2701 metrics->y_scale ) ); 2702 2703 metrics->height = FT_PIX_ROUND( FT_MulFix( face->height, 2704 metrics->y_scale ) ); 2705 2706 metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width, 2707 metrics->x_scale ) ); 2708 #else /* !GRID_FIT_METRICS */ 2709 metrics->ascender = FT_MulFix( face->ascender, 2710 metrics->y_scale ); 2711 2712 metrics->descender = FT_MulFix( face->descender, 2713 metrics->y_scale ); 2714 2715 metrics->height = FT_MulFix( face->height, 2716 metrics->y_scale ); 2717 2718 metrics->max_advance = FT_MulFix( face->max_advance_width, 2719 metrics->x_scale ); 2720 #endif /* !GRID_FIT_METRICS */ 2721 } 2722 2723 2724 FT_BASE_DEF( void ) 2725 FT_Select_Metrics( FT_Face face, 2726 FT_ULong strike_index ) 2727 { 2728 FT_Size_Metrics* metrics; 2729 FT_Bitmap_Size* bsize; 2730 2731 2732 metrics = &face->size->metrics; 2733 bsize = face->available_sizes + strike_index; 2734 2735 metrics->x_ppem = (FT_UShort)( ( bsize->x_ppem + 32 ) >> 6 ); 2736 metrics->y_ppem = (FT_UShort)( ( bsize->y_ppem + 32 ) >> 6 ); 2737 2738 if ( FT_IS_SCALABLE( face ) ) 2739 { 2740 metrics->x_scale = FT_DivFix( bsize->x_ppem, 2741 face->units_per_EM ); 2742 metrics->y_scale = FT_DivFix( bsize->y_ppem, 2743 face->units_per_EM ); 2744 2745 ft_recompute_scaled_metrics( face, metrics ); 2746 } 2747 else 2748 { 2749 metrics->x_scale = 1L << 16; 2750 metrics->y_scale = 1L << 16; 2751 metrics->ascender = bsize->y_ppem; 2752 metrics->descender = 0; 2753 metrics->height = bsize->height << 6; 2754 metrics->max_advance = bsize->x_ppem; 2755 } 2756 2757 FT_TRACE5(( "FT_Select_Metrics:\n" )); 2758 FT_TRACE5(( " x scale: %d (%f)\n", 2759 metrics->x_scale, metrics->x_scale / 65536.0 )); 2760 FT_TRACE5(( " y scale: %d (%f)\n", 2761 metrics->y_scale, metrics->y_scale / 65536.0 )); 2762 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 )); 2763 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 )); 2764 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 2765 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 )); 2766 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem )); 2767 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem )); 2768 } 2769 2770 2771 FT_BASE_DEF( void ) 2772 FT_Request_Metrics( FT_Face face, 2773 FT_Size_Request req ) 2774 { 2775 FT_Size_Metrics* metrics; 2776 2777 2778 metrics = &face->size->metrics; 2779 2780 if ( FT_IS_SCALABLE( face ) ) 2781 { 2782 FT_Long w = 0, h = 0, scaled_w = 0, scaled_h = 0; 2783 2784 2785 switch ( req->type ) 2786 { 2787 case FT_SIZE_REQUEST_TYPE_NOMINAL: 2788 w = h = face->units_per_EM; 2789 break; 2790 2791 case FT_SIZE_REQUEST_TYPE_REAL_DIM: 2792 w = h = face->ascender - face->descender; 2793 break; 2794 2795 case FT_SIZE_REQUEST_TYPE_BBOX: 2796 w = face->bbox.xMax - face->bbox.xMin; 2797 h = face->bbox.yMax - face->bbox.yMin; 2798 break; 2799 2800 case FT_SIZE_REQUEST_TYPE_CELL: 2801 w = face->max_advance_width; 2802 h = face->ascender - face->descender; 2803 break; 2804 2805 case FT_SIZE_REQUEST_TYPE_SCALES: 2806 metrics->x_scale = (FT_Fixed)req->width; 2807 metrics->y_scale = (FT_Fixed)req->height; 2808 if ( !metrics->x_scale ) 2809 metrics->x_scale = metrics->y_scale; 2810 else if ( !metrics->y_scale ) 2811 metrics->y_scale = metrics->x_scale; 2812 goto Calculate_Ppem; 2813 2814 case FT_SIZE_REQUEST_TYPE_MAX: 2815 break; 2816 } 2817 2818 /* to be on the safe side */ 2819 if ( w < 0 ) 2820 w = -w; 2821 2822 if ( h < 0 ) 2823 h = -h; 2824 2825 scaled_w = FT_REQUEST_WIDTH ( req ); 2826 scaled_h = FT_REQUEST_HEIGHT( req ); 2827 2828 /* determine scales */ 2829 if ( req->width ) 2830 { 2831 metrics->x_scale = FT_DivFix( scaled_w, w ); 2832 2833 if ( req->height ) 2834 { 2835 metrics->y_scale = FT_DivFix( scaled_h, h ); 2836 2837 if ( req->type == FT_SIZE_REQUEST_TYPE_CELL ) 2838 { 2839 if ( metrics->y_scale > metrics->x_scale ) 2840 metrics->y_scale = metrics->x_scale; 2841 else 2842 metrics->x_scale = metrics->y_scale; 2843 } 2844 } 2845 else 2846 { 2847 metrics->y_scale = metrics->x_scale; 2848 scaled_h = FT_MulDiv( scaled_w, h, w ); 2849 } 2850 } 2851 else 2852 { 2853 metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h ); 2854 scaled_w = FT_MulDiv( scaled_h, w, h ); 2855 } 2856 2857 Calculate_Ppem: 2858 /* calculate the ppems */ 2859 if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL ) 2860 { 2861 scaled_w = FT_MulFix( face->units_per_EM, metrics->x_scale ); 2862 scaled_h = FT_MulFix( face->units_per_EM, metrics->y_scale ); 2863 } 2864 2865 metrics->x_ppem = (FT_UShort)( ( scaled_w + 32 ) >> 6 ); 2866 metrics->y_ppem = (FT_UShort)( ( scaled_h + 32 ) >> 6 ); 2867 2868 ft_recompute_scaled_metrics( face, metrics ); 2869 } 2870 else 2871 { 2872 FT_ZERO( metrics ); 2873 metrics->x_scale = 1L << 16; 2874 metrics->y_scale = 1L << 16; 2875 } 2876 2877 FT_TRACE5(( "FT_Request_Metrics:\n" )); 2878 FT_TRACE5(( " x scale: %d (%f)\n", 2879 metrics->x_scale, metrics->x_scale / 65536.0 )); 2880 FT_TRACE5(( " y scale: %d (%f)\n", 2881 metrics->y_scale, metrics->y_scale / 65536.0 )); 2882 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 )); 2883 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 )); 2884 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 2885 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 )); 2886 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem )); 2887 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem )); 2888 } 2889 2890 2891 /* documentation is in freetype.h */ 2892 2893 FT_EXPORT_DEF( FT_Error ) 2894 FT_Select_Size( FT_Face face, 2895 FT_Int strike_index ) 2896 { 2897 FT_Driver_Class clazz; 2898 2899 2900 if ( !face || !FT_HAS_FIXED_SIZES( face ) ) 2901 return FT_THROW( Invalid_Face_Handle ); 2902 2903 if ( strike_index < 0 || strike_index >= face->num_fixed_sizes ) 2904 return FT_THROW( Invalid_Argument ); 2905 2906 clazz = face->driver->clazz; 2907 2908 if ( clazz->select_size ) 2909 { 2910 FT_Error error; 2911 2912 2913 error = clazz->select_size( face->size, (FT_ULong)strike_index ); 2914 2915 #ifdef FT_DEBUG_LEVEL_TRACE 2916 { 2917 FT_Size_Metrics* metrics = &face->size->metrics; 2918 2919 2920 FT_TRACE5(( "FT_Select_Size (font driver's `select_size'):\n" )); 2921 FT_TRACE5(( " x scale: %d (%f)\n", 2922 metrics->x_scale, metrics->x_scale / 65536.0 )); 2923 FT_TRACE5(( " y scale: %d (%f)\n", 2924 metrics->y_scale, metrics->y_scale / 65536.0 )); 2925 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 )); 2926 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 )); 2927 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 2928 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 )); 2929 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem )); 2930 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem )); 2931 } 2932 #endif 2933 2934 return error; 2935 } 2936 2937 FT_Select_Metrics( face, (FT_ULong)strike_index ); 2938 2939 return FT_Err_Ok; 2940 } 2941 2942 2943 /* documentation is in freetype.h */ 2944 2945 FT_EXPORT_DEF( FT_Error ) 2946 FT_Request_Size( FT_Face face, 2947 FT_Size_Request req ) 2948 { 2949 FT_Driver_Class clazz; 2950 FT_ULong strike_index; 2951 2952 2953 if ( !face ) 2954 return FT_THROW( Invalid_Face_Handle ); 2955 2956 if ( !req || req->width < 0 || req->height < 0 || 2957 req->type >= FT_SIZE_REQUEST_TYPE_MAX ) 2958 return FT_THROW( Invalid_Argument ); 2959 2960 clazz = face->driver->clazz; 2961 2962 if ( clazz->request_size ) 2963 { 2964 FT_Error error; 2965 2966 2967 error = clazz->request_size( face->size, req ); 2968 2969 #ifdef FT_DEBUG_LEVEL_TRACE 2970 { 2971 FT_Size_Metrics* metrics = &face->size->metrics; 2972 2973 2974 FT_TRACE5(( "FT_Request_Size (font driver's `request_size'):\n" )); 2975 FT_TRACE5(( " x scale: %d (%f)\n", 2976 metrics->x_scale, metrics->x_scale / 65536.0 )); 2977 FT_TRACE5(( " y scale: %d (%f)\n", 2978 metrics->y_scale, metrics->y_scale / 65536.0 )); 2979 FT_TRACE5(( " ascender: %f\n", metrics->ascender / 64.0 )); 2980 FT_TRACE5(( " descender: %f\n", metrics->descender / 64.0 )); 2981 FT_TRACE5(( " height: %f\n", metrics->height / 64.0 )); 2982 FT_TRACE5(( " max advance: %f\n", metrics->max_advance / 64.0 )); 2983 FT_TRACE5(( " x ppem: %d\n", metrics->x_ppem )); 2984 FT_TRACE5(( " y ppem: %d\n", metrics->y_ppem )); 2985 } 2986 #endif 2987 2988 return error; 2989 } 2990 2991 /* 2992 * The reason that a driver doesn't have `request_size' defined is 2993 * either that the scaling here suffices or that the supported formats 2994 * are bitmap-only and size matching is not implemented. 2995 * 2996 * In the latter case, a simple size matching is done. 2997 */ 2998 if ( !FT_IS_SCALABLE( face ) && FT_HAS_FIXED_SIZES( face ) ) 2999 { 3000 FT_Error error; 3001 3002 3003 error = FT_Match_Size( face, req, 0, &strike_index ); 3004 if ( error ) 3005 return error; 3006 3007 return FT_Select_Size( face, (FT_Int)strike_index ); 3008 } 3009 3010 FT_Request_Metrics( face, req ); 3011 3012 return FT_Err_Ok; 3013 } 3014 3015 3016 /* documentation is in freetype.h */ 3017 3018 FT_EXPORT_DEF( FT_Error ) 3019 FT_Set_Char_Size( FT_Face face, 3020 FT_F26Dot6 char_width, 3021 FT_F26Dot6 char_height, 3022 FT_UInt horz_resolution, 3023 FT_UInt vert_resolution ) 3024 { 3025 FT_Size_RequestRec req; 3026 3027 3028 /* check of `face' delayed to `FT_Request_Size' */ 3029 3030 if ( !char_width ) 3031 char_width = char_height; 3032 else if ( !char_height ) 3033 char_height = char_width; 3034 3035 if ( !horz_resolution ) 3036 horz_resolution = vert_resolution; 3037 else if ( !vert_resolution ) 3038 vert_resolution = horz_resolution; 3039 3040 if ( char_width < 1 * 64 ) 3041 char_width = 1 * 64; 3042 if ( char_height < 1 * 64 ) 3043 char_height = 1 * 64; 3044 3045 if ( !horz_resolution ) 3046 horz_resolution = vert_resolution = 72; 3047 3048 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL; 3049 req.width = char_width; 3050 req.height = char_height; 3051 req.horiResolution = horz_resolution; 3052 req.vertResolution = vert_resolution; 3053 3054 return FT_Request_Size( face, &req ); 3055 } 3056 3057 3058 /* documentation is in freetype.h */ 3059 3060 FT_EXPORT_DEF( FT_Error ) 3061 FT_Set_Pixel_Sizes( FT_Face face, 3062 FT_UInt pixel_width, 3063 FT_UInt pixel_height ) 3064 { 3065 FT_Size_RequestRec req; 3066 3067 3068 /* check of `face' delayed to `FT_Request_Size' */ 3069 3070 if ( pixel_width == 0 ) 3071 pixel_width = pixel_height; 3072 else if ( pixel_height == 0 ) 3073 pixel_height = pixel_width; 3074 3075 if ( pixel_width < 1 ) 3076 pixel_width = 1; 3077 if ( pixel_height < 1 ) 3078 pixel_height = 1; 3079 3080 /* use `>=' to avoid potential compiler warning on 16bit platforms */ 3081 if ( pixel_width >= 0xFFFFU ) 3082 pixel_width = 0xFFFFU; 3083 if ( pixel_height >= 0xFFFFU ) 3084 pixel_height = 0xFFFFU; 3085 3086 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL; 3087 req.width = (FT_Long)( pixel_width << 6 ); 3088 req.height = (FT_Long)( pixel_height << 6 ); 3089 req.horiResolution = 0; 3090 req.vertResolution = 0; 3091 3092 return FT_Request_Size( face, &req ); 3093 } 3094 3095 3096 /* documentation is in freetype.h */ 3097 3098 FT_EXPORT_DEF( FT_Error ) 3099 FT_Get_Kerning( FT_Face face, 3100 FT_UInt left_glyph, 3101 FT_UInt right_glyph, 3102 FT_UInt kern_mode, 3103 FT_Vector *akerning ) 3104 { 3105 FT_Error error = FT_Err_Ok; 3106 FT_Driver driver; 3107 3108 3109 if ( !face ) 3110 return FT_THROW( Invalid_Face_Handle ); 3111 3112 if ( !akerning ) 3113 return FT_THROW( Invalid_Argument ); 3114 3115 driver = face->driver; 3116 3117 akerning->x = 0; 3118 akerning->y = 0; 3119 3120 if ( driver->clazz->get_kerning ) 3121 { 3122 error = driver->clazz->get_kerning( face, 3123 left_glyph, 3124 right_glyph, 3125 akerning ); 3126 if ( !error ) 3127 { 3128 if ( kern_mode != FT_KERNING_UNSCALED ) 3129 { 3130 akerning->x = FT_MulFix( akerning->x, face->size->metrics.x_scale ); 3131 akerning->y = FT_MulFix( akerning->y, face->size->metrics.y_scale ); 3132 3133 if ( kern_mode != FT_KERNING_UNFITTED ) 3134 { 3135 FT_Pos orig_x = akerning->x; 3136 FT_Pos orig_y = akerning->y; 3137 3138 3139 /* we scale down kerning values for small ppem values */ 3140 /* to avoid that rounding makes them too big. */ 3141 /* `25' has been determined heuristically. */ 3142 if ( face->size->metrics.x_ppem < 25 ) 3143 akerning->x = FT_MulDiv( orig_x, 3144 face->size->metrics.x_ppem, 25 ); 3145 if ( face->size->metrics.y_ppem < 25 ) 3146 akerning->y = FT_MulDiv( orig_y, 3147 face->size->metrics.y_ppem, 25 ); 3148 3149 akerning->x = FT_PIX_ROUND( akerning->x ); 3150 akerning->y = FT_PIX_ROUND( akerning->y ); 3151 3152 #ifdef FT_DEBUG_LEVEL_TRACE 3153 { 3154 FT_Pos orig_x_rounded = FT_PIX_ROUND( orig_x ); 3155 FT_Pos orig_y_rounded = FT_PIX_ROUND( orig_y ); 3156 3157 3158 if ( akerning->x != orig_x_rounded || 3159 akerning->y != orig_y_rounded ) 3160 FT_TRACE5(( "FT_Get_Kerning: horizontal kerning" 3161 " (%d, %d) scaled down to (%d, %d) pixels\n", 3162 orig_x_rounded / 64, orig_y_rounded / 64, 3163 akerning->x / 64, akerning->y / 64 )); 3164 } 3165 #endif 3166 } 3167 } 3168 } 3169 } 3170 3171 return error; 3172 } 3173 3174 3175 /* documentation is in freetype.h */ 3176 3177 FT_EXPORT_DEF( FT_Error ) 3178 FT_Get_Track_Kerning( FT_Face face, 3179 FT_Fixed point_size, 3180 FT_Int degree, 3181 FT_Fixed* akerning ) 3182 { 3183 FT_Service_Kerning service; 3184 FT_Error error = FT_Err_Ok; 3185 3186 3187 if ( !face ) 3188 return FT_THROW( Invalid_Face_Handle ); 3189 3190 if ( !akerning ) 3191 return FT_THROW( Invalid_Argument ); 3192 3193 FT_FACE_FIND_SERVICE( face, service, KERNING ); 3194 if ( !service ) 3195 return FT_THROW( Unimplemented_Feature ); 3196 3197 error = service->get_track( face, 3198 point_size, 3199 degree, 3200 akerning ); 3201 3202 return error; 3203 } 3204 3205 3206 /* documentation is in freetype.h */ 3207 3208 FT_EXPORT_DEF( FT_Error ) 3209 FT_Select_Charmap( FT_Face face, 3210 FT_Encoding encoding ) 3211 { 3212 FT_CharMap* cur; 3213 FT_CharMap* limit; 3214 3215 3216 if ( !face ) 3217 return FT_THROW( Invalid_Face_Handle ); 3218 3219 if ( encoding == FT_ENCODING_NONE ) 3220 return FT_THROW( Invalid_Argument ); 3221 3222 /* FT_ENCODING_UNICODE is special. We try to find the `best' Unicode */ 3223 /* charmap available, i.e., one with UCS-4 characters, if possible. */ 3224 /* */ 3225 /* This is done by find_unicode_charmap() above, to share code. */ 3226 if ( encoding == FT_ENCODING_UNICODE ) 3227 return find_unicode_charmap( face ); 3228 3229 cur = face->charmaps; 3230 if ( !cur ) 3231 return FT_THROW( Invalid_CharMap_Handle ); 3232 3233 limit = cur + face->num_charmaps; 3234 3235 for ( ; cur < limit; cur++ ) 3236 { 3237 if ( cur[0]->encoding == encoding ) 3238 { 3239 face->charmap = cur[0]; 3240 return 0; 3241 } 3242 } 3243 3244 return FT_THROW( Invalid_Argument ); 3245 } 3246 3247 3248 /* documentation is in freetype.h */ 3249 3250 FT_EXPORT_DEF( FT_Error ) 3251 FT_Set_Charmap( FT_Face face, 3252 FT_CharMap charmap ) 3253 { 3254 FT_CharMap* cur; 3255 FT_CharMap* limit; 3256 3257 3258 if ( !face ) 3259 return FT_THROW( Invalid_Face_Handle ); 3260 3261 cur = face->charmaps; 3262 if ( !cur || !charmap ) 3263 return FT_THROW( Invalid_CharMap_Handle ); 3264 3265 if ( FT_Get_CMap_Format( charmap ) == 14 ) 3266 return FT_THROW( Invalid_Argument ); 3267 3268 limit = cur + face->num_charmaps; 3269 3270 for ( ; cur < limit; cur++ ) 3271 { 3272 if ( cur[0] == charmap ) 3273 { 3274 face->charmap = cur[0]; 3275 return FT_Err_Ok; 3276 } 3277 } 3278 3279 return FT_THROW( Invalid_Argument ); 3280 } 3281 3282 3283 /* documentation is in freetype.h */ 3284 3285 FT_EXPORT_DEF( FT_Int ) 3286 FT_Get_Charmap_Index( FT_CharMap charmap ) 3287 { 3288 FT_Int i; 3289 3290 3291 if ( !charmap || !charmap->face ) 3292 return -1; 3293 3294 for ( i = 0; i < charmap->face->num_charmaps; i++ ) 3295 if ( charmap->face->charmaps[i] == charmap ) 3296 break; 3297 3298 FT_ASSERT( i < charmap->face->num_charmaps ); 3299 3300 return i; 3301 } 3302 3303 3304 static void 3305 ft_cmap_done_internal( FT_CMap cmap ) 3306 { 3307 FT_CMap_Class clazz = cmap->clazz; 3308 FT_Face face = cmap->charmap.face; 3309 FT_Memory memory = FT_FACE_MEMORY( face ); 3310 3311 3312 if ( clazz->done ) 3313 clazz->done( cmap ); 3314 3315 FT_FREE( cmap ); 3316 } 3317 3318 3319 FT_BASE_DEF( void ) 3320 FT_CMap_Done( FT_CMap cmap ) 3321 { 3322 if ( cmap ) 3323 { 3324 FT_Face face = cmap->charmap.face; 3325 FT_Memory memory = FT_FACE_MEMORY( face ); 3326 FT_Error error; 3327 FT_Int i, j; 3328 3329 3330 for ( i = 0; i < face->num_charmaps; i++ ) 3331 { 3332 if ( (FT_CMap)face->charmaps[i] == cmap ) 3333 { 3334 FT_CharMap last_charmap = face->charmaps[face->num_charmaps - 1]; 3335 3336 3337 if ( FT_RENEW_ARRAY( face->charmaps, 3338 face->num_charmaps, 3339 face->num_charmaps - 1 ) ) 3340 return; 3341 3342 /* remove it from our list of charmaps */ 3343 for ( j = i + 1; j < face->num_charmaps; j++ ) 3344 { 3345 if ( j == face->num_charmaps - 1 ) 3346 face->charmaps[j - 1] = last_charmap; 3347 else 3348 face->charmaps[j - 1] = face->charmaps[j]; 3349 } 3350 3351 face->num_charmaps--; 3352 3353 if ( (FT_CMap)face->charmap == cmap ) 3354 face->charmap = NULL; 3355 3356 ft_cmap_done_internal( cmap ); 3357 3358 break; 3359 } 3360 } 3361 } 3362 } 3363 3364 3365 FT_BASE_DEF( FT_Error ) 3366 FT_CMap_New( FT_CMap_Class clazz, 3367 FT_Pointer init_data, 3368 FT_CharMap charmap, 3369 FT_CMap *acmap ) 3370 { 3371 FT_Error error = FT_Err_Ok; 3372 FT_Face face; 3373 FT_Memory memory; 3374 FT_CMap cmap = NULL; 3375 3376 3377 if ( clazz == NULL || charmap == NULL || charmap->face == NULL ) 3378 return FT_THROW( Invalid_Argument ); 3379 3380 face = charmap->face; 3381 memory = FT_FACE_MEMORY( face ); 3382 3383 if ( !FT_ALLOC( cmap, clazz->size ) ) 3384 { 3385 cmap->charmap = *charmap; 3386 cmap->clazz = clazz; 3387 3388 if ( clazz->init ) 3389 { 3390 error = clazz->init( cmap, init_data ); 3391 if ( error ) 3392 goto Fail; 3393 } 3394 3395 /* add it to our list of charmaps */ 3396 if ( FT_RENEW_ARRAY( face->charmaps, 3397 face->num_charmaps, 3398 face->num_charmaps + 1 ) ) 3399 goto Fail; 3400 3401 face->charmaps[face->num_charmaps++] = (FT_CharMap)cmap; 3402 } 3403 3404 Exit: 3405 if ( acmap ) 3406 *acmap = cmap; 3407 3408 return error; 3409 3410 Fail: 3411 ft_cmap_done_internal( cmap ); 3412 cmap = NULL; 3413 goto Exit; 3414 } 3415 3416 3417 /* documentation is in freetype.h */ 3418 3419 FT_EXPORT_DEF( FT_UInt ) 3420 FT_Get_Char_Index( FT_Face face, 3421 FT_ULong charcode ) 3422 { 3423 FT_UInt result = 0; 3424 3425 3426 if ( face && face->charmap ) 3427 { 3428 FT_CMap cmap = FT_CMAP( face->charmap ); 3429 3430 3431 if ( charcode > 0xFFFFFFFFUL ) 3432 { 3433 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" )); 3434 FT_TRACE1(( " 0x%x is truncated\n", charcode )); 3435 } 3436 3437 result = cmap->clazz->char_index( cmap, (FT_UInt32)charcode ); 3438 if ( result >= (FT_UInt)face->num_glyphs ) 3439 result = 0; 3440 } 3441 3442 return result; 3443 } 3444 3445 3446 /* documentation is in freetype.h */ 3447 3448 FT_EXPORT_DEF( FT_ULong ) 3449 FT_Get_First_Char( FT_Face face, 3450 FT_UInt *agindex ) 3451 { 3452 FT_ULong result = 0; 3453 FT_UInt gindex = 0; 3454 3455 3456 /* only do something if we have a charmap, and we have glyphs at all */ 3457 if ( face && face->charmap && face->num_glyphs ) 3458 { 3459 gindex = FT_Get_Char_Index( face, 0 ); 3460 if ( gindex == 0 ) 3461 result = FT_Get_Next_Char( face, 0, &gindex ); 3462 } 3463 3464 if ( agindex ) 3465 *agindex = gindex; 3466 3467 return result; 3468 } 3469 3470 3471 /* documentation is in freetype.h */ 3472 3473 FT_EXPORT_DEF( FT_ULong ) 3474 FT_Get_Next_Char( FT_Face face, 3475 FT_ULong charcode, 3476 FT_UInt *agindex ) 3477 { 3478 FT_ULong result = 0; 3479 FT_UInt gindex = 0; 3480 3481 3482 if ( face && face->charmap && face->num_glyphs ) 3483 { 3484 FT_UInt32 code = (FT_UInt32)charcode; 3485 FT_CMap cmap = FT_CMAP( face->charmap ); 3486 3487 3488 do 3489 { 3490 gindex = cmap->clazz->char_next( cmap, &code ); 3491 3492 } while ( gindex >= (FT_UInt)face->num_glyphs ); 3493 3494 result = ( gindex == 0 ) ? 0 : code; 3495 } 3496 3497 if ( agindex ) 3498 *agindex = gindex; 3499 3500 return result; 3501 } 3502 3503 3504 /* documentation is in freetype.h */ 3505 3506 FT_EXPORT_DEF( FT_UInt ) 3507 FT_Face_GetCharVariantIndex( FT_Face face, 3508 FT_ULong charcode, 3509 FT_ULong variantSelector ) 3510 { 3511 FT_UInt result = 0; 3512 3513 3514 if ( face && 3515 face->charmap && 3516 face->charmap->encoding == FT_ENCODING_UNICODE ) 3517 { 3518 FT_CharMap charmap = find_variant_selector_charmap( face ); 3519 FT_CMap ucmap = FT_CMAP( face->charmap ); 3520 3521 3522 if ( charmap != NULL ) 3523 { 3524 FT_CMap vcmap = FT_CMAP( charmap ); 3525 3526 3527 if ( charcode > 0xFFFFFFFFUL ) 3528 { 3529 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" )); 3530 FT_TRACE1(( " 0x%x is truncated\n", charcode )); 3531 } 3532 if ( variantSelector > 0xFFFFFFFFUL ) 3533 { 3534 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" )); 3535 FT_TRACE1(( " 0x%x is truncated\n", variantSelector )); 3536 } 3537 3538 result = vcmap->clazz->char_var_index( vcmap, ucmap, 3539 (FT_UInt32)charcode, 3540 (FT_UInt32)variantSelector ); 3541 } 3542 } 3543 3544 return result; 3545 } 3546 3547 3548 /* documentation is in freetype.h */ 3549 3550 FT_EXPORT_DEF( FT_Int ) 3551 FT_Face_GetCharVariantIsDefault( FT_Face face, 3552 FT_ULong charcode, 3553 FT_ULong variantSelector ) 3554 { 3555 FT_Int result = -1; 3556 3557 3558 if ( face ) 3559 { 3560 FT_CharMap charmap = find_variant_selector_charmap( face ); 3561 3562 3563 if ( charmap != NULL ) 3564 { 3565 FT_CMap vcmap = FT_CMAP( charmap ); 3566 3567 3568 if ( charcode > 0xFFFFFFFFUL ) 3569 { 3570 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" )); 3571 FT_TRACE1(( " 0x%x is truncated\n", charcode )); 3572 } 3573 if ( variantSelector > 0xFFFFFFFFUL ) 3574 { 3575 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" )); 3576 FT_TRACE1(( " 0x%x is truncated\n", variantSelector )); 3577 } 3578 3579 result = vcmap->clazz->char_var_default( vcmap, 3580 (FT_UInt32)charcode, 3581 (FT_UInt32)variantSelector ); 3582 } 3583 } 3584 3585 return result; 3586 } 3587 3588 3589 /* documentation is in freetype.h */ 3590 3591 FT_EXPORT_DEF( FT_UInt32* ) 3592 FT_Face_GetVariantSelectors( FT_Face face ) 3593 { 3594 FT_UInt32 *result = NULL; 3595 3596 3597 if ( face ) 3598 { 3599 FT_CharMap charmap = find_variant_selector_charmap( face ); 3600 3601 3602 if ( charmap != NULL ) 3603 { 3604 FT_CMap vcmap = FT_CMAP( charmap ); 3605 FT_Memory memory = FT_FACE_MEMORY( face ); 3606 3607 3608 result = vcmap->clazz->variant_list( vcmap, memory ); 3609 } 3610 } 3611 3612 return result; 3613 } 3614 3615 3616 /* documentation is in freetype.h */ 3617 3618 FT_EXPORT_DEF( FT_UInt32* ) 3619 FT_Face_GetVariantsOfChar( FT_Face face, 3620 FT_ULong charcode ) 3621 { 3622 FT_UInt32 *result = NULL; 3623 3624 3625 if ( face ) 3626 { 3627 FT_CharMap charmap = find_variant_selector_charmap( face ); 3628 3629 3630 if ( charmap != NULL ) 3631 { 3632 FT_CMap vcmap = FT_CMAP( charmap ); 3633 FT_Memory memory = FT_FACE_MEMORY( face ); 3634 3635 3636 if ( charcode > 0xFFFFFFFFUL ) 3637 { 3638 FT_TRACE1(( "FT_Get_Char_Index: too large charcode" )); 3639 FT_TRACE1(( " 0x%x is truncated\n", charcode )); 3640 } 3641 3642 result = vcmap->clazz->charvariant_list( vcmap, memory, 3643 (FT_UInt32)charcode ); 3644 } 3645 } 3646 return result; 3647 } 3648 3649 3650 /* documentation is in freetype.h */ 3651 3652 FT_EXPORT_DEF( FT_UInt32* ) 3653 FT_Face_GetCharsOfVariant( FT_Face face, 3654 FT_ULong variantSelector ) 3655 { 3656 FT_UInt32 *result = NULL; 3657 3658 3659 if ( face ) 3660 { 3661 FT_CharMap charmap = find_variant_selector_charmap( face ); 3662 3663 3664 if ( charmap != NULL ) 3665 { 3666 FT_CMap vcmap = FT_CMAP( charmap ); 3667 FT_Memory memory = FT_FACE_MEMORY( face ); 3668 3669 3670 if ( variantSelector > 0xFFFFFFFFUL ) 3671 { 3672 FT_TRACE1(( "FT_Get_Char_Index: too large variantSelector" )); 3673 FT_TRACE1(( " 0x%x is truncated\n", variantSelector )); 3674 } 3675 3676 result = vcmap->clazz->variantchar_list( vcmap, memory, 3677 (FT_UInt32)variantSelector ); 3678 } 3679 } 3680 3681 return result; 3682 } 3683 3684 3685 /* documentation is in freetype.h */ 3686 3687 FT_EXPORT_DEF( FT_UInt ) 3688 FT_Get_Name_Index( FT_Face face, 3689 FT_String* glyph_name ) 3690 { 3691 FT_UInt result = 0; 3692 3693 3694 if ( face && 3695 FT_HAS_GLYPH_NAMES( face ) && 3696 glyph_name ) 3697 { 3698 FT_Service_GlyphDict service; 3699 3700 3701 FT_FACE_LOOKUP_SERVICE( face, 3702 service, 3703 GLYPH_DICT ); 3704 3705 if ( service && service->name_index ) 3706 result = service->name_index( face, glyph_name ); 3707 } 3708 3709 return result; 3710 } 3711 3712 3713 /* documentation is in freetype.h */ 3714 3715 FT_EXPORT_DEF( FT_Error ) 3716 FT_Get_Glyph_Name( FT_Face face, 3717 FT_UInt glyph_index, 3718 FT_Pointer buffer, 3719 FT_UInt buffer_max ) 3720 { 3721 FT_Error error; 3722 FT_Service_GlyphDict service; 3723 3724 3725 if ( !face ) 3726 return FT_THROW( Invalid_Face_Handle ); 3727 3728 if ( !buffer || buffer_max == 0 ) 3729 return FT_THROW( Invalid_Argument ); 3730 3731 /* clean up buffer */ 3732 ((FT_Byte*)buffer)[0] = '\0'; 3733 3734 if ( (FT_Long)glyph_index >= face->num_glyphs ) 3735 return FT_THROW( Invalid_Glyph_Index ); 3736 3737 if ( !FT_HAS_GLYPH_NAMES( face ) ) 3738 return FT_THROW( Invalid_Argument ); 3739 3740 FT_FACE_LOOKUP_SERVICE( face, service, GLYPH_DICT ); 3741 if ( service && service->get_name ) 3742 error = service->get_name( face, glyph_index, buffer, buffer_max ); 3743 else 3744 error = FT_THROW( Invalid_Argument ); 3745 3746 return error; 3747 } 3748 3749 3750 /* documentation is in freetype.h */ 3751 3752 FT_EXPORT_DEF( const char* ) 3753 FT_Get_Postscript_Name( FT_Face face ) 3754 { 3755 const char* result = NULL; 3756 3757 3758 if ( !face ) 3759 goto Exit; 3760 3761 if ( !result ) 3762 { 3763 FT_Service_PsFontName service; 3764 3765 3766 FT_FACE_LOOKUP_SERVICE( face, 3767 service, 3768 POSTSCRIPT_FONT_NAME ); 3769 3770 if ( service && service->get_ps_font_name ) 3771 result = service->get_ps_font_name( face ); 3772 } 3773 3774 Exit: 3775 return result; 3776 } 3777 3778 3779 /* documentation is in tttables.h */ 3780 3781 FT_EXPORT_DEF( void* ) 3782 FT_Get_Sfnt_Table( FT_Face face, 3783 FT_Sfnt_Tag tag ) 3784 { 3785 void* table = NULL; 3786 FT_Service_SFNT_Table service; 3787 3788 3789 if ( face && FT_IS_SFNT( face ) ) 3790 { 3791 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE ); 3792 if ( service != NULL ) 3793 table = service->get_table( face, tag ); 3794 } 3795 3796 return table; 3797 } 3798 3799 3800 /* documentation is in tttables.h */ 3801 3802 FT_EXPORT_DEF( FT_Error ) 3803 FT_Load_Sfnt_Table( FT_Face face, 3804 FT_ULong tag, 3805 FT_Long offset, 3806 FT_Byte* buffer, 3807 FT_ULong* length ) 3808 { 3809 FT_Service_SFNT_Table service; 3810 3811 3812 if ( !face || !FT_IS_SFNT( face ) ) 3813 return FT_THROW( Invalid_Face_Handle ); 3814 3815 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE ); 3816 if ( service == NULL ) 3817 return FT_THROW( Unimplemented_Feature ); 3818 3819 return service->load_table( face, tag, offset, buffer, length ); 3820 } 3821 3822 3823 /* documentation is in tttables.h */ 3824 3825 FT_EXPORT_DEF( FT_Error ) 3826 FT_Sfnt_Table_Info( FT_Face face, 3827 FT_UInt table_index, 3828 FT_ULong *tag, 3829 FT_ULong *length ) 3830 { 3831 FT_Service_SFNT_Table service; 3832 FT_ULong offset; 3833 3834 3835 /* test for valid `length' delayed to `service->table_info' */ 3836 3837 if ( !face || !FT_IS_SFNT( face ) ) 3838 return FT_THROW( Invalid_Face_Handle ); 3839 3840 FT_FACE_FIND_SERVICE( face, service, SFNT_TABLE ); 3841 if ( service == NULL ) 3842 return FT_THROW( Unimplemented_Feature ); 3843 3844 return service->table_info( face, table_index, tag, &offset, length ); 3845 } 3846 3847 3848 /* documentation is in tttables.h */ 3849 3850 FT_EXPORT_DEF( FT_ULong ) 3851 FT_Get_CMap_Language_ID( FT_CharMap charmap ) 3852 { 3853 FT_Service_TTCMaps service; 3854 FT_Face face; 3855 TT_CMapInfo cmap_info; 3856 3857 3858 if ( !charmap || !charmap->face ) 3859 return 0; 3860 3861 face = charmap->face; 3862 FT_FACE_FIND_SERVICE( face, service, TT_CMAP ); 3863 if ( service == NULL ) 3864 return 0; 3865 if ( service->get_cmap_info( charmap, &cmap_info )) 3866 return 0; 3867 3868 return cmap_info.language; 3869 } 3870 3871 3872 /* documentation is in tttables.h */ 3873 3874 FT_EXPORT_DEF( FT_Long ) 3875 FT_Get_CMap_Format( FT_CharMap charmap ) 3876 { 3877 FT_Service_TTCMaps service; 3878 FT_Face face; 3879 TT_CMapInfo cmap_info; 3880 3881 3882 if ( !charmap || !charmap->face ) 3883 return -1; 3884 3885 face = charmap->face; 3886 FT_FACE_FIND_SERVICE( face, service, TT_CMAP ); 3887 if ( service == NULL ) 3888 return -1; 3889 if ( service->get_cmap_info( charmap, &cmap_info )) 3890 return -1; 3891 3892 return cmap_info.format; 3893 } 3894 3895 3896 /* documentation is in ftsizes.h */ 3897 3898 FT_EXPORT_DEF( FT_Error ) 3899 FT_Activate_Size( FT_Size size ) 3900 { 3901 FT_Face face; 3902 3903 3904 if ( !size ) 3905 return FT_THROW( Invalid_Size_Handle ); 3906 3907 face = size->face; 3908 if ( !face || !face->driver ) 3909 return FT_THROW( Invalid_Face_Handle ); 3910 3911 /* we don't need anything more complex than that; all size objects */ 3912 /* are already listed by the face */ 3913 face->size = size; 3914 3915 return FT_Err_Ok; 3916 } 3917 3918 3919 /*************************************************************************/ 3920 /*************************************************************************/ 3921 /*************************************************************************/ 3922 /**** ****/ 3923 /**** ****/ 3924 /**** R E N D E R E R S ****/ 3925 /**** ****/ 3926 /**** ****/ 3927 /*************************************************************************/ 3928 /*************************************************************************/ 3929 /*************************************************************************/ 3930 3931 /* lookup a renderer by glyph format in the library's list */ 3932 FT_BASE_DEF( FT_Renderer ) 3933 FT_Lookup_Renderer( FT_Library library, 3934 FT_Glyph_Format format, 3935 FT_ListNode* node ) 3936 { 3937 FT_ListNode cur; 3938 FT_Renderer result = NULL; 3939 3940 3941 if ( !library ) 3942 goto Exit; 3943 3944 cur = library->renderers.head; 3945 3946 if ( node ) 3947 { 3948 if ( *node ) 3949 cur = (*node)->next; 3950 *node = NULL; 3951 } 3952 3953 while ( cur ) 3954 { 3955 FT_Renderer renderer = FT_RENDERER( cur->data ); 3956 3957 3958 if ( renderer->glyph_format == format ) 3959 { 3960 if ( node ) 3961 *node = cur; 3962 3963 result = renderer; 3964 break; 3965 } 3966 cur = cur->next; 3967 } 3968 3969 Exit: 3970 return result; 3971 } 3972 3973 3974 static FT_Renderer 3975 ft_lookup_glyph_renderer( FT_GlyphSlot slot ) 3976 { 3977 FT_Face face = slot->face; 3978 FT_Library library = FT_FACE_LIBRARY( face ); 3979 FT_Renderer result = library->cur_renderer; 3980 3981 3982 if ( !result || result->glyph_format != slot->format ) 3983 result = FT_Lookup_Renderer( library, slot->format, 0 ); 3984 3985 return result; 3986 } 3987 3988 3989 static void 3990 ft_set_current_renderer( FT_Library library ) 3991 { 3992 FT_Renderer renderer; 3993 3994 3995 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE, 0 ); 3996 library->cur_renderer = renderer; 3997 } 3998 3999 4000 static FT_Error 4001 ft_add_renderer( FT_Module module ) 4002 { 4003 FT_Library library = module->library; 4004 FT_Memory memory = library->memory; 4005 FT_Error error; 4006 FT_ListNode node = NULL; 4007 4008 4009 if ( FT_NEW( node ) ) 4010 goto Exit; 4011 4012 { 4013 FT_Renderer render = FT_RENDERER( module ); 4014 FT_Renderer_Class* clazz = (FT_Renderer_Class*)module->clazz; 4015 4016 4017 render->clazz = clazz; 4018 render->glyph_format = clazz->glyph_format; 4019 4020 /* allocate raster object if needed */ 4021 if ( clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE && 4022 clazz->raster_class->raster_new ) 4023 { 4024 error = clazz->raster_class->raster_new( memory, &render->raster ); 4025 if ( error ) 4026 goto Fail; 4027 4028 render->raster_render = clazz->raster_class->raster_render; 4029 render->render = clazz->render_glyph; 4030 } 4031 4032 /* add to list */ 4033 node->data = module; 4034 FT_List_Add( &library->renderers, node ); 4035 4036 ft_set_current_renderer( library ); 4037 } 4038 4039 Fail: 4040 if ( error ) 4041 FT_FREE( node ); 4042 4043 Exit: 4044 return error; 4045 } 4046 4047 4048 static void 4049 ft_remove_renderer( FT_Module module ) 4050 { 4051 FT_Library library; 4052 FT_Memory memory; 4053 FT_ListNode node; 4054 4055 4056 library = module->library; 4057 if ( !library ) 4058 return; 4059 4060 memory = library->memory; 4061 4062 node = FT_List_Find( &library->renderers, module ); 4063 if ( node ) 4064 { 4065 FT_Renderer render = FT_RENDERER( module ); 4066 4067 4068 /* release raster object, if any */ 4069 if ( render->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE && 4070 render->raster ) 4071 render->clazz->raster_class->raster_done( render->raster ); 4072 4073 /* remove from list */ 4074 FT_List_Remove( &library->renderers, node ); 4075 FT_FREE( node ); 4076 4077 ft_set_current_renderer( library ); 4078 } 4079 } 4080 4081 4082 /* documentation is in ftrender.h */ 4083 4084 FT_EXPORT_DEF( FT_Renderer ) 4085 FT_Get_Renderer( FT_Library library, 4086 FT_Glyph_Format format ) 4087 { 4088 /* test for valid `library' delayed to `FT_Lookup_Renderer' */ 4089 4090 return FT_Lookup_Renderer( library, format, 0 ); 4091 } 4092 4093 4094 /* documentation is in ftrender.h */ 4095 4096 FT_EXPORT_DEF( FT_Error ) 4097 FT_Set_Renderer( FT_Library library, 4098 FT_Renderer renderer, 4099 FT_UInt num_params, 4100 FT_Parameter* parameters ) 4101 { 4102 FT_ListNode node; 4103 FT_Error error = FT_Err_Ok; 4104 4105 FT_Renderer_SetModeFunc set_mode; 4106 4107 4108 if ( !library ) 4109 { 4110 error = FT_THROW( Invalid_Library_Handle ); 4111 goto Exit; 4112 } 4113 4114 if ( !renderer ) 4115 { 4116 error = FT_THROW( Invalid_Argument ); 4117 goto Exit; 4118 } 4119 4120 if ( num_params > 0 && !parameters ) 4121 { 4122 error = FT_THROW( Invalid_Argument ); 4123 goto Exit; 4124 } 4125 4126 node = FT_List_Find( &library->renderers, renderer ); 4127 if ( !node ) 4128 { 4129 error = FT_THROW( Invalid_Argument ); 4130 goto Exit; 4131 } 4132 4133 FT_List_Up( &library->renderers, node ); 4134 4135 if ( renderer->glyph_format == FT_GLYPH_FORMAT_OUTLINE ) 4136 library->cur_renderer = renderer; 4137 4138 set_mode = renderer->clazz->set_mode; 4139 4140 for ( ; num_params > 0; num_params-- ) 4141 { 4142 error = set_mode( renderer, parameters->tag, parameters->data ); 4143 if ( error ) 4144 break; 4145 parameters++; 4146 } 4147 4148 Exit: 4149 return error; 4150 } 4151 4152 4153 FT_BASE_DEF( FT_Error ) 4154 FT_Render_Glyph_Internal( FT_Library library, 4155 FT_GlyphSlot slot, 4156 FT_Render_Mode render_mode ) 4157 { 4158 FT_Error error = FT_Err_Ok; 4159 FT_Renderer renderer; 4160 4161 4162 /* if it is already a bitmap, no need to do anything */ 4163 switch ( slot->format ) 4164 { 4165 case FT_GLYPH_FORMAT_BITMAP: /* already a bitmap, don't do anything */ 4166 break; 4167 4168 default: 4169 { 4170 FT_ListNode node = NULL; 4171 4172 4173 /* small shortcut for the very common case */ 4174 if ( slot->format == FT_GLYPH_FORMAT_OUTLINE ) 4175 { 4176 renderer = library->cur_renderer; 4177 node = library->renderers.head; 4178 } 4179 else 4180 renderer = FT_Lookup_Renderer( library, slot->format, &node ); 4181 4182 error = FT_ERR( Unimplemented_Feature ); 4183 while ( renderer ) 4184 { 4185 error = renderer->render( renderer, slot, render_mode, NULL ); 4186 if ( !error || 4187 FT_ERR_NEQ( error, Cannot_Render_Glyph ) ) 4188 break; 4189 4190 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */ 4191 /* is unsupported by the current renderer for this glyph image */ 4192 /* format. */ 4193 4194 /* now, look for another renderer that supports the same */ 4195 /* format. */ 4196 renderer = FT_Lookup_Renderer( library, slot->format, &node ); 4197 } 4198 } 4199 } 4200 4201 #ifdef FT_DEBUG_LEVEL_TRACE 4202 4203 #undef FT_COMPONENT 4204 #define FT_COMPONENT trace_bitmap 4205 4206 /* 4207 * Computing the MD5 checksum is expensive, unnecessarily distorting a 4208 * possible profiling of FreeType if compiled with tracing support. For 4209 * this reason, we execute the following code only if explicitly 4210 * requested. 4211 */ 4212 4213 /* we use FT_TRACE3 in this block */ 4214 if ( ft_trace_levels[trace_bitmap] >= 3 ) 4215 { 4216 /* we convert to a single bitmap format for computing the checksum */ 4217 if ( !error ) 4218 { 4219 FT_Bitmap bitmap; 4220 FT_Error err; 4221 4222 4223 FT_Bitmap_Init( &bitmap ); 4224 4225 /* this also converts the bitmap flow to `down' (i.e., pitch > 0) */ 4226 err = FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 ); 4227 if ( !err ) 4228 { 4229 MD5_CTX ctx; 4230 unsigned char md5[16]; 4231 int i; 4232 unsigned int rows = bitmap.rows; 4233 unsigned int pitch = (unsigned int)bitmap.pitch; 4234 4235 4236 MD5_Init( &ctx ); 4237 if ( bitmap.buffer ) 4238 MD5_Update( &ctx, bitmap.buffer, rows * pitch ); 4239 MD5_Final( md5, &ctx ); 4240 4241 FT_TRACE3(( "MD5 checksum for %dx%d bitmap:\n" 4242 " ", 4243 rows, pitch )); 4244 for ( i = 0; i < 16; i++ ) 4245 FT_TRACE3(( "%02X", md5[i] )); 4246 FT_TRACE3(( "\n" )); 4247 } 4248 4249 FT_Bitmap_Done( library, &bitmap ); 4250 } 4251 } 4252 4253 #undef FT_COMPONENT 4254 #define FT_COMPONENT trace_objs 4255 4256 #endif /* FT_DEBUG_LEVEL_TRACE */ 4257 4258 return error; 4259 } 4260 4261 4262 /* documentation is in freetype.h */ 4263 4264 FT_EXPORT_DEF( FT_Error ) 4265 FT_Render_Glyph( FT_GlyphSlot slot, 4266 FT_Render_Mode render_mode ) 4267 { 4268 FT_Library library; 4269 4270 4271 if ( !slot || !slot->face ) 4272 return FT_THROW( Invalid_Argument ); 4273 4274 library = FT_FACE_LIBRARY( slot->face ); 4275 4276 return FT_Render_Glyph_Internal( library, slot, render_mode ); 4277 } 4278 4279 4280 /*************************************************************************/ 4281 /*************************************************************************/ 4282 /*************************************************************************/ 4283 /**** ****/ 4284 /**** ****/ 4285 /**** M O D U L E S ****/ 4286 /**** ****/ 4287 /**** ****/ 4288 /*************************************************************************/ 4289 /*************************************************************************/ 4290 /*************************************************************************/ 4291 4292 4293 /*************************************************************************/ 4294 /* */ 4295 /* <Function> */ 4296 /* Destroy_Module */ 4297 /* */ 4298 /* <Description> */ 4299 /* Destroys a given module object. For drivers, this also destroys */ 4300 /* all child faces. */ 4301 /* */ 4302 /* <InOut> */ 4303 /* module :: A handle to the target driver object. */ 4304 /* */ 4305 /* <Note> */ 4306 /* The driver _must_ be LOCKED! */ 4307 /* */ 4308 static void 4309 Destroy_Module( FT_Module module ) 4310 { 4311 FT_Memory memory = module->memory; 4312 FT_Module_Class* clazz = module->clazz; 4313 FT_Library library = module->library; 4314 4315 4316 if ( library && library->auto_hinter == module ) 4317 library->auto_hinter = NULL; 4318 4319 /* if the module is a renderer */ 4320 if ( FT_MODULE_IS_RENDERER( module ) ) 4321 ft_remove_renderer( module ); 4322 4323 /* if the module is a font driver, add some steps */ 4324 if ( FT_MODULE_IS_DRIVER( module ) ) 4325 Destroy_Driver( FT_DRIVER( module ) ); 4326 4327 /* finalize the module object */ 4328 if ( clazz->module_done ) 4329 clazz->module_done( module ); 4330 4331 /* discard it */ 4332 FT_FREE( module ); 4333 } 4334 4335 4336 /* documentation is in ftmodapi.h */ 4337 4338 FT_EXPORT_DEF( FT_Error ) 4339 FT_Add_Module( FT_Library library, 4340 const FT_Module_Class* clazz ) 4341 { 4342 FT_Error error; 4343 FT_Memory memory; 4344 FT_Module module = NULL; 4345 FT_UInt nn; 4346 4347 4348 #define FREETYPE_VER_FIXED ( ( (FT_Long)FREETYPE_MAJOR << 16 ) | \ 4349 FREETYPE_MINOR ) 4350 4351 if ( !library ) 4352 return FT_THROW( Invalid_Library_Handle ); 4353 4354 if ( !clazz ) 4355 return FT_THROW( Invalid_Argument ); 4356 4357 /* check freetype version */ 4358 if ( clazz->module_requires > FREETYPE_VER_FIXED ) 4359 return FT_THROW( Invalid_Version ); 4360 4361 /* look for a module with the same name in the library's table */ 4362 for ( nn = 0; nn < library->num_modules; nn++ ) 4363 { 4364 module = library->modules[nn]; 4365 if ( ft_strcmp( module->clazz->module_name, clazz->module_name ) == 0 ) 4366 { 4367 /* this installed module has the same name, compare their versions */ 4368 if ( clazz->module_version <= module->clazz->module_version ) 4369 return FT_THROW( Lower_Module_Version ); 4370 4371 /* remove the module from our list, then exit the loop to replace */ 4372 /* it by our new version.. */ 4373 FT_Remove_Module( library, module ); 4374 break; 4375 } 4376 } 4377 4378 memory = library->memory; 4379 error = FT_Err_Ok; 4380 4381 if ( library->num_modules >= FT_MAX_MODULES ) 4382 { 4383 error = FT_THROW( Too_Many_Drivers ); 4384 goto Exit; 4385 } 4386 4387 /* allocate module object */ 4388 if ( FT_ALLOC( module, clazz->module_size ) ) 4389 goto Exit; 4390 4391 /* base initialization */ 4392 module->library = library; 4393 module->memory = memory; 4394 module->clazz = (FT_Module_Class*)clazz; 4395 4396 /* check whether the module is a renderer - this must be performed */ 4397 /* before the normal module initialization */ 4398 if ( FT_MODULE_IS_RENDERER( module ) ) 4399 { 4400 /* add to the renderers list */ 4401 error = ft_add_renderer( module ); 4402 if ( error ) 4403 goto Fail; 4404 } 4405 4406 /* is the module a auto-hinter? */ 4407 if ( FT_MODULE_IS_HINTER( module ) ) 4408 library->auto_hinter = module; 4409 4410 /* if the module is a font driver */ 4411 if ( FT_MODULE_IS_DRIVER( module ) ) 4412 { 4413 FT_Driver driver = FT_DRIVER( module ); 4414 4415 4416 driver->clazz = (FT_Driver_Class)module->clazz; 4417 } 4418 4419 if ( clazz->module_init ) 4420 { 4421 error = clazz->module_init( module ); 4422 if ( error ) 4423 goto Fail; 4424 } 4425 4426 /* add module to the library's table */ 4427 library->modules[library->num_modules++] = module; 4428 4429 Exit: 4430 return error; 4431 4432 Fail: 4433 if ( FT_MODULE_IS_RENDERER( module ) ) 4434 { 4435 FT_Renderer renderer = FT_RENDERER( module ); 4436 4437 4438 if ( renderer->clazz && 4439 renderer->clazz->glyph_format == FT_GLYPH_FORMAT_OUTLINE && 4440 renderer->raster ) 4441 renderer->clazz->raster_class->raster_done( renderer->raster ); 4442 } 4443 4444 FT_FREE( module ); 4445 goto Exit; 4446 } 4447 4448 4449 /* documentation is in ftmodapi.h */ 4450 4451 FT_EXPORT_DEF( FT_Module ) 4452 FT_Get_Module( FT_Library library, 4453 const char* module_name ) 4454 { 4455 FT_Module result = NULL; 4456 FT_Module* cur; 4457 FT_Module* limit; 4458 4459 4460 if ( !library || !module_name ) 4461 return result; 4462 4463 cur = library->modules; 4464 limit = cur + library->num_modules; 4465 4466 for ( ; cur < limit; cur++ ) 4467 if ( ft_strcmp( cur[0]->clazz->module_name, module_name ) == 0 ) 4468 { 4469 result = cur[0]; 4470 break; 4471 } 4472 4473 return result; 4474 } 4475 4476 4477 /* documentation is in ftobjs.h */ 4478 4479 FT_BASE_DEF( const void* ) 4480 FT_Get_Module_Interface( FT_Library library, 4481 const char* mod_name ) 4482 { 4483 FT_Module module; 4484 4485 4486 /* test for valid `library' delayed to FT_Get_Module() */ 4487 4488 module = FT_Get_Module( library, mod_name ); 4489 4490 return module ? module->clazz->module_interface : 0; 4491 } 4492 4493 4494 FT_BASE_DEF( FT_Pointer ) 4495 ft_module_get_service( FT_Module module, 4496 const char* service_id ) 4497 { 4498 FT_Pointer result = NULL; 4499 4500 4501 if ( module ) 4502 { 4503 FT_ASSERT( module->clazz && module->clazz->get_interface ); 4504 4505 /* first, look for the service in the module */ 4506 if ( module->clazz->get_interface ) 4507 result = module->clazz->get_interface( module, service_id ); 4508 4509 if ( result == NULL ) 4510 { 4511 /* we didn't find it, look in all other modules then */ 4512 FT_Library library = module->library; 4513 FT_Module* cur = library->modules; 4514 FT_Module* limit = cur + library->num_modules; 4515 4516 4517 for ( ; cur < limit; cur++ ) 4518 { 4519 if ( cur[0] != module ) 4520 { 4521 FT_ASSERT( cur[0]->clazz ); 4522 4523 if ( cur[0]->clazz->get_interface ) 4524 { 4525 result = cur[0]->clazz->get_interface( cur[0], service_id ); 4526 if ( result != NULL ) 4527 break; 4528 } 4529 } 4530 } 4531 } 4532 } 4533 4534 return result; 4535 } 4536 4537 4538 /* documentation is in ftmodapi.h */ 4539 4540 FT_EXPORT_DEF( FT_Error ) 4541 FT_Remove_Module( FT_Library library, 4542 FT_Module module ) 4543 { 4544 /* try to find the module from the table, then remove it from there */ 4545 4546 if ( !library ) 4547 return FT_THROW( Invalid_Library_Handle ); 4548 4549 if ( module ) 4550 { 4551 FT_Module* cur = library->modules; 4552 FT_Module* limit = cur + library->num_modules; 4553 4554 4555 for ( ; cur < limit; cur++ ) 4556 { 4557 if ( cur[0] == module ) 4558 { 4559 /* remove it from the table */ 4560 library->num_modules--; 4561 limit--; 4562 while ( cur < limit ) 4563 { 4564 cur[0] = cur[1]; 4565 cur++; 4566 } 4567 limit[0] = NULL; 4568 4569 /* destroy the module */ 4570 Destroy_Module( module ); 4571 4572 return FT_Err_Ok; 4573 } 4574 } 4575 } 4576 return FT_THROW( Invalid_Driver_Handle ); 4577 } 4578 4579 4580 static FT_Error 4581 ft_property_do( FT_Library library, 4582 const FT_String* module_name, 4583 const FT_String* property_name, 4584 void* value, 4585 FT_Bool set, 4586 FT_Bool value_is_string ) 4587 { 4588 FT_Module* cur; 4589 FT_Module* limit; 4590 FT_Module_Interface interface; 4591 4592 FT_Service_Properties service; 4593 4594 #ifdef FT_DEBUG_LEVEL_ERROR 4595 const FT_String* set_name = "FT_Property_Set"; 4596 const FT_String* get_name = "FT_Property_Get"; 4597 const FT_String* func_name = set ? set_name : get_name; 4598 #endif 4599 4600 FT_Bool missing_func; 4601 4602 4603 if ( !library ) 4604 return FT_THROW( Invalid_Library_Handle ); 4605 4606 if ( !module_name || !property_name || !value ) 4607 return FT_THROW( Invalid_Argument ); 4608 4609 cur = library->modules; 4610 limit = cur + library->num_modules; 4611 4612 /* search module */ 4613 for ( ; cur < limit; cur++ ) 4614 if ( !ft_strcmp( cur[0]->clazz->module_name, module_name ) ) 4615 break; 4616 4617 if ( cur == limit ) 4618 { 4619 FT_ERROR(( "%s: can't find module `%s'\n", 4620 func_name, module_name )); 4621 return FT_THROW( Missing_Module ); 4622 } 4623 4624 /* check whether we have a service interface */ 4625 if ( !cur[0]->clazz->get_interface ) 4626 { 4627 FT_ERROR(( "%s: module `%s' doesn't support properties\n", 4628 func_name, module_name )); 4629 return FT_THROW( Unimplemented_Feature ); 4630 } 4631 4632 /* search property service */ 4633 interface = cur[0]->clazz->get_interface( cur[0], 4634 FT_SERVICE_ID_PROPERTIES ); 4635 if ( !interface ) 4636 { 4637 FT_ERROR(( "%s: module `%s' doesn't support properties\n", 4638 func_name, module_name )); 4639 return FT_THROW( Unimplemented_Feature ); 4640 } 4641 4642 service = (FT_Service_Properties)interface; 4643 4644 if ( set ) 4645 missing_func = (FT_Bool)( !service->set_property ); 4646 else 4647 missing_func = (FT_Bool)( !service->get_property ); 4648 4649 if ( missing_func ) 4650 { 4651 FT_ERROR(( "%s: property service of module `%s' is broken\n", 4652 func_name, module_name )); 4653 return FT_THROW( Unimplemented_Feature ); 4654 } 4655 4656 return set ? service->set_property( cur[0], 4657 property_name, 4658 value, 4659 value_is_string ) 4660 : service->get_property( cur[0], 4661 property_name, 4662 value ); 4663 } 4664 4665 4666 /* documentation is in ftmodapi.h */ 4667 4668 FT_EXPORT_DEF( FT_Error ) 4669 FT_Property_Set( FT_Library library, 4670 const FT_String* module_name, 4671 const FT_String* property_name, 4672 const void* value ) 4673 { 4674 return ft_property_do( library, 4675 module_name, 4676 property_name, 4677 (void*)value, 4678 TRUE, 4679 FALSE ); 4680 } 4681 4682 4683 /* documentation is in ftmodapi.h */ 4684 4685 FT_EXPORT_DEF( FT_Error ) 4686 FT_Property_Get( FT_Library library, 4687 const FT_String* module_name, 4688 const FT_String* property_name, 4689 void* value ) 4690 { 4691 return ft_property_do( library, 4692 module_name, 4693 property_name, 4694 value, 4695 FALSE, 4696 FALSE ); 4697 } 4698 4699 4700 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES 4701 4702 /* this variant is used for handling the FREETYPE_PROPERTIES */ 4703 /* environment variable */ 4704 4705 FT_BASE_DEF( FT_Error ) 4706 ft_property_string_set( FT_Library library, 4707 const FT_String* module_name, 4708 const FT_String* property_name, 4709 FT_String* value ) 4710 { 4711 return ft_property_do( library, 4712 module_name, 4713 property_name, 4714 (void*)value, 4715 TRUE, 4716 TRUE ); 4717 } 4718 4719 #endif 4720 4721 4722 /*************************************************************************/ 4723 /*************************************************************************/ 4724 /*************************************************************************/ 4725 /**** ****/ 4726 /**** ****/ 4727 /**** L I B R A R Y ****/ 4728 /**** ****/ 4729 /**** ****/ 4730 /*************************************************************************/ 4731 /*************************************************************************/ 4732 /*************************************************************************/ 4733 4734 4735 /* documentation is in ftmodapi.h */ 4736 4737 FT_EXPORT_DEF( FT_Error ) 4738 FT_Reference_Library( FT_Library library ) 4739 { 4740 if ( !library ) 4741 return FT_THROW( Invalid_Library_Handle ); 4742 4743 library->refcount++; 4744 4745 return FT_Err_Ok; 4746 } 4747 4748 4749 /* documentation is in ftmodapi.h */ 4750 4751 FT_EXPORT_DEF( FT_Error ) 4752 FT_New_Library( FT_Memory memory, 4753 FT_Library *alibrary ) 4754 { 4755 FT_Library library = NULL; 4756 FT_Error error; 4757 4758 4759 if ( !memory || !alibrary ) 4760 return FT_THROW( Invalid_Argument ); 4761 4762 #ifdef FT_DEBUG_LEVEL_ERROR 4763 /* init debugging support */ 4764 ft_debug_init(); 4765 #endif 4766 4767 /* first of all, allocate the library object */ 4768 if ( FT_NEW( library ) ) 4769 return error; 4770 4771 library->memory = memory; 4772 4773 #ifdef FT_CONFIG_OPTION_PIC 4774 /* initialize position independent code containers */ 4775 error = ft_pic_container_init( library ); 4776 if ( error ) 4777 goto Fail; 4778 #endif 4779 4780 /* we don't use raster_pool anymore. */ 4781 library->raster_pool_size = 0; 4782 library->raster_pool = NULL; 4783 4784 library->version_major = FREETYPE_MAJOR; 4785 library->version_minor = FREETYPE_MINOR; 4786 library->version_patch = FREETYPE_PATCH; 4787 4788 library->refcount = 1; 4789 4790 /* That's ok now */ 4791 *alibrary = library; 4792 4793 return FT_Err_Ok; 4794 4795 #ifdef FT_CONFIG_OPTION_PIC 4796 Fail: 4797 ft_pic_container_destroy( library ); 4798 #endif 4799 FT_FREE( library ); 4800 return error; 4801 } 4802 4803 4804 /* documentation is in freetype.h */ 4805 4806 FT_EXPORT_DEF( void ) 4807 FT_Library_Version( FT_Library library, 4808 FT_Int *amajor, 4809 FT_Int *aminor, 4810 FT_Int *apatch ) 4811 { 4812 FT_Int major = 0; 4813 FT_Int minor = 0; 4814 FT_Int patch = 0; 4815 4816 4817 if ( library ) 4818 { 4819 major = library->version_major; 4820 minor = library->version_minor; 4821 patch = library->version_patch; 4822 } 4823 4824 if ( amajor ) 4825 *amajor = major; 4826 4827 if ( aminor ) 4828 *aminor = minor; 4829 4830 if ( apatch ) 4831 *apatch = patch; 4832 } 4833 4834 4835 /* documentation is in ftmodapi.h */ 4836 4837 FT_EXPORT_DEF( FT_Error ) 4838 FT_Done_Library( FT_Library library ) 4839 { 4840 FT_Memory memory; 4841 4842 4843 if ( !library ) 4844 return FT_THROW( Invalid_Library_Handle ); 4845 4846 library->refcount--; 4847 if ( library->refcount > 0 ) 4848 goto Exit; 4849 4850 memory = library->memory; 4851 4852 /* 4853 * Close all faces in the library. If we don't do this, we can have 4854 * some subtle memory leaks. 4855 * 4856 * Example: 4857 * 4858 * - the cff font driver uses the pshinter module in cff_size_done 4859 * - if the pshinter module is destroyed before the cff font driver, 4860 * opened FT_Face objects managed by the driver are not properly 4861 * destroyed, resulting in a memory leak 4862 * 4863 * Some faces are dependent on other faces, like Type42 faces that 4864 * depend on TrueType faces synthesized internally. 4865 * 4866 * The order of drivers should be specified in driver_name[]. 4867 */ 4868 { 4869 FT_UInt m, n; 4870 const char* driver_name[] = { "type42", NULL }; 4871 4872 4873 for ( m = 0; 4874 m < sizeof ( driver_name ) / sizeof ( driver_name[0] ); 4875 m++ ) 4876 { 4877 for ( n = 0; n < library->num_modules; n++ ) 4878 { 4879 FT_Module module = library->modules[n]; 4880 const char* module_name = module->clazz->module_name; 4881 FT_List faces; 4882 4883 4884 if ( driver_name[m] && 4885 ft_strcmp( module_name, driver_name[m] ) != 0 ) 4886 continue; 4887 4888 if ( ( module->clazz->module_flags & FT_MODULE_FONT_DRIVER ) == 0 ) 4889 continue; 4890 4891 FT_TRACE7(( "FT_Done_Library: close faces for %s\n", module_name )); 4892 4893 faces = &FT_DRIVER( module )->faces_list; 4894 while ( faces->head ) 4895 { 4896 FT_Done_Face( FT_FACE( faces->head->data ) ); 4897 if ( faces->head ) 4898 FT_TRACE0(( "FT_Done_Library: failed to free some faces\n" )); 4899 } 4900 } 4901 } 4902 } 4903 4904 /* Close all other modules in the library */ 4905 #if 1 4906 /* XXX Modules are removed in the reversed order so that */ 4907 /* type42 module is removed before truetype module. This */ 4908 /* avoids double free in some occasions. It is a hack. */ 4909 while ( library->num_modules > 0 ) 4910 FT_Remove_Module( library, 4911 library->modules[library->num_modules - 1] ); 4912 #else 4913 { 4914 FT_UInt n; 4915 4916 4917 for ( n = 0; n < library->num_modules; n++ ) 4918 { 4919 FT_Module module = library->modules[n]; 4920 4921 4922 if ( module ) 4923 { 4924 Destroy_Module( module ); 4925 library->modules[n] = NULL; 4926 } 4927 } 4928 } 4929 #endif 4930 4931 #ifdef FT_CONFIG_OPTION_PIC 4932 /* Destroy pic container contents */ 4933 ft_pic_container_destroy( library ); 4934 #endif 4935 4936 FT_FREE( library ); 4937 4938 Exit: 4939 return FT_Err_Ok; 4940 } 4941 4942 4943 /* documentation is in ftmodapi.h */ 4944 4945 FT_EXPORT_DEF( void ) 4946 FT_Set_Debug_Hook( FT_Library library, 4947 FT_UInt hook_index, 4948 FT_DebugHook_Func debug_hook ) 4949 { 4950 if ( library && debug_hook && 4951 hook_index < 4952 ( sizeof ( library->debug_hooks ) / sizeof ( void* ) ) ) 4953 library->debug_hooks[hook_index] = debug_hook; 4954 } 4955 4956 4957 /* documentation is in ftmodapi.h */ 4958 4959 FT_EXPORT_DEF( FT_TrueTypeEngineType ) 4960 FT_Get_TrueType_Engine_Type( FT_Library library ) 4961 { 4962 FT_TrueTypeEngineType result = FT_TRUETYPE_ENGINE_TYPE_NONE; 4963 4964 4965 if ( library ) 4966 { 4967 FT_Module module = FT_Get_Module( library, "truetype" ); 4968 4969 4970 if ( module ) 4971 { 4972 FT_Service_TrueTypeEngine service; 4973 4974 4975 service = (FT_Service_TrueTypeEngine) 4976 ft_module_get_service( module, 4977 FT_SERVICE_ID_TRUETYPE_ENGINE ); 4978 if ( service ) 4979 result = service->engine_type; 4980 } 4981 } 4982 4983 return result; 4984 } 4985 4986 4987 /* documentation is in freetype.h */ 4988 4989 FT_EXPORT_DEF( FT_Error ) 4990 FT_Get_SubGlyph_Info( FT_GlyphSlot glyph, 4991 FT_UInt sub_index, 4992 FT_Int *p_index, 4993 FT_UInt *p_flags, 4994 FT_Int *p_arg1, 4995 FT_Int *p_arg2, 4996 FT_Matrix *p_transform ) 4997 { 4998 FT_Error error = FT_ERR( Invalid_Argument ); 4999 5000 5001 if ( glyph && 5002 glyph->subglyphs && 5003 glyph->format == FT_GLYPH_FORMAT_COMPOSITE && 5004 sub_index < glyph->num_subglyphs ) 5005 { 5006 FT_SubGlyph subg = glyph->subglyphs + sub_index; 5007 5008 5009 *p_index = subg->index; 5010 *p_flags = subg->flags; 5011 *p_arg1 = subg->arg1; 5012 *p_arg2 = subg->arg2; 5013 *p_transform = subg->transform; 5014 5015 error = FT_Err_Ok; 5016 } 5017 5018 return error; 5019 } 5020 5021 5022 /* END */ 5023