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