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