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