1 /***************************************************************************/
5 /* FreeType path stroker (body). */
7 /* Copyright 2002-2006, 2008-2011, 2013, 2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
21 #include FT_TRIGONOMETRY_H
23 #include FT_INTERNAL_MEMORY_H
24 #include FT_INTERNAL_DEBUG_H
25 #include FT_INTERNAL_OBJECTS_H
28 /* documentation is in ftstroke.h */
30 FT_EXPORT_DEF( FT_StrokerBorder )
31 FT_Outline_GetInsideBorder( FT_Outline* outline )
33 FT_Orientation o = FT_Outline_Get_Orientation( outline );
36 return o == FT_ORIENTATION_TRUETYPE ? FT_STROKER_BORDER_RIGHT
37 : FT_STROKER_BORDER_LEFT;
41 /* documentation is in ftstroke.h */
43 FT_EXPORT_DEF( FT_StrokerBorder )
44 FT_Outline_GetOutsideBorder( FT_Outline* outline )
46 FT_Orientation o = FT_Outline_Get_Orientation( outline );
49 return o == FT_ORIENTATION_TRUETYPE ? FT_STROKER_BORDER_LEFT
50 : FT_STROKER_BORDER_RIGHT;
54 /*************************************************************************/
55 /*************************************************************************/
57 /***** BEZIER COMPUTATIONS *****/
59 /*************************************************************************/
60 /*************************************************************************/
62 #define FT_SMALL_CONIC_THRESHOLD ( FT_ANGLE_PI / 6 )
63 #define FT_SMALL_CUBIC_THRESHOLD ( FT_ANGLE_PI / 8 )
67 #define FT_IS_SMALL( x ) ( (x) > -FT_EPSILON && (x) < FT_EPSILON )
71 ft_pos_abs( FT_Pos x )
73 return x >= 0 ? x : -x;
78 ft_conic_split( FT_Vector* base )
83 base[4].x = base[2].x;
85 a = base[3].x = ( base[2].x + b ) / 2;
86 b = base[1].x = ( base[0].x + b ) / 2;
87 base[2].x = ( a + b ) / 2;
89 base[4].y = base[2].y;
91 a = base[3].y = ( base[2].y + b ) / 2;
92 b = base[1].y = ( base[0].y + b ) / 2;
93 base[2].y = ( a + b ) / 2;
98 ft_conic_is_small_enough( FT_Vector* base,
100 FT_Angle *angle_out )
104 FT_Int close1, close2;
107 d1.x = base[1].x - base[2].x;
108 d1.y = base[1].y - base[2].y;
109 d2.x = base[0].x - base[1].x;
110 d2.y = base[0].y - base[1].y;
112 close1 = FT_IS_SMALL( d1.x ) && FT_IS_SMALL( d1.y );
113 close2 = FT_IS_SMALL( d2.x ) && FT_IS_SMALL( d2.y );
119 /* basically a point; */
120 /* do nothing to retain original direction */
125 *angle_out = FT_Atan2( d2.x, d2.y );
133 *angle_out = FT_Atan2( d1.x, d1.y );
137 *angle_in = FT_Atan2( d1.x, d1.y );
138 *angle_out = FT_Atan2( d2.x, d2.y );
142 theta = ft_pos_abs( FT_Angle_Diff( *angle_in, *angle_out ) );
144 return FT_BOOL( theta < FT_SMALL_CONIC_THRESHOLD );
149 ft_cubic_split( FT_Vector* base )
154 base[6].x = base[3].x;
157 base[1].x = a = ( base[0].x + c ) / 2;
158 base[5].x = b = ( base[3].x + d ) / 2;
160 base[2].x = a = ( a + c ) / 2;
161 base[4].x = b = ( b + c ) / 2;
162 base[3].x = ( a + b ) / 2;
164 base[6].y = base[3].y;
167 base[1].y = a = ( base[0].y + c ) / 2;
168 base[5].y = b = ( base[3].y + d ) / 2;
170 base[2].y = a = ( a + c ) / 2;
171 base[4].y = b = ( b + c ) / 2;
172 base[3].y = ( a + b ) / 2;
176 /* Return the average of `angle1' and `angle2'. */
177 /* This gives correct result even if `angle1' and `angle2' */
178 /* have opposite signs. */
180 ft_angle_mean( FT_Angle angle1,
183 return angle1 + FT_Angle_Diff( angle1, angle2 ) / 2;
188 ft_cubic_is_small_enough( FT_Vector* base,
191 FT_Angle *angle_out )
193 FT_Vector d1, d2, d3;
194 FT_Angle theta1, theta2;
195 FT_Int close1, close2, close3;
198 d1.x = base[2].x - base[3].x;
199 d1.y = base[2].y - base[3].y;
200 d2.x = base[1].x - base[2].x;
201 d2.y = base[1].y - base[2].y;
202 d3.x = base[0].x - base[1].x;
203 d3.y = base[0].y - base[1].y;
205 close1 = FT_IS_SMALL( d1.x ) && FT_IS_SMALL( d1.y );
206 close2 = FT_IS_SMALL( d2.x ) && FT_IS_SMALL( d2.y );
207 close3 = FT_IS_SMALL( d3.x ) && FT_IS_SMALL( d3.y );
215 /* basically a point; */
216 /* do nothing to retain original direction */
222 *angle_out = FT_Atan2( d3.x, d3.y );
231 *angle_out = FT_Atan2( d2.x, d2.y );
236 *angle_mid = FT_Atan2( d2.x, d2.y );
237 *angle_out = FT_Atan2( d3.x, d3.y );
249 *angle_out = FT_Atan2( d1.x, d1.y );
253 *angle_in = FT_Atan2( d1.x, d1.y );
254 *angle_out = FT_Atan2( d3.x, d3.y );
255 *angle_mid = ft_angle_mean( *angle_in, *angle_out );
262 *angle_in = FT_Atan2( d1.x, d1.y );
264 *angle_out = FT_Atan2( d2.x, d2.y );
268 *angle_in = FT_Atan2( d1.x, d1.y );
269 *angle_mid = FT_Atan2( d2.x, d2.y );
270 *angle_out = FT_Atan2( d3.x, d3.y );
275 theta1 = ft_pos_abs( FT_Angle_Diff( *angle_in, *angle_mid ) );
276 theta2 = ft_pos_abs( FT_Angle_Diff( *angle_mid, *angle_out ) );
278 return FT_BOOL( theta1 < FT_SMALL_CUBIC_THRESHOLD &&
279 theta2 < FT_SMALL_CUBIC_THRESHOLD );
283 /*************************************************************************/
284 /*************************************************************************/
286 /***** STROKE BORDERS *****/
288 /*************************************************************************/
289 /*************************************************************************/
291 typedef enum FT_StrokeTags_
293 FT_STROKE_TAG_ON = 1, /* on-curve point */
294 FT_STROKE_TAG_CUBIC = 2, /* cubic off-point */
295 FT_STROKE_TAG_BEGIN = 4, /* sub-path start */
296 FT_STROKE_TAG_END = 8 /* sub-path end */
300 #define FT_STROKE_TAG_BEGIN_END ( FT_STROKE_TAG_BEGIN | FT_STROKE_TAG_END )
302 typedef struct FT_StrokeBorderRec_
308 FT_Bool movable; /* TRUE for ends of lineto borders */
309 FT_Int start; /* index of current sub-path start point */
313 } FT_StrokeBorderRec, *FT_StrokeBorder;
317 ft_stroke_border_grow( FT_StrokeBorder border,
320 FT_UInt old_max = border->max_points;
321 FT_UInt new_max = border->num_points + new_points;
322 FT_Error error = FT_Err_Ok;
325 if ( new_max > old_max )
327 FT_UInt cur_max = old_max;
328 FT_Memory memory = border->memory;
331 while ( cur_max < new_max )
332 cur_max += ( cur_max >> 1 ) + 16;
334 if ( FT_RENEW_ARRAY( border->points, old_max, cur_max ) ||
335 FT_RENEW_ARRAY( border->tags, old_max, cur_max ) )
338 border->max_points = cur_max;
347 ft_stroke_border_close( FT_StrokeBorder border,
350 FT_UInt start = border->start;
351 FT_UInt count = border->num_points;
354 FT_ASSERT( border->start >= 0 );
356 /* don't record empty paths! */
357 if ( count <= start + 1U )
358 border->num_points = start;
361 /* copy the last point to the start of this sub-path, since */
362 /* it contains the `adjusted' starting coordinates */
363 border->num_points = --count;
364 border->points[start] = border->points[count];
368 /* reverse the points */
370 FT_Vector* vec1 = border->points + start + 1;
371 FT_Vector* vec2 = border->points + count - 1;
374 for ( ; vec1 < vec2; vec1++, vec2-- )
387 FT_Byte* tag1 = border->tags + start + 1;
388 FT_Byte* tag2 = border->tags + count - 1;
391 for ( ; tag1 < tag2; tag1++, tag2-- )
403 border->tags[start ] |= FT_STROKE_TAG_BEGIN;
404 border->tags[count - 1] |= FT_STROKE_TAG_END;
408 border->movable = FALSE;
413 ft_stroke_border_lineto( FT_StrokeBorder border,
417 FT_Error error = FT_Err_Ok;
420 FT_ASSERT( border->start >= 0 );
422 if ( border->movable )
424 /* move last point */
425 border->points[border->num_points - 1] = *to;
429 /* don't add zero-length lineto */
430 if ( border->num_points > 0 &&
431 FT_IS_SMALL( border->points[border->num_points - 1].x - to->x ) &&
432 FT_IS_SMALL( border->points[border->num_points - 1].y - to->y ) )
436 error = ft_stroke_border_grow( border, 1 );
439 FT_Vector* vec = border->points + border->num_points;
440 FT_Byte* tag = border->tags + border->num_points;
444 tag[0] = FT_STROKE_TAG_ON;
446 border->num_points += 1;
449 border->movable = movable;
455 ft_stroke_border_conicto( FT_StrokeBorder border,
462 FT_ASSERT( border->start >= 0 );
464 error = ft_stroke_border_grow( border, 2 );
467 FT_Vector* vec = border->points + border->num_points;
468 FT_Byte* tag = border->tags + border->num_points;
475 tag[1] = FT_STROKE_TAG_ON;
477 border->num_points += 2;
480 border->movable = FALSE;
487 ft_stroke_border_cubicto( FT_StrokeBorder border,
495 FT_ASSERT( border->start >= 0 );
497 error = ft_stroke_border_grow( border, 3 );
500 FT_Vector* vec = border->points + border->num_points;
501 FT_Byte* tag = border->tags + border->num_points;
508 tag[0] = FT_STROKE_TAG_CUBIC;
509 tag[1] = FT_STROKE_TAG_CUBIC;
510 tag[2] = FT_STROKE_TAG_ON;
512 border->num_points += 3;
515 border->movable = FALSE;
521 #define FT_ARC_CUBIC_ANGLE ( FT_ANGLE_PI / 2 )
525 ft_stroke_border_arcto( FT_StrokeBorder border,
528 FT_Angle angle_start,
529 FT_Angle angle_diff )
531 FT_Angle total, angle, step, rotate, next, theta;
532 FT_Vector a, b, a2, b2;
534 FT_Error error = FT_Err_Ok;
537 /* compute start point */
538 FT_Vector_From_Polar( &a, radius, angle_start );
544 rotate = ( angle_diff >= 0 ) ? FT_ANGLE_PI2 : -FT_ANGLE_PI2;
549 if ( step > FT_ARC_CUBIC_ANGLE )
550 step = FT_ARC_CUBIC_ANGLE;
552 else if ( step < -FT_ARC_CUBIC_ANGLE )
553 step = -FT_ARC_CUBIC_ANGLE;
562 /* compute end point */
563 FT_Vector_From_Polar( &b, radius, next );
567 /* compute first and second control points */
568 length = FT_MulDiv( radius, FT_Sin( theta ) * 4,
569 ( 0x10000L + FT_Cos( theta ) ) * 3 );
571 FT_Vector_From_Polar( &a2, length, angle + rotate );
575 FT_Vector_From_Polar( &b2, length, next - rotate );
580 error = ft_stroke_border_cubicto( border, &a2, &b2, &b );
584 /* process the rest of the arc ?? */
595 ft_stroke_border_moveto( FT_StrokeBorder border,
598 /* close current open path if any ? */
599 if ( border->start >= 0 )
600 ft_stroke_border_close( border, FALSE );
602 border->start = border->num_points;
603 border->movable = FALSE;
605 return ft_stroke_border_lineto( border, to, FALSE );
610 ft_stroke_border_init( FT_StrokeBorder border,
613 border->memory = memory;
614 border->points = NULL;
617 border->num_points = 0;
618 border->max_points = 0;
620 border->valid = FALSE;
625 ft_stroke_border_reset( FT_StrokeBorder border )
627 border->num_points = 0;
629 border->valid = FALSE;
634 ft_stroke_border_done( FT_StrokeBorder border )
636 FT_Memory memory = border->memory;
639 FT_FREE( border->points );
640 FT_FREE( border->tags );
642 border->num_points = 0;
643 border->max_points = 0;
645 border->valid = FALSE;
650 ft_stroke_border_get_counts( FT_StrokeBorder border,
651 FT_UInt *anum_points,
652 FT_UInt *anum_contours )
654 FT_Error error = FT_Err_Ok;
655 FT_UInt num_points = 0;
656 FT_UInt num_contours = 0;
658 FT_UInt count = border->num_points;
659 FT_Vector* point = border->points;
660 FT_Byte* tags = border->tags;
661 FT_Int in_contour = 0;
664 for ( ; count > 0; count--, num_points++, point++, tags++ )
666 if ( tags[0] & FT_STROKE_TAG_BEGIN )
668 if ( in_contour != 0 )
673 else if ( in_contour == 0 )
676 if ( tags[0] & FT_STROKE_TAG_END )
683 if ( in_contour != 0 )
686 border->valid = TRUE;
689 *anum_points = num_points;
690 *anum_contours = num_contours;
701 ft_stroke_border_export( FT_StrokeBorder border,
702 FT_Outline* outline )
704 /* copy point locations */
705 FT_ARRAY_COPY( outline->points + outline->n_points,
707 border->num_points );
711 FT_UInt count = border->num_points;
712 FT_Byte* read = border->tags;
713 FT_Byte* write = (FT_Byte*)outline->tags + outline->n_points;
716 for ( ; count > 0; count--, read++, write++ )
718 if ( *read & FT_STROKE_TAG_ON )
719 *write = FT_CURVE_TAG_ON;
720 else if ( *read & FT_STROKE_TAG_CUBIC )
721 *write = FT_CURVE_TAG_CUBIC;
723 *write = FT_CURVE_TAG_CONIC;
729 FT_UInt count = border->num_points;
730 FT_Byte* tags = border->tags;
731 FT_Short* write = outline->contours + outline->n_contours;
732 FT_Short idx = (FT_Short)outline->n_points;
735 for ( ; count > 0; count--, tags++, idx++ )
737 if ( *tags & FT_STROKE_TAG_END )
740 outline->n_contours++;
745 outline->n_points = (short)( outline->n_points + border->num_points );
747 FT_ASSERT( FT_Outline_Check( outline ) == 0 );
751 /*************************************************************************/
752 /*************************************************************************/
754 /***** STROKER *****/
756 /*************************************************************************/
757 /*************************************************************************/
759 #define FT_SIDE_TO_ROTATE( s ) ( FT_ANGLE_PI2 - (s) * FT_ANGLE_PI )
761 typedef struct FT_StrokerRec_
763 FT_Angle angle_in; /* direction into curr join */
764 FT_Angle angle_out; /* direction out of join */
765 FT_Vector center; /* current position */
766 FT_Fixed line_length; /* length of last lineto */
767 FT_Bool first_point; /* is this the start? */
768 FT_Bool subpath_open; /* is the subpath open? */
769 FT_Angle subpath_angle; /* subpath start direction */
770 FT_Vector subpath_start; /* subpath start position */
771 FT_Fixed subpath_line_length; /* subpath start lineto len */
772 FT_Bool handle_wide_strokes; /* use wide strokes logic? */
774 FT_Stroker_LineCap line_cap;
775 FT_Stroker_LineJoin line_join;
776 FT_Stroker_LineJoin line_join_saved;
777 FT_Fixed miter_limit;
780 FT_StrokeBorderRec borders[2];
786 /* documentation is in ftstroke.h */
788 FT_EXPORT_DEF( FT_Error )
789 FT_Stroker_New( FT_Library library,
790 FT_Stroker *astroker )
792 FT_Error error; /* assigned in FT_NEW */
794 FT_Stroker stroker = NULL;
798 return FT_THROW( Invalid_Library_Handle );
801 return FT_THROW( Invalid_Argument );
803 memory = library->memory;
805 if ( !FT_NEW( stroker ) )
807 stroker->library = library;
809 ft_stroke_border_init( &stroker->borders[0], memory );
810 ft_stroke_border_init( &stroker->borders[1], memory );
819 /* documentation is in ftstroke.h */
821 FT_EXPORT_DEF( void )
822 FT_Stroker_Set( FT_Stroker stroker,
824 FT_Stroker_LineCap line_cap,
825 FT_Stroker_LineJoin line_join,
826 FT_Fixed miter_limit )
831 stroker->radius = radius;
832 stroker->line_cap = line_cap;
833 stroker->line_join = line_join;
834 stroker->miter_limit = miter_limit;
836 /* ensure miter limit has sensible value */
837 if ( stroker->miter_limit < 0x10000L )
838 stroker->miter_limit = 0x10000L;
840 /* save line join style: */
841 /* line join style can be temporarily changed when stroking curves */
842 stroker->line_join_saved = line_join;
844 FT_Stroker_Rewind( stroker );
848 /* documentation is in ftstroke.h */
850 FT_EXPORT_DEF( void )
851 FT_Stroker_Rewind( FT_Stroker stroker )
855 ft_stroke_border_reset( &stroker->borders[0] );
856 ft_stroke_border_reset( &stroker->borders[1] );
861 /* documentation is in ftstroke.h */
863 FT_EXPORT_DEF( void )
864 FT_Stroker_Done( FT_Stroker stroker )
868 FT_Memory memory = stroker->library->memory;
871 ft_stroke_border_done( &stroker->borders[0] );
872 ft_stroke_border_done( &stroker->borders[1] );
874 stroker->library = NULL;
880 /* create a circular arc at a corner or cap */
882 ft_stroker_arcto( FT_Stroker stroker,
885 FT_Angle total, rotate;
886 FT_Fixed radius = stroker->radius;
887 FT_Error error = FT_Err_Ok;
888 FT_StrokeBorder border = stroker->borders + side;
891 rotate = FT_SIDE_TO_ROTATE( side );
893 total = FT_Angle_Diff( stroker->angle_in, stroker->angle_out );
894 if ( total == FT_ANGLE_PI )
897 error = ft_stroke_border_arcto( border,
900 stroker->angle_in + rotate,
902 border->movable = FALSE;
907 /* add a cap at the end of an opened path */
909 ft_stroker_cap( FT_Stroker stroker,
913 FT_Error error = FT_Err_Ok;
916 if ( stroker->line_cap == FT_STROKER_LINECAP_ROUND )
918 /* add a round cap */
919 stroker->angle_in = angle;
920 stroker->angle_out = angle + FT_ANGLE_PI;
922 error = ft_stroker_arcto( stroker, side );
924 else if ( stroker->line_cap == FT_STROKER_LINECAP_SQUARE )
926 /* add a square cap */
927 FT_Vector delta, delta2;
928 FT_Angle rotate = FT_SIDE_TO_ROTATE( side );
929 FT_Fixed radius = stroker->radius;
930 FT_StrokeBorder border = stroker->borders + side;
933 FT_Vector_From_Polar( &delta2, radius, angle + rotate );
934 FT_Vector_From_Polar( &delta, radius, angle );
936 delta.x += stroker->center.x + delta2.x;
937 delta.y += stroker->center.y + delta2.y;
939 error = ft_stroke_border_lineto( border, &delta, FALSE );
943 FT_Vector_From_Polar( &delta2, radius, angle - rotate );
944 FT_Vector_From_Polar( &delta, radius, angle );
946 delta.x += delta2.x + stroker->center.x;
947 delta.y += delta2.y + stroker->center.y;
949 error = ft_stroke_border_lineto( border, &delta, FALSE );
951 else if ( stroker->line_cap == FT_STROKER_LINECAP_BUTT )
953 /* add a butt ending */
955 FT_Angle rotate = FT_SIDE_TO_ROTATE( side );
956 FT_Fixed radius = stroker->radius;
957 FT_StrokeBorder border = stroker->borders + side;
960 FT_Vector_From_Polar( &delta, radius, angle + rotate );
962 delta.x += stroker->center.x;
963 delta.y += stroker->center.y;
965 error = ft_stroke_border_lineto( border, &delta, FALSE );
969 FT_Vector_From_Polar( &delta, radius, angle - rotate );
971 delta.x += stroker->center.x;
972 delta.y += stroker->center.y;
974 error = ft_stroke_border_lineto( border, &delta, FALSE );
982 /* process an inside corner, i.e. compute intersection */
984 ft_stroker_inside( FT_Stroker stroker,
986 FT_Fixed line_length )
988 FT_StrokeBorder border = stroker->borders + side;
989 FT_Angle phi, theta, rotate;
990 FT_Fixed length, thcos;
992 FT_Error error = FT_Err_Ok;
993 FT_Bool intersect; /* use intersection of lines? */
996 rotate = FT_SIDE_TO_ROTATE( side );
998 theta = FT_Angle_Diff( stroker->angle_in, stroker->angle_out ) / 2;
1000 /* Only intersect borders if between two lineto's and both */
1001 /* lines are long enough (line_length is zero for curves). */
1002 if ( !border->movable || line_length == 0 )
1006 /* compute minimum required length of lines */
1007 FT_Fixed min_length = ft_pos_abs( FT_MulFix( stroker->radius,
1008 FT_Tan( theta ) ) );
1011 intersect = FT_BOOL( min_length &&
1012 stroker->line_length >= min_length &&
1013 line_length >= min_length );
1018 FT_Vector_From_Polar( &delta, stroker->radius,
1019 stroker->angle_out + rotate );
1020 delta.x += stroker->center.x;
1021 delta.y += stroker->center.y;
1023 border->movable = FALSE;
1027 /* compute median angle */
1028 phi = stroker->angle_in + theta;
1030 thcos = FT_Cos( theta );
1032 length = FT_DivFix( stroker->radius, thcos );
1034 FT_Vector_From_Polar( &delta, length, phi + rotate );
1035 delta.x += stroker->center.x;
1036 delta.y += stroker->center.y;
1039 error = ft_stroke_border_lineto( border, &delta, FALSE );
1045 /* process an outside corner, i.e. compute bevel/miter/round */
1047 ft_stroker_outside( FT_Stroker stroker,
1049 FT_Fixed line_length )
1051 FT_StrokeBorder border = stroker->borders + side;
1056 if ( stroker->line_join == FT_STROKER_LINEJOIN_ROUND )
1057 error = ft_stroker_arcto( stroker, side );
1060 /* this is a mitered (pointed) or beveled (truncated) corner */
1061 FT_Fixed sigma = 0, radius = stroker->radius;
1062 FT_Angle theta = 0, phi = 0;
1064 FT_Bool bevel, fixed_bevel;
1067 rotate = FT_SIDE_TO_ROTATE( side );
1070 FT_BOOL( stroker->line_join == FT_STROKER_LINEJOIN_BEVEL );
1073 FT_BOOL( stroker->line_join != FT_STROKER_LINEJOIN_MITER_VARIABLE );
1077 theta = FT_Angle_Diff( stroker->angle_in, stroker->angle_out );
1079 if ( theta == FT_ANGLE_PI )
1082 phi = stroker->angle_in;
1087 phi = stroker->angle_in + theta + rotate;
1090 thcos = FT_Cos( theta );
1091 sigma = FT_MulFix( stroker->miter_limit, thcos );
1093 /* is miter limit exceeded? */
1094 if ( sigma < 0x10000L )
1096 /* don't create variable bevels for very small deviations; */
1097 /* FT_Sin(x) = 0 for x <= 57 */
1098 if ( fixed_bevel || ft_pos_abs( theta ) > 57 )
1103 if ( bevel ) /* this is a bevel (broken angle) */
1107 /* the outer corners are simply joined together */
1112 FT_Vector_From_Polar( &delta,
1114 stroker->angle_out + rotate );
1115 delta.x += stroker->center.x;
1116 delta.y += stroker->center.y;
1118 border->movable = FALSE;
1119 error = ft_stroke_border_lineto( border, &delta, FALSE );
1121 else /* variable bevel */
1123 /* the miter is truncated */
1124 FT_Vector middle, delta;
1128 /* compute middle point */
1129 FT_Vector_From_Polar( &middle,
1130 FT_MulFix( radius, stroker->miter_limit ),
1132 middle.x += stroker->center.x;
1133 middle.y += stroker->center.y;
1135 /* compute first angle point */
1136 length = FT_MulDiv( radius, 0x10000L - sigma,
1137 ft_pos_abs( FT_Sin( theta ) ) );
1139 FT_Vector_From_Polar( &delta, length, phi + rotate );
1140 delta.x += middle.x;
1141 delta.y += middle.y;
1143 error = ft_stroke_border_lineto( border, &delta, FALSE );
1147 /* compute second angle point */
1148 FT_Vector_From_Polar( &delta, length, phi - rotate );
1149 delta.x += middle.x;
1150 delta.y += middle.y;
1152 error = ft_stroke_border_lineto( border, &delta, FALSE );
1156 /* finally, add an end point; only needed if not lineto */
1157 /* (line_length is zero for curves) */
1158 if ( line_length == 0 )
1160 FT_Vector_From_Polar( &delta,
1162 stroker->angle_out + rotate );
1164 delta.x += stroker->center.x;
1165 delta.y += stroker->center.y;
1167 error = ft_stroke_border_lineto( border, &delta, FALSE );
1171 else /* this is a miter (intersection) */
1177 length = FT_DivFix( stroker->radius, thcos );
1179 FT_Vector_From_Polar( &delta, length, phi );
1180 delta.x += stroker->center.x;
1181 delta.y += stroker->center.y;
1183 error = ft_stroke_border_lineto( border, &delta, FALSE );
1187 /* now add an end point; only needed if not lineto */
1188 /* (line_length is zero for curves) */
1189 if ( line_length == 0 )
1191 FT_Vector_From_Polar( &delta,
1193 stroker->angle_out + rotate );
1194 delta.x += stroker->center.x;
1195 delta.y += stroker->center.y;
1197 error = ft_stroke_border_lineto( border, &delta, FALSE );
1208 ft_stroker_process_corner( FT_Stroker stroker,
1209 FT_Fixed line_length )
1211 FT_Error error = FT_Err_Ok;
1216 turn = FT_Angle_Diff( stroker->angle_in, stroker->angle_out );
1218 /* no specific corner processing is required if the turn is 0 */
1222 /* when we turn to the right, the inside side is 0 */
1225 /* otherwise, the inside side is 1 */
1229 /* process the inside side */
1230 error = ft_stroker_inside( stroker, inside_side, line_length );
1234 /* process the outside side */
1235 error = ft_stroker_outside( stroker, 1 - inside_side, line_length );
1242 /* add two points to the left and right borders corresponding to the */
1243 /* start of the subpath */
1245 ft_stroker_subpath_start( FT_Stroker stroker,
1246 FT_Angle start_angle,
1247 FT_Fixed line_length )
1252 FT_StrokeBorder border;
1255 FT_Vector_From_Polar( &delta, stroker->radius,
1256 start_angle + FT_ANGLE_PI2 );
1258 point.x = stroker->center.x + delta.x;
1259 point.y = stroker->center.y + delta.y;
1261 border = stroker->borders;
1262 error = ft_stroke_border_moveto( border, &point );
1266 point.x = stroker->center.x - delta.x;
1267 point.y = stroker->center.y - delta.y;
1270 error = ft_stroke_border_moveto( border, &point );
1272 /* save angle, position, and line length for last join */
1273 /* (line_length is zero for curves) */
1274 stroker->subpath_angle = start_angle;
1275 stroker->first_point = FALSE;
1276 stroker->subpath_line_length = line_length;
1283 /* documentation is in ftstroke.h */
1285 FT_EXPORT_DEF( FT_Error )
1286 FT_Stroker_LineTo( FT_Stroker stroker,
1289 FT_Error error = FT_Err_Ok;
1290 FT_StrokeBorder border;
1294 FT_Fixed line_length;
1297 if ( !stroker || !to )
1298 return FT_THROW( Invalid_Argument );
1300 delta.x = to->x - stroker->center.x;
1301 delta.y = to->y - stroker->center.y;
1303 /* a zero-length lineto is a no-op; avoid creating a spurious corner */
1304 if ( delta.x == 0 && delta.y == 0 )
1307 /* compute length of line */
1308 line_length = FT_Vector_Length( &delta );
1310 angle = FT_Atan2( delta.x, delta.y );
1311 FT_Vector_From_Polar( &delta, stroker->radius, angle + FT_ANGLE_PI2 );
1313 /* process corner if necessary */
1314 if ( stroker->first_point )
1316 /* This is the first segment of a subpath. We need to */
1317 /* add a point to each border at their respective starting */
1318 /* point locations. */
1319 error = ft_stroker_subpath_start( stroker, angle, line_length );
1325 /* process the current corner */
1326 stroker->angle_out = angle;
1327 error = ft_stroker_process_corner( stroker, line_length );
1332 /* now add a line segment to both the `inside' and `outside' paths */
1333 for ( border = stroker->borders, side = 1; side >= 0; side--, border++ )
1338 point.x = to->x + delta.x;
1339 point.y = to->y + delta.y;
1341 /* the ends of lineto borders are movable */
1342 error = ft_stroke_border_lineto( border, &point, TRUE );
1350 stroker->angle_in = angle;
1351 stroker->center = *to;
1352 stroker->line_length = line_length;
1359 /* documentation is in ftstroke.h */
1361 FT_EXPORT_DEF( FT_Error )
1362 FT_Stroker_ConicTo( FT_Stroker stroker,
1366 FT_Error error = FT_Err_Ok;
1367 FT_Vector bez_stack[34];
1369 FT_Vector* limit = bez_stack + 30;
1370 FT_Bool first_arc = TRUE;
1373 if ( !stroker || !control || !to )
1375 error = FT_THROW( Invalid_Argument );
1379 /* if all control points are coincident, this is a no-op; */
1380 /* avoid creating a spurious corner */
1381 if ( FT_IS_SMALL( stroker->center.x - control->x ) &&
1382 FT_IS_SMALL( stroker->center.y - control->y ) &&
1383 FT_IS_SMALL( control->x - to->x ) &&
1384 FT_IS_SMALL( control->y - to->y ) )
1386 stroker->center = *to;
1393 arc[2] = stroker->center;
1395 while ( arc >= bez_stack )
1397 FT_Angle angle_in, angle_out;
1400 /* initialize with current direction */
1401 angle_in = angle_out = stroker->angle_in;
1404 !ft_conic_is_small_enough( arc, &angle_in, &angle_out ) )
1406 if ( stroker->first_point )
1407 stroker->angle_in = angle_in;
1409 ft_conic_split( arc );
1418 /* process corner if necessary */
1419 if ( stroker->first_point )
1420 error = ft_stroker_subpath_start( stroker, angle_in, 0 );
1423 stroker->angle_out = angle_in;
1424 error = ft_stroker_process_corner( stroker, 0 );
1427 else if ( ft_pos_abs( FT_Angle_Diff( stroker->angle_in, angle_in ) ) >
1428 FT_SMALL_CONIC_THRESHOLD / 4 )
1430 /* if the deviation from one arc to the next is too great, */
1431 /* add a round corner */
1432 stroker->center = arc[2];
1433 stroker->angle_out = angle_in;
1434 stroker->line_join = FT_STROKER_LINEJOIN_ROUND;
1436 error = ft_stroker_process_corner( stroker, 0 );
1438 /* reinstate line join style */
1439 stroker->line_join = stroker->line_join_saved;
1445 /* the arc's angle is small enough; we can add it directly to each */
1448 FT_Vector ctrl, end;
1449 FT_Angle theta, phi, rotate, alpha0 = 0;
1451 FT_StrokeBorder border;
1455 theta = FT_Angle_Diff( angle_in, angle_out ) / 2;
1456 phi = angle_in + theta;
1457 length = FT_DivFix( stroker->radius, FT_Cos( theta ) );
1459 /* compute direction of original arc */
1460 if ( stroker->handle_wide_strokes )
1461 alpha0 = FT_Atan2( arc[0].x - arc[2].x, arc[0].y - arc[2].y );
1463 for ( border = stroker->borders, side = 0;
1467 rotate = FT_SIDE_TO_ROTATE( side );
1469 /* compute control point */
1470 FT_Vector_From_Polar( &ctrl, length, phi + rotate );
1474 /* compute end point */
1475 FT_Vector_From_Polar( &end, stroker->radius, angle_out + rotate );
1479 if ( stroker->handle_wide_strokes )
1485 /* determine whether the border radius is greater than the */
1486 /* radius of curvature of the original arc */
1487 start = border->points[border->num_points - 1];
1489 alpha1 = FT_Atan2( end.x - start.x, end.y - start.y );
1491 /* is the direction of the border arc opposite to */
1492 /* that of the original arc? */
1493 if ( ft_pos_abs( FT_Angle_Diff( alpha0, alpha1 ) ) >
1496 FT_Angle beta, gamma;
1497 FT_Vector bvec, delta;
1498 FT_Fixed blen, sinA, sinB, alen;
1501 /* use the sine rule to find the intersection point */
1502 beta = FT_Atan2( arc[2].x - start.x, arc[2].y - start.y );
1503 gamma = FT_Atan2( arc[0].x - end.x, arc[0].y - end.y );
1505 bvec.x = end.x - start.x;
1506 bvec.y = end.y - start.y;
1508 blen = FT_Vector_Length( &bvec );
1510 sinA = ft_pos_abs( FT_Sin( alpha1 - gamma ) );
1511 sinB = ft_pos_abs( FT_Sin( beta - gamma ) );
1513 alen = FT_MulDiv( blen, sinA, sinB );
1515 FT_Vector_From_Polar( &delta, alen, beta );
1519 /* circumnavigate the negative sector backwards */
1520 border->movable = FALSE;
1521 error = ft_stroke_border_lineto( border, &delta, FALSE );
1524 error = ft_stroke_border_lineto( border, &end, FALSE );
1527 error = ft_stroke_border_conicto( border, &ctrl, &start );
1530 /* and then move to the endpoint */
1531 error = ft_stroke_border_lineto( border, &end, FALSE );
1538 /* else fall through */
1541 /* simply add an arc */
1542 error = ft_stroke_border_conicto( border, &ctrl, &end );
1550 stroker->angle_in = angle_out;
1553 stroker->center = *to;
1560 /* documentation is in ftstroke.h */
1562 FT_EXPORT_DEF( FT_Error )
1563 FT_Stroker_CubicTo( FT_Stroker stroker,
1564 FT_Vector* control1,
1565 FT_Vector* control2,
1568 FT_Error error = FT_Err_Ok;
1569 FT_Vector bez_stack[37];
1571 FT_Vector* limit = bez_stack + 32;
1572 FT_Bool first_arc = TRUE;
1575 if ( !stroker || !control1 || !control2 || !to )
1577 error = FT_THROW( Invalid_Argument );
1581 /* if all control points are coincident, this is a no-op; */
1582 /* avoid creating a spurious corner */
1583 if ( FT_IS_SMALL( stroker->center.x - control1->x ) &&
1584 FT_IS_SMALL( stroker->center.y - control1->y ) &&
1585 FT_IS_SMALL( control1->x - control2->x ) &&
1586 FT_IS_SMALL( control1->y - control2->y ) &&
1587 FT_IS_SMALL( control2->x - to->x ) &&
1588 FT_IS_SMALL( control2->y - to->y ) )
1590 stroker->center = *to;
1598 arc[3] = stroker->center;
1600 while ( arc >= bez_stack )
1602 FT_Angle angle_in, angle_mid, angle_out;
1605 /* initialize with current direction */
1606 angle_in = angle_out = angle_mid = stroker->angle_in;
1609 !ft_cubic_is_small_enough( arc, &angle_in,
1610 &angle_mid, &angle_out ) )
1612 if ( stroker->first_point )
1613 stroker->angle_in = angle_in;
1615 ft_cubic_split( arc );
1624 /* process corner if necessary */
1625 if ( stroker->first_point )
1626 error = ft_stroker_subpath_start( stroker, angle_in, 0 );
1629 stroker->angle_out = angle_in;
1630 error = ft_stroker_process_corner( stroker, 0 );
1633 else if ( ft_pos_abs( FT_Angle_Diff( stroker->angle_in, angle_in ) ) >
1634 FT_SMALL_CUBIC_THRESHOLD / 4 )
1636 /* if the deviation from one arc to the next is too great, */
1637 /* add a round corner */
1638 stroker->center = arc[3];
1639 stroker->angle_out = angle_in;
1640 stroker->line_join = FT_STROKER_LINEJOIN_ROUND;
1642 error = ft_stroker_process_corner( stroker, 0 );
1644 /* reinstate line join style */
1645 stroker->line_join = stroker->line_join_saved;
1651 /* the arc's angle is small enough; we can add it directly to each */
1654 FT_Vector ctrl1, ctrl2, end;
1655 FT_Angle theta1, phi1, theta2, phi2, rotate, alpha0 = 0;
1656 FT_Fixed length1, length2;
1657 FT_StrokeBorder border;
1661 theta1 = FT_Angle_Diff( angle_in, angle_mid ) / 2;
1662 theta2 = FT_Angle_Diff( angle_mid, angle_out ) / 2;
1663 phi1 = ft_angle_mean( angle_in, angle_mid );
1664 phi2 = ft_angle_mean( angle_mid, angle_out );
1665 length1 = FT_DivFix( stroker->radius, FT_Cos( theta1 ) );
1666 length2 = FT_DivFix( stroker->radius, FT_Cos( theta2 ) );
1668 /* compute direction of original arc */
1669 if ( stroker->handle_wide_strokes )
1670 alpha0 = FT_Atan2( arc[0].x - arc[3].x, arc[0].y - arc[3].y );
1672 for ( border = stroker->borders, side = 0;
1676 rotate = FT_SIDE_TO_ROTATE( side );
1678 /* compute control points */
1679 FT_Vector_From_Polar( &ctrl1, length1, phi1 + rotate );
1680 ctrl1.x += arc[2].x;
1681 ctrl1.y += arc[2].y;
1683 FT_Vector_From_Polar( &ctrl2, length2, phi2 + rotate );
1684 ctrl2.x += arc[1].x;
1685 ctrl2.y += arc[1].y;
1687 /* compute end point */
1688 FT_Vector_From_Polar( &end, stroker->radius, angle_out + rotate );
1692 if ( stroker->handle_wide_strokes )
1698 /* determine whether the border radius is greater than the */
1699 /* radius of curvature of the original arc */
1700 start = border->points[border->num_points - 1];
1702 alpha1 = FT_Atan2( end.x - start.x, end.y - start.y );
1704 /* is the direction of the border arc opposite to */
1705 /* that of the original arc? */
1706 if ( ft_pos_abs( FT_Angle_Diff( alpha0, alpha1 ) ) >
1709 FT_Angle beta, gamma;
1710 FT_Vector bvec, delta;
1711 FT_Fixed blen, sinA, sinB, alen;
1714 /* use the sine rule to find the intersection point */
1715 beta = FT_Atan2( arc[3].x - start.x, arc[3].y - start.y );
1716 gamma = FT_Atan2( arc[0].x - end.x, arc[0].y - end.y );
1718 bvec.x = end.x - start.x;
1719 bvec.y = end.y - start.y;
1721 blen = FT_Vector_Length( &bvec );
1723 sinA = ft_pos_abs( FT_Sin( alpha1 - gamma ) );
1724 sinB = ft_pos_abs( FT_Sin( beta - gamma ) );
1726 alen = FT_MulDiv( blen, sinA, sinB );
1728 FT_Vector_From_Polar( &delta, alen, beta );
1732 /* circumnavigate the negative sector backwards */
1733 border->movable = FALSE;
1734 error = ft_stroke_border_lineto( border, &delta, FALSE );
1737 error = ft_stroke_border_lineto( border, &end, FALSE );
1740 error = ft_stroke_border_cubicto( border,
1746 /* and then move to the endpoint */
1747 error = ft_stroke_border_lineto( border, &end, FALSE );
1754 /* else fall through */
1757 /* simply add an arc */
1758 error = ft_stroke_border_cubicto( border, &ctrl1, &ctrl2, &end );
1766 stroker->angle_in = angle_out;
1769 stroker->center = *to;
1776 /* documentation is in ftstroke.h */
1778 FT_EXPORT_DEF( FT_Error )
1779 FT_Stroker_BeginSubPath( FT_Stroker stroker,
1783 if ( !stroker || !to )
1784 return FT_THROW( Invalid_Argument );
1786 /* We cannot process the first point, because there is not enough */
1787 /* information regarding its corner/cap. The latter will be processed */
1788 /* in the `FT_Stroker_EndSubPath' routine. */
1790 stroker->first_point = TRUE;
1791 stroker->center = *to;
1792 stroker->subpath_open = open;
1794 /* Determine if we need to check whether the border radius is greater */
1795 /* than the radius of curvature of a curve, to handle this case */
1796 /* specially. This is only required if bevel joins or butt caps may */
1797 /* be created, because round & miter joins and round & square caps */
1798 /* cover the negative sector created with wide strokes. */
1799 stroker->handle_wide_strokes =
1800 FT_BOOL( stroker->line_join != FT_STROKER_LINEJOIN_ROUND ||
1801 ( stroker->subpath_open &&
1802 stroker->line_cap == FT_STROKER_LINECAP_BUTT ) );
1804 /* record the subpath start point for each border */
1805 stroker->subpath_start = *to;
1807 stroker->angle_in = 0;
1814 ft_stroker_add_reverse_left( FT_Stroker stroker,
1817 FT_StrokeBorder right = stroker->borders + 0;
1818 FT_StrokeBorder left = stroker->borders + 1;
1820 FT_Error error = FT_Err_Ok;
1823 FT_ASSERT( left->start >= 0 );
1825 new_points = left->num_points - left->start;
1826 if ( new_points > 0 )
1828 error = ft_stroke_border_grow( right, (FT_UInt)new_points );
1833 FT_Vector* dst_point = right->points + right->num_points;
1834 FT_Byte* dst_tag = right->tags + right->num_points;
1835 FT_Vector* src_point = left->points + left->num_points - 1;
1836 FT_Byte* src_tag = left->tags + left->num_points - 1;
1839 while ( src_point >= left->points + left->start )
1841 *dst_point = *src_point;
1842 *dst_tag = *src_tag;
1845 dst_tag[0] &= ~FT_STROKE_TAG_BEGIN_END;
1849 (FT_Byte)( dst_tag[0] & FT_STROKE_TAG_BEGIN_END );
1852 /* switch begin/end tags if necessary */
1853 if ( ttag == FT_STROKE_TAG_BEGIN ||
1854 ttag == FT_STROKE_TAG_END )
1855 dst_tag[0] ^= FT_STROKE_TAG_BEGIN_END;
1865 left->num_points = left->start;
1866 right->num_points += new_points;
1868 right->movable = FALSE;
1869 left->movable = FALSE;
1877 /* documentation is in ftstroke.h */
1879 /* there's a lot of magic in this function! */
1880 FT_EXPORT_DEF( FT_Error )
1881 FT_Stroker_EndSubPath( FT_Stroker stroker )
1883 FT_Error error = FT_Err_Ok;
1888 error = FT_THROW( Invalid_Argument );
1892 if ( stroker->subpath_open )
1894 FT_StrokeBorder right = stroker->borders;
1897 /* All right, this is an opened path, we need to add a cap between */
1898 /* right & left, add the reverse of left, then add a final cap */
1899 /* between left & right. */
1900 error = ft_stroker_cap( stroker, stroker->angle_in, 0 );
1904 /* add reversed points from `left' to `right' */
1905 error = ft_stroker_add_reverse_left( stroker, TRUE );
1909 /* now add the final cap */
1910 stroker->center = stroker->subpath_start;
1911 error = ft_stroker_cap( stroker,
1912 stroker->subpath_angle + FT_ANGLE_PI, 0 );
1916 /* Now end the right subpath accordingly. The left one is */
1917 /* rewind and doesn't need further processing. */
1918 ft_stroke_border_close( right, FALSE );
1926 /* close the path if needed */
1927 if ( stroker->center.x != stroker->subpath_start.x ||
1928 stroker->center.y != stroker->subpath_start.y )
1930 error = FT_Stroker_LineTo( stroker, &stroker->subpath_start );
1935 /* process the corner */
1936 stroker->angle_out = stroker->subpath_angle;
1937 turn = FT_Angle_Diff( stroker->angle_in,
1938 stroker->angle_out );
1940 /* no specific corner processing is required if the turn is 0 */
1943 /* when we turn to the right, the inside side is 0 */
1946 /* otherwise, the inside side is 1 */
1950 error = ft_stroker_inside( stroker,
1952 stroker->subpath_line_length );
1956 /* process the outside side */
1957 error = ft_stroker_outside( stroker,
1959 stroker->subpath_line_length );
1964 /* then end our two subpaths */
1965 ft_stroke_border_close( stroker->borders + 0, FALSE );
1966 ft_stroke_border_close( stroker->borders + 1, TRUE );
1974 /* documentation is in ftstroke.h */
1976 FT_EXPORT_DEF( FT_Error )
1977 FT_Stroker_GetBorderCounts( FT_Stroker stroker,
1978 FT_StrokerBorder border,
1979 FT_UInt *anum_points,
1980 FT_UInt *anum_contours )
1982 FT_UInt num_points = 0, num_contours = 0;
1986 if ( !stroker || border > 1 )
1988 error = FT_THROW( Invalid_Argument );
1992 error = ft_stroke_border_get_counts( stroker->borders + border,
1993 &num_points, &num_contours );
1996 *anum_points = num_points;
1998 if ( anum_contours )
1999 *anum_contours = num_contours;
2005 /* documentation is in ftstroke.h */
2007 FT_EXPORT_DEF( FT_Error )
2008 FT_Stroker_GetCounts( FT_Stroker stroker,
2009 FT_UInt *anum_points,
2010 FT_UInt *anum_contours )
2012 FT_UInt count1, count2, num_points = 0;
2013 FT_UInt count3, count4, num_contours = 0;
2019 error = FT_THROW( Invalid_Argument );
2023 error = ft_stroke_border_get_counts( stroker->borders + 0,
2028 error = ft_stroke_border_get_counts( stroker->borders + 1,
2033 num_points = count1 + count3;
2034 num_contours = count2 + count4;
2038 *anum_points = num_points;
2040 if ( anum_contours )
2041 *anum_contours = num_contours;
2047 /* documentation is in ftstroke.h */
2049 FT_EXPORT_DEF( void )
2050 FT_Stroker_ExportBorder( FT_Stroker stroker,
2051 FT_StrokerBorder border,
2052 FT_Outline* outline )
2054 if ( !stroker || !outline )
2057 if ( border == FT_STROKER_BORDER_LEFT ||
2058 border == FT_STROKER_BORDER_RIGHT )
2060 FT_StrokeBorder sborder = & stroker->borders[border];
2063 if ( sborder->valid )
2064 ft_stroke_border_export( sborder, outline );
2069 /* documentation is in ftstroke.h */
2071 FT_EXPORT_DEF( void )
2072 FT_Stroker_Export( FT_Stroker stroker,
2073 FT_Outline* outline )
2075 FT_Stroker_ExportBorder( stroker, FT_STROKER_BORDER_LEFT, outline );
2076 FT_Stroker_ExportBorder( stroker, FT_STROKER_BORDER_RIGHT, outline );
2080 /* documentation is in ftstroke.h */
2083 * The following is very similar to FT_Outline_Decompose, except
2084 * that we do support opened paths, and do not scale the outline.
2086 FT_EXPORT_DEF( FT_Error )
2087 FT_Stroker_ParseOutline( FT_Stroker stroker,
2088 FT_Outline* outline,
2092 FT_Vector v_control;
2101 FT_Int n; /* index of contour in outline */
2102 FT_UInt first; /* index of first point in contour */
2103 FT_Int tag; /* current point's state */
2107 return FT_THROW( Invalid_Outline );
2110 return FT_THROW( Invalid_Argument );
2112 FT_Stroker_Rewind( stroker );
2116 for ( n = 0; n < outline->n_contours; n++ )
2118 FT_UInt last; /* index of last point in contour */
2121 last = outline->contours[n];
2122 limit = outline->points + last;
2124 /* skip empty points; we don't stroke these */
2125 if ( last <= first )
2131 v_start = outline->points[first];
2132 v_last = outline->points[last];
2134 v_control = v_start;
2136 point = outline->points + first;
2137 tags = outline->tags + first;
2138 tag = FT_CURVE_TAG( tags[0] );
2140 /* A contour cannot start with a cubic control point! */
2141 if ( tag == FT_CURVE_TAG_CUBIC )
2142 goto Invalid_Outline;
2144 /* check first point to determine origin */
2145 if ( tag == FT_CURVE_TAG_CONIC )
2147 /* First point is conic control. Yes, this happens. */
2148 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
2150 /* start at last point if it is on the curve */
2156 /* if both first and last points are conic, */
2157 /* start at their middle */
2158 v_start.x = ( v_start.x + v_last.x ) / 2;
2159 v_start.y = ( v_start.y + v_last.y ) / 2;
2165 error = FT_Stroker_BeginSubPath( stroker, &v_start, opened );
2169 while ( point < limit )
2174 tag = FT_CURVE_TAG( tags[0] );
2177 case FT_CURVE_TAG_ON: /* emit a single line_to */
2185 error = FT_Stroker_LineTo( stroker, &vec );
2191 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
2192 v_control.x = point->x;
2193 v_control.y = point->y;
2196 if ( point < limit )
2204 tag = FT_CURVE_TAG( tags[0] );
2208 if ( tag == FT_CURVE_TAG_ON )
2210 error = FT_Stroker_ConicTo( stroker, &v_control, &vec );
2216 if ( tag != FT_CURVE_TAG_CONIC )
2217 goto Invalid_Outline;
2219 v_middle.x = ( v_control.x + vec.x ) / 2;
2220 v_middle.y = ( v_control.y + vec.y ) / 2;
2222 error = FT_Stroker_ConicTo( stroker, &v_control, &v_middle );
2230 error = FT_Stroker_ConicTo( stroker, &v_control, &v_start );
2233 default: /* FT_CURVE_TAG_CUBIC */
2235 FT_Vector vec1, vec2;
2238 if ( point + 1 > limit ||
2239 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
2240 goto Invalid_Outline;
2248 if ( point <= limit )
2255 error = FT_Stroker_CubicTo( stroker, &vec1, &vec2, &vec );
2261 error = FT_Stroker_CubicTo( stroker, &vec1, &vec2, &v_start );
2271 /* don't try to end the path if no segments have been generated */
2272 if ( !stroker->first_point )
2274 error = FT_Stroker_EndSubPath( stroker );
2288 return FT_THROW( Invalid_Outline );
2292 /* declare an extern to access `ft_outline_glyph_class' globally */
2293 /* allocated in `ftglyph.c', and use the FT_OUTLINE_GLYPH_CLASS_GET */
2294 /* macro to access it when FT_CONFIG_OPTION_PIC is defined */
2295 #ifndef FT_CONFIG_OPTION_PIC
2296 extern const FT_Glyph_Class ft_outline_glyph_class;
2298 #include "basepic.h"
2301 /* documentation is in ftstroke.h */
2303 FT_EXPORT_DEF( FT_Error )
2304 FT_Glyph_Stroke( FT_Glyph *pglyph,
2308 FT_Error error = FT_ERR( Invalid_Argument );
2309 FT_Glyph glyph = NULL;
2311 /* for FT_OUTLINE_GLYPH_CLASS_GET (in PIC mode) */
2312 FT_Library library = stroker->library;
2314 FT_UNUSED( library );
2321 if ( !glyph || glyph->clazz != FT_OUTLINE_GLYPH_CLASS_GET )
2328 error = FT_Glyph_Copy( glyph, © );
2336 FT_OutlineGlyph oglyph = (FT_OutlineGlyph)glyph;
2337 FT_Outline* outline = &oglyph->outline;
2338 FT_UInt num_points, num_contours;
2341 error = FT_Stroker_ParseOutline( stroker, outline, FALSE );
2345 FT_Stroker_GetCounts( stroker, &num_points, &num_contours );
2347 FT_Outline_Done( glyph->library, outline );
2349 error = FT_Outline_New( glyph->library,
2350 num_points, num_contours, outline );
2354 outline->n_points = 0;
2355 outline->n_contours = 0;
2357 FT_Stroker_Export( stroker, outline );
2361 FT_Done_Glyph( *pglyph );
2367 FT_Done_Glyph( glyph );
2378 /* documentation is in ftstroke.h */
2380 FT_EXPORT_DEF( FT_Error )
2381 FT_Glyph_StrokeBorder( FT_Glyph *pglyph,
2386 FT_Error error = FT_ERR( Invalid_Argument );
2387 FT_Glyph glyph = NULL;
2389 /* for FT_OUTLINE_GLYPH_CLASS_GET (in PIC mode) */
2390 FT_Library library = stroker->library;
2392 FT_UNUSED( library );
2399 if ( !glyph || glyph->clazz != FT_OUTLINE_GLYPH_CLASS_GET )
2406 error = FT_Glyph_Copy( glyph, © );
2414 FT_OutlineGlyph oglyph = (FT_OutlineGlyph)glyph;
2415 FT_StrokerBorder border;
2416 FT_Outline* outline = &oglyph->outline;
2417 FT_UInt num_points, num_contours;
2420 border = FT_Outline_GetOutsideBorder( outline );
2423 if ( border == FT_STROKER_BORDER_LEFT )
2424 border = FT_STROKER_BORDER_RIGHT;
2426 border = FT_STROKER_BORDER_LEFT;
2429 error = FT_Stroker_ParseOutline( stroker, outline, FALSE );
2433 FT_Stroker_GetBorderCounts( stroker, border,
2434 &num_points, &num_contours );
2436 FT_Outline_Done( glyph->library, outline );
2438 error = FT_Outline_New( glyph->library,
2445 outline->n_points = 0;
2446 outline->n_contours = 0;
2448 FT_Stroker_ExportBorder( stroker, border, outline );
2452 FT_Done_Glyph( *pglyph );
2458 FT_Done_Glyph( glyph );