1 /***************************************************************************/
5 /* FreeType outline management (body). */
7 /* Copyright 1996-2000 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 /*************************************************************************/
26 #include <freetype/ftoutln.h>
27 #include <freetype/internal/ftobjs.h>
30 /*************************************************************************/
32 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
33 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
34 /* messages during execution. */
37 #define FT_COMPONENT trace_outline
41 const FT_Outline null_outline
= { 0, 0, 0, 0, 0, 0 };
44 /*************************************************************************/
47 /* FT_Outline_Decompose */
50 /* Walks over an outline's structure to decompose it into individual */
51 /* segments and Bezier arcs. This function is also able to emit */
52 /* `move to' and `close to' operations to indicate the start and end */
53 /* of new contours in the outline. */
56 /* outline :: A pointer to the source target. */
58 /* interface :: A table of `emitters', i.e,. function pointers called */
59 /* during decomposition to indicate path operations. */
61 /* user :: A typeless pointer which is passed to each emitter */
62 /* during the decomposition. It can be used to store */
63 /* the state during the decomposition. */
66 /* FreeType error code. 0 means sucess. */
68 FT_EXPORT_FUNC( FT_Error
) FT_Outline_Decompose(
70 FT_Outline_Funcs
* interface
,
74 #define SCALED( x ) ( ( (x) << shift ) - delta )
86 FT_Int n
; /* index of contour in outline */
87 FT_UInt first
; /* index of first point in contour */
88 char tag
; /* current point's state */
94 if ( !outline
|| !interface
)
95 return FT_Err_Invalid_Argument
;
97 shift
= interface
->shift
;
98 delta
= interface
->delta
;
101 for ( n
= 0; n
< outline
->n_contours
; n
++ )
103 FT_Int last
; /* index of last point in contour */
106 last
= outline
->contours
[n
];
107 limit
= outline
->points
+ last
;
109 v_start
= outline
->points
[first
];
110 v_last
= outline
->points
[last
];
112 v_start
.x
= SCALED( v_start
.x
); v_start
.y
= SCALED( v_start
.y
);
113 v_last
.x
= SCALED( v_last
.x
); v_last
.y
= SCALED( v_last
.y
);
117 point
= outline
->points
+ first
;
118 tags
= outline
->tags
+ first
;
119 tag
= FT_CURVE_TAG( tags
[0] );
121 /* A contour cannot start with a cubic control point! */
122 if ( tag
== FT_Curve_Tag_Cubic
)
123 goto Invalid_Outline
;
125 /* check first point to determine origin */
126 if ( tag
== FT_Curve_Tag_Conic
)
128 /* first point is conic control. Yes, this happens. */
129 if ( FT_CURVE_TAG( outline
->tags
[last
] ) == FT_Curve_Tag_On
)
131 /* start at last point if it is on the curve */
137 /* if both first and last points are conic, */
138 /* start at their middle and record its position */
140 v_start
.x
= ( v_start
.x
+ v_last
.x
) / 2;
141 v_start
.y
= ( v_start
.y
+ v_last
.y
) / 2;
149 error
= interface
->move_to( &v_start
, user
);
153 while ( point
< limit
)
158 tag
= FT_CURVE_TAG( tags
[0] );
161 case FT_Curve_Tag_On
: /* emit a single line_to */
166 vec
.x
= SCALED( point
->x
);
167 vec
.y
= SCALED( point
->y
);
169 error
= interface
->line_to( &vec
, user
);
175 case FT_Curve_Tag_Conic
: /* consume conic arcs */
176 v_control
.x
= SCALED( point
->x
);
177 v_control
.y
= SCALED( point
->y
);
188 tag
= FT_CURVE_TAG( tags
[0] );
190 vec
.x
= SCALED( point
->x
);
191 vec
.y
= SCALED( point
->y
);
193 if ( tag
== FT_Curve_Tag_On
)
195 error
= interface
->conic_to( &v_control
, &vec
, user
);
201 if ( tag
!= FT_Curve_Tag_Conic
)
202 goto Invalid_Outline
;
204 v_middle
.x
= ( v_control
.x
+ vec
.x
) / 2;
205 v_middle
.y
= ( v_control
.y
+ vec
.y
) / 2;
207 error
= interface
->conic_to( &v_control
, &v_middle
, user
);
215 error
= interface
->conic_to( &v_control
, &v_start
, user
);
218 default: /* FT_Curve_Tag_Cubic */
220 FT_Vector vec1
, vec2
;
223 if ( point
+ 1 > limit
||
224 FT_CURVE_TAG( tags
[1] ) != FT_Curve_Tag_Cubic
)
225 goto Invalid_Outline
;
230 vec1
.x
= SCALED( point
[-2].x
); vec1
.y
= SCALED( point
[-2].y
);
231 vec2
.x
= SCALED( point
[-1].x
); vec2
.y
= SCALED( point
[-1].y
);
233 if ( point
<= limit
)
238 vec
.x
= SCALED( point
->x
);
239 vec
.y
= SCALED( point
->y
);
241 error
= interface
->cubic_to( &vec1
, &vec2
, &vec
, user
);
247 error
= interface
->cubic_to( &vec1
, &vec2
, &v_start
, user
);
253 /* close the contour with a line segment */
254 error
= interface
->line_to( &v_start
, user
);
269 return FT_Err_Invalid_Outline
;
273 FT_EXPORT_FUNC( FT_Error
) FT_Outline_New_Internal(
277 FT_Outline
* outline
)
282 if ( !outline
|| !memory
)
283 return FT_Err_Invalid_Argument
;
285 *outline
= null_outline
;
287 if ( ALLOC_ARRAY( outline
->points
, numPoints
* 2L, FT_Pos
) ||
288 ALLOC_ARRAY( outline
->tags
, numPoints
, FT_Byte
) ||
289 ALLOC_ARRAY( outline
->contours
, numContours
, FT_UShort
) )
292 outline
->n_points
= (FT_UShort
)numPoints
;
293 outline
->n_contours
= (FT_Short
)numContours
;
294 outline
->flags
|= ft_outline_owner
;
299 outline
->flags
|= ft_outline_owner
;
300 FT_Outline_Done_Internal( memory
, outline
);
306 /*************************************************************************/
312 /* Creates a new outline of a given size. */
315 /* library :: A handle to the library object from where the */
316 /* outline is allocated. Note however that the new */
317 /* outline will NOT necessarily be FREED, when */
318 /* destroying the library, by FT_Done_FreeType(). */
320 /* numPoints :: The maximal number of points within the outline. */
322 /* numContours :: The maximal number of contours within the outline. */
325 /* outline :: A handle to the new outline. NULL in case of */
329 /* FreeType error code. 0 means success. */
335 /* The reason why this function takes a `library' parameter is simply */
336 /* to use the library's memory allocator. */
338 FT_EXPORT_FUNC( FT_Error
) FT_Outline_New( FT_Library library
,
341 FT_Outline
* outline
)
344 return FT_Err_Invalid_Library_Handle
;
346 return FT_Outline_New_Internal( library
->memory
, numPoints
,
347 numContours
, outline
);
351 /*************************************************************************/
354 /* FT_Outline_Copy */
357 /* Copies an outline into another one. Both objects must have the */
358 /* same sizes (number of points & number of contours) when this */
359 /* function is called. */
362 /* source :: A handle to the source outline. */
365 /* target :: A handle to the target outline. */
368 /* FreeType error code. 0 means success. */
370 FT_EXPORT_FUNC( FT_Error
) FT_Outline_Copy( FT_Outline
* source
,
376 if ( !source
|| !target
||
377 source
->n_points
!= target
->n_points
||
378 source
->n_contours
!= target
->n_contours
)
379 return FT_Err_Invalid_Argument
;
381 MEM_Copy( target
->points
, source
->points
,
382 source
->n_points
* sizeof ( FT_Vector
) );
384 MEM_Copy( target
->tags
, source
->tags
,
385 source
->n_points
* sizeof ( FT_Byte
) );
387 MEM_Copy( target
->contours
, source
->contours
,
388 source
->n_contours
* sizeof ( FT_Short
) );
390 /* copy all flags, except the `ft_outline_owner' one */
391 is_owner
= target
->flags
& ft_outline_owner
;
392 target
->flags
= source
->flags
;
394 target
->flags
&= ~ft_outline_owner
;
395 target
->flags
|= is_owner
;
401 FT_EXPORT_FUNC( FT_Error
) FT_Outline_Done_Internal( FT_Memory memory
,
402 FT_Outline
* outline
)
406 if ( outline
->flags
& ft_outline_owner
)
408 FREE( outline
->points
);
409 FREE( outline
->tags
);
410 FREE( outline
->contours
);
412 *outline
= null_outline
;
417 return FT_Err_Invalid_Argument
;
421 /*************************************************************************/
424 /* FT_Outline_Done */
427 /* Destroys an outline created with FT_Outline_New(). */
430 /* library :: A handle of the library object used to allocate the */
433 /* outline :: A pointer to the outline object to be discarded. */
436 /* FreeType error code. 0 means success. */
442 /* If the outline's `owner' field is not set, only the outline */
443 /* descriptor will be released. */
445 /* The reason why this function takes an `outline' parameter is */
446 /* simply to use FT_Free(). */
448 FT_EXPORT_FUNC( FT_Error
) FT_Outline_Done( FT_Library library
,
449 FT_Outline
* outline
)
451 /* check for valid `outline' in FT_Outline_Done_Internal() */
454 return FT_Err_Invalid_Library_Handle
;
456 return FT_Outline_Done_Internal( library
->memory
, outline
);
460 /*************************************************************************/
463 /* FT_Outline_Get_CBox */
466 /* Returns an outline's `control box'. The control box encloses all */
467 /* the outline's points, including Bezier control points. Though it */
468 /* coincides with the exact bounding box for most glyphs, it can be */
469 /* slightly larger in some situations (like when rotating an outline */
470 /* which contains Bezier outside arcs). */
472 /* Computing the control box is very fast, while getting the bounding */
473 /* box can take much more time as it needs to walk over all segments */
474 /* and arcs in the outline. To get the latter, you can use the */
475 /* `ftbbox' component which is dedicated to this single task. */
478 /* outline :: A pointer to the source outline descriptor. */
481 /* cbox :: The outline's control box. */
486 FT_EXPORT_FUNC( void ) FT_Outline_Get_CBox( FT_Outline
* outline
,
489 FT_Pos xMin
, yMin
, xMax
, yMax
;
492 if ( outline
&& cbox
)
494 if ( outline
->n_points
== 0 )
503 FT_Vector
* vec
= outline
->points
;
504 FT_Vector
* limit
= vec
+ outline
->n_points
;
507 xMin
= xMax
= vec
->x
;
508 yMin
= yMax
= vec
->y
;
511 for ( ; vec
< limit
; vec
++ )
517 if ( x
< xMin
) xMin
= x
;
518 if ( x
> xMax
) xMax
= x
;
521 if ( y
< yMin
) yMin
= y
;
522 if ( y
> yMax
) yMax
= y
;
533 /*************************************************************************/
536 /* FT_Outline_Translate */
539 /* Applies a simple translation to the points of an outline. */
542 /* outline :: A pointer to the target outline descriptor. */
544 /* xOffset :: The horizontal offset. */
546 /* yOffset :: The vertical offset. */
551 FT_EXPORT_FUNC( void ) FT_Outline_Translate( FT_Outline
* outline
,
556 FT_Vector
* vec
= outline
->points
;
559 for ( n
= 0; n
< outline
->n_points
; n
++ )
568 /*************************************************************************/
571 /* FT_Outline_Reverse */
574 /* Reverses the drawing direction of an outline. This is used to */
575 /* ensure consistent fill conventions for mirrored glyphs. */
578 /* outline :: A pointer to the target outline descriptor. */
581 /* This functions toggles the bit flag `ft_outline_reverse_fill' in */
582 /* the outline's `flags' field. */
584 FT_EXPORT_FUNC( void ) FT_Outline_Reverse( FT_Outline
* outline
)
592 for ( n
= 0; n
< outline
->n_contours
; n
++ )
594 last
= outline
->contours
[n
];
596 /* reverse point table */
598 FT_Vector
* p
= outline
->points
+ first
;
599 FT_Vector
* q
= outline
->points
+ last
;
613 /* reverse tags table */
615 char* p
= outline
->tags
+ first
;
616 char* q
= outline
->tags
+ last
;
633 outline
->flags
^= ft_outline_reverse_fill
;
637 /*************************************************************************/
640 /* FT_Outline_Render */
643 /* Renders an outline within a bitmap using the current scan-convert. */
644 /* This functions uses an FT_Raster_Params structure as an argument, */
645 /* allowing advanced features like direct composition, translucency, */
649 /* library :: A handle to a FreeType library object. */
651 /* outline :: A pointer to the source outline descriptor. */
653 /* params :: A pointer to a FT_Raster_Params structure used to */
654 /* describe the rendering operation. */
657 /* FreeType error code. 0 means success. */
660 /* YES. Rendering is synchronized, so that concurrent calls to the */
661 /* scan-line converter will be serialized. */
664 /* You should know what you are doing and how FT_Raster_Params works */
665 /* to use this function. */
667 /* The field `params.source' will be set to `outline' before the scan */
668 /* converter is called, which means that the value you give to it is */
669 /* actually ignored. */
671 FT_EXPORT_FUNC( FT_Error
) FT_Outline_Render( FT_Library library
,
673 FT_Raster_Params
* params
)
677 FT_Renderer renderer
;
682 return FT_Err_Invalid_Library_Handle
;
685 return FT_Err_Invalid_Argument
;
687 renderer
= library
->cur_renderer
;
688 node
= library
->renderers
.head
;
690 params
->source
= (void*)outline
;
692 error
= FT_Err_Cannot_Render_Glyph
;
695 error
= renderer
->raster_render( renderer
->raster
, params
);
696 if ( !error
|| error
!= FT_Err_Cannot_Render_Glyph
)
699 /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
700 /* is unsupported by the current renderer for this glyph image */
703 /* now, look for another renderer that supports the same */
705 renderer
= FT_Lookup_Renderer( library
, ft_glyph_format_outline
,
710 /* if we changed the current renderer for the glyph image format */
711 /* we need to select it as the next current one */
712 if ( !error
&& update
&& renderer
)
713 FT_Set_Renderer( library
, renderer
, 0, 0 );
719 /*************************************************************************/
722 /* FT_Outline_Get_Bitmap */
725 /* Renders an outline within a bitmap. The outline's image is simply */
726 /* OR-ed to the target bitmap. */
729 /* library :: A handle to a FreeType library object. */
731 /* outline :: A pointer to the source outline descriptor. */
733 /* map :: A pointer to the target bitmap descriptor. */
736 /* FreeType error code. 0 means success. */
739 /* YES. Rendering is synchronized, so that concurrent calls to the */
740 /* scan-line converter will be serialized. */
743 /* This function does NOT CREATE the bitmap, it only renders an */
744 /* outline image within the one you pass to it! */
746 /* It will use the raster correponding to the default glyph format. */
748 FT_EXPORT_FUNC( FT_Error
) FT_Outline_Get_Bitmap( FT_Library library
,
752 FT_Raster_Params params
;
756 return FT_Err_Invalid_Argument
;
758 /* other checks are delayed to FT_Outline_Render() */
760 params
.target
= bitmap
;
763 if ( bitmap
->pixel_mode
== ft_pixel_mode_grays
)
764 params
.flags
|= ft_raster_flag_aa
;
766 return FT_Outline_Render( library
, outline
, ¶ms
);
770 /*************************************************************************/
773 /* FT_Vector_Transform */
776 /* Transforms a single vector through a 2x2 matrix. */
779 /* vector :: The target vector to transform. */
782 /* matrix :: A pointer to the source 2x2 matrix. */
788 /* The result is undefined if either `vector' or `matrix' is invalid. */
790 FT_EXPORT_FUNC( void ) FT_Vector_Transform( FT_Vector
* vector
,
796 if ( !vector
|| !matrix
)
799 xz
= FT_MulFix( vector
->x
, matrix
->xx
) +
800 FT_MulFix( vector
->y
, matrix
->xy
);
802 yz
= FT_MulFix( vector
->x
, matrix
->yx
) +
803 FT_MulFix( vector
->y
, matrix
->yy
);
810 /*************************************************************************/
813 /* FT_Outline_Transform */
816 /* Applies a simple 2x2 matrix to all of an outline's points. Useful */
817 /* for applying rotations, slanting, flipping, etc. */
820 /* outline :: A pointer to the target outline descriptor. */
822 /* matrix :: A pointer to the transformation matrix. */
828 /* You can use FT_Outline_Translate() if you need to translate the */
829 /* outline's points. */
831 FT_EXPORT_FUNC( void ) FT_Outline_Transform( FT_Outline
* outline
,
834 FT_Vector
* vec
= outline
->points
;
835 FT_Vector
* limit
= vec
+ outline
->n_points
;
838 for ( ; vec
< limit
; vec
++ )
839 FT_Vector_Transform( vec
, matrix
);