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