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