ttgload.c revision 416d87963fc96b892271f0cb176c761690e4c403
1/***************************************************************************/ 2/* */ 3/* ttgload.c */ 4/* */ 5/* TrueType Glyph Loader (body). */ 6/* */ 7/* Copyright 1996-2015 by */ 8/* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9/* */ 10/* This file is part of the FreeType project, and may only be used, */ 11/* modified, and distributed under the terms of the FreeType project */ 12/* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13/* this file you indicate that you have read the license and */ 14/* understand and accept it fully. */ 15/* */ 16/***************************************************************************/ 17 18 19#include <ft2build.h> 20#include FT_INTERNAL_DEBUG_H 21#include FT_INTERNAL_CALC_H 22#include FT_INTERNAL_STREAM_H 23#include FT_INTERNAL_SFNT_H 24#include FT_TRUETYPE_TAGS_H 25#include FT_OUTLINE_H 26#include FT_TRUETYPE_DRIVER_H 27 28#include "ttgload.h" 29#include "ttpload.h" 30 31#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 32#include "ttgxvar.h" 33#endif 34 35#include "tterrors.h" 36#include "ttsubpix.h" 37 38 39 /*************************************************************************/ 40 /* */ 41 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 42 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 43 /* messages during execution. */ 44 /* */ 45#undef FT_COMPONENT 46#define FT_COMPONENT trace_ttgload 47 48 49 /*************************************************************************/ 50 /* */ 51 /* Composite glyph flags. */ 52 /* */ 53#define ARGS_ARE_WORDS 0x0001 54#define ARGS_ARE_XY_VALUES 0x0002 55#define ROUND_XY_TO_GRID 0x0004 56#define WE_HAVE_A_SCALE 0x0008 57/* reserved 0x0010 */ 58#define MORE_COMPONENTS 0x0020 59#define WE_HAVE_AN_XY_SCALE 0x0040 60#define WE_HAVE_A_2X2 0x0080 61#define WE_HAVE_INSTR 0x0100 62#define USE_MY_METRICS 0x0200 63#define OVERLAP_COMPOUND 0x0400 64#define SCALED_COMPONENT_OFFSET 0x0800 65#define UNSCALED_COMPONENT_OFFSET 0x1000 66 67 68 /*************************************************************************/ 69 /* */ 70 /* Return the horizontal metrics in font units for a given glyph. */ 71 /* */ 72 FT_LOCAL_DEF( void ) 73 TT_Get_HMetrics( TT_Face face, 74 FT_UInt idx, 75 FT_Short* lsb, 76 FT_UShort* aw ) 77 { 78 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw ); 79 80 FT_TRACE5(( " advance width (font units): %d\n", *aw )); 81 FT_TRACE5(( " left side bearing (font units): %d\n", *lsb )); 82 } 83 84 85 /*************************************************************************/ 86 /* */ 87 /* Return the vertical metrics in font units for a given glyph. */ 88 /* See macro `TT_LOADER_SET_PP' below for explanations. */ 89 /* */ 90 FT_LOCAL_DEF( void ) 91 TT_Get_VMetrics( TT_Face face, 92 FT_UInt idx, 93 FT_Pos yMax, 94 FT_Short* tsb, 95 FT_UShort* ah ) 96 { 97 if ( face->vertical_info ) 98 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah ); 99 100 else if ( face->os2.version != 0xFFFFU ) 101 { 102 *tsb = (FT_Short)( face->os2.sTypoAscender - yMax ); 103 *ah = face->os2.sTypoAscender - face->os2.sTypoDescender; 104 } 105 106 else 107 { 108 *tsb = (FT_Short)( face->horizontal.Ascender - yMax ); 109 *ah = face->horizontal.Ascender - face->horizontal.Descender; 110 } 111 112 FT_TRACE5(( " advance height (font units): %d\n", *ah )); 113 FT_TRACE5(( " top side bearing (font units): %d\n", *tsb )); 114 } 115 116 117 static FT_Error 118 tt_get_metrics( TT_Loader loader, 119 FT_UInt glyph_index ) 120 { 121 TT_Face face = (TT_Face)loader->face; 122#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 123 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 124#endif 125 126 FT_Error error; 127 FT_Stream stream = loader->stream; 128 129 FT_Short left_bearing = 0, top_bearing = 0; 130 FT_UShort advance_width = 0, advance_height = 0; 131 132 /* we must preserve the stream position */ 133 /* (which gets altered by the metrics functions) */ 134 FT_ULong pos = FT_STREAM_POS(); 135 136 137 TT_Get_HMetrics( face, glyph_index, 138 &left_bearing, 139 &advance_width ); 140 TT_Get_VMetrics( face, glyph_index, 141 loader->bbox.yMax, 142 &top_bearing, 143 &advance_height ); 144 145 if ( FT_STREAM_SEEK( pos ) ) 146 return error; 147 148 loader->left_bearing = left_bearing; 149 loader->advance = advance_width; 150 loader->top_bearing = top_bearing; 151 loader->vadvance = advance_height; 152 153#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 154 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 155 { 156 if ( loader->exec ) 157 loader->exec->sph_tweak_flags = 0; 158 159 /* this may not be the right place for this, but it works */ 160 if ( loader->exec && loader->exec->ignore_x_mode ) 161 sph_set_tweaks( loader, glyph_index ); 162 } 163#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 164 165 if ( !loader->linear_def ) 166 { 167 loader->linear_def = 1; 168 loader->linear = advance_width; 169 } 170 171 return FT_Err_Ok; 172 } 173 174 175#ifdef FT_CONFIG_OPTION_INCREMENTAL 176 177 static void 178 tt_get_metrics_incr_overrides( TT_Loader loader, 179 FT_UInt glyph_index ) 180 { 181 TT_Face face = (TT_Face)loader->face; 182 183 FT_Short left_bearing = 0, top_bearing = 0; 184 FT_UShort advance_width = 0, advance_height = 0; 185 186 187 /* If this is an incrementally loaded font check whether there are */ 188 /* overriding metrics for this glyph. */ 189 if ( face->root.internal->incremental_interface && 190 face->root.internal->incremental_interface->funcs->get_glyph_metrics ) 191 { 192 FT_Incremental_MetricsRec metrics; 193 FT_Error error; 194 195 196 metrics.bearing_x = loader->left_bearing; 197 metrics.bearing_y = 0; 198 metrics.advance = loader->advance; 199 metrics.advance_v = 0; 200 201 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 202 face->root.internal->incremental_interface->object, 203 glyph_index, FALSE, &metrics ); 204 if ( error ) 205 goto Exit; 206 207 left_bearing = (FT_Short)metrics.bearing_x; 208 advance_width = (FT_UShort)metrics.advance; 209 210#if 0 211 212 /* GWW: Do I do the same for vertical metrics? */ 213 metrics.bearing_x = 0; 214 metrics.bearing_y = loader->top_bearing; 215 metrics.advance = loader->vadvance; 216 217 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 218 face->root.internal->incremental_interface->object, 219 glyph_index, TRUE, &metrics ); 220 if ( error ) 221 goto Exit; 222 223 top_bearing = (FT_Short)metrics.bearing_y; 224 advance_height = (FT_UShort)metrics.advance; 225 226#endif /* 0 */ 227 228 loader->left_bearing = left_bearing; 229 loader->advance = advance_width; 230 loader->top_bearing = top_bearing; 231 loader->vadvance = advance_height; 232 233 if ( !loader->linear_def ) 234 { 235 loader->linear_def = 1; 236 loader->linear = advance_width; 237 } 238 } 239 240 Exit: 241 return; 242 } 243 244#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 245 246 247 /*************************************************************************/ 248 /* */ 249 /* Translates an array of coordinates. */ 250 /* */ 251 static void 252 translate_array( FT_UInt n, 253 FT_Vector* coords, 254 FT_Pos delta_x, 255 FT_Pos delta_y ) 256 { 257 FT_UInt k; 258 259 260 if ( delta_x ) 261 for ( k = 0; k < n; k++ ) 262 coords[k].x += delta_x; 263 264 if ( delta_y ) 265 for ( k = 0; k < n; k++ ) 266 coords[k].y += delta_y; 267 } 268 269 270 /*************************************************************************/ 271 /* */ 272 /* The following functions are used by default with TrueType fonts. */ 273 /* However, they can be replaced by alternatives if we need to support */ 274 /* TrueType-compressed formats (like MicroType) in the future. */ 275 /* */ 276 /*************************************************************************/ 277 278 FT_CALLBACK_DEF( FT_Error ) 279 TT_Access_Glyph_Frame( TT_Loader loader, 280 FT_UInt glyph_index, 281 FT_ULong offset, 282 FT_UInt byte_count ) 283 { 284 FT_Error error; 285 FT_Stream stream = loader->stream; 286 287 /* for non-debug mode */ 288 FT_UNUSED( glyph_index ); 289 290 291 FT_TRACE4(( "Glyph %ld\n", glyph_index )); 292 293 /* the following line sets the `error' variable through macros! */ 294 if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) ) 295 return error; 296 297 loader->cursor = stream->cursor; 298 loader->limit = stream->limit; 299 300 return FT_Err_Ok; 301 } 302 303 304 FT_CALLBACK_DEF( void ) 305 TT_Forget_Glyph_Frame( TT_Loader loader ) 306 { 307 FT_Stream stream = loader->stream; 308 309 310 FT_FRAME_EXIT(); 311 } 312 313 314 FT_CALLBACK_DEF( FT_Error ) 315 TT_Load_Glyph_Header( TT_Loader loader ) 316 { 317 FT_Byte* p = loader->cursor; 318 FT_Byte* limit = loader->limit; 319 320 321 if ( p + 10 > limit ) 322 return FT_THROW( Invalid_Outline ); 323 324 loader->n_contours = FT_NEXT_SHORT( p ); 325 326 loader->bbox.xMin = FT_NEXT_SHORT( p ); 327 loader->bbox.yMin = FT_NEXT_SHORT( p ); 328 loader->bbox.xMax = FT_NEXT_SHORT( p ); 329 loader->bbox.yMax = FT_NEXT_SHORT( p ); 330 331 FT_TRACE5(( " # of contours: %d\n", loader->n_contours )); 332 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin, 333 loader->bbox.xMax )); 334 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin, 335 loader->bbox.yMax )); 336 loader->cursor = p; 337 338 return FT_Err_Ok; 339 } 340 341 342 FT_CALLBACK_DEF( FT_Error ) 343 TT_Load_Simple_Glyph( TT_Loader load ) 344 { 345 FT_Error error; 346 FT_Byte* p = load->cursor; 347 FT_Byte* limit = load->limit; 348 FT_GlyphLoader gloader = load->gloader; 349 FT_Int n_contours = load->n_contours; 350 FT_Outline* outline; 351 FT_UShort n_ins; 352 FT_Int n_points; 353 FT_ULong tmp; 354 355 FT_Byte *flag, *flag_limit; 356 FT_Byte c, count; 357 FT_Vector *vec, *vec_limit; 358 FT_Pos x; 359 FT_Short *cont, *cont_limit, prev_cont; 360 FT_Int xy_size = 0; 361 362 363 /* check that we can add the contours to the glyph */ 364 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours ); 365 if ( error ) 366 goto Fail; 367 368 /* reading the contours' endpoints & number of points */ 369 cont = gloader->current.outline.contours; 370 cont_limit = cont + n_contours; 371 372 /* check space for contours array + instructions count */ 373 if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit ) 374 goto Invalid_Outline; 375 376 prev_cont = FT_NEXT_SHORT( p ); 377 378 if ( n_contours > 0 ) 379 cont[0] = prev_cont; 380 381 if ( prev_cont < 0 ) 382 goto Invalid_Outline; 383 384 for ( cont++; cont < cont_limit; cont++ ) 385 { 386 cont[0] = FT_NEXT_SHORT( p ); 387 if ( cont[0] <= prev_cont ) 388 { 389 /* unordered contours: this is invalid */ 390 goto Invalid_Outline; 391 } 392 prev_cont = cont[0]; 393 } 394 395 n_points = 0; 396 if ( n_contours > 0 ) 397 { 398 n_points = cont[-1] + 1; 399 if ( n_points < 0 ) 400 goto Invalid_Outline; 401 } 402 403 /* note that we will add four phantom points later */ 404 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 ); 405 if ( error ) 406 goto Fail; 407 408 /* reading the bytecode instructions */ 409 load->glyph->control_len = 0; 410 load->glyph->control_data = 0; 411 412 if ( p + 2 > limit ) 413 goto Invalid_Outline; 414 415 n_ins = FT_NEXT_USHORT( p ); 416 417 FT_TRACE5(( " Instructions size: %u\n", n_ins )); 418 419 /* check it */ 420 if ( ( limit - p ) < n_ins ) 421 { 422 FT_TRACE0(( "TT_Load_Simple_Glyph: instruction count mismatch\n" )); 423 error = FT_THROW( Too_Many_Hints ); 424 goto Fail; 425 } 426 427#ifdef TT_USE_BYTECODE_INTERPRETER 428 429 if ( IS_HINTED( load->load_flags ) ) 430 { 431 /* we don't trust `maxSizeOfInstructions' in the `maxp' table */ 432 /* and thus update the bytecode array size by ourselves */ 433 434 tmp = load->exec->glyphSize; 435 error = Update_Max( load->exec->memory, 436 &tmp, 437 sizeof ( FT_Byte ), 438 (void*)&load->exec->glyphIns, 439 n_ins ); 440 441 load->exec->glyphSize = (FT_UShort)tmp; 442 if ( error ) 443 return error; 444 445 load->glyph->control_len = n_ins; 446 load->glyph->control_data = load->exec->glyphIns; 447 448 FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins ); 449 } 450 451#endif /* TT_USE_BYTECODE_INTERPRETER */ 452 453 p += n_ins; 454 455 outline = &gloader->current.outline; 456 457 /* reading the point tags */ 458 flag = (FT_Byte*)outline->tags; 459 flag_limit = flag + n_points; 460 461 FT_ASSERT( flag != NULL ); 462 463 while ( flag < flag_limit ) 464 { 465 if ( p + 1 > limit ) 466 goto Invalid_Outline; 467 468 *flag++ = c = FT_NEXT_BYTE( p ); 469 if ( c & 8 ) 470 { 471 if ( p + 1 > limit ) 472 goto Invalid_Outline; 473 474 count = FT_NEXT_BYTE( p ); 475 if ( flag + (FT_Int)count > flag_limit ) 476 goto Invalid_Outline; 477 478 for ( ; count > 0; count-- ) 479 *flag++ = c; 480 } 481 } 482 483 /* reading the X coordinates */ 484 485 vec = outline->points; 486 vec_limit = vec + n_points; 487 flag = (FT_Byte*)outline->tags; 488 x = 0; 489 490 if ( p + xy_size > limit ) 491 goto Invalid_Outline; 492 493 for ( ; vec < vec_limit; vec++, flag++ ) 494 { 495 FT_Pos y = 0; 496 FT_Byte f = *flag; 497 498 499 if ( f & 2 ) 500 { 501 if ( p + 1 > limit ) 502 goto Invalid_Outline; 503 504 y = (FT_Pos)FT_NEXT_BYTE( p ); 505 if ( ( f & 16 ) == 0 ) 506 y = -y; 507 } 508 else if ( ( f & 16 ) == 0 ) 509 { 510 if ( p + 2 > limit ) 511 goto Invalid_Outline; 512 513 y = (FT_Pos)FT_NEXT_SHORT( p ); 514 } 515 516 x += y; 517 vec->x = x; 518 /* the cast is for stupid compilers */ 519 *flag = (FT_Byte)( f & ~( 2 | 16 ) ); 520 } 521 522 /* reading the Y coordinates */ 523 524 vec = gloader->current.outline.points; 525 vec_limit = vec + n_points; 526 flag = (FT_Byte*)outline->tags; 527 x = 0; 528 529 for ( ; vec < vec_limit; vec++, flag++ ) 530 { 531 FT_Pos y = 0; 532 FT_Byte f = *flag; 533 534 535 if ( f & 4 ) 536 { 537 if ( p + 1 > limit ) 538 goto Invalid_Outline; 539 540 y = (FT_Pos)FT_NEXT_BYTE( p ); 541 if ( ( f & 32 ) == 0 ) 542 y = -y; 543 } 544 else if ( ( f & 32 ) == 0 ) 545 { 546 if ( p + 2 > limit ) 547 goto Invalid_Outline; 548 549 y = (FT_Pos)FT_NEXT_SHORT( p ); 550 } 551 552 x += y; 553 vec->y = x; 554 /* the cast is for stupid compilers */ 555 *flag = (FT_Byte)( f & FT_CURVE_TAG_ON ); 556 } 557 558 outline->n_points = (FT_UShort)n_points; 559 outline->n_contours = (FT_Short) n_contours; 560 561 load->cursor = p; 562 563 Fail: 564 return error; 565 566 Invalid_Outline: 567 error = FT_THROW( Invalid_Outline ); 568 goto Fail; 569 } 570 571 572 FT_CALLBACK_DEF( FT_Error ) 573 TT_Load_Composite_Glyph( TT_Loader loader ) 574 { 575 FT_Error error; 576 FT_Byte* p = loader->cursor; 577 FT_Byte* limit = loader->limit; 578 FT_GlyphLoader gloader = loader->gloader; 579 FT_SubGlyph subglyph; 580 FT_UInt num_subglyphs; 581 582 583 num_subglyphs = 0; 584 585 do 586 { 587 FT_Fixed xx, xy, yy, yx; 588 FT_UInt count; 589 590 591 /* check that we can load a new subglyph */ 592 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 ); 593 if ( error ) 594 goto Fail; 595 596 /* check space */ 597 if ( p + 4 > limit ) 598 goto Invalid_Composite; 599 600 subglyph = gloader->current.subglyphs + num_subglyphs; 601 602 subglyph->arg1 = subglyph->arg2 = 0; 603 604 subglyph->flags = FT_NEXT_USHORT( p ); 605 subglyph->index = FT_NEXT_USHORT( p ); 606 607 /* check space */ 608 count = 2; 609 if ( subglyph->flags & ARGS_ARE_WORDS ) 610 count += 2; 611 if ( subglyph->flags & WE_HAVE_A_SCALE ) 612 count += 2; 613 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 614 count += 4; 615 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 616 count += 8; 617 618 if ( p + count > limit ) 619 goto Invalid_Composite; 620 621 /* read arguments */ 622 if ( subglyph->flags & ARGS_ARE_WORDS ) 623 { 624 subglyph->arg1 = FT_NEXT_SHORT( p ); 625 subglyph->arg2 = FT_NEXT_SHORT( p ); 626 } 627 else 628 { 629 subglyph->arg1 = FT_NEXT_CHAR( p ); 630 subglyph->arg2 = FT_NEXT_CHAR( p ); 631 } 632 633 /* read transform */ 634 xx = yy = 0x10000L; 635 xy = yx = 0; 636 637 if ( subglyph->flags & WE_HAVE_A_SCALE ) 638 { 639 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 640 yy = xx; 641 } 642 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 643 { 644 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 645 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 646 } 647 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 648 { 649 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 650 yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 651 xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 652 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 653 } 654 655 subglyph->transform.xx = xx; 656 subglyph->transform.xy = xy; 657 subglyph->transform.yx = yx; 658 subglyph->transform.yy = yy; 659 660 num_subglyphs++; 661 662 } while ( subglyph->flags & MORE_COMPONENTS ); 663 664 gloader->current.num_subglyphs = num_subglyphs; 665 666#ifdef TT_USE_BYTECODE_INTERPRETER 667 668 { 669 FT_Stream stream = loader->stream; 670 671 672 /* we must undo the FT_FRAME_ENTER in order to point */ 673 /* to the composite instructions, if we find some. */ 674 /* We will process them later. */ 675 /* */ 676 loader->ins_pos = (FT_ULong)( FT_STREAM_POS() + 677 p - limit ); 678 } 679 680#endif 681 682 loader->cursor = p; 683 684 Fail: 685 return error; 686 687 Invalid_Composite: 688 error = FT_THROW( Invalid_Composite ); 689 goto Fail; 690 } 691 692 693 FT_LOCAL_DEF( void ) 694 TT_Init_Glyph_Loading( TT_Face face ) 695 { 696 face->access_glyph_frame = TT_Access_Glyph_Frame; 697 face->read_glyph_header = TT_Load_Glyph_Header; 698 face->read_simple_glyph = TT_Load_Simple_Glyph; 699 face->read_composite_glyph = TT_Load_Composite_Glyph; 700 face->forget_glyph_frame = TT_Forget_Glyph_Frame; 701 } 702 703 704 static void 705 tt_prepare_zone( TT_GlyphZone zone, 706 FT_GlyphLoad load, 707 FT_UInt start_point, 708 FT_UInt start_contour ) 709 { 710 zone->n_points = (FT_UShort)( load->outline.n_points - start_point ); 711 zone->n_contours = (FT_Short) ( load->outline.n_contours - 712 start_contour ); 713 zone->org = load->extra_points + start_point; 714 zone->cur = load->outline.points + start_point; 715 zone->orus = load->extra_points2 + start_point; 716 zone->tags = (FT_Byte*)load->outline.tags + start_point; 717 zone->contours = (FT_UShort*)load->outline.contours + start_contour; 718 zone->first_point = (FT_UShort)start_point; 719 } 720 721 722 /*************************************************************************/ 723 /* */ 724 /* <Function> */ 725 /* TT_Hint_Glyph */ 726 /* */ 727 /* <Description> */ 728 /* Hint the glyph using the zone prepared by the caller. Note that */ 729 /* the zone is supposed to include four phantom points. */ 730 /* */ 731 static FT_Error 732 TT_Hint_Glyph( TT_Loader loader, 733 FT_Bool is_composite ) 734 { 735#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 736 TT_Face face = (TT_Face)loader->face; 737 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 738#endif 739 740 TT_GlyphZone zone = &loader->zone; 741 742#ifdef TT_USE_BYTECODE_INTERPRETER 743 FT_UInt n_ins; 744#else 745 FT_UNUSED( is_composite ); 746#endif 747 748 749#ifdef TT_USE_BYTECODE_INTERPRETER 750 if ( loader->glyph->control_len > 0xFFFFL ) 751 { 752 FT_TRACE1(( "TT_Hint_Glyph: too long instructions" )); 753 FT_TRACE1(( " (0x%lx byte) is truncated\n", 754 loader->glyph->control_len )); 755 } 756 n_ins = (FT_UInt)( loader->glyph->control_len ); 757 758 /* save original point position in org */ 759 if ( n_ins > 0 ) 760 FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points ); 761 762 /* Reset graphics state. */ 763 loader->exec->GS = ((TT_Size)loader->size)->GS; 764 765 /* XXX: UNDOCUMENTED! Hinting instructions of a composite glyph */ 766 /* completely refer to the (already) hinted subglyphs. */ 767 if ( is_composite ) 768 { 769 loader->exec->metrics.x_scale = 1 << 16; 770 loader->exec->metrics.y_scale = 1 << 16; 771 772 FT_ARRAY_COPY( zone->orus, zone->cur, zone->n_points ); 773 } 774 else 775 { 776 loader->exec->metrics.x_scale = 777 ((TT_Size)loader->size)->metrics.x_scale; 778 loader->exec->metrics.y_scale = 779 ((TT_Size)loader->size)->metrics.y_scale; 780 } 781#endif 782 783 /* round phantom points */ 784 zone->cur[zone->n_points - 4].x = 785 FT_PIX_ROUND( zone->cur[zone->n_points - 4].x ); 786 zone->cur[zone->n_points - 3].x = 787 FT_PIX_ROUND( zone->cur[zone->n_points - 3].x ); 788 zone->cur[zone->n_points - 2].y = 789 FT_PIX_ROUND( zone->cur[zone->n_points - 2].y ); 790 zone->cur[zone->n_points - 1].y = 791 FT_PIX_ROUND( zone->cur[zone->n_points - 1].y ); 792 793#ifdef TT_USE_BYTECODE_INTERPRETER 794 795 if ( n_ins > 0 ) 796 { 797 FT_Error error; 798 799 FT_GlyphLoader gloader = loader->gloader; 800 FT_Outline current_outline = gloader->current.outline; 801 802 803 TT_Set_CodeRange( loader->exec, tt_coderange_glyph, 804 loader->exec->glyphIns, n_ins ); 805 806 loader->exec->is_composite = is_composite; 807 loader->exec->pts = *zone; 808 809 error = TT_Run_Context( loader->exec ); 810 if ( error && loader->exec->pedantic_hinting ) 811 return error; 812 813 /* store drop-out mode in bits 5-7; set bit 2 also as a marker */ 814 current_outline.tags[0] |= 815 ( loader->exec->GS.scan_type << 5 ) | FT_CURVE_TAG_HAS_SCANMODE; 816 } 817 818#endif 819 820 /* save glyph phantom points */ 821 loader->pp1 = zone->cur[zone->n_points - 4]; 822 loader->pp2 = zone->cur[zone->n_points - 3]; 823 loader->pp3 = zone->cur[zone->n_points - 2]; 824 loader->pp4 = zone->cur[zone->n_points - 1]; 825 826#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 827 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 828 { 829 if ( loader->exec->sph_tweak_flags & SPH_TWEAK_DEEMBOLDEN ) 830 FT_Outline_EmboldenXY( &loader->gloader->current.outline, -24, 0 ); 831 832 else if ( loader->exec->sph_tweak_flags & SPH_TWEAK_EMBOLDEN ) 833 FT_Outline_EmboldenXY( &loader->gloader->current.outline, 24, 0 ); 834 } 835#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 836 837 return FT_Err_Ok; 838 } 839 840 841 /*************************************************************************/ 842 /* */ 843 /* <Function> */ 844 /* TT_Process_Simple_Glyph */ 845 /* */ 846 /* <Description> */ 847 /* Once a simple glyph has been loaded, it needs to be processed. */ 848 /* Usually, this means scaling and hinting through bytecode */ 849 /* interpretation. */ 850 /* */ 851 static FT_Error 852 TT_Process_Simple_Glyph( TT_Loader loader ) 853 { 854 FT_GlyphLoader gloader = loader->gloader; 855 FT_Error error = FT_Err_Ok; 856 FT_Outline* outline; 857 FT_Int n_points; 858 859 860 outline = &gloader->current.outline; 861 n_points = outline->n_points; 862 863 /* set phantom points */ 864 865 outline->points[n_points ] = loader->pp1; 866 outline->points[n_points + 1] = loader->pp2; 867 outline->points[n_points + 2] = loader->pp3; 868 outline->points[n_points + 3] = loader->pp4; 869 870 outline->tags[n_points ] = 0; 871 outline->tags[n_points + 1] = 0; 872 outline->tags[n_points + 2] = 0; 873 outline->tags[n_points + 3] = 0; 874 875 n_points += 4; 876 877#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 878 879 if ( ((TT_Face)loader->face)->doblend ) 880 { 881 /* Deltas apply to the unscaled data. */ 882 FT_Vector* deltas; 883 FT_Memory memory = loader->face->memory; 884 FT_Int i; 885 886 887 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face), 888 loader->glyph_index, 889 &deltas, 890 n_points ); 891 if ( error ) 892 return error; 893 894 for ( i = 0; i < n_points; ++i ) 895 { 896 outline->points[i].x += deltas[i].x; 897 outline->points[i].y += deltas[i].y; 898 } 899 900 FT_FREE( deltas ); 901 } 902 903#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 904 905 if ( IS_HINTED( loader->load_flags ) ) 906 { 907 tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 ); 908 909 FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur, 910 loader->zone.n_points + 4 ); 911 } 912 913 { 914#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 915 TT_Face face = (TT_Face)loader->face; 916 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 917 918 FT_String* family = face->root.family_name; 919 FT_Int ppem = loader->size->metrics.x_ppem; 920 FT_String* style = face->root.style_name; 921 FT_Int x_scale_factor = 1000; 922#endif 923 924 FT_Vector* vec = outline->points; 925 FT_Vector* limit = outline->points + n_points; 926 927 FT_Fixed x_scale = 0; /* pacify compiler */ 928 FT_Fixed y_scale = 0; 929 930 FT_Bool do_scale = FALSE; 931 932 933#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 934 935 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 936 { 937 /* scale, but only if enabled and only if TT hinting is being used */ 938 if ( IS_HINTED( loader->load_flags ) ) 939 x_scale_factor = sph_test_tweak_x_scaling( face, 940 family, 941 ppem, 942 style, 943 loader->glyph_index ); 944 /* scale the glyph */ 945 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 || 946 x_scale_factor != 1000 ) 947 { 948 x_scale = FT_MulDiv( ((TT_Size)loader->size)->metrics.x_scale, 949 x_scale_factor, 1000 ); 950 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 951 952 /* compensate for any scaling by de/emboldening; */ 953 /* the amount was determined via experimentation */ 954 if ( x_scale_factor != 1000 && ppem > 11 ) 955 FT_Outline_EmboldenXY( outline, 956 FT_MulFix( 1280 * ppem, 957 1000 - x_scale_factor ), 958 0 ); 959 do_scale = TRUE; 960 } 961 } 962 else 963 964#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 965 966 { 967 /* scale the glyph */ 968 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 969 { 970 x_scale = ((TT_Size)loader->size)->metrics.x_scale; 971 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 972 973 do_scale = TRUE; 974 } 975 } 976 977 if ( do_scale ) 978 { 979 for ( ; vec < limit; vec++ ) 980 { 981 vec->x = FT_MulFix( vec->x, x_scale ); 982 vec->y = FT_MulFix( vec->y, y_scale ); 983 } 984 985 loader->pp1 = outline->points[n_points - 4]; 986 loader->pp2 = outline->points[n_points - 3]; 987 loader->pp3 = outline->points[n_points - 2]; 988 loader->pp4 = outline->points[n_points - 1]; 989 } 990 } 991 992 if ( IS_HINTED( loader->load_flags ) ) 993 { 994 loader->zone.n_points += 4; 995 996 error = TT_Hint_Glyph( loader, 0 ); 997 } 998 999 return error; 1000 } 1001 1002 1003 /*************************************************************************/ 1004 /* */ 1005 /* <Function> */ 1006 /* TT_Process_Composite_Component */ 1007 /* */ 1008 /* <Description> */ 1009 /* Once a composite component has been loaded, it needs to be */ 1010 /* processed. Usually, this means transforming and translating. */ 1011 /* */ 1012 static FT_Error 1013 TT_Process_Composite_Component( TT_Loader loader, 1014 FT_SubGlyph subglyph, 1015 FT_UInt start_point, 1016 FT_UInt num_base_points ) 1017 { 1018 FT_GlyphLoader gloader = loader->gloader; 1019 FT_Vector* base_vec = gloader->base.outline.points; 1020 FT_UInt num_points = gloader->base.outline.n_points; 1021 FT_Bool have_scale; 1022 FT_Pos x, y; 1023 1024 1025 have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE | 1026 WE_HAVE_AN_XY_SCALE | 1027 WE_HAVE_A_2X2 ) ); 1028 1029 /* perform the transform required for this subglyph */ 1030 if ( have_scale ) 1031 { 1032 FT_UInt i; 1033 1034 1035 for ( i = num_base_points; i < num_points; i++ ) 1036 FT_Vector_Transform( base_vec + i, &subglyph->transform ); 1037 } 1038 1039 /* get offset */ 1040 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) ) 1041 { 1042 FT_UInt k = subglyph->arg1; 1043 FT_UInt l = subglyph->arg2; 1044 FT_Vector* p1; 1045 FT_Vector* p2; 1046 1047 1048 /* match l-th point of the newly loaded component to the k-th point */ 1049 /* of the previously loaded components. */ 1050 1051 /* change to the point numbers used by our outline */ 1052 k += start_point; 1053 l += num_base_points; 1054 if ( k >= num_base_points || 1055 l >= num_points ) 1056 return FT_THROW( Invalid_Composite ); 1057 1058 p1 = gloader->base.outline.points + k; 1059 p2 = gloader->base.outline.points + l; 1060 1061 x = p1->x - p2->x; 1062 y = p1->y - p2->y; 1063 } 1064 else 1065 { 1066 x = subglyph->arg1; 1067 y = subglyph->arg2; 1068 1069 if ( !x && !y ) 1070 return FT_Err_Ok; 1071 1072 /* Use a default value dependent on */ 1073 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old */ 1074 /* TT fonts which don't set the xxx_COMPONENT_OFFSET bit. */ 1075 1076 if ( have_scale && 1077#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED 1078 !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) ) 1079#else 1080 ( subglyph->flags & SCALED_COMPONENT_OFFSET ) ) 1081#endif 1082 { 1083 1084#if 0 1085 1086 /*******************************************************************/ 1087 /* */ 1088 /* This algorithm is what Apple documents. But it doesn't work. */ 1089 /* */ 1090 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx 1091 : -subglyph->transform.xx; 1092 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx 1093 : -subglyph->transform.yx; 1094 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy 1095 : -subglyph->transform.xy; 1096 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy 1097 : -subglyph->transform.yy; 1098 int m = a > b ? a : b; 1099 int n = c > d ? c : d; 1100 1101 1102 if ( a - b <= 33 && a - b >= -33 ) 1103 m *= 2; 1104 if ( c - d <= 33 && c - d >= -33 ) 1105 n *= 2; 1106 x = FT_MulFix( x, m ); 1107 y = FT_MulFix( y, n ); 1108 1109#else /* 1 */ 1110 1111 /*******************************************************************/ 1112 /* */ 1113 /* This algorithm is a guess and works much better than the above. */ 1114 /* */ 1115 FT_Fixed mac_xscale = FT_Hypot( subglyph->transform.xx, 1116 subglyph->transform.xy ); 1117 FT_Fixed mac_yscale = FT_Hypot( subglyph->transform.yy, 1118 subglyph->transform.yx ); 1119 1120 1121 x = FT_MulFix( x, mac_xscale ); 1122 y = FT_MulFix( y, mac_yscale ); 1123 1124#endif /* 1 */ 1125 1126 } 1127 1128 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1129 { 1130 FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale; 1131 FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale; 1132 1133 1134 x = FT_MulFix( x, x_scale ); 1135 y = FT_MulFix( y, y_scale ); 1136 1137 if ( subglyph->flags & ROUND_XY_TO_GRID ) 1138 { 1139 x = FT_PIX_ROUND( x ); 1140 y = FT_PIX_ROUND( y ); 1141 } 1142 } 1143 } 1144 1145 if ( x || y ) 1146 translate_array( num_points - num_base_points, 1147 base_vec + num_base_points, 1148 x, y ); 1149 1150 return FT_Err_Ok; 1151 } 1152 1153 1154 /*************************************************************************/ 1155 /* */ 1156 /* <Function> */ 1157 /* TT_Process_Composite_Glyph */ 1158 /* */ 1159 /* <Description> */ 1160 /* This is slightly different from TT_Process_Simple_Glyph, in that */ 1161 /* its sole purpose is to hint the glyph. Thus this function is */ 1162 /* only available when bytecode interpreter is enabled. */ 1163 /* */ 1164 static FT_Error 1165 TT_Process_Composite_Glyph( TT_Loader loader, 1166 FT_UInt start_point, 1167 FT_UInt start_contour ) 1168 { 1169 FT_Error error; 1170 FT_Outline* outline; 1171 FT_UInt i; 1172 1173 1174 outline = &loader->gloader->base.outline; 1175 1176 /* make room for phantom points */ 1177 error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader, 1178 outline->n_points + 4, 1179 0 ); 1180 if ( error ) 1181 return error; 1182 1183 outline->points[outline->n_points ] = loader->pp1; 1184 outline->points[outline->n_points + 1] = loader->pp2; 1185 outline->points[outline->n_points + 2] = loader->pp3; 1186 outline->points[outline->n_points + 3] = loader->pp4; 1187 1188 outline->tags[outline->n_points ] = 0; 1189 outline->tags[outline->n_points + 1] = 0; 1190 outline->tags[outline->n_points + 2] = 0; 1191 outline->tags[outline->n_points + 3] = 0; 1192 1193#ifdef TT_USE_BYTECODE_INTERPRETER 1194 1195 { 1196 FT_Stream stream = loader->stream; 1197 FT_UShort n_ins, max_ins; 1198 FT_ULong tmp; 1199 1200 1201 /* TT_Load_Composite_Glyph only gives us the offset of instructions */ 1202 /* so we read them here */ 1203 if ( FT_STREAM_SEEK( loader->ins_pos ) || 1204 FT_READ_USHORT( n_ins ) ) 1205 return error; 1206 1207 FT_TRACE5(( " Instructions size = %d\n", n_ins )); 1208 1209 /* check it */ 1210 max_ins = ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions; 1211 if ( n_ins > max_ins ) 1212 { 1213 /* don't trust `maxSizeOfInstructions'; */ 1214 /* only do a rough safety check */ 1215 if ( (FT_Int)n_ins > loader->byte_len ) 1216 { 1217 FT_TRACE1(( "TT_Process_Composite_Glyph:" 1218 " too many instructions (%d) for glyph with length %d\n", 1219 n_ins, loader->byte_len )); 1220 return FT_THROW( Too_Many_Hints ); 1221 } 1222 1223 tmp = loader->exec->glyphSize; 1224 error = Update_Max( loader->exec->memory, 1225 &tmp, 1226 sizeof ( FT_Byte ), 1227 (void*)&loader->exec->glyphIns, 1228 n_ins ); 1229 1230 loader->exec->glyphSize = (FT_UShort)tmp; 1231 if ( error ) 1232 return error; 1233 } 1234 else if ( n_ins == 0 ) 1235 return FT_Err_Ok; 1236 1237 if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) ) 1238 return error; 1239 1240 loader->glyph->control_data = loader->exec->glyphIns; 1241 loader->glyph->control_len = n_ins; 1242 } 1243 1244#endif 1245 1246 tt_prepare_zone( &loader->zone, &loader->gloader->base, 1247 start_point, start_contour ); 1248 1249 /* Some points are likely touched during execution of */ 1250 /* instructions on components. So let's untouch them. */ 1251 for ( i = 0; i < loader->zone.n_points; i++ ) 1252 loader->zone.tags[i] &= ~FT_CURVE_TAG_TOUCH_BOTH; 1253 1254 loader->zone.n_points += 4; 1255 1256 return TT_Hint_Glyph( loader, 1 ); 1257 } 1258 1259 1260 /* 1261 * Calculate the phantom points 1262 * 1263 * Defining the right side bearing (rsb) as 1264 * 1265 * rsb = aw - (lsb + xmax - xmin) 1266 * 1267 * (with `aw' the advance width, `lsb' the left side bearing, and `xmin' 1268 * and `xmax' the glyph's minimum and maximum x value), the OpenType 1269 * specification defines the initial position of horizontal phantom points 1270 * as 1271 * 1272 * pp1 = (round(xmin - lsb), 0) , 1273 * pp2 = (round(pp1 + aw), 0) . 1274 * 1275 * Note that the rounding to the grid (in the device space) is not 1276 * documented currently in the specification. 1277 * 1278 * However, the specification lacks the precise definition of vertical 1279 * phantom points. Greg Hitchcock provided the following explanation. 1280 * 1281 * - a `vmtx' table is present 1282 * 1283 * For any glyph, the minimum and maximum y values (`ymin' and `ymax') 1284 * are given in the `glyf' table, the top side bearing (tsb) and advance 1285 * height (ah) are given in the `vmtx' table. The bottom side bearing 1286 * (bsb) is then calculated as 1287 * 1288 * bsb = ah - (tsb + ymax - ymin) , 1289 * 1290 * and the initial position of vertical phantom points is 1291 * 1292 * pp3 = (x, round(ymax + tsb)) , 1293 * pp4 = (x, round(pp3 - ah)) . 1294 * 1295 * See below for value `x'. 1296 * 1297 * - no `vmtx' table in the font 1298 * 1299 * If there is an `OS/2' table, we set 1300 * 1301 * DefaultAscender = sTypoAscender , 1302 * DefaultDescender = sTypoDescender , 1303 * 1304 * otherwise we use data from the `hhea' table: 1305 * 1306 * DefaultAscender = Ascender , 1307 * DefaultDescender = Descender . 1308 * 1309 * With these two variables we can now set 1310 * 1311 * ah = DefaultAscender - sDefaultDescender , 1312 * tsb = DefaultAscender - yMax , 1313 * 1314 * and proceed as if a `vmtx' table was present. 1315 * 1316 * Usually we have 1317 * 1318 * x = aw / 2 , (1) 1319 * 1320 * but there is one compatibility case where it can be set to 1321 * 1322 * x = -DefaultDescender - 1323 * ((DefaultAscender - DefaultDescender - aw) / 2) . (2) 1324 * 1325 * and another one with 1326 * 1327 * x = 0 . (3) 1328 * 1329 * In Windows, the history of those values is quite complicated, 1330 * depending on the hinting engine (that is, the graphics framework). 1331 * 1332 * framework from to formula 1333 * ---------------------------------------------------------- 1334 * GDI Windows 98 current (1) 1335 * (Windows 2000 for NT) 1336 * GDI+ Windows XP Windows 7 (2) 1337 * GDI+ Windows 8 current (3) 1338 * DWrite Windows 7 current (3) 1339 * 1340 * For simplicity, FreeType uses (1) for grayscale subpixel hinting and 1341 * (3) for everything else. 1342 * 1343 */ 1344#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1345 1346#define TT_LOADER_SET_PP( loader ) \ 1347 do \ 1348 { \ 1349 FT_Bool subpixel_ = loader->exec ? loader->exec->subpixel \ 1350 : 0; \ 1351 FT_Bool grayscale_ = loader->exec ? loader->exec->grayscale \ 1352 : 0; \ 1353 FT_Bool use_aw_2_ = (FT_Bool)( subpixel_ && grayscale_ ); \ 1354 \ 1355 \ 1356 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \ 1357 (loader)->pp1.y = 0; \ 1358 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \ 1359 (loader)->pp2.y = 0; \ 1360 \ 1361 (loader)->pp3.x = use_aw_2_ ? (loader)->advance / 2 : 0; \ 1362 (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \ 1363 (loader)->pp4.x = use_aw_2_ ? (loader)->advance / 2 : 0; \ 1364 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \ 1365 } while ( 0 ) 1366 1367#else /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 1368 1369#define TT_LOADER_SET_PP( loader ) \ 1370 do \ 1371 { \ 1372 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \ 1373 (loader)->pp1.y = 0; \ 1374 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \ 1375 (loader)->pp2.y = 0; \ 1376 \ 1377 (loader)->pp3.x = 0; \ 1378 (loader)->pp3.y = (loader)->bbox.yMax + (loader)->top_bearing; \ 1379 (loader)->pp4.x = 0; \ 1380 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \ 1381 } while ( 0 ) 1382 1383#endif /* !TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 1384 1385 1386 /*************************************************************************/ 1387 /* */ 1388 /* <Function> */ 1389 /* load_truetype_glyph */ 1390 /* */ 1391 /* <Description> */ 1392 /* Loads a given truetype glyph. Handles composites and uses a */ 1393 /* TT_Loader object. */ 1394 /* */ 1395 static FT_Error 1396 load_truetype_glyph( TT_Loader loader, 1397 FT_UInt glyph_index, 1398 FT_UInt recurse_count, 1399 FT_Bool header_only ) 1400 { 1401 FT_Error error = FT_Err_Ok; 1402 FT_Fixed x_scale, y_scale; 1403 FT_ULong offset; 1404 TT_Face face = (TT_Face)loader->face; 1405 FT_GlyphLoader gloader = loader->gloader; 1406 FT_Bool opened_frame = 0; 1407 1408#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1409 FT_Vector* deltas = NULL; 1410#endif 1411 1412#ifdef FT_CONFIG_OPTION_INCREMENTAL 1413 FT_StreamRec inc_stream; 1414 FT_Data glyph_data; 1415 FT_Bool glyph_data_loaded = 0; 1416#endif 1417 1418 1419 /* some fonts have an incorrect value of `maxComponentDepth', */ 1420 /* thus we allow depth 1 to catch the majority of them */ 1421 if ( recurse_count > 1 && 1422 recurse_count > face->max_profile.maxComponentDepth ) 1423 { 1424 error = FT_THROW( Invalid_Composite ); 1425 goto Exit; 1426 } 1427 1428#ifndef FT_CONFIG_OPTION_INCREMENTAL 1429 /* check glyph index */ 1430 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 1431 { 1432 error = FT_THROW( Invalid_Glyph_Index ); 1433 goto Exit; 1434 } 1435#endif 1436 1437 loader->glyph_index = glyph_index; 1438 1439 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1440 { 1441 x_scale = ((TT_Size)loader->size)->metrics.x_scale; 1442 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 1443 } 1444 else 1445 { 1446 x_scale = 0x10000L; 1447 y_scale = 0x10000L; 1448 } 1449 1450 /* Set `offset' to the start of the glyph relative to the start of */ 1451 /* the `glyf' table, and `byte_len' to the length of the glyph in */ 1452 /* bytes. */ 1453 1454#ifdef FT_CONFIG_OPTION_INCREMENTAL 1455 1456 /* If we are loading glyph data via the incremental interface, set */ 1457 /* the loader stream to a memory stream reading the data returned */ 1458 /* by the interface. */ 1459 if ( face->root.internal->incremental_interface ) 1460 { 1461 error = face->root.internal->incremental_interface->funcs->get_glyph_data( 1462 face->root.internal->incremental_interface->object, 1463 glyph_index, &glyph_data ); 1464 if ( error ) 1465 goto Exit; 1466 1467 glyph_data_loaded = 1; 1468 offset = 0; 1469 loader->byte_len = glyph_data.length; 1470 1471 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) ); 1472 FT_Stream_OpenMemory( &inc_stream, 1473 glyph_data.pointer, glyph_data.length ); 1474 1475 loader->stream = &inc_stream; 1476 } 1477 else 1478 1479#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1480 1481 offset = tt_face_get_location( face, glyph_index, 1482 (FT_UInt*)&loader->byte_len ); 1483 1484 if ( loader->byte_len > 0 ) 1485 { 1486#ifdef FT_CONFIG_OPTION_INCREMENTAL 1487 /* for the incremental interface, `glyf_offset' is always zero */ 1488 if ( !loader->glyf_offset && 1489 !face->root.internal->incremental_interface ) 1490#else 1491 if ( !loader->glyf_offset ) 1492#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1493 { 1494 FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" )); 1495 error = FT_THROW( Invalid_Table ); 1496 goto Exit; 1497 } 1498 1499 error = face->access_glyph_frame( loader, glyph_index, 1500 loader->glyf_offset + offset, 1501 loader->byte_len ); 1502 if ( error ) 1503 goto Exit; 1504 1505 opened_frame = 1; 1506 1507 /* read glyph header first */ 1508 error = face->read_glyph_header( loader ); 1509 if ( error ) 1510 goto Exit; 1511 1512 /* the metrics must be computed after loading the glyph header */ 1513 /* since we need the glyph's `yMax' value in case the vertical */ 1514 /* metrics must be emulated */ 1515 error = tt_get_metrics( loader, glyph_index ); 1516 if ( error ) 1517 goto Exit; 1518 1519 if ( header_only ) 1520 goto Exit; 1521 } 1522 1523 if ( loader->byte_len == 0 || loader->n_contours == 0 ) 1524 { 1525 loader->bbox.xMin = 0; 1526 loader->bbox.xMax = 0; 1527 loader->bbox.yMin = 0; 1528 loader->bbox.yMax = 0; 1529 1530 error = tt_get_metrics( loader, glyph_index ); 1531 if ( error ) 1532 goto Exit; 1533 1534 if ( header_only ) 1535 goto Exit; 1536 1537 /* must initialize points before (possibly) overriding */ 1538 /* glyph metrics from the incremental interface */ 1539 TT_LOADER_SET_PP( loader ); 1540 1541#ifdef FT_CONFIG_OPTION_INCREMENTAL 1542 tt_get_metrics_incr_overrides( loader, glyph_index ); 1543#endif 1544 1545#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1546 1547 if ( ((TT_Face)(loader->face))->doblend ) 1548 { 1549 /* this must be done before scaling */ 1550 FT_Memory memory = loader->face->memory; 1551 1552 1553 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face), 1554 glyph_index, &deltas, 4 ); 1555 if ( error ) 1556 goto Exit; 1557 1558 loader->pp1.x += deltas[0].x; 1559 loader->pp1.y += deltas[0].y; 1560 loader->pp2.x += deltas[1].x; 1561 loader->pp2.y += deltas[1].y; 1562 1563 loader->pp3.x += deltas[2].x; 1564 loader->pp3.y += deltas[2].y; 1565 loader->pp4.x += deltas[3].x; 1566 loader->pp4.y += deltas[3].y; 1567 1568 FT_FREE( deltas ); 1569 } 1570 1571#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1572 1573 /* scale phantom points, if necessary; */ 1574 /* they get rounded in `TT_Hint_Glyph' */ 1575 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1576 { 1577 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1578 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1579 /* pp1.y and pp2.y are always zero */ 1580 1581 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale ); 1582 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1583 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale ); 1584 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1585 } 1586 1587 error = FT_Err_Ok; 1588 goto Exit; 1589 } 1590 1591 /* must initialize phantom points before (possibly) overriding */ 1592 /* glyph metrics from the incremental interface */ 1593 TT_LOADER_SET_PP( loader ); 1594 1595#ifdef FT_CONFIG_OPTION_INCREMENTAL 1596 tt_get_metrics_incr_overrides( loader, glyph_index ); 1597#endif 1598 1599 /***********************************************************************/ 1600 /***********************************************************************/ 1601 /***********************************************************************/ 1602 1603 /* if it is a simple glyph, load it */ 1604 1605 if ( loader->n_contours > 0 ) 1606 { 1607 error = face->read_simple_glyph( loader ); 1608 if ( error ) 1609 goto Exit; 1610 1611 /* all data have been read */ 1612 face->forget_glyph_frame( loader ); 1613 opened_frame = 0; 1614 1615 error = TT_Process_Simple_Glyph( loader ); 1616 if ( error ) 1617 goto Exit; 1618 1619 FT_GlyphLoader_Add( gloader ); 1620 } 1621 1622 /***********************************************************************/ 1623 /***********************************************************************/ 1624 /***********************************************************************/ 1625 1626 /* otherwise, load a composite! */ 1627 else if ( loader->n_contours == -1 ) 1628 { 1629 FT_UInt start_point; 1630 FT_UInt start_contour; 1631 FT_ULong ins_pos; /* position of composite instructions, if any */ 1632 1633 1634 start_point = gloader->base.outline.n_points; 1635 start_contour = gloader->base.outline.n_contours; 1636 1637 /* for each subglyph, read composite header */ 1638 error = face->read_composite_glyph( loader ); 1639 if ( error ) 1640 goto Exit; 1641 1642 /* store the offset of instructions */ 1643 ins_pos = loader->ins_pos; 1644 1645 /* all data we need are read */ 1646 face->forget_glyph_frame( loader ); 1647 opened_frame = 0; 1648 1649#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1650 1651 if ( face->doblend ) 1652 { 1653 FT_Int i, limit; 1654 FT_SubGlyph subglyph; 1655 FT_Memory memory = face->root.memory; 1656 1657 1658 /* this provides additional offsets */ 1659 /* for each component's translation */ 1660 1661 if ( ( error = TT_Vary_Get_Glyph_Deltas( 1662 face, 1663 glyph_index, 1664 &deltas, 1665 gloader->current.num_subglyphs + 4 ) ) != 0 ) 1666 goto Exit; 1667 1668 subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs; 1669 limit = gloader->current.num_subglyphs; 1670 1671 for ( i = 0; i < limit; ++i, ++subglyph ) 1672 { 1673 if ( subglyph->flags & ARGS_ARE_XY_VALUES ) 1674 { 1675 /* XXX: overflow check for subglyph->{arg1,arg2}. */ 1676 /* deltas[i].{x,y} must be within signed 16-bit, */ 1677 /* but the restriction of summed delta is not clear */ 1678 subglyph->arg1 += (FT_Int16)deltas[i].x; 1679 subglyph->arg2 += (FT_Int16)deltas[i].y; 1680 } 1681 } 1682 1683 loader->pp1.x += deltas[i + 0].x; 1684 loader->pp1.y += deltas[i + 0].y; 1685 loader->pp2.x += deltas[i + 1].x; 1686 loader->pp2.y += deltas[i + 1].y; 1687 1688 loader->pp3.x += deltas[i + 2].x; 1689 loader->pp3.y += deltas[i + 2].y; 1690 loader->pp4.x += deltas[i + 3].x; 1691 loader->pp4.y += deltas[i + 3].y; 1692 1693 FT_FREE( deltas ); 1694 } 1695 1696#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1697 1698 /* scale phantom points, if necessary; */ 1699 /* they get rounded in `TT_Hint_Glyph' */ 1700 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1701 { 1702 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1703 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1704 /* pp1.y and pp2.y are always zero */ 1705 1706 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale ); 1707 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1708 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale ); 1709 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1710 } 1711 1712 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ 1713 /* `as is' in the glyph slot (the client application will be */ 1714 /* responsible for interpreting these data)... */ 1715 if ( loader->load_flags & FT_LOAD_NO_RECURSE ) 1716 { 1717 FT_GlyphLoader_Add( gloader ); 1718 loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE; 1719 1720 goto Exit; 1721 } 1722 1723 /*********************************************************************/ 1724 /*********************************************************************/ 1725 /*********************************************************************/ 1726 1727 { 1728 FT_UInt n, num_base_points; 1729 FT_SubGlyph subglyph = 0; 1730 1731 FT_UInt num_points = start_point; 1732 FT_UInt num_subglyphs = gloader->current.num_subglyphs; 1733 FT_UInt num_base_subgs = gloader->base.num_subglyphs; 1734 1735 FT_Stream old_stream = loader->stream; 1736 FT_Int old_byte_len = loader->byte_len; 1737 1738 1739 FT_GlyphLoader_Add( gloader ); 1740 1741 /* read each subglyph independently */ 1742 for ( n = 0; n < num_subglyphs; n++ ) 1743 { 1744 FT_Vector pp[4]; 1745 1746 1747 /* Each time we call load_truetype_glyph in this loop, the */ 1748 /* value of `gloader.base.subglyphs' can change due to table */ 1749 /* reallocations. We thus need to recompute the subglyph */ 1750 /* pointer on each iteration. */ 1751 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1752 1753 pp[0] = loader->pp1; 1754 pp[1] = loader->pp2; 1755 pp[2] = loader->pp3; 1756 pp[3] = loader->pp4; 1757 1758 num_base_points = gloader->base.outline.n_points; 1759 1760 error = load_truetype_glyph( loader, subglyph->index, 1761 recurse_count + 1, FALSE ); 1762 if ( error ) 1763 goto Exit; 1764 1765 /* restore subglyph pointer */ 1766 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1767 1768 /* restore phantom points if necessary */ 1769 if ( !( subglyph->flags & USE_MY_METRICS ) ) 1770 { 1771 loader->pp1 = pp[0]; 1772 loader->pp2 = pp[1]; 1773 loader->pp3 = pp[2]; 1774 loader->pp4 = pp[3]; 1775 } 1776 1777 num_points = gloader->base.outline.n_points; 1778 1779 if ( num_points == num_base_points ) 1780 continue; 1781 1782 /* gloader->base.outline consists of three parts: */ 1783 /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */ 1784 /* */ 1785 /* (1): exists from the beginning */ 1786 /* (2): components that have been loaded so far */ 1787 /* (3): the newly loaded component */ 1788 error = TT_Process_Composite_Component( loader, 1789 subglyph, 1790 start_point, 1791 num_base_points ); 1792 if ( error ) 1793 goto Exit; 1794 } 1795 1796 loader->stream = old_stream; 1797 loader->byte_len = old_byte_len; 1798 1799 /* process the glyph */ 1800 loader->ins_pos = ins_pos; 1801 if ( IS_HINTED( loader->load_flags ) && 1802#ifdef TT_USE_BYTECODE_INTERPRETER 1803 subglyph->flags & WE_HAVE_INSTR && 1804#endif 1805 num_points > start_point ) 1806 { 1807 error = TT_Process_Composite_Glyph( loader, 1808 start_point, 1809 start_contour ); 1810 if ( error ) 1811 goto Exit; 1812 } 1813 } 1814 } 1815 else 1816 { 1817 /* invalid composite count (negative but not -1) */ 1818 error = FT_THROW( Invalid_Outline ); 1819 goto Exit; 1820 } 1821 1822 /***********************************************************************/ 1823 /***********************************************************************/ 1824 /***********************************************************************/ 1825 1826 Exit: 1827 1828 if ( opened_frame ) 1829 face->forget_glyph_frame( loader ); 1830 1831#ifdef FT_CONFIG_OPTION_INCREMENTAL 1832 1833 if ( glyph_data_loaded ) 1834 face->root.internal->incremental_interface->funcs->free_glyph_data( 1835 face->root.internal->incremental_interface->object, 1836 &glyph_data ); 1837 1838#endif 1839 1840 return error; 1841 } 1842 1843 1844 static FT_Error 1845 compute_glyph_metrics( TT_Loader loader, 1846 FT_UInt glyph_index ) 1847 { 1848 TT_Face face = (TT_Face)loader->face; 1849#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1850 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 1851#endif 1852 1853 FT_BBox bbox; 1854 FT_Fixed y_scale; 1855 TT_GlyphSlot glyph = loader->glyph; 1856 TT_Size size = (TT_Size)loader->size; 1857 1858 1859 y_scale = 0x10000L; 1860 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1861 y_scale = size->root.metrics.y_scale; 1862 1863 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE ) 1864 FT_Outline_Get_CBox( &glyph->outline, &bbox ); 1865 else 1866 bbox = loader->bbox; 1867 1868 /* get the device-independent horizontal advance; it is scaled later */ 1869 /* by the base layer. */ 1870 glyph->linearHoriAdvance = loader->linear; 1871 1872 glyph->metrics.horiBearingX = bbox.xMin; 1873 glyph->metrics.horiBearingY = bbox.yMax; 1874 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; 1875 1876 /* adjust advance width to the value contained in the hdmx table */ 1877 if ( !face->postscript.isFixedPitch && 1878 IS_HINTED( loader->load_flags ) ) 1879 { 1880 FT_Byte* widthp; 1881 1882 1883 widthp = tt_face_get_device_metrics( face, 1884 size->root.metrics.x_ppem, 1885 glyph_index ); 1886 1887#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1888 1889 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 1890 { 1891 FT_Bool ignore_x_mode; 1892 1893 1894 ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) != 1895 FT_RENDER_MODE_MONO ); 1896 1897 if ( widthp && 1898 ( ( ignore_x_mode && loader->exec->compatible_widths ) || 1899 !ignore_x_mode || 1900 SPH_OPTION_BITMAP_WIDTHS ) ) 1901 glyph->metrics.horiAdvance = *widthp << 6; 1902 } 1903 else 1904 1905#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 1906 1907 { 1908 if ( widthp ) 1909 glyph->metrics.horiAdvance = *widthp << 6; 1910 } 1911 } 1912 1913 /* set glyph dimensions */ 1914 glyph->metrics.width = bbox.xMax - bbox.xMin; 1915 glyph->metrics.height = bbox.yMax - bbox.yMin; 1916 1917 /* Now take care of vertical metrics. In the case where there is */ 1918 /* no vertical information within the font (relatively common), */ 1919 /* create some metrics manually */ 1920 { 1921 FT_Pos top; /* scaled vertical top side bearing */ 1922 FT_Pos advance; /* scaled vertical advance height */ 1923 1924 1925 /* Get the unscaled top bearing and advance height. */ 1926 if ( face->vertical_info && 1927 face->vertical.number_Of_VMetrics > 0 ) 1928 { 1929 top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax, 1930 y_scale ); 1931 1932 if ( loader->pp3.y <= loader->pp4.y ) 1933 advance = 0; 1934 else 1935 advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y, 1936 y_scale ); 1937 } 1938 else 1939 { 1940 FT_Pos height; 1941 1942 1943 /* XXX Compute top side bearing and advance height in */ 1944 /* Get_VMetrics instead of here. */ 1945 1946 /* NOTE: The OS/2 values are the only `portable' ones, */ 1947 /* which is why we use them, if there is an OS/2 */ 1948 /* table in the font. Otherwise, we use the */ 1949 /* values defined in the horizontal header. */ 1950 1951 height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin, 1952 y_scale ); 1953 if ( face->os2.version != 0xFFFFU ) 1954 advance = (FT_Pos)( face->os2.sTypoAscender - 1955 face->os2.sTypoDescender ); 1956 else 1957 advance = (FT_Pos)( face->horizontal.Ascender - 1958 face->horizontal.Descender ); 1959 1960 top = ( advance - height ) / 2; 1961 } 1962 1963#ifdef FT_CONFIG_OPTION_INCREMENTAL 1964 { 1965 FT_Incremental_InterfaceRec* incr; 1966 FT_Incremental_MetricsRec metrics; 1967 FT_Error error; 1968 1969 1970 incr = face->root.internal->incremental_interface; 1971 1972 /* If this is an incrementally loaded font see if there are */ 1973 /* overriding metrics for this glyph. */ 1974 if ( incr && incr->funcs->get_glyph_metrics ) 1975 { 1976 metrics.bearing_x = 0; 1977 metrics.bearing_y = top; 1978 metrics.advance = advance; 1979 1980 error = incr->funcs->get_glyph_metrics( incr->object, 1981 glyph_index, 1982 TRUE, 1983 &metrics ); 1984 if ( error ) 1985 return error; 1986 1987 top = metrics.bearing_y; 1988 advance = metrics.advance; 1989 } 1990 } 1991 1992 /* GWW: Do vertical metrics get loaded incrementally too? */ 1993 1994#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1995 1996 glyph->linearVertAdvance = advance; 1997 1998 /* scale the metrics */ 1999 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 2000 { 2001 top = FT_MulFix( top, y_scale ); 2002 advance = FT_MulFix( advance, y_scale ); 2003 } 2004 2005 /* XXX: for now, we have no better algorithm for the lsb, but it */ 2006 /* should work fine. */ 2007 /* */ 2008 glyph->metrics.vertBearingX = glyph->metrics.horiBearingX - 2009 glyph->metrics.horiAdvance / 2; 2010 glyph->metrics.vertBearingY = top; 2011 glyph->metrics.vertAdvance = advance; 2012 } 2013 2014 return 0; 2015 } 2016 2017 2018#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 2019 2020 static FT_Error 2021 load_sbit_image( TT_Size size, 2022 TT_GlyphSlot glyph, 2023 FT_UInt glyph_index, 2024 FT_Int32 load_flags ) 2025 { 2026 TT_Face face; 2027 SFNT_Service sfnt; 2028 FT_Stream stream; 2029 FT_Error error; 2030 TT_SBit_MetricsRec metrics; 2031 2032 2033 face = (TT_Face)glyph->face; 2034 sfnt = (SFNT_Service)face->sfnt; 2035 stream = face->root.stream; 2036 2037 error = sfnt->load_sbit_image( face, 2038 size->strike_index, 2039 glyph_index, 2040 (FT_Int)load_flags, 2041 stream, 2042 &glyph->bitmap, 2043 &metrics ); 2044 if ( !error ) 2045 { 2046 glyph->outline.n_points = 0; 2047 glyph->outline.n_contours = 0; 2048 2049 glyph->metrics.width = (FT_Pos)metrics.width << 6; 2050 glyph->metrics.height = (FT_Pos)metrics.height << 6; 2051 2052 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; 2053 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; 2054 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; 2055 2056 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; 2057 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; 2058 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; 2059 2060 glyph->format = FT_GLYPH_FORMAT_BITMAP; 2061 2062 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 2063 { 2064 glyph->bitmap_left = metrics.vertBearingX; 2065 glyph->bitmap_top = metrics.vertBearingY; 2066 } 2067 else 2068 { 2069 glyph->bitmap_left = metrics.horiBearingX; 2070 glyph->bitmap_top = metrics.horiBearingY; 2071 } 2072 } 2073 2074 return error; 2075 } 2076 2077#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 2078 2079 2080 static FT_Error 2081 tt_loader_init( TT_Loader loader, 2082 TT_Size size, 2083 TT_GlyphSlot glyph, 2084 FT_Int32 load_flags, 2085 FT_Bool glyf_table_only ) 2086 { 2087 FT_Error error; 2088 2089 TT_Face face; 2090 FT_Stream stream; 2091#ifdef TT_USE_BYTECODE_INTERPRETER 2092 FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 2093#endif 2094 2095 2096 face = (TT_Face)glyph->face; 2097 stream = face->root.stream; 2098 2099 FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) ); 2100 2101#ifdef TT_USE_BYTECODE_INTERPRETER 2102 2103 /* load execution context */ 2104 if ( IS_HINTED( load_flags ) && !glyf_table_only ) 2105 { 2106 TT_ExecContext exec; 2107 FT_Bool grayscale; 2108 2109#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2110 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 2111 2112 FT_Bool subpixel = FALSE; 2113 2114#if 0 2115 /* not used yet */ 2116 FT_Bool compatible_widths; 2117 FT_Bool symmetrical_smoothing; 2118 FT_Bool bgr; 2119 FT_Bool subpixel_positioned; 2120#endif 2121#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2122 2123 FT_Bool reexecute = FALSE; 2124 2125 2126 if ( size->bytecode_ready < 0 || size->cvt_ready < 0 ) 2127 { 2128 error = tt_size_ready_bytecode( size, pedantic ); 2129 if ( error ) 2130 return error; 2131 } 2132 else if ( size->bytecode_ready ) 2133 return size->bytecode_ready; 2134 else if ( size->cvt_ready ) 2135 return size->cvt_ready; 2136 2137 /* query new execution context */ 2138 exec = size->context; 2139 if ( !exec ) 2140 return FT_THROW( Could_Not_Find_Context ); 2141 2142#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2143 2144 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2145 { 2146 subpixel = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags ) != 2147 FT_RENDER_MODE_MONO ) && 2148 SPH_OPTION_SET_SUBPIXEL ); 2149 2150 if ( subpixel ) 2151 grayscale = FALSE; 2152 else if ( SPH_OPTION_SET_GRAYSCALE ) 2153 { 2154 grayscale = TRUE; 2155 subpixel = FALSE; 2156 } 2157 else 2158 grayscale = FALSE; 2159 2160 if ( FT_IS_TRICKY( glyph->face ) ) 2161 subpixel = FALSE; 2162 2163 exec->ignore_x_mode = subpixel || grayscale; 2164 exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION; 2165 if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 ) 2166 exec->rasterizer_version = TT_INTERPRETER_VERSION_35; 2167 2168#if 1 2169 exec->compatible_widths = SPH_OPTION_SET_COMPATIBLE_WIDTHS; 2170 exec->symmetrical_smoothing = FALSE; 2171 exec->bgr = FALSE; 2172 exec->subpixel_positioned = TRUE; 2173#else /* 0 */ 2174 exec->compatible_widths = 2175 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2176 TT_LOAD_COMPATIBLE_WIDTHS ); 2177 exec->symmetrical_smoothing = 2178 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2179 TT_LOAD_SYMMETRICAL_SMOOTHING ); 2180 exec->bgr = 2181 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2182 TT_LOAD_BGR ); 2183 exec->subpixel_positioned = 2184 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2185 TT_LOAD_SUBPIXEL_POSITIONED ); 2186#endif /* 0 */ 2187 2188 } 2189 else 2190 2191#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2192 2193 { 2194 grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2195 FT_RENDER_MODE_MONO ); 2196 } 2197 2198 error = TT_Load_Context( exec, face, size ); 2199 if ( error ) 2200 return error; 2201 2202#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2203 2204 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2205 { 2206 /* a change from mono to subpixel rendering (and vice versa) */ 2207 /* requires a re-execution of the CVT program */ 2208 if ( subpixel != exec->subpixel ) 2209 { 2210 FT_TRACE4(( "tt_loader_init: subpixel hinting change," 2211 " re-executing `prep' table\n" )); 2212 2213 exec->subpixel = subpixel; 2214 reexecute = TRUE; 2215 } 2216 2217 /* a change from mono to grayscale rendering (and vice versa) */ 2218 /* requires a re-execution of the CVT program */ 2219 if ( grayscale != exec->grayscale ) 2220 { 2221 FT_TRACE4(( "tt_loader_init: grayscale hinting change," 2222 " re-executing `prep' table\n" )); 2223 2224 exec->grayscale = grayscale; 2225 reexecute = TRUE; 2226 } 2227 } 2228 else 2229 2230#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2231 2232 { 2233 /* a change from mono to grayscale rendering (and vice versa) */ 2234 /* requires a re-execution of the CVT program */ 2235 if ( grayscale != exec->grayscale ) 2236 { 2237 FT_TRACE4(( "tt_loader_init: grayscale change," 2238 " re-executing `prep' table\n" )); 2239 2240 exec->grayscale = grayscale; 2241 reexecute = TRUE; 2242 } 2243 } 2244 2245 if ( reexecute ) 2246 { 2247 FT_UInt i; 2248 2249 2250 for ( i = 0; i < size->cvt_size; i++ ) 2251 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale ); 2252 error = tt_size_run_prep( size, pedantic ); 2253 if ( error ) 2254 return error; 2255 } 2256 2257 /* see whether the cvt program has disabled hinting */ 2258 if ( exec->GS.instruct_control & 1 ) 2259 load_flags |= FT_LOAD_NO_HINTING; 2260 2261 /* load default graphics state -- if needed */ 2262 if ( exec->GS.instruct_control & 2 ) 2263 exec->GS = tt_default_graphics_state; 2264 2265 exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 2266 loader->exec = exec; 2267 loader->instructions = exec->glyphIns; 2268 } 2269 2270#endif /* TT_USE_BYTECODE_INTERPRETER */ 2271 2272 /* seek to the beginning of the glyph table -- for Type 42 fonts */ 2273 /* the table might be accessed from a Postscript stream or something */ 2274 /* else... */ 2275 2276#ifdef FT_CONFIG_OPTION_INCREMENTAL 2277 2278 if ( face->root.internal->incremental_interface ) 2279 loader->glyf_offset = 0; 2280 else 2281 2282#endif 2283 2284 { 2285 error = face->goto_table( face, TTAG_glyf, stream, 0 ); 2286 2287 if ( FT_ERR_EQ( error, Table_Missing ) ) 2288 loader->glyf_offset = 0; 2289 else if ( error ) 2290 { 2291 FT_ERROR(( "tt_loader_init: could not access glyph table\n" )); 2292 return error; 2293 } 2294 else 2295 loader->glyf_offset = FT_STREAM_POS(); 2296 } 2297 2298 /* get face's glyph loader */ 2299 if ( !glyf_table_only ) 2300 { 2301 FT_GlyphLoader gloader = glyph->internal->loader; 2302 2303 2304 FT_GlyphLoader_Rewind( gloader ); 2305 loader->gloader = gloader; 2306 } 2307 2308 loader->load_flags = load_flags; 2309 2310 loader->face = (FT_Face)face; 2311 loader->size = (FT_Size)size; 2312 loader->glyph = (FT_GlyphSlot)glyph; 2313 loader->stream = stream; 2314 2315 return FT_Err_Ok; 2316 } 2317 2318 2319 /*************************************************************************/ 2320 /* */ 2321 /* <Function> */ 2322 /* TT_Load_Glyph */ 2323 /* */ 2324 /* <Description> */ 2325 /* A function used to load a single glyph within a given glyph slot, */ 2326 /* for a given size. */ 2327 /* */ 2328 /* <Input> */ 2329 /* glyph :: A handle to a target slot object where the glyph */ 2330 /* will be loaded. */ 2331 /* */ 2332 /* size :: A handle to the source face size at which the glyph */ 2333 /* must be scaled/loaded. */ 2334 /* */ 2335 /* glyph_index :: The index of the glyph in the font file. */ 2336 /* */ 2337 /* load_flags :: A flag indicating what to load for this glyph. The */ 2338 /* FT_LOAD_XXX constants can be used to control the */ 2339 /* glyph loading process (e.g., whether the outline */ 2340 /* should be scaled, whether to load bitmaps or not, */ 2341 /* whether to hint the outline, etc). */ 2342 /* */ 2343 /* <Return> */ 2344 /* FreeType error code. 0 means success. */ 2345 /* */ 2346 FT_LOCAL_DEF( FT_Error ) 2347 TT_Load_Glyph( TT_Size size, 2348 TT_GlyphSlot glyph, 2349 FT_UInt glyph_index, 2350 FT_Int32 load_flags ) 2351 { 2352 FT_Error error; 2353 TT_LoaderRec loader; 2354 2355 2356 FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index )); 2357 2358#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 2359 2360 /* try to load embedded bitmap if any */ 2361 /* */ 2362 /* XXX: The convention should be emphasized in */ 2363 /* the documents because it can be confusing. */ 2364 if ( size->strike_index != 0xFFFFFFFFUL && 2365 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) 2366 { 2367 error = load_sbit_image( size, glyph, glyph_index, load_flags ); 2368 if ( !error ) 2369 { 2370 if ( FT_IS_SCALABLE( glyph->face ) ) 2371 { 2372 /* for the bbox we need the header only */ 2373 (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE ); 2374 (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE ); 2375 glyph->linearHoriAdvance = loader.linear; 2376 glyph->linearVertAdvance = loader.vadvance; 2377 2378 /* sanity checks: if `xxxAdvance' in the sbit metric */ 2379 /* structure isn't set, use `linearXXXAdvance' */ 2380 if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance ) 2381 glyph->metrics.horiAdvance = 2382 FT_MulFix( glyph->linearHoriAdvance, 2383 size->root.metrics.x_scale ); 2384 if ( !glyph->metrics.vertAdvance && glyph->linearVertAdvance ) 2385 glyph->metrics.vertAdvance = 2386 FT_MulFix( glyph->linearVertAdvance, 2387 size->root.metrics.y_scale ); 2388 } 2389 2390 return FT_Err_Ok; 2391 } 2392 } 2393 2394#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 2395 2396 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */ 2397 if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid ) 2398 return FT_THROW( Invalid_Size_Handle ); 2399 2400 if ( load_flags & FT_LOAD_SBITS_ONLY ) 2401 return FT_THROW( Invalid_Argument ); 2402 2403 error = tt_loader_init( &loader, size, glyph, load_flags, FALSE ); 2404 if ( error ) 2405 return error; 2406 2407 glyph->format = FT_GLYPH_FORMAT_OUTLINE; 2408 glyph->num_subglyphs = 0; 2409 glyph->outline.flags = 0; 2410 2411 /* main loading loop */ 2412 error = load_truetype_glyph( &loader, glyph_index, 0, FALSE ); 2413 if ( !error ) 2414 { 2415 if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE ) 2416 { 2417 glyph->num_subglyphs = loader.gloader->base.num_subglyphs; 2418 glyph->subglyphs = loader.gloader->base.subglyphs; 2419 } 2420 else 2421 { 2422 glyph->outline = loader.gloader->base.outline; 2423 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS; 2424 2425 /* Translate array so that (0,0) is the glyph's origin. Note */ 2426 /* that this behaviour is independent on the value of bit 1 of */ 2427 /* the `flags' field in the `head' table -- at least major */ 2428 /* applications like Acroread indicate that. */ 2429 if ( loader.pp1.x ) 2430 FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 ); 2431 } 2432 2433#ifdef TT_USE_BYTECODE_INTERPRETER 2434 2435 if ( IS_HINTED( load_flags ) ) 2436 { 2437 if ( loader.exec->GS.scan_control ) 2438 { 2439 /* convert scan conversion mode to FT_OUTLINE_XXX flags */ 2440 switch ( loader.exec->GS.scan_type ) 2441 { 2442 case 0: /* simple drop-outs including stubs */ 2443 glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS; 2444 break; 2445 case 1: /* simple drop-outs excluding stubs */ 2446 /* nothing; it's the default rendering mode */ 2447 break; 2448 case 4: /* smart drop-outs including stubs */ 2449 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS | 2450 FT_OUTLINE_INCLUDE_STUBS; 2451 break; 2452 case 5: /* smart drop-outs excluding stubs */ 2453 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS; 2454 break; 2455 2456 default: /* no drop-out control */ 2457 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS; 2458 break; 2459 } 2460 } 2461 else 2462 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS; 2463 } 2464 2465#endif /* TT_USE_BYTECODE_INTERPRETER */ 2466 2467 error = compute_glyph_metrics( &loader, glyph_index ); 2468 } 2469 2470 /* Set the `high precision' bit flag. */ 2471 /* This is _critical_ to get correct output for monochrome */ 2472 /* TrueType glyphs at all sizes using the bytecode interpreter. */ 2473 /* */ 2474 if ( !( load_flags & FT_LOAD_NO_SCALE ) && 2475 size->root.metrics.y_ppem < 24 ) 2476 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 2477 2478 return error; 2479 } 2480 2481 2482/* END */ 2483