cf2hints.c revision ec0bab5697bb31ba980810145f62e3799946ec60
1/***************************************************************************/ 2/* */ 3/* cf2hints.c */ 4/* */ 5/* Adobe's code for handling CFF hints (body). */ 6/* */ 7/* Copyright 2007-2013 Adobe Systems Incorporated. */ 8/* */ 9/* This software, and all works of authorship, whether in source or */ 10/* object code form as indicated by the copyright notice(s) included */ 11/* herein (collectively, the "Work") is made available, and may only be */ 12/* used, modified, and distributed under the FreeType Project License, */ 13/* LICENSE.TXT. Additionally, subject to the terms and conditions of the */ 14/* FreeType Project License, each contributor to the Work hereby grants */ 15/* to any individual or legal entity exercising permissions granted by */ 16/* the FreeType Project License and this section (hereafter, "You" or */ 17/* "Your") a perpetual, worldwide, non-exclusive, no-charge, */ 18/* royalty-free, irrevocable (except as stated in this section) patent */ 19/* license to make, have made, use, offer to sell, sell, import, and */ 20/* otherwise transfer the Work, where such license applies only to those */ 21/* patent claims licensable by such contributor that are necessarily */ 22/* infringed by their contribution(s) alone or by combination of their */ 23/* contribution(s) with the Work to which such contribution(s) was */ 24/* submitted. If You institute patent litigation against any entity */ 25/* (including a cross-claim or counterclaim in a lawsuit) alleging that */ 26/* the Work or a contribution incorporated within the Work constitutes */ 27/* direct or contributory patent infringement, then any patent licenses */ 28/* granted to You under this License for that Work shall terminate as of */ 29/* the date such litigation is filed. */ 30/* */ 31/* By using, modifying, or distributing the Work you indicate that you */ 32/* have read and understood the terms and conditions of the */ 33/* FreeType Project License as well as those provided in this section, */ 34/* and you accept them fully. */ 35/* */ 36/***************************************************************************/ 37 38 39#include "cf2ft.h" 40#include FT_INTERNAL_DEBUG_H 41 42#include "cf2glue.h" 43#include "cf2font.h" 44#include "cf2hints.h" 45#include "cf2intrp.h" 46 47 48 /*************************************************************************/ 49 /* */ 50 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 51 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 52 /* messages during execution. */ 53 /* */ 54#undef FT_COMPONENT 55#define FT_COMPONENT trace_cf2hints 56 57 58 typedef struct CF2_HintMoveRec_ 59 { 60 size_t j; /* index of upper hint map edge */ 61 CF2_Fixed moveUp; /* adjustment to optimum position */ 62 63 } CF2_HintMoveRec, *CF2_HintMove; 64 65 66 /* Compute angular momentum for winding order detection. It is called */ 67 /* for all lines and curves, but not necessarily in element order. */ 68 static CF2_Int 69 cf2_getWindingMomentum( CF2_Fixed x1, 70 CF2_Fixed y1, 71 CF2_Fixed x2, 72 CF2_Fixed y2 ) 73 { 74 /* cross product of pt1 position from origin with pt2 position from */ 75 /* pt1; we reduce the precision so that the result fits into 32 bits */ 76 77 return ( x1 >> 16 ) * ( ( y2 - y1 ) >> 16 ) - 78 ( y1 >> 16 ) * ( ( x2 - x1 ) >> 16 ); 79 } 80 81 82 /* 83 * Construct from a StemHint; this is used as a parameter to 84 * `cf2_blues_capture'. 85 * `hintOrigin' is the character space displacement of a seac accent. 86 * Adjust stem hint for darkening here. 87 * 88 */ 89 static void 90 cf2_hint_init( CF2_Hint hint, 91 const CF2_ArrStack stemHintArray, 92 size_t indexStemHint, 93 const CF2_Font font, 94 CF2_Fixed hintOrigin, 95 CF2_Fixed scale, 96 FT_Bool bottom ) 97 { 98 CF2_Fixed width; 99 const CF2_StemHintRec* stemHint; 100 101 102 FT_ZERO( hint ); 103 104 stemHint = (const CF2_StemHintRec*)cf2_arrstack_getPointer( 105 stemHintArray, 106 indexStemHint ); 107 108 width = stemHint->max - stemHint->min; 109 110 if ( width == cf2_intToFixed( -21 ) ) 111 { 112 /* ghost bottom */ 113 114 if ( bottom ) 115 { 116 hint->csCoord = stemHint->max; 117 hint->flags = CF2_GhostBottom; 118 } 119 else 120 hint->flags = 0; 121 } 122 123 else if ( width == cf2_intToFixed( -20 ) ) 124 { 125 /* ghost top */ 126 127 if ( bottom ) 128 hint->flags = 0; 129 else 130 { 131 hint->csCoord = stemHint->min; 132 hint->flags = CF2_GhostTop; 133 } 134 } 135 136 else if ( width < 0 ) 137 { 138 /* inverted pair */ 139 140 /* 141 * Hints with negative widths were produced by an early version of a 142 * non-Adobe font tool. The Type 2 spec allows edge (ghost) hints 143 * with negative widths, but says 144 * 145 * All other negative widths have undefined meaning. 146 * 147 * CoolType has a silent workaround that negates the hint width; for 148 * permissive mode, we do the same here. 149 * 150 * Note: Such fonts cannot use ghost hints, but should otherwise work. 151 * Note: Some poor hints in our faux fonts can produce negative 152 * widths at some blends. For example, see a light weight of 153 * `u' in ASerifMM. 154 * 155 */ 156 if ( bottom ) 157 { 158 hint->csCoord = stemHint->max; 159 hint->flags = CF2_PairBottom; 160 } 161 else 162 { 163 hint->csCoord = stemHint->min; 164 hint->flags = CF2_PairTop; 165 } 166 } 167 168 else 169 { 170 /* normal pair */ 171 172 if ( bottom ) 173 { 174 hint->csCoord = stemHint->min; 175 hint->flags = CF2_PairBottom; 176 } 177 else 178 { 179 hint->csCoord = stemHint->max; 180 hint->flags = CF2_PairTop; 181 } 182 } 183 184 /* Now that ghost hints have been detected, adjust this edge for */ 185 /* darkening. Bottoms are not changed; tops are incremented by twice */ 186 /* `darkenY'. */ 187 if ( cf2_hint_isTop( hint ) ) 188 hint->csCoord += 2 * font->darkenY; 189 190 hint->csCoord += hintOrigin; 191 hint->scale = scale; 192 hint->index = indexStemHint; /* index in original stem hint array */ 193 194 /* if original stem hint has been used, use the same position */ 195 if ( hint->flags != 0 && stemHint->used ) 196 { 197 if ( cf2_hint_isTop( hint ) ) 198 hint->dsCoord = stemHint->maxDS; 199 else 200 hint->dsCoord = stemHint->minDS; 201 202 cf2_hint_lock( hint ); 203 } 204 else 205 hint->dsCoord = FT_MulFix( hint->csCoord, scale ); 206 } 207 208 209 /* initialize an invalid hint map element */ 210 static void 211 cf2_hint_initZero( CF2_Hint hint ) 212 { 213 FT_ZERO( hint ); 214 } 215 216 217 FT_LOCAL_DEF( FT_Bool ) 218 cf2_hint_isValid( const CF2_Hint hint ) 219 { 220 return (FT_Bool)( hint->flags != 0 ); 221 } 222 223 224 static FT_Bool 225 cf2_hint_isPair( const CF2_Hint hint ) 226 { 227 return (FT_Bool)( ( hint->flags & 228 ( CF2_PairBottom | CF2_PairTop ) ) != 0 ); 229 } 230 231 232 static FT_Bool 233 cf2_hint_isPairTop( const CF2_Hint hint ) 234 { 235 return (FT_Bool)( ( hint->flags & CF2_PairTop ) != 0 ); 236 } 237 238 239 FT_LOCAL_DEF( FT_Bool ) 240 cf2_hint_isTop( const CF2_Hint hint ) 241 { 242 return (FT_Bool)( ( hint->flags & 243 ( CF2_PairTop | CF2_GhostTop ) ) != 0 ); 244 } 245 246 247 FT_LOCAL_DEF( FT_Bool ) 248 cf2_hint_isBottom( const CF2_Hint hint ) 249 { 250 return (FT_Bool)( ( hint->flags & 251 ( CF2_PairBottom | CF2_GhostBottom ) ) != 0 ); 252 } 253 254 255 static FT_Bool 256 cf2_hint_isLocked( const CF2_Hint hint ) 257 { 258 return (FT_Bool)( ( hint->flags & CF2_Locked ) != 0 ); 259 } 260 261 262 static FT_Bool 263 cf2_hint_isSynthetic( const CF2_Hint hint ) 264 { 265 return (FT_Bool)( ( hint->flags & CF2_Synthetic ) != 0 ); 266 } 267 268 269 FT_LOCAL_DEF( void ) 270 cf2_hint_lock( CF2_Hint hint ) 271 { 272 hint->flags |= CF2_Locked; 273 } 274 275 276 FT_LOCAL_DEF( void ) 277 cf2_hintmap_init( CF2_HintMap hintmap, 278 CF2_Font font, 279 CF2_HintMap initialMap, 280 CF2_ArrStack hintMoves, 281 CF2_Fixed scale ) 282 { 283 FT_ZERO( hintmap ); 284 285 /* copy parameters from font instance */ 286 hintmap->hinted = font->hinted; 287 hintmap->scale = scale; 288 hintmap->font = font; 289 hintmap->initialHintMap = initialMap; 290 /* will clear in `cf2_hintmap_adjustHints' */ 291 hintmap->hintMoves = hintMoves; 292 } 293 294 295 static FT_Bool 296 cf2_hintmap_isValid( const CF2_HintMap hintmap ) 297 { 298 return hintmap->isValid; 299 } 300 301 302 /* transform character space coordinate to device space using hint map */ 303 static CF2_Fixed 304 cf2_hintmap_map( CF2_HintMap hintmap, 305 CF2_Fixed csCoord ) 306 { 307 FT_ASSERT( hintmap->isValid ); /* must call Build before Map */ 308 FT_ASSERT( hintmap->lastIndex < CF2_MAX_HINT_EDGES ); 309 310 if ( hintmap->count == 0 || ! hintmap->hinted ) 311 { 312 /* there are no hints; use uniform scale and zero offset */ 313 return FT_MulFix( csCoord, hintmap->scale ); 314 } 315 else 316 { 317 /* start linear search from last hit */ 318 CF2_UInt i = hintmap->lastIndex; 319 320 321 /* search up */ 322 while ( i < hintmap->count - 1 && 323 csCoord >= hintmap->edge[i + 1].csCoord ) 324 i += 1; 325 326 /* search down */ 327 while ( i > 0 && csCoord < hintmap->edge[i].csCoord ) 328 i -= 1; 329 330 hintmap->lastIndex = i; 331 332 if ( i == 0 && csCoord < hintmap->edge[0].csCoord ) 333 { 334 /* special case for points below first edge: use uniform scale */ 335 return FT_MulFix( csCoord - hintmap->edge[0].csCoord, 336 hintmap->scale ) + 337 hintmap->edge[0].dsCoord; 338 } 339 else 340 { 341 /* 342 * Note: entries with duplicate csCoord are allowed. 343 * Use edge[i], the highest entry where csCoord >= entry[i].csCoord 344 */ 345 return FT_MulFix( csCoord - hintmap->edge[i].csCoord, 346 hintmap->edge[i].scale ) + 347 hintmap->edge[i].dsCoord; 348 } 349 } 350 } 351 352 353 /* 354 * This hinting policy moves a hint pair in device space so that one of 355 * its two edges is on a device pixel boundary (its fractional part is 356 * zero). `cf2_hintmap_insertHint' guarantees no overlap in CS 357 * space. Ensure here that there is no overlap in DS. 358 * 359 * In the first pass, edges are adjusted relative to adjacent hints. 360 * Those that are below have already been adjusted. Those that are 361 * above have not yet been adjusted. If a hint above blocks an 362 * adjustment to an optimal position, we will try again in a second 363 * pass. The second pass is top-down. 364 * 365 */ 366 367 static void 368 cf2_hintmap_adjustHints( CF2_HintMap hintmap ) 369 { 370 size_t i, j; 371 372 373 cf2_arrstack_clear( hintmap->hintMoves ); /* working storage */ 374 375 /* 376 * First pass is bottom-up (font hint order) without look-ahead. 377 * Locked edges are already adjusted. 378 * Unlocked edges begin with dsCoord from `initialHintMap'. 379 * Save edges that are not optimally adjusted in `hintMoves' array, 380 * and process them in second pass. 381 */ 382 383 for ( i = 0; i < hintmap->count; i++ ) 384 { 385 FT_Bool isPair = cf2_hint_isPair( &hintmap->edge[i] ); 386 387 388 /* index of upper edge (same value for ghost hint) */ 389 j = isPair ? i + 1 : i; 390 391 FT_ASSERT( j < hintmap->count ); 392 FT_ASSERT( cf2_hint_isValid( &hintmap->edge[i] ) ); 393 FT_ASSERT( cf2_hint_isValid( &hintmap->edge[j] ) ); 394 FT_ASSERT( cf2_hint_isLocked( &hintmap->edge[i] ) == 395 cf2_hint_isLocked( &hintmap->edge[j] ) ); 396 397 if ( !cf2_hint_isLocked( &hintmap->edge[i] ) ) 398 { 399 /* hint edge is not locked, we can adjust it */ 400 CF2_Fixed fracDown = cf2_fixedFraction( hintmap->edge[i].dsCoord ); 401 CF2_Fixed fracUp = cf2_fixedFraction( hintmap->edge[j].dsCoord ); 402 403 /* calculate all four possibilities; moves down are negative */ 404 CF2_Fixed downMoveDown = 0 - fracDown; 405 CF2_Fixed upMoveDown = 0 - fracUp; 406 CF2_Fixed downMoveUp = fracDown == 0 407 ? 0 408 : cf2_intToFixed( 1 ) - fracDown; 409 CF2_Fixed upMoveUp = fracUp == 0 410 ? 0 411 : cf2_intToFixed( 1 ) - fracUp; 412 413 /* smallest move up */ 414 CF2_Fixed moveUp = FT_MIN( downMoveUp, upMoveUp ); 415 /* smallest move down */ 416 CF2_Fixed moveDown = FT_MAX( downMoveDown, upMoveDown ); 417 418 /* final amount to move edge or edge pair */ 419 CF2_Fixed move; 420 421 CF2_Fixed downMinCounter = CF2_MIN_COUNTER; 422 CF2_Fixed upMinCounter = CF2_MIN_COUNTER; 423 FT_Bool saveEdge = FALSE; 424 425 426 /* minimum counter constraint doesn't apply when adjacent edges */ 427 /* are synthetic */ 428 /* TODO: doesn't seem a big effect; for now, reduce the code */ 429#if 0 430 if ( i == 0 || 431 cf2_hint_isSynthetic( &hintmap->edge[i - 1] ) ) 432 downMinCounter = 0; 433 434 if ( j >= hintmap->count - 1 || 435 cf2_hint_isSynthetic( &hintmap->edge[j + 1] ) ) 436 upMinCounter = 0; 437#endif 438 439 /* is there room to move up? */ 440 /* there is if we are at top of array or the next edge is at or */ 441 /* beyond proposed move up? */ 442 if ( j >= hintmap->count - 1 || 443 hintmap->edge[j + 1].dsCoord >= 444 hintmap->edge[j].dsCoord + moveUp + upMinCounter ) 445 { 446 /* there is room to move up; is there also room to move down? */ 447 if ( i == 0 || 448 hintmap->edge[i - 1].dsCoord <= 449 hintmap->edge[i].dsCoord + moveDown - downMinCounter ) 450 { 451 /* move smaller absolute amount */ 452 move = ( -moveDown < moveUp ) ? moveDown : moveUp; /* optimum */ 453 } 454 else 455 move = moveUp; 456 } 457 else 458 { 459 /* is there room to move down? */ 460 if ( i == 0 || 461 hintmap->edge[i - 1].dsCoord <= 462 hintmap->edge[i].dsCoord + moveDown - downMinCounter ) 463 { 464 move = moveDown; 465 /* true if non-optimum move */ 466 saveEdge = (FT_Bool)( moveUp < -moveDown ); 467 } 468 else 469 { 470 /* no room to move either way without overlapping or reducing */ 471 /* the counter too much */ 472 move = 0; 473 saveEdge = TRUE; 474 } 475 } 476 477 /* Identify non-moves and moves down that aren't optimal, and save */ 478 /* them for second pass. */ 479 /* Do this only if there is an unlocked edge above (which could */ 480 /* possibly move). */ 481 if ( saveEdge && 482 j < hintmap->count - 1 && 483 !cf2_hint_isLocked( &hintmap->edge[j + 1] ) ) 484 { 485 CF2_HintMoveRec savedMove; 486 487 488 savedMove.j = j; 489 /* desired adjustment in second pass */ 490 savedMove.moveUp = moveUp - move; 491 492 cf2_arrstack_push( hintmap->hintMoves, &savedMove ); 493 } 494 495 /* move the edge(s) */ 496 hintmap->edge[i].dsCoord += move; 497 if ( isPair ) 498 hintmap->edge[j].dsCoord += move; 499 } 500 501 /* assert there are no overlaps in device space */ 502 FT_ASSERT( i == 0 || 503 hintmap->edge[i - 1].dsCoord <= hintmap->edge[i].dsCoord ); 504 FT_ASSERT( i < j || 505 hintmap->edge[i].dsCoord <= hintmap->edge[j].dsCoord ); 506 507 /* adjust the scales, avoiding divide by zero */ 508 if ( i > 0 ) 509 { 510 if ( hintmap->edge[i].csCoord != hintmap->edge[i - 1].csCoord ) 511 hintmap->edge[i - 1].scale = 512 FT_DivFix( 513 hintmap->edge[i].dsCoord - hintmap->edge[i - 1].dsCoord, 514 hintmap->edge[i].csCoord - hintmap->edge[i - 1].csCoord ); 515 } 516 517 if ( isPair ) 518 { 519 if ( hintmap->edge[j].csCoord != hintmap->edge[j - 1].csCoord ) 520 hintmap->edge[j - 1].scale = 521 FT_DivFix( 522 hintmap->edge[j].dsCoord - hintmap->edge[j - 1].dsCoord, 523 hintmap->edge[j].csCoord - hintmap->edge[j - 1].csCoord ); 524 525 i += 1; /* skip upper edge on next loop */ 526 } 527 } 528 529 /* second pass tries to move non-optimal hints up, in case there is */ 530 /* room now */ 531 for ( i = cf2_arrstack_size( hintmap->hintMoves ); i > 0; i-- ) 532 { 533 CF2_HintMove hintMove = (CF2_HintMove) 534 cf2_arrstack_getPointer( hintmap->hintMoves, i - 1 ); 535 536 537 j = hintMove->j; 538 539 /* this was tested before the push, above */ 540 FT_ASSERT( j < hintmap->count - 1 ); 541 542 /* is there room to move up? */ 543 if ( hintmap->edge[j + 1].dsCoord >= 544 hintmap->edge[j].dsCoord + hintMove->moveUp + CF2_MIN_COUNTER ) 545 { 546 /* there is more room now, move edge up */ 547 hintmap->edge[j].dsCoord += hintMove->moveUp; 548 549 if ( cf2_hint_isPair( &hintmap->edge[j] ) ) 550 { 551 FT_ASSERT( j > 0 ); 552 hintmap->edge[j - 1].dsCoord += hintMove->moveUp; 553 } 554 } 555 } 556 } 557 558 559 /* insert hint edges into map, sorted by csCoord */ 560 static void 561 cf2_hintmap_insertHint( CF2_HintMap hintmap, 562 CF2_Hint bottomHintEdge, 563 CF2_Hint topHintEdge ) 564 { 565 CF2_UInt indexInsert; 566 567 /* set default values, then check for edge hints */ 568 FT_Bool isPair = TRUE; 569 CF2_Hint firstHintEdge = bottomHintEdge; 570 CF2_Hint secondHintEdge = topHintEdge; 571 572 573 /* one or none of the input params may be invalid when dealing with */ 574 /* edge hints; at least one edge must be valid */ 575 FT_ASSERT( cf2_hint_isValid( bottomHintEdge ) || 576 cf2_hint_isValid( topHintEdge ) ); 577 578 /* determine how many and which edges to insert */ 579 if ( !cf2_hint_isValid( bottomHintEdge ) ) 580 { 581 /* insert only the top edge */ 582 firstHintEdge = topHintEdge; 583 isPair = FALSE; 584 } 585 else if ( !cf2_hint_isValid( topHintEdge ) ) 586 { 587 /* insert only the bottom edge */ 588 isPair = FALSE; 589 } 590 591 /* paired edges must be in proper order */ 592 FT_ASSERT( !isPair || 593 topHintEdge->csCoord >= bottomHintEdge->csCoord ); 594 595 /* linear search to find index value of insertion point */ 596 indexInsert = 0; 597 for ( ; indexInsert < hintmap->count; indexInsert++ ) 598 { 599 if ( hintmap->edge[indexInsert].csCoord >= firstHintEdge->csCoord ) 600 break; 601 } 602 603 /* 604 * Discard any hints that overlap in character space. Most often, this 605 * is while building the initial map, where captured hints from all 606 * zones are combined. Define overlap to include hints that `touch' 607 * (overlap zero). Hiragino Sans/Gothic fonts have numerous hints that 608 * touch. Some fonts have non-ideographic glyphs that overlap our 609 * synthetic hints. 610 * 611 * Overlap also occurs when darkening stem hints that are close. 612 * 613 */ 614 if ( indexInsert < hintmap->count ) 615 { 616 /* we are inserting before an existing edge: */ 617 /* verify that an existing edge is not the same */ 618 if ( hintmap->edge[indexInsert].csCoord == firstHintEdge->csCoord ) 619 return; /* ignore overlapping stem hint */ 620 621 /* verify that a new pair does not straddle the next edge */ 622 if ( isPair && 623 hintmap->edge[indexInsert].csCoord <= secondHintEdge->csCoord ) 624 return; /* ignore overlapping stem hint */ 625 626 /* verify that we are not inserting between paired edges */ 627 if ( cf2_hint_isPairTop( &hintmap->edge[indexInsert] ) ) 628 return; /* ignore overlapping stem hint */ 629 } 630 631 /* recompute device space locations using initial hint map */ 632 if ( cf2_hintmap_isValid( hintmap->initialHintMap ) && 633 !cf2_hint_isLocked( firstHintEdge ) ) 634 { 635 if ( isPair ) 636 { 637 /* Use hint map to position the center of stem, and nominal scale */ 638 /* to position the two edges. This preserves the stem width. */ 639 CF2_Fixed midpoint = cf2_hintmap_map( 640 hintmap->initialHintMap, 641 ( secondHintEdge->csCoord + 642 firstHintEdge->csCoord ) / 2 ); 643 CF2_Fixed halfWidth = FT_MulFix( 644 ( secondHintEdge->csCoord - 645 firstHintEdge->csCoord ) / 2, 646 hintmap->scale ); 647 648 649 firstHintEdge->dsCoord = midpoint - halfWidth; 650 secondHintEdge->dsCoord = midpoint + halfWidth; 651 } 652 else 653 firstHintEdge->dsCoord = cf2_hintmap_map( hintmap->initialHintMap, 654 firstHintEdge->csCoord ); 655 } 656 657 /* 658 * Discard any hints that overlap in device space; this can occur 659 * because locked hints have been moved to align with blue zones. 660 * 661 * TODO: Although we might correct this later during adjustment, we 662 * don't currently have a way to delete a conflicting hint once it has 663 * been inserted. See v2.030 MinionPro-Regular, 12 ppem darkened, 664 * initial hint map for second path, glyph 945 (the perispomeni (tilde) 665 * in U+1F6E, Greek omega with psili and perispomeni). Darkening is 666 * 25. Pair 667,747 initially conflicts in design space with top edge 667 * 660. This is because 667 maps to 7.87, and the top edge was 668 * captured by a zone at 8.0. The pair is later successfully inserted 669 * in a zone without the top edge. In this zone it is adjusted to 8.0, 670 * and no longer conflicts with the top edge in design space. This 671 * means it can be included in yet a later zone which does have the top 672 * edge hint. This produces a small mismatch between the first and 673 * last points of this path, even though the hint masks are the same. 674 * The density map difference is tiny (1/256). 675 * 676 */ 677 678 if ( indexInsert > 0 ) 679 { 680 /* we are inserting after an existing edge */ 681 if ( firstHintEdge->dsCoord < hintmap->edge[indexInsert - 1].dsCoord ) 682 return; 683 } 684 685 if ( indexInsert < hintmap->count ) 686 { 687 /* we are inserting before an existing edge */ 688 if ( isPair ) 689 { 690 if ( secondHintEdge->dsCoord > hintmap->edge[indexInsert].dsCoord ) 691 return; 692 } 693 else 694 { 695 if ( firstHintEdge->dsCoord > hintmap->edge[indexInsert].dsCoord ) 696 return; 697 } 698 } 699 700 /* make room to insert */ 701 { 702 CF2_Int iSrc = hintmap->count - 1; 703 CF2_Int iDst = isPair ? hintmap->count + 1 : hintmap->count; 704 705 CF2_Int count = hintmap->count - indexInsert; 706 707 708 if ( iDst >= CF2_MAX_HINT_EDGES ) 709 { 710 FT_TRACE4(( "cf2_hintmap_insertHint: too many hintmaps\n" )); 711 return; 712 } 713 714 while ( count-- ) 715 hintmap->edge[iDst--] = hintmap->edge[iSrc--]; 716 717 /* insert first edge */ 718 hintmap->edge[indexInsert] = *firstHintEdge; /* copy struct */ 719 hintmap->count += 1; 720 721 if ( isPair ) 722 { 723 /* insert second edge */ 724 hintmap->edge[indexInsert + 1] = *secondHintEdge; /* copy struct */ 725 hintmap->count += 1; 726 } 727 } 728 729 return; 730 } 731 732 733 /* 734 * Build a map from hints and mask. 735 * 736 * This function may recur one level if `hintmap->initialHintMap' is not yet 737 * valid. 738 * If `initialMap' is true, simply build initial map. 739 * 740 * Synthetic hints are used in two ways. A hint at zero is inserted, if 741 * needed, in the initial hint map, to prevent translations from 742 * propagating across the origin. If synthetic em box hints are enabled 743 * for ideographic dictionaries, then they are inserted in all hint 744 * maps, including the initial one. 745 * 746 */ 747 FT_LOCAL_DEF( void ) 748 cf2_hintmap_build( CF2_HintMap hintmap, 749 CF2_ArrStack hStemHintArray, 750 CF2_ArrStack vStemHintArray, 751 CF2_HintMask hintMask, 752 CF2_Fixed hintOrigin, 753 FT_Bool initialMap ) 754 { 755 FT_Byte* maskPtr; 756 757 CF2_Font font = hintmap->font; 758 CF2_HintMaskRec tempHintMask; 759 760 size_t bitCount, i; 761 FT_Byte maskByte; 762 763 764 /* check whether initial map is constructed */ 765 if ( !initialMap && !cf2_hintmap_isValid( hintmap->initialHintMap ) ) 766 { 767 /* make recursive call with initialHintMap and temporary mask; */ 768 /* temporary mask will get all bits set, below */ 769 cf2_hintmask_init( &tempHintMask, hintMask->error ); 770 cf2_hintmap_build( hintmap->initialHintMap, 771 hStemHintArray, 772 vStemHintArray, 773 &tempHintMask, 774 hintOrigin, 775 TRUE ); 776 } 777 778 if ( !cf2_hintmask_isValid( hintMask ) ) 779 { 780 /* without a hint mask, assume all hints are active */ 781 cf2_hintmask_setAll( hintMask, 782 cf2_arrstack_size( hStemHintArray ) + 783 cf2_arrstack_size( vStemHintArray ) ); 784 } 785 786 /* begin by clearing the map */ 787 hintmap->count = 0; 788 hintmap->lastIndex = 0; 789 790 /* make a copy of the hint mask so we can modify it */ 791 tempHintMask = *hintMask; 792 maskPtr = cf2_hintmask_getMaskPtr( &tempHintMask ); 793 794 /* use the hStem hints only, which are first in the mask */ 795 /* TODO: compare this to cffhintmaskGetBitCount */ 796 bitCount = cf2_arrstack_size( hStemHintArray ); 797 798 /* synthetic embox hints get highest priority */ 799 if ( font->blues.doEmBoxHints ) 800 { 801 CF2_HintRec dummy; 802 803 804 cf2_hint_initZero( &dummy ); /* invalid hint map element */ 805 806 /* ghost bottom */ 807 cf2_hintmap_insertHint( hintmap, 808 &font->blues.emBoxBottomEdge, 809 &dummy ); 810 /* ghost top */ 811 cf2_hintmap_insertHint( hintmap, 812 &dummy, 813 &font->blues.emBoxTopEdge ); 814 } 815 816 /* insert hints captured by a blue zone or already locked (higher */ 817 /* priority) */ 818 for ( i = 0, maskByte = 0x80; i < bitCount; i++ ) 819 { 820 if ( maskByte & *maskPtr ) 821 { 822 /* expand StemHint into two `CF2_Hint' elements */ 823 CF2_HintRec bottomHintEdge, topHintEdge; 824 825 826 cf2_hint_init( &bottomHintEdge, 827 hStemHintArray, 828 i, 829 font, 830 hintOrigin, 831 hintmap->scale, 832 TRUE /* bottom */ ); 833 cf2_hint_init( &topHintEdge, 834 hStemHintArray, 835 i, 836 font, 837 hintOrigin, 838 hintmap->scale, 839 FALSE /* top */ ); 840 841 if ( cf2_hint_isLocked( &bottomHintEdge ) || 842 cf2_hint_isLocked( &topHintEdge ) || 843 cf2_blues_capture( &font->blues, 844 &bottomHintEdge, 845 &topHintEdge ) ) 846 { 847 /* insert captured hint into map */ 848 cf2_hintmap_insertHint( hintmap, &bottomHintEdge, &topHintEdge ); 849 850 *maskPtr &= ~maskByte; /* turn off the bit for this hint */ 851 } 852 } 853 854 if ( ( i & 7 ) == 7 ) 855 { 856 /* move to next mask byte */ 857 maskPtr++; 858 maskByte = 0x80; 859 } 860 else 861 maskByte >>= 1; 862 } 863 864 /* initial hint map includes only captured hints plus maybe one at 0 */ 865 866 /* 867 * TODO: There is a problem here because we are trying to build a 868 * single hint map containing all captured hints. It is 869 * possible for there to be conflicts between captured hints, 870 * either because of darkening or because the hints are in 871 * separate hint zones (we are ignoring hint zones for the 872 * initial map). An example of the latter is MinionPro-Regular 873 * v2.030 glyph 883 (Greek Capital Alpha with Psili) at 15ppem. 874 * A stem hint for the psili conflicts with the top edge hint 875 * for the base character. The stem hint gets priority because 876 * of its sort order. In glyph 884 (Greek Capital Alpha with 877 * Psili and Oxia), the top of the base character gets a stem 878 * hint, and the psili does not. This creates different initial 879 * maps for the two glyphs resulting in different renderings of 880 * the base character. Will probably defer this either as not 881 * worth the cost or as a font bug. I don't think there is any 882 * good reason for an accent to be captured by an alignment 883 * zone. -darnold 2/12/10 884 */ 885 886 if ( initialMap ) 887 { 888 /* Apply a heuristic that inserts a point for (0,0), unless it's */ 889 /* already covered by a mapping. This locks the baseline for glyphs */ 890 /* that have no baseline hints. */ 891 892 if ( hintmap->count == 0 || 893 hintmap->edge[0].csCoord > 0 || 894 hintmap->edge[hintmap->count - 1].csCoord < 0 ) 895 { 896 /* all edges are above 0 or all edges are below 0; */ 897 /* construct a locked edge hint at 0 */ 898 899 CF2_HintRec edge, invalid; 900 901 902 cf2_hint_initZero( &edge ); 903 904 edge.flags = CF2_GhostBottom | 905 CF2_Locked | 906 CF2_Synthetic; 907 edge.scale = hintmap->scale; 908 909 cf2_hint_initZero( &invalid ); 910 cf2_hintmap_insertHint( hintmap, &edge, &invalid ); 911 } 912 } 913 else 914 { 915 /* insert remaining hints */ 916 917 maskPtr = cf2_hintmask_getMaskPtr( &tempHintMask ); 918 919 for ( i = 0, maskByte = 0x80; i < bitCount; i++ ) 920 { 921 if ( maskByte & *maskPtr ) 922 { 923 CF2_HintRec bottomHintEdge, topHintEdge; 924 925 926 cf2_hint_init( &bottomHintEdge, 927 hStemHintArray, 928 i, 929 font, 930 hintOrigin, 931 hintmap->scale, 932 TRUE /* bottom */ ); 933 cf2_hint_init( &topHintEdge, 934 hStemHintArray, 935 i, 936 font, 937 hintOrigin, 938 hintmap->scale, 939 FALSE /* top */ ); 940 941 cf2_hintmap_insertHint( hintmap, &bottomHintEdge, &topHintEdge ); 942 } 943 944 if ( ( i & 7 ) == 7 ) 945 { 946 /* move to next mask byte */ 947 maskPtr++; 948 maskByte = 0x80; 949 } 950 else 951 maskByte >>= 1; 952 } 953 } 954 955 /* 956 * Note: The following line is a convenient place to break when 957 * debugging hinting. Examine `hintmap->edge' for the list of 958 * enabled hints, then step over the call to see the effect of 959 * adjustment. We stop here first on the recursive call that 960 * creates the initial map, and then on each counter group and 961 * hint zone. 962 */ 963 964 /* adjust positions of hint edges that are not locked to blue zones */ 965 cf2_hintmap_adjustHints( hintmap ); 966 967 /* save the position of all hints that were used in this hint map; */ 968 /* if we use them again, we'll locate them in the same position */ 969 if ( !initialMap ) 970 { 971 for ( i = 0; i < hintmap->count; i++ ) 972 { 973 if ( !cf2_hint_isSynthetic( &hintmap->edge[i] ) ) 974 { 975 /* Note: include both valid and invalid edges */ 976 /* Note: top and bottom edges are copied back separately */ 977 CF2_StemHint stemhint = (CF2_StemHint) 978 cf2_arrstack_getPointer( hStemHintArray, 979 hintmap->edge[i].index ); 980 981 982 if ( cf2_hint_isTop( &hintmap->edge[i] ) ) 983 stemhint->maxDS = hintmap->edge[i].dsCoord; 984 else 985 stemhint->minDS = hintmap->edge[i].dsCoord; 986 987 stemhint->used = TRUE; 988 } 989 } 990 } 991 992 /* hint map is ready to use */ 993 hintmap->isValid = TRUE; 994 995 /* remember this mask has been used */ 996 cf2_hintmask_setNew( hintMask, FALSE ); 997 } 998 999 1000 FT_LOCAL_DEF( void ) 1001 cf2_glyphpath_init( CF2_GlyphPath glyphpath, 1002 CF2_Font font, 1003 CF2_OutlineCallbacks callbacks, 1004 CF2_Fixed scaleY, 1005 /* CF2_Fixed hShift, */ 1006 CF2_ArrStack hStemHintArray, 1007 CF2_ArrStack vStemHintArray, 1008 CF2_HintMask hintMask, 1009 CF2_Fixed hintOriginY, 1010 const CF2_Blues blues, 1011 const FT_Vector* fractionalTranslation ) 1012 { 1013 FT_ZERO( glyphpath ); 1014 1015 glyphpath->font = font; 1016 glyphpath->callbacks = callbacks; 1017 1018 cf2_arrstack_init( &glyphpath->hintMoves, 1019 font->memory, 1020 &font->error, 1021 sizeof ( CF2_HintMoveRec ) ); 1022 1023 cf2_hintmap_init( &glyphpath->initialHintMap, 1024 font, 1025 &glyphpath->initialHintMap, 1026 &glyphpath->hintMoves, 1027 scaleY ); 1028 cf2_hintmap_init( &glyphpath->firstHintMap, 1029 font, 1030 &glyphpath->initialHintMap, 1031 &glyphpath->hintMoves, 1032 scaleY ); 1033 cf2_hintmap_init( &glyphpath->hintMap, 1034 font, 1035 &glyphpath->initialHintMap, 1036 &glyphpath->hintMoves, 1037 scaleY ); 1038 1039 glyphpath->scaleX = font->innerTransform.a; 1040 glyphpath->scaleC = font->innerTransform.c; 1041 glyphpath->scaleY = font->innerTransform.d; 1042 1043 glyphpath->fractionalTranslation = *fractionalTranslation; 1044 1045#if 0 1046 glyphpath->hShift = hShift; /* for fauxing */ 1047#endif 1048 1049 glyphpath->hStemHintArray = hStemHintArray; 1050 glyphpath->vStemHintArray = vStemHintArray; 1051 glyphpath->hintMask = hintMask; /* ptr to current mask */ 1052 glyphpath->hintOriginY = hintOriginY; 1053 glyphpath->blues = blues; 1054 glyphpath->darken = font->darkened; /* TODO: should we make copies? */ 1055 glyphpath->xOffset = font->darkenX; 1056 glyphpath->yOffset = font->darkenY; 1057 glyphpath->miterLimit = 2 * FT_MAX( 1058 cf2_fixedAbs( glyphpath->xOffset ), 1059 cf2_fixedAbs( glyphpath->yOffset ) ); 1060 1061 /* .1 character space unit */ 1062 glyphpath->snapThreshold = cf2_floatToFixed( 0.1f ); 1063 1064 glyphpath->moveIsPending = TRUE; 1065 glyphpath->pathIsOpen = FALSE; 1066 glyphpath->pathIsClosing = FALSE; 1067 glyphpath->elemIsQueued = FALSE; 1068 } 1069 1070 1071 FT_LOCAL_DEF( void ) 1072 cf2_glyphpath_finalize( CF2_GlyphPath glyphpath ) 1073 { 1074 cf2_arrstack_finalize( &glyphpath->hintMoves ); 1075 } 1076 1077 1078 /* 1079 * Hint point in y-direction and apply outerTransform. 1080 * Input `current' hint map (which is actually delayed by one element). 1081 * Input x,y point in Character Space. 1082 * Output x,y point in Device Space, including translation. 1083 */ 1084 static void 1085 cf2_glyphpath_hintPoint( CF2_GlyphPath glyphpath, 1086 CF2_HintMap hintmap, 1087 FT_Vector* ppt, 1088 CF2_Fixed x, 1089 CF2_Fixed y ) 1090 { 1091 FT_Vector pt; /* hinted point in upright DS */ 1092 1093 1094 pt.x = FT_MulFix( glyphpath->scaleX, x ) + 1095 FT_MulFix( glyphpath->scaleC, y ); 1096 pt.y = cf2_hintmap_map( hintmap, y ); 1097 1098 ppt->x = FT_MulFix( glyphpath->font->outerTransform.a, pt.x ) + 1099 FT_MulFix( glyphpath->font->outerTransform.c, pt.y ) + 1100 glyphpath->fractionalTranslation.x; 1101 ppt->y = FT_MulFix( glyphpath->font->outerTransform.b, pt.x ) + 1102 FT_MulFix( glyphpath->font->outerTransform.d, pt.y ) + 1103 glyphpath->fractionalTranslation.y; 1104 } 1105 1106 1107 /* 1108 * From two line segments, (u1,u2) and (v1,v2), compute a point of 1109 * intersection on the corresponding lines. 1110 * Return false if no intersection is found, or if the intersection is 1111 * too far away from the ends of the line segments, u2 and v1. 1112 * 1113 */ 1114 static FT_Bool 1115 cf2_glyphpath_computeIntersection( CF2_GlyphPath glyphpath, 1116 const FT_Vector* u1, 1117 const FT_Vector* u2, 1118 const FT_Vector* v1, 1119 const FT_Vector* v2, 1120 FT_Vector* intersection ) 1121 { 1122 /* 1123 * Let `u' be a zero-based vector from the first segment, `v' from the 1124 * second segment. 1125 * Let `w 'be the zero-based vector from `u1' to `v1'. 1126 * `perp' is the `perpendicular dot product'; see 1127 * http://mathworld.wolfram.com/PerpDotProduct.html. 1128 * `s' is the parameter for the parametric line for the first segment 1129 * (`u'). 1130 * 1131 * See notation in 1132 * http://softsurfer.com/Archive/algorithm_0104/algorithm_0104B.htm. 1133 * Calculations are done in 16.16, but must handle the squaring of 1134 * line lengths in character space. We scale all vectors by 1/32 to 1135 * avoid overflow. This allows values up to 4095 to be squared. The 1136 * scale factor cancels in the divide. 1137 * 1138 * TODO: the scale factor could be computed from UnitsPerEm. 1139 * 1140 */ 1141 1142#define cf2_perp( a, b ) \ 1143 ( FT_MulFix( a.x, b.y ) - FT_MulFix( a.y, b.x ) ) 1144 1145 /* round and divide by 32 */ 1146#define CF2_CS_SCALE( x ) \ 1147 ( ( (x) + 0x10 ) >> 5 ) 1148 1149 FT_Vector u, v, w; /* scaled vectors */ 1150 CF2_Fixed denominator, s; 1151 1152 1153 u.x = CF2_CS_SCALE( u2->x - u1->x ); 1154 u.y = CF2_CS_SCALE( u2->y - u1->y ); 1155 v.x = CF2_CS_SCALE( v2->x - v1->x ); 1156 v.y = CF2_CS_SCALE( v2->y - v1->y ); 1157 w.x = CF2_CS_SCALE( v1->x - u1->x ); 1158 w.y = CF2_CS_SCALE( v1->y - u1->y ); 1159 1160 denominator = cf2_perp( u, v ); 1161 1162 if ( denominator == 0 ) 1163 return FALSE; /* parallel or coincident lines */ 1164 1165 s = FT_DivFix( cf2_perp( w, v ), denominator ); 1166 1167 intersection->x = u1->x + FT_MulFix( s, u2->x - u1->x ); 1168 intersection->y = u1->y + FT_MulFix( s, u2->y - u1->y ); 1169 1170 /* 1171 * Special case snapping for horizontal and vertical lines. 1172 * This cleans up intersections and reduces problems with winding 1173 * order detection. 1174 * Sample case is sbc cd KozGoPr6N-Medium.otf 20 16685. 1175 * Note: these calculations are in character space. 1176 * 1177 */ 1178 1179 if ( u1->x == u2->x && 1180 cf2_fixedAbs( intersection->x - u1->x ) < glyphpath->snapThreshold ) 1181 intersection->x = u1->x; 1182 if ( u1->y == u2->y && 1183 cf2_fixedAbs( intersection->y - u1->y ) < glyphpath->snapThreshold ) 1184 intersection->y = u1->y; 1185 1186 if ( v1->x == v2->x && 1187 cf2_fixedAbs( intersection->x - v1->x ) < glyphpath->snapThreshold ) 1188 intersection->x = v1->x; 1189 if ( v1->y == v2->y && 1190 cf2_fixedAbs( intersection->y - v1->y ) < glyphpath->snapThreshold ) 1191 intersection->y = v1->y; 1192 1193 /* limit the intersection distance from midpoint of u2 and v1 */ 1194 if ( cf2_fixedAbs( intersection->x - ( u2->x + v1->x ) / 2 ) > 1195 glyphpath->miterLimit || 1196 cf2_fixedAbs( intersection->y - ( u2->y + v1->y ) / 2 ) > 1197 glyphpath->miterLimit ) 1198 return FALSE; 1199 1200 return TRUE; 1201 } 1202 1203 1204 /* 1205 * Push the cached element (glyphpath->prevElem*) to the outline 1206 * consumer. When a darkening offset is used, the end point of the 1207 * cached element may be adjusted to an intersection point or we may 1208 * synthesize a connecting line to the current element. If we are 1209 * closing a subpath, we may also generate a connecting line to the start 1210 * point. 1211 * 1212 * This is where Character Space (CS) is converted to Device Space (DS) 1213 * using a hint map. This calculation must use a HintMap that was valid 1214 * at the time the element was saved. For the first point in a subpath, 1215 * that is a saved HintMap. For most elements, it just means the caller 1216 * has delayed building a HintMap from the current HintMask. 1217 * 1218 * Transform each point with outerTransform and call the outline 1219 * callbacks. This is a general 3x3 transform: 1220 * 1221 * x' = a*x + c*y + tx, y' = b*x + d*y + ty 1222 * 1223 * but it uses 4 elements from CF2_Font and the translation part 1224 * from CF2_GlyphPath. 1225 * 1226 */ 1227 static void 1228 cf2_glyphpath_pushPrevElem( CF2_GlyphPath glyphpath, 1229 CF2_HintMap hintmap, 1230 FT_Vector* nextP0, 1231 FT_Vector nextP1, 1232 FT_Bool close ) 1233 { 1234 CF2_CallbackParamsRec params; 1235 1236 FT_Vector* prevP0; 1237 FT_Vector* prevP1; 1238 1239 FT_Vector intersection = { 0, 0 }; 1240 FT_Bool useIntersection = FALSE; 1241 1242 1243 FT_ASSERT( glyphpath->prevElemOp == CF2_PathOpLineTo || 1244 glyphpath->prevElemOp == CF2_PathOpCubeTo ); 1245 1246 if ( glyphpath->prevElemOp == CF2_PathOpLineTo ) 1247 { 1248 prevP0 = &glyphpath->prevElemP0; 1249 prevP1 = &glyphpath->prevElemP1; 1250 } 1251 else 1252 { 1253 prevP0 = &glyphpath->prevElemP2; 1254 prevP1 = &glyphpath->prevElemP3; 1255 } 1256 1257 /* optimization: if previous and next elements are offset by the same */ 1258 /* amount, then there will be no gap, and no need to compute an */ 1259 /* intersection. */ 1260 if ( prevP1->x != nextP0->x || prevP1->y != nextP0->y ) 1261 { 1262 /* previous element does not join next element: */ 1263 /* adjust end point of previous element to the intersection */ 1264 useIntersection = cf2_glyphpath_computeIntersection( glyphpath, 1265 prevP0, 1266 prevP1, 1267 nextP0, 1268 &nextP1, 1269 &intersection ); 1270 if ( useIntersection ) 1271 { 1272 /* modify the last point of the cached element (either line or */ 1273 /* curve) */ 1274 *prevP1 = intersection; 1275 } 1276 } 1277 1278 params.pt0 = glyphpath->currentDS; 1279 1280 switch( glyphpath->prevElemOp ) 1281 { 1282 case CF2_PathOpLineTo: 1283 params.op = CF2_PathOpLineTo; 1284 1285 /* note: pt2 and pt3 are unused */ 1286 1287 if ( close ) 1288 { 1289 /* use first hint map if closing */ 1290 cf2_glyphpath_hintPoint( glyphpath, 1291 &glyphpath->firstHintMap, 1292 ¶ms.pt1, 1293 glyphpath->prevElemP1.x, 1294 glyphpath->prevElemP1.y ); 1295 } 1296 else 1297 { 1298 cf2_glyphpath_hintPoint( glyphpath, 1299 hintmap, 1300 ¶ms.pt1, 1301 glyphpath->prevElemP1.x, 1302 glyphpath->prevElemP1.y ); 1303 } 1304 1305 /* output only non-zero length lines */ 1306 if ( params.pt0.x != params.pt1.x || params.pt0.y != params.pt1.y ) 1307 { 1308 glyphpath->callbacks->lineTo( glyphpath->callbacks, ¶ms ); 1309 1310 glyphpath->currentDS = params.pt1; 1311 } 1312 break; 1313 1314 case CF2_PathOpCubeTo: 1315 params.op = CF2_PathOpCubeTo; 1316 1317 /* TODO: should we intersect the interior joins (p1-p2 and p2-p3)? */ 1318 cf2_glyphpath_hintPoint( glyphpath, 1319 hintmap, 1320 ¶ms.pt1, 1321 glyphpath->prevElemP1.x, 1322 glyphpath->prevElemP1.y ); 1323 cf2_glyphpath_hintPoint( glyphpath, 1324 hintmap, 1325 ¶ms.pt2, 1326 glyphpath->prevElemP2.x, 1327 glyphpath->prevElemP2.y ); 1328 cf2_glyphpath_hintPoint( glyphpath, 1329 hintmap, 1330 ¶ms.pt3, 1331 glyphpath->prevElemP3.x, 1332 glyphpath->prevElemP3.y ); 1333 1334 glyphpath->callbacks->cubeTo( glyphpath->callbacks, ¶ms ); 1335 1336 glyphpath->currentDS = params.pt3; 1337 1338 break; 1339 } 1340 1341 if ( !useIntersection || close ) 1342 { 1343 /* insert connecting line between end of previous element and start */ 1344 /* of current one */ 1345 /* note: at the end of a subpath, we might do both, so use `nextP0' */ 1346 /* before we change it, below */ 1347 1348 if ( close ) 1349 { 1350 /* if we are closing the subpath, then nextP0 is in the first */ 1351 /* hint zone */ 1352 cf2_glyphpath_hintPoint( glyphpath, 1353 &glyphpath->firstHintMap, 1354 ¶ms.pt1, 1355 nextP0->x, 1356 nextP0->y ); 1357 } 1358 else 1359 { 1360 cf2_glyphpath_hintPoint( glyphpath, 1361 hintmap, 1362 ¶ms.pt1, 1363 nextP0->x, 1364 nextP0->y ); 1365 } 1366 1367 if ( params.pt1.x != glyphpath->currentDS.x || 1368 params.pt1.y != glyphpath->currentDS.y ) 1369 { 1370 /* length is nonzero */ 1371 params.op = CF2_PathOpLineTo; 1372 params.pt0 = glyphpath->currentDS; 1373 1374 /* note: pt2 and pt3 are unused */ 1375 glyphpath->callbacks->lineTo( glyphpath->callbacks, ¶ms ); 1376 1377 glyphpath->currentDS = params.pt1; 1378 } 1379 } 1380 1381 if ( useIntersection ) 1382 { 1383 /* return intersection point to caller */ 1384 *nextP0 = intersection; 1385 } 1386 } 1387 1388 1389 /* push a MoveTo element based on current point and offset of current */ 1390 /* element */ 1391 static void 1392 cf2_glyphpath_pushMove( CF2_GlyphPath glyphpath, 1393 FT_Vector start ) 1394 { 1395 CF2_CallbackParamsRec params; 1396 1397 1398 params.op = CF2_PathOpMoveTo; 1399 params.pt0 = glyphpath->currentDS; 1400 1401 /* Test if move has really happened yet; it would have called */ 1402 /* `cf2_hintmap_build' to set `isValid'. */ 1403 if ( !cf2_hintmap_isValid( &glyphpath->hintMap ) ) 1404 { 1405 /* we are here iff first subpath is missing a moveto operator: */ 1406 /* synthesize first moveTo to finish initialization of hintMap */ 1407 cf2_glyphpath_moveTo( glyphpath, 1408 glyphpath->start.x, 1409 glyphpath->start.y ); 1410 } 1411 1412 cf2_glyphpath_hintPoint( glyphpath, 1413 &glyphpath->hintMap, 1414 ¶ms.pt1, 1415 start.x, 1416 start.y ); 1417 1418 /* note: pt2 and pt3 are unused */ 1419 glyphpath->callbacks->moveTo( glyphpath->callbacks, ¶ms ); 1420 1421 glyphpath->currentDS = params.pt1; 1422 glyphpath->offsetStart0 = start; 1423 } 1424 1425 1426 /* 1427 * All coordinates are in character space. 1428 * On input, (x1, y1) and (x2, y2) give line segment. 1429 * On output, (x, y) give offset vector. 1430 * We use a piecewise approximation to trig functions. 1431 * 1432 * TODO: Offset true perpendicular and proper length 1433 * supply the y-translation for hinting here, too, 1434 * that adds yOffset unconditionally to *y. 1435 */ 1436 static void 1437 cf2_glyphpath_computeOffset( CF2_GlyphPath glyphpath, 1438 CF2_Fixed x1, 1439 CF2_Fixed y1, 1440 CF2_Fixed x2, 1441 CF2_Fixed y2, 1442 CF2_Fixed* x, 1443 CF2_Fixed* y ) 1444 { 1445 CF2_Fixed dx = x2 - x1; 1446 CF2_Fixed dy = y2 - y1; 1447 1448 1449 /* note: negative offsets don't work here; negate deltas to change */ 1450 /* quadrants, below */ 1451 if ( glyphpath->font->reverseWinding ) 1452 { 1453 dx = -dx; 1454 dy = -dy; 1455 } 1456 1457 *x = *y = 0; 1458 1459 if ( !glyphpath->darken ) 1460 return; 1461 1462 /* add momentum for this path element */ 1463 glyphpath->callbacks->windingMomentum += 1464 cf2_getWindingMomentum( x1, y1, x2, y2 ); 1465 1466 /* note: allow mixed integer and fixed multiplication here */ 1467 if ( dx >= 0 ) 1468 { 1469 if ( dy >= 0 ) 1470 { 1471 /* first quadrant, +x +y */ 1472 1473 if ( dx > 2 * dy ) 1474 { 1475 /* +x */ 1476 *x = 0; 1477 *y = 0; 1478 } 1479 else if ( dy > 2 * dx ) 1480 { 1481 /* +y */ 1482 *x = glyphpath->xOffset; 1483 *y = glyphpath->yOffset; 1484 } 1485 else 1486 { 1487 /* +x +y */ 1488 *x = FT_MulFix( cf2_floatToFixed( 0.7 ), 1489 glyphpath->xOffset ); 1490 *y = FT_MulFix( cf2_floatToFixed( 1.0 - 0.7 ), 1491 glyphpath->yOffset ); 1492 } 1493 } 1494 else 1495 { 1496 /* fourth quadrant, +x -y */ 1497 1498 if ( dx > -2 * dy ) 1499 { 1500 /* +x */ 1501 *x = 0; 1502 *y = 0; 1503 } 1504 else if ( -dy > 2 * dx ) 1505 { 1506 /* -y */ 1507 *x = -glyphpath->xOffset; 1508 *y = glyphpath->yOffset; 1509 } 1510 else 1511 { 1512 /* +x -y */ 1513 *x = FT_MulFix( cf2_floatToFixed( -0.7 ), 1514 glyphpath->xOffset ); 1515 *y = FT_MulFix( cf2_floatToFixed( 1.0 - 0.7 ), 1516 glyphpath->yOffset ); 1517 } 1518 } 1519 } 1520 else 1521 { 1522 if ( dy >= 0 ) 1523 { 1524 /* second quadrant, -x +y */ 1525 1526 if ( -dx > 2 * dy ) 1527 { 1528 /* -x */ 1529 *x = 0; 1530 *y = 2 * glyphpath->yOffset; 1531 } 1532 else if ( dy > -2 * dx ) 1533 { 1534 /* +y */ 1535 *x = glyphpath->xOffset; 1536 *y = glyphpath->yOffset; 1537 } 1538 else 1539 { 1540 /* -x +y */ 1541 *x = FT_MulFix( cf2_floatToFixed( 0.7 ), 1542 glyphpath->xOffset ); 1543 *y = FT_MulFix( cf2_floatToFixed( 1.0 + 0.7 ), 1544 glyphpath->yOffset ); 1545 } 1546 } 1547 else 1548 { 1549 /* third quadrant, -x -y */ 1550 1551 if ( -dx > -2 * dy ) 1552 { 1553 /* -x */ 1554 *x = 0; 1555 *y = 2 * glyphpath->yOffset; 1556 } 1557 else if ( -dy > -2 * dx ) 1558 { 1559 /* -y */ 1560 *x = -glyphpath->xOffset; 1561 *y = glyphpath->xOffset; 1562 } 1563 else 1564 { 1565 /* -x -y */ 1566 *x = FT_MulFix( cf2_floatToFixed( -0.7 ), 1567 glyphpath->xOffset ); 1568 *y = FT_MulFix( cf2_floatToFixed( 1.0 + 0.7 ), 1569 glyphpath->yOffset ); 1570 } 1571 } 1572 } 1573 } 1574 1575 1576 /* 1577 * The functions cf2_glyphpath_{moveTo,lineTo,curveTo,closeOpenPath} are 1578 * called by the interpreter with Character Space (CS) coordinates. Each 1579 * path element is placed into a queue of length one to await the 1580 * calculation of the following element. At that time, the darkening 1581 * offset of the following element is known and joins can be computed, 1582 * including possible modification of this element, before mapping to 1583 * Device Space (DS) and passing it on to the outline consumer. 1584 * 1585 */ 1586 FT_LOCAL_DEF( void ) 1587 cf2_glyphpath_moveTo( CF2_GlyphPath glyphpath, 1588 CF2_Fixed x, 1589 CF2_Fixed y ) 1590 { 1591 cf2_glyphpath_closeOpenPath( glyphpath ); 1592 1593 /* save the parameters of the move for later, when we'll know how to */ 1594 /* offset it; */ 1595 /* also save last move point */ 1596 glyphpath->currentCS.x = glyphpath->start.x = x; 1597 glyphpath->currentCS.y = glyphpath->start.y = y; 1598 1599 glyphpath->moveIsPending = TRUE; 1600 1601 /* ensure we have a valid map with current mask */ 1602 if ( !cf2_hintmap_isValid( &glyphpath->hintMap ) || 1603 cf2_hintmask_isNew( glyphpath->hintMask ) ) 1604 cf2_hintmap_build( &glyphpath->hintMap, 1605 glyphpath->hStemHintArray, 1606 glyphpath->vStemHintArray, 1607 glyphpath->hintMask, 1608 glyphpath->hintOriginY, 1609 FALSE ); 1610 1611 /* save a copy of current HintMap to use when drawing initial point */ 1612 glyphpath->firstHintMap = glyphpath->hintMap; /* structure copy */ 1613 } 1614 1615 1616 FT_LOCAL_DEF( void ) 1617 cf2_glyphpath_lineTo( CF2_GlyphPath glyphpath, 1618 CF2_Fixed x, 1619 CF2_Fixed y ) 1620 { 1621 CF2_Fixed xOffset, yOffset; 1622 FT_Vector P0, P1; 1623 FT_Bool newHintMap; 1624 1625 /* 1626 * New hints will be applied after cf2_glyphpath_pushPrevElem has run. 1627 * In case this is a synthesized closing line, any new hints should be 1628 * delayed until this path is closed (`cf2_hintmask_isNew' will be 1629 * called again before the next line or curve). 1630 */ 1631 1632 /* true if new hint map not on close */ 1633 newHintMap = cf2_hintmask_isNew( glyphpath->hintMask ) && 1634 !glyphpath->pathIsClosing; 1635 1636 /* 1637 * Zero-length lines may occur in the charstring. Because we cannot 1638 * compute darkening offsets or intersections from zero-length lines, 1639 * it is best to remove them and avoid artifacts. However, zero-length 1640 * lines in CS at the start of a new hint map can generate non-zero 1641 * lines in DS due to hint substitution. We detect a change in hint 1642 * map here and pass those zero-length lines along. 1643 */ 1644 1645 /* 1646 * Note: Find explicitly closed paths here with a conditional 1647 * breakpoint using 1648 * 1649 * !gp->pathIsClosing && gp->start.x == x && gp->start.y == y 1650 * 1651 */ 1652 1653 if ( glyphpath->currentCS.x == x && 1654 glyphpath->currentCS.y == y && 1655 !newHintMap ) 1656 /* 1657 * Ignore zero-length lines in CS where the hint map is the same 1658 * because the line in DS will also be zero length. 1659 * 1660 * Ignore zero-length lines when we synthesize a closing line because 1661 * the close will be handled in cf2_glyphPath_pushPrevElem. 1662 */ 1663 return; 1664 1665 cf2_glyphpath_computeOffset( glyphpath, 1666 glyphpath->currentCS.x, 1667 glyphpath->currentCS.y, 1668 x, 1669 y, 1670 &xOffset, 1671 &yOffset ); 1672 1673 /* construct offset points */ 1674 P0.x = glyphpath->currentCS.x + xOffset; 1675 P0.y = glyphpath->currentCS.y + yOffset; 1676 P1.x = x + xOffset; 1677 P1.y = y + yOffset; 1678 1679 if ( glyphpath->moveIsPending ) 1680 { 1681 /* emit offset 1st point as MoveTo */ 1682 cf2_glyphpath_pushMove( glyphpath, P0 ); 1683 1684 glyphpath->moveIsPending = FALSE; /* adjust state machine */ 1685 glyphpath->pathIsOpen = TRUE; 1686 1687 glyphpath->offsetStart1 = P1; /* record second point */ 1688 } 1689 1690 if ( glyphpath->elemIsQueued ) 1691 { 1692 FT_ASSERT( cf2_hintmap_isValid( &glyphpath->hintMap ) ); 1693 1694 cf2_glyphpath_pushPrevElem( glyphpath, 1695 &glyphpath->hintMap, 1696 &P0, 1697 P1, 1698 FALSE ); 1699 } 1700 1701 /* queue the current element with offset points */ 1702 glyphpath->elemIsQueued = TRUE; 1703 glyphpath->prevElemOp = CF2_PathOpLineTo; 1704 glyphpath->prevElemP0 = P0; 1705 glyphpath->prevElemP1 = P1; 1706 1707 /* update current map */ 1708 if ( newHintMap ) 1709 cf2_hintmap_build( &glyphpath->hintMap, 1710 glyphpath->hStemHintArray, 1711 glyphpath->vStemHintArray, 1712 glyphpath->hintMask, 1713 glyphpath->hintOriginY, 1714 FALSE ); 1715 1716 glyphpath->currentCS.x = x; /* pre-offset current point */ 1717 glyphpath->currentCS.y = y; 1718 } 1719 1720 1721 FT_LOCAL_DEF( void ) 1722 cf2_glyphpath_curveTo( CF2_GlyphPath glyphpath, 1723 CF2_Fixed x1, 1724 CF2_Fixed y1, 1725 CF2_Fixed x2, 1726 CF2_Fixed y2, 1727 CF2_Fixed x3, 1728 CF2_Fixed y3 ) 1729 { 1730 CF2_Fixed xOffset1, yOffset1, xOffset3, yOffset3; 1731 FT_Vector P0, P1, P2, P3; 1732 1733 1734 /* TODO: ignore zero length portions of curve?? */ 1735 cf2_glyphpath_computeOffset( glyphpath, 1736 glyphpath->currentCS.x, 1737 glyphpath->currentCS.y, 1738 x1, 1739 y1, 1740 &xOffset1, 1741 &yOffset1 ); 1742 cf2_glyphpath_computeOffset( glyphpath, 1743 x2, 1744 y2, 1745 x3, 1746 y3, 1747 &xOffset3, 1748 &yOffset3 ); 1749 1750 /* add momentum from the middle segment */ 1751 glyphpath->callbacks->windingMomentum += 1752 cf2_getWindingMomentum( x1, y1, x2, y2 ); 1753 1754 /* construct offset points */ 1755 P0.x = glyphpath->currentCS.x + xOffset1; 1756 P0.y = glyphpath->currentCS.y + yOffset1; 1757 P1.x = x1 + xOffset1; 1758 P1.y = y1 + yOffset1; 1759 /* note: preserve angle of final segment by using offset3 at both ends */ 1760 P2.x = x2 + xOffset3; 1761 P2.y = y2 + yOffset3; 1762 P3.x = x3 + xOffset3; 1763 P3.y = y3 + yOffset3; 1764 1765 if ( glyphpath->moveIsPending ) 1766 { 1767 /* emit offset 1st point as MoveTo */ 1768 cf2_glyphpath_pushMove( glyphpath, P0 ); 1769 1770 glyphpath->moveIsPending = FALSE; 1771 glyphpath->pathIsOpen = TRUE; 1772 1773 glyphpath->offsetStart1 = P1; /* record second point */ 1774 } 1775 1776 if ( glyphpath->elemIsQueued ) 1777 { 1778 FT_ASSERT( cf2_hintmap_isValid( &glyphpath->hintMap ) ); 1779 1780 cf2_glyphpath_pushPrevElem( glyphpath, 1781 &glyphpath->hintMap, 1782 &P0, 1783 P1, 1784 FALSE ); 1785 } 1786 1787 /* queue the current element with offset points */ 1788 glyphpath->elemIsQueued = TRUE; 1789 glyphpath->prevElemOp = CF2_PathOpCubeTo; 1790 glyphpath->prevElemP0 = P0; 1791 glyphpath->prevElemP1 = P1; 1792 glyphpath->prevElemP2 = P2; 1793 glyphpath->prevElemP3 = P3; 1794 1795 /* update current map */ 1796 if ( cf2_hintmask_isNew( glyphpath->hintMask ) ) 1797 cf2_hintmap_build( &glyphpath->hintMap, 1798 glyphpath->hStemHintArray, 1799 glyphpath->vStemHintArray, 1800 glyphpath->hintMask, 1801 glyphpath->hintOriginY, 1802 FALSE ); 1803 1804 glyphpath->currentCS.x = x3; /* pre-offset current point */ 1805 glyphpath->currentCS.y = y3; 1806 } 1807 1808 1809 FT_LOCAL_DEF( void ) 1810 cf2_glyphpath_closeOpenPath( CF2_GlyphPath glyphpath ) 1811 { 1812 if ( glyphpath->pathIsOpen ) 1813 { 1814 /* 1815 * A closing line in Character Space line is always generated below 1816 * with `cf2_glyphPath_lineTo'. It may be ignored later if it turns 1817 * out to be zero length in Device Space. 1818 */ 1819 glyphpath->pathIsClosing = TRUE; 1820 1821 cf2_glyphpath_lineTo( glyphpath, 1822 glyphpath->start.x, 1823 glyphpath->start.y ); 1824 1825 /* empty the final element from the queue and close the path */ 1826 if ( glyphpath->elemIsQueued ) 1827 cf2_glyphpath_pushPrevElem( glyphpath, 1828 &glyphpath->hintMap, 1829 &glyphpath->offsetStart0, 1830 glyphpath->offsetStart1, 1831 TRUE ); 1832 1833 /* reset state machine */ 1834 glyphpath->moveIsPending = TRUE; 1835 glyphpath->pathIsOpen = FALSE; 1836 glyphpath->pathIsClosing = FALSE; 1837 glyphpath->elemIsQueued = FALSE; 1838 } 1839 } 1840 1841 1842/* END */ 1843