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