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