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