1 /****************************************************************************
5 * FreeType outline management (body).
7 * Copyright (C) 1996-2020 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.
19 #include <freetype/ftoutln.h>
20 #include <freetype/internal/ftobjs.h>
21 #include <freetype/internal/ftcalc.h>
22 #include <freetype/internal/ftdebug.h>
23 #include <freetype/fttrigon.h>
26 /**************************************************************************
28 * The macro FT_COMPONENT is used in trace mode. It is an implicit
29 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
30 * messages during execution.
33 #define FT_COMPONENT outline
37 const FT_Outline null_outline = { 0, 0, NULL, NULL, NULL, 0 };
40 /* documentation is in ftoutln.h */
42 FT_EXPORT_DEF( FT_Error )
43 FT_Outline_Decompose( FT_Outline* outline,
44 const FT_Outline_Funcs* func_interface,
48 #define SCALED( x ) ( (x) * ( 1L << shift ) - delta )
60 FT_Int n; /* index of contour in outline */
61 FT_UInt first; /* index of first point in contour */
62 FT_Int tag; /* current point's state */
69 return FT_THROW( Invalid_Outline );
71 if ( !func_interface )
72 return FT_THROW( Invalid_Argument );
74 shift = func_interface->shift;
75 delta = func_interface->delta;
78 for ( n = 0; n < outline->n_contours; n++ )
80 FT_Int last; /* index of last point in contour */
83 FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
85 last = outline->contours[n];
88 limit = outline->points + last;
90 v_start = outline->points[first];
91 v_start.x = SCALED( v_start.x );
92 v_start.y = SCALED( v_start.y );
94 v_last = outline->points[last];
95 v_last.x = SCALED( v_last.x );
96 v_last.y = SCALED( v_last.y );
100 point = outline->points + first;
101 tags = outline->tags + first;
102 tag = FT_CURVE_TAG( tags[0] );
104 /* A contour cannot start with a cubic control point! */
105 if ( tag == FT_CURVE_TAG_CUBIC )
106 goto Invalid_Outline;
108 /* check first point to determine origin */
109 if ( tag == FT_CURVE_TAG_CONIC )
111 /* first point is conic control. Yes, this happens. */
112 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
114 /* start at last point if it is on the curve */
120 /* if both first and last points are conic, */
121 /* start at their middle and record its position */
123 v_start.x = ( v_start.x + v_last.x ) / 2;
124 v_start.y = ( v_start.y + v_last.y ) / 2;
126 /* v_last = v_start; */
132 FT_TRACE5(( " move to (%.2f, %.2f)\n",
133 v_start.x / 64.0, v_start.y / 64.0 ));
134 error = func_interface->move_to( &v_start, user );
138 while ( point < limit )
143 tag = FT_CURVE_TAG( tags[0] );
146 case FT_CURVE_TAG_ON: /* emit a single line_to */
151 vec.x = SCALED( point->x );
152 vec.y = SCALED( point->y );
154 FT_TRACE5(( " line to (%.2f, %.2f)\n",
155 vec.x / 64.0, vec.y / 64.0 ));
156 error = func_interface->line_to( &vec, user );
162 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
163 v_control.x = SCALED( point->x );
164 v_control.y = SCALED( point->y );
175 tag = FT_CURVE_TAG( tags[0] );
177 vec.x = SCALED( point->x );
178 vec.y = SCALED( point->y );
180 if ( tag == FT_CURVE_TAG_ON )
182 FT_TRACE5(( " conic to (%.2f, %.2f)"
183 " with control (%.2f, %.2f)\n",
184 vec.x / 64.0, vec.y / 64.0,
185 v_control.x / 64.0, v_control.y / 64.0 ));
186 error = func_interface->conic_to( &v_control, &vec, user );
192 if ( tag != FT_CURVE_TAG_CONIC )
193 goto Invalid_Outline;
195 v_middle.x = ( v_control.x + vec.x ) / 2;
196 v_middle.y = ( v_control.y + vec.y ) / 2;
198 FT_TRACE5(( " conic to (%.2f, %.2f)"
199 " with control (%.2f, %.2f)\n",
200 v_middle.x / 64.0, v_middle.y / 64.0,
201 v_control.x / 64.0, v_control.y / 64.0 ));
202 error = func_interface->conic_to( &v_control, &v_middle, user );
210 FT_TRACE5(( " conic to (%.2f, %.2f)"
211 " with control (%.2f, %.2f)\n",
212 v_start.x / 64.0, v_start.y / 64.0,
213 v_control.x / 64.0, v_control.y / 64.0 ));
214 error = func_interface->conic_to( &v_control, &v_start, user );
217 default: /* FT_CURVE_TAG_CUBIC */
219 FT_Vector vec1, vec2;
222 if ( point + 1 > limit ||
223 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
224 goto Invalid_Outline;
229 vec1.x = SCALED( point[-2].x );
230 vec1.y = SCALED( point[-2].y );
232 vec2.x = SCALED( point[-1].x );
233 vec2.y = SCALED( point[-1].y );
235 if ( point <= limit )
240 vec.x = SCALED( point->x );
241 vec.y = SCALED( point->y );
243 FT_TRACE5(( " cubic to (%.2f, %.2f)"
244 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
245 vec.x / 64.0, vec.y / 64.0,
246 vec1.x / 64.0, vec1.y / 64.0,
247 vec2.x / 64.0, vec2.y / 64.0 ));
248 error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
254 FT_TRACE5(( " cubic to (%.2f, %.2f)"
255 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
256 v_start.x / 64.0, v_start.y / 64.0,
257 vec1.x / 64.0, vec1.y / 64.0,
258 vec2.x / 64.0, vec2.y / 64.0 ));
259 error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
265 /* close the contour with a line segment */
266 FT_TRACE5(( " line to (%.2f, %.2f)\n",
267 v_start.x / 64.0, v_start.y / 64.0 ));
268 error = func_interface->line_to( &v_start, user );
274 first = (FT_UInt)last + 1;
277 FT_TRACE5(( "FT_Outline_Decompose: Done\n" ));
281 error = FT_THROW( Invalid_Outline );
285 FT_TRACE5(( "FT_Outline_Decompose: Error 0x%x\n", error ));
290 /* documentation is in ftoutln.h */
292 FT_EXPORT_DEF( FT_Error )
293 FT_Outline_New( FT_Library library,
296 FT_Outline *anoutline )
303 return FT_THROW( Invalid_Library_Handle );
305 memory = library->memory;
307 if ( !anoutline || !memory )
308 return FT_THROW( Invalid_Argument );
310 *anoutline = null_outline;
312 if ( numContours < 0 ||
313 (FT_UInt)numContours > numPoints )
314 return FT_THROW( Invalid_Argument );
316 if ( numPoints > FT_OUTLINE_POINTS_MAX )
317 return FT_THROW( Array_Too_Large );
319 if ( FT_NEW_ARRAY( anoutline->points, numPoints ) ||
320 FT_NEW_ARRAY( anoutline->tags, numPoints ) ||
321 FT_NEW_ARRAY( anoutline->contours, numContours ) )
324 anoutline->n_points = (FT_Short)numPoints;
325 anoutline->n_contours = (FT_Short)numContours;
326 anoutline->flags |= FT_OUTLINE_OWNER;
331 anoutline->flags |= FT_OUTLINE_OWNER;
332 FT_Outline_Done( library, anoutline );
338 /* documentation is in ftoutln.h */
340 FT_EXPORT_DEF( FT_Error )
341 FT_Outline_Check( FT_Outline* outline )
345 FT_Int n_points = outline->n_points;
346 FT_Int n_contours = outline->n_contours;
352 if ( n_points == 0 && n_contours == 0 )
355 /* check point and contour counts */
356 if ( n_points <= 0 || n_contours <= 0 )
360 for ( n = 0; n < n_contours; n++ )
362 end = outline->contours[n];
364 /* note that we don't accept empty contours */
365 if ( end <= end0 || end >= n_points )
371 if ( end != n_points - 1 )
374 /* XXX: check the tags array */
379 return FT_THROW( Invalid_Argument );
383 /* documentation is in ftoutln.h */
385 FT_EXPORT_DEF( FT_Error )
386 FT_Outline_Copy( const FT_Outline* source,
392 if ( !source || !target )
393 return FT_THROW( Invalid_Outline );
395 if ( source->n_points != target->n_points ||
396 source->n_contours != target->n_contours )
397 return FT_THROW( Invalid_Argument );
399 if ( source == target )
402 if ( source->n_points )
404 FT_ARRAY_COPY( target->points, source->points, source->n_points );
405 FT_ARRAY_COPY( target->tags, source->tags, source->n_points );
408 if ( source->n_contours )
409 FT_ARRAY_COPY( target->contours, source->contours, source->n_contours );
411 /* copy all flags, except the `FT_OUTLINE_OWNER' one */
412 is_owner = target->flags & FT_OUTLINE_OWNER;
413 target->flags = source->flags;
415 target->flags &= ~FT_OUTLINE_OWNER;
416 target->flags |= is_owner;
422 /* documentation is in ftoutln.h */
424 FT_EXPORT_DEF( FT_Error )
425 FT_Outline_Done( FT_Library library,
426 FT_Outline* outline )
432 return FT_THROW( Invalid_Library_Handle );
435 return FT_THROW( Invalid_Outline );
437 memory = library->memory;
440 return FT_THROW( Invalid_Argument );
442 if ( outline->flags & FT_OUTLINE_OWNER )
444 FT_FREE( outline->points );
445 FT_FREE( outline->tags );
446 FT_FREE( outline->contours );
448 *outline = null_outline;
454 /* documentation is in ftoutln.h */
456 FT_EXPORT_DEF( void )
457 FT_Outline_Get_CBox( const FT_Outline* outline,
460 FT_Pos xMin, yMin, xMax, yMax;
463 if ( outline && acbox )
465 if ( outline->n_points == 0 )
474 FT_Vector* vec = outline->points;
475 FT_Vector* limit = vec + outline->n_points;
478 xMin = xMax = vec->x;
479 yMin = yMax = vec->y;
482 for ( ; vec < limit; vec++ )
488 if ( x < xMin ) xMin = x;
489 if ( x > xMax ) xMax = x;
492 if ( y < yMin ) yMin = y;
493 if ( y > yMax ) yMax = y;
504 /* documentation is in ftoutln.h */
506 FT_EXPORT_DEF( void )
507 FT_Outline_Translate( const FT_Outline* outline,
518 vec = outline->points;
520 for ( n = 0; n < outline->n_points; n++ )
522 vec->x = ADD_LONG( vec->x, xOffset );
523 vec->y = ADD_LONG( vec->y, yOffset );
529 /* documentation is in ftoutln.h */
531 FT_EXPORT_DEF( void )
532 FT_Outline_Reverse( FT_Outline* outline )
543 for ( n = 0; n < outline->n_contours; n++ )
545 last = outline->contours[n];
547 /* reverse point table */
549 FT_Vector* p = outline->points + first;
550 FT_Vector* q = outline->points + last;
564 /* reverse tags table */
566 char* p = outline->tags + first;
567 char* q = outline->tags + last;
586 outline->flags ^= FT_OUTLINE_REVERSE_FILL;
590 /* documentation is in ftoutln.h */
592 FT_EXPORT_DEF( FT_Error )
593 FT_Outline_Render( FT_Library library,
595 FT_Raster_Params* params )
598 FT_Renderer renderer;
604 return FT_THROW( Invalid_Library_Handle );
607 return FT_THROW( Invalid_Outline );
610 return FT_THROW( Invalid_Argument );
612 FT_Outline_Get_CBox( outline, &cbox );
613 if ( cbox.xMin < -0x1000000L || cbox.yMin < -0x1000000L ||
614 cbox.xMax > 0x1000000L || cbox.yMax > 0x1000000L )
615 return FT_THROW( Invalid_Outline );
617 renderer = library->cur_renderer;
618 node = library->renderers.head;
620 params->source = (void*)outline;
622 /* preset clip_box for direct mode */
623 if ( params->flags & FT_RASTER_FLAG_DIRECT &&
624 !( params->flags & FT_RASTER_FLAG_CLIP ) )
626 params->clip_box.xMin = cbox.xMin >> 6;
627 params->clip_box.yMin = cbox.yMin >> 6;
628 params->clip_box.xMax = ( cbox.xMax + 63 ) >> 6;
629 params->clip_box.yMax = ( cbox.yMax + 63 ) >> 6;
632 error = FT_ERR( Cannot_Render_Glyph );
635 error = renderer->raster_render( renderer->raster, params );
636 if ( !error || FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
639 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
640 /* is unsupported by the current renderer for this glyph image */
643 /* now, look for another renderer that supports the same */
645 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
653 /* documentation is in ftoutln.h */
655 FT_EXPORT_DEF( FT_Error )
656 FT_Outline_Get_Bitmap( FT_Library library,
658 const FT_Bitmap *abitmap )
660 FT_Raster_Params params;
664 return FT_THROW( Invalid_Argument );
666 /* other checks are delayed to `FT_Outline_Render' */
668 params.target = abitmap;
671 if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY ||
672 abitmap->pixel_mode == FT_PIXEL_MODE_LCD ||
673 abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
674 params.flags |= FT_RASTER_FLAG_AA;
676 return FT_Outline_Render( library, outline, ¶ms );
680 /* documentation is in freetype.h */
682 FT_EXPORT_DEF( void )
683 FT_Vector_Transform( FT_Vector* vector,
684 const FT_Matrix* matrix )
689 if ( !vector || !matrix )
692 xz = FT_MulFix( vector->x, matrix->xx ) +
693 FT_MulFix( vector->y, matrix->xy );
695 yz = FT_MulFix( vector->x, matrix->yx ) +
696 FT_MulFix( vector->y, matrix->yy );
703 /* documentation is in ftoutln.h */
705 FT_EXPORT_DEF( void )
706 FT_Outline_Transform( const FT_Outline* outline,
707 const FT_Matrix* matrix )
713 if ( !outline || !matrix || !outline->points )
716 vec = outline->points;
717 limit = vec + outline->n_points;
719 for ( ; vec < limit; vec++ )
720 FT_Vector_Transform( vec, matrix );
726 #define FT_OUTLINE_GET_CONTOUR( outline, c, first, last ) \
729 (first) = ( c > 0 ) ? (outline)->points + \
730 (outline)->contours[c - 1] + 1 \
731 : (outline)->points; \
732 (last) = (outline)->points + (outline)->contours[c]; \
736 /* Is a point in some contour? */
738 /* We treat every point of the contour as if it */
739 /* it were ON. That is, we allow false positives, */
740 /* but disallow false negatives. (XXX really?) */
742 ft_contour_has( FT_Outline* outline,
753 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
755 for ( a = first; a <= last; a++ )
761 b = ( a == last ) ? first : a + 1;
763 intersect = ( a->y - point->y ) ^ ( b->y - point->y );
765 /* a and b are on the same side */
766 if ( intersect >= 0 )
768 if ( intersect == 0 && a->y == point->y )
770 if ( ( a->x <= point->x && b->x >= point->x ) ||
771 ( a->x >= point->x && b->x <= point->x ) )
778 x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
782 else if ( x == point->x )
791 ft_contour_enclosed( FT_Outline* outline,
799 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
801 for ( i = 0; i < outline->n_contours; i++ )
803 if ( i != c && ft_contour_has( outline, i, first ) )
808 for ( pt = first + 1; pt <= last; pt++ )
809 if ( !ft_contour_has( outline, i, pt ) )
820 /* This version differs from the public one in that each */
821 /* part (contour not enclosed in another contour) of the */
822 /* outline is checked for orientation. This is */
823 /* necessary for some buggy CJK fonts. */
824 static FT_Orientation
825 ft_outline_get_orientation( FT_Outline* outline )
830 FT_Orientation orient = FT_ORIENTATION_NONE;
833 first = outline->points;
834 for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
837 FT_Vector* xmin_point;
841 last = outline->points + outline->contours[i];
843 /* skip degenerate contours */
844 if ( last < first + 2 )
847 if ( ft_contour_enclosed( outline, i ) )
853 for ( point = first + 1; point <= last; point++ )
855 if ( point->x < xmin )
862 /* check the orientation of the contour */
869 prev = ( xmin_point == first ) ? last : xmin_point - 1;
870 next = ( xmin_point == last ) ? first : xmin_point + 1;
872 if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
873 FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
874 o = FT_ORIENTATION_POSTSCRIPT;
876 o = FT_ORIENTATION_TRUETYPE;
878 if ( orient == FT_ORIENTATION_NONE )
880 else if ( orient != o )
881 return FT_ORIENTATION_NONE;
891 /* documentation is in ftoutln.h */
893 FT_EXPORT_DEF( FT_Error )
894 FT_Outline_Embolden( FT_Outline* outline,
897 return FT_Outline_EmboldenXY( outline, strength, strength );
901 /* documentation is in ftoutln.h */
903 FT_EXPORT_DEF( FT_Error )
904 FT_Outline_EmboldenXY( FT_Outline* outline,
909 FT_Int c, first, last;
910 FT_Orientation orientation;
914 return FT_THROW( Invalid_Outline );
918 if ( xstrength == 0 && ystrength == 0 )
921 orientation = FT_Outline_Get_Orientation( outline );
922 if ( orientation == FT_ORIENTATION_NONE )
924 if ( outline->n_contours )
925 return FT_THROW( Invalid_Argument );
930 points = outline->points;
933 for ( c = 0; c < outline->n_contours; c++ )
935 FT_Vector in, out, anchor, shift;
936 FT_Fixed l_in, l_out, l_anchor = 0, l, q, d;
941 last = outline->contours[c];
943 /* pacify compiler */
944 in.x = in.y = anchor.x = anchor.y = 0;
946 /* Counter j cycles though the points; counter i advances only */
947 /* when points are moved; anchor k marks the first moved point. */
948 for ( i = last, j = first, k = -1;
950 j = j < last ? j + 1 : first )
954 out.x = points[j].x - points[i].x;
955 out.y = points[j].y - points[i].y;
956 l_out = (FT_Fixed)FT_Vector_NormLen( &out );
976 d = FT_MulFix( in.x, out.x ) + FT_MulFix( in.y, out.y );
978 /* shift only if turn is less than ~160 degrees */
983 /* shift components along lateral bisector in proper orientation */
984 shift.x = in.y + out.y;
985 shift.y = in.x + out.x;
987 if ( orientation == FT_ORIENTATION_TRUETYPE )
992 /* restrict shift magnitude to better handle collapsing segments */
993 q = FT_MulFix( out.x, in.y ) - FT_MulFix( out.y, in.x );
994 if ( orientation == FT_ORIENTATION_TRUETYPE )
997 l = FT_MIN( l_in, l_out );
999 /* non-strict inequalities avoid divide-by-zero when q == l == 0 */
1000 if ( FT_MulFix( xstrength, q ) <= FT_MulFix( l, d ) )
1001 shift.x = FT_MulDiv( shift.x, xstrength, d );
1003 shift.x = FT_MulDiv( shift.x, l, q );
1006 if ( FT_MulFix( ystrength, q ) <= FT_MulFix( l, d ) )
1007 shift.y = FT_MulDiv( shift.y, ystrength, d );
1009 shift.y = FT_MulDiv( shift.y, l, q );
1012 shift.x = shift.y = 0;
1016 i = i < last ? i + 1 : first )
1018 points[i].x += xstrength + shift.x;
1019 points[i].y += ystrength + shift.y;
1036 /* documentation is in ftoutln.h */
1038 FT_EXPORT_DEF( FT_Orientation )
1039 FT_Outline_Get_Orientation( FT_Outline* outline )
1041 FT_BBox cbox = { 0, 0, 0, 0 };
1042 FT_Int xshift, yshift;
1044 FT_Vector v_prev, v_cur;
1049 if ( !outline || outline->n_points <= 0 )
1050 return FT_ORIENTATION_TRUETYPE;
1052 /* We use the nonzero winding rule to find the orientation. */
1053 /* Since glyph outlines behave much more `regular' than arbitrary */
1054 /* cubic or quadratic curves, this test deals with the polygon */
1055 /* only that is spanned up by the control points. */
1057 FT_Outline_Get_CBox( outline, &cbox );
1059 /* Handle collapsed outlines to avoid undefined FT_MSB. */
1060 if ( cbox.xMin == cbox.xMax || cbox.yMin == cbox.yMax )
1061 return FT_ORIENTATION_NONE;
1063 /* Reject values large outlines. */
1064 if ( cbox.xMin < -0x1000000L || cbox.yMin < -0x1000000L ||
1065 cbox.xMax > 0x1000000L || cbox.yMax > 0x1000000L )
1066 return FT_ORIENTATION_NONE;
1068 xshift = FT_MSB( (FT_UInt32)( FT_ABS( cbox.xMax ) |
1069 FT_ABS( cbox.xMin ) ) ) - 14;
1070 xshift = FT_MAX( xshift, 0 );
1072 yshift = FT_MSB( (FT_UInt32)( cbox.yMax - cbox.yMin ) ) - 14;
1073 yshift = FT_MAX( yshift, 0 );
1075 points = outline->points;
1078 for ( c = 0; c < outline->n_contours; c++ )
1080 FT_Int last = outline->contours[c];
1083 v_prev.x = points[last].x >> xshift;
1084 v_prev.y = points[last].y >> yshift;
1086 for ( n = first; n <= last; n++ )
1088 v_cur.x = points[n].x >> xshift;
1089 v_cur.y = points[n].y >> yshift;
1091 area = ADD_LONG( area,
1092 MUL_LONG( v_cur.y - v_prev.y,
1093 v_cur.x + v_prev.x ) );
1102 return FT_ORIENTATION_POSTSCRIPT;
1103 else if ( area < 0 )
1104 return FT_ORIENTATION_TRUETYPE;
1106 return FT_ORIENTATION_NONE;