1 /***************************************************************************/
5 /* FreeType outline management (body). */
7 /* Copyright 1996-2008, 2010, 2012-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 /***************************************************************************/
19 /*************************************************************************/
21 /* All functions are declared in freetype.h. */
23 /*************************************************************************/
28 #include FT_INTERNAL_OBJECTS_H
29 #include FT_INTERNAL_CALC_H
30 #include FT_INTERNAL_DEBUG_H
31 #include FT_TRIGONOMETRY_H
34 /*************************************************************************/
36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38 /* messages during execution. */
41 #define FT_COMPONENT trace_outline
45 const FT_Outline null_outline = { 0, 0, 0, 0, 0, 0 };
48 /* documentation is in ftoutln.h */
50 FT_EXPORT_DEF( FT_Error )
51 FT_Outline_Decompose( FT_Outline* outline,
52 const FT_Outline_Funcs* func_interface,
56 #define SCALED( x ) ( ( (x) << shift ) - delta )
68 FT_Int n; /* index of contour in outline */
69 FT_UInt first; /* index of first point in contour */
70 FT_Int tag; /* current point's state */
77 return FT_THROW( Invalid_Outline );
79 if ( !func_interface )
80 return FT_THROW( Invalid_Argument );
82 shift = func_interface->shift;
83 delta = func_interface->delta;
86 for ( n = 0; n < outline->n_contours; n++ )
88 FT_Int last; /* index of last point in contour */
91 FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
93 last = outline->contours[n];
96 limit = outline->points + last;
98 v_start = outline->points[first];
99 v_start.x = SCALED( v_start.x );
100 v_start.y = SCALED( v_start.y );
102 v_last = outline->points[last];
103 v_last.x = SCALED( v_last.x );
104 v_last.y = SCALED( v_last.y );
108 point = outline->points + first;
109 tags = outline->tags + first;
110 tag = FT_CURVE_TAG( tags[0] );
112 /* A contour cannot start with a cubic control point! */
113 if ( tag == FT_CURVE_TAG_CUBIC )
114 goto Invalid_Outline;
116 /* check first point to determine origin */
117 if ( tag == FT_CURVE_TAG_CONIC )
119 /* first point is conic control. Yes, this happens. */
120 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
122 /* start at last point if it is on the curve */
128 /* if both first and last points are conic, */
129 /* start at their middle and record its position */
131 v_start.x = ( v_start.x + v_last.x ) / 2;
132 v_start.y = ( v_start.y + v_last.y ) / 2;
134 /* v_last = v_start; */
140 FT_TRACE5(( " move to (%.2f, %.2f)\n",
141 v_start.x / 64.0, v_start.y / 64.0 ));
142 error = func_interface->move_to( &v_start, user );
146 while ( point < limit )
151 tag = FT_CURVE_TAG( tags[0] );
154 case FT_CURVE_TAG_ON: /* emit a single line_to */
159 vec.x = SCALED( point->x );
160 vec.y = SCALED( point->y );
162 FT_TRACE5(( " line to (%.2f, %.2f)\n",
163 vec.x / 64.0, vec.y / 64.0 ));
164 error = func_interface->line_to( &vec, user );
170 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
171 v_control.x = SCALED( point->x );
172 v_control.y = SCALED( point->y );
183 tag = FT_CURVE_TAG( tags[0] );
185 vec.x = SCALED( point->x );
186 vec.y = SCALED( point->y );
188 if ( tag == FT_CURVE_TAG_ON )
190 FT_TRACE5(( " conic to (%.2f, %.2f)"
191 " with control (%.2f, %.2f)\n",
192 vec.x / 64.0, vec.y / 64.0,
193 v_control.x / 64.0, v_control.y / 64.0 ));
194 error = func_interface->conic_to( &v_control, &vec, user );
200 if ( tag != FT_CURVE_TAG_CONIC )
201 goto Invalid_Outline;
203 v_middle.x = ( v_control.x + vec.x ) / 2;
204 v_middle.y = ( v_control.y + vec.y ) / 2;
206 FT_TRACE5(( " conic to (%.2f, %.2f)"
207 " with control (%.2f, %.2f)\n",
208 v_middle.x / 64.0, v_middle.y / 64.0,
209 v_control.x / 64.0, v_control.y / 64.0 ));
210 error = func_interface->conic_to( &v_control, &v_middle, user );
218 FT_TRACE5(( " conic to (%.2f, %.2f)"
219 " with control (%.2f, %.2f)\n",
220 v_start.x / 64.0, v_start.y / 64.0,
221 v_control.x / 64.0, v_control.y / 64.0 ));
222 error = func_interface->conic_to( &v_control, &v_start, user );
225 default: /* FT_CURVE_TAG_CUBIC */
227 FT_Vector vec1, vec2;
230 if ( point + 1 > limit ||
231 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
232 goto Invalid_Outline;
237 vec1.x = SCALED( point[-2].x );
238 vec1.y = SCALED( point[-2].y );
240 vec2.x = SCALED( point[-1].x );
241 vec2.y = SCALED( point[-1].y );
243 if ( point <= limit )
248 vec.x = SCALED( point->x );
249 vec.y = SCALED( point->y );
251 FT_TRACE5(( " cubic to (%.2f, %.2f)"
252 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
253 vec.x / 64.0, vec.y / 64.0,
254 vec1.x / 64.0, vec1.y / 64.0,
255 vec2.x / 64.0, vec2.y / 64.0 ));
256 error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
262 FT_TRACE5(( " cubic to (%.2f, %.2f)"
263 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
264 v_start.x / 64.0, v_start.y / 64.0,
265 vec1.x / 64.0, vec1.y / 64.0,
266 vec2.x / 64.0, vec2.y / 64.0 ));
267 error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
273 /* close the contour with a line segment */
274 FT_TRACE5(( " line to (%.2f, %.2f)\n",
275 v_start.x / 64.0, v_start.y / 64.0 ));
276 error = func_interface->line_to( &v_start, user );
285 FT_TRACE5(( "FT_Outline_Decompose: Done\n", n ));
289 FT_TRACE5(( "FT_Outline_Decompose: Error %d\n", error ));
293 return FT_THROW( Invalid_Outline );
297 FT_EXPORT_DEF( FT_Error )
298 FT_Outline_New_Internal( FT_Memory memory,
301 FT_Outline *anoutline )
306 if ( !anoutline || !memory )
307 return FT_THROW( Invalid_Argument );
309 *anoutline = null_outline;
311 if ( numContours < 0 ||
312 (FT_UInt)numContours > numPoints )
313 return FT_THROW( Invalid_Argument );
315 if ( numPoints > FT_OUTLINE_POINTS_MAX )
316 return FT_THROW( Array_Too_Large );
318 if ( FT_NEW_ARRAY( anoutline->points, numPoints ) ||
319 FT_NEW_ARRAY( anoutline->tags, numPoints ) ||
320 FT_NEW_ARRAY( anoutline->contours, numContours ) )
323 anoutline->n_points = (FT_UShort)numPoints;
324 anoutline->n_contours = (FT_Short)numContours;
325 anoutline->flags |= FT_OUTLINE_OWNER;
330 anoutline->flags |= FT_OUTLINE_OWNER;
331 FT_Outline_Done_Internal( memory, anoutline );
337 /* documentation is in ftoutln.h */
339 FT_EXPORT_DEF( FT_Error )
340 FT_Outline_New( FT_Library library,
343 FT_Outline *anoutline )
346 return FT_THROW( Invalid_Library_Handle );
348 return FT_Outline_New_Internal( library->memory, numPoints,
349 numContours, anoutline );
353 /* documentation is in ftoutln.h */
355 FT_EXPORT_DEF( FT_Error )
356 FT_Outline_Check( FT_Outline* outline )
360 FT_Int n_points = outline->n_points;
361 FT_Int n_contours = outline->n_contours;
367 if ( n_points == 0 && n_contours == 0 )
370 /* check point and contour counts */
371 if ( n_points <= 0 || n_contours <= 0 )
375 for ( n = 0; n < n_contours; n++ )
377 end = outline->contours[n];
379 /* note that we don't accept empty contours */
380 if ( end <= end0 || end >= n_points )
386 if ( end != n_points - 1 )
389 /* XXX: check the tags array */
394 return FT_THROW( Invalid_Argument );
398 /* documentation is in ftoutln.h */
400 FT_EXPORT_DEF( FT_Error )
401 FT_Outline_Copy( const FT_Outline* source,
407 if ( !source || !target )
408 return FT_THROW( Invalid_Outline );
410 if ( source->n_points != target->n_points ||
411 source->n_contours != target->n_contours )
412 return FT_THROW( Invalid_Argument );
414 if ( source == target )
417 FT_ARRAY_COPY( target->points, source->points, source->n_points );
419 FT_ARRAY_COPY( target->tags, source->tags, source->n_points );
421 FT_ARRAY_COPY( target->contours, source->contours, source->n_contours );
423 /* copy all flags, except the `FT_OUTLINE_OWNER' one */
424 is_owner = target->flags & FT_OUTLINE_OWNER;
425 target->flags = source->flags;
427 target->flags &= ~FT_OUTLINE_OWNER;
428 target->flags |= is_owner;
434 FT_EXPORT_DEF( FT_Error )
435 FT_Outline_Done_Internal( FT_Memory memory,
436 FT_Outline* outline )
439 return FT_THROW( Invalid_Outline );
442 return FT_THROW( Invalid_Argument );
444 if ( outline->flags & FT_OUTLINE_OWNER )
446 FT_FREE( outline->points );
447 FT_FREE( outline->tags );
448 FT_FREE( outline->contours );
450 *outline = null_outline;
456 /* documentation is in ftoutln.h */
458 FT_EXPORT_DEF( FT_Error )
459 FT_Outline_Done( FT_Library library,
460 FT_Outline* outline )
462 /* check for valid `outline' in FT_Outline_Done_Internal() */
465 return FT_THROW( Invalid_Library_Handle );
467 return FT_Outline_Done_Internal( library->memory, outline );
471 /* documentation is in ftoutln.h */
473 FT_EXPORT_DEF( void )
474 FT_Outline_Get_CBox( const FT_Outline* outline,
477 FT_Pos xMin, yMin, xMax, yMax;
480 if ( outline && acbox )
482 if ( outline->n_points == 0 )
491 FT_Vector* vec = outline->points;
492 FT_Vector* limit = vec + outline->n_points;
495 xMin = xMax = vec->x;
496 yMin = yMax = vec->y;
499 for ( ; vec < limit; vec++ )
505 if ( x < xMin ) xMin = x;
506 if ( x > xMax ) xMax = x;
509 if ( y < yMin ) yMin = y;
510 if ( y > yMax ) yMax = y;
521 /* documentation is in ftoutln.h */
523 FT_EXPORT_DEF( void )
524 FT_Outline_Translate( const FT_Outline* outline,
535 vec = outline->points;
537 for ( n = 0; n < outline->n_points; n++ )
546 /* documentation is in ftoutln.h */
548 FT_EXPORT_DEF( void )
549 FT_Outline_Reverse( FT_Outline* outline )
560 for ( n = 0; n < outline->n_contours; n++ )
562 last = outline->contours[n];
564 /* reverse point table */
566 FT_Vector* p = outline->points + first;
567 FT_Vector* q = outline->points + last;
581 /* reverse tags table */
583 char* p = outline->tags + first;
584 char* q = outline->tags + last;
603 outline->flags ^= FT_OUTLINE_REVERSE_FILL;
607 /* documentation is in ftoutln.h */
609 FT_EXPORT_DEF( FT_Error )
610 FT_Outline_Render( FT_Library library,
612 FT_Raster_Params* params )
615 FT_Bool update = FALSE;
616 FT_Renderer renderer;
621 return FT_THROW( Invalid_Library_Handle );
624 return FT_THROW( Invalid_Outline );
627 return FT_THROW( Invalid_Argument );
629 renderer = library->cur_renderer;
630 node = library->renderers.head;
632 params->source = (void*)outline;
634 error = FT_ERR( Cannot_Render_Glyph );
637 error = renderer->raster_render( renderer->raster, params );
638 if ( !error || FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
641 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
642 /* is unsupported by the current renderer for this glyph image */
645 /* now, look for another renderer that supports the same */
647 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
652 /* if we changed the current renderer for the glyph image format */
653 /* we need to select it as the next current one */
654 if ( !error && update && renderer )
655 error = FT_Set_Renderer( library, renderer, 0, 0 );
661 /* documentation is in ftoutln.h */
663 FT_EXPORT_DEF( FT_Error )
664 FT_Outline_Get_Bitmap( FT_Library library,
666 const FT_Bitmap *abitmap )
668 FT_Raster_Params params;
672 return FT_THROW( Invalid_Argument );
674 /* other checks are delayed to `FT_Outline_Render' */
676 params.target = abitmap;
679 if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY ||
680 abitmap->pixel_mode == FT_PIXEL_MODE_LCD ||
681 abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
682 params.flags |= FT_RASTER_FLAG_AA;
684 return FT_Outline_Render( library, outline, ¶ms );
688 /* documentation is in freetype.h */
690 FT_EXPORT_DEF( void )
691 FT_Vector_Transform( FT_Vector* vector,
692 const FT_Matrix* matrix )
697 if ( !vector || !matrix )
700 xz = FT_MulFix( vector->x, matrix->xx ) +
701 FT_MulFix( vector->y, matrix->xy );
703 yz = FT_MulFix( vector->x, matrix->yx ) +
704 FT_MulFix( vector->y, matrix->yy );
711 /* documentation is in ftoutln.h */
713 FT_EXPORT_DEF( void )
714 FT_Outline_Transform( const FT_Outline* outline,
715 const FT_Matrix* matrix )
721 if ( !outline || !matrix )
724 vec = outline->points;
725 limit = vec + outline->n_points;
727 for ( ; vec < limit; vec++ )
728 FT_Vector_Transform( vec, matrix );
734 #define FT_OUTLINE_GET_CONTOUR( outline, c, first, last ) \
737 (first) = ( c > 0 ) ? (outline)->points + \
738 (outline)->contours[c - 1] + 1 \
739 : (outline)->points; \
740 (last) = (outline)->points + (outline)->contours[c]; \
744 /* Is a point in some contour? */
746 /* We treat every point of the contour as if it */
747 /* it were ON. That is, we allow false positives, */
748 /* but disallow false negatives. (XXX really?) */
750 ft_contour_has( FT_Outline* outline,
761 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
763 for ( a = first; a <= last; a++ )
769 b = ( a == last ) ? first : a + 1;
771 intersect = ( a->y - point->y ) ^ ( b->y - point->y );
773 /* a and b are on the same side */
774 if ( intersect >= 0 )
776 if ( intersect == 0 && a->y == point->y )
778 if ( ( a->x <= point->x && b->x >= point->x ) ||
779 ( a->x >= point->x && b->x <= point->x ) )
786 x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
790 else if ( x == point->x )
799 ft_contour_enclosed( FT_Outline* outline,
807 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
809 for ( i = 0; i < outline->n_contours; i++ )
811 if ( i != c && ft_contour_has( outline, i, first ) )
816 for ( pt = first + 1; pt <= last; pt++ )
817 if ( !ft_contour_has( outline, i, pt ) )
828 /* This version differs from the public one in that each */
829 /* part (contour not enclosed in another contour) of the */
830 /* outline is checked for orientation. This is */
831 /* necessary for some buggy CJK fonts. */
832 static FT_Orientation
833 ft_outline_get_orientation( FT_Outline* outline )
838 FT_Orientation orient = FT_ORIENTATION_NONE;
841 first = outline->points;
842 for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
845 FT_Vector* xmin_point;
849 last = outline->points + outline->contours[i];
851 /* skip degenerate contours */
852 if ( last < first + 2 )
855 if ( ft_contour_enclosed( outline, i ) )
861 for ( point = first + 1; point <= last; point++ )
863 if ( point->x < xmin )
870 /* check the orientation of the contour */
877 prev = ( xmin_point == first ) ? last : xmin_point - 1;
878 next = ( xmin_point == last ) ? first : xmin_point + 1;
880 if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
881 FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
882 o = FT_ORIENTATION_POSTSCRIPT;
884 o = FT_ORIENTATION_TRUETYPE;
886 if ( orient == FT_ORIENTATION_NONE )
888 else if ( orient != o )
889 return FT_ORIENTATION_NONE;
899 /* documentation is in ftoutln.h */
901 FT_EXPORT_DEF( FT_Error )
902 FT_Outline_Embolden( FT_Outline* outline,
905 return FT_Outline_EmboldenXY( outline, strength, strength );
909 /* documentation is in ftoutln.h */
911 FT_EXPORT_DEF( FT_Error )
912 FT_Outline_EmboldenXY( FT_Outline* outline,
917 FT_Vector v_prev, v_first, v_next, v_cur;
923 return FT_THROW( Invalid_Outline );
927 if ( xstrength == 0 && ystrength == 0 )
930 orientation = FT_Outline_Get_Orientation( outline );
931 if ( orientation == FT_ORIENTATION_NONE )
933 if ( outline->n_contours )
934 return FT_THROW( Invalid_Argument );
939 points = outline->points;
942 for ( c = 0; c < outline->n_contours; c++ )
944 FT_Vector in, out, shift;
945 FT_Fixed l_in, l_out, l, q, d;
946 int last = outline->contours[c];
949 v_first = points[first];
950 v_prev = points[last];
953 /* compute incoming normalized vector */
954 in.x = v_cur.x - v_prev.x;
955 in.y = v_cur.y - v_prev.y;
956 l_in = FT_Vector_Length( &in );
959 in.x = FT_DivFix( in.x, l_in );
960 in.y = FT_DivFix( in.y, l_in );
963 for ( n = first; n <= last; n++ )
966 v_next = points[n + 1];
970 /* compute outgoing normalized vector */
971 out.x = v_next.x - v_cur.x;
972 out.y = v_next.y - v_cur.y;
973 l_out = FT_Vector_Length( &out );
976 out.x = FT_DivFix( out.x, l_out );
977 out.y = FT_DivFix( out.y, l_out );
980 d = FT_MulFix( in.x, out.x ) + FT_MulFix( in.y, out.y );
982 /* shift only if turn is less than ~160 degrees */
987 /* shift components are aligned along lateral bisector */
988 /* and directed according to the outline orientation. */
989 shift.x = in.y + out.y;
990 shift.y = in.x + out.x;
992 if ( orientation == FT_ORIENTATION_TRUETYPE )
997 /* restrict shift magnitude to better handle collapsing segments */
998 q = FT_MulFix( out.x, in.y ) - FT_MulFix( out.y, in.x );
999 if ( orientation == FT_ORIENTATION_TRUETYPE )
1002 l = FT_MIN( l_in, l_out );
1004 /* non-strict inequalities avoid divide-by-zero when q == l == 0 */
1005 if ( FT_MulFix( xstrength, q ) <= FT_MulFix( d, l ) )
1006 shift.x = FT_MulDiv( shift.x, xstrength, d );
1008 shift.x = FT_MulDiv( shift.x, l, q );
1011 if ( FT_MulFix( ystrength, q ) <= FT_MulFix( d, l ) )
1012 shift.y = FT_MulDiv( shift.y, ystrength, d );
1014 shift.y = FT_MulDiv( shift.y, l, q );
1017 shift.x = shift.y = 0;
1019 outline->points[n].x = v_cur.x + xstrength + shift.x;
1020 outline->points[n].y = v_cur.y + ystrength + shift.y;
1034 /* documentation is in ftoutln.h */
1036 FT_EXPORT_DEF( FT_Orientation )
1037 FT_Outline_Get_Orientation( FT_Outline* outline )
1040 FT_Int xshift, yshift;
1042 FT_Vector v_prev, v_cur;
1047 if ( !outline || outline->n_points <= 0 )
1048 return FT_ORIENTATION_TRUETYPE;
1050 /* We use the nonzero winding rule to find the orientation. */
1051 /* Since glyph outlines behave much more `regular' than arbitrary */
1052 /* cubic or quadratic curves, this test deals with the polygon */
1053 /* only which is spanned up by the control points. */
1055 FT_Outline_Get_CBox( outline, &cbox );
1057 /* Handle collapsed outlines to avoid undefined FT_MSB. */
1058 if ( cbox.xMin == cbox.xMax || cbox.yMin == cbox.yMax )
1059 return FT_ORIENTATION_NONE;
1061 xshift = FT_MSB( FT_ABS( cbox.xMax ) | FT_ABS( cbox.xMin ) ) - 14;
1062 xshift = FT_MAX( xshift, 0 );
1064 yshift = FT_MSB( cbox.yMax - cbox.yMin ) - 14;
1065 yshift = FT_MAX( yshift, 0 );
1067 points = outline->points;
1070 for ( c = 0; c < outline->n_contours; c++ )
1072 FT_Int last = outline->contours[c];
1075 v_prev = points[last];
1077 for ( n = first; n <= last; n++ )
1080 area += ( ( v_cur.y - v_prev.y ) >> yshift ) *
1081 ( ( v_cur.x + v_prev.x ) >> xshift );
1089 return FT_ORIENTATION_POSTSCRIPT;
1090 else if ( area < 0 )
1091 return FT_ORIENTATION_TRUETYPE;
1093 return FT_ORIENTATION_NONE;