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