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