ttgload.c revision 46960df7b377494426ebccb8b99c65aca558c2ae
1/***************************************************************************/ 2/* */ 3/* ttgload.c */ 4/* */ 5/* TrueType Glyph Loader (body). */ 6/* */ 7/* Copyright 1996-2001, 2002 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 if ( recurse_count >= TT_MAX_COMPOSITE_RECURSE ) 783 { 784 error = TT_Err_Invalid_Composite; 785 goto Exit; 786 } 787 788 /* check glyph index */ 789 if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) 790 { 791 error = TT_Err_Invalid_Glyph_Index; 792 goto Exit; 793 } 794 795 loader->glyph_index = glyph_index; 796 num_points = 0; 797 798 x_scale = 0x10000L; 799 y_scale = 0x10000L; 800 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 801 { 802 x_scale = loader->size->metrics.x_scale; 803 y_scale = loader->size->metrics.y_scale; 804 } 805 806 /* get horizontal metrics */ 807 { 808 FT_Short left_bearing = 0; 809 FT_UShort advance_width = 0; 810 811 Get_HMetrics( face, glyph_index, 812 (FT_Bool)!( loader->load_flags & 813 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ), 814 &left_bearing, 815 &advance_width ); 816 817#ifdef FT_CONFIG_OPTION_INCREMENTAL 818 819 /* If this is an incrementally loaded font see if there are */ 820 /* overriding metrics for this glyph. */ 821 if ( face->root.internal->incremental_interface && 822 face->root.internal->incremental_interface->funcs->get_glyph_metrics ) 823 { 824 FT_Incremental_MetricsRec metrics; 825 826 metrics.bearing_x = left_bearing; 827 metrics.bearing_y = 0; 828 metrics.advance = advance_width; 829 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( 830 face->root.internal->incremental_interface->object, 831 glyph_index, FALSE, &metrics ); 832 if ( error ) 833 goto Exit; 834 left_bearing = (FT_Short)metrics.bearing_x; 835 advance_width = (FT_UShort)metrics.advance; 836 } 837 838#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 839 840 loader->left_bearing = left_bearing; 841 loader->advance = advance_width; 842 843 if ( !loader->linear_def ) 844 { 845 loader->linear_def = 1; 846 loader->linear = advance_width; 847 } 848 } 849 850#ifdef FT_CONFIG_OPTION_INCREMENTAL 851 852 /* Set `offset' to the start of the glyph program relative to the */ 853 /* start of the 'glyf' table, and `count' to the length of the */ 854 /* glyph program in bytes. */ 855 /* */ 856 /* If we are loading glyph data via the incremental interface, set */ 857 /* the loader stream to a memory stream reading the data returned */ 858 /* by the interface. */ 859 860 if ( face->root.internal->incremental_interface ) 861 { 862 error = face->root.internal->incremental_interface->funcs->get_glyph_data( 863 face->root.internal->incremental_interface->object, 864 glyph_index, &glyph_data ); 865 if ( error ) 866 goto Exit; 867 868 glyph_data_loaded = 1; 869 offset = 0; 870 count = glyph_data.length; 871 872 FT_MEM_ZERO( &inc_stream, sizeof ( inc_stream ) ); 873 FT_Stream_OpenMemory( &inc_stream, 874 glyph_data.pointer, glyph_data.length ); 875 876 loader->stream = &inc_stream; 877 } 878 else 879 880#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 881 882 { 883 offset = face->glyph_locations[glyph_index]; 884 count = 0; 885 886 if ( glyph_index < (FT_UInt)face->num_locations - 1 ) 887 count = face->glyph_locations[glyph_index + 1] - offset; 888 } 889 890 if ( count == 0 ) 891 { 892 /* as described by Frederic Loyer, these are spaces, and */ 893 /* not the unknown glyph. */ 894 loader->bbox.xMin = 0; 895 loader->bbox.xMax = 0; 896 loader->bbox.yMin = 0; 897 loader->bbox.yMax = 0; 898 899 loader->pp1.x = 0; 900 loader->pp2.x = loader->advance; 901 902 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 903 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 904 905#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 906 907 if ( loader->exec ) 908 loader->exec->glyphSize = 0; 909 910#endif 911 912 error = TT_Err_Ok; 913 goto Exit; 914 } 915 916 loader->byte_len = (FT_Int)count; 917 918 offset = loader->glyf_offset + offset; 919 920 /* access glyph frame */ 921 error = face->access_glyph_frame( loader, glyph_index, offset, count ); 922 if ( error ) 923 goto Exit; 924 925 opened_frame = 1; 926 927 /* read first glyph header */ 928 error = face->read_glyph_header( loader ); 929 if ( error ) 930 goto Fail; 931 932 contours_count = loader->n_contours; 933 934 count -= 10; 935 936 loader->pp1.x = loader->bbox.xMin - loader->left_bearing; 937 loader->pp1.y = 0; 938 loader->pp2.x = loader->pp1.x + loader->advance; 939 loader->pp2.y = 0; 940 941 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 942 { 943 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 944 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 945 } 946 947 /***********************************************************************/ 948 /***********************************************************************/ 949 /***********************************************************************/ 950 951 /* if it is a simple glyph, load it */ 952 953 if ( contours_count >= 0 ) 954 { 955 /* check that we can add the contours to the glyph */ 956 error = FT_GlyphLoader_CheckPoints( gloader, 0, contours_count ); 957 if ( error ) 958 goto Fail; 959 960 error = face->read_simple_glyph( loader ); 961 if ( error ) 962 goto Fail; 963 964#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 965 966 { 967 TT_Size size = (TT_Size)loader->size; 968 969 970 error = TT_Process_Simple_Glyph( loader, 971 (FT_Bool)( size && size->debug ) ); 972 } 973 974#else 975 976 error = TT_Process_Simple_Glyph( loader, 0 ); 977 978#endif 979 980 if ( error ) 981 goto Fail; 982 983 FT_GlyphLoader_Add( gloader ); 984 985 /* Note: We could have put the simple loader source there */ 986 /* but the code is fat enough already :-) */ 987 } 988 989 /***********************************************************************/ 990 /***********************************************************************/ 991 /***********************************************************************/ 992 993 /* otherwise, load a composite! */ 994 else if ( contours_count == -1 ) 995 { 996 TT_GlyphSlot glyph = (TT_GlyphSlot)loader->glyph; 997 FT_UInt start_point; 998#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 999 FT_UInt start_contour; 1000 FT_ULong ins_pos; /* position of composite instructions, if any */ 1001#endif 1002 1003 1004 /* for each subglyph, read composite header */ 1005 start_point = gloader->base.outline.n_points; 1006#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1007 start_contour = gloader->base.outline.n_contours; 1008#endif 1009 1010 error = face->read_composite_glyph( loader ); 1011 if ( error ) 1012 goto Fail; 1013 1014#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1015 ins_pos = loader->ins_pos; 1016#endif 1017 face->forget_glyph_frame( loader ); 1018 opened_frame = 0; 1019 1020 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ 1021 /* `as is' in the glyph slot (the client application will be */ 1022 /* responsible for interpreting these data)... */ 1023 /* */ 1024 if ( loader->load_flags & FT_LOAD_NO_RECURSE ) 1025 { 1026 /* set up remaining glyph fields */ 1027 FT_GlyphLoader_Add( gloader ); 1028 1029 glyph->num_subglyphs = gloader->base.num_subglyphs; 1030 glyph->format = FT_GLYPH_FORMAT_COMPOSITE; 1031 glyph->subglyphs = gloader->base.subglyphs; 1032 1033 goto Exit; 1034 } 1035 1036 /*********************************************************************/ 1037 /*********************************************************************/ 1038 /*********************************************************************/ 1039 1040 /* Now, read each subglyph independently. */ 1041 { 1042 FT_Int n, num_base_points, num_new_points; 1043 FT_SubGlyph subglyph = 0; 1044 1045 FT_UInt num_subglyphs = gloader->current.num_subglyphs; 1046 FT_UInt num_base_subgs = gloader->base.num_subglyphs; 1047 1048 1049 FT_GlyphLoader_Add( gloader ); 1050 1051 for ( n = 0; n < (FT_Int)num_subglyphs; n++ ) 1052 { 1053 FT_Vector pp1, pp2; 1054 FT_Pos x, y; 1055 1056 1057 /* Each time we call load_truetype_glyph in this loop, the */ 1058 /* value of `gloader.base.subglyphs' can change due to table */ 1059 /* reallocations. We thus need to recompute the subglyph */ 1060 /* pointer on each iteration. */ 1061 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1062 1063 pp1 = loader->pp1; 1064 pp2 = loader->pp2; 1065 1066 num_base_points = gloader->base.outline.n_points; 1067 1068 error = load_truetype_glyph( loader, subglyph->index, 1069 recurse_count+1 ); 1070 if ( error ) 1071 goto Fail; 1072 1073 /* restore subglyph pointer */ 1074 subglyph = gloader->base.subglyphs + num_base_subgs + n; 1075 1076 if ( subglyph->flags & USE_MY_METRICS ) 1077 { 1078 pp1 = loader->pp1; 1079 pp2 = loader->pp2; 1080 } 1081 else 1082 { 1083 loader->pp1 = pp1; 1084 loader->pp2 = pp2; 1085 } 1086 1087 num_points = gloader->base.outline.n_points; 1088 1089 num_new_points = num_points - num_base_points; 1090 1091 /* now perform the transform required for this subglyph */ 1092 1093 if ( subglyph->flags & ( WE_HAVE_A_SCALE | 1094 WE_HAVE_AN_XY_SCALE | 1095 WE_HAVE_A_2X2 ) ) 1096 { 1097 FT_Vector* cur = gloader->base.outline.points + 1098 num_base_points; 1099 FT_Vector* org = gloader->base.extra_points + 1100 num_base_points; 1101 FT_Vector* limit = cur + num_new_points; 1102 1103 1104 for ( ; cur < limit; cur++, org++ ) 1105 { 1106 FT_Vector_Transform( cur, &subglyph->transform ); 1107 FT_Vector_Transform( org, &subglyph->transform ); 1108 } 1109 } 1110 1111 /* apply offset */ 1112 1113 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) ) 1114 { 1115 FT_UInt k = subglyph->arg1; 1116 FT_UInt l = subglyph->arg2; 1117 FT_Vector* p1; 1118 FT_Vector* p2; 1119 1120 1121 if ( start_point + k >= (FT_UInt)num_base_points || 1122 l >= (FT_UInt)num_new_points ) 1123 { 1124 error = TT_Err_Invalid_Composite; 1125 goto Fail; 1126 } 1127 1128 l += num_base_points; 1129 1130 p1 = gloader->base.outline.points + start_point + k; 1131 p2 = gloader->base.outline.points + start_point + l; 1132 1133 x = p1->x - p2->x; 1134 y = p1->y - p2->y; 1135 } 1136 else 1137 { 1138 x = subglyph->arg1; 1139 y = subglyph->arg2; 1140 1141 /* Use a default value dependent on */ 1142 /* TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED. This is useful for old TT */ 1143 /* fonts which don't set the xxx_COMPONENT_OFFSET bit. */ 1144 1145#ifdef TT_CONFIG_OPTION_COMPONENT_OFFSET_SCALED 1146 if ( !( subglyph->flags & UNSCALED_COMPONENT_OFFSET ) && 1147#else 1148 if ( ( subglyph->flags & SCALED_COMPONENT_OFFSET ) && 1149#endif 1150 ( subglyph->flags & ( WE_HAVE_A_SCALE | 1151 WE_HAVE_AN_XY_SCALE | 1152 WE_HAVE_A_2X2 )) ) 1153 { 1154#if 0 1155 1156 /*************************************************************************/ 1157 /* */ 1158 /* This algorithm is what Apple documents. But it doesn't work. */ 1159 /* */ 1160 int a = subglyph->transform.xx > 0 ? subglyph->transform.xx 1161 : -subglyph->transform.xx; 1162 int b = subglyph->transform.yx > 0 ? subglyph->transform.yx 1163 : -subglyph->transform.yx; 1164 int c = subglyph->transform.xy > 0 ? subglyph->transform.xy 1165 : -subglyph->transform.xy; 1166 int d = subglyph->transform.yy > 0 ? subglyph->transform.yy 1167 : -subglyph->transform.yy; 1168 int m = a > b ? a : b; 1169 int n = c > d ? c : d; 1170 1171 1172 if ( a - b <= 33 && a - b >= -33 ) 1173 m *= 2; 1174 if ( c - d <= 33 && c - d >= -33 ) 1175 n *= 2; 1176 x = FT_MulFix( x, m ); 1177 y = FT_MulFix( y, n ); 1178 1179#else /* 0 */ 1180 1181 /*************************************************************************/ 1182 /* */ 1183 /* This algorithm is a guess and works much better than the above. */ 1184 /* */ 1185 int mac_xscale = FT_SqrtFixed( 1186 FT_MulFix( subglyph->transform.xx, 1187 subglyph->transform.xx ) + 1188 FT_MulFix( subglyph->transform.xy, 1189 subglyph->transform.xy) ); 1190 int mac_yscale = FT_SqrtFixed( 1191 FT_MulFix( subglyph->transform.yy, 1192 subglyph->transform.yy ) + 1193 FT_MulFix( subglyph->transform.yx, 1194 subglyph->transform.yx ) ); 1195 1196 1197 x = FT_MulFix( x, mac_xscale ); 1198 y = FT_MulFix( y, mac_yscale ); 1199#endif /* 0 */ 1200 1201 } 1202 1203 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1204 { 1205 x = FT_MulFix( x, x_scale ); 1206 y = FT_MulFix( y, y_scale ); 1207 1208 if ( subglyph->flags & ROUND_XY_TO_GRID ) 1209 { 1210 x = ( x + 32 ) & -64; 1211 y = ( y + 32 ) & -64; 1212 } 1213 } 1214 } 1215 1216 if ( x || y ) 1217 { 1218 translate_array( num_new_points, 1219 gloader->base.outline.points + num_base_points, 1220 x, y ); 1221 1222 translate_array( num_new_points, 1223 gloader->base.extra_points + num_base_points, 1224 x, y ); 1225 } 1226 } 1227 1228 /*******************************************************************/ 1229 /*******************************************************************/ 1230 /*******************************************************************/ 1231 1232 /* we have finished loading all sub-glyphs; now, look for */ 1233 /* instructions for this composite! */ 1234 1235#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1236 1237 if ( num_subglyphs > 0 && 1238 loader->exec && 1239 ins_pos > 0 && 1240 subglyph->flags & WE_HAVE_INSTR ) 1241 { 1242 FT_UShort n_ins; 1243 TT_ExecContext exec = loader->exec; 1244 TT_GlyphZone pts; 1245 FT_Vector* pp1; 1246 1247 1248 /* read size of instructions */ 1249 if ( FT_STREAM_SEEK( ins_pos ) || 1250 FT_READ_USHORT( n_ins ) ) 1251 goto Fail; 1252 FT_TRACE5(( " Instructions size = %d\n", n_ins )); 1253 1254 /* in some fonts? */ 1255 if ( n_ins == 0xFFFFU ) 1256 n_ins = 0; 1257 1258 /* check it */ 1259 if ( n_ins > face->max_profile.maxSizeOfInstructions ) 1260 { 1261 FT_TRACE0(( "Too many instructions (%d) in composite glyph %ld\n", 1262 n_ins, subglyph->index )); 1263 error = TT_Err_Too_Many_Hints; 1264 goto Fail; 1265 } 1266 1267 /* read the instructions */ 1268 if ( FT_STREAM_READ( exec->glyphIns, n_ins ) ) 1269 goto Fail; 1270 1271 glyph->control_data = exec->glyphIns; 1272 glyph->control_len = n_ins; 1273 1274 error = TT_Set_CodeRange( exec, 1275 tt_coderange_glyph, 1276 exec->glyphIns, 1277 n_ins ); 1278 if ( error ) 1279 goto Fail; 1280 1281 /* prepare the execution context */ 1282 tt_prepare_zone( &exec->pts, &gloader->base, 1283 start_point, start_contour ); 1284 pts = &exec->pts; 1285 1286 pts->n_points = (short)(num_points + 2); 1287 pts->n_contours = gloader->base.outline.n_contours; 1288 1289 /* add phantom points */ 1290 pp1 = pts->cur + num_points; 1291 pp1[0] = loader->pp1; 1292 pp1[1] = loader->pp2; 1293 1294 pts->tags[num_points ] = 0; 1295 pts->tags[num_points + 1] = 0; 1296 1297 /* if hinting, round the phantom points */ 1298 if ( IS_HINTED( loader->load_flags ) ) 1299 { 1300 pp1[0].x = ( ( loader->pp1.x + 32 ) & -64 ); 1301 pp1[1].x = ( ( loader->pp2.x + 32 ) & -64 ); 1302 } 1303 1304 { 1305 FT_UInt k; 1306 1307 1308 for ( k = 0; k < num_points; k++ ) 1309 pts->tags[k] &= FT_CURVE_TAG_ON; 1310 } 1311 1312 cur_to_org( num_points + 2, pts ); 1313 1314 /* now consider hinting */ 1315 if ( IS_HINTED( loader->load_flags ) && n_ins > 0 ) 1316 { 1317 exec->is_composite = TRUE; 1318 exec->pedantic_hinting = 1319 (FT_Bool)( loader->load_flags & FT_LOAD_PEDANTIC ); 1320 error = TT_Run_Context( exec, ((TT_Size)loader->size)->debug ); 1321 if ( error && exec->pedantic_hinting ) 1322 goto Fail; 1323 } 1324 1325 /* save glyph origin and advance points */ 1326 loader->pp1 = pp1[0]; 1327 loader->pp2 = pp1[1]; 1328 } 1329 1330#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1331 1332 } 1333 /* end of composite loading */ 1334 } 1335 else 1336 { 1337 /* invalid composite count ( negative but not -1 ) */ 1338 error = TT_Err_Invalid_Outline; 1339 goto Fail; 1340 } 1341 1342 /***********************************************************************/ 1343 /***********************************************************************/ 1344 /***********************************************************************/ 1345 1346 Fail: 1347 if ( opened_frame ) 1348 face->forget_glyph_frame( loader ); 1349 1350 Exit: 1351 1352#ifdef FT_CONFIG_OPTION_INCREMENTAL 1353 if ( glyph_data_loaded ) 1354 face->root.internal->incremental_interface->funcs->free_glyph_data( 1355 face->root.internal->incremental_interface->object, 1356 &glyph_data ); 1357#endif 1358 1359 return error; 1360 } 1361 1362 1363 static FT_Error 1364 compute_glyph_metrics( TT_Loader loader, 1365 FT_UInt glyph_index ) 1366 { 1367 FT_BBox bbox; 1368 TT_Face face = (TT_Face)loader->face; 1369 FT_Fixed y_scale; 1370 TT_GlyphSlot glyph = loader->glyph; 1371 TT_Size size = (TT_Size)loader->size; 1372 1373 1374 y_scale = 0x10000L; 1375 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1376 y_scale = size->root.metrics.y_scale; 1377 1378 if ( glyph->format != FT_GLYPH_FORMAT_COMPOSITE ) 1379 { 1380 glyph->outline.flags &= ~FT_OUTLINE_SINGLE_PASS; 1381 1382 /* copy outline to our glyph slot */ 1383 FT_GlyphLoader_CopyPoints( glyph->internal->loader, loader->gloader ); 1384 glyph->outline = glyph->internal->loader->base.outline; 1385 1386 /* translate array so that (0,0) is the glyph's origin */ 1387 FT_Outline_Translate( &glyph->outline, -loader->pp1.x, 0 ); 1388 1389 FT_Outline_Get_CBox( &glyph->outline, &bbox ); 1390 1391 if ( IS_HINTED( loader->load_flags ) ) 1392 { 1393 /* grid-fit the bounding box */ 1394 bbox.xMin &= -64; 1395 bbox.yMin &= -64; 1396 bbox.xMax = ( bbox.xMax + 63 ) & -64; 1397 bbox.yMax = ( bbox.yMax + 63 ) & -64; 1398 } 1399 } 1400 else 1401 bbox = loader->bbox; 1402 1403 /* get the device-independent horizontal advance. It is scaled later */ 1404 /* by the base layer. */ 1405 { 1406 FT_Pos advance = loader->linear; 1407 1408 1409 /* the flag FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH was introduced to */ 1410 /* correctly support DynaLab fonts, which have an incorrect */ 1411 /* `advance_Width_Max' field! It is used, to my knowledge, */ 1412 /* exclusively in the X-TrueType font server. */ 1413 /* */ 1414 if ( face->postscript.isFixedPitch && 1415 ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 ) 1416 advance = face->horizontal.advance_Width_Max; 1417 1418 /* we need to return the advance in font units in linearHoriAdvance, */ 1419 /* it will be scaled later by the base layer. */ 1420 glyph->linearHoriAdvance = advance; 1421 } 1422 1423 glyph->metrics.horiBearingX = bbox.xMin; 1424 glyph->metrics.horiBearingY = bbox.yMax; 1425 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; 1426 1427 /* don't forget to hint the advance when we need to */ 1428 if ( IS_HINTED( loader->load_flags ) ) 1429 glyph->metrics.horiAdvance = ( glyph->metrics.horiAdvance + 32 ) & -64; 1430 1431 /* Now take care of vertical metrics. In the case where there is */ 1432 /* no vertical information within the font (relatively common), make */ 1433 /* up some metrics by `hand'... */ 1434 1435 { 1436 FT_Short top_bearing; /* vertical top side bearing (EM units) */ 1437 FT_UShort advance_height; /* vertical advance height (EM units) */ 1438 1439 FT_Pos left; /* scaled vertical left side bearing */ 1440 FT_Pos top; /* scaled vertical top side bearing */ 1441 FT_Pos advance; /* scaled vertical advance height */ 1442 1443 /* Get the unscaled top bearing and advance height. */ 1444 if ( face->vertical_info && 1445 face->vertical.number_Of_VMetrics > 0 ) 1446 { 1447 /* Don't assume that both the vertical header and vertical */ 1448 /* metrics are present in the same font :-) */ 1449 1450 TT_Get_Metrics( (TT_HoriHeader*)&face->vertical, 1451 glyph_index, 1452 &top_bearing, 1453 &advance_height ); 1454 } 1455 else 1456 { 1457 /* Make up the distances from the horizontal header. */ 1458 1459 /* NOTE: The OS/2 values are the only `portable' ones, */ 1460 /* which is why we use them, if there is an OS/2 */ 1461 /* table in the font. Otherwise, we use the */ 1462 /* values defined in the horizontal header. */ 1463 /* */ 1464 /* NOTE2: The sTypoDescender is negative, which is why */ 1465 /* we compute the baseline-to-baseline distance */ 1466 /* here with: */ 1467 /* ascender - descender + linegap */ 1468 /* */ 1469 if ( face->os2.version != 0xFFFFU ) 1470 { 1471 top_bearing = (FT_Short)( face->os2.sTypoLineGap / 2 ); 1472 advance_height = (FT_UShort)( face->os2.sTypoAscender - 1473 face->os2.sTypoDescender + 1474 face->os2.sTypoLineGap ); 1475 } 1476 else 1477 { 1478 top_bearing = (FT_Short)( face->horizontal.Line_Gap / 2 ); 1479 advance_height = (FT_UShort)( face->horizontal.Ascender + 1480 face->horizontal.Descender + 1481 face->horizontal.Line_Gap ); 1482 } 1483 } 1484 1485#ifdef FT_CONFIG_OPTION_INCREMENTAL 1486 1487 /* If this is an incrementally loaded font see if there are */ 1488 /* overriding metrics for this glyph. */ 1489 if ( face->root.internal->incremental_interface && 1490 face->root.internal->incremental_interface->funcs->get_glyph_metrics ) 1491 { 1492 FT_Incremental_MetricsRec metrics; 1493 FT_Error error = 0; 1494 1495 metrics.bearing_x = 0; 1496 metrics.bearing_y = top_bearing; 1497 metrics.advance = advance_height; 1498 error = 1499 face->root.internal->incremental_interface->funcs->get_glyph_metrics( 1500 face->root.internal->incremental_interface->object, 1501 glyph_index, TRUE, &metrics ); 1502 1503 if ( error ) 1504 return error; 1505 1506 top_bearing = (FT_Short)metrics.bearing_y; 1507 advance_height = (FT_UShort)metrics.advance; 1508 } 1509 1510#endif /* FT_CONFIG_OPTION_INCREMENTAL */ 1511 1512 /* We must adjust the top_bearing value from the bounding box given */ 1513 /* in the glyph header to the bounding box calculated with */ 1514 /* FT_Get_Outline_CBox(). */ 1515 1516 /* scale the metrics */ 1517 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1518 { 1519 top = FT_MulFix( top_bearing + loader->bbox.yMax, y_scale ) 1520 - bbox.yMax; 1521 advance = FT_MulFix( advance_height, y_scale ); 1522 } 1523 else 1524 { 1525 top = top_bearing + loader->bbox.yMax - bbox.yMax; 1526 advance = advance_height; 1527 } 1528 1529 /* set the advance height in design units. It is scaled later by */ 1530 /* the base layer. */ 1531 glyph->linearVertAdvance = advance_height; 1532 1533 /* XXX: for now, we have no better algorithm for the lsb, but it */ 1534 /* should work fine. */ 1535 /* */ 1536 left = ( bbox.xMin - bbox.xMax ) / 2; 1537 1538 /* grid-fit them if necessary */ 1539 if ( IS_HINTED( loader->load_flags ) ) 1540 { 1541 left &= -64; 1542 top = ( top + 63 ) & -64; 1543 advance = ( advance + 32 ) & -64; 1544 } 1545 1546 glyph->metrics.vertBearingX = left; 1547 glyph->metrics.vertBearingY = top; 1548 glyph->metrics.vertAdvance = advance; 1549 } 1550 1551 /* adjust advance width to the value contained in the hdmx table */ 1552 if ( !face->postscript.isFixedPitch && size && 1553 IS_HINTED( loader->load_flags ) ) 1554 { 1555 FT_Byte* widths = Get_Advance_Widths( face, 1556 size->root.metrics.x_ppem ); 1557 1558 1559 if ( widths ) 1560 glyph->metrics.horiAdvance = widths[glyph_index] << 6; 1561 } 1562 1563 /* set glyph dimensions */ 1564 glyph->metrics.width = bbox.xMax - bbox.xMin; 1565 glyph->metrics.height = bbox.yMax - bbox.yMin; 1566 1567 return 0; 1568 } 1569 1570 1571 /*************************************************************************/ 1572 /* */ 1573 /* <Function> */ 1574 /* TT_Load_Glyph */ 1575 /* */ 1576 /* <Description> */ 1577 /* A function used to load a single glyph within a given glyph slot, */ 1578 /* for a given size. */ 1579 /* */ 1580 /* <Input> */ 1581 /* glyph :: A handle to a target slot object where the glyph */ 1582 /* will be loaded. */ 1583 /* */ 1584 /* size :: A handle to the source face size at which the glyph */ 1585 /* must be scaled/loaded. */ 1586 /* */ 1587 /* glyph_index :: The index of the glyph in the font file. */ 1588 /* */ 1589 /* load_flags :: A flag indicating what to load for this glyph. The */ 1590 /* FT_LOAD_XXX constants can be used to control the */ 1591 /* glyph loading process (e.g., whether the outline */ 1592 /* should be scaled, whether to load bitmaps or not, */ 1593 /* whether to hint the outline, etc). */ 1594 /* */ 1595 /* <Return> */ 1596 /* FreeType error code. 0 means success. */ 1597 /* */ 1598 FT_LOCAL_DEF( FT_Error ) 1599 TT_Load_Glyph( TT_Size size, 1600 TT_GlyphSlot glyph, 1601 FT_UShort glyph_index, 1602 FT_Int32 load_flags ) 1603 { 1604 SFNT_Service sfnt; 1605 TT_Face face; 1606 FT_Stream stream; 1607 FT_Error error; 1608 TT_LoaderRec loader; 1609 1610 1611 face = (TT_Face)glyph->face; 1612 sfnt = (SFNT_Service)face->sfnt; 1613 stream = face->root.stream; 1614 error = 0; 1615 1616 if ( !size || ( load_flags & FT_LOAD_NO_SCALE ) || 1617 ( load_flags & FT_LOAD_NO_RECURSE ) ) 1618 { 1619 size = NULL; 1620 load_flags |= FT_LOAD_NO_SCALE | 1621 FT_LOAD_NO_HINTING | 1622 FT_LOAD_NO_BITMAP; 1623 } 1624 1625 glyph->num_subglyphs = 0; 1626 1627#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 1628 1629 /* try to load embedded bitmap if any */ 1630 /* */ 1631 /* XXX: The convention should be emphasized in */ 1632 /* the documents because it can be confusing. */ 1633 if ( size && 1634 size->strike_index != 0xFFFFU && 1635 sfnt->load_sbits && 1636 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) 1637 1638 { 1639 TT_SBit_MetricsRec metrics; 1640 1641 1642 error = sfnt->load_sbit_image( face, 1643 size->strike_index, 1644 glyph_index, 1645 load_flags, 1646 stream, 1647 &glyph->bitmap, 1648 &metrics ); 1649 if ( !error ) 1650 { 1651 glyph->outline.n_points = 0; 1652 glyph->outline.n_contours = 0; 1653 1654 glyph->metrics.width = (FT_Pos)metrics.width << 6; 1655 glyph->metrics.height = (FT_Pos)metrics.height << 6; 1656 1657 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; 1658 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; 1659 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; 1660 1661 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; 1662 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; 1663 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; 1664 1665 glyph->format = FT_GLYPH_FORMAT_BITMAP; 1666 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 1667 { 1668 glyph->bitmap_left = metrics.vertBearingX; 1669 glyph->bitmap_top = metrics.vertBearingY; 1670 } 1671 else 1672 { 1673 glyph->bitmap_left = metrics.horiBearingX; 1674 glyph->bitmap_top = metrics.horiBearingY; 1675 } 1676 return error; 1677 } 1678 } 1679 1680#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 1681 1682 /* return immediately if we only want the embedded bitmaps */ 1683 if ( load_flags & FT_LOAD_SBITS_ONLY ) 1684 return FT_Err_Invalid_Argument; 1685 1686 /* seek to the beginning of the glyph table. For Type 42 fonts */ 1687 /* the table might be accessed from a Postscript stream or something */ 1688 /* else... */ 1689 1690#ifdef FT_CONFIG_OPTION_INCREMENTAL 1691 1692 /* Don't look for the glyph table if this is an incremental font. */ 1693 if ( !face->root.internal->incremental_interface ) 1694 1695#endif 1696 1697 { 1698 error = face->goto_table( face, TTAG_glyf, stream, 0 ); 1699 if ( error ) 1700 { 1701 FT_ERROR(( "TT_Load_Glyph: could not access glyph table\n" )); 1702 goto Exit; 1703 } 1704 } 1705 1706 FT_MEM_ZERO( &loader, sizeof ( loader ) ); 1707 1708 /* update the glyph zone bounds */ 1709 { 1710 FT_GlyphLoader gloader = FT_FACE_DRIVER(face)->glyph_loader; 1711 1712 1713 loader.gloader = gloader; 1714 1715 FT_GlyphLoader_Rewind( gloader ); 1716 1717 tt_prepare_zone( &loader.zone, &gloader->base, 0, 0 ); 1718 tt_prepare_zone( &loader.base, &gloader->base, 0, 0 ); 1719 } 1720 1721#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1722 1723 if ( size ) 1724 { 1725 /* query new execution context */ 1726 loader.exec = size->debug ? size->context : TT_New_Context( face ); 1727 if ( !loader.exec ) 1728 return TT_Err_Could_Not_Find_Context; 1729 1730 TT_Load_Context( loader.exec, face, size ); 1731 loader.instructions = loader.exec->glyphIns; 1732 1733 /* load default graphics state - if needed */ 1734 if ( size->GS.instruct_control & 2 ) 1735 loader.exec->GS = tt_default_graphics_state; 1736 } 1737 1738#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1739 1740 /* clear all outline flags, except the `owner' one */ 1741 glyph->outline.flags = 0; 1742 1743 /* let's initialize the rest of our loader now */ 1744 1745 loader.load_flags = load_flags; 1746 1747 loader.face = (FT_Face)face; 1748 loader.size = (FT_Size)size; 1749 loader.glyph = (FT_GlyphSlot)glyph; 1750 loader.stream = stream; 1751 1752#ifdef FT_CONFIG_OPTION_INCREMENTAL 1753 1754 if ( face->root.internal->incremental_interface ) 1755 loader.glyf_offset = 0; 1756 else 1757 1758#endif 1759 1760 loader.glyf_offset = FT_STREAM_POS(); 1761 1762#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1763 1764 /* if the cvt program has disabled hinting, the argument */ 1765 /* is ignored. */ 1766 if ( size && ( size->GS.instruct_control & 1 ) ) 1767 loader.load_flags |= FT_LOAD_NO_HINTING; 1768 1769#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1770 1771 /* Main loading loop */ 1772 glyph->format = FT_GLYPH_FORMAT_OUTLINE; 1773 glyph->num_subglyphs = 0; 1774 1775 error = load_truetype_glyph( &loader, glyph_index, 0 ); 1776 if ( !error ) 1777 compute_glyph_metrics( &loader, glyph_index ); 1778 1779#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1780 1781 if ( !size || !size->debug ) 1782 TT_Done_Context( loader.exec ); 1783 1784#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1785 1786 /* Set the `high precision' bit flag. */ 1787 /* This is _critical_ to get correct output for monochrome */ 1788 /* TrueType glyphs at all sizes using the bytecode interpreter. */ 1789 /* */ 1790 if ( size && size->root.metrics.y_ppem < 24 ) 1791 glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; 1792 1793 Exit: 1794 return error; 1795 } 1796 1797 1798/* END */ 1799