ttgload.c revision 0ba89912928144811c0294ae5903658c0a33e387
1/***************************************************************************/ 2/* */ 3/* ttgload.c */ 4/* */ 5/* TrueType Glyph Loader (body). */ 6/* */ 7/* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007 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 27#include "ttgload.h" 28#include "ttpload.h" 29 30#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 31#include "ttgxvar.h" 32#endif 33 34#include "tterrors.h" 35 36 37 /*************************************************************************/ 38 /* */ 39 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 40 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 41 /* messages during execution. */ 42 /* */ 43#undef FT_COMPONENT 44#define FT_COMPONENT trace_ttgload 45 46 47 /*************************************************************************/ 48 /* */ 49 /* Composite font flags. */ 50 /* */ 51#define ARGS_ARE_WORDS 0x0001 52#define ARGS_ARE_XY_VALUES 0x0002 53#define ROUND_XY_TO_GRID 0x0004 54#define WE_HAVE_A_SCALE 0x0008 55/* reserved 0x0010 */ 56#define MORE_COMPONENTS 0x0020 57#define WE_HAVE_AN_XY_SCALE 0x0040 58#define WE_HAVE_A_2X2 0x0080 59#define WE_HAVE_INSTR 0x0100 60#define USE_MY_METRICS 0x0200 61#define OVERLAP_COMPOUND 0x0400 62#define SCALED_COMPONENT_OFFSET 0x0800 63#define UNSCALED_COMPONENT_OFFSET 0x1000 64 65 66 /*************************************************************************/ 67 /* */ 68 /* Returns the horizontal metrics in font units for a given glyph. If */ 69 /* `check' is true, take care of monospaced fonts by returning the */ 70 /* advance width maximum. */ 71 /* */ 72 static void 73 Get_HMetrics( TT_Face face, 74 FT_UInt idx, 75 FT_Bool check, 76 FT_Short* lsb, 77 FT_UShort* aw ) 78 { 79 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, idx, lsb, aw ); 80 81 if ( check && face->postscript.isFixedPitch ) 82 *aw = face->horizontal.advance_Width_Max; 83 } 84 85 86 /*************************************************************************/ 87 /* */ 88 /* Returns the vertical metrics in font units for a given glyph. */ 89 /* Greg Hitchcock from Microsoft told us that if there were no `vmtx' */ 90 /* table, typoAscender/Descender from the `OS/2' table would be used */ 91 /* instead, and if there were no `OS/2' table, use ascender/descender */ 92 /* from the `hhea' table. But that is not what Microsoft's rasterizer */ 93 /* apparently does: It uses the ppem value as the advance height, and */ 94 /* sets the top side bearing to be zero. */ 95 /* */ 96 /* The monospace `check' is probably not meaningful here, but we leave */ 97 /* it in for a consistent interface. */ 98 /* */ 99 static void 100 Get_VMetrics( TT_Face face, 101 FT_UInt idx, 102 FT_Bool check, 103 FT_Short* tsb, 104 FT_UShort* ah ) 105 { 106 FT_UNUSED( check ); 107 108 if ( face->vertical_info ) 109 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, idx, tsb, ah ); 110 111#if 1 /* Empirically determined, at variance with what MS said */ 112 113 else 114 { 115 *tsb = 0; 116 *ah = face->root.units_per_EM; 117 } 118 119#else /* This is what MS said to do. It isn't what they do, however. */ 120 121 else if ( face->os2.version != 0xFFFFU ) 122 { 123 *tsb = face->os2.sTypoAscender; 124 *ah = face->os2.sTypoAscender - face->os2.sTypoDescender; 125 } 126 else 127 { 128 *tsb = face->horizontal.Ascender; 129 *ah = face->horizontal.Ascender - face->horizontal.Descender; 130 } 131 132#endif 133 134 } 135 136 137 /*************************************************************************/ 138 /* */ 139 /* Translates an array of coordinates. */ 140 /* */ 141 static void 142 translate_array( FT_UInt n, 143 FT_Vector* coords, 144 FT_Pos delta_x, 145 FT_Pos delta_y ) 146 { 147 FT_UInt k; 148 149 150 if ( delta_x ) 151 for ( k = 0; k < n; k++ ) 152 coords[k].x += delta_x; 153 154 if ( delta_y ) 155 for ( k = 0; k < n; k++ ) 156 coords[k].y += delta_y; 157 } 158 159 160#undef IS_HINTED 161#define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 ) 162 163 164 /*************************************************************************/ 165 /* */ 166 /* The following functions are used by default with TrueType fonts. */ 167 /* However, they can be replaced by alternatives if we need to support */ 168 /* TrueType-compressed formats (like MicroType) in the future. */ 169 /* */ 170 /*************************************************************************/ 171 172 FT_CALLBACK_DEF( FT_Error ) 173 TT_Access_Glyph_Frame( TT_Loader loader, 174 FT_UInt glyph_index, 175 FT_ULong offset, 176 FT_UInt byte_count ) 177 { 178 FT_Error error; 179 FT_Stream stream = loader->stream; 180 181 /* for non-debug mode */ 182 FT_UNUSED( glyph_index ); 183 184 185 FT_TRACE5(( "Glyph %ld\n", glyph_index )); 186 187 /* the following line sets the `error' variable through macros! */ 188 if ( FT_STREAM_SEEK( offset ) || FT_FRAME_ENTER( byte_count ) ) 189 return error; 190 191 loader->cursor = stream->cursor; 192 loader->limit = stream->limit; 193 194 return TT_Err_Ok; 195 } 196 197 198 FT_CALLBACK_DEF( void ) 199 TT_Forget_Glyph_Frame( TT_Loader loader ) 200 { 201 FT_Stream stream = loader->stream; 202 203 204 FT_FRAME_EXIT(); 205 } 206 207 208 FT_CALLBACK_DEF( FT_Error ) 209 TT_Load_Glyph_Header( TT_Loader loader ) 210 { 211 FT_Byte* p = loader->cursor; 212 FT_Byte* limit = loader->limit; 213 214 215 if ( p + 10 > limit ) 216 return TT_Err_Invalid_Outline; 217 218 loader->n_contours = FT_NEXT_SHORT( p ); 219 220 loader->bbox.xMin = FT_NEXT_SHORT( p ); 221 loader->bbox.yMin = FT_NEXT_SHORT( p ); 222 loader->bbox.xMax = FT_NEXT_SHORT( p ); 223 loader->bbox.yMax = FT_NEXT_SHORT( p ); 224 225 FT_TRACE5(( " # of contours: %d\n", loader->n_contours )); 226 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin, 227 loader->bbox.xMax )); 228 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin, 229 loader->bbox.yMax )); 230 loader->cursor = p; 231 232 return TT_Err_Ok; 233 } 234 235 236 FT_CALLBACK_DEF( FT_Error ) 237 TT_Load_Simple_Glyph( TT_Loader load ) 238 { 239 FT_Error error; 240 FT_Byte* p = load->cursor; 241 FT_Byte* limit = load->limit; 242 FT_GlyphLoader gloader = load->gloader; 243 FT_Int n_contours = load->n_contours; 244 FT_Outline* outline; 245 TT_Face face = (TT_Face)load->face; 246 FT_UShort n_ins; 247 FT_Int n_points; 248 249 FT_Byte *flag, *flag_limit; 250 FT_Byte c, count; 251 FT_Vector *vec, *vec_limit; 252 FT_Pos x; 253 FT_Short *cont, *cont_limit, prev_cont; 254 FT_Int xy_size = 0; 255 256 257 /* check that we can add the contours to the glyph */ 258 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, 0, n_contours ); 259 if ( error ) 260 goto Fail; 261 262 /* reading the contours' endpoints & number of points */ 263 cont = gloader->current.outline.contours; 264 cont_limit = cont + n_contours; 265 266 /* check space for contours array + instructions count */ 267 if ( n_contours >= 0xFFF || p + ( n_contours + 1 ) * 2 > limit ) 268 goto Invalid_Outline; 269 270 cont[0] = prev_cont = FT_NEXT_USHORT( p ); 271 for ( cont++; cont < cont_limit; cont++ ) 272 { 273 cont[0] = FT_NEXT_USHORT( p ); 274 if ( cont[0] <= prev_cont ) 275 { 276 /* unordered contours: this is invalid */ 277 error = FT_Err_Invalid_Table; 278 goto Fail; 279 } 280 prev_cont = cont[0]; 281 } 282 283 n_points = 0; 284 if ( n_contours > 0 ) 285 { 286 n_points = cont[-1] + 1; 287 if ( n_points < 0 ) 288 goto Invalid_Outline; 289 } 290 291 /* note that we will add four phantom points later */ 292 error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 ); 293 if ( error ) 294 goto Fail; 295 296 /* we'd better check the contours table right now */ 297 outline = &gloader->current.outline; 298 299 for ( cont = outline->contours + 1; cont < cont_limit; cont++ ) 300 if ( cont[-1] >= cont[0] ) 301 goto Invalid_Outline; 302 303 /* reading the bytecode instructions */ 304 load->glyph->control_len = 0; 305 load->glyph->control_data = 0; 306 307 if ( p + 2 > limit ) 308 goto Invalid_Outline; 309 310 n_ins = FT_NEXT_USHORT( p ); 311 312 FT_TRACE5(( " Instructions size: %u\n", n_ins )); 313 314 if ( n_ins > face->max_profile.maxSizeOfInstructions ) 315 { 316 FT_TRACE0(( "TT_Load_Simple_Glyph: Too many instructions (%d)\n", 317 n_ins )); 318 error = TT_Err_Too_Many_Hints; 319 goto Fail; 320 } 321 322 if ( ( limit - p ) < n_ins ) 323 { 324 FT_TRACE0(( "TT_Load_Simple_Glyph: Instruction count mismatch!\n" )); 325 error = TT_Err_Too_Many_Hints; 326 goto Fail; 327 } 328 329#ifdef TT_USE_BYTECODE_INTERPRETER 330 331 if ( IS_HINTED( load->load_flags ) ) 332 { 333 load->glyph->control_len = n_ins; 334 load->glyph->control_data = load->exec->glyphIns; 335 336 FT_MEM_COPY( load->exec->glyphIns, p, (FT_Long)n_ins ); 337 } 338 339#endif /* TT_USE_BYTECODE_INTERPRETER */ 340 341 p += n_ins; 342 343 /* reading the point tags */ 344 flag = (FT_Byte*)outline->tags; 345 flag_limit = flag + n_points; 346 347 FT_ASSERT( flag != NULL ); 348 349 while ( flag < flag_limit ) 350 { 351 if ( p + 1 > limit ) 352 goto Invalid_Outline; 353 354 *flag++ = c = FT_NEXT_BYTE( p ); 355 if ( c & 8 ) 356 { 357 if ( p + 1 > limit ) 358 goto Invalid_Outline; 359 360 count = FT_NEXT_BYTE( p ); 361 if ( flag + (FT_Int)count > flag_limit ) 362 goto Invalid_Outline; 363 364 for ( ; count > 0; count-- ) 365 *flag++ = c; 366 } 367 } 368 369 /* reading the X coordinates */ 370 371 vec = outline->points; 372 vec_limit = vec + n_points; 373 flag = (FT_Byte*)outline->tags; 374 x = 0; 375 376 if ( p + xy_size > limit ) 377 goto Invalid_Outline; 378 379 for ( ; vec < vec_limit; vec++, flag++ ) 380 { 381 FT_Pos y = 0; 382 FT_Byte f = *flag; 383 384 385 if ( f & 2 ) 386 { 387 if ( p + 1 > limit ) 388 goto Invalid_Outline; 389 390 y = (FT_Pos)FT_NEXT_BYTE( p ); 391 if ( ( f & 16 ) == 0 ) 392 y = -y; 393 } 394 else if ( ( f & 16 ) == 0 ) 395 { 396 if ( p + 2 > limit ) 397 goto Invalid_Outline; 398 399 y = (FT_Pos)FT_NEXT_SHORT( p ); 400 } 401 402 x += y; 403 vec->x = x; 404 *flag = f & ~( 2 | 16 ); 405 } 406 407 /* reading the Y coordinates */ 408 409 vec = gloader->current.outline.points; 410 vec_limit = vec + n_points; 411 flag = (FT_Byte*)outline->tags; 412 x = 0; 413 414 for ( ; vec < vec_limit; vec++, flag++ ) 415 { 416 FT_Pos y = 0; 417 FT_Byte f = *flag; 418 419 420 if ( f & 4 ) 421 { 422 if ( p + 1 > limit ) 423 goto Invalid_Outline; 424 425 y = (FT_Pos)FT_NEXT_BYTE( p ); 426 if ( ( f & 32 ) == 0 ) 427 y = -y; 428 } 429 else if ( ( f & 32 ) == 0 ) 430 { 431 if ( p + 2 > limit ) 432 goto Invalid_Outline; 433 434 y = (FT_Pos)FT_NEXT_SHORT( p ); 435 } 436 437 x += y; 438 vec->y = x; 439 *flag = f & FT_CURVE_TAG_ON; 440 } 441 442 outline->n_points = (FT_UShort)n_points; 443 outline->n_contours = (FT_Short) n_contours; 444 445 load->cursor = p; 446 447 Fail: 448 return error; 449 450 Invalid_Outline: 451 error = TT_Err_Invalid_Outline; 452 goto Fail; 453 } 454 455 456 FT_CALLBACK_DEF( FT_Error ) 457 TT_Load_Composite_Glyph( TT_Loader loader ) 458 { 459 FT_Error error; 460 FT_Byte* p = loader->cursor; 461 FT_Byte* limit = loader->limit; 462 FT_GlyphLoader gloader = loader->gloader; 463 FT_SubGlyph subglyph; 464 FT_UInt num_subglyphs; 465 466 467 num_subglyphs = 0; 468 469 do 470 { 471 FT_Fixed xx, xy, yy, yx; 472 FT_UInt count; 473 474 475 /* check that we can load a new subglyph */ 476 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs + 1 ); 477 if ( error ) 478 goto Fail; 479 480 /* check space */ 481 if ( p + 4 > limit ) 482 goto Invalid_Composite; 483 484 subglyph = gloader->current.subglyphs + num_subglyphs; 485 486 subglyph->arg1 = subglyph->arg2 = 0; 487 488 subglyph->flags = FT_NEXT_USHORT( p ); 489 subglyph->index = FT_NEXT_USHORT( p ); 490 491 /* check space */ 492 count = 2; 493 if ( subglyph->flags & ARGS_ARE_WORDS ) 494 count += 2; 495 if ( subglyph->flags & WE_HAVE_A_SCALE ) 496 count += 2; 497 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 498 count += 4; 499 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 500 count += 8; 501 502 if ( p + count > limit ) 503 goto Invalid_Composite; 504 505 /* read arguments */ 506 if ( subglyph->flags & ARGS_ARE_WORDS ) 507 { 508 subglyph->arg1 = FT_NEXT_SHORT( p ); 509 subglyph->arg2 = FT_NEXT_SHORT( p ); 510 } 511 else 512 { 513 subglyph->arg1 = FT_NEXT_CHAR( p ); 514 subglyph->arg2 = FT_NEXT_CHAR( p ); 515 } 516 517 /* read transform */ 518 xx = yy = 0x10000L; 519 xy = yx = 0; 520 521 if ( subglyph->flags & WE_HAVE_A_SCALE ) 522 { 523 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 524 yy = xx; 525 } 526 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 527 { 528 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 529 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 530 } 531 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 532 { 533 xx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 534 yx = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 535 xy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 536 yy = (FT_Fixed)FT_NEXT_SHORT( p ) << 2; 537 } 538 539 subglyph->transform.xx = xx; 540 subglyph->transform.xy = xy; 541 subglyph->transform.yx = yx; 542 subglyph->transform.yy = yy; 543 544 num_subglyphs++; 545 546 } while ( subglyph->flags & MORE_COMPONENTS ); 547 548 gloader->current.num_subglyphs = num_subglyphs; 549 550#ifdef TT_USE_BYTECODE_INTERPRETER 551 552 { 553 FT_Stream stream = loader->stream; 554 555 556 /* we must undo the FT_FRAME_ENTER in order to point to the */ 557 /* composite instructions, if we find some. */ 558 /* we will process them later... */ 559 /* */ 560 loader->ins_pos = (FT_ULong)( FT_STREAM_POS() + 561 p - limit ); 562 } 563 564#endif 565 566 loader->cursor = p; 567 568 Fail: 569 return error; 570 571 Invalid_Composite: 572 error = TT_Err_Invalid_Composite; 573 goto Fail; 574 } 575 576 577 FT_LOCAL_DEF( void ) 578 TT_Init_Glyph_Loading( TT_Face face ) 579 { 580 face->access_glyph_frame = TT_Access_Glyph_Frame; 581 face->read_glyph_header = TT_Load_Glyph_Header; 582 face->read_simple_glyph = TT_Load_Simple_Glyph; 583 face->read_composite_glyph = TT_Load_Composite_Glyph; 584 face->forget_glyph_frame = TT_Forget_Glyph_Frame; 585 } 586 587 588 static void 589 tt_prepare_zone( TT_GlyphZone zone, 590 FT_GlyphLoad load, 591 FT_UInt start_point, 592 FT_UInt start_contour ) 593 { 594 zone->n_points = (FT_UShort)( load->outline.n_points - start_point ); 595 zone->n_contours = (FT_Short) ( load->outline.n_contours - 596 start_contour ); 597 zone->org = load->extra_points + start_point; 598 zone->cur = load->outline.points + start_point; 599 zone->orus = load->extra_points2 + start_point; 600 zone->tags = (FT_Byte*)load->outline.tags + start_point; 601 zone->contours = (FT_UShort*)load->outline.contours + start_contour; 602 zone->first_point = (FT_UShort)start_point; 603 } 604 605 606 /*************************************************************************/ 607 /* */ 608 /* <Function> */ 609 /* TT_Hint_Glyph */ 610 /* */ 611 /* <Description> */ 612 /* Hint the glyph using the zone prepared by the caller. Note that */ 613 /* the zone is supposed to include four phantom points. */ 614 /* */ 615 static FT_Error 616 TT_Hint_Glyph( TT_Loader loader, 617 FT_Bool is_composite ) 618 { 619 TT_GlyphZone zone = &loader->zone; 620 FT_Pos origin; 621 622#ifdef TT_USE_BYTECODE_INTERPRETER 623 FT_UInt n_ins; 624#else 625 FT_UNUSED( is_composite ); 626#endif 627 628 629#ifdef TT_USE_BYTECODE_INTERPRETER 630 n_ins = loader->glyph->control_len; 631#endif 632 633 origin = zone->cur[zone->n_points - 4].x; 634 origin = FT_PIX_ROUND( origin ) - origin; 635 if ( origin ) 636 translate_array( zone->n_points, zone->cur, origin, 0 ); 637 638#ifdef TT_USE_BYTECODE_INTERPRETER 639 /* save original point position in org */ 640 if ( n_ins > 0 ) 641 FT_ARRAY_COPY( zone->org, zone->cur, zone->n_points ); 642#endif 643 644 /* round pp2 and pp4 */ 645 zone->cur[zone->n_points - 3].x = 646 FT_PIX_ROUND( zone->cur[zone->n_points - 3].x ); 647 zone->cur[zone->n_points - 1].y = 648 FT_PIX_ROUND( zone->cur[zone->n_points - 1].y ); 649 650#ifdef TT_USE_BYTECODE_INTERPRETER 651 652 if ( n_ins > 0 ) 653 { 654 FT_Bool debug; 655 FT_Error error; 656 657 658 error = TT_Set_CodeRange( loader->exec, tt_coderange_glyph, 659 loader->exec->glyphIns, n_ins ); 660 if ( error ) 661 return error; 662 663 loader->exec->is_composite = is_composite; 664 loader->exec->pts = *zone; 665 666 debug = FT_BOOL( !( loader->load_flags & FT_LOAD_NO_SCALE ) && 667 ((TT_Size)loader->size)->debug ); 668 669 error = TT_Run_Context( loader->exec, debug ); 670 if ( error && loader->exec->pedantic_hinting ) 671 return error; 672 } 673 674#endif 675 676 /* save glyph phantom points */ 677 if ( !loader->preserve_pps ) 678 { 679 loader->pp1 = zone->cur[zone->n_points - 4]; 680 loader->pp2 = zone->cur[zone->n_points - 3]; 681 loader->pp3 = zone->cur[zone->n_points - 2]; 682 loader->pp4 = zone->cur[zone->n_points - 1]; 683 } 684 685 return TT_Err_Ok; 686 } 687 688 689 /*************************************************************************/ 690 /* */ 691 /* <Function> */ 692 /* TT_Process_Simple_Glyph */ 693 /* */ 694 /* <Description> */ 695 /* Once a simple glyph has been loaded, it needs to be processed. */ 696 /* Usually, this means scaling and hinting through bytecode */ 697 /* interpretation. */ 698 /* */ 699 static FT_Error 700 TT_Process_Simple_Glyph( TT_Loader loader ) 701 { 702 FT_GlyphLoader gloader = loader->gloader; 703 FT_Error error = TT_Err_Ok; 704 FT_Outline* outline; 705 FT_UInt n_points; 706 707 708 outline = &gloader->current.outline; 709 n_points = outline->n_points; 710 711 /* set phantom points */ 712 713 outline->points[n_points ] = loader->pp1; 714 outline->points[n_points + 1] = loader->pp2; 715 outline->points[n_points + 2] = loader->pp3; 716 outline->points[n_points + 3] = loader->pp4; 717 718 outline->tags[n_points ] = 0; 719 outline->tags[n_points + 1] = 0; 720 outline->tags[n_points + 2] = 0; 721 outline->tags[n_points + 3] = 0; 722 723 n_points += 4; 724 725#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 726 727 if ( ((TT_Face)loader->face)->doblend ) 728 { 729 /* Deltas apply to the unscaled data. */ 730 FT_Vector* deltas; 731 FT_Memory memory = loader->face->memory; 732 FT_UInt i; 733 734 735 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face), 736 loader->glyph_index, 737 &deltas, 738 n_points ); 739 if ( error ) 740 return error; 741 742 for ( i = 0; i < n_points; ++i ) 743 { 744 outline->points[i].x += deltas[i].x; 745 outline->points[i].y += deltas[i].y; 746 } 747 748 FT_FREE( deltas ); 749 } 750 751#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 752 753 if ( IS_HINTED( loader->load_flags ) ) 754 { 755 tt_prepare_zone( &loader->zone, &gloader->current, 0, 0 ); 756 757 FT_ARRAY_COPY( loader->zone.orus, loader->zone.cur, 758 loader->zone.n_points + 4 ); 759 } 760 761 /* scale the glyph */ 762 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 763 { 764 FT_Vector* vec = outline->points; 765 FT_Vector* limit = outline->points + n_points; 766 FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale; 767 FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale; 768 769 770 for ( ; vec < limit; vec++ ) 771 { 772 vec->x = FT_MulFix( vec->x, x_scale ); 773 vec->y = FT_MulFix( vec->y, y_scale ); 774 } 775 776 loader->pp1 = outline->points[n_points - 4]; 777 loader->pp2 = outline->points[n_points - 3]; 778 loader->pp3 = outline->points[n_points - 2]; 779 loader->pp4 = outline->points[n_points - 1]; 780 } 781 782 if ( IS_HINTED( loader->load_flags ) ) 783 { 784 loader->zone.n_points += 4; 785 786 error = TT_Hint_Glyph( loader, 0 ); 787 } 788 789 return error; 790 } 791 792 793 /*************************************************************************/ 794 /* */ 795 /* <Function> */ 796 /* TT_Process_Composite_Component */ 797 /* */ 798 /* <Description> */ 799 /* Once a composite component has been loaded, it needs to be */ 800 /* processed. Usually, this means transforming and translating. */ 801 /* */ 802 static FT_Error 803 TT_Process_Composite_Component( TT_Loader loader, 804 FT_SubGlyph subglyph, 805 FT_UInt start_point, 806 FT_UInt num_base_points ) 807 { 808 FT_GlyphLoader gloader = loader->gloader; 809 FT_Vector* base_vec = gloader->base.outline.points; 810 FT_UInt num_points = gloader->base.outline.n_points; 811 FT_Bool have_scale; 812 FT_Pos x, y; 813 814 815 have_scale = FT_BOOL( subglyph->flags & ( WE_HAVE_A_SCALE | 816 WE_HAVE_AN_XY_SCALE | 817 WE_HAVE_A_2X2 ) ); 818 819 /* perform the transform required for this subglyph */ 820 if ( have_scale ) 821 { 822 FT_UInt i; 823 824 825 for ( i = num_base_points; i < num_points; i++ ) 826 FT_Vector_Transform( base_vec + i, &subglyph->transform ); 827 } 828 829 /* get offset */ 830 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) ) 831 { 832 FT_UInt k = subglyph->arg1; 833 FT_UInt l = subglyph->arg2; 834 FT_Vector* p1; 835 FT_Vector* p2; 836 837 838 /* match l-th point of the newly loaded component to the k-th point */ 839 /* of the previously loaded components. */ 840 841 /* change to the point numbers used by our outline */ 842 k += start_point; 843 l += num_base_points; 844 if ( k >= num_base_points || 845 l >= num_points ) 846 return TT_Err_Invalid_Composite; 847 848 p1 = gloader->base.outline.points + k; 849 p2 = gloader->base.outline.points + l; 850 851 x = p1->x - p2->x; 852 y = p1->y - p2->y; 853 } 854 else 855 { 856 x = subglyph->arg1; 857 y = subglyph->arg2; 858 859 if ( !x && !y ) 860 return TT_Err_Ok; 861 862 /* Use a default value dependent on */ 863 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */ 864 /* fonts which don't set the xxx_COMPONENT_OFFSET bit. */ 865 866 if ( have_scale && 867#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED 868 !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) ) 869#else 870 ( subglyph->flags & SCALED_COMPONENT_OFFSET ) ) 871#endif 872 { 873 874#if 0 875 876 /*************************************************************************/ 877 /* */ 878 /* This algorithm is what Apple documents. But it doesn't work. */ 879 /* */ 880 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx 881 : -subglyph->transform.xx; 882 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx 883 : -subglyph->transform.yx; 884 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy 885 : -subglyph->transform.xy; 886 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy 887 : -subglyph->transform.yy; 888 int m = a > b ? a : b; 889 int n = c > d ? c : d; 890 891 892 if ( a - b <= 33 && a - b >= -33 ) 893 m *= 2; 894 if ( c - d <= 33 && c - d >= -33 ) 895 n *= 2; 896 x = FT_MulFix( x, m ); 897 y = FT_MulFix( y, n ); 898 899#else /* 0 */ 900 901 /*************************************************************************/ 902 /* */ 903 /* This algorithm is a guess and works much better than the above. */ 904 /* */ 905 FT_Fixed mac_xscale = FT_SqrtFixed( 906 FT_MulFix( subglyph->transform.xx, 907 subglyph->transform.xx ) + 908 FT_MulFix( subglyph->transform.xy, 909 subglyph->transform.xy ) ); 910 FT_Fixed mac_yscale = FT_SqrtFixed( 911 FT_MulFix( subglyph->transform.yy, 912 subglyph->transform.yy ) + 913 FT_MulFix( subglyph->transform.yx, 914 subglyph->transform.yx ) ); 915 916 917 x = FT_MulFix( x, mac_xscale ); 918 y = FT_MulFix( y, mac_yscale ); 919 920#endif /* 0 */ 921 922 } 923 924 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 925 { 926 FT_Fixed x_scale = ((TT_Size)loader->size)->metrics.x_scale; 927 FT_Fixed y_scale = ((TT_Size)loader->size)->metrics.y_scale; 928 929 930 x = FT_MulFix( x, x_scale ); 931 y = FT_MulFix( y, y_scale ); 932 933 if ( subglyph->flags & ROUND_XY_TO_GRID ) 934 { 935 x = FT_PIX_ROUND( x ); 936 y = FT_PIX_ROUND( y ); 937 } 938 } 939 } 940 941 if ( x || y ) 942 translate_array( num_points - num_base_points, 943 base_vec + num_base_points, 944 x, y ); 945 946 return TT_Err_Ok; 947 } 948 949 950 /*************************************************************************/ 951 /* */ 952 /* <Function> */ 953 /* TT_Process_Composite_Glyph */ 954 /* */ 955 /* <Description> */ 956 /* This is slightly different from TT_Process_Simple_Glyph, in that */ 957 /* its sole purpose is to hint the glyph. Thus this function is */ 958 /* only available when bytecode interpreter is enabled. */ 959 /* */ 960 static FT_Error 961 TT_Process_Composite_Glyph( TT_Loader loader, 962 FT_UInt start_point, 963 FT_UInt start_contour ) 964 { 965 FT_Error error; 966 FT_Outline* outline; 967 FT_UInt i; 968 969 970 outline = &loader->gloader->base.outline; 971 972 /* make room for phantom points */ 973 error = FT_GLYPHLOADER_CHECK_POINTS( loader->gloader, 974 outline->n_points + 4, 975 0 ); 976 if ( error ) 977 return error; 978 979 outline->points[outline->n_points ] = loader->pp1; 980 outline->points[outline->n_points + 1] = loader->pp2; 981 outline->points[outline->n_points + 2] = loader->pp3; 982 outline->points[outline->n_points + 3] = loader->pp4; 983 984 outline->tags[outline->n_points ] = 0; 985 outline->tags[outline->n_points + 1] = 0; 986 outline->tags[outline->n_points + 2] = 0; 987 outline->tags[outline->n_points + 3] = 0; 988 989#ifdef TT_USE_BYTECODE_INTERPRETER 990 991 { 992 FT_Stream stream = loader->stream; 993 FT_UShort n_ins; 994 995 996 /* TT_Load_Composite_Glyph only gives us the offset of instructions */ 997 /* so we read them here */ 998 if ( FT_STREAM_SEEK( loader->ins_pos ) || 999 FT_READ_USHORT( n_ins ) ) 1000 return error; 1001 1002 FT_TRACE5(( " Instructions size = %d\n", n_ins )); 1003 1004 /* check it */ 1005 if ( n_ins > ((TT_Face)loader->face)->max_profile.maxSizeOfInstructions ) 1006 { 1007 FT_TRACE0(( "TT_Process_Composite_Glyph: Too many instructions (%d)\n", 1008 n_ins )); 1009 1010 return TT_Err_Too_Many_Hints; 1011 } 1012 else if ( n_ins == 0 ) 1013 return TT_Err_Ok; 1014 1015 if ( FT_STREAM_READ( loader->exec->glyphIns, n_ins ) ) 1016 return error; 1017 1018 loader->glyph->control_data = loader->exec->glyphIns; 1019 loader->glyph->control_len = n_ins; 1020 } 1021 1022#endif 1023 1024 tt_prepare_zone( &loader->zone, &loader->gloader->base, 1025 start_point, start_contour ); 1026 1027 /* Some points are likely touched during execution of */ 1028 /* instructions on components. So let's untouch them. */ 1029 for ( i = start_point; i < loader->zone.n_points; i++ ) 1030 loader->zone.tags[i] &= ~( FT_CURVE_TAG_TOUCH_X | 1031 FT_CURVE_TAG_TOUCH_Y ); 1032 1033 loader->zone.n_points += 4; 1034 1035 return TT_Hint_Glyph( loader, 1 ); 1036 } 1037 1038 1039 /* Calculate the four phantom points. */ 1040 /* The first two stand for horizontal origin and advance. */ 1041 /* The last two stand for vertical origin and advance. */ 1042#define TT_LOADER_SET_PP( loader ) \ 1043 do { \ 1044 (loader)->pp1.x = (loader)->bbox.xMin - (loader)->left_bearing; \ 1045 (loader)->pp1.y = 0; \ 1046 (loader)->pp2.x = (loader)->pp1.x + (loader)->advance; \ 1047 (loader)->pp2.y = 0; \ 1048 (loader)->pp3.x = 0; \ 1049 (loader)->pp3.y = (loader)->top_bearing + (loader)->bbox.yMax; \ 1050 (loader)->pp4.x = 0; \ 1051 (loader)->pp4.y = (loader)->pp3.y - (loader)->vadvance; \ 1052 } while ( 0 ) 1053 1054 1055 /*************************************************************************/ 1056 /* */ 1057 /* <Function> */ 1058 /* load_truetype_glyph */ 1059 /* */ 1060 /* <Description> */ 1061 /* Loads a given truetype glyph. Handles composites and uses a */ 1062 /* TT_Loader object. */ 1063 /* */ 1064 static FT_Error 1065 load_truetype_glyph( TT_Loader loader, 1066 FT_UInt glyph_index, 1067 FT_UInt recurse_count ) 1068 { 1069 FT_Error error; 1070 FT_Fixed x_scale, y_scale; 1071 FT_ULong offset; 1072 TT_Face face = (TT_Face)loader->face; 1073 FT_GlyphLoader gloader = loader->gloader; 1074 FT_Bool opened_frame = 0; 1075 1076#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1077 FT_Vector* deltas = NULL; 1078#endif 1079 1080#ifdef FT_CONFIG_OPTION_INCREMENTAL 1081 FT_StreamRec inc_stream; 1082 FT_Data glyph_data; 1083 FT_Bool glyph_data_loaded = 0; 1084#endif 1085 1086 1087 if ( recurse_count > face->max_profile.maxComponentDepth ) 1088 { 1089 error = TT_Err_Invalid_Composite; 1090 goto Exit; 1091 } 1092 1093 /* check glyph index */ 1094 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 1095 { 1096 error = TT_Err_Invalid_Glyph_Index; 1097 goto Exit; 1098 } 1099 1100 loader->glyph_index = glyph_index; 1101 1102 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1103 { 1104 x_scale = ((TT_Size)loader->size)->metrics.x_scale; 1105 y_scale = ((TT_Size)loader->size)->metrics.y_scale; 1106 } 1107 else 1108 { 1109 x_scale = 0x10000L; 1110 y_scale = 0x10000L; 1111 } 1112 1113 /* get metrics, horizontal and vertical */ 1114 { 1115 FT_Short left_bearing = 0, top_bearing = 0; 1116 FT_UShort advance_width = 0, advance_height = 0; 1117 1118 1119 Get_HMetrics( face, glyph_index, 1120 (FT_Bool)!( loader->load_flags & 1121 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ), 1122 &left_bearing, 1123 &advance_width ); 1124 Get_VMetrics( face, glyph_index, 1125 (FT_Bool)!( loader->load_flags & 1126 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ), 1127 &top_bearing, 1128 &advance_height ); 1129 1130#ifdef FT_CONFIG_OPTION_INCREMENTAL 1131 1132 /* If this is an incrementally loaded font see if there are */ 1133 /* overriding metrics for this glyph. */ 1134 if ( face->root.internal->incremental_interface && 1135 face->root.internal->incremental_interface->funcs->get_glyph_metrics ) 1136 { 1137 FT_Incremental_MetricsRec metrics; 1138 1139 1140 metrics.bearing_x = left_bearing; 1141 metrics.bearing_y = 0; 1142 metrics.advance = advance_width; 1143 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 1144 face->root.internal->incremental_interface->object, 1145 glyph_index, FALSE, &metrics ); 1146 if ( error ) 1147 goto Exit; 1148 left_bearing = (FT_Short)metrics.bearing_x; 1149 advance_width = (FT_UShort)metrics.advance; 1150 1151#if 0 1152 1153 /* GWW: Do I do the same for vertical metrics? */ 1154 metrics.bearing_x = 0; 1155 metrics.bearing_y = top_bearing; 1156 metrics.advance = advance_height; 1157 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 1158 face->root.internal->incremental_interface->object, 1159 glyph_index, TRUE, &metrics ); 1160 if ( error ) 1161 goto Exit; 1162 top_bearing = (FT_Short)metrics.bearing_y; 1163 advance_height = (FT_UShort)metrics.advance; 1164 1165#endif /* 0 */ 1166 1167 } 1168 1169#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1170 1171 loader->left_bearing = left_bearing; 1172 loader->advance = advance_width; 1173 loader->top_bearing = top_bearing; 1174 loader->vadvance = advance_height; 1175 1176 if ( !loader->linear_def ) 1177 { 1178 loader->linear_def = 1; 1179 loader->linear = advance_width; 1180 } 1181 } 1182 1183 /* Set `offset' to the start of the glyph relative to the start of */ 1184 /* the `glyf' table, and `byte_len' to the length of the glyph in */ 1185 /* bytes. */ 1186 1187#ifdef FT_CONFIG_OPTION_INCREMENTAL 1188 1189 /* If we are loading glyph data via the incremental interface, set */ 1190 /* the loader stream to a memory stream reading the data returned */ 1191 /* by the interface. */ 1192 if ( face->root.internal->incremental_interface ) 1193 { 1194 error = face->root.internal->incremental_interface->funcs->get_glyph_data( 1195 face->root.internal->incremental_interface->object, 1196 glyph_index, &glyph_data ); 1197 if ( error ) 1198 goto Exit; 1199 1200 glyph_data_loaded = 1; 1201 offset = 0; 1202 loader->byte_len = glyph_data.length; 1203 1204 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) ); 1205 FT_Stream_OpenMemory( &inc_stream, 1206 glyph_data.pointer, glyph_data.length ); 1207 1208 loader->stream = &inc_stream; 1209 } 1210 else 1211 1212#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1213 1214 offset = tt_face_get_location( face, glyph_index, 1215 (FT_UInt*)&loader->byte_len ); 1216 1217 if ( loader->byte_len == 0 ) 1218 { 1219 /* as described by Frederic Loyer, these are spaces or */ 1220 /* the unknown glyph. */ 1221 loader->bbox.xMin = 0; 1222 loader->bbox.xMax = 0; 1223 loader->bbox.yMin = 0; 1224 loader->bbox.yMax = 0; 1225 1226 TT_LOADER_SET_PP( loader ); 1227 1228#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1229 1230 if ( ((TT_Face)(loader->face))->doblend ) 1231 { 1232 /* this must be done before scaling */ 1233 FT_Memory memory = loader->face->memory; 1234 1235 1236 error = TT_Vary_Get_Glyph_Deltas( (TT_Face)(loader->face), 1237 glyph_index, &deltas, 4 ); 1238 if ( error ) 1239 goto Exit; 1240 1241 loader->pp1.x += deltas[0].x; loader->pp1.y += deltas[0].y; 1242 loader->pp2.x += deltas[1].x; loader->pp2.y += deltas[1].y; 1243 loader->pp3.x += deltas[2].x; loader->pp3.y += deltas[2].y; 1244 loader->pp4.x += deltas[3].x; loader->pp4.y += deltas[3].y; 1245 1246 FT_FREE( deltas ); 1247 } 1248 1249#endif 1250 1251 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1252 { 1253 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1254 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1255 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1256 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1257 } 1258 1259 error = TT_Err_Ok; 1260 goto Exit; 1261 } 1262 1263 error = face->access_glyph_frame( loader, glyph_index, 1264 loader->glyf_offset + offset, 1265 loader->byte_len ); 1266 if ( error ) 1267 goto Exit; 1268 1269 opened_frame = 1; 1270 1271 /* read first glyph header */ 1272 error = face->read_glyph_header( loader ); 1273 if ( error ) 1274 goto Exit; 1275 1276 TT_LOADER_SET_PP( loader ); 1277 1278 /***********************************************************************/ 1279 /***********************************************************************/ 1280 /***********************************************************************/ 1281 1282 /* if it is a simple glyph, load it */ 1283 1284 if ( loader->n_contours >= 0 ) 1285 { 1286 error = face->read_simple_glyph( loader ); 1287 if ( error ) 1288 goto Exit; 1289 1290 /* all data have been read */ 1291 face->forget_glyph_frame( loader ); 1292 opened_frame = 0; 1293 1294 error = TT_Process_Simple_Glyph( loader ); 1295 if ( error ) 1296 goto Exit; 1297 1298 FT_GlyphLoader_Add( gloader ); 1299 } 1300 1301 /***********************************************************************/ 1302 /***********************************************************************/ 1303 /***********************************************************************/ 1304 1305 /* otherwise, load a composite! */ 1306 else if ( loader->n_contours == -1 ) 1307 { 1308 FT_UInt start_point; 1309 FT_UInt start_contour; 1310 FT_ULong ins_pos; /* position of composite instructions, if any */ 1311 1312 1313 start_point = gloader->base.outline.n_points; 1314 start_contour = gloader->base.outline.n_contours; 1315 1316 /* for each subglyph, read composite header */ 1317 error = face->read_composite_glyph( loader ); 1318 if ( error ) 1319 goto Exit; 1320 1321 /* store the offset of instructions */ 1322 ins_pos = loader->ins_pos; 1323 1324 /* all data we need are read */ 1325 face->forget_glyph_frame( loader ); 1326 opened_frame = 0; 1327 1328#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT 1329 1330 if ( face->doblend ) 1331 { 1332 FT_Int i, limit; 1333 FT_SubGlyph subglyph; 1334 FT_Memory memory = face->root.memory; 1335 1336 1337 /* this provides additional offsets */ 1338 /* for each component's translation */ 1339 1340 if ( ( error = TT_Vary_Get_Glyph_Deltas( 1341 face, 1342 glyph_index, 1343 &deltas, 1344 gloader->current.num_subglyphs + 4 )) != 0 ) 1345 goto Exit; 1346 1347 subglyph = gloader->current.subglyphs + gloader->base.num_subglyphs; 1348 limit = gloader->current.num_subglyphs; 1349 1350 for ( i = 0; i < limit; ++i, ++subglyph ) 1351 { 1352 if ( subglyph->flags & ARGS_ARE_XY_VALUES ) 1353 { 1354 subglyph->arg1 += deltas[i].x; 1355 subglyph->arg2 += deltas[i].y; 1356 } 1357 } 1358 1359 loader->pp1.x += deltas[i + 0].x; loader->pp1.y += deltas[i + 0].y; 1360 loader->pp2.x += deltas[i + 1].x; loader->pp2.y += deltas[i + 1].y; 1361 loader->pp3.x += deltas[i + 2].x; loader->pp3.y += deltas[i + 2].y; 1362 loader->pp4.x += deltas[i + 3].x; loader->pp4.y += deltas[i + 3].y; 1363 1364 FT_FREE( deltas ); 1365 } 1366 1367#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ 1368 1369 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1370 { 1371 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 1372 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 1373 loader->pp3.y = FT_MulFix( loader->pp3.y, y_scale ); 1374 loader->pp4.y = FT_MulFix( loader->pp4.y, y_scale ); 1375 } 1376 1377 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ 1378 /* `as is' in the glyph slot (the client application will be */ 1379 /* responsible for interpreting these data)... */ 1380 if ( loader->load_flags & FT_LOAD_NO_RECURSE ) 1381 { 1382 FT_GlyphLoader_Add( gloader ); 1383 loader->glyph->format = FT_GLYPH_FORMAT_COMPOSITE; 1384 1385 goto Exit; 1386 } 1387 1388 /*********************************************************************/ 1389 /*********************************************************************/ 1390 /*********************************************************************/ 1391 1392 { 1393 FT_UInt n, num_base_points; 1394 FT_SubGlyph subglyph = 0; 1395 1396 FT_UInt num_points = start_point; 1397 FT_UInt num_subglyphs = gloader->current.num_subglyphs; 1398 FT_UInt num_base_subgs = gloader->base.num_subglyphs; 1399 1400 FT_Stream old_stream = loader->stream; 1401 1402 TT_GraphicsState saved_GS; 1403 1404 1405 if ( loader->exec ) 1406 saved_GS = loader->exec->GS; 1407 1408 FT_GlyphLoader_Add( gloader ); 1409 1410 /* read each subglyph independently */ 1411 for ( n = 0; n < num_subglyphs; n++ ) 1412 { 1413 FT_Vector pp[4]; 1414 1415 1416 /* reinitialize graphics state */ 1417 if ( loader->exec ) 1418 loader->exec->GS = saved_GS; 1419 1420 /* Each time we call load_truetype_glyph in this loop, the */ 1421 /* value of `gloader.base.subglyphs' can change due to table */ 1422 /* reallocations. We thus need to recompute the subglyph */ 1423 /* pointer on each iteration. */ 1424 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1425 1426 pp[0] = loader->pp1; 1427 pp[1] = loader->pp2; 1428 pp[2] = loader->pp3; 1429 pp[3] = loader->pp4; 1430 1431 num_base_points = gloader->base.outline.n_points; 1432 1433 error = load_truetype_glyph( loader, subglyph->index, 1434 recurse_count + 1 ); 1435 if ( error ) 1436 goto Exit; 1437 1438 /* restore subglyph pointer */ 1439 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1440 1441 if ( !( subglyph->flags & USE_MY_METRICS ) ) 1442 { 1443 loader->pp1 = pp[0]; 1444 loader->pp2 = pp[1]; 1445 loader->pp3 = pp[2]; 1446 loader->pp4 = pp[3]; 1447 } 1448 1449 num_points = gloader->base.outline.n_points; 1450 1451 if ( num_points == num_base_points ) 1452 continue; 1453 1454 /* gloader->base.outline consists of three parts: */ 1455 /* 0 -(1)-> start_point -(2)-> num_base_points -(3)-> n_points. */ 1456 /* */ 1457 /* (1): exists from the beginning */ 1458 /* (2): components that have been loaded so far */ 1459 /* (3): the newly loaded component */ 1460 TT_Process_Composite_Component( loader, subglyph, start_point, 1461 num_base_points ); 1462 } 1463 1464 loader->stream = old_stream; 1465 1466 /* process the glyph */ 1467 loader->ins_pos = ins_pos; 1468 if ( IS_HINTED( loader->load_flags ) && 1469 1470#ifdef TT_USE_BYTECODE_INTERPRETER 1471 1472 subglyph->flags & WE_HAVE_INSTR && 1473 1474#endif 1475 1476 num_points > start_point ) 1477 TT_Process_Composite_Glyph( loader, start_point, start_contour ); 1478 1479 } 1480 } 1481 else 1482 { 1483 /* invalid composite count (negative but not -1) */ 1484 error = TT_Err_Invalid_Outline; 1485 goto Exit; 1486 } 1487 1488 /***********************************************************************/ 1489 /***********************************************************************/ 1490 /***********************************************************************/ 1491 1492 Exit: 1493 1494 if ( opened_frame ) 1495 face->forget_glyph_frame( loader ); 1496 1497#ifdef FT_CONFIG_OPTION_INCREMENTAL 1498 1499 if ( glyph_data_loaded ) 1500 face->root.internal->incremental_interface->funcs->free_glyph_data( 1501 face->root.internal->incremental_interface->object, 1502 &glyph_data ); 1503 1504#endif 1505 1506 return error; 1507 } 1508 1509 1510 static FT_Error 1511 compute_glyph_metrics( TT_Loader loader, 1512 FT_UInt glyph_index ) 1513 { 1514 FT_BBox bbox; 1515 TT_Face face = (TT_Face)loader->face; 1516 FT_Fixed y_scale; 1517 TT_GlyphSlot glyph = loader->glyph; 1518 TT_Size size = (TT_Size)loader->size; 1519 1520 1521 y_scale = 0x10000L; 1522 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1523 y_scale = size->root.metrics.y_scale; 1524 1525 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE ) 1526 FT_Outline_Get_CBox( &glyph->outline, &bbox ); 1527 else 1528 bbox = loader->bbox; 1529 1530 /* get the device-independent horizontal advance; it is scaled later */ 1531 /* by the base layer. */ 1532 { 1533 FT_Pos advance = loader->linear; 1534 1535 1536 /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */ 1537 /* correctly support DynaLab fonts, which have an incorrect */ 1538 /* `advance_Width_Max' field! It is used, to my knowledge, */ 1539 /* exclusively in the X-TrueType font server. */ 1540 /* */ 1541 if ( face->postscript.isFixedPitch && 1542 ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 ) 1543 advance = face->horizontal.advance_Width_Max; 1544 1545 /* we need to return the advance in font units in linearHoriAdvance, */ 1546 /* it will be scaled later by the base layer. */ 1547 glyph->linearHoriAdvance = advance; 1548 } 1549 1550 glyph->metrics.horiBearingX = bbox.xMin; 1551 glyph->metrics.horiBearingY = bbox.yMax; 1552 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; 1553 1554 /* Now take care of vertical metrics. In the case where there is */ 1555 /* no vertical information within the font (relatively common), make */ 1556 /* up some metrics by `hand'... */ 1557 1558 { 1559 FT_Pos top; /* scaled vertical top side bearing */ 1560 FT_Pos advance; /* scaled vertical advance height */ 1561 1562 1563 /* Get the unscaled top bearing and advance height. */ 1564 if ( face->vertical_info && 1565 face->vertical.number_Of_VMetrics > 0 ) 1566 { 1567 top = (FT_Short)FT_DivFix( loader->pp3.y - bbox.yMax, 1568 y_scale ); 1569 1570 if ( loader->pp3.y <= loader->pp4.y ) 1571 advance = 0; 1572 else 1573 advance = (FT_UShort)FT_DivFix( loader->pp3.y - loader->pp4.y, 1574 y_scale ); 1575 } 1576 else 1577 { 1578 FT_Pos height; 1579 1580 1581 /* XXX Compute top side bearing and advance height in */ 1582 /* Get_VMetrics instead of here. */ 1583 1584 /* NOTE: The OS/2 values are the only `portable' ones, */ 1585 /* which is why we use them, if there is an OS/2 */ 1586 /* table in the font. Otherwise, we use the */ 1587 /* values defined in the horizontal header. */ 1588 1589 height = (FT_Short)FT_DivFix( bbox.yMax - bbox.yMin, 1590 y_scale ); 1591 if ( face->os2.version != 0xFFFFU ) 1592 advance = (FT_Pos)( face->os2.sTypoAscender - 1593 face->os2.sTypoDescender ); 1594 else 1595 advance = (FT_Pos)( face->horizontal.Ascender - 1596 face->horizontal.Descender ); 1597 1598 top = ( advance - height ) / 2; 1599 } 1600 1601#ifdef FT_CONFIG_OPTION_INCREMENTAL 1602 { 1603 FT_Incremental_InterfaceRec* incr; 1604 FT_Incremental_MetricsRec metrics; 1605 FT_Error error; 1606 1607 1608 incr = face->root.internal->incremental_interface; 1609 1610 /* If this is an incrementally loaded font see if there are */ 1611 /* overriding metrics for this glyph. */ 1612 if ( incr && incr->funcs->get_glyph_metrics ) 1613 { 1614 metrics.bearing_x = 0; 1615 metrics.bearing_y = top; 1616 metrics.advance = advance; 1617 1618 error = incr->funcs->get_glyph_metrics( incr->object, 1619 glyph_index, 1620 TRUE, 1621 &metrics ); 1622 if ( error ) 1623 return error; 1624 1625 top = metrics.bearing_y; 1626 advance = metrics.advance; 1627 } 1628 } 1629 1630 /* GWW: Do vertical metrics get loaded incrementally too? */ 1631 1632#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1633 1634 glyph->linearVertAdvance = advance; 1635 1636 /* scale the metrics */ 1637 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1638 { 1639 top = FT_MulFix( top, y_scale ); 1640 advance = FT_MulFix( advance, y_scale ); 1641 } 1642 1643 /* XXX: for now, we have no better algorithm for the lsb, but it */ 1644 /* should work fine. */ 1645 /* */ 1646 glyph->metrics.vertBearingX = ( bbox.xMin - bbox.xMax ) / 2; 1647 glyph->metrics.vertBearingY = top; 1648 glyph->metrics.vertAdvance = advance; 1649 } 1650 1651 /* adjust advance width to the value contained in the hdmx table */ 1652 if ( !face->postscript.isFixedPitch && 1653 IS_HINTED( loader->load_flags ) ) 1654 { 1655 FT_Byte* widthp; 1656 1657 1658 widthp = tt_face_get_device_metrics( face, 1659 size->root.metrics.x_ppem, 1660 glyph_index ); 1661 1662 if ( widthp ) 1663 glyph->metrics.horiAdvance = *widthp << 6; 1664 } 1665 1666 /* set glyph dimensions */ 1667 glyph->metrics.width = bbox.xMax - bbox.xMin; 1668 glyph->metrics.height = bbox.yMax - bbox.yMin; 1669 1670 return 0; 1671 } 1672 1673 1674#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 1675 1676 static FT_Error 1677 load_sbit_image( TT_Size size, 1678 TT_GlyphSlot glyph, 1679 FT_UInt glyph_index, 1680 FT_Int32 load_flags ) 1681 { 1682 TT_Face face; 1683 SFNT_Service sfnt; 1684 FT_Stream stream; 1685 FT_Error error; 1686 TT_SBit_MetricsRec metrics; 1687 1688 1689 face = (TT_Face)glyph->face; 1690 sfnt = (SFNT_Service)face->sfnt; 1691 stream = face->root.stream; 1692 1693 error = sfnt->load_sbit_image( face, 1694 size->strike_index, 1695 glyph_index, 1696 (FT_Int)load_flags, 1697 stream, 1698 &glyph->bitmap, 1699 &metrics ); 1700 if ( !error ) 1701 { 1702 glyph->outline.n_points = 0; 1703 glyph->outline.n_contours = 0; 1704 1705 glyph->metrics.width = (FT_Pos)metrics.width << 6; 1706 glyph->metrics.height = (FT_Pos)metrics.height << 6; 1707 1708 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; 1709 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; 1710 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; 1711 1712 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; 1713 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; 1714 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; 1715 1716 glyph->format = FT_GLYPH_FORMAT_BITMAP; 1717 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 1718 { 1719 glyph->bitmap_left = metrics.vertBearingX; 1720 glyph->bitmap_top = metrics.vertBearingY; 1721 } 1722 else 1723 { 1724 glyph->bitmap_left = metrics.horiBearingX; 1725 glyph->bitmap_top = metrics.horiBearingY; 1726 } 1727 } 1728 1729 return error; 1730 } 1731 1732#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 1733 1734 1735 static FT_Error 1736 tt_loader_init( TT_Loader loader, 1737 TT_Size size, 1738 TT_GlyphSlot glyph, 1739 FT_Int32 load_flags ) 1740 { 1741 TT_Face face; 1742 FT_Stream stream; 1743 1744 1745 face = (TT_Face)glyph->face; 1746 stream = face->root.stream; 1747 1748 FT_MEM_ZERO( loader, sizeof ( TT_LoaderRec ) ); 1749 1750#ifdef TT_USE_BYTECODE_INTERPRETER 1751 1752 /* load execution context */ 1753 if ( IS_HINTED( load_flags ) ) 1754 { 1755 TT_ExecContext exec; 1756 FT_Bool grayscale; 1757 1758 1759 if ( !size->cvt_ready ) 1760 { 1761 FT_Error error = tt_size_ready_bytecode( size ); 1762 if ( error ) 1763 return error; 1764 } 1765 1766 /* query new execution context */ 1767 exec = size->debug ? size->context 1768 : ( (TT_Driver)FT_FACE_DRIVER( face ) )->context; 1769 if ( !exec ) 1770 return TT_Err_Could_Not_Find_Context; 1771 1772 grayscale = 1773 FT_BOOL( FT_LOAD_TARGET_MODE( load_flags ) != FT_RENDER_MODE_MONO ); 1774 1775 TT_Load_Context( exec, face, size ); 1776 1777 /* a change from mono to grayscale rendering (and vice versa) */ 1778 /* requires a re-execution of the CVT program */ 1779 if ( grayscale != exec->grayscale ) 1780 { 1781 FT_UInt i; 1782 1783 1784 exec->grayscale = grayscale; 1785 1786 for ( i = 0; i < size->cvt_size; i++ ) 1787 size->cvt[i] = FT_MulFix( face->cvt[i], size->ttmetrics.scale ); 1788 tt_size_run_prep( size ); 1789 } 1790 1791 /* see whether the cvt program has disabled hinting */ 1792 if ( exec->GS.instruct_control & 1 ) 1793 load_flags |= FT_LOAD_NO_HINTING; 1794 1795 /* load default graphics state -- if needed */ 1796 if ( exec->GS.instruct_control & 2 ) 1797 exec->GS = tt_default_graphics_state; 1798 1799 exec->pedantic_hinting = FT_BOOL( load_flags & FT_LOAD_PEDANTIC ); 1800 loader->exec = exec; 1801 loader->instructions = exec->glyphIns; 1802 } 1803 1804#endif /* TT_USE_BYTECODE_INTERPRETER */ 1805 1806 /* seek to the beginning of the glyph table -- for Type 42 fonts */ 1807 /* the table might be accessed from a Postscript stream or something */ 1808 /* else... */ 1809 1810#ifdef FT_CONFIG_OPTION_INCREMENTAL 1811 1812 if ( face->root.internal->incremental_interface ) 1813 loader->glyf_offset = 0; 1814 else 1815 1816#endif 1817 1818 { 1819 FT_Error error = face->goto_table( face, TTAG_glyf, stream, 0 ); 1820 1821 1822 if ( error ) 1823 { 1824 FT_ERROR(( "TT_Load_Glyph: could not access glyph table\n" )); 1825 return error; 1826 } 1827 loader->glyf_offset = FT_STREAM_POS(); 1828 } 1829 1830 /* get face's glyph loader */ 1831 { 1832 FT_GlyphLoader gloader = glyph->internal->loader; 1833 1834 1835 FT_GlyphLoader_Rewind( gloader ); 1836 loader->gloader = gloader; 1837 } 1838 1839 loader->load_flags = load_flags; 1840 1841 loader->face = (FT_Face)face; 1842 loader->size = (FT_Size)size; 1843 loader->glyph = (FT_GlyphSlot)glyph; 1844 loader->stream = stream; 1845 1846 return TT_Err_Ok; 1847 } 1848 1849 1850 /*************************************************************************/ 1851 /* */ 1852 /* <Function> */ 1853 /* TT_Load_Glyph */ 1854 /* */ 1855 /* <Description> */ 1856 /* A function used to load a single glyph within a given glyph slot, */ 1857 /* for a given size. */ 1858 /* */ 1859 /* <Input> */ 1860 /* glyph :: A handle to a target slot object where the glyph */ 1861 /* will be loaded. */ 1862 /* */ 1863 /* size :: A handle to the source face size at which the glyph */ 1864 /* must be scaled/loaded. */ 1865 /* */ 1866 /* glyph_index :: The index of the glyph in the font file. */ 1867 /* */ 1868 /* load_flags :: A flag indicating what to load for this glyph. The */ 1869 /* FT_LOAD_XXX constants can be used to control the */ 1870 /* glyph loading process (e.g., whether the outline */ 1871 /* should be scaled, whether to load bitmaps or not, */ 1872 /* whether to hint the outline, etc). */ 1873 /* */ 1874 /* <Return> */ 1875 /* FreeType error code. 0 means success. */ 1876 /* */ 1877 FT_LOCAL_DEF( FT_Error ) 1878 TT_Load_Glyph( TT_Size size, 1879 TT_GlyphSlot glyph, 1880 FT_UInt glyph_index, 1881 FT_Int32 load_flags ) 1882 { 1883 TT_Face face; 1884 FT_Error error; 1885 TT_LoaderRec loader; 1886 1887 1888 face = (TT_Face)glyph->face; 1889 error = TT_Err_Ok; 1890 1891#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 1892 1893 /* try to load embedded bitmap if any */ 1894 /* */ 1895 /* XXX: The convention should be emphasized in */ 1896 /* the documents because it can be confusing. */ 1897 if ( size->strike_index != 0xFFFFFFFFUL && 1898 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) 1899 { 1900 error = load_sbit_image( size, glyph, glyph_index, load_flags ); 1901 if ( !error ) 1902 return TT_Err_Ok; 1903 } 1904 1905#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 1906 1907 /* if FT_LOAD_NO_SCALE is not set, `ttmetrics' must be valid */ 1908 if ( !( load_flags & FT_LOAD_NO_SCALE ) && !size->ttmetrics.valid ) 1909 return TT_Err_Invalid_Size_Handle; 1910 1911 if ( load_flags & FT_LOAD_SBITS_ONLY ) 1912 return TT_Err_Invalid_Argument; 1913 1914 error = tt_loader_init( &loader, size, glyph, load_flags ); 1915 if ( error ) 1916 return error; 1917 1918 glyph->format = FT_GLYPH_FORMAT_OUTLINE; 1919 glyph->num_subglyphs = 0; 1920 glyph->outline.flags = 0; 1921 1922 /* main loading loop */ 1923 error = load_truetype_glyph( &loader, glyph_index, 0 ); 1924 if ( !error ) 1925 { 1926 if ( glyph->format == FT_GLYPH_FORMAT_COMPOSITE ) 1927 { 1928 glyph->num_subglyphs = loader.gloader->base.num_subglyphs; 1929 glyph->subglyphs = loader.gloader->base.subglyphs; 1930 } 1931 else 1932 { 1933 glyph->outline = loader.gloader->base.outline; 1934 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS; 1935 1936 /* In case bit 1 of the `flags' field in the `head' table isn't */ 1937 /* set, translate array so that (0,0) is the glyph's origin. */ 1938 if ( ( face->header.Flags & 2 ) == 0 && loader.pp1.x ) 1939 FT_Outline_Translate( &glyph->outline, -loader.pp1.x, 0 ); 1940 } 1941 1942 compute_glyph_metrics( &loader, glyph_index ); 1943 } 1944 1945 /* Set the `high precision' bit flag. */ 1946 /* This is _critical_ to get correct output for monochrome */ 1947 /* TrueType glyphs at all sizes using the bytecode interpreter. */ 1948 /* */ 1949 if ( !( load_flags & FT_LOAD_NO_SCALE ) && 1950 size->root.metrics.y_ppem < 24 ) 1951 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 1952 1953 return error; 1954 } 1955 1956 1957/* END */ 1958