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