ttgload.c revision a7d8bdbcfeb65b4859fe553df9d4922627837888
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 /* a utility function to retrieve i-th node from given FT_List */ 1373 static FT_ListNode 1374 ft_list_get_node_at( FT_List list, 1375 FT_UInt index ) 1376 { 1377 FT_ListNode cur; 1378 1379 1380 if ( !list ) 1381 return NULL; 1382 1383 for ( cur = list->head; cur; cur = cur->next ) 1384 { 1385 if ( !index ) 1386 return cur; 1387 1388 index --; 1389 } 1390 1391 return NULL; 1392 } 1393 1394 1395 /*************************************************************************/ 1396 /* */ 1397 /* <Function> */ 1398 /* load_truetype_glyph */ 1399 /* */ 1400 /* <Description> */ 1401 /* Loads a given truetype glyph. Handles composites and uses a */ 1402 /* TT_Loader object. */ 1403 /* */ 1404 static FT_Error 1405 load_truetype_glyph( TT_Loader loader, 1406 FT_UInt glyph_index, 1407 FT_UInt recurse_count, 1408 FT_Bool header_only ) 1409 { 1410 FT_Error error = FT_Err_Ok; 1411 FT_Fixed x_scale, y_scale; 1412 FT_ULong offset; 1413 TT_Face face = loader->face; 1414 FT_GlyphLoader gloader = loader->gloader; 1415 FT_Bool opened_frame = 0; 1416 1417#ifdef FT_CONFIG_OPTION_INCREMENTAL 1418 FT_StreamRec inc_stream; 1419 FT_Data glyph_data; 1420 FT_Bool glyph_data_loaded = 0; 1421#endif 1422 1423 1424#ifdef FT_DEBUG_LEVEL_TRACE 1425 if ( recurse_count ) 1426 FT_TRACE5(( " nesting level: %d\n", recurse_count )); 1427#endif 1428 1429 /* some fonts have an incorrect value of `maxComponentDepth', */ 1430 /* thus we allow depth 1 to catch the majority of them */ 1431 if ( recurse_count > 1 && 1432 recurse_count > face->max_profile.maxComponentDepth ) 1433 { 1434 error = FT_THROW( Invalid_Composite ); 1435 goto Exit; 1436 } 1437 1438#ifndef FT_CONFIG_OPTION_INCREMENTAL 1439 /* check glyph index */ 1440 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 1441 { 1442 error = FT_THROW( Invalid_Glyph_Index ); 1443 goto Exit; 1444 } 1445#endif 1446 1447 loader->glyph_index = glyph_index; 1448 1449 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1450 { 1451 x_scale = loader->size->metrics.x_scale; 1452 y_scale = loader->size->metrics.y_scale; 1453 } 1454 else 1455 { 1456 x_scale = 0x10000L; 1457 y_scale = 0x10000L; 1458 } 1459 1460 /* Set `offset' to the start of the glyph relative to the start of */ 1461 /* the `glyf' table, and `byte_len' to the length of the glyph in */ 1462 /* bytes. */ 1463 1464#ifdef FT_CONFIG_OPTION_INCREMENTAL 1465 1466 /* If we are loading glyph data via the incremental interface, set */ 1467 /* the loader stream to a memory stream reading the data returned */ 1468 /* by the interface. */ 1469 if ( face->root.internal->incremental_interface ) 1470 { 1471 error = face->root.internal->incremental_interface->funcs->get_glyph_data( 1472 face->root.internal->incremental_interface->object, 1473 glyph_index, &glyph_data ); 1474 if ( error ) 1475 goto Exit; 1476 1477 glyph_data_loaded = 1; 1478 offset = 0; 1479 loader->byte_len = glyph_data.length; 1480 1481 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) ); 1482 FT_Stream_OpenMemory( &inc_stream, 1483 glyph_data.pointer, 1484 (FT_ULong)glyph_data.length ); 1485 1486 loader->stream = &inc_stream; 1487 } 1488 else 1489 1490#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1491 1492 offset = tt_face_get_location( face, glyph_index, 1493 (FT_UInt*)&loader->byte_len ); 1494 1495 if ( loader->byte_len > 0 ) 1496 { 1497#ifdef FT_CONFIG_OPTION_INCREMENTAL 1498 /* for the incremental interface, `glyf_offset' is always zero */ 1499 if ( !loader->glyf_offset && 1500 !face->root.internal->incremental_interface ) 1501#else 1502 if ( !loader->glyf_offset ) 1503#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1504 { 1505 FT_TRACE2(( "no `glyf' table but non-zero `loca' entry\n" )); 1506 error = FT_THROW( Invalid_Table ); 1507 goto Exit; 1508 } 1509 1510 error = face->access_glyph_frame( loader, glyph_index, 1511 loader->glyf_offset + offset, 1512 (FT_UInt)loader->byte_len ); 1513 if ( error ) 1514 goto Exit; 1515 1516 opened_frame = 1; 1517 1518 /* read glyph header first */ 1519 error = face->read_glyph_header( loader ); 1520 if ( error ) 1521 goto Exit; 1522 1523 /* the metrics must be computed after loading the glyph header */ 1524 /* since we need the glyph's `yMax' value in case the vertical */ 1525 /* metrics must be emulated */ 1526 error = tt_get_metrics( loader, glyph_index ); 1527 if ( error ) 1528 goto Exit; 1529 1530 if ( header_only ) 1531 goto Exit; 1532 } 1533 1534 if ( loader->byte_len == 0 || loader->n_contours == 0 ) 1535 { 1536 loader->bbox.xMin = 0; 1537 loader->bbox.xMax = 0; 1538 loader->bbox.yMin = 0; 1539 loader->bbox.yMax = 0; 1540 1541 error = tt_get_metrics( loader, glyph_index ); 1542 if ( error ) 1543 goto Exit; 1544 1545 if ( header_only ) 1546 goto Exit; 1547 1548 /* must initialize points before (possibly) overriding */ 1549 /* glyph metrics from the incremental interface */ 1550 TT_LOADER_SET_PP( loader ); 1551 1552#ifdef FT_CONFIG_OPTION_INCREMENTAL 1553 tt_get_metrics_incr_overrides( loader, glyph_index ); 1554#endif 1555 1556#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1557 1558 if ( loader->face->doblend ) 1559 { 1560 /* a small outline structure with four elements for */ 1561 /* communication with `TT_Vary_Apply_Glyph_Deltas' */ 1562 FT_Vector points[4]; 1563 char tags[4] = { 1, 1, 1, 1 }; 1564 short contours[4] = { 0, 1, 2, 3 }; 1565 FT_Outline outline; 1566 1567 1568 points[0].x = loader->pp1.x; 1569 points[0].y = loader->pp1.y; 1570 points[1].x = loader->pp2.x; 1571 points[1].y = loader->pp2.y; 1572 1573 points[2].x = loader->pp3.x; 1574 points[2].y = loader->pp3.y; 1575 points[3].x = loader->pp4.x; 1576 points[3].y = loader->pp4.y; 1577 1578 outline.n_points = 4; 1579 outline.n_contours = 4; 1580 outline.points = points; 1581 outline.tags = tags; 1582 outline.contours = contours; 1583 1584 /* this must be done before scaling */ 1585 error = TT_Vary_Apply_Glyph_Deltas( loader->face, 1586 glyph_index, 1587 &outline, 1588 (FT_UInt)outline.n_points ); 1589 if ( error ) 1590 goto Exit; 1591 1592 loader->pp1.x = points[0].x; 1593 loader->pp1.y = points[0].y; 1594 loader->pp2.x = points[1].x; 1595 loader->pp2.y = points[1].y; 1596 1597 loader->pp3.x = points[2].x; 1598 loader->pp3.y = points[2].y; 1599 loader->pp4.x = points[3].x; 1600 loader->pp4.y = points[3].y; 1601 } 1602 1603#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1604 1605 /* scale phantom points, if necessary; */ 1606 /* they get rounded in `TT_Hint_Glyph' */ 1607 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1608 { 1609 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1610 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1611 /* pp1.y and pp2.y are always zero */ 1612 1613 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale ); 1614 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1615 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale ); 1616 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1617 } 1618 1619 error = FT_Err_Ok; 1620 goto Exit; 1621 } 1622 1623 /* must initialize phantom points before (possibly) overriding */ 1624 /* glyph metrics from the incremental interface */ 1625 TT_LOADER_SET_PP( loader ); 1626 1627#ifdef FT_CONFIG_OPTION_INCREMENTAL 1628 tt_get_metrics_incr_overrides( loader, glyph_index ); 1629#endif 1630 1631 /***********************************************************************/ 1632 /***********************************************************************/ 1633 /***********************************************************************/ 1634 1635 /* if it is a simple glyph, load it */ 1636 1637 if ( loader->n_contours > 0 ) 1638 { 1639 error = face->read_simple_glyph( loader ); 1640 if ( error ) 1641 goto Exit; 1642 1643 /* all data have been read */ 1644 face->forget_glyph_frame( loader ); 1645 opened_frame = 0; 1646 1647 error = TT_Process_Simple_Glyph( loader ); 1648 if ( error ) 1649 goto Exit; 1650 1651 FT_GlyphLoader_Add( gloader ); 1652 } 1653 1654 /***********************************************************************/ 1655 /***********************************************************************/ 1656 /***********************************************************************/ 1657 1658 /* otherwise, load a composite! */ 1659 else if ( loader->n_contours == -1 ) 1660 { 1661 FT_Memory memory = face->root.memory; 1662 1663 FT_UInt start_point; 1664 FT_UInt start_contour; 1665 FT_ULong ins_pos; /* position of composite instructions, if any */ 1666 FT_ListNode node, node2; 1667 1668 1669 /* 1670 * We store the glyph index directly in the `node->data' pointer, 1671 * following the glib solution (cf. macro `GUINT_TO_POINTER') with a 1672 * double cast to make this portable. Note, however, that this needs 1673 * pointers with a width of at least 32 bits. 1674 */ 1675 1676 1677 /* clear the nodes filled by sibling chains */ 1678 node = ft_list_get_node_at( &loader->composites, recurse_count ); 1679 for ( node2 = node ; node2 ; node2 = node2->next ) 1680 node2->data = (void*)ULONG_MAX; 1681 1682 1683 /* check whether we already have a composite glyph with this index */ 1684 if ( FT_List_Find( &loader->composites, 1685 (void*)(unsigned long)glyph_index ) ) 1686 { 1687 FT_TRACE1(( "TT_Load_Composite_Glyph:" 1688 " infinite recursion detected\n" )); 1689 error = FT_THROW( Invalid_Composite ); 1690 goto Exit; 1691 } 1692 else if ( node ) 1693 { 1694 node->data = (void*)(unsigned long)glyph_index; 1695 } 1696 else 1697 { 1698 if ( FT_NEW( node ) ) 1699 goto Exit; 1700 node->data = (void*)(unsigned long)glyph_index; 1701 FT_List_Add( &loader->composites, node ); 1702 } 1703 1704 start_point = (FT_UInt)gloader->base.outline.n_points; 1705 start_contour = (FT_UInt)gloader->base.outline.n_contours; 1706 1707 /* for each subglyph, read composite header */ 1708 error = face->read_composite_glyph( loader ); 1709 if ( error ) 1710 goto Exit; 1711 1712 /* store the offset of instructions */ 1713 ins_pos = loader->ins_pos; 1714 1715 /* all data we need are read */ 1716 face->forget_glyph_frame( loader ); 1717 opened_frame = 0; 1718 1719#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1720 1721 if ( face->doblend ) 1722 { 1723 short i, limit; 1724 FT_SubGlyph subglyph; 1725 1726 FT_Outline outline; 1727 FT_Vector* points = NULL; 1728 char* tags = NULL; 1729 short* contours = NULL; 1730 1731 1732 limit = (short)gloader->current.num_subglyphs; 1733 1734 /* construct an outline structure for */ 1735 /* communication with `TT_Vary_Apply_Glyph_Deltas' */ 1736 outline.n_points = (short)( gloader->current.num_subglyphs + 4 ); 1737 outline.n_contours = outline.n_points; 1738 1739 outline.tags = NULL; 1740 outline.contours = NULL; 1741 1742 if ( FT_NEW_ARRAY( points, outline.n_points ) || 1743 FT_NEW_ARRAY( tags, outline.n_points ) || 1744 FT_NEW_ARRAY( contours, outline.n_points ) ) 1745 goto Exit1; 1746 1747 subglyph = gloader->current.subglyphs; 1748 1749 for ( i = 0; i < limit; i++, subglyph++ ) 1750 { 1751 /* applying deltas for anchor points doesn't make sense, */ 1752 /* but we don't have to specially check this since */ 1753 /* unused delta values are zero anyways */ 1754 points[i].x = subglyph->arg1; 1755 points[i].y = subglyph->arg2; 1756 tags[i] = 1; 1757 contours[i] = i; 1758 } 1759 1760 points[i].x = loader->pp1.x; 1761 points[i].y = loader->pp1.y; 1762 tags[i] = 1; 1763 contours[i] = i; 1764 1765 i++; 1766 points[i].x = loader->pp2.x; 1767 points[i].y = loader->pp2.y; 1768 tags[i] = 1; 1769 contours[i] = i; 1770 1771 i++; 1772 points[i].x = loader->pp3.x; 1773 points[i].y = loader->pp3.y; 1774 tags[i] = 1; 1775 contours[i] = i; 1776 1777 i++; 1778 points[i].x = loader->pp4.x; 1779 points[i].y = loader->pp4.y; 1780 tags[i] = 1; 1781 contours[i] = i; 1782 1783 outline.points = points; 1784 outline.tags = tags; 1785 outline.contours = contours; 1786 1787 /* this call provides additional offsets */ 1788 /* for each component's translation */ 1789 if ( ( error = TT_Vary_Apply_Glyph_Deltas( 1790 face, 1791 glyph_index, 1792 &outline, 1793 (FT_UInt)outline.n_points ) ) != 0 ) 1794 goto Exit1; 1795 1796 subglyph = gloader->current.subglyphs; 1797 1798 for ( i = 0; i < limit; i++, subglyph++ ) 1799 { 1800 /* XXX: overflow check for subglyph->{arg1,arg2}. */ 1801 /* Deltas must be within signed 16-bit, */ 1802 /* but the restriction of summed deltas is not clear */ 1803 subglyph->arg1 = (FT_Int16)points[i].x; 1804 subglyph->arg2 = (FT_Int16)points[i].y; 1805 } 1806 1807 loader->pp1.x = points[i + 0].x; 1808 loader->pp1.y = points[i + 0].y; 1809 loader->pp2.x = points[i + 1].x; 1810 loader->pp2.y = points[i + 1].y; 1811 1812 loader->pp3.x = points[i + 2].x; 1813 loader->pp3.y = points[i + 2].y; 1814 loader->pp4.x = points[i + 3].x; 1815 loader->pp4.y = points[i + 3].y; 1816 1817 Exit1: 1818 FT_FREE( outline.points ); 1819 FT_FREE( outline.tags ); 1820 FT_FREE( outline.contours ); 1821 1822 if ( error ) 1823 goto Exit; 1824 } 1825 1826#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1827 1828 /* scale phantom points, if necessary; */ 1829 /* they get rounded in `TT_Hint_Glyph' */ 1830 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1831 { 1832 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1833 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1834 /* pp1.y and pp2.y are always zero */ 1835 1836 loader->pp3.x = FT_MulFix( loader->pp3.x, x_scale ); 1837 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1838 loader->pp4.x = FT_MulFix( loader->pp4.x, x_scale ); 1839 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1840 } 1841 1842 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ 1843 /* `as is' in the glyph slot (the client application will be */ 1844 /* responsible for interpreting these data)... */ 1845 if ( loader->load_flags & FT_LOAD_NO_RECURSE ) 1846 { 1847 FT_GlyphLoader_Add( gloader ); 1848 loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE; 1849 1850 goto Exit; 1851 } 1852 1853 /*********************************************************************/ 1854 /*********************************************************************/ 1855 /*********************************************************************/ 1856 1857 { 1858 FT_UInt n, num_base_points; 1859 FT_SubGlyph subglyph = NULL; 1860 1861 FT_UInt num_points = start_point; 1862 FT_UInt num_subglyphs = gloader->current.num_subglyphs; 1863 FT_UInt num_base_subgs = gloader->base.num_subglyphs; 1864 1865 FT_Stream old_stream = loader->stream; 1866 FT_Int old_byte_len = loader->byte_len; 1867 1868 1869 FT_GlyphLoader_Add( gloader ); 1870 1871 /* read each subglyph independently */ 1872 for ( n = 0; n < num_subglyphs; n++ ) 1873 { 1874 FT_Vector pp[4]; 1875 1876 1877 /* Each time we call load_truetype_glyph in this loop, the */ 1878 /* value of `gloader.base.subglyphs' can change due to table */ 1879 /* reallocations. We thus need to recompute the subglyph */ 1880 /* pointer on each iteration. */ 1881 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1882 1883 pp[0] = loader->pp1; 1884 pp[1] = loader->pp2; 1885 pp[2] = loader->pp3; 1886 pp[3] = loader->pp4; 1887 1888 num_base_points = (FT_UInt)gloader->base.outline.n_points; 1889 1890 error = load_truetype_glyph( loader, 1891 (FT_UInt)subglyph->index, 1892 recurse_count + 1, 1893 FALSE ); 1894 if ( error ) 1895 goto Exit; 1896 1897 /* restore subglyph pointer */ 1898 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1899 1900 /* restore phantom points if necessary */ 1901 if ( !( subglyph->flags & USE_MY_METRICS ) ) 1902 { 1903 loader->pp1 = pp[0]; 1904 loader->pp2 = pp[1]; 1905 loader->pp3 = pp[2]; 1906 loader->pp4 = pp[3]; 1907 } 1908 1909 num_points = (FT_UInt)gloader->base.outline.n_points; 1910 1911 if ( num_points == num_base_points ) 1912 continue; 1913 1914 /* gloader->base.outline consists of three parts: */ 1915 /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */ 1916 /* */ 1917 /* (1): exists from the beginning */ 1918 /* (2): components that have been loaded so far */ 1919 /* (3): the newly loaded component */ 1920 error = TT_Process_Composite_Component( loader, 1921 subglyph, 1922 start_point, 1923 num_base_points ); 1924 if ( error ) 1925 goto Exit; 1926 } 1927 1928 loader->stream = old_stream; 1929 loader->byte_len = old_byte_len; 1930 1931 /* process the glyph */ 1932 loader->ins_pos = ins_pos; 1933 if ( IS_HINTED( loader->load_flags ) && 1934#ifdef TT_USE_BYTECODE_INTERPRETER 1935 subglyph->flags & WE_HAVE_INSTR && 1936#endif 1937 num_points > start_point ) 1938 { 1939 error = TT_Process_Composite_Glyph( loader, 1940 start_point, 1941 start_contour ); 1942 if ( error ) 1943 goto Exit; 1944 } 1945 } 1946 } 1947 else 1948 { 1949 /* invalid composite count (negative but not -1) */ 1950 error = FT_THROW( Invalid_Outline ); 1951 goto Exit; 1952 } 1953 1954 /***********************************************************************/ 1955 /***********************************************************************/ 1956 /***********************************************************************/ 1957 1958 Exit: 1959 1960 if ( opened_frame ) 1961 face->forget_glyph_frame( loader ); 1962 1963#ifdef FT_CONFIG_OPTION_INCREMENTAL 1964 1965 if ( glyph_data_loaded ) 1966 face->root.internal->incremental_interface->funcs->free_glyph_data( 1967 face->root.internal->incremental_interface->object, 1968 &glyph_data ); 1969 1970#endif 1971 1972 return error; 1973 } 1974 1975 1976 static FT_Error 1977 compute_glyph_metrics( TT_Loader loader, 1978 FT_UInt glyph_index ) 1979 { 1980 TT_Face face = loader->face; 1981#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 1982 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 1983#endif 1984 1985 FT_BBox bbox; 1986 FT_Fixed y_scale; 1987 TT_GlyphSlot glyph = loader->glyph; 1988 TT_Size size = loader->size; 1989 1990 1991 y_scale = 0x10000L; 1992 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1993 y_scale = size->root.metrics.y_scale; 1994 1995 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE ) 1996 FT_Outline_Get_CBox( &glyph->outline, &bbox ); 1997 else 1998 bbox = loader->bbox; 1999 2000 /* get the device-independent horizontal advance; it is scaled later */ 2001 /* by the base layer. */ 2002 glyph->linearHoriAdvance = loader->linear; 2003 2004 glyph->metrics.horiBearingX = bbox.xMin; 2005 glyph->metrics.horiBearingY = bbox.yMax; 2006 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; 2007 2008 /* adjust advance width to the value contained in the hdmx table */ 2009 /* unless FT_LOAD_COMPUTE_METRICS is set */ 2010 if ( !face->postscript.isFixedPitch && 2011 IS_HINTED( loader->load_flags ) && 2012 !( loader->load_flags & FT_LOAD_COMPUTE_METRICS ) ) 2013 { 2014 FT_Byte* widthp; 2015 2016 2017 widthp = tt_face_get_device_metrics( face, 2018 size->root.metrics.x_ppem, 2019 glyph_index ); 2020 2021#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2022 2023 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2024 { 2025 FT_Bool ignore_x_mode; 2026 2027 2028 ignore_x_mode = FT_BOOL( FT_LOAD_TARGET_MODE( loader->load_flags ) != 2029 FT_RENDER_MODE_MONO ); 2030 2031 if ( widthp && 2032 ( ( ignore_x_mode && loader->exec->compatible_widths ) || 2033 !ignore_x_mode || 2034 SPH_OPTION_BITMAP_WIDTHS ) ) 2035 glyph->metrics.horiAdvance = *widthp * 64; 2036 } 2037 else 2038 2039#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2040 2041 { 2042 if ( widthp ) 2043 glyph->metrics.horiAdvance = *widthp * 64; 2044 } 2045 } 2046 2047 /* set glyph dimensions */ 2048 glyph->metrics.width = bbox.xMax - bbox.xMin; 2049 glyph->metrics.height = bbox.yMax - bbox.yMin; 2050 2051 /* Now take care of vertical metrics. In the case where there is */ 2052 /* no vertical information within the font (relatively common), */ 2053 /* create some metrics manually */ 2054 { 2055 FT_Pos top; /* scaled vertical top side bearing */ 2056 FT_Pos advance; /* scaled vertical advance height */ 2057 2058 2059 /* Get the unscaled top bearing and advance height. */ 2060 if ( face->vertical_info && 2061 face->vertical.number_Of_VMetrics > 0 ) 2062 { 2063 top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax, 2064 y_scale ); 2065 2066 if ( loader->pp3.y <= loader->pp4.y ) 2067 advance = 0; 2068 else 2069 advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y, 2070 y_scale ); 2071 } 2072 else 2073 { 2074 FT_Pos height; 2075 2076 2077 /* XXX Compute top side bearing and advance height in */ 2078 /* Get_VMetrics instead of here. */ 2079 2080 /* NOTE: The OS/2 values are the only `portable' ones, */ 2081 /* which is why we use them, if there is an OS/2 */ 2082 /* table in the font. Otherwise, we use the */ 2083 /* values defined in the horizontal header. */ 2084 2085 height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin, 2086 y_scale ); 2087 if ( face->os2.version != 0xFFFFU ) 2088 advance = (FT_Pos)( face->os2.sTypoAscender - 2089 face->os2.sTypoDescender ); 2090 else 2091 advance = (FT_Pos)( face->horizontal.Ascender - 2092 face->horizontal.Descender ); 2093 2094 top = ( advance - height ) / 2; 2095 } 2096 2097#ifdef FT_CONFIG_OPTION_INCREMENTAL 2098 { 2099 FT_Incremental_InterfaceRec* incr; 2100 FT_Incremental_MetricsRec metrics; 2101 FT_Error error; 2102 2103 2104 incr = face->root.internal->incremental_interface; 2105 2106 /* If this is an incrementally loaded font see if there are */ 2107 /* overriding metrics for this glyph. */ 2108 if ( incr && incr->funcs->get_glyph_metrics ) 2109 { 2110 metrics.bearing_x = 0; 2111 metrics.bearing_y = top; 2112 metrics.advance = advance; 2113 2114 error = incr->funcs->get_glyph_metrics( incr->object, 2115 glyph_index, 2116 TRUE, 2117 &metrics ); 2118 if ( error ) 2119 return error; 2120 2121 top = metrics.bearing_y; 2122 advance = metrics.advance; 2123 } 2124 } 2125 2126 /* GWW: Do vertical metrics get loaded incrementally too? */ 2127 2128#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 2129 2130 glyph->linearVertAdvance = advance; 2131 2132 /* scale the metrics */ 2133 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 2134 { 2135 top = FT_MulFix( top, y_scale ); 2136 advance = FT_MulFix( advance, y_scale ); 2137 } 2138 2139 /* XXX: for now, we have no better algorithm for the lsb, but it */ 2140 /* should work fine. */ 2141 /* */ 2142 glyph->metrics.vertBearingX = glyph->metrics.horiBearingX - 2143 glyph->metrics.horiAdvance / 2; 2144 glyph->metrics.vertBearingY = top; 2145 glyph->metrics.vertAdvance = advance; 2146 } 2147 2148 return 0; 2149 } 2150 2151 2152#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 2153 2154 static FT_Error 2155 load_sbit_image( TT_Size size, 2156 TT_GlyphSlot glyph, 2157 FT_UInt glyph_index, 2158 FT_Int32 load_flags ) 2159 { 2160 TT_Face face; 2161 SFNT_Service sfnt; 2162 FT_Stream stream; 2163 FT_Error error; 2164 TT_SBit_MetricsRec metrics; 2165 2166 2167 face = (TT_Face)glyph->face; 2168 sfnt = (SFNT_Service)face->sfnt; 2169 stream = face->root.stream; 2170 2171 error = sfnt->load_sbit_image( face, 2172 size->strike_index, 2173 glyph_index, 2174 (FT_UInt)load_flags, 2175 stream, 2176 &glyph->bitmap, 2177 &metrics ); 2178 if ( !error ) 2179 { 2180 glyph->outline.n_points = 0; 2181 glyph->outline.n_contours = 0; 2182 2183 glyph->metrics.width = (FT_Pos)metrics.width * 64; 2184 glyph->metrics.height = (FT_Pos)metrics.height * 64; 2185 2186 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX * 64; 2187 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY * 64; 2188 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance * 64; 2189 2190 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX * 64; 2191 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY * 64; 2192 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance * 64; 2193 2194 glyph->format = FT_GLYPH_FORMAT_BITMAP; 2195 2196 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 2197 { 2198 glyph->bitmap_left = metrics.vertBearingX; 2199 glyph->bitmap_top = metrics.vertBearingY; 2200 } 2201 else 2202 { 2203 glyph->bitmap_left = metrics.horiBearingX; 2204 glyph->bitmap_top = metrics.horiBearingY; 2205 } 2206 } 2207 2208 return error; 2209 } 2210 2211#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 2212 2213 2214 static FT_Error 2215 tt_loader_init( TT_Loader loader, 2216 TT_Size size, 2217 TT_GlyphSlot glyph, 2218 FT_Int32 load_flags, 2219 FT_Bool glyf_table_only ) 2220 { 2221 FT_Error error; 2222 2223 TT_Face face; 2224 FT_Stream stream; 2225#ifdef TT_USE_BYTECODE_INTERPRETER 2226 FT_Bool pedantic = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 2227#endif 2228 2229 2230 face = (TT_Face)glyph->face; 2231 stream = face->root.stream; 2232 2233 FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) ); 2234 2235#ifdef TT_USE_BYTECODE_INTERPRETER 2236 2237 /* load execution context */ 2238 if ( IS_HINTED( load_flags ) && !glyf_table_only ) 2239 { 2240 TT_ExecContext exec; 2241 FT_Bool grayscale; 2242 2243#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2244 TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face ); 2245 2246 FT_Bool subpixel_hinting = FALSE; 2247 2248#if 0 2249 /* not used yet */ 2250 FT_Bool compatible_widths; 2251 FT_Bool symmetrical_smoothing; 2252 FT_Bool bgr; 2253 FT_Bool vertical_lcd; 2254 FT_Bool subpixel_positioned; 2255 FT_Bool gray_cleartype; 2256#endif 2257#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2258 2259 FT_Bool reexecute = FALSE; 2260 2261 2262 if ( size->bytecode_ready < 0 || size->cvt_ready < 0 ) 2263 { 2264 error = tt_size_ready_bytecode( size, pedantic ); 2265 if ( error ) 2266 return error; 2267 } 2268 else if ( size->bytecode_ready ) 2269 return size->bytecode_ready; 2270 else if ( size->cvt_ready ) 2271 return size->cvt_ready; 2272 2273 /* query new execution context */ 2274 exec = size->context; 2275 if ( !exec ) 2276 return FT_THROW( Could_Not_Find_Context ); 2277 2278#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2279 2280 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2281 { 2282 subpixel_hinting = FT_BOOL( ( FT_LOAD_TARGET_MODE( load_flags ) != 2283 FT_RENDER_MODE_MONO ) && 2284 SPH_OPTION_SET_SUBPIXEL ); 2285 2286 if ( subpixel_hinting ) 2287 grayscale = FALSE; 2288 else if ( SPH_OPTION_SET_GRAYSCALE ) 2289 { 2290 grayscale = TRUE; 2291 subpixel_hinting = FALSE; 2292 } 2293 else 2294 grayscale = FALSE; 2295 2296 if ( FT_IS_TRICKY( glyph->face ) ) 2297 subpixel_hinting = FALSE; 2298 2299 exec->ignore_x_mode = subpixel_hinting || grayscale; 2300 exec->rasterizer_version = SPH_OPTION_SET_RASTERIZER_VERSION; 2301 if ( exec->sph_tweak_flags & SPH_TWEAK_RASTERIZER_35 ) 2302 exec->rasterizer_version = TT_INTERPRETER_VERSION_35; 2303 2304#if 1 2305 exec->compatible_widths = SPH_OPTION_SET_COMPATIBLE_WIDTHS; 2306 exec->symmetrical_smoothing = TRUE; 2307 exec->bgr = FALSE; 2308 exec->vertical_lcd = FALSE; 2309 exec->subpixel_positioned = TRUE; 2310 exec->gray_cleartype = FALSE; 2311#else /* 0 */ 2312 exec->compatible_widths = 2313 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2314 TT_LOAD_COMPATIBLE_WIDTHS ); 2315 exec->symmetrical_smoothing = 2316 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2317 TT_LOAD_SYMMETRICAL_SMOOTHING ); 2318 exec->bgr = 2319 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2320 TT_LOAD_BGR ); 2321 exec->vertical_lcd = 2322 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2323 TT_LOAD_VERTICAL_LCD ); 2324 exec->subpixel_positioned = 2325 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2326 TT_LOAD_SUBPIXEL_POSITIONED ); 2327 exec->gray_cleartype = 2328 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2329 TT_LOAD_GRAY_CLEARTYPE ); 2330#endif /* 0 */ 2331 2332 } 2333 else 2334 2335#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2336 2337 { 2338 grayscale = FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != 2339 FT_RENDER_MODE_MONO ); 2340 } 2341 2342 error = TT_Load_Context( exec, face, size ); 2343 if ( error ) 2344 return error; 2345 2346#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2347 2348 if ( driver->interpreter_version == TT_INTERPRETER_VERSION_38 ) 2349 { 2350 /* a change from mono to subpixel rendering (and vice versa) */ 2351 /* requires a re-execution of the CVT program */ 2352 if ( subpixel_hinting != exec->subpixel_hinting ) 2353 { 2354 FT_TRACE4(( "tt_loader_init: subpixel hinting change," 2355 " re-executing `prep' table\n" )); 2356 2357 exec->subpixel_hinting = subpixel_hinting; 2358 reexecute = TRUE; 2359 } 2360 2361 /* a change from mono to grayscale rendering (and vice versa) */ 2362 /* requires a re-execution of the CVT program */ 2363 if ( grayscale != exec->grayscale ) 2364 { 2365 FT_TRACE4(( "tt_loader_init: grayscale hinting change," 2366 " re-executing `prep' table\n" )); 2367 2368 exec->grayscale = grayscale; 2369 reexecute = TRUE; 2370 } 2371 } 2372 else 2373 2374#endif /* TT_CONFIG_OPTION_SUBPIXEL_HINTING */ 2375 2376 { 2377 /* a change from mono to grayscale rendering (and vice versa) */ 2378 /* requires a re-execution of the CVT program */ 2379 if ( grayscale != exec->grayscale ) 2380 { 2381 FT_TRACE4(( "tt_loader_init: grayscale hinting change," 2382 " re-executing `prep' table\n" )); 2383 2384 exec->grayscale = grayscale; 2385 reexecute = TRUE; 2386 } 2387 } 2388 2389 if ( reexecute ) 2390 { 2391 FT_UInt i; 2392 2393 2394 for ( i = 0; i < size->cvt_size; i++ ) 2395 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale ); 2396 error = tt_size_run_prep( size, pedantic ); 2397 if ( error ) 2398 return error; 2399 } 2400 2401 /* check whether the cvt program has disabled hinting */ 2402 if ( exec->GS.instruct_control & 1 ) 2403 load_flags |= FT_LOAD_NO_HINTING; 2404 2405 /* load default graphics state -- if needed */ 2406 if ( exec->GS.instruct_control & 2 ) 2407 exec->GS = tt_default_graphics_state; 2408 2409#ifdef TT_CONFIG_OPTION_SUBPIXEL_HINTING 2410 /* check whether we have a font hinted for ClearType -- */ 2411 /* note that this flag can also be modified in a glyph's bytecode */ 2412 if ( exec->GS.instruct_control & 4 ) 2413 exec->ignore_x_mode = 0; 2414#endif 2415 2416 exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 2417 loader->exec = exec; 2418 loader->instructions = exec->glyphIns; 2419 } 2420 2421#endif /* TT_USE_BYTECODE_INTERPRETER */ 2422 2423 /* seek to the beginning of the glyph table -- for Type 42 fonts */ 2424 /* the table might be accessed from a Postscript stream or something */ 2425 /* else... */ 2426 2427#ifdef FT_CONFIG_OPTION_INCREMENTAL 2428 2429 if ( face->root.internal->incremental_interface ) 2430 loader->glyf_offset = 0; 2431 else 2432 2433#endif 2434 2435 { 2436 error = face->goto_table( face, TTAG_glyf, stream, 0 ); 2437 2438 if ( FT_ERR_EQ( error, Table_Missing ) ) 2439 loader->glyf_offset = 0; 2440 else if ( error ) 2441 { 2442 FT_ERROR(( "tt_loader_init: could not access glyph table\n" )); 2443 return error; 2444 } 2445 else 2446 loader->glyf_offset = FT_STREAM_POS(); 2447 } 2448 2449 /* get face's glyph loader */ 2450 if ( !glyf_table_only ) 2451 { 2452 FT_GlyphLoader gloader = glyph->internal->loader; 2453 2454 2455 FT_GlyphLoader_Rewind( gloader ); 2456 loader->gloader = gloader; 2457 } 2458 2459 loader->load_flags = (FT_ULong)load_flags; 2460 2461 loader->face = face; 2462 loader->size = size; 2463 loader->glyph = (FT_GlyphSlot)glyph; 2464 loader->stream = stream; 2465 2466 loader->composites.head = NULL; 2467 loader->composites.tail = NULL; 2468 2469 return FT_Err_Ok; 2470 } 2471 2472 2473 static void 2474 tt_loader_done( TT_Loader loader ) 2475 { 2476 FT_List_Finalize( &loader->composites, 2477 NULL, 2478 loader->face->root.memory, 2479 NULL ); 2480 } 2481 2482 2483 /*************************************************************************/ 2484 /* */ 2485 /* <Function> */ 2486 /* TT_Load_Glyph */ 2487 /* */ 2488 /* <Description> */ 2489 /* A function used to load a single glyph within a given glyph slot, */ 2490 /* for a given size. */ 2491 /* */ 2492 /* <Input> */ 2493 /* glyph :: A handle to a target slot object where the glyph */ 2494 /* will be loaded. */ 2495 /* */ 2496 /* size :: A handle to the source face size at which the glyph */ 2497 /* must be scaled/loaded. */ 2498 /* */ 2499 /* glyph_index :: The index of the glyph in the font file. */ 2500 /* */ 2501 /* load_flags :: A flag indicating what to load for this glyph. The */ 2502 /* FT_LOAD_XXX constants can be used to control the */ 2503 /* glyph loading process (e.g., whether the outline */ 2504 /* should be scaled, whether to load bitmaps or not, */ 2505 /* whether to hint the outline, etc). */ 2506 /* */ 2507 /* <Return> */ 2508 /* FreeType error code. 0 means success. */ 2509 /* */ 2510 FT_LOCAL_DEF( FT_Error ) 2511 TT_Load_Glyph( TT_Size size, 2512 TT_GlyphSlot glyph, 2513 FT_UInt glyph_index, 2514 FT_Int32 load_flags ) 2515 { 2516 FT_Error error; 2517 TT_LoaderRec loader; 2518 2519 2520 FT_TRACE1(( "TT_Load_Glyph: glyph index %d\n", glyph_index )); 2521 2522#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 2523 2524 /* try to load embedded bitmap if any */ 2525 /* */ 2526 /* XXX: The convention should be emphasized in */ 2527 /* the documents because it can be confusing. */ 2528 if ( size->strike_index != 0xFFFFFFFFUL && 2529 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) 2530 { 2531 error = load_sbit_image( size, glyph, glyph_index, load_flags ); 2532 if ( !error ) 2533 { 2534 if ( FT_IS_SCALABLE( glyph->face ) ) 2535 { 2536 /* for the bbox we need the header only */ 2537 (void)tt_loader_init( &loader, size, glyph, load_flags, TRUE ); 2538 (void)load_truetype_glyph( &loader, glyph_index, 0, TRUE ); 2539 tt_loader_done( &loader ); 2540 glyph->linearHoriAdvance = loader.linear; 2541 glyph->linearVertAdvance = loader.vadvance; 2542 2543 /* sanity checks: if `xxxAdvance' in the sbit metric */ 2544 /* structure isn't set, use `linearXXXAdvance' */ 2545 if ( !glyph->metrics.horiAdvance && glyph->linearHoriAdvance ) 2546 glyph->metrics.horiAdvance = 2547 FT_MulFix( glyph->linearHoriAdvance, 2548 size->root.metrics.x_scale ); 2549 if ( !glyph->metrics.vertAdvance && glyph->linearVertAdvance ) 2550 glyph->metrics.vertAdvance = 2551 FT_MulFix( glyph->linearVertAdvance, 2552 size->root.metrics.y_scale ); 2553 } 2554 2555 return FT_Err_Ok; 2556 } 2557 } 2558 2559#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 2560 2561 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */ 2562 if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid ) 2563 return FT_THROW( Invalid_Size_Handle ); 2564 2565 if ( load_flags & FT_LOAD_SBITS_ONLY ) 2566 return FT_THROW( Invalid_Argument ); 2567 2568 error = tt_loader_init( &loader, size, glyph, load_flags, FALSE ); 2569 if ( error ) 2570 return error; 2571 2572 glyph->format = FT_GLYPH_FORMAT_OUTLINE; 2573 glyph->num_subglyphs = 0; 2574 glyph->outline.flags = 0; 2575 2576 /* main loading loop */ 2577 error = load_truetype_glyph( &loader, glyph_index, 0, FALSE ); 2578 if ( !error ) 2579 { 2580 if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE ) 2581 { 2582 glyph->num_subglyphs = loader.gloader->base.num_subglyphs; 2583 glyph->subglyphs = loader.gloader->base.subglyphs; 2584 } 2585 else 2586 { 2587 glyph->outline = loader.gloader->base.outline; 2588 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS; 2589 2590 /* Translate array so that (0,0) is the glyph's origin. Note */ 2591 /* that this behaviour is independent on the value of bit 1 of */ 2592 /* the `flags' field in the `head' table -- at least major */ 2593 /* applications like Acroread indicate that. */ 2594 if ( loader.pp1.x ) 2595 FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 ); 2596 } 2597 2598#ifdef TT_USE_BYTECODE_INTERPRETER 2599 2600 if ( IS_HINTED( load_flags ) ) 2601 { 2602 if ( loader.exec->GS.scan_control ) 2603 { 2604 /* convert scan conversion mode to FT_OUTLINE_XXX flags */ 2605 switch ( loader.exec->GS.scan_type ) 2606 { 2607 case 0: /* simple drop-outs including stubs */ 2608 glyph->outline.flags |= FT_OUTLINE_INCLUDE_STUBS; 2609 break; 2610 case 1: /* simple drop-outs excluding stubs */ 2611 /* nothing; it's the default rendering mode */ 2612 break; 2613 case 4: /* smart drop-outs including stubs */ 2614 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS | 2615 FT_OUTLINE_INCLUDE_STUBS; 2616 break; 2617 case 5: /* smart drop-outs excluding stubs */ 2618 glyph->outline.flags |= FT_OUTLINE_SMART_DROPOUTS; 2619 break; 2620 2621 default: /* no drop-out control */ 2622 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS; 2623 break; 2624 } 2625 } 2626 else 2627 glyph->outline.flags |= FT_OUTLINE_IGNORE_DROPOUTS; 2628 } 2629 2630#endif /* TT_USE_BYTECODE_INTERPRETER */ 2631 2632 error = compute_glyph_metrics( &loader, glyph_index ); 2633 } 2634 2635 tt_loader_done( &loader ); 2636 2637 /* Set the `high precision' bit flag. */ 2638 /* This is _critical_ to get correct output for monochrome */ 2639 /* TrueType glyphs at all sizes using the bytecode interpreter. */ 2640 /* */ 2641 if ( !( load_flags & FT_LOAD_NO_SCALE ) && 2642 size->root.metrics.y_ppem < 24 ) 2643 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 2644 2645 return error; 2646 } 2647 2648 2649/* END */ 2650