29 FT_CALLBACK_TABLE
const FT_Glyph_Class ft_outline_glyph_class;
34 FT_EXPORT_DEF( FT_StrokerBorder )
35 FT_Outline_GetInsideBorder( FT_Outline* outline )
37 FT_Orientation o = FT_Outline_Get_Orientation( outline );
40 return o == FT_ORIENTATION_TRUETYPE ? FT_STROKER_BORDER_RIGHT
41 : FT_STROKER_BORDER_LEFT;
47 FT_EXPORT_DEF( FT_StrokerBorder )
48 FT_Outline_GetOutsideBorder( FT_Outline* outline )
50 FT_Orientation o = FT_Outline_Get_Orientation( outline );
53 return o == FT_ORIENTATION_TRUETYPE ? FT_STROKER_BORDER_LEFT
54 : FT_STROKER_BORDER_RIGHT;
66#define FT_SMALL_CONIC_THRESHOLD ( FT_ANGLE_PI / 6 )
67#define FT_SMALL_CUBIC_THRESHOLD ( FT_ANGLE_PI / 8 )
71#define FT_IS_SMALL( x ) ( (x) > -FT_EPSILON && (x) < FT_EPSILON )
75 ft_pos_abs( FT_Pos x )
77 return x >= 0 ? x : -x;
82 ft_conic_split( FT_Vector* base )
87 base[4].x = base[2].x;
88 a = base[0].x + base[1].x;
89 b = base[1].x + base[2].x;
91 base[2].x = ( a + b ) >> 2;
94 base[4].y = base[2].y;
95 a = base[0].y + base[1].y;
96 b = base[1].y + base[2].y;
98 base[2].y = ( a + b ) >> 2;
104 ft_conic_is_small_enough( FT_Vector* base,
106 FT_Angle *angle_out )
110 FT_Int close1, close2;
113 d1.x = base[1].x - base[2].x;
114 d1.y = base[1].y - base[2].y;
115 d2.x = base[0].x - base[1].x;
116 d2.y = base[0].y - base[1].y;
118 close1 = FT_IS_SMALL( d1.x ) && FT_IS_SMALL( d1.y );
119 close2 = FT_IS_SMALL( d2.x ) && FT_IS_SMALL( d2.y );
131 *angle_out = FT_Atan2( d2.x, d2.y );
139 *angle_out = FT_Atan2( d1.x, d1.y );
143 *angle_in = FT_Atan2( d1.x, d1.y );
144 *angle_out = FT_Atan2( d2.x, d2.y );
148 theta = ft_pos_abs( FT_Angle_Diff( *angle_in, *angle_out ) );
150 return FT_BOOL( theta < FT_SMALL_CONIC_THRESHOLD );
155 ft_cubic_split( FT_Vector* base )
160 base[6].x = base[3].x;
161 a = base[0].x + base[1].x;
162 b = base[1].x + base[2].x;
163 c = base[2].x + base[3].x;
170 base[3].x = ( a + c ) >> 3;
172 base[6].y = base[3].y;
173 a = base[0].y + base[1].y;
174 b = base[1].y + base[2].y;
175 c = base[2].y + base[3].y;
182 base[3].y = ( a + c ) >> 3;
190 ft_angle_mean( FT_Angle angle1,
193 return angle1 + FT_Angle_Diff( angle1, angle2 ) / 2;
198 ft_cubic_is_small_enough( FT_Vector* base,
201 FT_Angle *angle_out )
203 FT_Vector d1, d2, d3;
204 FT_Angle theta1, theta2;
205 FT_Int close1, close2, close3;
208 d1.x = base[2].x - base[3].x;
209 d1.y = base[2].y - base[3].y;
210 d2.x = base[1].x - base[2].x;
211 d2.y = base[1].y - base[2].y;
212 d3.x = base[0].x - base[1].x;
213 d3.y = base[0].y - base[1].y;
215 close1 = FT_IS_SMALL( d1.x ) && FT_IS_SMALL( d1.y );
216 close2 = FT_IS_SMALL( d2.x ) && FT_IS_SMALL( d2.y );
217 close3 = FT_IS_SMALL( d3.x ) && FT_IS_SMALL( d3.y );
232 *angle_out = FT_Atan2( d3.x, d3.y );
241 *angle_out = FT_Atan2( d2.x, d2.y );
246 *angle_mid = FT_Atan2( d2.x, d2.y );
247 *angle_out = FT_Atan2( d3.x, d3.y );
259 *angle_out = FT_Atan2( d1.x, d1.y );
263 *angle_in = FT_Atan2( d1.x, d1.y );
264 *angle_out = FT_Atan2( d3.x, d3.y );
265 *angle_mid = ft_angle_mean( *angle_in, *angle_out );
272 *angle_in = FT_Atan2( d1.x, d1.y );
274 *angle_out = FT_Atan2( d2.x, d2.y );
278 *angle_in = FT_Atan2( d1.x, d1.y );
279 *angle_mid = FT_Atan2( d2.x, d2.y );
280 *angle_out = FT_Atan2( d3.x, d3.y );
285 theta1 = ft_pos_abs( FT_Angle_Diff( *angle_in, *angle_mid ) );
286 theta2 = ft_pos_abs( FT_Angle_Diff( *angle_mid, *angle_out ) );
288 return FT_BOOL( theta1 < FT_SMALL_CUBIC_THRESHOLD &&
289 theta2 < FT_SMALL_CUBIC_THRESHOLD );
301 typedef enum FT_StrokeTags_
303 FT_STROKE_TAG_ON = 1,
304 FT_STROKE_TAG_CUBIC = 2,
305 FT_STROKE_TAG_BEGIN = 4,
306 FT_STROKE_TAG_END = 8
310#define FT_STROKE_TAG_BEGIN_END ( FT_STROKE_TAG_BEGIN | FT_STROKE_TAG_END )
323 } FT_StrokeBorderRec, *FT_StrokeBorder;
327 ft_stroke_border_grow( FT_StrokeBorder border,
330 FT_UInt old_max = border->max_points;
331 FT_UInt new_max = border->num_points + new_points;
332 FT_Error error = FT_Err_Ok;
335 if ( new_max > old_max )
337 FT_UInt cur_max = old_max;
338 FT_Memory memory = border->memory;
341 while ( cur_max < new_max )
342 cur_max += ( cur_max >> 1 ) + 16;
344 if ( FT_RENEW_ARRAY( border->points, old_max, cur_max ) ||
345 FT_RENEW_ARRAY( border->tags, old_max, cur_max ) )
348 border->max_points = cur_max;
357 ft_stroke_border_close( FT_StrokeBorder border,
360 FT_UInt start = (FT_UInt)border->start;
361 FT_UInt count = border->num_points;
364 FT_ASSERT( border->start >= 0 );
367 if ( count <= start + 1U )
368 border->num_points = start;
373 border->num_points = --count;
374 border->points[start] = border->points[count];
375 border->tags[start] = border->tags[count];
381 FT_Vector* vec1 = border->points + start + 1;
382 FT_Vector* vec2 = border->points + count - 1;
385 for ( ; vec1 < vec2; vec1++, vec2-- )
398 FT_Byte* tag1 = border->tags + start + 1;
399 FT_Byte* tag2 = border->tags + count - 1;
402 for ( ; tag1 < tag2; tag1++, tag2-- )
414 border->tags[start ] |= FT_STROKE_TAG_BEGIN;
415 border->tags[count - 1] |= FT_STROKE_TAG_END;
419 border->movable = FALSE;
424 ft_stroke_border_lineto( FT_StrokeBorder border,
428 FT_Error error = FT_Err_Ok;
431 FT_ASSERT( border->start >= 0 );
433 if ( border->movable )
436 border->points[border->num_points - 1] = *to;
441 if ( border->num_points > (FT_UInt)border->start &&
442 FT_IS_SMALL( border->points[border->num_points - 1].x - to->x ) &&
443 FT_IS_SMALL( border->points[border->num_points - 1].y - to->y ) )
447 error = ft_stroke_border_grow( border, 1 );
450 FT_Vector* vec = border->points + border->num_points;
451 FT_Byte* tag = border->tags + border->num_points;
455 tag[0] = FT_STROKE_TAG_ON;
457 border->num_points += 1;
460 border->movable = movable;
466 ft_stroke_border_conicto( FT_StrokeBorder border,
473 FT_ASSERT( border->start >= 0 );
475 error = ft_stroke_border_grow( border, 2 );
478 FT_Vector* vec = border->points + border->num_points;
479 FT_Byte* tag = border->tags + border->num_points;
486 tag[1] = FT_STROKE_TAG_ON;
488 border->num_points += 2;
491 border->movable = FALSE;
498 ft_stroke_border_cubicto( FT_StrokeBorder border,
506 FT_ASSERT( border->start >= 0 );
508 error = ft_stroke_border_grow( border, 3 );
511 FT_Vector* vec = border->points + border->num_points;
512 FT_Byte* tag = border->tags + border->num_points;
519 tag[0] = FT_STROKE_TAG_CUBIC;
520 tag[1] = FT_STROKE_TAG_CUBIC;
521 tag[2] = FT_STROKE_TAG_ON;
523 border->num_points += 3;
526 border->movable = FALSE;
532#define FT_ARC_CUBIC_ANGLE ( FT_ANGLE_PI / 2 )
536 ft_stroke_border_arcto( FT_StrokeBorder border,
539 FT_Angle angle_start,
540 FT_Angle angle_diff )
543 FT_Vector a0, a1, a2, a3;
545 FT_Error error = FT_Err_Ok;
549 while ( angle_diff > FT_ARC_CUBIC_ANGLE * arcs ||
550 -angle_diff > FT_ARC_CUBIC_ANGLE * arcs )
554 coef = FT_Tan( angle_diff / ( 4 * arcs ) );
558 FT_Vector_From_Polar( &a0, radius, angle_start );
559 a1.x = FT_MulFix( -a0.y, coef );
560 a1.y = FT_MulFix( a0.x, coef );
567 for ( i = 1; i <= arcs; i++ )
570 FT_Vector_From_Polar( &a3, radius,
571 angle_start + i * angle_diff / arcs );
572 a2.x = FT_MulFix( a3.y, coef );
573 a2.y = FT_MulFix( -a3.x, coef );
581 error = ft_stroke_border_cubicto( border, &a1, &a2, &a3 );
586 a1.x = a3.x - a2.x + a3.x;
587 a1.y = a3.y - a2.y + a3.y;
595 ft_stroke_border_moveto( FT_StrokeBorder border,
599 if ( border->start >= 0 )
600 ft_stroke_border_close( border, FALSE );
602 border->start = (FT_Int)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 )
705 if ( border->num_points )
706 FT_ARRAY_COPY( outline->points + outline->n_points,
708 border->num_points );
712 FT_UInt count = border->num_points;
713 FT_Byte* read = border->tags;
714 FT_Byte* write = outline->tags + outline->n_points;
717 for ( ; count > 0; count--, read++, write++ )
719 if ( *read & FT_STROKE_TAG_ON )
720 *write = FT_CURVE_TAG_ON;
721 else if ( *read & FT_STROKE_TAG_CUBIC )
722 *write = FT_CURVE_TAG_CUBIC;
724 *write = FT_CURVE_TAG_CONIC;
730 FT_UInt count = border->num_points;
731 FT_Byte* tags = border->tags;
732 FT_UShort* write = outline->contours + outline->n_contours;
733 FT_UShort idx = outline->n_points;
736 for ( ; count > 0; count--, tags++, idx++ )
738 if ( *tags & FT_STROKE_TAG_END )
741 outline->n_contours++;
746 outline->n_points += (FT_UShort)border->num_points;
748 FT_ASSERT( FT_Outline_Check( outline ) == 0 );
760#define FT_SIDE_TO_ROTATE( s ) ( FT_ANGLE_PI2 - (s) * FT_ANGLE_PI )
767 FT_Fixed line_length;
769 FT_Bool subpath_open;
770 FT_Angle subpath_angle;
771 FT_Vector subpath_start;
772 FT_Fixed subpath_line_length;
773 FT_Bool handle_wide_strokes;
775 FT_Stroker_LineCap line_cap;
776 FT_Stroker_LineJoin line_join;
777 FT_Stroker_LineJoin line_join_saved;
778 FT_Fixed miter_limit;
781 FT_StrokeBorderRec borders[2];
789 FT_EXPORT_DEF( FT_Error )
790 FT_Stroker_New( FT_Library library,
791 FT_Stroker *astroker )
795 FT_Stroker stroker = NULL;
799 return FT_THROW( Invalid_Library_Handle );
802 return FT_THROW( Invalid_Argument );
804 memory = library->memory;
806 if ( !FT_NEW( stroker ) )
808 stroker->library = library;
810 ft_stroke_border_init( &stroker->borders[0], memory );
811 ft_stroke_border_init( &stroker->borders[1], memory );
822 FT_EXPORT_DEF(
void )
823 FT_Stroker_Set( FT_Stroker stroker,
825 FT_Stroker_LineCap line_cap,
826 FT_Stroker_LineJoin line_join,
827 FT_Fixed miter_limit )
832 stroker->radius = radius;
833 stroker->line_cap = line_cap;
834 stroker->line_join = line_join;
835 stroker->miter_limit = miter_limit;
838 if ( stroker->miter_limit < 0x10000L )
839 stroker->miter_limit = 0x10000L;
843 stroker->line_join_saved = line_join;
845 FT_Stroker_Rewind( stroker );
851 FT_EXPORT_DEF(
void )
852 FT_Stroker_Rewind( FT_Stroker stroker )
856 ft_stroke_border_reset( &stroker->borders[0] );
857 ft_stroke_border_reset( &stroker->borders[1] );
864 FT_EXPORT_DEF(
void )
865 FT_Stroker_Done( FT_Stroker stroker )
869 FT_Memory memory = stroker->library->memory;
872 ft_stroke_border_done( &stroker->borders[0] );
873 ft_stroke_border_done( &stroker->borders[1] );
875 stroker->library = NULL;
883 ft_stroker_arcto( FT_Stroker stroker,
886 FT_Angle total, rotate;
887 FT_Fixed radius = stroker->radius;
888 FT_Error error = FT_Err_Ok;
889 FT_StrokeBorder border = stroker->borders + side;
892 rotate = FT_SIDE_TO_ROTATE( side );
894 total = FT_Angle_Diff( stroker->angle_in, stroker->angle_out );
895 if ( total == FT_ANGLE_PI )
898 error = ft_stroke_border_arcto( border,
901 stroker->angle_in + rotate,
903 border->movable = FALSE;
910 ft_stroker_cap( FT_Stroker stroker,
914 FT_Error error = FT_Err_Ok;
917 if ( stroker->line_cap == FT_STROKER_LINECAP_ROUND )
920 stroker->angle_in = angle;
921 stroker->angle_out = angle + FT_ANGLE_PI;
923 error = ft_stroker_arcto( stroker, side );
928 FT_Vector middle, delta;
929 FT_Fixed radius = stroker->radius;
930 FT_StrokeBorder border = stroker->borders + side;
934 FT_Vector_From_Polar( &middle, radius, angle );
935 delta.x = side ? middle.y : -middle.y;
936 delta.y = side ? -middle.x : middle.x;
938 if ( stroker->line_cap == FT_STROKER_LINECAP_SQUARE )
940 middle.x += stroker->center.x;
941 middle.y += stroker->center.y;
945 middle.x = stroker->center.x;
946 middle.y = stroker->center.y;
952 error = ft_stroke_border_lineto( border, &delta, FALSE );
957 delta.x = middle.x - delta.x + middle.x;
958 delta.y = middle.y - delta.y + middle.y;
960 error = ft_stroke_border_lineto( border, &delta, FALSE );
970 ft_stroker_inside( FT_Stroker stroker,
972 FT_Fixed line_length )
974 FT_StrokeBorder border = stroker->borders + side;
975 FT_Angle phi, theta, rotate;
977 FT_Vector sigma = { 0, 0 };
979 FT_Error error = FT_Err_Ok;
983 rotate = FT_SIDE_TO_ROTATE( side );
985 theta = FT_Angle_Diff( stroker->angle_in, stroker->angle_out ) / 2;
990 if ( !border->movable || line_length == 0 ||
991 theta > 0x59C000 || theta < -0x59C000 )
999 FT_Vector_Unit( &sigma, theta );
1001 ft_pos_abs( FT_MulDiv( stroker->radius, sigma.y, sigma.x ) );
1003 intersect = FT_BOOL( min_length &&
1004 stroker->line_length >= min_length &&
1005 line_length >= min_length );
1010 FT_Vector_From_Polar( &delta, stroker->radius,
1011 stroker->angle_out + rotate );
1012 delta.x += stroker->center.x;
1013 delta.y += stroker->center.y;
1015 border->movable = FALSE;
1020 phi = stroker->angle_in + theta + rotate;
1022 length = FT_DivFix( stroker->radius, sigma.x );
1024 FT_Vector_From_Polar( &delta, length, phi );
1025 delta.x += stroker->center.x;
1026 delta.y += stroker->center.y;
1029 error = ft_stroke_border_lineto( border, &delta, FALSE );
1037 ft_stroker_outside( FT_Stroker stroker,
1039 FT_Fixed line_length )
1041 FT_StrokeBorder border = stroker->borders + side;
1046 if ( stroker->line_join == FT_STROKER_LINEJOIN_ROUND )
1047 error = ft_stroker_arcto( stroker, side );
1051 FT_Fixed radius = stroker->radius;
1052 FT_Vector sigma = { 0, 0 };
1053 FT_Angle theta = 0, phi = 0;
1054 FT_Bool bevel, fixed_bevel;
1057 rotate = FT_SIDE_TO_ROTATE( side );
1060 FT_BOOL( stroker->line_join == FT_STROKER_LINEJOIN_BEVEL );
1063 FT_BOOL( stroker->line_join != FT_STROKER_LINEJOIN_MITER_VARIABLE );
1068 theta = FT_Angle_Diff( stroker->angle_in, stroker->angle_out ) / 2;
1070 if ( theta == FT_ANGLE_PI2 )
1073 phi = stroker->angle_in + theta + rotate;
1075 FT_Vector_From_Polar( &sigma, stroker->miter_limit, theta );
1078 if ( sigma.x < 0x10000L )
1082 if ( fixed_bevel || ft_pos_abs( theta ) > 57 )
1096 FT_Vector_From_Polar( &delta,
1098 stroker->angle_out + rotate );
1099 delta.x += stroker->center.x;
1100 delta.y += stroker->center.y;
1102 border->movable = FALSE;
1103 error = ft_stroke_border_lineto( border, &delta, FALSE );
1108 FT_Vector middle, delta;
1113 FT_Vector_From_Polar( &middle,
1114 FT_MulFix( radius, stroker->miter_limit ),
1117 coef = FT_DivFix( 0x10000L - sigma.x, sigma.y );
1118 delta.x = FT_MulFix( middle.y, coef );
1119 delta.y = FT_MulFix( -middle.x, coef );
1121 middle.x += stroker->center.x;
1122 middle.y += stroker->center.y;
1123 delta.x += middle.x;
1124 delta.y += middle.y;
1126 error = ft_stroke_border_lineto( border, &delta, FALSE );
1131 delta.x = middle.x - delta.x + middle.x;
1132 delta.y = middle.y - delta.y + middle.y;
1134 error = ft_stroke_border_lineto( border, &delta, FALSE );
1140 if ( line_length == 0 )
1142 FT_Vector_From_Polar( &delta,
1144 stroker->angle_out + rotate );
1146 delta.x += stroker->center.x;
1147 delta.y += stroker->center.y;
1149 error = ft_stroke_border_lineto( border, &delta, FALSE );
1159 length = FT_MulDiv( stroker->radius, stroker->miter_limit, sigma.x );
1161 FT_Vector_From_Polar( &delta, length, phi );
1162 delta.x += stroker->center.x;
1163 delta.y += stroker->center.y;
1165 error = ft_stroke_border_lineto( border, &delta, FALSE );
1171 if ( line_length == 0 )
1173 FT_Vector_From_Polar( &delta,
1175 stroker->angle_out + rotate );
1176 delta.x += stroker->center.x;
1177 delta.y += stroker->center.y;
1179 error = ft_stroke_border_lineto( border, &delta, FALSE );
1190 ft_stroker_process_corner( FT_Stroker stroker,
1191 FT_Fixed line_length )
1193 FT_Error error = FT_Err_Ok;
1198 turn = FT_Angle_Diff( stroker->angle_in, stroker->angle_out );
1206 inside_side = ( turn < 0 );
1209 error = ft_stroker_inside( stroker, inside_side, line_length );
1214 error = ft_stroker_outside( stroker, !inside_side, line_length );
1224 ft_stroker_subpath_start( FT_Stroker stroker,
1225 FT_Angle start_angle,
1226 FT_Fixed line_length )
1231 FT_StrokeBorder border;
1234 FT_Vector_From_Polar( &delta, stroker->radius,
1235 start_angle + FT_ANGLE_PI2 );
1237 point.x = stroker->center.x + delta.x;
1238 point.y = stroker->center.y + delta.y;
1240 border = stroker->borders;
1241 error = ft_stroke_border_moveto( border, &point );
1245 point.x = stroker->center.x - delta.x;
1246 point.y = stroker->center.y - delta.y;
1249 error = ft_stroke_border_moveto( border, &point );
1253 stroker->subpath_angle = start_angle;
1254 stroker->first_point = FALSE;
1255 stroker->subpath_line_length = line_length;
1264 FT_EXPORT_DEF( FT_Error )
1265 FT_Stroker_LineTo( FT_Stroker stroker,
1268 FT_Error error = FT_Err_Ok;
1269 FT_StrokeBorder border;
1273 FT_Fixed line_length;
1276 if ( !stroker || !to )
1277 return FT_THROW( Invalid_Argument );
1279 delta.x = to->x - stroker->center.x;
1280 delta.y = to->y - stroker->center.y;
1283 if ( delta.x == 0 && delta.y == 0 )
1287 line_length = FT_Vector_Length( &delta );
1289 angle = FT_Atan2( delta.x, delta.y );
1290 FT_Vector_From_Polar( &delta, stroker->radius, angle + FT_ANGLE_PI2 );
1293 if ( stroker->first_point )
1298 error = ft_stroker_subpath_start( stroker, angle, line_length );
1305 stroker->angle_out = angle;
1306 error = ft_stroker_process_corner( stroker, line_length );
1312 for ( border = stroker->borders, side = 1; side >= 0; side--, border++ )
1317 point.x = to->x + delta.x;
1318 point.y = to->y + delta.y;
1321 error = ft_stroke_border_lineto( border, &point, TRUE );
1329 stroker->angle_in = angle;
1330 stroker->center = *to;
1331 stroker->line_length = line_length;
1340 FT_EXPORT_DEF( FT_Error )
1341 FT_Stroker_ConicTo( FT_Stroker stroker,
1345 FT_Error error = FT_Err_Ok;
1346 FT_Vector bez_stack[34];
1348 FT_Vector* limit = bez_stack + 30;
1349 FT_Bool first_arc = TRUE;
1352 if ( !stroker || !control || !to )
1354 error = FT_THROW( Invalid_Argument );
1360 if ( FT_IS_SMALL( stroker->center.x - control->x ) &&
1361 FT_IS_SMALL( stroker->center.y - control->y ) &&
1362 FT_IS_SMALL( control->x - to->x ) &&
1363 FT_IS_SMALL( control->y - to->y ) )
1365 stroker->center = *to;
1372 arc[2] = stroker->center;
1376 FT_Angle angle_in, angle_out;
1380 angle_in = angle_out = stroker->angle_in;
1383 !ft_conic_is_small_enough( arc, &angle_in, &angle_out ) )
1385 if ( stroker->first_point )
1386 stroker->angle_in = angle_in;
1388 ft_conic_split( arc );
1398 if ( stroker->first_point )
1399 error = ft_stroker_subpath_start( stroker, angle_in, 0 );
1402 stroker->angle_out = angle_in;
1403 error = ft_stroker_process_corner( stroker, 0 );
1406 else if ( ft_pos_abs( FT_Angle_Diff( stroker->angle_in, angle_in ) ) >
1407 FT_SMALL_CONIC_THRESHOLD / 4 )
1411 stroker->center = arc[2];
1412 stroker->angle_out = angle_in;
1413 stroker->line_join = FT_STROKER_LINEJOIN_ROUND;
1415 error = ft_stroker_process_corner( stroker, 0 );
1418 stroker->line_join = stroker->line_join_saved;
1427 FT_Vector ctrl, end;
1428 FT_Angle theta, phi, rotate, alpha0 = 0;
1430 FT_StrokeBorder border;
1434 theta = FT_Angle_Diff( angle_in, angle_out ) / 2;
1435 phi = angle_in + theta;
1436 length = FT_DivFix( stroker->radius, FT_Cos( theta ) );
1439 if ( stroker->handle_wide_strokes )
1440 alpha0 = FT_Atan2( arc[0].x - arc[2].x, arc[0].y - arc[2].y );
1442 for ( border = stroker->borders, side = 0;
1446 rotate = FT_SIDE_TO_ROTATE( side );
1449 FT_Vector_From_Polar( &ctrl, length, phi + rotate );
1454 FT_Vector_From_Polar( &end, stroker->radius, angle_out + rotate );
1458 if ( stroker->handle_wide_strokes )
1466 start = border->points[border->num_points - 1];
1468 alpha1 = FT_Atan2( end.x - start.x, end.y - start.y );
1472 if ( ft_pos_abs( FT_Angle_Diff( alpha0, alpha1 ) ) >
1475 FT_Angle beta, gamma;
1476 FT_Vector bvec, delta;
1477 FT_Fixed blen, sinA, sinB, alen;
1481 beta = FT_Atan2( arc[2].x - start.x, arc[2].y - start.y );
1482 gamma = FT_Atan2( arc[0].x - end.x, arc[0].y - end.y );
1484 bvec.x = end.x - start.x;
1485 bvec.y = end.y - start.y;
1487 blen = FT_Vector_Length( &bvec );
1489 sinA = ft_pos_abs( FT_Sin( alpha1 - gamma ) );
1490 sinB = ft_pos_abs( FT_Sin( beta - gamma ) );
1492 alen = FT_MulDiv( blen, sinA, sinB );
1494 FT_Vector_From_Polar( &delta, alen, beta );
1499 border->movable = FALSE;
1500 error = ft_stroke_border_lineto( border, &delta, FALSE );
1503 error = ft_stroke_border_lineto( border, &end, FALSE );
1506 error = ft_stroke_border_conicto( border, &ctrl, &start );
1510 error = ft_stroke_border_lineto( border, &end, FALSE );
1521 error = ft_stroke_border_conicto( border, &ctrl, &end );
1527 stroker->angle_in = angle_out;
1529 if ( arc == bez_stack )
1534 stroker->center = *to;
1535 stroker->line_length = 0;
1544 FT_EXPORT_DEF( FT_Error )
1545 FT_Stroker_CubicTo( FT_Stroker stroker,
1546 FT_Vector* control1,
1547 FT_Vector* control2,
1550 FT_Error error = FT_Err_Ok;
1551 FT_Vector bez_stack[37];
1553 FT_Vector* limit = bez_stack + 32;
1554 FT_Bool first_arc = TRUE;
1557 if ( !stroker || !control1 || !control2 || !to )
1559 error = FT_THROW( Invalid_Argument );
1565 if ( FT_IS_SMALL( stroker->center.x - control1->x ) &&
1566 FT_IS_SMALL( stroker->center.y - control1->y ) &&
1567 FT_IS_SMALL( control1->x - control2->x ) &&
1568 FT_IS_SMALL( control1->y - control2->y ) &&
1569 FT_IS_SMALL( control2->x - to->x ) &&
1570 FT_IS_SMALL( control2->y - to->y ) )
1572 stroker->center = *to;
1580 arc[3] = stroker->center;
1584 FT_Angle angle_in, angle_mid, angle_out;
1588 angle_in = angle_out = angle_mid = stroker->angle_in;
1591 !ft_cubic_is_small_enough( arc, &angle_in,
1592 &angle_mid, &angle_out ) )
1594 if ( stroker->first_point )
1595 stroker->angle_in = angle_in;
1597 ft_cubic_split( arc );
1607 if ( stroker->first_point )
1608 error = ft_stroker_subpath_start( stroker, angle_in, 0 );
1611 stroker->angle_out = angle_in;
1612 error = ft_stroker_process_corner( stroker, 0 );
1615 else if ( ft_pos_abs( FT_Angle_Diff( stroker->angle_in, angle_in ) ) >
1616 FT_SMALL_CUBIC_THRESHOLD / 4 )
1620 stroker->center = arc[3];
1621 stroker->angle_out = angle_in;
1622 stroker->line_join = FT_STROKER_LINEJOIN_ROUND;
1624 error = ft_stroker_process_corner( stroker, 0 );
1627 stroker->line_join = stroker->line_join_saved;
1636 FT_Vector ctrl1, ctrl2, end;
1637 FT_Angle theta1, phi1, theta2, phi2, rotate, alpha0 = 0;
1638 FT_Fixed length1, length2;
1639 FT_StrokeBorder border;
1643 theta1 = FT_Angle_Diff( angle_in, angle_mid ) / 2;
1644 theta2 = FT_Angle_Diff( angle_mid, angle_out ) / 2;
1645 phi1 = ft_angle_mean( angle_in, angle_mid );
1646 phi2 = ft_angle_mean( angle_mid, angle_out );
1647 length1 = FT_DivFix( stroker->radius, FT_Cos( theta1 ) );
1648 length2 = FT_DivFix( stroker->radius, FT_Cos( theta2 ) );
1651 if ( stroker->handle_wide_strokes )
1652 alpha0 = FT_Atan2( arc[0].x - arc[3].x, arc[0].y - arc[3].y );
1654 for ( border = stroker->borders, side = 0;
1658 rotate = FT_SIDE_TO_ROTATE( side );
1661 FT_Vector_From_Polar( &ctrl1, length1, phi1 + rotate );
1662 ctrl1.x += arc[2].x;
1663 ctrl1.y += arc[2].y;
1665 FT_Vector_From_Polar( &ctrl2, length2, phi2 + rotate );
1666 ctrl2.x += arc[1].x;
1667 ctrl2.y += arc[1].y;
1670 FT_Vector_From_Polar( &end, stroker->radius, angle_out + rotate );
1674 if ( stroker->handle_wide_strokes )
1682 start = border->points[border->num_points - 1];
1684 alpha1 = FT_Atan2( end.x - start.x, end.y - start.y );
1688 if ( ft_pos_abs( FT_Angle_Diff( alpha0, alpha1 ) ) >
1691 FT_Angle beta, gamma;
1692 FT_Vector bvec, delta;
1693 FT_Fixed blen, sinA, sinB, alen;
1697 beta = FT_Atan2( arc[3].x - start.x, arc[3].y - start.y );
1698 gamma = FT_Atan2( arc[0].x - end.x, arc[0].y - end.y );
1700 bvec.x = end.x - start.x;
1701 bvec.y = end.y - start.y;
1703 blen = FT_Vector_Length( &bvec );
1705 sinA = ft_pos_abs( FT_Sin( alpha1 - gamma ) );
1706 sinB = ft_pos_abs( FT_Sin( beta - gamma ) );
1708 alen = FT_MulDiv( blen, sinA, sinB );
1710 FT_Vector_From_Polar( &delta, alen, beta );
1715 border->movable = FALSE;
1716 error = ft_stroke_border_lineto( border, &delta, FALSE );
1719 error = ft_stroke_border_lineto( border, &end, FALSE );
1722 error = ft_stroke_border_cubicto( border,
1729 error = ft_stroke_border_lineto( border, &end, FALSE );
1740 error = ft_stroke_border_cubicto( border, &ctrl1, &ctrl2, &end );
1746 stroker->angle_in = angle_out;
1748 if ( arc == bez_stack )
1753 stroker->center = *to;
1754 stroker->line_length = 0;
1763 FT_EXPORT_DEF( FT_Error )
1764 FT_Stroker_BeginSubPath( FT_Stroker stroker,
1768 if ( !stroker || !to )
1769 return FT_THROW( Invalid_Argument );
1775 stroker->first_point = TRUE;
1776 stroker->center = *to;
1777 stroker->subpath_open = open;
1784 stroker->handle_wide_strokes =
1785 FT_BOOL( stroker->line_join != FT_STROKER_LINEJOIN_ROUND ||
1786 ( stroker->subpath_open &&
1787 stroker->line_cap == FT_STROKER_LINECAP_BUTT ) );
1790 stroker->subpath_start = *to;
1792 stroker->angle_in = 0;
1799 ft_stroker_add_reverse_left( FT_Stroker stroker,
1802 FT_StrokeBorder right = stroker->borders + 0;
1803 FT_StrokeBorder left = stroker->borders + 1;
1805 FT_Error error = FT_Err_Ok;
1808 FT_ASSERT( left->start >= 0 );
1810 new_points = (FT_Int)left->num_points - left->start;
1811 if ( new_points > 0 )
1813 error = ft_stroke_border_grow( right, (FT_UInt)new_points );
1818 FT_Vector* dst_point = right->points + right->num_points;
1819 FT_Byte* dst_tag = right->tags + right->num_points;
1820 FT_Vector* src_point = left->points + left->num_points - 1;
1821 FT_Byte* src_tag = left->tags + left->num_points - 1;
1824 while ( src_point >= left->points + left->start )
1826 *dst_point = *src_point;
1827 *dst_tag = *src_tag;
1830 dst_tag[0] &= ~FT_STROKE_TAG_BEGIN_END;
1834 (FT_Byte)( dst_tag[0] & FT_STROKE_TAG_BEGIN_END );
1838 if ( ttag == FT_STROKE_TAG_BEGIN ||
1839 ttag == FT_STROKE_TAG_END )
1840 dst_tag[0] ^= FT_STROKE_TAG_BEGIN_END;
1850 left->num_points = (FT_UInt)left->start;
1851 right->num_points += (FT_UInt)new_points;
1853 right->movable = FALSE;
1854 left->movable = FALSE;
1865 FT_EXPORT_DEF( FT_Error )
1866 FT_Stroker_EndSubPath( FT_Stroker stroker )
1868 FT_Error error = FT_Err_Ok;
1873 error = FT_THROW( Invalid_Argument );
1877 if ( stroker->subpath_open )
1879 FT_StrokeBorder right = stroker->borders;
1885 error = ft_stroker_cap( stroker, stroker->angle_in, 0 );
1890 error = ft_stroker_add_reverse_left( stroker, TRUE );
1895 stroker->center = stroker->subpath_start;
1896 error = ft_stroker_cap( stroker,
1897 stroker->subpath_angle + FT_ANGLE_PI, 0 );
1903 ft_stroke_border_close( right, FALSE );
1908 if ( !FT_IS_SMALL( stroker->center.x - stroker->subpath_start.x ) ||
1909 !FT_IS_SMALL( stroker->center.y - stroker->subpath_start.y ) )
1911 error = FT_Stroker_LineTo( stroker, &stroker->subpath_start );
1917 stroker->angle_out = stroker->subpath_angle;
1919 error = ft_stroker_process_corner( stroker,
1920 stroker->subpath_line_length );
1925 ft_stroke_border_close( stroker->borders + 0, FALSE );
1926 ft_stroke_border_close( stroker->borders + 1, TRUE );
1936 FT_EXPORT_DEF( FT_Error )
1937 FT_Stroker_GetBorderCounts( FT_Stroker stroker,
1938 FT_StrokerBorder border,
1939 FT_UInt *anum_points,
1940 FT_UInt *anum_contours )
1942 FT_UInt num_points = 0, num_contours = 0;
1946 if ( !stroker || border > 1 )
1948 error = FT_THROW( Invalid_Argument );
1952 error = ft_stroke_border_get_counts( stroker->borders + border,
1953 &num_points, &num_contours );
1956 *anum_points = num_points;
1958 if ( anum_contours )
1959 *anum_contours = num_contours;
1967 FT_EXPORT_DEF( FT_Error )
1968 FT_Stroker_GetCounts( FT_Stroker stroker,
1969 FT_UInt *anum_points,
1970 FT_UInt *anum_contours )
1972 FT_UInt count1, count2, num_points = 0;
1973 FT_UInt count3, count4, num_contours = 0;
1979 error = FT_THROW( Invalid_Argument );
1983 error = ft_stroke_border_get_counts( stroker->borders + 0,
1988 error = ft_stroke_border_get_counts( stroker->borders + 1,
1993 num_points = count1 + count3;
1994 num_contours = count2 + count4;
1998 *anum_points = num_points;
2000 if ( anum_contours )
2001 *anum_contours = num_contours;
2009 FT_EXPORT_DEF(
void )
2010 FT_Stroker_ExportBorder( FT_Stroker stroker,
2011 FT_StrokerBorder border,
2012 FT_Outline* outline )
2014 if ( !stroker || !outline )
2017 if ( border == FT_STROKER_BORDER_LEFT ||
2018 border == FT_STROKER_BORDER_RIGHT )
2020 FT_StrokeBorder sborder = & stroker->borders[border];
2023 if ( sborder->valid )
2024 ft_stroke_border_export( sborder, outline );
2031 FT_EXPORT_DEF(
void )
2032 FT_Stroker_Export( FT_Stroker stroker,
2033 FT_Outline* outline )
2035 FT_Stroker_ExportBorder( stroker, FT_STROKER_BORDER_LEFT, outline );
2036 FT_Stroker_ExportBorder( stroker, FT_STROKER_BORDER_RIGHT, outline );
2046 FT_EXPORT_DEF( FT_Error )
2047 FT_Stroker_ParseOutline( FT_Stroker stroker,
2048 FT_Outline* outline,
2052 FT_Vector v_control;
2069 return FT_THROW( Invalid_Outline );
2072 return FT_THROW( Invalid_Argument );
2074 FT_Stroker_Rewind( stroker );
2077 for ( n = 0; n < outline->n_contours; n++ )
2080 last = outline->contours[n];
2083 if ( last <= first )
2086 limit = outline->points + last;
2088 v_start = outline->points[first];
2089 v_last = outline->points[last];
2091 v_control = v_start;
2093 point = outline->points + first;
2094 tags = outline->tags + first;
2095 tag = FT_CURVE_TAG( tags[0] );
2098 if ( tag == FT_CURVE_TAG_CUBIC )
2099 goto Invalid_Outline;
2102 if ( tag == FT_CURVE_TAG_CONIC )
2105 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
2115 v_start.x = ( v_start.x + v_last.x ) / 2;
2116 v_start.y = ( v_start.y + v_last.y ) / 2;
2122 error = FT_Stroker_BeginSubPath( stroker, &v_start, opened );
2126 while ( point < limit )
2131 tag = FT_CURVE_TAG( tags[0] );
2134 case FT_CURVE_TAG_ON:
2142 error = FT_Stroker_LineTo( stroker, &vec );
2148 case FT_CURVE_TAG_CONIC:
2149 v_control.x = point->x;
2150 v_control.y = point->y;
2153 if ( point < limit )
2161 tag = FT_CURVE_TAG( tags[0] );
2165 if ( tag == FT_CURVE_TAG_ON )
2167 error = FT_Stroker_ConicTo( stroker, &v_control, &vec );
2173 if ( tag != FT_CURVE_TAG_CONIC )
2174 goto Invalid_Outline;
2176 v_middle.x = ( v_control.x + vec.x ) / 2;
2177 v_middle.y = ( v_control.y + vec.y ) / 2;
2179 error = FT_Stroker_ConicTo( stroker, &v_control, &v_middle );
2187 error = FT_Stroker_ConicTo( stroker, &v_control, &v_start );
2192 FT_Vector vec1, vec2;
2195 if ( point + 1 > limit ||
2196 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
2197 goto Invalid_Outline;
2205 if ( point <= limit )
2212 error = FT_Stroker_CubicTo( stroker, &vec1, &vec2, &vec );
2218 error = FT_Stroker_CubicTo( stroker, &vec1, &vec2, &v_start );
2229 if ( !stroker->first_point )
2231 error = FT_Stroker_EndSubPath( stroker );
2243 return FT_THROW( Invalid_Outline );
2249 FT_EXPORT_DEF( FT_Error )
2250 FT_Glyph_Stroke( FT_Glyph *pglyph,
2254 FT_Error error = FT_ERR( Invalid_Argument );
2255 FT_Glyph glyph = NULL;
2262 if ( !glyph || glyph->clazz != &ft_outline_glyph_class )
2269 error = FT_Glyph_Copy( glyph, © );
2277 FT_OutlineGlyph oglyph = (FT_OutlineGlyph)glyph;
2278 FT_Outline* outline = &oglyph->outline;
2279 FT_UInt num_points, num_contours;
2282 error = FT_Stroker_ParseOutline( stroker, outline, FALSE );
2286 FT_Stroker_GetCounts( stroker, &num_points, &num_contours );
2288 FT_Outline_Done( glyph->library, outline );
2290 error = FT_Outline_New( glyph->library,
2292 (FT_Int)num_contours,
2297 outline->n_points = 0;
2298 outline->n_contours = 0;
2300 FT_Stroker_Export( stroker, outline );
2304 FT_Done_Glyph( *pglyph );
2310 FT_Done_Glyph( glyph );
2323 FT_EXPORT_DEF( FT_Error )
2324 FT_Glyph_StrokeBorder( FT_Glyph *pglyph,
2329 FT_Error error = FT_ERR( Invalid_Argument );
2330 FT_Glyph glyph = NULL;
2337 if ( !glyph || glyph->clazz != &ft_outline_glyph_class )
2344 error = FT_Glyph_Copy( glyph, © );
2352 FT_OutlineGlyph oglyph = (FT_OutlineGlyph)glyph;
2353 FT_StrokerBorder border;
2354 FT_Outline* outline = &oglyph->outline;
2355 FT_UInt num_points, num_contours;
2358 border = FT_Outline_GetOutsideBorder( outline );
2361 if ( border == FT_STROKER_BORDER_LEFT )
2362 border = FT_STROKER_BORDER_RIGHT;
2364 border = FT_STROKER_BORDER_LEFT;
2367 error = FT_Stroker_ParseOutline( stroker, outline, FALSE );
2371 FT_Stroker_GetBorderCounts( stroker, border,
2372 &num_points, &num_contours );
2374 FT_Outline_Done( glyph->library, outline );
2376 error = FT_Outline_New( glyph->library,
2378 (FT_Int)num_contours,
2383 outline->n_points = 0;
2384 outline->n_contours = 0;
2386 FT_Stroker_ExportBorder( stroker, border, outline );
2390 FT_Done_Glyph( *pglyph );
2396 FT_Done_Glyph( glyph );