• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftoutln.c                                                              */
4 /*                                                                         */
5 /*    FreeType outline management (body).                                  */
6 /*                                                                         */
7 /*  Copyright 1996-2015 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 )
FT_Outline_Decompose(FT_Outline * outline,const FT_Outline_Funcs * func_interface,void * user)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 = (FT_UInt)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 )
FT_Outline_New_Internal(FT_Memory memory,FT_UInt numPoints,FT_Int numContours,FT_Outline * anoutline)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_Short)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 )
FT_Outline_New(FT_Library library,FT_UInt numPoints,FT_Int numContours,FT_Outline * anoutline)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 )
FT_Outline_Check(FT_Outline * outline)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 )
FT_Outline_Copy(const FT_Outline * source,FT_Outline * target)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 )
FT_Outline_Done_Internal(FT_Memory memory,FT_Outline * outline)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 )
FT_Outline_Done(FT_Library library,FT_Outline * outline)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 )
FT_Outline_Get_CBox(const FT_Outline * outline,FT_BBox * acbox)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 )
FT_Outline_Translate(const FT_Outline * outline,FT_Pos xOffset,FT_Pos yOffset)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 )
FT_Outline_Reverse(FT_Outline * outline)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 )
FT_Outline_Render(FT_Library library,FT_Outline * outline,FT_Raster_Params * params)610   FT_Outline_Render( FT_Library         library,
611                      FT_Outline*        outline,
612                      FT_Raster_Params*  params )
613   {
614     FT_Error     error;
615     FT_Renderer  renderer;
616     FT_ListNode  node;
617 
618 
619     if ( !library )
620       return FT_THROW( Invalid_Library_Handle );
621 
622     if ( !outline )
623       return FT_THROW( Invalid_Outline );
624 
625     if ( !params )
626       return FT_THROW( Invalid_Argument );
627 
628     renderer = library->cur_renderer;
629     node     = library->renderers.head;
630 
631     params->source = (void*)outline;
632 
633     error = FT_ERR( Cannot_Render_Glyph );
634     while ( renderer )
635     {
636       error = renderer->raster_render( renderer->raster, params );
637       if ( !error || FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
638         break;
639 
640       /* FT_Err_Cannot_Render_Glyph is returned if the render mode   */
641       /* is unsupported by the current renderer for this glyph image */
642       /* format                                                      */
643 
644       /* now, look for another renderer that supports the same */
645       /* format                                                */
646       renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
647                                      &node );
648     }
649 
650     return error;
651   }
652 
653 
654   /* documentation is in ftoutln.h */
655 
656   FT_EXPORT_DEF( FT_Error )
FT_Outline_Get_Bitmap(FT_Library library,FT_Outline * outline,const FT_Bitmap * abitmap)657   FT_Outline_Get_Bitmap( FT_Library        library,
658                          FT_Outline*       outline,
659                          const FT_Bitmap  *abitmap )
660   {
661     FT_Raster_Params  params;
662 
663 
664     if ( !abitmap )
665       return FT_THROW( Invalid_Argument );
666 
667     /* other checks are delayed to `FT_Outline_Render' */
668 
669     params.target = abitmap;
670     params.flags  = 0;
671 
672     if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY  ||
673          abitmap->pixel_mode == FT_PIXEL_MODE_LCD   ||
674          abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
675       params.flags |= FT_RASTER_FLAG_AA;
676 
677     return FT_Outline_Render( library, outline, &params );
678   }
679 
680 
681   /* documentation is in freetype.h */
682 
683   FT_EXPORT_DEF( void )
FT_Vector_Transform(FT_Vector * vector,const FT_Matrix * matrix)684   FT_Vector_Transform( FT_Vector*        vector,
685                        const FT_Matrix*  matrix )
686   {
687     FT_Pos  xz, yz;
688 
689 
690     if ( !vector || !matrix )
691       return;
692 
693     xz = FT_MulFix( vector->x, matrix->xx ) +
694          FT_MulFix( vector->y, matrix->xy );
695 
696     yz = FT_MulFix( vector->x, matrix->yx ) +
697          FT_MulFix( vector->y, matrix->yy );
698 
699     vector->x = xz;
700     vector->y = yz;
701   }
702 
703 
704   /* documentation is in ftoutln.h */
705 
706   FT_EXPORT_DEF( void )
FT_Outline_Transform(const FT_Outline * outline,const FT_Matrix * matrix)707   FT_Outline_Transform( const FT_Outline*  outline,
708                         const FT_Matrix*   matrix )
709   {
710     FT_Vector*  vec;
711     FT_Vector*  limit;
712 
713 
714     if ( !outline || !matrix )
715       return;
716 
717     vec   = outline->points;
718     limit = vec + outline->n_points;
719 
720     for ( ; vec < limit; vec++ )
721       FT_Vector_Transform( vec, matrix );
722   }
723 
724 
725 #if 0
726 
727 #define FT_OUTLINE_GET_CONTOUR( outline, c, first, last )  \
728   do                                                       \
729   {                                                        \
730     (first) = ( c > 0 ) ? (outline)->points +              \
731                             (outline)->contours[c - 1] + 1 \
732                         : (outline)->points;               \
733     (last) = (outline)->points + (outline)->contours[c];   \
734   } while ( 0 )
735 
736 
737   /* Is a point in some contour?                     */
738   /*                                                 */
739   /* We treat every point of the contour as if it    */
740   /* it were ON.  That is, we allow false positives, */
741   /* but disallow false negatives.  (XXX really?)    */
742   static FT_Bool
743   ft_contour_has( FT_Outline*  outline,
744                   FT_Short     c,
745                   FT_Vector*   point )
746   {
747     FT_Vector*  first;
748     FT_Vector*  last;
749     FT_Vector*  a;
750     FT_Vector*  b;
751     FT_UInt     n = 0;
752 
753 
754     FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
755 
756     for ( a = first; a <= last; a++ )
757     {
758       FT_Pos  x;
759       FT_Int  intersect;
760 
761 
762       b = ( a == last ) ? first : a + 1;
763 
764       intersect = ( a->y - point->y ) ^ ( b->y - point->y );
765 
766       /* a and b are on the same side */
767       if ( intersect >= 0 )
768       {
769         if ( intersect == 0 && a->y == point->y )
770         {
771           if ( ( a->x <= point->x && b->x >= point->x ) ||
772                ( a->x >= point->x && b->x <= point->x ) )
773             return 1;
774         }
775 
776         continue;
777       }
778 
779       x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
780 
781       if ( x < point->x )
782         n++;
783       else if ( x == point->x )
784         return 1;
785     }
786 
787     return n & 1;
788   }
789 
790 
791   static FT_Bool
792   ft_contour_enclosed( FT_Outline*  outline,
793                        FT_UShort    c )
794   {
795     FT_Vector*  first;
796     FT_Vector*  last;
797     FT_Short    i;
798 
799 
800     FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
801 
802     for ( i = 0; i < outline->n_contours; i++ )
803     {
804       if ( i != c && ft_contour_has( outline, i, first ) )
805       {
806         FT_Vector*  pt;
807 
808 
809         for ( pt = first + 1; pt <= last; pt++ )
810           if ( !ft_contour_has( outline, i, pt ) )
811             return 0;
812 
813         return 1;
814       }
815     }
816 
817     return 0;
818   }
819 
820 
821   /* This version differs from the public one in that each */
822   /* part (contour not enclosed in another contour) of the */
823   /* outline is checked for orientation.  This is          */
824   /* necessary for some buggy CJK fonts.                   */
825   static FT_Orientation
826   ft_outline_get_orientation( FT_Outline*  outline )
827   {
828     FT_Short        i;
829     FT_Vector*      first;
830     FT_Vector*      last;
831     FT_Orientation  orient = FT_ORIENTATION_NONE;
832 
833 
834     first = outline->points;
835     for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
836     {
837       FT_Vector*  point;
838       FT_Vector*  xmin_point;
839       FT_Pos      xmin;
840 
841 
842       last = outline->points + outline->contours[i];
843 
844       /* skip degenerate contours */
845       if ( last < first + 2 )
846         continue;
847 
848       if ( ft_contour_enclosed( outline, i ) )
849         continue;
850 
851       xmin       = first->x;
852       xmin_point = first;
853 
854       for ( point = first + 1; point <= last; point++ )
855       {
856         if ( point->x < xmin )
857         {
858           xmin       = point->x;
859           xmin_point = point;
860         }
861       }
862 
863       /* check the orientation of the contour */
864       {
865         FT_Vector*      prev;
866         FT_Vector*      next;
867         FT_Orientation  o;
868 
869 
870         prev = ( xmin_point == first ) ? last : xmin_point - 1;
871         next = ( xmin_point == last ) ? first : xmin_point + 1;
872 
873         if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
874              FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
875           o = FT_ORIENTATION_POSTSCRIPT;
876         else
877           o = FT_ORIENTATION_TRUETYPE;
878 
879         if ( orient == FT_ORIENTATION_NONE )
880           orient = o;
881         else if ( orient != o )
882           return FT_ORIENTATION_NONE;
883       }
884     }
885 
886     return orient;
887   }
888 
889 #endif /* 0 */
890 
891 
892   /* documentation is in ftoutln.h */
893 
894   FT_EXPORT_DEF( FT_Error )
FT_Outline_Embolden(FT_Outline * outline,FT_Pos strength)895   FT_Outline_Embolden( FT_Outline*  outline,
896                        FT_Pos       strength )
897   {
898     return FT_Outline_EmboldenXY( outline, strength, strength );
899   }
900 
901 
902   /* documentation is in ftoutln.h */
903 
904   FT_EXPORT_DEF( FT_Error )
FT_Outline_EmboldenXY(FT_Outline * outline,FT_Pos xstrength,FT_Pos ystrength)905   FT_Outline_EmboldenXY( FT_Outline*  outline,
906                          FT_Pos       xstrength,
907                          FT_Pos       ystrength )
908   {
909     FT_Vector*  points;
910     FT_Vector   v_prev, v_first, v_next, v_cur;
911     FT_Int      c, n, first;
912     FT_Int      orientation;
913 
914 
915     if ( !outline )
916       return FT_THROW( Invalid_Outline );
917 
918     xstrength /= 2;
919     ystrength /= 2;
920     if ( xstrength == 0 && ystrength == 0 )
921       return FT_Err_Ok;
922 
923     orientation = FT_Outline_Get_Orientation( outline );
924     if ( orientation == FT_ORIENTATION_NONE )
925     {
926       if ( outline->n_contours )
927         return FT_THROW( Invalid_Argument );
928       else
929         return FT_Err_Ok;
930     }
931 
932     points = outline->points;
933 
934     first = 0;
935     for ( c = 0; c < outline->n_contours; c++ )
936     {
937       FT_Vector  in, out, shift;
938       FT_Fixed   l_in, l_out, l, q, d;
939       int        last = outline->contours[c];
940 
941 
942       v_first = points[first];
943       v_prev  = points[last];
944       v_cur   = v_first;
945 
946       /* compute incoming normalized vector */
947       in.x = v_cur.x - v_prev.x;
948       in.y = v_cur.y - v_prev.y;
949       l_in = FT_Vector_Length( &in );
950       if ( l_in )
951       {
952         in.x = FT_DivFix( in.x, l_in );
953         in.y = FT_DivFix( in.y, l_in );
954       }
955 
956       for ( n = first; n <= last; n++ )
957       {
958         if ( n < last )
959           v_next = points[n + 1];
960         else
961           v_next = v_first;
962 
963         /* compute outgoing normalized vector */
964         out.x = v_next.x - v_cur.x;
965         out.y = v_next.y - v_cur.y;
966         l_out = FT_Vector_Length( &out );
967         if ( l_out )
968         {
969           out.x = FT_DivFix( out.x, l_out );
970           out.y = FT_DivFix( out.y, l_out );
971         }
972 
973         d = FT_MulFix( in.x, out.x ) + FT_MulFix( in.y, out.y );
974 
975         /* shift only if turn is less than ~160 degrees */
976         if ( d > -0xF000L )
977         {
978           d = d + 0x10000L;
979 
980           /* shift components are aligned along lateral bisector */
981           /* and directed according to the outline orientation.  */
982           shift.x = in.y + out.y;
983           shift.y = in.x + out.x;
984 
985           if ( orientation == FT_ORIENTATION_TRUETYPE )
986             shift.x = -shift.x;
987           else
988             shift.y = -shift.y;
989 
990           /* restrict shift magnitude to better handle collapsing segments */
991           q = FT_MulFix( out.x, in.y ) - FT_MulFix( out.y, in.x );
992           if ( orientation == FT_ORIENTATION_TRUETYPE )
993             q = -q;
994 
995           l = FT_MIN( l_in, l_out );
996 
997           /* non-strict inequalities avoid divide-by-zero when q == l == 0 */
998           if ( FT_MulFix( xstrength, q ) <= FT_MulFix( l, d ) )
999             shift.x = FT_MulDiv( shift.x, xstrength, d );
1000           else
1001             shift.x = FT_MulDiv( shift.x, l, q );
1002 
1003 
1004           if ( FT_MulFix( ystrength, q ) <= FT_MulFix( l, d ) )
1005             shift.y = FT_MulDiv( shift.y, ystrength, d );
1006           else
1007             shift.y = FT_MulDiv( shift.y, l, q );
1008         }
1009         else
1010           shift.x = shift.y = 0;
1011 
1012         outline->points[n].x = v_cur.x + xstrength + shift.x;
1013         outline->points[n].y = v_cur.y + ystrength + shift.y;
1014 
1015         in    = out;
1016         l_in  = l_out;
1017         v_cur = v_next;
1018       }
1019 
1020       first = last + 1;
1021     }
1022 
1023     return FT_Err_Ok;
1024   }
1025 
1026 
1027   /* documentation is in ftoutln.h */
1028 
1029   FT_EXPORT_DEF( FT_Orientation )
FT_Outline_Get_Orientation(FT_Outline * outline)1030   FT_Outline_Get_Orientation( FT_Outline*  outline )
1031   {
1032     FT_BBox     cbox;
1033     FT_Int      xshift, yshift;
1034     FT_Vector*  points;
1035     FT_Vector   v_prev, v_cur;
1036     FT_Int      c, n, first;
1037     FT_Pos      area = 0;
1038 
1039 
1040     if ( !outline || outline->n_points <= 0 )
1041       return FT_ORIENTATION_TRUETYPE;
1042 
1043     /* We use the nonzero winding rule to find the orientation.       */
1044     /* Since glyph outlines behave much more `regular' than arbitrary */
1045     /* cubic or quadratic curves, this test deals with the polygon    */
1046     /* only that is spanned up by the control points.                 */
1047 
1048     FT_Outline_Get_CBox( outline, &cbox );
1049 
1050     /* Handle collapsed outlines to avoid undefined FT_MSB. */
1051     if ( cbox.xMin == cbox.xMax || cbox.yMin == cbox.yMax )
1052       return FT_ORIENTATION_NONE;
1053 
1054     xshift = FT_MSB( (FT_UInt32)( FT_ABS( cbox.xMax ) |
1055                                   FT_ABS( cbox.xMin ) ) ) - 14;
1056     xshift = FT_MAX( xshift, 0 );
1057 
1058     yshift = FT_MSB( (FT_UInt32)( cbox.yMax - cbox.yMin ) ) - 14;
1059     yshift = FT_MAX( yshift, 0 );
1060 
1061     points = outline->points;
1062 
1063     first = 0;
1064     for ( c = 0; c < outline->n_contours; c++ )
1065     {
1066       FT_Int  last = outline->contours[c];
1067 
1068 
1069       v_prev = points[last];
1070 
1071       for ( n = first; n <= last; n++ )
1072       {
1073         v_cur = points[n];
1074         area += ( ( v_cur.y - v_prev.y ) >> yshift ) *
1075                 ( ( v_cur.x + v_prev.x ) >> xshift );
1076         v_prev = v_cur;
1077       }
1078 
1079       first = last + 1;
1080     }
1081 
1082     if ( area > 0 )
1083       return FT_ORIENTATION_POSTSCRIPT;
1084     else if ( area < 0 )
1085       return FT_ORIENTATION_TRUETYPE;
1086     else
1087       return FT_ORIENTATION_NONE;
1088   }
1089 
1090 
1091 /* END */
1092