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