1 /****************************************************************************
5 * Adobe's code for handling CFF hints (body).
7 * Copyright 2007-2014 Adobe Systems Incorporated.
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.
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.
40 #include <freetype/internal/ftdebug.h>
48 /**************************************************************************
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.
55 #define FT_COMPONENT cf2hints
58 typedef struct CF2_HintMoveRec_
60 size_t j; /* index of upper hint map edge */
61 CF2_Fixed moveUp; /* adjustment to optimum position */
63 } CF2_HintMoveRec, *CF2_HintMove;
66 /* Compute angular momentum for winding order detection. It is called */
67 /* for all lines and curves, but not necessarily in element order. */
69 cf2_getWindingMomentum( CF2_Fixed x1,
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 */
77 return ( x1 >> 16 ) * ( SUB_INT32( y2, y1 ) >> 16 ) -
78 ( y1 >> 16 ) * ( SUB_INT32( x2, x1 ) >> 16 );
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.
90 cf2_hint_init( CF2_Hint hint,
91 const CF2_ArrStack stemHintArray,
99 const CF2_StemHintRec* stemHint;
104 stemHint = (const CF2_StemHintRec*)cf2_arrstack_getPointer(
108 width = SUB_INT32( stemHint->max, stemHint->min );
110 if ( width == cf2_intToFixed( -21 ) )
116 hint->csCoord = stemHint->max;
117 hint->flags = CF2_GhostBottom;
123 else if ( width == cf2_intToFixed( -20 ) )
131 hint->csCoord = stemHint->min;
132 hint->flags = CF2_GhostTop;
136 else if ( width < 0 )
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
145 * All other negative widths have undefined meaning.
147 * CoolType has a silent workaround that negates the hint width; for
148 * permissive mode, we do the same here.
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
158 hint->csCoord = stemHint->max;
159 hint->flags = CF2_PairBottom;
163 hint->csCoord = stemHint->min;
164 hint->flags = CF2_PairTop;
174 hint->csCoord = stemHint->min;
175 hint->flags = CF2_PairBottom;
179 hint->csCoord = stemHint->max;
180 hint->flags = CF2_PairTop;
184 /* Now that ghost hints have been detected, adjust this edge for */
185 /* darkening. Bottoms are not changed; tops are incremented by twice */
187 if ( cf2_hint_isTop( hint ) )
188 hint->csCoord = ADD_INT32( hint->csCoord, 2 * font->darkenY );
190 hint->csCoord = ADD_INT32( hint->csCoord, hintOrigin );
192 hint->index = indexStemHint; /* index in original stem hint array */
194 /* if original stem hint has been used, use the same position */
195 if ( hint->flags != 0 && stemHint->used )
197 if ( cf2_hint_isTop( hint ) )
198 hint->dsCoord = stemHint->maxDS;
200 hint->dsCoord = stemHint->minDS;
202 cf2_hint_lock( hint );
205 hint->dsCoord = FT_MulFix( hint->csCoord, scale );
209 /* initialize an invalid hint map element */
211 cf2_hint_initZero( CF2_Hint hint )
217 FT_LOCAL_DEF( FT_Bool )
218 cf2_hint_isValid( const CF2_Hint hint )
220 return FT_BOOL( hint->flags );
225 cf2_hint_isPair( const CF2_Hint hint )
227 return FT_BOOL( hint->flags & ( CF2_PairBottom | CF2_PairTop ) );
232 cf2_hint_isPairTop( const CF2_Hint hint )
234 return FT_BOOL( hint->flags & CF2_PairTop );
238 FT_LOCAL_DEF( FT_Bool )
239 cf2_hint_isTop( const CF2_Hint hint )
241 return FT_BOOL( hint->flags & ( CF2_PairTop | CF2_GhostTop ) );
245 FT_LOCAL_DEF( FT_Bool )
246 cf2_hint_isBottom( const CF2_Hint hint )
248 return FT_BOOL( hint->flags & ( CF2_PairBottom | CF2_GhostBottom ) );
253 cf2_hint_isLocked( const CF2_Hint hint )
255 return FT_BOOL( hint->flags & CF2_Locked );
260 cf2_hint_isSynthetic( const CF2_Hint hint )
262 return FT_BOOL( hint->flags & CF2_Synthetic );
267 cf2_hint_lock( CF2_Hint hint )
269 hint->flags |= CF2_Locked;
274 cf2_hintmap_init( CF2_HintMap hintmap,
276 CF2_HintMap initialMap,
277 CF2_ArrStack hintMoves,
282 /* copy parameters from font instance */
283 hintmap->hinted = font->hinted;
284 hintmap->scale = scale;
285 hintmap->font = font;
286 hintmap->initialHintMap = initialMap;
287 /* will clear in `cf2_hintmap_adjustHints' */
288 hintmap->hintMoves = hintMoves;
293 cf2_hintmap_isValid( const CF2_HintMap hintmap )
295 return hintmap->isValid;
300 cf2_hintmap_dump( CF2_HintMap hintmap )
302 #ifdef FT_DEBUG_LEVEL_TRACE
306 FT_TRACE6(( " index csCoord dsCoord scale flags\n" ));
308 for ( i = 0; i < hintmap->count; i++ )
310 CF2_Hint hint = &hintmap->edge[i];
313 FT_TRACE6(( " %3ld %7.2f %7.2f %5d %s%s%s%s\n",
315 hint->csCoord / 65536.0,
316 hint->dsCoord / ( hint->scale * 1.0 ),
318 ( cf2_hint_isPair( hint ) ? "p" : "g" ),
319 ( cf2_hint_isTop( hint ) ? "t" : "b" ),
320 ( cf2_hint_isLocked( hint ) ? "L" : ""),
321 ( cf2_hint_isSynthetic( hint ) ? "S" : "" ) ));
324 FT_UNUSED( hintmap );
329 /* transform character space coordinate to device space using hint map */
331 cf2_hintmap_map( CF2_HintMap hintmap,
334 if ( hintmap->count == 0 || !hintmap->hinted )
336 /* there are no hints; use uniform scale and zero offset */
337 return FT_MulFix( csCoord, hintmap->scale );
341 /* start linear search from last hit */
342 CF2_UInt i = hintmap->lastIndex;
345 FT_ASSERT( hintmap->lastIndex < CF2_MAX_HINT_EDGES );
348 while ( i < hintmap->count - 1 &&
349 csCoord >= hintmap->edge[i + 1].csCoord )
353 while ( i > 0 && csCoord < hintmap->edge[i].csCoord )
356 hintmap->lastIndex = i;
358 if ( i == 0 && csCoord < hintmap->edge[0].csCoord )
360 /* special case for points below first edge: use uniform scale */
361 return ADD_INT32( FT_MulFix( SUB_INT32( csCoord,
362 hintmap->edge[0].csCoord ),
364 hintmap->edge[0].dsCoord );
369 * Note: entries with duplicate csCoord are allowed.
370 * Use edge[i], the highest entry where csCoord >= entry[i].csCoord
372 return ADD_INT32( FT_MulFix( SUB_INT32( csCoord,
373 hintmap->edge[i].csCoord ),
374 hintmap->edge[i].scale ),
375 hintmap->edge[i].dsCoord );
382 * This hinting policy moves a hint pair in device space so that one of
383 * its two edges is on a device pixel boundary (its fractional part is
384 * zero). `cf2_hintmap_insertHint' guarantees no overlap in CS
385 * space. Ensure here that there is no overlap in DS.
387 * In the first pass, edges are adjusted relative to adjacent hints.
388 * Those that are below have already been adjusted. Those that are
389 * above have not yet been adjusted. If a hint above blocks an
390 * adjustment to an optimal position, we will try again in a second
391 * pass. The second pass is top-down.
396 cf2_hintmap_adjustHints( CF2_HintMap hintmap )
401 cf2_arrstack_clear( hintmap->hintMoves ); /* working storage */
404 * First pass is bottom-up (font hint order) without look-ahead.
405 * Locked edges are already adjusted.
406 * Unlocked edges begin with dsCoord from `initialHintMap'.
407 * Save edges that are not optimally adjusted in `hintMoves' array,
408 * and process them in second pass.
411 for ( i = 0; i < hintmap->count; i++ )
413 FT_Bool isPair = cf2_hint_isPair( &hintmap->edge[i] );
416 /* index of upper edge (same value for ghost hint) */
417 j = isPair ? i + 1 : i;
419 FT_ASSERT( j < hintmap->count );
420 FT_ASSERT( cf2_hint_isValid( &hintmap->edge[i] ) );
421 FT_ASSERT( cf2_hint_isValid( &hintmap->edge[j] ) );
422 FT_ASSERT( cf2_hint_isLocked( &hintmap->edge[i] ) ==
423 cf2_hint_isLocked( &hintmap->edge[j] ) );
425 if ( !cf2_hint_isLocked( &hintmap->edge[i] ) )
427 /* hint edge is not locked, we can adjust it */
428 CF2_Fixed fracDown = cf2_fixedFraction( hintmap->edge[i].dsCoord );
429 CF2_Fixed fracUp = cf2_fixedFraction( hintmap->edge[j].dsCoord );
431 /* calculate all four possibilities; moves down are negative */
432 CF2_Fixed downMoveDown = 0 - fracDown;
433 CF2_Fixed upMoveDown = 0 - fracUp;
434 CF2_Fixed downMoveUp = ( fracDown == 0 )
436 : cf2_intToFixed( 1 ) - fracDown;
437 CF2_Fixed upMoveUp = ( fracUp == 0 )
439 : cf2_intToFixed( 1 ) - fracUp;
441 /* smallest move up */
442 CF2_Fixed moveUp = FT_MIN( downMoveUp, upMoveUp );
443 /* smallest move down */
444 CF2_Fixed moveDown = FT_MAX( downMoveDown, upMoveDown );
446 /* final amount to move edge or edge pair */
449 CF2_Fixed downMinCounter = CF2_MIN_COUNTER;
450 CF2_Fixed upMinCounter = CF2_MIN_COUNTER;
451 FT_Bool saveEdge = FALSE;
454 /* minimum counter constraint doesn't apply when adjacent edges */
456 /* TODO: doesn't seem a big effect; for now, reduce the code */
459 cf2_hint_isSynthetic( &hintmap->edge[i - 1] ) )
462 if ( j >= hintmap->count - 1 ||
463 cf2_hint_isSynthetic( &hintmap->edge[j + 1] ) )
467 /* is there room to move up? */
468 /* there is if we are at top of array or the next edge is at or */
469 /* beyond proposed move up? */
470 if ( j >= hintmap->count - 1 ||
471 hintmap->edge[j + 1].dsCoord >=
472 ADD_INT32( hintmap->edge[j].dsCoord,
473 moveUp + upMinCounter ) )
475 /* there is room to move up; is there also room to move down? */
477 hintmap->edge[i - 1].dsCoord <=
478 ADD_INT32( hintmap->edge[i].dsCoord,
479 moveDown - downMinCounter ) )
481 /* move smaller absolute amount */
482 move = ( -moveDown < moveUp ) ? moveDown : moveUp; /* optimum */
489 /* is there room to move down? */
491 hintmap->edge[i - 1].dsCoord <=
492 ADD_INT32( hintmap->edge[i].dsCoord,
493 moveDown - downMinCounter ) )
496 /* true if non-optimum move */
497 saveEdge = FT_BOOL( moveUp < -moveDown );
501 /* no room to move either way without overlapping or reducing */
502 /* the counter too much */
508 /* Identify non-moves and moves down that aren't optimal, and save */
509 /* them for second pass. */
510 /* Do this only if there is an unlocked edge above (which could */
511 /* possibly move). */
513 j < hintmap->count - 1 &&
514 !cf2_hint_isLocked( &hintmap->edge[j + 1] ) )
516 CF2_HintMoveRec savedMove;
520 /* desired adjustment in second pass */
521 savedMove.moveUp = moveUp - move;
523 cf2_arrstack_push( hintmap->hintMoves, &savedMove );
526 /* move the edge(s) */
527 hintmap->edge[i].dsCoord = ADD_INT32( hintmap->edge[i].dsCoord,
530 hintmap->edge[j].dsCoord = ADD_INT32( hintmap->edge[j].dsCoord,
534 /* assert there are no overlaps in device space */
536 hintmap->edge[i - 1].dsCoord <= hintmap->edge[i].dsCoord );
538 hintmap->edge[i].dsCoord <= hintmap->edge[j].dsCoord );
540 /* adjust the scales, avoiding divide by zero */
543 if ( hintmap->edge[i].csCoord != hintmap->edge[i - 1].csCoord )
544 hintmap->edge[i - 1].scale =
545 FT_DivFix( SUB_INT32( hintmap->edge[i].dsCoord,
546 hintmap->edge[i - 1].dsCoord ),
547 SUB_INT32( hintmap->edge[i].csCoord,
548 hintmap->edge[i - 1].csCoord ) );
553 if ( hintmap->edge[j].csCoord != hintmap->edge[j - 1].csCoord )
554 hintmap->edge[j - 1].scale =
555 FT_DivFix( SUB_INT32( hintmap->edge[j].dsCoord,
556 hintmap->edge[j - 1].dsCoord ),
557 SUB_INT32( hintmap->edge[j].csCoord,
558 hintmap->edge[j - 1].csCoord ) );
560 i += 1; /* skip upper edge on next loop */
564 /* second pass tries to move non-optimal hints up, in case there is */
566 for ( i = cf2_arrstack_size( hintmap->hintMoves ); i > 0; i-- )
568 CF2_HintMove hintMove = (CF2_HintMove)
569 cf2_arrstack_getPointer( hintmap->hintMoves, i - 1 );
574 /* this was tested before the push, above */
575 FT_ASSERT( j < hintmap->count - 1 );
577 /* is there room to move up? */
578 if ( hintmap->edge[j + 1].dsCoord >=
579 ADD_INT32( hintmap->edge[j].dsCoord,
580 hintMove->moveUp + CF2_MIN_COUNTER ) )
582 /* there is more room now, move edge up */
583 hintmap->edge[j].dsCoord = ADD_INT32( hintmap->edge[j].dsCoord,
586 if ( cf2_hint_isPair( &hintmap->edge[j] ) )
589 hintmap->edge[j - 1].dsCoord =
590 ADD_INT32( hintmap->edge[j - 1].dsCoord, hintMove->moveUp );
597 /* insert hint edges into map, sorted by csCoord */
599 cf2_hintmap_insertHint( CF2_HintMap hintmap,
600 CF2_Hint bottomHintEdge,
601 CF2_Hint topHintEdge )
603 CF2_UInt indexInsert;
605 /* set default values, then check for edge hints */
606 FT_Bool isPair = TRUE;
607 CF2_Hint firstHintEdge = bottomHintEdge;
608 CF2_Hint secondHintEdge = topHintEdge;
611 /* one or none of the input params may be invalid when dealing with */
612 /* edge hints; at least one edge must be valid */
613 FT_ASSERT( cf2_hint_isValid( bottomHintEdge ) ||
614 cf2_hint_isValid( topHintEdge ) );
616 /* determine how many and which edges to insert */
617 if ( !cf2_hint_isValid( bottomHintEdge ) )
619 /* insert only the top edge */
620 firstHintEdge = topHintEdge;
623 else if ( !cf2_hint_isValid( topHintEdge ) )
625 /* insert only the bottom edge */
629 /* paired edges must be in proper order */
631 topHintEdge->csCoord < bottomHintEdge->csCoord )
634 /* linear search to find index value of insertion point */
636 for ( ; indexInsert < hintmap->count; indexInsert++ )
638 if ( hintmap->edge[indexInsert].csCoord >= firstHintEdge->csCoord )
642 FT_TRACE7(( " Got hint at %.2f (%.2f)\n",
643 firstHintEdge->csCoord / 65536.0,
644 firstHintEdge->dsCoord / 65536.0 ));
646 FT_TRACE7(( " Got hint at %.2f (%.2f)\n",
647 secondHintEdge->csCoord / 65536.0,
648 secondHintEdge->dsCoord / 65536.0 ));
651 * Discard any hints that overlap in character space. Most often, this
652 * is while building the initial map, where captured hints from all
653 * zones are combined. Define overlap to include hints that `touch'
654 * (overlap zero). Hiragino Sans/Gothic fonts have numerous hints that
655 * touch. Some fonts have non-ideographic glyphs that overlap our
658 * Overlap also occurs when darkening stem hints that are close.
661 if ( indexInsert < hintmap->count )
663 /* we are inserting before an existing edge: */
664 /* verify that an existing edge is not the same */
665 if ( hintmap->edge[indexInsert].csCoord == firstHintEdge->csCoord )
666 return; /* ignore overlapping stem hint */
668 /* verify that a new pair does not straddle the next edge */
670 hintmap->edge[indexInsert].csCoord <= secondHintEdge->csCoord )
671 return; /* ignore overlapping stem hint */
673 /* verify that we are not inserting between paired edges */
674 if ( cf2_hint_isPairTop( &hintmap->edge[indexInsert] ) )
675 return; /* ignore overlapping stem hint */
678 /* recompute device space locations using initial hint map */
679 if ( cf2_hintmap_isValid( hintmap->initialHintMap ) &&
680 !cf2_hint_isLocked( firstHintEdge ) )
684 /* Use hint map to position the center of stem, and nominal scale */
685 /* to position the two edges. This preserves the stem width. */
688 hintmap->initialHintMap,
689 ADD_INT32( secondHintEdge->csCoord,
690 firstHintEdge->csCoord ) / 2 );
691 CF2_Fixed halfWidth =
692 FT_MulFix( SUB_INT32( secondHintEdge->csCoord,
693 firstHintEdge->csCoord ) / 2,
697 firstHintEdge->dsCoord = SUB_INT32( midpoint, halfWidth );
698 secondHintEdge->dsCoord = ADD_INT32( midpoint, halfWidth );
701 firstHintEdge->dsCoord = cf2_hintmap_map( hintmap->initialHintMap,
702 firstHintEdge->csCoord );
706 * Discard any hints that overlap in device space; this can occur
707 * because locked hints have been moved to align with blue zones.
709 * TODO: Although we might correct this later during adjustment, we
710 * don't currently have a way to delete a conflicting hint once it has
711 * been inserted. See v2.030 MinionPro-Regular, 12 ppem darkened,
712 * initial hint map for second path, glyph 945 (the perispomeni (tilde)
713 * in U+1F6E, Greek omega with psili and perispomeni). Darkening is
714 * 25. Pair 667,747 initially conflicts in design space with top edge
715 * 660. This is because 667 maps to 7.87, and the top edge was
716 * captured by a zone at 8.0. The pair is later successfully inserted
717 * in a zone without the top edge. In this zone it is adjusted to 8.0,
718 * and no longer conflicts with the top edge in design space. This
719 * means it can be included in yet a later zone which does have the top
720 * edge hint. This produces a small mismatch between the first and
721 * last points of this path, even though the hint masks are the same.
722 * The density map difference is tiny (1/256).
726 if ( indexInsert > 0 )
728 /* we are inserting after an existing edge */
729 if ( firstHintEdge->dsCoord < hintmap->edge[indexInsert - 1].dsCoord )
733 if ( indexInsert < hintmap->count )
735 /* we are inserting before an existing edge */
738 if ( secondHintEdge->dsCoord > hintmap->edge[indexInsert].dsCoord )
743 if ( firstHintEdge->dsCoord > hintmap->edge[indexInsert].dsCoord )
748 /* make room to insert */
750 CF2_UInt iSrc = hintmap->count - 1;
751 CF2_UInt iDst = isPair ? hintmap->count + 1 : hintmap->count;
753 CF2_UInt count = hintmap->count - indexInsert;
756 if ( iDst >= CF2_MAX_HINT_EDGES )
758 FT_TRACE4(( "cf2_hintmap_insertHint: too many hintmaps\n" ));
763 hintmap->edge[iDst--] = hintmap->edge[iSrc--];
765 /* insert first edge */
766 hintmap->edge[indexInsert] = *firstHintEdge; /* copy struct */
769 FT_TRACE7(( " Inserting hint %.2f (%.2f)\n",
770 firstHintEdge->csCoord / 65536.0,
771 firstHintEdge->dsCoord / 65536.0 ));
775 /* insert second edge */
776 hintmap->edge[indexInsert + 1] = *secondHintEdge; /* copy struct */
779 FT_TRACE7(( " Inserting hint %.2f (%.2f)\n",
780 secondHintEdge->csCoord / 65536.0,
781 secondHintEdge->dsCoord / 65536.0 ));
791 * Build a map from hints and mask.
793 * This function may recur one level if `hintmap->initialHintMap' is not yet
795 * If `initialMap' is true, simply build initial map.
797 * Synthetic hints are used in two ways. A hint at zero is inserted, if
798 * needed, in the initial hint map, to prevent translations from
799 * propagating across the origin. If synthetic em box hints are enabled
800 * for ideographic dictionaries, then they are inserted in all hint
801 * maps, including the initial one.
805 cf2_hintmap_build( CF2_HintMap hintmap,
806 CF2_ArrStack hStemHintArray,
807 CF2_ArrStack vStemHintArray,
808 CF2_HintMask hintMask,
809 CF2_Fixed hintOrigin,
814 CF2_Font font = hintmap->font;
815 CF2_HintMaskRec tempHintMask;
821 /* check whether initial map is constructed */
822 if ( !initialMap && !cf2_hintmap_isValid( hintmap->initialHintMap ) )
824 /* make recursive call with initialHintMap and temporary mask; */
825 /* temporary mask will get all bits set, below */
826 cf2_hintmask_init( &tempHintMask, hintMask->error );
827 cf2_hintmap_build( hintmap->initialHintMap,
835 if ( !cf2_hintmask_isValid( hintMask ) )
837 /* without a hint mask, assume all hints are active */
838 cf2_hintmask_setAll( hintMask,
839 cf2_arrstack_size( hStemHintArray ) +
840 cf2_arrstack_size( vStemHintArray ) );
841 if ( !cf2_hintmask_isValid( hintMask ) )
845 /* no error, just continue unhinted */
846 *hintMask->error = FT_Err_Ok;
847 hintmap->hinted = FALSE;
849 return; /* too many stem hints */
853 /* begin by clearing the map */
855 hintmap->lastIndex = 0;
857 /* make a copy of the hint mask so we can modify it */
858 tempHintMask = *hintMask;
859 maskPtr = cf2_hintmask_getMaskPtr( &tempHintMask );
861 /* use the hStem hints only, which are first in the mask */
862 bitCount = cf2_arrstack_size( hStemHintArray );
864 /* Defense-in-depth. Should never return here. */
865 if ( bitCount > hintMask->bitCount )
868 /* synthetic embox hints get highest priority */
869 if ( font->blues.doEmBoxHints )
874 cf2_hint_initZero( &dummy ); /* invalid hint map element */
877 cf2_hintmap_insertHint( hintmap,
878 &font->blues.emBoxBottomEdge,
881 cf2_hintmap_insertHint( hintmap,
883 &font->blues.emBoxTopEdge );
886 /* insert hints captured by a blue zone or already locked (higher */
888 for ( i = 0, maskByte = 0x80; i < bitCount; i++ )
890 if ( maskByte & *maskPtr )
892 /* expand StemHint into two `CF2_Hint' elements */
893 CF2_HintRec bottomHintEdge, topHintEdge;
896 cf2_hint_init( &bottomHintEdge,
903 cf2_hint_init( &topHintEdge,
911 if ( cf2_hint_isLocked( &bottomHintEdge ) ||
912 cf2_hint_isLocked( &topHintEdge ) ||
913 cf2_blues_capture( &font->blues,
917 /* insert captured hint into map */
918 cf2_hintmap_insertHint( hintmap, &bottomHintEdge, &topHintEdge );
920 *maskPtr &= ~maskByte; /* turn off the bit for this hint */
924 if ( ( i & 7 ) == 7 )
926 /* move to next mask byte */
934 /* initial hint map includes only captured hints plus maybe one at 0 */
937 * TODO: There is a problem here because we are trying to build a
938 * single hint map containing all captured hints. It is
939 * possible for there to be conflicts between captured hints,
940 * either because of darkening or because the hints are in
941 * separate hint zones (we are ignoring hint zones for the
942 * initial map). An example of the latter is MinionPro-Regular
943 * v2.030 glyph 883 (Greek Capital Alpha with Psili) at 15ppem.
944 * A stem hint for the psili conflicts with the top edge hint
945 * for the base character. The stem hint gets priority because
946 * of its sort order. In glyph 884 (Greek Capital Alpha with
947 * Psili and Oxia), the top of the base character gets a stem
948 * hint, and the psili does not. This creates different initial
949 * maps for the two glyphs resulting in different renderings of
950 * the base character. Will probably defer this either as not
951 * worth the cost or as a font bug. I don't think there is any
952 * good reason for an accent to be captured by an alignment
953 * zone. -darnold 2/12/10
958 /* Apply a heuristic that inserts a point for (0,0), unless it's */
959 /* already covered by a mapping. This locks the baseline for glyphs */
960 /* that have no baseline hints. */
962 if ( hintmap->count == 0 ||
963 hintmap->edge[0].csCoord > 0 ||
964 hintmap->edge[hintmap->count - 1].csCoord < 0 )
966 /* all edges are above 0 or all edges are below 0; */
967 /* construct a locked edge hint at 0 */
969 CF2_HintRec edge, invalid;
972 cf2_hint_initZero( &edge );
974 edge.flags = CF2_GhostBottom |
977 edge.scale = hintmap->scale;
979 cf2_hint_initZero( &invalid );
980 cf2_hintmap_insertHint( hintmap, &edge, &invalid );
985 /* insert remaining hints */
987 maskPtr = cf2_hintmask_getMaskPtr( &tempHintMask );
989 for ( i = 0, maskByte = 0x80; i < bitCount; i++ )
991 if ( maskByte & *maskPtr )
993 CF2_HintRec bottomHintEdge, topHintEdge;
996 cf2_hint_init( &bottomHintEdge,
1002 TRUE /* bottom */ );
1003 cf2_hint_init( &topHintEdge,
1011 cf2_hintmap_insertHint( hintmap, &bottomHintEdge, &topHintEdge );
1014 if ( ( i & 7 ) == 7 )
1016 /* move to next mask byte */
1025 FT_TRACE6(( "%s\n", initialMap ? "flags: [p]air [g]host [t]op"
1026 " [b]ottom [L]ocked [S]ynthetic\n"
1029 cf2_hintmap_dump( hintmap );
1032 * Note: The following line is a convenient place to break when
1033 * debugging hinting. Examine `hintmap->edge' for the list of
1034 * enabled hints, then step over the call to see the effect of
1035 * adjustment. We stop here first on the recursive call that
1036 * creates the initial map, and then on each counter group and
1040 /* adjust positions of hint edges that are not locked to blue zones */
1041 cf2_hintmap_adjustHints( hintmap );
1043 FT_TRACE6(( "(adjusted)\n" ));
1044 cf2_hintmap_dump( hintmap );
1046 /* save the position of all hints that were used in this hint map; */
1047 /* if we use them again, we'll locate them in the same position */
1050 for ( i = 0; i < hintmap->count; i++ )
1052 if ( !cf2_hint_isSynthetic( &hintmap->edge[i] ) )
1054 /* Note: include both valid and invalid edges */
1055 /* Note: top and bottom edges are copied back separately */
1056 CF2_StemHint stemhint = (CF2_StemHint)
1057 cf2_arrstack_getPointer( hStemHintArray,
1058 hintmap->edge[i].index );
1061 if ( cf2_hint_isTop( &hintmap->edge[i] ) )
1062 stemhint->maxDS = hintmap->edge[i].dsCoord;
1064 stemhint->minDS = hintmap->edge[i].dsCoord;
1066 stemhint->used = TRUE;
1071 /* hint map is ready to use */
1072 hintmap->isValid = TRUE;
1074 /* remember this mask has been used */
1075 cf2_hintmask_setNew( hintMask, FALSE );
1079 FT_LOCAL_DEF( void )
1080 cf2_glyphpath_init( CF2_GlyphPath glyphpath,
1082 CF2_OutlineCallbacks callbacks,
1084 /* CF2_Fixed hShift, */
1085 CF2_ArrStack hStemHintArray,
1086 CF2_ArrStack vStemHintArray,
1087 CF2_HintMask hintMask,
1088 CF2_Fixed hintOriginY,
1089 const CF2_Blues blues,
1090 const FT_Vector* fractionalTranslation )
1092 FT_ZERO( glyphpath );
1094 glyphpath->font = font;
1095 glyphpath->callbacks = callbacks;
1097 cf2_arrstack_init( &glyphpath->hintMoves,
1100 sizeof ( CF2_HintMoveRec ) );
1102 cf2_hintmap_init( &glyphpath->initialHintMap,
1104 &glyphpath->initialHintMap,
1105 &glyphpath->hintMoves,
1107 cf2_hintmap_init( &glyphpath->firstHintMap,
1109 &glyphpath->initialHintMap,
1110 &glyphpath->hintMoves,
1112 cf2_hintmap_init( &glyphpath->hintMap,
1114 &glyphpath->initialHintMap,
1115 &glyphpath->hintMoves,
1118 glyphpath->scaleX = font->innerTransform.a;
1119 glyphpath->scaleC = font->innerTransform.c;
1120 glyphpath->scaleY = font->innerTransform.d;
1122 glyphpath->fractionalTranslation = *fractionalTranslation;
1125 glyphpath->hShift = hShift; /* for fauxing */
1128 glyphpath->hStemHintArray = hStemHintArray;
1129 glyphpath->vStemHintArray = vStemHintArray;
1130 glyphpath->hintMask = hintMask; /* ptr to current mask */
1131 glyphpath->hintOriginY = hintOriginY;
1132 glyphpath->blues = blues;
1133 glyphpath->darken = font->darkened; /* TODO: should we make copies? */
1134 glyphpath->xOffset = font->darkenX;
1135 glyphpath->yOffset = font->darkenY;
1136 glyphpath->miterLimit = 2 * FT_MAX(
1137 cf2_fixedAbs( glyphpath->xOffset ),
1138 cf2_fixedAbs( glyphpath->yOffset ) );
1140 /* .1 character space unit */
1141 glyphpath->snapThreshold = cf2_doubleToFixed( 0.1 );
1143 glyphpath->moveIsPending = TRUE;
1144 glyphpath->pathIsOpen = FALSE;
1145 glyphpath->pathIsClosing = FALSE;
1146 glyphpath->elemIsQueued = FALSE;
1150 FT_LOCAL_DEF( void )
1151 cf2_glyphpath_finalize( CF2_GlyphPath glyphpath )
1153 cf2_arrstack_finalize( &glyphpath->hintMoves );
1158 * Hint point in y-direction and apply outerTransform.
1159 * Input `current' hint map (which is actually delayed by one element).
1160 * Input x,y point in Character Space.
1161 * Output x,y point in Device Space, including translation.
1164 cf2_glyphpath_hintPoint( CF2_GlyphPath glyphpath,
1165 CF2_HintMap hintmap,
1170 FT_Vector pt; /* hinted point in upright DS */
1173 pt.x = ADD_INT32( FT_MulFix( glyphpath->scaleX, x ),
1174 FT_MulFix( glyphpath->scaleC, y ) );
1175 pt.y = cf2_hintmap_map( hintmap, y );
1178 FT_MulFix( glyphpath->font->outerTransform.a, pt.x ),
1180 FT_MulFix( glyphpath->font->outerTransform.c, pt.y ),
1181 glyphpath->fractionalTranslation.x ) );
1183 FT_MulFix( glyphpath->font->outerTransform.b, pt.x ),
1185 FT_MulFix( glyphpath->font->outerTransform.d, pt.y ),
1186 glyphpath->fractionalTranslation.y ) );
1191 * From two line segments, (u1,u2) and (v1,v2), compute a point of
1192 * intersection on the corresponding lines.
1193 * Return false if no intersection is found, or if the intersection is
1194 * too far away from the ends of the line segments, u2 and v1.
1198 cf2_glyphpath_computeIntersection( CF2_GlyphPath glyphpath,
1199 const FT_Vector* u1,
1200 const FT_Vector* u2,
1201 const FT_Vector* v1,
1202 const FT_Vector* v2,
1203 FT_Vector* intersection )
1206 * Let `u' be a zero-based vector from the first segment, `v' from the
1208 * Let `w 'be the zero-based vector from `u1' to `v1'.
1209 * `perp' is the `perpendicular dot product'; see
1210 * https://mathworld.wolfram.com/PerpDotProduct.html.
1211 * `s' is the parameter for the parametric line for the first segment
1215 * http://geomalgorithms.com/a05-_intersect-1.html.
1216 * Calculations are done in 16.16, but must handle the squaring of
1217 * line lengths in character space. We scale all vectors by 1/32 to
1218 * avoid overflow. This allows values up to 4095 to be squared. The
1219 * scale factor cancels in the divide.
1221 * TODO: the scale factor could be computed from UnitsPerEm.
1225 #define cf2_perp( a, b ) \
1226 ( FT_MulFix( a.x, b.y ) - FT_MulFix( a.y, b.x ) )
1228 /* round and divide by 32 */
1229 #define CF2_CS_SCALE( x ) \
1230 ( ( (x) + 0x10 ) >> 5 )
1232 FT_Vector u, v, w; /* scaled vectors */
1233 CF2_Fixed denominator, s;
1236 u.x = CF2_CS_SCALE( SUB_INT32( u2->x, u1->x ) );
1237 u.y = CF2_CS_SCALE( SUB_INT32( u2->y, u1->y ) );
1238 v.x = CF2_CS_SCALE( SUB_INT32( v2->x, v1->x ) );
1239 v.y = CF2_CS_SCALE( SUB_INT32( v2->y, v1->y ) );
1240 w.x = CF2_CS_SCALE( SUB_INT32( v1->x, u1->x ) );
1241 w.y = CF2_CS_SCALE( SUB_INT32( v1->y, u1->y ) );
1243 denominator = cf2_perp( u, v );
1245 if ( denominator == 0 )
1246 return FALSE; /* parallel or coincident lines */
1248 s = FT_DivFix( cf2_perp( w, v ), denominator );
1250 intersection->x = ADD_INT32( u1->x,
1251 FT_MulFix( s, SUB_INT32( u2->x, u1->x ) ) );
1252 intersection->y = ADD_INT32( u1->y,
1253 FT_MulFix( s, SUB_INT32( u2->y, u1->y ) ) );
1257 * Special case snapping for horizontal and vertical lines.
1258 * This cleans up intersections and reduces problems with winding
1260 * Sample case is sbc cd KozGoPr6N-Medium.otf 20 16685.
1261 * Note: these calculations are in character space.
1265 if ( u1->x == u2->x &&
1266 cf2_fixedAbs( SUB_INT32( intersection->x,
1267 u1->x ) ) < glyphpath->snapThreshold )
1268 intersection->x = u1->x;
1269 if ( u1->y == u2->y &&
1270 cf2_fixedAbs( SUB_INT32( intersection->y,
1271 u1->y ) ) < glyphpath->snapThreshold )
1272 intersection->y = u1->y;
1274 if ( v1->x == v2->x &&
1275 cf2_fixedAbs( SUB_INT32( intersection->x,
1276 v1->x ) ) < glyphpath->snapThreshold )
1277 intersection->x = v1->x;
1278 if ( v1->y == v2->y &&
1279 cf2_fixedAbs( SUB_INT32( intersection->y,
1280 v1->y ) ) < glyphpath->snapThreshold )
1281 intersection->y = v1->y;
1283 /* limit the intersection distance from midpoint of u2 and v1 */
1284 if ( cf2_fixedAbs( intersection->x - ADD_INT32( u2->x, v1->x ) / 2 ) >
1285 glyphpath->miterLimit ||
1286 cf2_fixedAbs( intersection->y - ADD_INT32( u2->y, v1->y ) / 2 ) >
1287 glyphpath->miterLimit )
1295 * Push the cached element (glyphpath->prevElem*) to the outline
1296 * consumer. When a darkening offset is used, the end point of the
1297 * cached element may be adjusted to an intersection point or we may
1298 * synthesize a connecting line to the current element. If we are
1299 * closing a subpath, we may also generate a connecting line to the start
1302 * This is where Character Space (CS) is converted to Device Space (DS)
1303 * using a hint map. This calculation must use a HintMap that was valid
1304 * at the time the element was saved. For the first point in a subpath,
1305 * that is a saved HintMap. For most elements, it just means the caller
1306 * has delayed building a HintMap from the current HintMask.
1308 * Transform each point with outerTransform and call the outline
1309 * callbacks. This is a general 3x3 transform:
1311 * x' = a*x + c*y + tx, y' = b*x + d*y + ty
1313 * but it uses 4 elements from CF2_Font and the translation part
1314 * from CF2_GlyphPath.
1318 cf2_glyphpath_pushPrevElem( CF2_GlyphPath glyphpath,
1319 CF2_HintMap hintmap,
1324 CF2_CallbackParamsRec params;
1329 FT_Vector intersection = { 0, 0 };
1330 FT_Bool useIntersection = FALSE;
1333 FT_ASSERT( glyphpath->prevElemOp == CF2_PathOpLineTo ||
1334 glyphpath->prevElemOp == CF2_PathOpCubeTo );
1336 if ( glyphpath->prevElemOp == CF2_PathOpLineTo )
1338 prevP0 = &glyphpath->prevElemP0;
1339 prevP1 = &glyphpath->prevElemP1;
1343 prevP0 = &glyphpath->prevElemP2;
1344 prevP1 = &glyphpath->prevElemP3;
1347 /* optimization: if previous and next elements are offset by the same */
1348 /* amount, then there will be no gap, and no need to compute an */
1350 if ( prevP1->x != nextP0->x || prevP1->y != nextP0->y )
1352 /* previous element does not join next element: */
1353 /* adjust end point of previous element to the intersection */
1354 useIntersection = cf2_glyphpath_computeIntersection( glyphpath,
1360 if ( useIntersection )
1362 /* modify the last point of the cached element (either line or */
1364 *prevP1 = intersection;
1368 params.pt0 = glyphpath->currentDS;
1370 switch( glyphpath->prevElemOp )
1372 case CF2_PathOpLineTo:
1373 params.op = CF2_PathOpLineTo;
1375 /* note: pt2 and pt3 are unused */
1379 /* use first hint map if closing */
1380 cf2_glyphpath_hintPoint( glyphpath,
1381 &glyphpath->firstHintMap,
1383 glyphpath->prevElemP1.x,
1384 glyphpath->prevElemP1.y );
1388 cf2_glyphpath_hintPoint( glyphpath,
1391 glyphpath->prevElemP1.x,
1392 glyphpath->prevElemP1.y );
1395 /* output only non-zero length lines */
1396 if ( params.pt0.x != params.pt1.x || params.pt0.y != params.pt1.y )
1398 glyphpath->callbacks->lineTo( glyphpath->callbacks, ¶ms );
1400 glyphpath->currentDS = params.pt1;
1404 case CF2_PathOpCubeTo:
1405 params.op = CF2_PathOpCubeTo;
1407 /* TODO: should we intersect the interior joins (p1-p2 and p2-p3)? */
1408 cf2_glyphpath_hintPoint( glyphpath,
1411 glyphpath->prevElemP1.x,
1412 glyphpath->prevElemP1.y );
1413 cf2_glyphpath_hintPoint( glyphpath,
1416 glyphpath->prevElemP2.x,
1417 glyphpath->prevElemP2.y );
1418 cf2_glyphpath_hintPoint( glyphpath,
1421 glyphpath->prevElemP3.x,
1422 glyphpath->prevElemP3.y );
1424 glyphpath->callbacks->cubeTo( glyphpath->callbacks, ¶ms );
1426 glyphpath->currentDS = params.pt3;
1431 if ( !useIntersection || close )
1433 /* insert connecting line between end of previous element and start */
1434 /* of current one */
1435 /* note: at the end of a subpath, we might do both, so use `nextP0' */
1436 /* before we change it, below */
1440 /* if we are closing the subpath, then nextP0 is in the first */
1442 cf2_glyphpath_hintPoint( glyphpath,
1443 &glyphpath->firstHintMap,
1450 cf2_glyphpath_hintPoint( glyphpath,
1457 if ( params.pt1.x != glyphpath->currentDS.x ||
1458 params.pt1.y != glyphpath->currentDS.y )
1460 /* length is nonzero */
1461 params.op = CF2_PathOpLineTo;
1462 params.pt0 = glyphpath->currentDS;
1464 /* note: pt2 and pt3 are unused */
1465 glyphpath->callbacks->lineTo( glyphpath->callbacks, ¶ms );
1467 glyphpath->currentDS = params.pt1;
1471 if ( useIntersection )
1473 /* return intersection point to caller */
1474 *nextP0 = intersection;
1479 /* push a MoveTo element based on current point and offset of current */
1482 cf2_glyphpath_pushMove( CF2_GlyphPath glyphpath,
1485 CF2_CallbackParamsRec params;
1488 params.op = CF2_PathOpMoveTo;
1489 params.pt0 = glyphpath->currentDS;
1491 /* Test if move has really happened yet; it would have called */
1492 /* `cf2_hintmap_build' to set `isValid'. */
1493 if ( !cf2_hintmap_isValid( &glyphpath->hintMap ) )
1495 /* we are here iff first subpath is missing a moveto operator: */
1496 /* synthesize first moveTo to finish initialization of hintMap */
1497 cf2_glyphpath_moveTo( glyphpath,
1499 glyphpath->start.y );
1502 cf2_glyphpath_hintPoint( glyphpath,
1503 &glyphpath->hintMap,
1508 /* note: pt2 and pt3 are unused */
1509 glyphpath->callbacks->moveTo( glyphpath->callbacks, ¶ms );
1511 glyphpath->currentDS = params.pt1;
1512 glyphpath->offsetStart0 = start;
1517 * All coordinates are in character space.
1518 * On input, (x1, y1) and (x2, y2) give line segment.
1519 * On output, (x, y) give offset vector.
1520 * We use a piecewise approximation to trig functions.
1522 * TODO: Offset true perpendicular and proper length
1523 * supply the y-translation for hinting here, too,
1524 * that adds yOffset unconditionally to *y.
1527 cf2_glyphpath_computeOffset( CF2_GlyphPath glyphpath,
1535 CF2_Fixed dx = SUB_INT32( x2, x1 );
1536 CF2_Fixed dy = SUB_INT32( y2, y1 );
1539 /* note: negative offsets don't work here; negate deltas to change */
1540 /* quadrants, below */
1541 if ( glyphpath->font->reverseWinding )
1543 dx = NEG_INT32( dx );
1544 dy = NEG_INT32( dy );
1549 if ( !glyphpath->darken )
1552 /* add momentum for this path element */
1553 glyphpath->callbacks->windingMomentum =
1554 ADD_INT32( glyphpath->callbacks->windingMomentum,
1555 cf2_getWindingMomentum( x1, y1, x2, y2 ) );
1557 /* note: allow mixed integer and fixed multiplication here */
1562 /* first quadrant, +x +y */
1564 if ( dx > MUL_INT32( 2, dy ) )
1570 else if ( dy > MUL_INT32( 2, dx ) )
1573 *x = glyphpath->xOffset;
1574 *y = glyphpath->yOffset;
1579 *x = FT_MulFix( cf2_doubleToFixed( 0.7 ),
1580 glyphpath->xOffset );
1581 *y = FT_MulFix( cf2_doubleToFixed( 1.0 - 0.7 ),
1582 glyphpath->yOffset );
1587 /* fourth quadrant, +x -y */
1589 if ( dx > MUL_INT32( -2, dy ) )
1595 else if ( NEG_INT32( dy ) > MUL_INT32( 2, dx ) )
1598 *x = NEG_INT32( glyphpath->xOffset );
1599 *y = glyphpath->yOffset;
1604 *x = FT_MulFix( cf2_doubleToFixed( -0.7 ),
1605 glyphpath->xOffset );
1606 *y = FT_MulFix( cf2_doubleToFixed( 1.0 - 0.7 ),
1607 glyphpath->yOffset );
1615 /* second quadrant, -x +y */
1617 if ( NEG_INT32( dx ) > MUL_INT32( 2, dy ) )
1621 *y = MUL_INT32( 2, glyphpath->yOffset );
1623 else if ( dy > MUL_INT32( -2, dx ) )
1626 *x = glyphpath->xOffset;
1627 *y = glyphpath->yOffset;
1632 *x = FT_MulFix( cf2_doubleToFixed( 0.7 ),
1633 glyphpath->xOffset );
1634 *y = FT_MulFix( cf2_doubleToFixed( 1.0 + 0.7 ),
1635 glyphpath->yOffset );
1640 /* third quadrant, -x -y */
1642 if ( NEG_INT32( dx ) > MUL_INT32( -2, dy ) )
1646 *y = MUL_INT32( 2, glyphpath->yOffset );
1648 else if ( NEG_INT32( dy ) > MUL_INT32( -2, dx ) )
1651 *x = NEG_INT32( glyphpath->xOffset );
1652 *y = glyphpath->yOffset;
1657 *x = FT_MulFix( cf2_doubleToFixed( -0.7 ),
1658 glyphpath->xOffset );
1659 *y = FT_MulFix( cf2_doubleToFixed( 1.0 + 0.7 ),
1660 glyphpath->yOffset );
1668 * The functions cf2_glyphpath_{moveTo,lineTo,curveTo,closeOpenPath} are
1669 * called by the interpreter with Character Space (CS) coordinates. Each
1670 * path element is placed into a queue of length one to await the
1671 * calculation of the following element. At that time, the darkening
1672 * offset of the following element is known and joins can be computed,
1673 * including possible modification of this element, before mapping to
1674 * Device Space (DS) and passing it on to the outline consumer.
1677 FT_LOCAL_DEF( void )
1678 cf2_glyphpath_moveTo( CF2_GlyphPath glyphpath,
1682 cf2_glyphpath_closeOpenPath( glyphpath );
1684 /* save the parameters of the move for later, when we'll know how to */
1686 /* also save last move point */
1687 glyphpath->currentCS.x = glyphpath->start.x = x;
1688 glyphpath->currentCS.y = glyphpath->start.y = y;
1690 glyphpath->moveIsPending = TRUE;
1692 /* ensure we have a valid map with current mask */
1693 if ( !cf2_hintmap_isValid( &glyphpath->hintMap ) ||
1694 cf2_hintmask_isNew( glyphpath->hintMask ) )
1695 cf2_hintmap_build( &glyphpath->hintMap,
1696 glyphpath->hStemHintArray,
1697 glyphpath->vStemHintArray,
1698 glyphpath->hintMask,
1699 glyphpath->hintOriginY,
1702 /* save a copy of current HintMap to use when drawing initial point */
1703 glyphpath->firstHintMap = glyphpath->hintMap; /* structure copy */
1707 FT_LOCAL_DEF( void )
1708 cf2_glyphpath_lineTo( CF2_GlyphPath glyphpath,
1712 CF2_Fixed xOffset, yOffset;
1717 * New hints will be applied after cf2_glyphpath_pushPrevElem has run.
1718 * In case this is a synthesized closing line, any new hints should be
1719 * delayed until this path is closed (`cf2_hintmask_isNew' will be
1720 * called again before the next line or curve).
1723 /* true if new hint map not on close */
1724 newHintMap = cf2_hintmask_isNew( glyphpath->hintMask ) &&
1725 !glyphpath->pathIsClosing;
1728 * Zero-length lines may occur in the charstring. Because we cannot
1729 * compute darkening offsets or intersections from zero-length lines,
1730 * it is best to remove them and avoid artifacts. However, zero-length
1731 * lines in CS at the start of a new hint map can generate non-zero
1732 * lines in DS due to hint substitution. We detect a change in hint
1733 * map here and pass those zero-length lines along.
1737 * Note: Find explicitly closed paths here with a conditional
1740 * !gp->pathIsClosing && gp->start.x == x && gp->start.y == y
1744 if ( glyphpath->currentCS.x == x &&
1745 glyphpath->currentCS.y == y &&
1748 * Ignore zero-length lines in CS where the hint map is the same
1749 * because the line in DS will also be zero length.
1751 * Ignore zero-length lines when we synthesize a closing line because
1752 * the close will be handled in cf2_glyphPath_pushPrevElem.
1756 cf2_glyphpath_computeOffset( glyphpath,
1757 glyphpath->currentCS.x,
1758 glyphpath->currentCS.y,
1764 /* construct offset points */
1765 P0.x = ADD_INT32( glyphpath->currentCS.x, xOffset );
1766 P0.y = ADD_INT32( glyphpath->currentCS.y, yOffset );
1767 P1.x = ADD_INT32( x, xOffset );
1768 P1.y = ADD_INT32( y, yOffset );
1770 if ( glyphpath->moveIsPending )
1772 /* emit offset 1st point as MoveTo */
1773 cf2_glyphpath_pushMove( glyphpath, P0 );
1775 glyphpath->moveIsPending = FALSE; /* adjust state machine */
1776 glyphpath->pathIsOpen = TRUE;
1778 glyphpath->offsetStart1 = P1; /* record second point */
1781 if ( glyphpath->elemIsQueued )
1783 FT_ASSERT( cf2_hintmap_isValid( &glyphpath->hintMap ) ||
1784 glyphpath->hintMap.count == 0 );
1786 cf2_glyphpath_pushPrevElem( glyphpath,
1787 &glyphpath->hintMap,
1793 /* queue the current element with offset points */
1794 glyphpath->elemIsQueued = TRUE;
1795 glyphpath->prevElemOp = CF2_PathOpLineTo;
1796 glyphpath->prevElemP0 = P0;
1797 glyphpath->prevElemP1 = P1;
1799 /* update current map */
1801 cf2_hintmap_build( &glyphpath->hintMap,
1802 glyphpath->hStemHintArray,
1803 glyphpath->vStemHintArray,
1804 glyphpath->hintMask,
1805 glyphpath->hintOriginY,
1808 glyphpath->currentCS.x = x; /* pre-offset current point */
1809 glyphpath->currentCS.y = y;
1813 FT_LOCAL_DEF( void )
1814 cf2_glyphpath_curveTo( CF2_GlyphPath glyphpath,
1822 CF2_Fixed xOffset1, yOffset1, xOffset3, yOffset3;
1823 FT_Vector P0, P1, P2, P3;
1826 /* TODO: ignore zero length portions of curve?? */
1827 cf2_glyphpath_computeOffset( glyphpath,
1828 glyphpath->currentCS.x,
1829 glyphpath->currentCS.y,
1834 cf2_glyphpath_computeOffset( glyphpath,
1842 /* add momentum from the middle segment */
1843 glyphpath->callbacks->windingMomentum =
1844 ADD_INT32( glyphpath->callbacks->windingMomentum,
1845 cf2_getWindingMomentum( x1, y1, x2, y2 ) );
1847 /* construct offset points */
1848 P0.x = ADD_INT32( glyphpath->currentCS.x, xOffset1 );
1849 P0.y = ADD_INT32( glyphpath->currentCS.y, yOffset1 );
1850 P1.x = ADD_INT32( x1, xOffset1 );
1851 P1.y = ADD_INT32( y1, yOffset1 );
1852 /* note: preserve angle of final segment by using offset3 at both ends */
1853 P2.x = ADD_INT32( x2, xOffset3 );
1854 P2.y = ADD_INT32( y2, yOffset3 );
1855 P3.x = ADD_INT32( x3, xOffset3 );
1856 P3.y = ADD_INT32( y3, yOffset3 );
1858 if ( glyphpath->moveIsPending )
1860 /* emit offset 1st point as MoveTo */
1861 cf2_glyphpath_pushMove( glyphpath, P0 );
1863 glyphpath->moveIsPending = FALSE;
1864 glyphpath->pathIsOpen = TRUE;
1866 glyphpath->offsetStart1 = P1; /* record second point */
1869 if ( glyphpath->elemIsQueued )
1871 FT_ASSERT( cf2_hintmap_isValid( &glyphpath->hintMap ) ||
1872 glyphpath->hintMap.count == 0 );
1874 cf2_glyphpath_pushPrevElem( glyphpath,
1875 &glyphpath->hintMap,
1881 /* queue the current element with offset points */
1882 glyphpath->elemIsQueued = TRUE;
1883 glyphpath->prevElemOp = CF2_PathOpCubeTo;
1884 glyphpath->prevElemP0 = P0;
1885 glyphpath->prevElemP1 = P1;
1886 glyphpath->prevElemP2 = P2;
1887 glyphpath->prevElemP3 = P3;
1889 /* update current map */
1890 if ( cf2_hintmask_isNew( glyphpath->hintMask ) )
1891 cf2_hintmap_build( &glyphpath->hintMap,
1892 glyphpath->hStemHintArray,
1893 glyphpath->vStemHintArray,
1894 glyphpath->hintMask,
1895 glyphpath->hintOriginY,
1898 glyphpath->currentCS.x = x3; /* pre-offset current point */
1899 glyphpath->currentCS.y = y3;
1903 FT_LOCAL_DEF( void )
1904 cf2_glyphpath_closeOpenPath( CF2_GlyphPath glyphpath )
1906 if ( glyphpath->pathIsOpen )
1909 * A closing line in Character Space line is always generated below
1910 * with `cf2_glyphPath_lineTo'. It may be ignored later if it turns
1911 * out to be zero length in Device Space.
1913 glyphpath->pathIsClosing = TRUE;
1915 cf2_glyphpath_lineTo( glyphpath,
1917 glyphpath->start.y );
1919 /* empty the final element from the queue and close the path */
1920 if ( glyphpath->elemIsQueued )
1921 cf2_glyphpath_pushPrevElem( glyphpath,
1922 &glyphpath->hintMap,
1923 &glyphpath->offsetStart0,
1924 glyphpath->offsetStart1,
1927 /* reset state machine */
1928 glyphpath->moveIsPending = TRUE;
1929 glyphpath->pathIsOpen = FALSE;
1930 glyphpath->pathIsClosing = FALSE;
1931 glyphpath->elemIsQueued = FALSE;