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