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