33#define FT_COMPONENT outline
37 const FT_Outline null_outline = { 0, 0, NULL, NULL, NULL, 0 };
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 )
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] );
106 if ( tag == FT_CURVE_TAG_CUBIC )
107 goto Invalid_Outline;
110 if ( tag == FT_CURVE_TAG_CONIC )
113 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
124 v_start.x = ( v_start.x + v_last.x ) / 2;
125 v_start.y = ( v_start.y + v_last.y ) / 2;
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:
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:
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 );
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 );
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 ));
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_UShort)numPoints;
336 anoutline->n_contours = (FT_UShort)numContours;
337 anoutline->flags |= FT_OUTLINE_OWNER;
342 anoutline->flags |= FT_OUTLINE_OWNER;
343 FT_Outline_Done( library, anoutline );
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;
362 FT_TRACE5((
"FT_Outline_Check: contours = %d, points = %d\n",
363 n_contours, n_points ));
365 if ( n_points == 0 && n_contours == 0 )
369 if ( n_points == 0 || n_contours == 0 )
373 for ( n = 0; n < n_contours; n++ )
375 end = outline->contours[n];
378 if ( end <= end0 || end >= n_points )
384 if ( end0 != n_points - 1 )
392 return FT_THROW( Invalid_Outline );
398 FT_EXPORT_DEF( FT_Error )
399 FT_Outline_Copy(
const FT_Outline* source,
405 if ( !source || !target )
406 return FT_THROW( Invalid_Outline );
408 if ( source->n_points != target->n_points ||
409 source->n_contours != target->n_contours )
410 return FT_THROW( Invalid_Argument );
412 if ( source == target )
415 if ( source->n_points )
417 FT_ARRAY_COPY( target->points, source->points, source->n_points );
418 FT_ARRAY_COPY( target->tags, source->tags, source->n_points );
421 if ( source->n_contours )
422 FT_ARRAY_COPY( target->contours, source->contours, source->n_contours );
425 is_owner = target->flags & FT_OUTLINE_OWNER;
426 target->flags = source->flags;
428 target->flags &= ~FT_OUTLINE_OWNER;
429 target->flags |= is_owner;
437 FT_EXPORT_DEF( FT_Error )
438 FT_Outline_Done( FT_Library library,
439 FT_Outline* outline )
445 return FT_THROW( Invalid_Library_Handle );
448 return FT_THROW( Invalid_Outline );
450 memory = library->memory;
453 return FT_THROW( Invalid_Argument );
455 if ( outline->flags & FT_OUTLINE_OWNER )
457 FT_FREE( outline->points );
458 FT_FREE( outline->tags );
459 FT_FREE( outline->contours );
461 *outline = null_outline;
469 FT_EXPORT_DEF(
void )
470 FT_Outline_Get_CBox(
const FT_Outline* outline,
473 FT_Pos xMin, yMin, xMax, yMax;
476 if ( outline && acbox )
478 if ( outline->n_points == 0 )
487 FT_Vector* vec = outline->points;
488 FT_Vector* limit = vec + outline->n_points;
491 xMin = xMax = vec->x;
492 yMin = yMax = vec->y;
495 for ( ; vec < limit; vec++ )
501 if ( x < xMin ) xMin = x;
502 if ( x > xMax ) xMax = x;
505 if ( y < yMin ) yMin = y;
506 if ( y > yMax ) yMax = y;
519 FT_EXPORT_DEF(
void )
520 FT_Outline_Translate(
const FT_Outline* outline,
531 vec = outline->points;
533 for ( n = 0; n < outline->n_points; n++ )
535 vec->x = ADD_LONG( vec->x, xOffset );
536 vec->y = ADD_LONG( vec->y, yOffset );
544 FT_EXPORT_DEF(
void )
545 FT_Outline_Reverse( FT_Outline* outline )
555 for ( n = 0; n < outline->n_contours; n++ )
560 last = outline->contours[n];
564 FT_Vector* p = outline->points + first;
565 FT_Vector* q = outline->points + last;
581 FT_Byte* p = outline->tags + first;
582 FT_Byte* q = outline->tags + last;
599 outline->flags ^= FT_OUTLINE_REVERSE_FILL;
605 FT_EXPORT_DEF( FT_Error )
606 FT_Outline_Render( FT_Library library,
608 FT_Raster_Params* params )
611 FT_Renderer renderer;
617 return FT_THROW( Invalid_Library_Handle );
620 return FT_THROW( Invalid_Outline );
623 return FT_THROW( Invalid_Argument );
625 FT_Outline_Get_CBox( outline, &cbox );
626 if ( cbox.xMin < -0x1000000L || cbox.yMin < -0x1000000L ||
627 cbox.xMax > 0x1000000L || cbox.yMax > 0x1000000L )
628 return FT_THROW( Invalid_Outline );
630 renderer = library->cur_renderer;
631 node = library->renderers.head;
633 params->source = (
void*)outline;
636 if ( params->flags & FT_RASTER_FLAG_DIRECT &&
637 !( params->flags & FT_RASTER_FLAG_CLIP ) )
639 params->clip_box.xMin = cbox.xMin >> 6;
640 params->clip_box.yMin = cbox.yMin >> 6;
641 params->clip_box.xMax = ( cbox.xMax + 63 ) >> 6;
642 params->clip_box.yMax = ( cbox.yMax + 63 ) >> 6;
645 error = FT_ERR( Cannot_Render_Glyph );
648 error = renderer->raster_render( renderer->raster, params );
649 if ( !error || FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
658 renderer = FT_Lookup_Renderer( library, FT_GLYPH_FORMAT_OUTLINE,
668 FT_EXPORT_DEF( FT_Error )
669 FT_Outline_Get_Bitmap( FT_Library library,
671 const FT_Bitmap *abitmap )
673 FT_Raster_Params params;
677 return FT_THROW( Invalid_Argument );
681 params.target = abitmap;
684 if ( abitmap->pixel_mode == FT_PIXEL_MODE_GRAY ||
685 abitmap->pixel_mode == FT_PIXEL_MODE_LCD ||
686 abitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
687 params.flags |= FT_RASTER_FLAG_AA;
689 return FT_Outline_Render( library, outline, ¶ms );
695 FT_EXPORT_DEF(
void )
696 FT_Vector_Transform( FT_Vector* vector,
697 const FT_Matrix* matrix )
702 if ( !vector || !matrix )
705 xz = FT_MulFix( vector->x, matrix->xx ) +
706 FT_MulFix( vector->y, matrix->xy );
708 yz = FT_MulFix( vector->x, matrix->yx ) +
709 FT_MulFix( vector->y, matrix->yy );
718 FT_EXPORT_DEF(
void )
719 FT_Outline_Transform(
const FT_Outline* outline,
720 const FT_Matrix* matrix )
726 if ( !outline || !matrix || !outline->points )
729 vec = outline->points;
730 limit = vec + outline->n_points;
732 for ( ; vec < limit; vec++ )
733 FT_Vector_Transform( vec, matrix );
739#define FT_OUTLINE_GET_CONTOUR( outline, c, first, last ) \
742 (first) = ( c > 0 ) ? (outline)->points + \
743 (outline)->contours[c - 1] + 1 \
744 : (outline)->points; \
745 (last) = (outline)->points + (outline)->contours[c]; \
755 ft_contour_has( FT_Outline* outline,
766 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
768 for ( a = first; a <= last; a++ )
774 b = ( a == last ) ? first : a + 1;
776 intersect = ( a->y - point->y ) ^ ( b->y - point->y );
779 if ( intersect >= 0 )
781 if ( intersect == 0 && a->y == point->y )
783 if ( ( a->x <= point->x && b->x >= point->x ) ||
784 ( a->x >= point->x && b->x <= point->x ) )
791 x = a->x + ( b->x - a->x ) * (point->y - a->y ) / ( b->y - a->y );
795 else if ( x == point->x )
804 ft_contour_enclosed( FT_Outline* outline,
812 FT_OUTLINE_GET_CONTOUR( outline, c, first, last );
814 for ( i = 0; i < outline->n_contours; i++ )
816 if ( i != c && ft_contour_has( outline, i, first ) )
821 for ( pt = first + 1; pt <= last; pt++ )
822 if ( !ft_contour_has( outline, i, pt ) )
837 static FT_Orientation
838 ft_outline_get_orientation( FT_Outline* outline )
843 FT_Orientation orient = FT_ORIENTATION_NONE;
846 first = outline->points;
847 for ( i = 0; i < outline->n_contours; i++, first = last + 1 )
850 FT_Vector* xmin_point;
854 last = outline->points + outline->contours[i];
857 if ( last < first + 2 )
860 if ( ft_contour_enclosed( outline, i ) )
866 for ( point = first + 1; point <= last; point++ )
868 if ( point->x < xmin )
882 prev = ( xmin_point == first ) ? last : xmin_point - 1;
883 next = ( xmin_point == last ) ? first : xmin_point + 1;
885 if ( FT_Atan2( prev->x - xmin_point->x, prev->y - xmin_point->y ) >
886 FT_Atan2( next->x - xmin_point->x, next->y - xmin_point->y ) )
887 o = FT_ORIENTATION_POSTSCRIPT;
889 o = FT_ORIENTATION_TRUETYPE;
891 if ( orient == FT_ORIENTATION_NONE )
893 else if ( orient != o )
894 return FT_ORIENTATION_NONE;
906 FT_EXPORT_DEF( FT_Error )
907 FT_Outline_Embolden( FT_Outline* outline,
910 return FT_Outline_EmboldenXY( outline, strength, strength );
916 FT_EXPORT_DEF( FT_Error )
917 FT_Outline_EmboldenXY( FT_Outline* outline,
922 FT_Int c, first, last;
923 FT_Orientation orientation;
927 return FT_THROW( Invalid_Outline );
931 if ( xstrength == 0 && ystrength == 0 )
934 orientation = FT_Outline_Get_Orientation( outline );
935 if ( orientation == FT_ORIENTATION_NONE )
937 if ( outline->n_contours )
938 return FT_THROW( Invalid_Argument );
943 points = outline->points;
946 for ( c = 0; c < outline->n_contours; c++ )
948 FT_Vector in, out, anchor, shift;
949 FT_Fixed l_in, l_out, l_anchor = 0, l, q, d;
954 last = outline->contours[c];
958 in.x = in.y = anchor.x = anchor.y = 0;
962 for ( i = last, j = first, k = -1;
964 j = j < last ? j + 1 : first )
968 out.x = points[j].x - points[i].x;
969 out.y = points[j].y - points[i].y;
970 l_out = (FT_Fixed)FT_Vector_NormLen( &out );
990 d = FT_MulFix( in.x, out.x ) + FT_MulFix( in.y, out.y );
998 shift.x = in.y + out.y;
999 shift.y = in.x + out.x;
1001 if ( orientation == FT_ORIENTATION_TRUETYPE )
1007 q = FT_MulFix( out.x, in.y ) - FT_MulFix( out.y, in.x );
1008 if ( orientation == FT_ORIENTATION_TRUETYPE )
1011 l = FT_MIN( l_in, l_out );
1014 if ( FT_MulFix( xstrength, q ) <= FT_MulFix( l, d ) )
1015 shift.x = FT_MulDiv( shift.x, xstrength, d );
1017 shift.x = FT_MulDiv( shift.x, l, q );
1020 if ( FT_MulFix( ystrength, q ) <= FT_MulFix( l, d ) )
1021 shift.y = FT_MulDiv( shift.y, ystrength, d );
1023 shift.y = FT_MulDiv( shift.y, l, q );
1026 shift.x = shift.y = 0;
1030 i = i < last ? i + 1 : first )
1032 points[i].x += xstrength + shift.x;
1033 points[i].y += ystrength + shift.y;
1050 FT_EXPORT_DEF( FT_Orientation )
1051 FT_Outline_Get_Orientation( FT_Outline* outline )
1053 FT_BBox cbox = { 0, 0, 0, 0 };
1054 FT_Int xshift, yshift;
1056 FT_Vector v_prev, v_cur;
1057 FT_Int c, n, first, last;
1061 if ( !outline || outline->n_points <= 0 )
1062 return FT_ORIENTATION_TRUETYPE;
1069 FT_Outline_Get_CBox( outline, &cbox );
1072 if ( cbox.xMin == cbox.xMax || cbox.yMin == cbox.yMax )
1073 return FT_ORIENTATION_NONE;
1076 if ( cbox.xMin < -0x1000000L || cbox.yMin < -0x1000000L ||
1077 cbox.xMax > 0x1000000L || cbox.yMax > 0x1000000L )
1078 return FT_ORIENTATION_NONE;
1080 xshift = FT_MSB( (FT_UInt32)( FT_ABS( cbox.xMax ) |
1081 FT_ABS( cbox.xMin ) ) ) - 14;
1082 xshift = FT_MAX( xshift, 0 );
1084 yshift = FT_MSB( (FT_UInt32)( cbox.yMax - cbox.yMin ) ) - 14;
1085 yshift = FT_MAX( yshift, 0 );
1087 points = outline->points;
1090 for ( c = 0; c < outline->n_contours; c++ )
1093 last = outline->contours[c];
1095 v_prev.x = points[last].x >> xshift;
1096 v_prev.y = points[last].y >> yshift;
1098 for ( n = first; n <= last; n++ )
1100 v_cur.x = points[n].x >> xshift;
1101 v_cur.y = points[n].y >> yshift;
1103 area = ADD_LONG( area,
1104 MUL_LONG( v_cur.y - v_prev.y,
1105 v_cur.x + v_prev.x ) );
1112 return FT_ORIENTATION_POSTSCRIPT;
1113 else if ( area < 0 )
1114 return FT_ORIENTATION_TRUETYPE;
1116 return FT_ORIENTATION_NONE;