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