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