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