Home | History | Annotate | Download | only in base
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ftoutln.c                                                              */
      4 /*                                                                         */
      5 /*    FreeType outline management (body).                                  */
      6 /*                                                                         */
      7 /*  Copyright 1996-2008, 2010, 2012-2014 by                                */
      8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
      9 /*                                                                         */
     10 /*  This file is part of the FreeType project, and may only be used,       */
     11 /*  modified, and distributed under the terms of the FreeType project      */
     12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
     13 /*  this file you indicate that you have read the license and              */
     14 /*  understand and accept it fully.                                        */
     15 /*                                                                         */
     16 /***************************************************************************/
     17 
     18 
     19   /*************************************************************************/
     20   /*                                                                       */
     21   /* All functions are declared in freetype.h.                             */
     22   /*                                                                       */
     23   /*************************************************************************/
     24 
     25 
     26 #include <ft2build.h>
     27 #include FT_OUTLINE_H
     28 #include FT_INTERNAL_OBJECTS_H
     29 #include FT_INTERNAL_CALC_H
     30 #include FT_INTERNAL_DEBUG_H
     31 #include FT_TRIGONOMETRY_H
     32 
     33 
     34   /*************************************************************************/
     35   /*                                                                       */
     36   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
     37   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
     38   /* messages during execution.                                            */
     39   /*                                                                       */
     40 #undef  FT_COMPONENT
     41 #define FT_COMPONENT  trace_outline
     42 
     43 
     44   static
     45   const FT_Outline  null_outline = { 0, 0, 0, 0, 0, 0 };
     46 
     47 
     48   /* documentation is in ftoutln.h */
     49 
     50   FT_EXPORT_DEF( FT_Error )
     51   FT_Outline_Decompose( FT_Outline*              outline,
     52                         const FT_Outline_Funcs*  func_interface,
     53                         void*                    user )
     54   {
     55 #undef SCALED
     56 #define SCALED( x )  ( ( (x) << shift ) - delta )
     57 
     58     FT_Vector   v_last;
     59     FT_Vector   v_control;
     60     FT_Vector   v_start;
     61 
     62     FT_Vector*  point;
     63     FT_Vector*  limit;
     64     char*       tags;
     65 
     66     FT_Error    error;
     67 
     68     FT_Int   n;         /* index of contour in outline     */
     69     FT_UInt  first;     /* index of first point in contour */
     70     FT_Int   tag;       /* current point's state           */
     71 
     72     FT_Int   shift;
     73     FT_Pos   delta;
     74 
     75 
     76     if ( !outline || !func_interface )
     77       return FT_THROW( Invalid_Argument );
     78 
     79     shift = func_interface->shift;
     80     delta = func_interface->delta;
     81     first = 0;
     82 
     83     for ( n = 0; n < outline->n_contours; n++ )
     84     {
     85       FT_Int  last;  /* index of last point in contour */
     86 
     87 
     88       FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
     89 
     90       last = outline->contours[n];
     91       if ( last < 0 )
     92         goto Invalid_Outline;
     93       limit = outline->points + last;
     94 
     95       v_start   = outline->points[first];
     96       v_start.x = SCALED( v_start.x );
     97       v_start.y = SCALED( v_start.y );
     98 
     99       v_last   = outline->points[last];
    100       v_last.x = SCALED( v_last.x );
    101       v_last.y = SCALED( v_last.y );
    102 
    103       v_control = v_start;
    104 
    105       point = outline->points + first;
    106       tags  = outline->tags   + first;
    107       tag   = FT_CURVE_TAG( tags[0] );
    108 
    109       /* A contour cannot start with a cubic control point! */
    110       if ( tag == FT_CURVE_TAG_CUBIC )
    111         goto Invalid_Outline;
    112 
    113       /* check first point to determine origin */
    114       if ( tag == FT_CURVE_TAG_CONIC )
    115       {
    116         /* first point is conic control.  Yes, this happens. */
    117         if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
    118         {
    119           /* start at last point if it is on the curve */
    120           v_start = v_last;
    121           limit--;
    122         }
    123         else
    124         {
    125           /* if both first and last points are conic,         */
    126           /* start at their middle and record its position    */
    127           /* for closure                                      */
    128           v_start.x = ( v_start.x + v_last.x ) / 2;
    129           v_start.y = ( v_start.y + v_last.y ) / 2;
    130 
    131        /* v_last = v_start; */
    132         }
    133         point--;
    134         tags--;
    135       }
    136 
    137       FT_TRACE5(( "  move to (%.2f, %.2f)\n",
    138                   v_start.x / 64.0, v_start.y / 64.0 ));
    139       error = func_interface->move_to( &v_start, user );
    140       if ( error )
    141         goto Exit;
    142 
    143       while ( point < limit )
    144       {
    145         point++;
    146         tags++;
    147 
    148         tag = FT_CURVE_TAG( tags[0] );
    149         switch ( tag )
    150         {
    151         case FT_CURVE_TAG_ON:  /* emit a single line_to */
    152           {
    153             FT_Vector  vec;
    154 
    155 
    156             vec.x = SCALED( point->x );
    157             vec.y = SCALED( point->y );
    158 
    159             FT_TRACE5(( "  line to (%.2f, %.2f)\n",
    160                         vec.x / 64.0, vec.y / 64.0 ));
    161             error = func_interface->line_to( &vec, user );
    162             if ( error )
    163               goto Exit;
    164             continue;
    165           }
    166 
    167         case FT_CURVE_TAG_CONIC:  /* consume conic arcs */
    168           v_control.x = SCALED( point->x );
    169           v_control.y = SCALED( point->y );
    170 
    171         Do_Conic:
    172           if ( point < limit )
    173           {
    174             FT_Vector  vec;
    175             FT_Vector  v_middle;
    176 
    177 
    178             point++;
    179             tags++;
    180             tag = FT_CURVE_TAG( tags[0] );
    181 
    182             vec.x = SCALED( point->x );
    183             vec.y = SCALED( point->y );
    184 
    185             if ( tag == FT_CURVE_TAG_ON )
    186             {
    187               FT_TRACE5(( "  conic to (%.2f, %.2f)"
    188                           " with control (%.2f, %.2f)\n",
    189                           vec.x / 64.0, vec.y / 64.0,
    190                           v_control.x / 64.0, v_control.y / 64.0 ));
    191               error = func_interface->conic_to( &v_control, &vec, user );
    192               if ( error )
    193                 goto Exit;
    194               continue;
    195             }
    196 
    197             if ( tag != FT_CURVE_TAG_CONIC )
    198               goto Invalid_Outline;
    199 
    200             v_middle.x = ( v_control.x + vec.x ) / 2;
    201             v_middle.y = ( v_control.y + vec.y ) / 2;
    202 
    203             FT_TRACE5(( "  conic to (%.2f, %.2f)"
    204                         " with control (%.2f, %.2f)\n",
    205                         v_middle.x / 64.0, v_middle.y / 64.0,
    206                         v_control.x / 64.0, v_control.y / 64.0 ));
    207             error = func_interface->conic_to( &v_control, &v_middle, user );
    208             if ( error )
    209               goto Exit;
    210 
    211             v_control = vec;
    212             goto Do_Conic;
    213           }
    214 
    215           FT_TRACE5(( "  conic to (%.2f, %.2f)"
    216                       " with control (%.2f, %.2f)\n",
    217                       v_start.x / 64.0, v_start.y / 64.0,
    218                       v_control.x / 64.0, v_control.y / 64.0 ));
    219           error = func_interface->conic_to( &v_control, &v_start, user );
    220           goto Close;
    221 
    222         default:  /* FT_CURVE_TAG_CUBIC */
    223           {
    224             FT_Vector  vec1, vec2;
    225 
    226 
    227             if ( point + 1 > limit                             ||
    228                  FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
    229               goto Invalid_Outline;
    230 
    231             point += 2;
    232             tags  += 2;
    233 
    234             vec1.x = SCALED( point[-2].x );
    235             vec1.y = SCALED( point[-2].y );
    236 
    237             vec2.x = SCALED( point[-1].x );
    238             vec2.y = SCALED( point[-1].y );
    239 
    240             if ( point <= limit )
    241             {
    242               FT_Vector  vec;
    243 
    244 
    245               vec.x = SCALED( point->x );
    246               vec.y = SCALED( point->y );
    247 
    248               FT_TRACE5(( "  cubic to (%.2f, %.2f)"
    249                           " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
    250                           vec.x / 64.0, vec.y / 64.0,
    251                           vec1.x / 64.0, vec1.y / 64.0,
    252                           vec2.x / 64.0, vec2.y / 64.0 ));
    253               error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
    254               if ( error )
    255                 goto Exit;
    256               continue;
    257             }
    258 
    259             FT_TRACE5(( "  cubic to (%.2f, %.2f)"
    260                         " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
    261                         v_start.x / 64.0, v_start.y / 64.0,
    262                         vec1.x / 64.0, vec1.y / 64.0,
    263                         vec2.x / 64.0, vec2.y / 64.0 ));
    264             error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
    265             goto Close;
    266           }
    267         }
    268       }
    269 
    270       /* close the contour with a line segment */
    271       FT_TRACE5(( "  line to (%.2f, %.2f)\n",
    272                   v_start.x / 64.0, v_start.y / 64.0 ));
    273       error = func_interface->line_to( &v_start, user );
    274 
    275     Close:
    276       if ( error )
    277         goto Exit;
    278 
    279       first = last + 1;
    280     }
    281 
    282     FT_TRACE5(( "FT_Outline_Decompose: Done\n", n ));
    283     return FT_Err_Ok;
    284 
    285   Exit:
    286     FT_TRACE5(( "FT_Outline_Decompose: Error %d\n", error ));
    287     return error;
    288 
    289   Invalid_Outline:
    290     return FT_THROW( Invalid_Outline );
    291   }
    292 
    293 
    294   FT_EXPORT_DEF( FT_Error )
    295   FT_Outline_New_Internal( FT_Memory    memory,
    296                            FT_UInt      numPoints,
    297                            FT_Int       numContours,
    298                            FT_Outline  *anoutline )
    299   {
    300     FT_Error  error;
    301 
    302 
    303     if ( !anoutline || !memory )
    304       return FT_THROW( Invalid_Argument );
    305 
    306     *anoutline = null_outline;
    307 
    308     if ( numContours < 0                  ||
    309          (FT_UInt)numContours > numPoints )
    310       return FT_THROW( Invalid_Argument );
    311 
    312     if ( numPoints > FT_OUTLINE_POINTS_MAX )
    313       return FT_THROW( Array_Too_Large );
    314 
    315     if ( FT_NEW_ARRAY( anoutline->points,   numPoints   ) ||
    316          FT_NEW_ARRAY( anoutline->tags,     numPoints   ) ||
    317          FT_NEW_ARRAY( anoutline->contours, numContours ) )
    318       goto Fail;
    319 
    320     anoutline->n_points    = (FT_UShort)numPoints;
    321     anoutline->n_contours  = (FT_Short)numContours;
    322     anoutline->flags      |= FT_OUTLINE_OWNER;
    323 
    324     return FT_Err_Ok;
    325 
    326   Fail:
    327     anoutline->flags |= FT_OUTLINE_OWNER;
    328     FT_Outline_Done_Internal( memory, anoutline );
    329 
    330     return error;
    331   }
    332 
    333 
    334   /* documentation is in ftoutln.h */
    335 
    336   FT_EXPORT_DEF( FT_Error )
    337   FT_Outline_New( FT_Library   library,
    338                   FT_UInt      numPoints,
    339                   FT_Int       numContours,
    340                   FT_Outline  *anoutline )
    341   {
    342     if ( !library )
    343       return FT_THROW( Invalid_Library_Handle );
    344 
    345     return FT_Outline_New_Internal( library->memory, numPoints,
    346                                     numContours, anoutline );
    347   }
    348 
    349 
    350   /* documentation is in ftoutln.h */
    351 
    352   FT_EXPORT_DEF( FT_Error )
    353   FT_Outline_Check( FT_Outline*  outline )
    354   {
    355     if ( outline )
    356     {
    357       FT_Int  n_points   = outline->n_points;
    358       FT_Int  n_contours = outline->n_contours;
    359       FT_Int  end0, end;
    360       FT_Int  n;
    361 
    362 
    363       /* empty glyph? */
    364       if ( n_points == 0 && n_contours == 0 )
    365         return 0;
    366 
    367       /* check point and contour counts */
    368       if ( n_points <= 0 || n_contours <= 0 )
    369         goto Bad;
    370 
    371       end0 = end = -1;
    372       for ( n = 0; n < n_contours; n++ )
    373       {
    374         end = outline->contours[n];
    375 
    376         /* note that we don't accept empty contours */
    377         if ( end <= end0 || end >= n_points )
    378           goto Bad;
    379 
    380         end0 = end;
    381       }
    382 
    383       if ( end != n_points - 1 )
    384         goto Bad;
    385 
    386       /* XXX: check the tags array */
    387       return 0;
    388     }
    389 
    390   Bad:
    391     return FT_THROW( Invalid_Argument );
    392   }
    393 
    394 
    395   /* documentation is in ftoutln.h */
    396 
    397   FT_EXPORT_DEF( FT_Error )
    398   FT_Outline_Copy( const FT_Outline*  source,
    399                    FT_Outline        *target )
    400   {
    401     FT_Int  is_owner;
    402 
    403 
    404     if ( !source            || !target            ||
    405          source->n_points   != target->n_points   ||
    406          source->n_contours != target->n_contours )
    407       return FT_THROW( Invalid_Argument );
    408 
    409     if ( source == target )
    410       return FT_Err_Ok;
    411 
    412     FT_ARRAY_COPY( target->points, source->points, source->n_points );
    413 
    414     FT_ARRAY_COPY( target->tags, source->tags, source->n_points );
    415 
    416     FT_ARRAY_COPY( target->contours, source->contours, source->n_contours );
    417 
    418     /* copy all flags, except the `FT_OUTLINE_OWNER' one */
    419     is_owner      = target->flags & FT_OUTLINE_OWNER;
    420     target->flags = source->flags;
    421 
    422     target->flags &= ~FT_OUTLINE_OWNER;
    423     target->flags |= is_owner;
    424 
    425     return FT_Err_Ok;
    426   }
    427 
    428 
    429   FT_EXPORT_DEF( FT_Error )
    430   FT_Outline_Done_Internal( FT_Memory    memory,
    431                             FT_Outline*  outline )
    432   {
    433     if ( memory && outline )
    434     {
    435       if ( outline->flags & FT_OUTLINE_OWNER )
    436       {
    437         FT_FREE( outline->points   );
    438         FT_FREE( outline->tags     );
    439         FT_FREE( outline->contours );
    440       }
    441       *outline = null_outline;
    442 
    443       return FT_Err_Ok;
    444     }
    445     else
    446       return FT_THROW( Invalid_Argument );
    447   }
    448 
    449 
    450   /* documentation is in ftoutln.h */
    451 
    452   FT_EXPORT_DEF( FT_Error )
    453   FT_Outline_Done( FT_Library   library,
    454                    FT_Outline*  outline )
    455   {
    456     /* check for valid `outline' in FT_Outline_Done_Internal() */
    457 
    458     if ( !library )
    459       return FT_THROW( Invalid_Library_Handle );
    460 
    461     return FT_Outline_Done_Internal( library->memory, outline );
    462   }
    463 
    464 
    465   /* documentation is in ftoutln.h */
    466 
    467   FT_EXPORT_DEF( void )
    468   FT_Outline_Get_CBox( const FT_Outline*  outline,
    469                        FT_BBox           *acbox )
    470   {
    471     FT_Pos  xMin, yMin, xMax, yMax;
    472 
    473 
    474     if ( outline && acbox )
    475     {
    476       if ( outline->n_points == 0 )
    477       {
    478         xMin = 0;
    479         yMin = 0;
    480         xMax = 0;
    481         yMax = 0;
    482       }
    483       else
    484       {
    485         FT_Vector*  vec   = outline->points;
    486         FT_Vector*  limit = vec + outline->n_points;
    487 
    488 
    489         xMin = xMax = vec->x;
    490         yMin = yMax = vec->y;
    491         vec++;
    492 
    493         for ( ; vec < limit; vec++ )
    494         {
    495           FT_Pos  x, y;
    496 
    497 
    498           x = vec->x;
    499           if ( x < xMin ) xMin = x;
    500           if ( x > xMax ) xMax = x;
    501 
    502           y = vec->y;
    503           if ( y < yMin ) yMin = y;
    504           if ( y > yMax ) yMax = y;
    505         }
    506       }
    507       acbox->xMin = xMin;
    508       acbox->xMax = xMax;
    509       acbox->yMin = yMin;
    510       acbox->yMax = yMax;
    511     }
    512   }
    513 
    514 
    515   /* documentation is in ftoutln.h */
    516 
    517   FT_EXPORT_DEF( void )
    518   FT_Outline_Translate( const FT_Outline*  outline,
    519                         FT_Pos             xOffset,
    520                         FT_Pos             yOffset )
    521   {
    522     FT_UShort   n;
    523     FT_Vector*  vec;
    524 
    525 
    526     if ( !outline )
    527       return;
    528 
    529     vec = outline->points;
    530 
    531     for ( n = 0; n < outline->n_points; n++ )
    532     {
    533       vec->x += xOffset;
    534       vec->y += yOffset;
    535       vec++;
    536     }
    537   }
    538 
    539 
    540   /* documentation is in ftoutln.h */
    541 
    542   FT_EXPORT_DEF( void )
    543   FT_Outline_Reverse( FT_Outline*  outline )
    544   {
    545     FT_UShort  n;
    546     FT_Int     first, last;
    547 
    548 
    549     if ( !outline )
    550       return;
    551 
    552     first = 0;
    553 
    554     for ( n = 0; n < outline->n_contours; n++ )
    555     {
    556       last  = outline->contours[n];
    557 
    558       /* reverse point table */
    559       {
    560         FT_Vector*  p = outline->points + first;
    561         FT_Vector*  q = outline->points + last;
    562         FT_Vector   swap;
    563 
    564 
    565         while ( p < q )
    566         {
    567           swap = *p;
    568           *p   = *q;
    569           *q   = swap;
    570           p++;
    571           q--;
    572         }
    573       }
    574 
    575       /* reverse tags table */
    576       {
    577         char*  p = outline->tags + first;
    578         char*  q = outline->tags + last;
    579 
    580 
    581         while ( p < q )
    582         {
    583           char  swap;
    584 
    585 
    586           swap = *p;
    587           *p   = *q;
    588           *q   = swap;
    589           p++;
    590           q--;
    591         }
    592       }
    593 
    594       first = last + 1;
    595     }
    596 
    597     outline->flags ^= FT_OUTLINE_REVERSE_FILL;
    598   }
    599 
    600 
    601   /* documentation is in ftoutln.h */
    602 
    603   FT_EXPORT_DEF( FT_Error )
    604   FT_Outline_Render( FT_Library         library,
    605                      FT_Outline*        outline,
    606                      FT_Raster_Params*  params )
    607   {
    608     FT_Error     error;
    609     FT_Bool      update = FALSE;
    610     FT_Renderer  renderer;
    611     FT_ListNode  node;
    612 
    613 
    614     if ( !library )
    615       return FT_THROW( Invalid_Library_Handle );
    616 
    617     if ( !outline || !params )
    618       return FT_THROW( Invalid_Argument );
    619 
    620     renderer = library->cur_renderer;
    621     node     = library->renderers.head;
    622 
    623     params->source = (void*)outline;
    624 
    625     error = FT_ERR( Cannot_Render_Glyph );
    626     while ( renderer )
    627     {
    628       error = renderer->raster_render( renderer->raster, params );
    629       if ( !error || FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
    630         break;
    631 
    632       /* FT_Err_Cannot_Render_Glyph is returned if the render mode   */
    633       /* is unsupported by the current renderer for this glyph image */
    634       /* format                                                      */
    635 
    636       /* now, look for another renderer that supports the same */
    637       /* format                                                */
    638       renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
    639                                      &node );
    640       update   = TRUE;
    641     }
    642 
    643     /* if we changed the current renderer for the glyph image format */
    644     /* we need to select it as the next current one                  */
    645     if ( !error && update && renderer )
    646       FT_Set_Renderer( library, renderer, 0, 0 );
    647 
    648     return error;
    649   }
    650 
    651 
    652   /* documentation is in ftoutln.h */
    653 
    654   FT_EXPORT_DEF( FT_Error )
    655   FT_Outline_Get_Bitmap( FT_Library        library,
    656                          FT_Outline*       outline,
    657                          const FT_Bitmap  *abitmap )
    658   {
    659     FT_Raster_Params  params;
    660 
    661 
    662     if ( !abitmap )
    663       return FT_THROW( Invalid_Argument );
    664 
    665     /* other checks are delayed to FT_Outline_Render() */
    666 
    667     params.target = abitmap;
    668     params.flags  = 0;
    669 
    670     if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY  ||
    671          abitmap->pixel_mode == FT_PIXEL_MODE_LCD   ||
    672          abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
    673       params.flags |= FT_RASTER_FLAG_AA;
    674 
    675     return FT_Outline_Render( library, outline, &params );
    676   }
    677 
    678 
    679   /* documentation is in freetype.h */
    680 
    681   FT_EXPORT_DEF( void )
    682   FT_Vector_Transform( FT_Vector*        vector,
    683                        const FT_Matrix*  matrix )
    684   {
    685     FT_Pos  xz, yz;
    686 
    687 
    688     if ( !vector || !matrix )
    689       return;
    690 
    691     xz = FT_MulFix( vector->x, matrix->xx ) +
    692          FT_MulFix( vector->y, matrix->xy );
    693 
    694     yz = FT_MulFix( vector->x, matrix->yx ) +
    695          FT_MulFix( vector->y, matrix->yy );
    696 
    697     vector->x = xz;
    698     vector->y = yz;
    699   }
    700 
    701 
    702   /* documentation is in ftoutln.h */
    703 
    704   FT_EXPORT_DEF( void )
    705   FT_Outline_Transform( const FT_Outline*  outline,
    706                         const FT_Matrix*   matrix )
    707   {
    708     FT_Vector*  vec;
    709     FT_Vector*  limit;
    710 
    711 
    712     if ( !outline || !matrix )
    713       return;
    714 
    715     vec   = outline->points;
    716     limit = vec + outline->n_points;
    717 
    718     for ( ; vec < limit; vec++ )
    719       FT_Vector_Transform( vec, matrix );
    720   }
    721 
    722 
    723 #if 0
    724 
    725 #define FT_OUTLINE_GET_CONTOUR( outline, c, first, last )  \
    726   do                                                       \
    727   {                                                        \
    728     (first) = ( c > 0 ) ? (outline)->points +              \
    729                             (outline)->contours[c - 1] + 1 \
    730                         : (outline)->points;               \
    731     (last) = (outline)->points + (outline)->contours[c];   \
    732   } while ( 0 )
    733 
    734 
    735   /* Is a point in some contour?                     */
    736   /*                                                 */
    737   /* We treat every point of the contour as if it    */
    738   /* it were ON.  That is, we allow false positives, */
    739   /* but disallow false negatives.  (XXX really?)    */
    740   static FT_Bool
    741   ft_contour_has( FT_Outline*  outline,
    742                   FT_Short     c,
    743                   FT_Vector*   point )
    744   {
    745     FT_Vector*  first;
    746     FT_Vector*  last;
    747     FT_Vector*  a;
    748     FT_Vector*  b;
    749     FT_UInt     n = 0;
    750 
    751 
    752     FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
    753 
    754     for ( a = first; a <= last; a++ )
    755     {
    756       FT_Pos  x;
    757       FT_Int  intersect;
    758 
    759 
    760       b = ( a == last ) ? first : a + 1;
    761 
    762       intersect = ( a->y - point->y ) ^ ( b->y - point->y );
    763 
    764       /* a and b are on the same side */
    765       if ( intersect >= 0 )
    766       {
    767         if ( intersect == 0 && a->y == point->y )
    768         {
    769           if ( ( a->x <= point->x && b->x >= point->x ) ||
    770                ( a->x >= point->x && b->x <= point->x ) )
    771             return 1;
    772         }
    773 
    774         continue;
    775       }
    776 
    777       x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
    778 
    779       if ( x < point->x )
    780         n++;
    781       else if ( x == point->x )
    782         return 1;
    783     }
    784 
    785     return n & 1;
    786   }
    787 
    788 
    789   static FT_Bool
    790   ft_contour_enclosed( FT_Outline*  outline,
    791                        FT_UShort    c )
    792   {
    793     FT_Vector*  first;
    794     FT_Vector*  last;
    795     FT_Short    i;
    796 
    797 
    798     FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
    799 
    800     for ( i = 0; i < outline->n_contours; i++ )
    801     {
    802       if ( i != c && ft_contour_has( outline, i, first ) )
    803       {
    804         FT_Vector*  pt;
    805 
    806 
    807         for ( pt = first + 1; pt <= last; pt++ )
    808           if ( !ft_contour_has( outline, i, pt ) )
    809             return 0;
    810 
    811         return 1;
    812       }
    813     }
    814 
    815     return 0;
    816   }
    817 
    818 
    819   /* This version differs from the public one in that each */
    820   /* part (contour not enclosed in another contour) of the */
    821   /* outline is checked for orientation.  This is          */
    822   /* necessary for some buggy CJK fonts.                   */
    823   static FT_Orientation
    824   ft_outline_get_orientation( FT_Outline*  outline )
    825   {
    826     FT_Short        i;
    827     FT_Vector*      first;
    828     FT_Vector*      last;
    829     FT_Orientation  orient = FT_ORIENTATION_NONE;
    830 
    831 
    832     first = outline->points;
    833     for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
    834     {
    835       FT_Vector*  point;
    836       FT_Vector*  xmin_point;
    837       FT_Pos      xmin;
    838 
    839 
    840       last = outline->points + outline->contours[i];
    841 
    842       /* skip degenerate contours */
    843       if ( last < first + 2 )
    844         continue;
    845 
    846       if ( ft_contour_enclosed( outline, i ) )
    847         continue;
    848 
    849       xmin       = first->x;
    850       xmin_point = first;
    851 
    852       for ( point = first + 1; point <= last; point++ )
    853       {
    854         if ( point->x < xmin )
    855         {
    856           xmin       = point->x;
    857           xmin_point = point;
    858         }
    859       }
    860 
    861       /* check the orientation of the contour */
    862       {
    863         FT_Vector*      prev;
    864         FT_Vector*      next;
    865         FT_Orientation  o;
    866 
    867 
    868         prev = ( xmin_point == first ) ? last : xmin_point - 1;
    869         next = ( xmin_point == last ) ? first : xmin_point + 1;
    870 
    871         if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
    872              FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
    873           o = FT_ORIENTATION_POSTSCRIPT;
    874         else
    875           o = FT_ORIENTATION_TRUETYPE;
    876 
    877         if ( orient == FT_ORIENTATION_NONE )
    878           orient = o;
    879         else if ( orient != o )
    880           return FT_ORIENTATION_NONE;
    881       }
    882     }
    883 
    884     return orient;
    885   }
    886 
    887 #endif /* 0 */
    888 
    889 
    890   /* documentation is in ftoutln.h */
    891 
    892   FT_EXPORT_DEF( FT_Error )
    893   FT_Outline_Embolden( FT_Outline*  outline,
    894                        FT_Pos       strength )
    895   {
    896     return FT_Outline_EmboldenXY( outline, strength, strength );
    897   }
    898 
    899 
    900   /* documentation is in ftoutln.h */
    901 
    902   FT_EXPORT_DEF( FT_Error )
    903   FT_Outline_EmboldenXY( FT_Outline*  outline,
    904                          FT_Pos       xstrength,
    905                          FT_Pos       ystrength )
    906   {
    907     FT_Vector*  points;
    908     FT_Vector   v_prev, v_first, v_next, v_cur;
    909     FT_Int      c, n, first;
    910     FT_Int      orientation;
    911 
    912 
    913     if ( !outline )
    914       return FT_THROW( Invalid_Argument );
    915 
    916     xstrength /= 2;
    917     ystrength /= 2;
    918     if ( xstrength == 0 && ystrength == 0 )
    919       return FT_Err_Ok;
    920 
    921     orientation = FT_Outline_Get_Orientation( outline );
    922     if ( orientation == FT_ORIENTATION_NONE )
    923     {
    924       if ( outline->n_contours )
    925         return FT_THROW( Invalid_Argument );
    926       else
    927         return FT_Err_Ok;
    928     }
    929 
    930     points = outline->points;
    931 
    932     first = 0;
    933     for ( c = 0; c < outline->n_contours; c++ )
    934     {
    935       FT_Vector  in, out, shift;
    936       FT_Fixed   l_in, l_out, l, q, d;
    937       int        last = outline->contours[c];
    938 
    939 
    940       v_first = points[first];
    941       v_prev  = points[last];
    942       v_cur   = v_first;
    943 
    944       /* compute incoming normalized vector */
    945       in.x = v_cur.x - v_prev.x;
    946       in.y = v_cur.y - v_prev.y;
    947       l_in = FT_Vector_Length( &in );
    948       if ( l_in )
    949       {
    950         in.x = FT_DivFix( in.x, l_in );
    951         in.y = FT_DivFix( in.y, l_in );
    952       }
    953 
    954       for ( n = first; n <= last; n++ )
    955       {
    956         if ( n < last )
    957           v_next = points[n + 1];
    958         else
    959           v_next = v_first;
    960 
    961         /* compute outgoing normalized vector */
    962         out.x = v_next.x - v_cur.x;
    963         out.y = v_next.y - v_cur.y;
    964         l_out = FT_Vector_Length( &out );
    965         if ( l_out )
    966         {
    967           out.x = FT_DivFix( out.x, l_out );
    968           out.y = FT_DivFix( out.y, l_out );
    969         }
    970 
    971         d = FT_MulFix( in.x, out.x ) + FT_MulFix( in.y, out.y );
    972 
    973         /* shift only if turn is less than ~160 degrees */
    974         if ( d > -0xF000L )
    975         {
    976           d = d + 0x10000L;
    977 
    978           /* shift components are aligned along lateral bisector */
    979           /* and directed according to the outline orientation.  */
    980           shift.x = in.y + out.y;
    981           shift.y = in.x + out.x;
    982 
    983           if ( orientation == FT_ORIENTATION_TRUETYPE )
    984             shift.x = -shift.x;
    985           else
    986             shift.y = -shift.y;
    987 
    988           /* restrict shift magnitude to better handle collapsing segments */
    989           q = FT_MulFix( out.x, in.y ) - FT_MulFix( out.y, in.x );
    990           if ( orientation == FT_ORIENTATION_TRUETYPE )
    991             q = -q;
    992 
    993           l = FT_MIN( l_in, l_out );
    994 
    995           /* non-strict inequalities avoid divide-by-zero when q == l == 0 */
    996           if ( FT_MulFix( xstrength, q ) <= FT_MulFix( d, l ) )
    997             shift.x = FT_MulDiv( shift.x, xstrength, d );
    998           else
    999             shift.x = FT_MulDiv( shift.x, l, q );
   1000 
   1001 
   1002           if ( FT_MulFix( ystrength, q ) <= FT_MulFix( d, l ) )
   1003             shift.y = FT_MulDiv( shift.y, ystrength, d );
   1004           else
   1005             shift.y = FT_MulDiv( shift.y, l, q );
   1006         }
   1007         else
   1008           shift.x = shift.y = 0;
   1009 
   1010         outline->points[n].x = v_cur.x + xstrength + shift.x;
   1011         outline->points[n].y = v_cur.y + ystrength + shift.y;
   1012 
   1013         in    = out;
   1014         l_in  = l_out;
   1015         v_cur = v_next;
   1016       }
   1017 
   1018       first = last + 1;
   1019     }
   1020 
   1021     return FT_Err_Ok;
   1022   }
   1023 
   1024 
   1025   /* documentation is in ftoutln.h */
   1026 
   1027   FT_EXPORT_DEF( FT_Orientation )
   1028   FT_Outline_Get_Orientation( FT_Outline*  outline )
   1029   {
   1030     FT_BBox     cbox;
   1031     FT_Int      xshift, yshift;
   1032     FT_Vector*  points;
   1033     FT_Vector   v_prev, v_cur;
   1034     FT_Int      c, n, first;
   1035     FT_Pos      area = 0;
   1036 
   1037 
   1038     if ( !outline || outline->n_points <= 0 )
   1039       return FT_ORIENTATION_TRUETYPE;
   1040 
   1041     /* We use the nonzero winding rule to find the orientation.       */
   1042     /* Since glyph outlines behave much more `regular' than arbitrary */
   1043     /* cubic or quadratic curves, this test deals with the polygon    */
   1044     /* only which is spanned up by the control points.                */
   1045 
   1046     FT_Outline_Get_CBox( outline, &cbox );
   1047 
   1048     xshift = FT_MSB( FT_ABS( cbox.xMax ) | FT_ABS( cbox.xMin ) ) - 14;
   1049     xshift = FT_MAX( xshift, 0 );
   1050 
   1051     yshift = FT_MSB( cbox.yMax - cbox.yMin ) - 14;
   1052     yshift = FT_MAX( yshift, 0 );
   1053 
   1054     points = outline->points;
   1055 
   1056     first = 0;
   1057     for ( c = 0; c < outline->n_contours; c++ )
   1058     {
   1059       FT_Int  last = outline->contours[c];
   1060 
   1061 
   1062       v_prev = points[last];
   1063 
   1064       for ( n = first; n <= last; n++ )
   1065       {
   1066         v_cur = points[n];
   1067         area += ( ( v_cur.y - v_prev.y ) >> yshift ) *
   1068                 ( ( v_cur.x + v_prev.x ) >> xshift );
   1069         v_prev = v_cur;
   1070       }
   1071 
   1072       first = last + 1;
   1073     }
   1074 
   1075     if ( area > 0 )
   1076       return FT_ORIENTATION_POSTSCRIPT;
   1077     else if ( area < 0 )
   1078       return FT_ORIENTATION_TRUETYPE;
   1079     else
   1080       return FT_ORIENTATION_NONE;
   1081   }
   1082 
   1083 
   1084 /* END */
   1085