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