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