1 /****************************************************************************
5 * FreeType outline management (body).
7 * Copyright (C) 1996-2023 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_Int first; /* index of first point in contour */
62 FT_Int last; /* index of last point in contour */
64 FT_Int tag; /* current point's state */
71 return FT_THROW( Invalid_Outline );
73 if ( !func_interface )
74 return FT_THROW( Invalid_Argument );
76 shift = func_interface->shift;
77 delta = func_interface->delta;
80 for ( n = 0; n < outline->n_contours; n++ )
82 FT_TRACE5(( "FT_Outline_Decompose: Contour %d\n", n ));
85 last = outline->contours[n];
89 limit = outline->points + last;
91 v_start = outline->points[first];
92 v_start.x = SCALED( v_start.x );
93 v_start.y = SCALED( v_start.y );
95 v_last = outline->points[last];
96 v_last.x = SCALED( v_last.x );
97 v_last.y = SCALED( v_last.y );
101 point = outline->points + first;
102 tags = outline->tags + first;
103 tag = FT_CURVE_TAG( tags[0] );
105 /* A contour cannot start with a cubic control point! */
106 if ( tag == FT_CURVE_TAG_CUBIC )
107 goto Invalid_Outline;
109 /* check first point to determine origin */
110 if ( tag == FT_CURVE_TAG_CONIC )
112 /* first point is conic control. Yes, this happens. */
113 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
115 /* start at last point if it is on the curve */
121 /* if both first and last points are conic, */
122 /* start at their middle and record its position */
124 v_start.x = ( v_start.x + v_last.x ) / 2;
125 v_start.y = ( v_start.y + v_last.y ) / 2;
127 /* v_last = v_start; */
133 FT_TRACE5(( " move to (%.2f, %.2f)\n",
134 (double)v_start.x / 64, (double)v_start.y / 64 ));
135 error = func_interface->move_to( &v_start, user );
139 while ( point < limit )
144 tag = FT_CURVE_TAG( tags[0] );
147 case FT_CURVE_TAG_ON: /* emit a single line_to */
152 vec.x = SCALED( point->x );
153 vec.y = SCALED( point->y );
155 FT_TRACE5(( " line to (%.2f, %.2f)\n",
156 (double)vec.x / 64, (double)vec.y / 64 ));
157 error = func_interface->line_to( &vec, user );
163 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
164 v_control.x = SCALED( point->x );
165 v_control.y = SCALED( point->y );
176 tag = FT_CURVE_TAG( tags[0] );
178 vec.x = SCALED( point->x );
179 vec.y = SCALED( point->y );
181 if ( tag == FT_CURVE_TAG_ON )
183 FT_TRACE5(( " conic to (%.2f, %.2f)"
184 " with control (%.2f, %.2f)\n",
187 (double)v_control.x / 64,
188 (double)v_control.y / 64 ));
189 error = func_interface->conic_to( &v_control, &vec, user );
195 if ( tag != FT_CURVE_TAG_CONIC )
196 goto Invalid_Outline;
198 v_middle.x = ( v_control.x + vec.x ) / 2;
199 v_middle.y = ( v_control.y + vec.y ) / 2;
201 FT_TRACE5(( " conic to (%.2f, %.2f)"
202 " with control (%.2f, %.2f)\n",
203 (double)v_middle.x / 64,
204 (double)v_middle.y / 64,
205 (double)v_control.x / 64,
206 (double)v_control.y / 64 ));
207 error = func_interface->conic_to( &v_control, &v_middle, user );
215 FT_TRACE5(( " conic to (%.2f, %.2f)"
216 " with control (%.2f, %.2f)\n",
217 (double)v_start.x / 64,
218 (double)v_start.y / 64,
219 (double)v_control.x / 64,
220 (double)v_control.y / 64 ));
221 error = func_interface->conic_to( &v_control, &v_start, user );
224 default: /* FT_CURVE_TAG_CUBIC */
226 FT_Vector vec1, vec2;
229 if ( point + 1 > limit ||
230 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
231 goto Invalid_Outline;
236 vec1.x = SCALED( point[-2].x );
237 vec1.y = SCALED( point[-2].y );
239 vec2.x = SCALED( point[-1].x );
240 vec2.y = SCALED( point[-1].y );
242 if ( point <= limit )
247 vec.x = SCALED( point->x );
248 vec.y = SCALED( point->y );
250 FT_TRACE5(( " cubic to (%.2f, %.2f)"
251 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
257 (double)vec2.y / 64 ));
258 error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
264 FT_TRACE5(( " cubic to (%.2f, %.2f)"
265 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
266 (double)v_start.x / 64,
267 (double)v_start.y / 64,
271 (double)vec2.y / 64 ));
272 error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
278 /* close the contour with a line segment */
279 FT_TRACE5(( " line to (%.2f, %.2f)\n",
280 (double)v_start.x / 64, (double)v_start.y / 64 ));
281 error = func_interface->line_to( &v_start, user );
288 FT_TRACE5(( "FT_Outline_Decompose: Done\n" ));
292 error = FT_THROW( Invalid_Outline );
296 FT_TRACE5(( "FT_Outline_Decompose: Error 0x%x\n", error ));
301 /* documentation is in ftoutln.h */
303 FT_EXPORT_DEF( FT_Error )
304 FT_Outline_New( FT_Library library,
307 FT_Outline *anoutline )
314 return FT_THROW( Invalid_Library_Handle );
316 memory = library->memory;
318 if ( !anoutline || !memory )
319 return FT_THROW( Invalid_Argument );
321 *anoutline = null_outline;
323 if ( numContours < 0 ||
324 (FT_UInt)numContours > numPoints )
325 return FT_THROW( Invalid_Argument );
327 if ( numPoints > FT_OUTLINE_POINTS_MAX )
328 return FT_THROW( Array_Too_Large );
330 if ( FT_NEW_ARRAY( anoutline->points, numPoints ) ||
331 FT_NEW_ARRAY( anoutline->tags, numPoints ) ||
332 FT_NEW_ARRAY( anoutline->contours, numContours ) )
335 anoutline->n_points = (FT_Short)numPoints;
336 anoutline->n_contours = (FT_Short)numContours;
337 anoutline->flags |= FT_OUTLINE_OWNER;
342 anoutline->flags |= FT_OUTLINE_OWNER;
343 FT_Outline_Done( library, anoutline );
349 /* documentation is in ftoutln.h */
351 FT_EXPORT_DEF( FT_Error )
352 FT_Outline_Check( FT_Outline* outline )
356 FT_Int n_points = outline->n_points;
357 FT_Int n_contours = outline->n_contours;
363 if ( n_points == 0 && n_contours == 0 )
366 /* check point and contour counts */
367 if ( n_points <= 0 || n_contours <= 0 )
371 for ( n = 0; n < n_contours; n++ )
373 end = outline->contours[n];
375 /* note that we don't accept empty contours */
376 if ( end <= end0 || end >= n_points )
382 if ( end0 != n_points - 1 )
385 /* XXX: check the tags array */
390 return FT_THROW( Invalid_Outline );
394 /* documentation is in ftoutln.h */
396 FT_EXPORT_DEF( FT_Error )
397 FT_Outline_Copy( const FT_Outline* source,
403 if ( !source || !target )
404 return FT_THROW( Invalid_Outline );
406 if ( source->n_points != target->n_points ||
407 source->n_contours != target->n_contours )
408 return FT_THROW( Invalid_Argument );
410 if ( source == target )
413 if ( source->n_points )
415 FT_ARRAY_COPY( target->points, source->points, source->n_points );
416 FT_ARRAY_COPY( target->tags, source->tags, source->n_points );
419 if ( source->n_contours )
420 FT_ARRAY_COPY( target->contours, source->contours, source->n_contours );
422 /* copy all flags, except the `FT_OUTLINE_OWNER' one */
423 is_owner = target->flags & FT_OUTLINE_OWNER;
424 target->flags = source->flags;
426 target->flags &= ~FT_OUTLINE_OWNER;
427 target->flags |= is_owner;
433 /* documentation is in ftoutln.h */
435 FT_EXPORT_DEF( FT_Error )
436 FT_Outline_Done( FT_Library library,
437 FT_Outline* outline )
443 return FT_THROW( Invalid_Library_Handle );
446 return FT_THROW( Invalid_Outline );
448 memory = library->memory;
451 return FT_THROW( Invalid_Argument );
453 if ( outline->flags & FT_OUTLINE_OWNER )
455 FT_FREE( outline->points );
456 FT_FREE( outline->tags );
457 FT_FREE( outline->contours );
459 *outline = null_outline;
465 /* documentation is in ftoutln.h */
467 FT_EXPORT_DEF( void )
468 FT_Outline_Get_CBox( const FT_Outline* outline,
471 FT_Pos xMin, yMin, xMax, yMax;
474 if ( outline && acbox )
476 if ( outline->n_points == 0 )
485 FT_Vector* vec = outline->points;
486 FT_Vector* limit = vec + outline->n_points;
489 xMin = xMax = vec->x;
490 yMin = yMax = vec->y;
493 for ( ; vec < limit; vec++ )
499 if ( x < xMin ) xMin = x;
500 if ( x > xMax ) xMax = x;
503 if ( y < yMin ) yMin = y;
504 if ( y > yMax ) yMax = y;
515 /* documentation is in ftoutln.h */
517 FT_EXPORT_DEF( void )
518 FT_Outline_Translate( const FT_Outline* outline,
529 vec = outline->points;
531 for ( n = 0; n < outline->n_points; n++ )
533 vec->x = ADD_LONG( vec->x, xOffset );
534 vec->y = ADD_LONG( vec->y, yOffset );
540 /* documentation is in ftoutln.h */
542 FT_EXPORT_DEF( void )
543 FT_Outline_Reverse( FT_Outline* outline )
553 for ( n = 0; n < outline->n_contours; n++ )
555 /* keep the first contour point as is and swap points around it */
556 /* to guarantee that the cubic arches stay valid after reverse */
558 last = outline->contours[n];
560 /* reverse point table */
562 FT_Vector* p = outline->points + first;
563 FT_Vector* q = outline->points + last;
577 /* reverse tags table */
579 char* p = outline->tags + first;
580 char* q = outline->tags + last;
597 outline->flags ^= FT_OUTLINE_REVERSE_FILL;
601 /* documentation is in ftoutln.h */
603 FT_EXPORT_DEF( FT_Error )
604 FT_Outline_Render( FT_Library library,
606 FT_Raster_Params* params )
609 FT_Renderer renderer;
615 return FT_THROW( Invalid_Library_Handle );
618 return FT_THROW( Invalid_Outline );
621 return FT_THROW( Invalid_Argument );
623 FT_Outline_Get_CBox( outline, &cbox );
624 if ( cbox.xMin < -0x1000000L || cbox.yMin < -0x1000000L ||
625 cbox.xMax > 0x1000000L || cbox.yMax > 0x1000000L )
626 return FT_THROW( Invalid_Outline );
628 renderer = library->cur_renderer;
629 node = library->renderers.head;
631 params->source = (void*)outline;
633 /* preset clip_box for direct mode */
634 if ( params->flags & FT_RASTER_FLAG_DIRECT &&
635 !( params->flags & FT_RASTER_FLAG_CLIP ) )
637 params->clip_box.xMin = cbox.xMin >> 6;
638 params->clip_box.yMin = cbox.yMin >> 6;
639 params->clip_box.xMax = ( cbox.xMax + 63 ) >> 6;
640 params->clip_box.yMax = ( cbox.yMax + 63 ) >> 6;
643 error = FT_ERR( Cannot_Render_Glyph );
646 error = renderer->raster_render( renderer->raster, params );
647 if ( !error || FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
650 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
651 /* is unsupported by the current renderer for this glyph image */
654 /* now, look for another renderer that supports the same */
656 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
664 /* documentation is in ftoutln.h */
666 FT_EXPORT_DEF( FT_Error )
667 FT_Outline_Get_Bitmap( FT_Library library,
669 const FT_Bitmap *abitmap )
671 FT_Raster_Params params;
675 return FT_THROW( Invalid_Argument );
677 /* other checks are delayed to `FT_Outline_Render' */
679 params.target = abitmap;
682 if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY ||
683 abitmap->pixel_mode == FT_PIXEL_MODE_LCD ||
684 abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
685 params.flags |= FT_RASTER_FLAG_AA;
687 return FT_Outline_Render( library, outline, ¶ms );
691 /* documentation is in freetype.h */
693 FT_EXPORT_DEF( void )
694 FT_Vector_Transform( FT_Vector* vector,
695 const FT_Matrix* matrix )
700 if ( !vector || !matrix )
703 xz = FT_MulFix( vector->x, matrix->xx ) +
704 FT_MulFix( vector->y, matrix->xy );
706 yz = FT_MulFix( vector->x, matrix->yx ) +
707 FT_MulFix( vector->y, matrix->yy );
714 /* documentation is in ftoutln.h */
716 FT_EXPORT_DEF( void )
717 FT_Outline_Transform( const FT_Outline* outline,
718 const FT_Matrix* matrix )
724 if ( !outline || !matrix || !outline->points )
727 vec = outline->points;
728 limit = vec + outline->n_points;
730 for ( ; vec < limit; vec++ )
731 FT_Vector_Transform( vec, matrix );
737 #define FT_OUTLINE_GET_CONTOUR( outline, c, first, last ) \
740 (first) = ( c > 0 ) ? (outline)->points + \
741 (outline)->contours[c - 1] + 1 \
742 : (outline)->points; \
743 (last) = (outline)->points + (outline)->contours[c]; \
747 /* Is a point in some contour? */
749 /* We treat every point of the contour as if it */
750 /* it were ON. That is, we allow false positives, */
751 /* but disallow false negatives. (XXX really?) */
753 ft_contour_has( FT_Outline* outline,
764 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
766 for ( a = first; a <= last; a++ )
772 b = ( a == last ) ? first : a + 1;
774 intersect = ( a->y - point->y ) ^ ( b->y - point->y );
776 /* a and b are on the same side */
777 if ( intersect >= 0 )
779 if ( intersect == 0 && a->y == point->y )
781 if ( ( a->x <= point->x && b->x >= point->x ) ||
782 ( a->x >= point->x && b->x <= point->x ) )
789 x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
793 else if ( x == point->x )
802 ft_contour_enclosed( FT_Outline* outline,
810 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
812 for ( i = 0; i < outline->n_contours; i++ )
814 if ( i != c && ft_contour_has( outline, i, first ) )
819 for ( pt = first + 1; pt <= last; pt++ )
820 if ( !ft_contour_has( outline, i, pt ) )
831 /* This version differs from the public one in that each */
832 /* part (contour not enclosed in another contour) of the */
833 /* outline is checked for orientation. This is */
834 /* necessary for some buggy CJK fonts. */
835 static FT_Orientation
836 ft_outline_get_orientation( FT_Outline* outline )
841 FT_Orientation orient = FT_ORIENTATION_NONE;
844 first = outline->points;
845 for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
848 FT_Vector* xmin_point;
852 last = outline->points + outline->contours[i];
854 /* skip degenerate contours */
855 if ( last < first + 2 )
858 if ( ft_contour_enclosed( outline, i ) )
864 for ( point = first + 1; point <= last; point++ )
866 if ( point->x < xmin )
873 /* check the orientation of the contour */
880 prev = ( xmin_point == first ) ? last : xmin_point - 1;
881 next = ( xmin_point == last ) ? first : xmin_point + 1;
883 if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
884 FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
885 o = FT_ORIENTATION_POSTSCRIPT;
887 o = FT_ORIENTATION_TRUETYPE;
889 if ( orient == FT_ORIENTATION_NONE )
891 else if ( orient != o )
892 return FT_ORIENTATION_NONE;
902 /* documentation is in ftoutln.h */
904 FT_EXPORT_DEF( FT_Error )
905 FT_Outline_Embolden( FT_Outline* outline,
908 return FT_Outline_EmboldenXY( outline, strength, strength );
912 /* documentation is in ftoutln.h */
914 FT_EXPORT_DEF( FT_Error )
915 FT_Outline_EmboldenXY( FT_Outline* outline,
920 FT_Int c, first, last;
921 FT_Orientation orientation;
925 return FT_THROW( Invalid_Outline );
929 if ( xstrength == 0 && ystrength == 0 )
932 orientation = FT_Outline_Get_Orientation( outline );
933 if ( orientation == FT_ORIENTATION_NONE )
935 if ( outline->n_contours )
936 return FT_THROW( Invalid_Argument );
941 points = outline->points;
944 for ( c = 0; c < outline->n_contours; c++ )
946 FT_Vector in, out, anchor, shift;
947 FT_Fixed l_in, l_out, l_anchor = 0, l, q, d;
952 last = outline->contours[c];
955 /* pacify compiler */
956 in.x = in.y = anchor.x = anchor.y = 0;
958 /* Counter j cycles though the points; counter i advances only */
959 /* when points are moved; anchor k marks the first moved point. */
960 for ( i = last, j = first, k = -1;
962 j = j < last ? j + 1 : first )
966 out.x = points[j].x - points[i].x;
967 out.y = points[j].y - points[i].y;
968 l_out = (FT_Fixed)FT_Vector_NormLen( &out );
988 d = FT_MulFix( in.x, out.x ) + FT_MulFix( in.y, out.y );
990 /* shift only if turn is less than ~160 degrees */
995 /* shift components along lateral bisector in proper orientation */
996 shift.x = in.y + out.y;
997 shift.y = in.x + out.x;
999 if ( orientation == FT_ORIENTATION_TRUETYPE )
1004 /* restrict shift magnitude to better handle collapsing segments */
1005 q = FT_MulFix( out.x, in.y ) - FT_MulFix( out.y, in.x );
1006 if ( orientation == FT_ORIENTATION_TRUETYPE )
1009 l = FT_MIN( l_in, l_out );
1011 /* non-strict inequalities avoid divide-by-zero when q == l == 0 */
1012 if ( FT_MulFix( xstrength, q ) <= FT_MulFix( l, d ) )
1013 shift.x = FT_MulDiv( shift.x, xstrength, d );
1015 shift.x = FT_MulDiv( shift.x, l, q );
1018 if ( FT_MulFix( ystrength, q ) <= FT_MulFix( l, d ) )
1019 shift.y = FT_MulDiv( shift.y, ystrength, d );
1021 shift.y = FT_MulDiv( shift.y, l, q );
1024 shift.x = shift.y = 0;
1028 i = i < last ? i + 1 : first )
1030 points[i].x += xstrength + shift.x;
1031 points[i].y += ystrength + shift.y;
1046 /* documentation is in ftoutln.h */
1048 FT_EXPORT_DEF( FT_Orientation )
1049 FT_Outline_Get_Orientation( FT_Outline* outline )
1051 FT_BBox cbox = { 0, 0, 0, 0 };
1052 FT_Int xshift, yshift;
1054 FT_Vector v_prev, v_cur;
1055 FT_Int c, n, first, last;
1059 if ( !outline || outline->n_points <= 0 )
1060 return FT_ORIENTATION_TRUETYPE;
1062 /* We use the nonzero winding rule to find the orientation. */
1063 /* Since glyph outlines behave much more `regular' than arbitrary */
1064 /* cubic or quadratic curves, this test deals with the polygon */
1065 /* only that is spanned up by the control points. */
1067 FT_Outline_Get_CBox( outline, &cbox );
1069 /* Handle collapsed outlines to avoid undefined FT_MSB. */
1070 if ( cbox.xMin == cbox.xMax || cbox.yMin == cbox.yMax )
1071 return FT_ORIENTATION_NONE;
1073 /* Reject values large outlines. */
1074 if ( cbox.xMin < -0x1000000L || cbox.yMin < -0x1000000L ||
1075 cbox.xMax > 0x1000000L || cbox.yMax > 0x1000000L )
1076 return FT_ORIENTATION_NONE;
1078 xshift = FT_MSB( (FT_UInt32)( FT_ABS( cbox.xMax ) |
1079 FT_ABS( cbox.xMin ) ) ) - 14;
1080 xshift = FT_MAX( xshift, 0 );
1082 yshift = FT_MSB( (FT_UInt32)( cbox.yMax - cbox.yMin ) ) - 14;
1083 yshift = FT_MAX( yshift, 0 );
1085 points = outline->points;
1088 for ( c = 0; c < outline->n_contours; c++ )
1091 last = outline->contours[c];
1093 v_prev.x = points[last].x >> xshift;
1094 v_prev.y = points[last].y >> yshift;
1096 for ( n = first; n <= last; n++ )
1098 v_cur.x = points[n].x >> xshift;
1099 v_cur.y = points[n].y >> yshift;
1101 area = ADD_LONG( area,
1102 MUL_LONG( v_cur.y - v_prev.y,
1103 v_cur.x + v_prev.x ) );
1110 return FT_ORIENTATION_POSTSCRIPT;
1111 else if ( area < 0 )
1112 return FT_ORIENTATION_TRUETYPE;
1114 return FT_ORIENTATION_NONE;