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