ttgload.c revision 90f68b727208b3a27b7a8c4694960d88d81570cf
1/***************************************************************************/ 2/* */ 3/* ttgload.c */ 4/* */ 5/* TrueType Glyph Loader (body). */ 6/* */ 7/* Copyright 1996-2000 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 <freetype/internal/ftdebug.h> 20#include <freetype/internal/ftcalc.h> 21#include <freetype/internal/ftstream.h> 22#include <freetype/internal/sfnt.h> 23#include <freetype/tttags.h> 24 25 26#include <ttgload.h> 27 28#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 29#include <ttinterp.h> 30#endif 31 32 33 /*************************************************************************/ 34 /* */ 35 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 36 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 37 /* messages during execution. */ 38 /* */ 39#undef FT_COMPONENT 40#define FT_COMPONENT trace_ttgload 41 42 43 /*************************************************************************/ 44 /* */ 45 /* Composite font flags. */ 46 /* */ 47#define ARGS_ARE_WORDS 0x001 48#define ARGS_ARE_XY_VALUES 0x002 49#define ROUND_XY_TO_GRID 0x004 50#define WE_HAVE_A_SCALE 0x008 51/* reserved 0x010 */ 52#define MORE_COMPONENTS 0x020 53#define WE_HAVE_AN_XY_SCALE 0x040 54#define WE_HAVE_A_2X2 0x080 55#define WE_HAVE_INSTR 0x100 56#define USE_MY_METRICS 0x200 57 58 59 60 /*************************************************************************/ 61 /* */ 62 /* <Function> */ 63 /* TT_Get_Metrics */ 64 /* */ 65 /* <Description> */ 66 /* Returns the horizontal or vertical metrics in font units for a */ 67 /* given glyph. The metrics are the left side bearing (resp. top */ 68 /* side bearing) and advance width (resp. advance height). */ 69 /* */ 70 /* <Input> */ 71 /* header :: A pointer to either the horizontal or vertical metrics */ 72 /* structure. */ 73 /* */ 74 /* index :: The glyph index. */ 75 /* */ 76 /* <Output> */ 77 /* bearing :: The bearing, either left side or top side. */ 78 /* */ 79 /* advance :: The advance width resp. advance height. */ 80 /* */ 81 /* <Note> */ 82 /* This function will much probably move to another component in the */ 83 /* near future, but I haven't decided which yet. */ 84 /* */ 85 LOCAL_FUNC 86 void TT_Get_Metrics( TT_HoriHeader* header, 87 FT_UInt index, 88 FT_Short* bearing, 89 FT_UShort* advance ) 90 { 91 TT_LongMetrics* longs_m; 92 FT_UShort k = header->number_Of_HMetrics; 93 94 95 if ( index < k ) 96 { 97 longs_m = (TT_LongMetrics*)header->long_metrics + index; 98 *bearing = longs_m->bearing; 99 *advance = longs_m->advance; 100 } 101 else 102 { 103 *bearing = ((TT_ShortMetrics*)header->short_metrics)[index - k]; 104 *advance = ((TT_LongMetrics*)header->long_metrics)[k - 1].advance; 105 } 106 } 107 108 109 /*************************************************************************/ 110 /* */ 111 /* Returns the horizontal metrics in font units for a given glyph. */ 112 /* If `check' is true, take care of monospaced fonts by returning the */ 113 /* advance width maximum. */ 114 /* */ 115 static 116 void Get_HMetrics( TT_Face face, 117 FT_UInt index, 118 FT_Bool check, 119 FT_Short* lsb, 120 FT_UShort* aw ) 121 { 122 TT_Get_Metrics( &face->horizontal, index, lsb, aw ); 123 124 if ( check && face->postscript.isFixedPitch ) 125 *aw = face->horizontal.advance_Width_Max; 126 } 127 128 129 /*************************************************************************/ 130 /* */ 131 /* Returns the advance width table for a given pixel size if it is */ 132 /* found in the font's `hdmx' table (if any). */ 133 /* */ 134 static 135 FT_Byte* Get_Advance_Widths( TT_Face face, 136 FT_UShort ppem ) 137 { 138 FT_UShort n; 139 140 for ( n = 0; n < face->hdmx.num_records; n++ ) 141 if ( face->hdmx.records[n].ppem == ppem ) 142 return face->hdmx.records[n].widths; 143 144 return NULL; 145 } 146 147 148#define cur_to_org( n, zone ) \ 149 MEM_Copy( (zone)->org, (zone)->cur, n * sizeof ( FT_Vector ) ) 150 151#define org_to_cur( n, zone ) \ 152 MEM_Copy( (zone)->cur, (zone)->org, n * sizeof ( FT_Vector ) ) 153 154 155 /*************************************************************************/ 156 /* */ 157 /* Translates an array of coordinates. */ 158 /* */ 159 static 160 void translate_array( FT_UInt n, 161 FT_Vector* coords, 162 FT_Pos delta_x, 163 FT_Pos delta_y ) 164 { 165 FT_UInt k; 166 167 if ( delta_x ) 168 for ( k = 0; k < n; k++ ) 169 coords[k].x += delta_x; 170 171 if ( delta_y ) 172 for ( k = 0; k < n; k++ ) 173 coords[k].y += delta_y; 174 } 175 176 177 static void tt_prepare_zone( TT_GlyphZone* zone, 178 FT_GlyphLoad* load, 179 FT_UInt start_point, 180 FT_UInt start_contour ) 181 { 182 zone->n_points = load->outline.n_points - start_point; 183 zone->n_contours = load->outline.n_contours - start_contour; 184 zone->org = load->extra_points + start_point; 185 zone->cur = load->outline.points + start_point; 186 zone->tags = (FT_Byte*)load->outline.tags + start_point; 187 zone->contours = (FT_UShort*)load->outline.contours + start_contour; 188 } 189 190 191#undef IS_HINTED 192#define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 ) 193 194 195 /*************************************************************************/ 196 /* */ 197 /* The following functions are used by default with TrueType fonts. */ 198 /* However, they can be replaced by alternatives if we need to support */ 199 /* TrueType-compressed formats (like MicroType) in the future.. */ 200 /* */ 201 202 static 203 FT_Error TT_Access_Glyph_Frame( TT_Loader* loader, 204 FT_UInt glyph_index, 205 FT_ULong offset, 206 FT_UInt byte_count ) 207 { 208 FT_Error error; 209 FT_Stream stream = loader->stream; 210 211 /* the following line sets the 'error' variable through macros !! */ 212 (void)( FILE_Seek( offset ) || ACCESS_Frame( byte_count ) ); 213 214 FT_TRACE5(( "Glyph %ld\n", glyph_index )); 215 return error; 216 } 217 218 219 220 static 221 void TT_Forget_Glyph_Frame( TT_Loader* loader ) 222 { 223 FT_Stream stream = loader->stream; 224 FORGET_Frame(); 225 } 226 227 228 229 static 230 FT_Error TT_Load_Glyph_Header( TT_Loader* loader ) 231 { 232 FT_Stream stream = loader->stream; 233 234 loader->n_contours = GET_Short(); 235 236 loader->bbox.xMin = GET_Short(); 237 loader->bbox.yMin = GET_Short(); 238 loader->bbox.xMax = GET_Short(); 239 loader->bbox.yMax = GET_Short(); 240 241 FT_TRACE5(( " # of contours: %d\n", loader->n_contours )); 242 FT_TRACE5(( " xMin: %4d xMax: %4d\n", loader->bbox.xMin, 243 loader->bbox.xMax )); 244 FT_TRACE5(( " yMin: %4d yMax: %4d\n", loader->bbox.yMin, 245 loader->bbox.yMax )); 246 247 return FT_Err_Ok; 248 } 249 250 251 252 static 253 FT_Error TT_Load_Simple_Glyph( TT_Loader* load, 254 FT_Int n_contours ) 255 { 256 FT_Error error; 257 FT_Stream stream = load->stream; 258 FT_GlyphLoader* gloader = load->gloader; 259 FT_Outline* outline; 260 TT_Face face = (TT_Face)load->face; 261 TT_GlyphSlot slot = (TT_GlyphSlot)load->glyph; 262 FT_UShort n_ins; 263 FT_Int n, n_points; 264 265 266 /* reading the contours endpoints & number of points */ 267 { 268 short* cur = gloader->current.outline.contours; 269 short* limit = cur + n_contours; 270 271 for ( ; cur < limit; cur++ ) 272 cur[0] = GET_UShort(); 273 274 n_points = 0; 275 if (n_contours > 0) 276 n_points = cur[-1]+1; 277 278 error = FT_GlyphLoader_Check_Points( gloader, n_points+2, 0 ); 279 if (error) goto Fail; 280 281 outline = &gloader->current.outline; 282 } 283 284 285 /* reading the bytecode instructions */ 286 slot->control_len = 0; 287 slot->control_data = 0; 288 289 n_ins = GET_UShort(); 290 291 FT_TRACE5(( " Instructions size: %d\n", n_ins )); 292 293 if ( n_ins > face->max_profile.maxSizeOfInstructions ) 294 { 295 FT_TRACE0(( "ERROR: Too many instructions!\n" )); 296 error = TT_Err_Too_Many_Ins; 297 goto Fail; 298 } 299 300 if ( stream->cursor + n_ins > stream->limit ) 301 { 302 FT_TRACE0(( "ERROR: Instruction count mismatch!\n" )); 303 error = TT_Err_Too_Many_Ins; 304 goto Fail; 305 } 306 307#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 308 if ( ( load->load_flags & 309 ( FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING ) ) == 0 && 310 load->instructions ) 311 { 312 slot->control_len = n_ins; 313 slot->control_data = load->instructions; 314 315 MEM_Copy( load->instructions, stream->cursor, n_ins ); 316 } 317#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 318 319 stream->cursor += n_ins; 320 321 /* reading the point tags */ 322 323 { 324 FT_Byte* flag = (FT_Byte*)outline->tags; 325 FT_Byte* limit = flag + n_points; 326 FT_Byte c, count; 327 328 for ( ; flag < limit; flag++ ) 329 { 330 *flag = c = GET_Byte(); 331 if ( c & 8 ) 332 { 333 for ( count = GET_Byte(); count > 0; count-- ) 334 *++flag = c; 335 } 336 } 337 } 338 339 /* reading the X coordinates */ 340 341 { 342 FT_Vector* vec = outline->points; 343 FT_Vector* limit = vec + n_points; 344 FT_Byte* flag = (FT_Byte*)outline->tags; 345 FT_Pos x = 0; 346 347 348 for ( ; vec < limit; vec++, flag++ ) 349 { 350 FT_Pos y = 0; 351 352 353 if ( *flag & 2 ) 354 { 355 y = GET_Byte(); 356 if ( ( *flag & 16 ) == 0 ) 357 y = -y; 358 } 359 else if ( ( *flag & 16 ) == 0 ) 360 y = GET_Short(); 361 362 x += y; 363 vec->x = x; 364 } 365 } 366 367 /* reading the Y coordinates */ 368 369 { 370 FT_Vector* vec = gloader->current.outline.points; 371 FT_Vector* limit = vec + n_points; 372 FT_Byte* flag = (FT_Byte*)outline->tags; 373 FT_Pos x = 0; 374 375 376 for ( ; vec < limit; vec++, flag++ ) 377 { 378 FT_Pos y = 0; 379 380 381 if ( *flag & 4 ) 382 { 383 y = GET_Byte(); 384 if ( ( *flag & 32 ) == 0 ) 385 y = -y; 386 } 387 else if ( ( *flag & 32 ) == 0 ) 388 y = GET_Short(); 389 390 x += y; 391 vec->y = x; 392 } 393 } 394 395 FORGET_Frame(); 396 397 /* clear the touch tags */ 398 for ( n = 0; n < n_points; n++ ) 399 outline->tags[n] &= FT_Curve_Tag_On; 400 401 outline->n_points = n_points; 402 outline->n_contours = n_contours; 403 404 Fail: 405 return error; 406 } 407 408 409 static 410 FT_Error TT_Load_Composite_Glyph( TT_Loader* loader, 411 FT_UInt byte_count ) 412 { 413 FT_Error error; 414 FT_Stream stream = loader->stream; 415 FT_GlyphLoader* gloader = loader->gloader; 416 FT_SubGlyph* subglyph; 417 FT_UInt num_subglyphs; 418 419 num_subglyphs = 0; 420 do 421 { 422 FT_Fixed xx, xy, yy, yx; 423 424 /* check that we can load a new subglyph */ 425 error = FT_GlyphLoader_Check_Subglyphs( gloader, num_subglyphs+1 ); 426 if (error) goto Fail; 427 428 subglyph = gloader->current.subglyphs + num_subglyphs; 429 430 subglyph->arg1 = subglyph->arg2 = 0; 431 432 subglyph->flags = GET_UShort(); 433 subglyph->index = GET_UShort(); 434 435 /* read arguments */ 436 if ( subglyph->flags & ARGS_ARE_WORDS ) 437 { 438 subglyph->arg1 = GET_Short(); 439 subglyph->arg2 = GET_Short(); 440 } 441 else 442 { 443 subglyph->arg1 = GET_Char(); 444 subglyph->arg2 = GET_Char(); 445 } 446 447 /* read transform */ 448 xx = yy = 0x10000L; 449 xy = yx = 0; 450 451 if ( subglyph->flags & WE_HAVE_A_SCALE ) 452 { 453 xx = (FT_Fixed)GET_Short() << 2; 454 yy = xx; 455 } 456 else if ( subglyph->flags & WE_HAVE_AN_XY_SCALE ) 457 { 458 xx = (FT_Fixed)GET_Short() << 2; 459 yy = (FT_Fixed)GET_Short() << 2; 460 } 461 else if ( subglyph->flags & WE_HAVE_A_2X2 ) 462 { 463 xx = (FT_Fixed)GET_Short() << 2; 464 xy = (FT_Fixed)GET_Short() << 2; 465 yx = (FT_Fixed)GET_Short() << 2; 466 yy = (FT_Fixed)GET_Short() << 2; 467 } 468 469 subglyph->transform.xx = xx; 470 subglyph->transform.xy = xy; 471 subglyph->transform.yx = yx; 472 subglyph->transform.yy = yy; 473 474 num_subglyphs++; 475 } 476 while (subglyph->flags & MORE_COMPONENTS); 477 478 gloader->current.num_subglyphs = num_subglyphs; 479 480#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 481 { 482 /* we must undo the ACCESS_Frame in order to point to the */ 483 /* composite instructions, if we find some. */ 484 /* we will process them later... */ 485 /* */ 486 loader->ins_pos = FILE_Pos() + stream->cursor - stream->limit; 487 } 488#endif 489 490 Fail: 491 return error; 492 } 493 494 495 496 /*************************************************************************/ 497 /* */ 498 /* <Function> */ 499 /* TT_Process_Simple_Glyph */ 500 /* */ 501 /* <Description> */ 502 /* Once a simple glyph has been loaded, it needs to be processed. */ 503 /* Usually, this means scaling and hinting through bytecode */ 504 /* interpretation.. */ 505 /* */ 506 static 507 FT_Error TT_Process_Simple_Glyph( TT_Loader* load, 508 FT_Bool debug ) 509 { 510 FT_GlyphLoader* gloader = load->gloader; 511 FT_Outline* outline = &gloader->current.outline; 512 FT_UInt n_points = outline->n_points; 513 FT_UInt n_ins; 514 TT_GlyphZone* zone = &load->zone; 515 FT_Error error = FT_Err_Ok; 516 517 n_ins = load->glyph->control_len; 518 519 /* add shadow points */ 520 521 /* Now add the two shadow points at n and n + 1. */ 522 /* We need the left side bearing and advance width. */ 523 524 { 525 FT_Vector* pp1; 526 FT_Vector* pp2; 527 528 /* pp1 = xMin - lsb */ 529 pp1 = outline->points + n_points; 530 pp1->x = load->bbox.xMin - load->left_bearing; 531 pp1->y = 0; 532 533 /* pp2 = pp1 + aw */ 534 pp2 = pp1 + 1; 535 pp2->x = pp1->x + load->advance; 536 pp2->y = 0; 537 538 outline->tags[n_points ] = 0; 539 outline->tags[n_points + 1] = 0; 540 } 541 542 /* Note that we return two more points that are not */ 543 /* part of the glyph outline. */ 544 545 n_points += 2; 546 547 /* set up zone for hinting */ 548 tt_prepare_zone( zone, &gloader->current, 0, 0 ); 549 550 /* eventually scale the glyph */ 551 if (!(load->load_flags & FT_LOAD_NO_SCALE)) 552 { 553 FT_Vector* vec = zone->cur; 554 FT_Vector* limit = vec + n_points; 555 FT_Fixed x_scale = load->size->metrics.x_scale; 556 FT_Fixed y_scale = load->size->metrics.y_scale; 557 558 /* first scale the glyph points */ 559 for ( ; vec < limit; vec++ ) 560 { 561 vec->x = FT_MulFix( vec->x, x_scale ); 562 vec->y = FT_MulFix( vec->y, y_scale ); 563 } 564 } 565 566 cur_to_org( n_points, zone ); 567 568 /* eventually hint the glyph */ 569 if ( IS_HINTED(load->load_flags) ) 570 { 571 FT_Pos x = zone->org[n_points-2].x; 572 573 x = ( ( x + 32 ) & -64 ) - x; 574 translate_array( n_points, zone->org, x, 0 ); 575 576 org_to_cur( n_points, zone ); 577 578 zone->cur[n_points - 1].x = ( zone->cur[n_points - 1].x + 32 ) & -64; 579 580#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 581 /* now consider hinting */ 582 if ( n_ins > 0 ) 583 { 584 error = TT_Set_CodeRange( load->exec, tt_coderange_glyph, 585 load->exec->glyphIns, n_ins ); 586 if ( error ) goto Exit; 587 588 load->exec->is_composite = FALSE; 589 load->exec->pedantic_hinting = (FT_Bool)(load->load_flags & 590 FT_LOAD_PEDANTIC); 591 load->exec->pts = *zone; 592 load->exec->pts.n_points += 2; 593 594 error = TT_Run_Context( load->exec, debug ); 595 if ( error && load->exec->pedantic_hinting ) 596 goto Exit; 597 598 error = FT_Err_Ok; /* ignore bytecode errors in non-pedantic mode */ 599 } 600#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 601 } 602 603 /* save glyph phantom points */ 604 if ( !load->preserve_pps ) 605 { 606 load->pp1 = zone->cur[n_points - 2]; 607 load->pp2 = zone->cur[n_points - 1]; 608 } 609 610 Exit: 611 return error; 612 } 613 614 615 616 617 618 619 /*************************************************************************/ 620 /* */ 621 /* <Function> */ 622 /* load_truetype_glyph */ 623 /* */ 624 /* <Description> */ 625 /* Loads a given truetype glyph. Handles composites and uses a */ 626 /* TT_Loader object. */ 627 /* */ 628 static 629 FT_Error load_truetype_glyph( TT_Loader* loader, 630 FT_UInt glyph_index ) 631 { 632 FT_Stream stream = loader->stream; 633 FT_Error error; 634 TT_Face face = (TT_Face)loader->face; 635 FT_ULong offset; 636 FT_Int contours_count; 637 FT_UInt index, num_points, num_contours, count; 638 FT_Fixed x_scale, y_scale; 639 FT_ULong ins_offset; 640 FT_GlyphLoader* gloader = loader->gloader; 641 FT_Bool opened_frame = 0; 642 643 644 /* check glyph index */ 645 index = glyph_index; 646 if ( index >= (FT_UInt)face->root.num_glyphs ) 647 { 648 error = TT_Err_Invalid_Glyph_Index; 649 goto Exit; 650 } 651 652 loader->glyph_index = glyph_index; 653 num_contours = 0; 654 num_points = 0; 655 ins_offset = 0; 656 657 x_scale = 0x10000L; 658 y_scale = 0x10000L; 659 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 660 { 661 x_scale = loader->size->metrics.x_scale; 662 y_scale = loader->size->metrics.y_scale; 663 } 664 665 /* get horizontal metrics */ 666 { 667 FT_Short left_bearing; 668 FT_UShort advance_width; 669 670 671 Get_HMetrics( face, index, 672 (FT_Bool)!(loader->load_flags & 673 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH), 674 &left_bearing, 675 &advance_width ); 676 677 loader->left_bearing = left_bearing; 678 loader->advance = advance_width; 679 } 680 681 offset = face->glyph_locations[index]; 682 count = 0; 683 684 if ( index < (FT_UInt)face->num_locations - 1 ) 685 count = face->glyph_locations[index + 1] - offset; 686 687 if ( count == 0 ) 688 { 689 /* as described by Frederic Loyer, these are spaces, and */ 690 /* not the unknown glyph. */ 691 loader->bbox.xMin = 0; 692 loader->bbox.xMax = 0; 693 loader->bbox.yMin = 0; 694 loader->bbox.yMax = 0; 695 696 loader->pp1.x = 0; 697 loader->pp2.x = loader->advance; 698 699 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 700 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 701 702#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 703 if ( loader->exec ) 704 loader->exec->glyphSize = 0; 705#endif 706 error = FT_Err_Ok; 707 goto Exit; 708 } 709 710 offset = loader->glyf_offset + offset; 711 712 /* access glyph frame */ 713 error = TT_Access_Glyph_Frame( loader, glyph_index, offset, count ); 714 if (error) goto Exit; 715 716 opened_frame = 1; 717 718 /* read first glyph header */ 719 error = TT_Load_Glyph_Header( loader ); 720 if (error) goto Fail; 721 722 contours_count = loader->n_contours; 723 724 count -= 10; 725 726 loader->pp1.x = loader->bbox.xMin - loader->left_bearing; 727 loader->pp1.y = 0; 728 loader->pp2.x = loader->pp1.x + loader->advance; 729 loader->pp2.y = 0; 730 731 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 732 { 733 loader->pp1.x = FT_MulFix( loader->pp1.x, x_scale ); 734 loader->pp2.x = FT_MulFix( loader->pp2.x, x_scale ); 735 } 736 737 /***********************************************************************/ 738 /***********************************************************************/ 739 /***********************************************************************/ 740 741 /* if it is a simple glyph, load it */ 742 743 if ( contours_count >= 0 ) 744 { 745 /* check that we can add the contours to the glyph */ 746 error = FT_GlyphLoader_Check_Points( gloader, 0, contours_count ); 747 if (error) goto Fail; 748 749 error = TT_Load_Simple_Glyph( loader, contours_count ); 750 if (error) goto Fail; 751 752#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 753 { 754 TT_Size size = (TT_Size)loader->size; 755 error = TT_Process_Simple_Glyph( loader, 756 (FT_Bool)( size && size->debug ) ); 757 } 758#else 759 error = Process_Simple_Glyph( loader, 0 ); 760#endif 761 if ( error ) goto Fail; 762 763 FT_GlyphLoader_Add( gloader ); 764 765 /* Note: We could have put the simple loader source there */ 766 /* but the code is fat enough already :-) */ 767 } 768 769 /***********************************************************************/ 770 /***********************************************************************/ 771 /***********************************************************************/ 772 773 /* otherwise, load a composite! */ 774 else 775 { 776 TT_GlyphSlot glyph = (TT_GlyphSlot)loader->glyph; 777 FT_UInt start_point, start_contour; 778 779 /* for each subglyph, read composite header */ 780 start_point = gloader->base.outline.n_points; 781 start_contour = gloader->base.outline.n_contours; 782 783 784 error = TT_Load_Composite_Glyph( loader, count ); 785 if (error) goto Fail; 786 787 TT_Forget_Glyph_Frame( loader ); 788 opened_frame = 0; 789 790 /* if the flag FT_LOAD_NO_RECURSE is set, we return the subglyph */ 791 /* `as is' in the glyph slot (the client application will be */ 792 /* responsible for interpreting this data)... */ 793 /* */ 794 if ( loader->load_flags & FT_LOAD_NO_RECURSE ) 795 { 796 /* set up remaining glyph fields */ 797 FT_GlyphLoader_Add( gloader ); 798 799 glyph->num_subglyphs = gloader->base.num_subglyphs; 800 glyph->format = ft_glyph_format_composite; 801 glyph->subglyphs = gloader->base.subglyphs; 802 803 goto Exit; 804 } 805 806 807 /***********************************************************************/ 808 /***********************************************************************/ 809 /***********************************************************************/ 810 811 /*********************************************************************/ 812 /* Now, read each subglyph independently.. */ 813 { 814 FT_Int n, num_base_points, num_new_points; 815 FT_SubGlyph* subglyph; 816 817 FT_UInt num_subglyphs = gloader->current.num_subglyphs; 818 FT_UInt num_base_subgs = gloader->base.num_subglyphs; 819 820 821 FT_GlyphLoader_Add( gloader ); 822 823 for ( n = 0; n < num_subglyphs; n++ ) 824 { 825 FT_Vector pp1, pp2; 826 FT_Pos x, y; 827 828 /* each time we call load_truetype_glyph in this loop, the */ 829 /* value of 'gloader.base.subglyphs' can change due to table */ 830 /* reallocations. We thus need to recompute the subglyph */ 831 /* pointer on each iteration.. */ 832 subglyph = gloader->base.subglyphs + num_base_subgs + n; 833 834 pp1 = loader->pp1; 835 pp2 = loader->pp2; 836 837 num_base_points = gloader->base.outline.n_points; 838 839 error = load_truetype_glyph( loader, subglyph->index ); 840 if ( error ) 841 goto Fail; 842 843 subglyph = gloader->base.subglyphs + num_base_subgs + n; 844 845 if ( subglyph->flags & USE_MY_METRICS ) 846 { 847 pp1 = loader->pp1; 848 pp2 = loader->pp2; 849 } 850 else 851 { 852 loader->pp1 = pp1; 853 loader->pp2 = pp2; 854 } 855 856 num_points = gloader->base.outline.n_points; 857 858 num_new_points = num_points - num_base_points; 859 860 /* now perform the transform required for this subglyph */ 861 862 if ( subglyph->flags & ( WE_HAVE_A_SCALE | 863 WE_HAVE_AN_XY_SCALE | 864 WE_HAVE_A_2X2 ) ) 865 { 866 FT_Vector* cur = gloader->base.outline.points + num_base_points; 867 FT_Vector* org = gloader->base.extra_points + num_base_points; 868 FT_Vector* limit = cur + num_new_points; 869 870 for ( ; cur < limit; cur++, org++ ) 871 { 872 FT_Vector_Transform( cur, &subglyph->transform ); 873 FT_Vector_Transform( org, &subglyph->transform ); 874 } 875 } 876 877 /* apply offset */ 878 879 if ( !( subglyph->flags & ARGS_ARE_XY_VALUES ) ) 880 { 881 FT_Int k = subglyph->arg1; 882 FT_UInt l = subglyph->arg2; 883 FT_Vector* p1; 884 FT_Vector* p2; 885 886 if ( start_point + k >= num_base_points || 887 l >= (FT_UInt)num_new_points ) 888 { 889 error = TT_Err_Invalid_Composite; 890 goto Fail; 891 } 892 893 l += num_base_points; 894 895 p1 = gloader->base.outline.points + start_point + k; 896 p2 = gloader->base.outline.points + start_point + l; 897 898 x = p1->x - p2->x; 899 y = p1->y - p2->y; 900 } 901 else 902 { 903 x = subglyph->arg1; 904 y = subglyph->arg2; 905 906 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 907 { 908 x = FT_MulFix( x, x_scale ); 909 y = FT_MulFix( y, y_scale ); 910 911 if ( subglyph->flags & ROUND_XY_TO_GRID ) 912 { 913 x = ( x + 32 ) & -64; 914 y = ( y + 32 ) & -64; 915 } 916 } 917 } 918 919 translate_array( num_new_points, loader->zone.cur, x, y ); 920 cur_to_org( num_new_points, &loader->zone ); 921 } 922 923 /***********************************************************************/ 924 /***********************************************************************/ 925 /***********************************************************************/ 926 927 /* we have finished loading all sub-glyphs, now, look for */ 928 /* instructions for this composite! */ 929 930#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 931 932 if ( num_subglyphs > 0 && 933 loader->exec && 934 loader->ins_pos > 0 && 935 subglyph->flags & WE_HAVE_INSTR ) 936 { 937 FT_UShort n_ins; 938 TT_ExecContext exec = loader->exec; 939 FT_UInt n_points = loader->base.n_points; 940 TT_GlyphZone* pts; 941 FT_Vector* pp1; 942 943 944 /* read size of instructions */ 945 if ( FILE_Seek( loader->ins_pos ) || 946 READ_UShort(n_ins) ) 947 goto Fail; 948 FT_TRACE5(( " Instructions size = %d\n", n_ins )); 949 950 /* in some fonts ?? */ 951 if (n_ins == 0xFFFF) 952 n_ins = 0; 953 954 /* check it */ 955 if ( n_ins > face->max_profile.maxSizeOfInstructions ) 956 { 957 FT_TRACE0(( "Too many instructions (%d) in composite glyph %ld\n", 958 n_ins, subglyph->index )); 959 return TT_Err_Too_Many_Ins; 960 } 961 962 /* read the instructions */ 963 if ( FILE_Read( exec->glyphIns, n_ins ) ) 964 goto Fail; 965 966 glyph->control_data = exec->glyphIns; 967 glyph->control_len = n_ins; 968 969 error = TT_Set_CodeRange( exec, 970 tt_coderange_glyph, 971 exec->glyphIns, 972 n_ins ); 973 if ( error ) 974 goto Fail; 975 976 /* prepare the execution context */ 977 tt_prepare_zone( &exec->pts, &gloader->base, start_point, start_contour ); 978 pts = &exec->pts; 979 980 pts->n_points = num_points + 2; 981 pts->n_contours = num_contours; 982 983 /* add phantom points */ 984 pp1 = pts->cur + num_points; 985 pp1[0] = loader->pp1; 986 pp1[1] = loader->pp2; 987 988 pts->tags[num_points + 1] = 0; 989 pts->tags[num_points + 2] = 0; 990 991 /* if hinting, round the phantom points */ 992 if ( IS_HINTED( loader->load_flags ) ) 993 { 994 pp1[0].x = ( ( loader->pp1.x + 32 ) & -64 ); 995 pp1[1].x = ( ( loader->pp2.x + 32 ) & -64 ); 996 } 997 998 { 999 FT_UInt k; 1000 1001 1002 for ( k = 0; k < n_points; k++ ) 1003 pts->tags[k] &= FT_Curve_Tag_On; 1004 } 1005 1006 cur_to_org( n_points, pts ); 1007 1008 /* now consider hinting */ 1009 if ( IS_HINTED(loader->load_flags) && n_ins > 0 ) 1010 { 1011 exec->is_composite = TRUE; 1012 exec->pedantic_hinting = 1013 (FT_Bool)(loader->load_flags & FT_LOAD_PEDANTIC); 1014 1015 error = TT_Run_Context( exec, ((TT_Size)loader->size)->debug ); 1016 if ( error && exec->pedantic_hinting ) 1017 goto Fail; 1018 } 1019 1020 /* save glyph origin and advance points */ 1021 loader->pp1 = pp1[0]; 1022 loader->pp2 = pp1[1]; 1023 } 1024#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1025 } 1026 /* end of composite loading */ 1027 } 1028 1029 /***********************************************************************/ 1030 /***********************************************************************/ 1031 /***********************************************************************/ 1032 /***********************************************************************/ 1033 1034 Fail: 1035 if (opened_frame) 1036 TT_Forget_Glyph_Frame( loader ); 1037 1038 Exit: 1039 return error; 1040 } 1041 1042 1043 static 1044 void compute_glyph_metrics( TT_Loader* loader, 1045 FT_UInt glyph_index ) 1046 { 1047 FT_BBox bbox; 1048 TT_Face face = (TT_Face)loader->face; 1049 FT_Fixed x_scale, y_scale; 1050 TT_GlyphSlot glyph = loader->glyph; 1051 TT_Size size = (TT_Size)loader->size; 1052 1053 x_scale = 0x10000L; 1054 y_scale = 0x10000L; 1055 if ( ( loader->load_flags & FT_LOAD_NO_SCALE ) == 0 ) 1056 { 1057 x_scale = size->root.metrics.x_scale; 1058 y_scale = size->root.metrics.y_scale; 1059 } 1060 1061 if ( glyph->format != ft_glyph_format_composite ) 1062 { 1063 glyph->outline.flags &= ~ft_outline_single_pass; 1064 1065 /* copy outline to our glyph slot */ 1066 FT_GlyphLoader_Copy_Points( glyph->loader, loader->gloader ); 1067 glyph->outline = glyph->loader->base.outline; 1068 1069 /* translate array so that (0,0) is the glyph's origin */ 1070 FT_Outline_Translate( &glyph->outline, -loader->pp1.x, 0 ); 1071 1072 FT_Outline_Get_CBox( &glyph->outline, &bbox ); 1073 1074 if ( IS_HINTED( loader->load_flags ) ) 1075 { 1076 /* grid-fit the bounding box */ 1077 bbox.xMin &= -64; 1078 bbox.yMin &= -64; 1079 bbox.xMax = ( bbox.xMax + 63 ) & -64; 1080 bbox.yMax = ( bbox.yMax + 63 ) & -64; 1081 } 1082 } 1083 else 1084 bbox = loader->bbox; 1085 1086 /* get the device-independent horizontal advance. It is scaled later */ 1087 /* by the base layer.. */ 1088 { 1089 FT_Pos advance = loader->advance; 1090 1091 /* the flag FT_LOAD_NO_ADVANCE_CHECK was introduced to */ 1092 /* correctly support DynaLab fonts, which have an incorrect */ 1093 /* `advance_Width_Max' field! It is used, to my knowledge, */ 1094 /* exclusively in the X-TrueType font server. */ 1095 /* */ 1096 if ( face->postscript.isFixedPitch && 1097 ( loader->load_flags & FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH ) == 0 ) 1098 advance = face->horizontal.advance_Width_Max; 1099 1100 /* we need to return the advance in font units in linearHoriAdvance, */ 1101 /* it will be scaled later by the base layer.. */ 1102 glyph->linearHoriAdvance = advance; 1103 } 1104 1105 glyph->metrics.horiBearingX = bbox.xMin; 1106 glyph->metrics.horiBearingY = bbox.yMax; 1107 glyph->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; 1108 1109 /* Now take care of vertical metrics. In the case where there is */ 1110 /* no vertical information within the font (relatively common), make */ 1111 /* up some metrics by `hand'... */ 1112 1113 { 1114 FT_Short top_bearing; /* vertical top side bearing (EM units) */ 1115 FT_UShort advance_height; /* vertical advance height (EM units) */ 1116 1117 FT_Pos left; /* scaled vertical left side bearing */ 1118 FT_Pos Top; /* scaled original vertical top side bearing */ 1119 FT_Pos top; /* scaled vertical top side bearing */ 1120 FT_Pos advance; /* scaled vertical advance height */ 1121 1122 1123 /* Get the unscaled `tsb' and `ah' */ 1124 if ( face->vertical_info && 1125 face->vertical.number_Of_VMetrics > 0 ) 1126 { 1127 /* Don't assume that both the vertical header and vertical */ 1128 /* metrics are present in the same font :-) */ 1129 1130 TT_Get_Metrics( (TT_HoriHeader*)&face->vertical, 1131 glyph_index, 1132 &top_bearing, 1133 &advance_height ); 1134 } 1135 else 1136 { 1137 /* Make up the distances from the horizontal header. */ 1138 1139 /* NOTE: The OS/2 values are the only `portable' ones, */ 1140 /* which is why we use them, if there is an OS/2 */ 1141 /* table in the font. Otherwise, we use the */ 1142 /* values defined in the horizontal header. */ 1143 /* */ 1144 /* NOTE2: The sTypoDescender is negative, which is why */ 1145 /* we compute the baseline-to-baseline distance */ 1146 /* here with: */ 1147 /* ascender - descender + linegap */ 1148 /* */ 1149 if ( face->os2.version != 0xFFFF ) 1150 { 1151 top_bearing = face->os2.sTypoLineGap / 2; 1152 advance_height = (FT_UShort)( face->os2.sTypoAscender - 1153 face->os2.sTypoDescender + 1154 face->os2.sTypoLineGap ); 1155 } 1156 else 1157 { 1158 top_bearing = face->horizontal.Line_Gap / 2; 1159 advance_height = (FT_UShort)( face->horizontal.Ascender + 1160 face->horizontal.Descender + 1161 face->horizontal.Line_Gap ); 1162 } 1163 } 1164 1165 /* We must adjust the top_bearing value from the bounding box given */ 1166 /* in the glyph header to te bounding box calculated with */ 1167 /* FT_Get_Outline_CBox(). */ 1168 1169 /* scale the metrics */ 1170 if ( !( loader->load_flags & FT_LOAD_NO_SCALE ) ) 1171 { 1172 Top = FT_MulFix( top_bearing, y_scale ); 1173 top = FT_MulFix( top_bearing + loader->bbox.yMax, y_scale ) 1174 - bbox.yMax; 1175 advance = FT_MulFix( advance_height, y_scale ); 1176 } 1177 else 1178 { 1179 Top = top_bearing; 1180 top = top_bearing + loader->bbox.yMax - bbox.yMax; 1181 advance = advance_height; 1182 } 1183 1184 /* set the advance height in design units. It is scaled later by the */ 1185 /* base layer.. */ 1186 glyph->linearVertAdvance = advance_height; 1187 1188 /* XXX: for now, we have no better algorithm for the lsb, but it */ 1189 /* should work fine. */ 1190 /* */ 1191 left = ( bbox.xMin - bbox.xMax ) / 2; 1192 1193 /* grid-fit them if necessary */ 1194 if ( IS_HINTED( loader->load_flags ) ) 1195 { 1196 left &= -64; 1197 top = ( top + 63 ) & -64; 1198 advance = ( advance + 32 ) & -64; 1199 } 1200 1201 glyph->metrics.vertBearingX = left; 1202 glyph->metrics.vertBearingY = top; 1203 glyph->metrics.vertAdvance = advance; 1204 } 1205 1206 /* Adjust advance width to the value contained in the hdmx table. */ 1207 if ( !face->postscript.isFixedPitch && size && 1208 IS_HINTED( loader->load_flags ) ) 1209 { 1210 FT_Byte* widths = Get_Advance_Widths( face, 1211 size->root.metrics.x_ppem ); 1212 if ( widths ) 1213 glyph->metrics.horiAdvance = widths[glyph_index] << 6; 1214 } 1215 1216 /* set glyph dimensions */ 1217 glyph->metrics.width = bbox.xMax - bbox.xMin; 1218 glyph->metrics.height = bbox.yMax - bbox.yMin; 1219 1220 } 1221 1222 1223 /*************************************************************************/ 1224 /* */ 1225 /* <Function> */ 1226 /* TT_Load_Glyph */ 1227 /* */ 1228 /* <Description> */ 1229 /* A function used to load a single glyph within a given glyph slot, */ 1230 /* for a given size. */ 1231 /* */ 1232 /* <Input> */ 1233 /* glyph :: A handle to a target slot object where the glyph */ 1234 /* will be loaded. */ 1235 /* */ 1236 /* size :: A handle to the source face size at which the glyph */ 1237 /* must be scaled/loaded. */ 1238 /* */ 1239 /* glyph_index :: The index of the glyph in the font file. */ 1240 /* */ 1241 /* load_flags :: A flag indicating what to load for this glyph. The */ 1242 /* FT_LOAD_XXX constants can be used to control the */ 1243 /* glyph loading process (e.g., whether the outline */ 1244 /* should be scaled, whether to load bitmaps or not, */ 1245 /* whether to hint the outline, etc). */ 1246 /* */ 1247 /* <Return> */ 1248 /* TrueType error code. 0 means success. */ 1249 /* */ 1250 LOCAL_FUNC 1251 FT_Error TT_Load_Glyph( TT_Size size, 1252 TT_GlyphSlot glyph, 1253 FT_UShort glyph_index, 1254 FT_UInt load_flags ) 1255 { 1256 SFNT_Interface* sfnt; 1257 TT_Face face; 1258 FT_Stream stream; 1259 FT_Memory memory; 1260 FT_Error error; 1261 TT_Loader loader; 1262 1263 1264 face = (TT_Face)glyph->face; 1265 sfnt = (SFNT_Interface*)face->sfnt; 1266 stream = face->root.stream; 1267 memory = face->root.memory; 1268 error = 0; 1269 1270 if ( !size || ( load_flags & FT_LOAD_NO_SCALE ) || 1271 ( load_flags & FT_LOAD_NO_RECURSE ) ) 1272 { 1273 size = NULL; 1274 load_flags |= FT_LOAD_NO_SCALE | 1275 FT_LOAD_NO_HINTING | 1276 FT_LOAD_NO_BITMAP; 1277 } 1278 1279 glyph->num_subglyphs = 0; 1280 1281#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS 1282 1283 /* try to load embedded bitmap if any */ 1284 if ( size && 1285 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 && 1286 sfnt->load_sbits ) 1287 { 1288 TT_SBit_Metrics metrics; 1289 1290 1291 error = sfnt->load_sbit_image( face, 1292 size->root.metrics.x_ppem, 1293 size->root.metrics.y_ppem, 1294 glyph_index, 1295 load_flags, 1296 stream, 1297 &glyph->bitmap, 1298 &metrics ); 1299 if ( !error ) 1300 { 1301 glyph->outline.n_points = 0; 1302 glyph->outline.n_contours = 0; 1303 1304 glyph->metrics.width = (FT_Pos)metrics.width << 6; 1305 glyph->metrics.height = (FT_Pos)metrics.height << 6; 1306 1307 glyph->metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; 1308 glyph->metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; 1309 glyph->metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; 1310 1311 glyph->metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; 1312 glyph->metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; 1313 glyph->metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; 1314 1315 glyph->format = ft_glyph_format_bitmap; 1316 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) 1317 { 1318 glyph->bitmap_left = metrics.horiBearingX; 1319 glyph->bitmap_top = metrics.horiBearingY; 1320 } 1321 else 1322 { 1323 glyph->bitmap_left = metrics.vertBearingX; 1324 glyph->bitmap_top = metrics.vertBearingY; 1325 } 1326 return error; 1327 } 1328 } 1329 1330#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ 1331 1332 /* seek to the beginning of the glyph table. For Type 43 fonts */ 1333 /* the table might be accessed from a Postscript stream or something */ 1334 /* else... */ 1335 1336 error = face->goto_table( face, TTAG_glyf, stream, 0 ); 1337 if ( error ) 1338 { 1339 FT_ERROR(( "TT_Load_Glyph: Could not access glyph table\n" )); 1340 goto Exit; 1341 } 1342 1343 MEM_Set( &loader, 0, sizeof ( loader ) ); 1344 1345 /* update the glyph zone bounds */ 1346 { 1347 FT_GlyphLoader* gloader = FT_FACE_DRIVER(face)->glyph_loader; 1348 1349 loader.gloader = gloader; 1350 1351 FT_GlyphLoader_Rewind( gloader ); 1352 1353 tt_prepare_zone( &loader.zone, &gloader->base, 0, 0 ); 1354 tt_prepare_zone( &loader.base, &gloader->base, 0, 0 ); 1355 } 1356 1357#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1358 1359 if ( size ) 1360 { 1361 /* query new execution context */ 1362 loader.exec = size->debug ? size->context : TT_New_Context( face ); 1363 if ( !loader.exec ) 1364 return TT_Err_Could_Not_Find_Context; 1365 1366 TT_Load_Context( loader.exec, face, size ); 1367 loader.instructions = loader.exec->glyphIns; 1368 1369 /* load default graphics state - if needed */ 1370 if ( size->GS.instruct_control & 2 ) 1371 loader.exec->GS = tt_default_graphics_state; 1372 } 1373 1374#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1375 1376 /* clear all outline flags, except the `owner' one */ 1377 glyph->outline.flags &= ft_outline_owner; 1378 1379 if ( size && size->root.metrics.y_ppem < 24 ) 1380 glyph->outline.flags |= ft_outline_high_precision; 1381 1382 /* let's initialize the rest of our loader now */ 1383 1384 loader.load_flags = load_flags; 1385 1386 loader.face = (FT_Face)face; 1387 loader.size = (FT_Size)size; 1388 loader.glyph = (FT_GlyphSlot)glyph; 1389 loader.stream = stream; 1390 1391 loader.glyf_offset = FILE_Pos(); 1392 1393#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1394 1395 /* if the cvt program has disabled hinting, the argument */ 1396 /* is ignored. */ 1397 if ( size && ( size->GS.instruct_control & 1 ) ) 1398 loader.load_flags |= FT_LOAD_NO_HINTING; 1399 1400#endif /* TT_CONFIG_OPTION_BYTECODE_INTERPRETER */ 1401 1402 /* Main loading loop */ 1403 glyph->format = ft_glyph_format_outline; 1404 glyph->num_subglyphs = 0; 1405 error = load_truetype_glyph( &loader, glyph_index ); 1406 if ( !error ) 1407 compute_glyph_metrics( &loader, glyph_index ); 1408 1409#ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER 1410 if ( !size || !size->debug ) 1411 TT_Done_Context( loader.exec ); 1412#endif 1413 1414 Exit: 1415 return error; 1416 } 1417 1418 1419/* END */ 1420