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