1 /***************************************************************************/
5 /* A new `perfect' anti-aliasing renderer (body). */
7 /* Copyright 2000-2003, 2005-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 /***************************************************************************/
18 /*************************************************************************/
20 /* This file can be compiled without the rest of the FreeType engine, by */
21 /* defining the _STANDALONE_ macro when compiling it. You also need to */
22 /* put the files `ftgrays.h' and `ftimage.h' into the current */
23 /* compilation directory. Typically, you could do something like */
25 /* - copy `src/smooth/ftgrays.c' (this file) to your current directory */
27 /* - copy `include/ftimage.h' and `src/smooth/ftgrays.h' to the same */
30 /* - compile `ftgrays' with the _STANDALONE_ macro defined, as in */
32 /* cc -c -D_STANDALONE_ ftgrays.c */
34 /* The renderer can be initialized with a call to */
35 /* `ft_gray_raster.raster_new'; an anti-aliased bitmap can be generated */
36 /* with a call to `ft_gray_raster.raster_render'. */
38 /* See the comments and documentation in the file `ftimage.h' for more */
39 /* details on how the raster works. */
41 /*************************************************************************/
43 /*************************************************************************/
45 /* This is a new anti-aliasing scan-converter for FreeType 2. The */
46 /* algorithm used here is _very_ different from the one in the standard */
47 /* `ftraster' module. Actually, `ftgrays' computes the _exact_ */
48 /* coverage of the outline on each pixel cell. */
50 /* It is based on ideas that I initially found in Raph Levien's */
51 /* excellent LibArt graphics library (see http://www.levien.com/libart */
52 /* for more information, though the web pages do not tell anything */
53 /* about the renderer; you'll have to dive into the source code to */
54 /* understand how it works). */
56 /* Note, however, that this is a _very_ different implementation */
57 /* compared to Raph's. Coverage information is stored in a very */
58 /* different way, and I don't use sorted vector paths. Also, it doesn't */
59 /* use floating point values. */
61 /* This renderer has the following advantages: */
63 /* - It doesn't need an intermediate bitmap. Instead, one can supply a */
64 /* callback function that will be called by the renderer to draw gray */
65 /* spans on any target surface. You can thus do direct composition on */
66 /* any kind of bitmap, provided that you give the renderer the right */
69 /* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on */
70 /* each pixel cell. */
72 /* - It performs a single pass on the outline (the `standard' FT2 */
73 /* renderer makes two passes). */
75 /* - It can easily be modified to render to _any_ number of gray levels */
78 /* - For small (< 20) pixel sizes, it is faster than the standard */
81 /*************************************************************************/
84 /*************************************************************************/
86 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
87 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
88 /* messages during execution. */
91 #define FT_COMPONENT trace_smooth
97 /* Auxiliary macros for token concatenation. */
98 #define FT_ERR_XCAT( x, y ) x ## y
99 #define FT_ERR_CAT( x, y ) FT_ERR_XCAT( x, y )
101 #define FT_BEGIN_STMNT do {
102 #define FT_END_STMNT } while ( 0 )
105 /* define this to dump debugging information */
106 /* #define FT_DEBUG_LEVEL_TRACE */
109 #ifdef FT_DEBUG_LEVEL_TRACE
118 #define FT_UINT_MAX UINT_MAX
119 #define FT_INT_MAX INT_MAX
121 #define ft_memset memset
123 #define ft_setjmp setjmp
124 #define ft_longjmp longjmp
125 #define ft_jmp_buf jmp_buf
127 typedef ptrdiff_t FT_PtrDist;
130 #define ErrRaster_Invalid_Mode -2
131 #define ErrRaster_Invalid_Outline -1
132 #define ErrRaster_Invalid_Argument -3
133 #define ErrRaster_Memory_Overflow -4
135 #define FT_BEGIN_HEADER
136 #define FT_END_HEADER
142 /* This macro is used to indicate that a function parameter is unused. */
143 /* Its purpose is simply to reduce compiler warnings. Note also that */
144 /* simply defining it as `(void)x' doesn't avoid warnings with certain */
145 /* ANSI compilers (e.g. LCC). */
146 #define FT_UNUSED( x ) (x) = (x)
149 /* we only use level 5 & 7 tracing messages; cf. ftdebug.h */
151 #ifdef FT_DEBUG_LEVEL_TRACE
154 FT_Message( const char* fmt,
161 vfprintf( stderr, fmt, ap );
166 /* empty function useful for setting a breakpoint to catch errors */
180 /* we don't handle tracing levels in stand-alone mode; */
182 #define FT_TRACE5( varformat ) FT_Message varformat
185 #define FT_TRACE7( varformat ) FT_Message varformat
188 #define FT_ERROR( varformat ) FT_Message varformat
191 #define FT_THROW( e ) \
192 ( FT_Throw( FT_ERR_CAT( ErrRaster, e ), \
195 FT_ERR_CAT( ErrRaster, e ) )
197 #else /* !FT_DEBUG_LEVEL_TRACE */
199 #define FT_TRACE5( x ) do { } while ( 0 ) /* nothing */
200 #define FT_TRACE7( x ) do { } while ( 0 ) /* nothing */
201 #define FT_ERROR( x ) do { } while ( 0 ) /* nothing */
202 #define FT_THROW( e ) FT_ERR_CAT( ErrRaster_, e )
205 #endif /* !FT_DEBUG_LEVEL_TRACE */
208 #define FT_DEFINE_OUTLINE_FUNCS( class_, \
209 move_to_, line_to_, \
210 conic_to_, cubic_to_, \
212 static const FT_Outline_Funcs class_ = \
222 #define FT_DEFINE_RASTER_FUNCS( class_, glyph_format_, \
223 raster_new_, raster_reset_, \
224 raster_set_mode_, raster_render_, \
226 const FT_Raster_Funcs class_ = \
237 #else /* !_STANDALONE_ */
240 #include <ft2build.h>
242 #include FT_INTERNAL_OBJECTS_H
243 #include FT_INTERNAL_DEBUG_H
244 #include FT_OUTLINE_H
246 #include "ftsmerrs.h"
250 #define Smooth_Err_Invalid_Mode Smooth_Err_Cannot_Render_Glyph
251 #define Smooth_Err_Memory_Overflow Smooth_Err_Out_Of_Memory
252 #define ErrRaster_Memory_Overflow Smooth_Err_Out_Of_Memory
255 #endif /* !_STANDALONE_ */
259 #define FT_MEM_SET( d, s, c ) ft_memset( d, s, c )
263 #define FT_MEM_ZERO( dest, count ) FT_MEM_SET( dest, 0, count )
266 /* as usual, for the speed hungry :-) */
273 #ifndef FT_STATIC_RASTER
275 #define RAS_ARG gray_PWorker worker
276 #define RAS_ARG_ gray_PWorker worker,
278 #define RAS_VAR worker
279 #define RAS_VAR_ worker,
281 #else /* FT_STATIC_RASTER */
283 #define RAS_ARG /* empty */
284 #define RAS_ARG_ /* empty */
285 #define RAS_VAR /* empty */
286 #define RAS_VAR_ /* empty */
288 #endif /* FT_STATIC_RASTER */
291 /* must be at least 6 bits! */
299 #define ONE_PIXEL ( 1L << PIXEL_BITS )
300 #define PIXEL_MASK ( -1L << PIXEL_BITS )
301 #define TRUNC( x ) ( (TCoord)( (x) >> PIXEL_BITS ) )
302 #define SUBPIXELS( x ) ( (TPos)(x) << PIXEL_BITS )
303 #define FLOOR( x ) ( (x) & -ONE_PIXEL )
304 #define CEILING( x ) ( ( (x) + ONE_PIXEL - 1 ) & -ONE_PIXEL )
305 #define ROUND( x ) ( ( (x) + ONE_PIXEL / 2 ) & -ONE_PIXEL )
308 #define UPSCALE( x ) ( (x) << ( PIXEL_BITS - 6 ) )
309 #define DOWNSCALE( x ) ( (x) >> ( PIXEL_BITS - 6 ) )
311 #define UPSCALE( x ) ( (x) >> ( 6 - PIXEL_BITS ) )
312 #define DOWNSCALE( x ) ( (x) << ( 6 - PIXEL_BITS ) )
316 /* Compute `dividend / divisor' and return both its quotient and */
317 /* remainder, cast to a specific type. This macro also ensures that */
318 /* the remainder is always positive. */
319 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
321 (quotient) = (type)( (dividend) / (divisor) ); \
322 (remainder) = (type)( (dividend) % (divisor) ); \
323 if ( (remainder) < 0 ) \
326 (remainder) += (type)(divisor); \
331 /* Work around a bug specific to GCC which make the compiler fail to */
332 /* optimize a division and modulo operation on the same parameters */
333 /* into a single call to `__aeabi_idivmod'. See */
335 /* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721 */
337 #define FT_DIV_MOD( type, dividend, divisor, quotient, remainder ) \
339 (quotient) = (type)( (dividend) / (divisor) ); \
340 (remainder) = (type)( (dividend) - (quotient) * (divisor) ); \
341 if ( (remainder) < 0 ) \
344 (remainder) += (type)(divisor); \
350 /*************************************************************************/
352 /* TYPE DEFINITIONS */
355 /* don't change the following types to FT_Int or FT_Pos, since we might */
356 /* need to define them to "float" or "double" when experimenting with */
359 typedef long TCoord; /* integer scanline/pixel coordinate */
360 typedef long TPos; /* sub-pixel coordinate */
362 /* determine the type used to store cell areas. This normally takes at */
363 /* least PIXEL_BITS*2 + 1 bits. On 16-bit systems, we need to use */
364 /* `long' instead of `int', otherwise bad things happen */
370 #else /* PIXEL_BITS >= 8 */
372 /* approximately determine the size of integers using an ANSI-C header */
373 #if FT_UINT_MAX == 0xFFFFU
379 #endif /* PIXEL_BITS >= 8 */
382 /* maximum number of gray spans in a call to the span callback */
383 #define FT_MAX_GRAY_SPANS 32
386 typedef struct TCell_* PCell;
388 typedef struct TCell_
390 TPos x; /* same with gray_TWorker.ex */
391 TCoord cover; /* same with gray_TWorker.cover */
398 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
399 /* We disable the warning `structure was padded due to */
400 /* __declspec(align())' in order to compile cleanly with */
401 /* the maximum level of warnings. */
402 #pragma warning( push )
403 #pragma warning( disable : 4324 )
404 #endif /* _MSC_VER */
406 typedef struct gray_TWorker_
408 ft_jmp_buf jump_buffer;
413 TPos count_ex, count_ey;
420 FT_PtrDist max_cells;
421 FT_PtrDist num_cells;
428 FT_Vector bez_stack[32 * 3 + 1];
435 FT_Span gray_spans[FT_MAX_GRAY_SPANS];
438 FT_Raster_Span_Func render_span;
439 void* render_span_data;
451 } gray_TWorker, *gray_PWorker;
453 #if defined( _MSC_VER )
454 #pragma warning( pop )
458 #ifndef FT_STATIC_RASTER
459 #define ras (*worker)
461 static gray_TWorker ras;
465 typedef struct gray_TRaster_
473 } gray_TRaster, *gray_PRaster;
477 /*************************************************************************/
479 /* Initialize the cells table. */
482 gray_init_cells( RAS_ARG_ void* buffer,
486 ras.buffer_size = byte_size;
488 ras.ycells = (PCell*) buffer;
498 /*************************************************************************/
500 /* Compute the outline bounding box. */
503 gray_compute_cbox( RAS_ARG )
505 FT_Outline* outline = &ras.outline;
506 FT_Vector* vec = outline->points;
507 FT_Vector* limit = vec + outline->n_points;
510 if ( outline->n_points <= 0 )
512 ras.min_ex = ras.max_ex = 0;
513 ras.min_ey = ras.max_ey = 0;
517 ras.min_ex = ras.max_ex = vec->x;
518 ras.min_ey = ras.max_ey = vec->y;
522 for ( ; vec < limit; vec++ )
528 if ( x < ras.min_ex ) ras.min_ex = x;
529 if ( x > ras.max_ex ) ras.max_ex = x;
530 if ( y < ras.min_ey ) ras.min_ey = y;
531 if ( y > ras.max_ey ) ras.max_ey = y;
534 /* truncate the bounding box to integer pixels */
535 ras.min_ex = ras.min_ex >> 6;
536 ras.min_ey = ras.min_ey >> 6;
537 ras.max_ex = ( ras.max_ex + 63 ) >> 6;
538 ras.max_ey = ( ras.max_ey + 63 ) >> 6;
542 /*************************************************************************/
544 /* Record the current cell in the table. */
547 gray_find_cell( RAS_ARG )
553 if ( x > ras.count_ex )
556 pcell = &ras.ycells[ras.ey];
560 if ( cell == NULL || cell->x > x )
569 if ( ras.num_cells >= ras.max_cells )
570 ft_longjmp( ras.jump_buffer, 1 );
572 cell = ras.cells + ras.num_cells++;
586 gray_record_cell( RAS_ARG )
588 if ( ras.area | ras.cover )
590 PCell cell = gray_find_cell( RAS_VAR );
593 cell->area += ras.area;
594 cell->cover += ras.cover;
599 /*************************************************************************/
601 /* Set the current cell to a new position. */
604 gray_set_cell( RAS_ARG_ TCoord ex,
607 /* Move the cell pointer to a new position. We set the `invalid' */
608 /* flag to indicate that the cell isn't part of those we're interested */
609 /* in during the render phase. This means that: */
611 /* . the new vertical position must be within min_ey..max_ey-1. */
612 /* . the new horizontal position must be strictly less than max_ex */
614 /* Note that if a cell is to the left of the clipping region, it is */
615 /* actually set to the (min_ex-1) horizontal position. */
617 /* All cells that are on the left of the clipping region go to the */
618 /* min_ex - 1 horizontal position. */
621 if ( ex > ras.max_ex )
628 /* are we moving to a different cell ? */
629 if ( ex != ras.ex || ey != ras.ey )
631 /* record the current one if it is valid */
633 gray_record_cell( RAS_VAR );
641 ras.invalid = ( (unsigned)ey >= (unsigned)ras.count_ey ||
642 ex >= ras.count_ex );
646 /*************************************************************************/
648 /* Start a new contour at a given cell. */
651 gray_start_cell( RAS_ARG_ TCoord ex,
654 if ( ex > ras.max_ex )
655 ex = (TCoord)( ras.max_ex );
657 if ( ex < ras.min_ex )
658 ex = (TCoord)( ras.min_ex - 1 );
662 ras.ex = ex - ras.min_ex;
663 ras.ey = ey - ras.min_ey;
664 ras.last_ey = SUBPIXELS( ey );
667 gray_set_cell( RAS_VAR_ ex, ey );
671 /*************************************************************************/
673 /* Render a scanline as one or more cells. */
676 gray_render_scanline( RAS_ARG_ TCoord ey,
682 TCoord ex1, ex2, fx1, fx2, delta, mod;
691 fx1 = (TCoord)( x1 - SUBPIXELS( ex1 ) );
692 fx2 = (TCoord)( x2 - SUBPIXELS( ex2 ) );
694 /* trivial case. Happens often */
697 gray_set_cell( RAS_VAR_ ex2, ey );
701 /* everything is located in a single cell. That is easy! */
706 ras.area += (TArea)(( fx1 + fx2 ) * delta);
711 /* ok, we'll have to render a run of adjacent cells on the same */
714 p = ( ONE_PIXEL - fx1 ) * ( y2 - y1 );
720 p = fx1 * ( y2 - y1 );
726 FT_DIV_MOD( TCoord, p, dx, delta, mod );
728 ras.area += (TArea)(( fx1 + first ) * delta);
732 gray_set_cell( RAS_VAR_ ex1, ey );
740 p = ONE_PIXEL * ( y2 - y1 + delta );
741 FT_DIV_MOD( TCoord, p, dx, lift, rem );
755 ras.area += (TArea)(ONE_PIXEL * delta);
759 gray_set_cell( RAS_VAR_ ex1, ey );
764 ras.area += (TArea)(( fx2 + ONE_PIXEL - first ) * delta);
769 /*************************************************************************/
771 /* Render a given line as a series of scanlines. */
774 gray_render_line( RAS_ARG_ TPos to_x,
777 TCoord ey1, ey2, fy1, fy2, mod;
780 int delta, rem, lift, incr;
783 ey1 = TRUNC( ras.last_ey );
784 ey2 = TRUNC( to_y ); /* if (ey2 >= ras.max_ey) ey2 = ras.max_ey-1; */
785 fy1 = (TCoord)( ras.y - ras.last_ey );
786 fy2 = (TCoord)( to_y - SUBPIXELS( ey2 ) );
791 /* perform vertical clipping */
803 if ( min >= ras.max_ey || max < ras.min_ey )
807 /* everything is on a single scanline */
810 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, to_x, fy2 );
814 /* vertical line - avoid calling gray_render_scanline */
819 TCoord ex = TRUNC( ras.x );
820 TCoord two_fx = (TCoord)( ( ras.x - SUBPIXELS( ex ) ) << 1 );
831 delta = (int)( first - fy1 );
832 ras.area += (TArea)two_fx * delta;
836 gray_set_cell( RAS_VAR_ ex, ey1 );
838 delta = (int)( first + first - ONE_PIXEL );
839 area = (TArea)two_fx * delta;
846 gray_set_cell( RAS_VAR_ ex, ey1 );
849 delta = (int)( fy2 - ONE_PIXEL + first );
850 ras.area += (TArea)two_fx * delta;
856 /* ok, we have to render several scanlines */
857 p = ( ONE_PIXEL - fy1 ) * dx;
869 FT_DIV_MOD( int, p, dy, delta, mod );
872 gray_render_scanline( RAS_VAR_ ey1, ras.x, fy1, x, (TCoord)first );
875 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
880 FT_DIV_MOD( int, p, dy, lift, rem );
894 gray_render_scanline( RAS_VAR_ ey1, x,
895 (TCoord)( ONE_PIXEL - first ), x2,
900 gray_set_cell( RAS_VAR_ TRUNC( x ), ey1 );
904 gray_render_scanline( RAS_VAR_ ey1, x,
905 (TCoord)( ONE_PIXEL - first ), to_x,
911 ras.last_ey = SUBPIXELS( ey2 );
916 gray_split_conic( FT_Vector* base )
921 base[4].x = base[2].x;
923 a = base[3].x = ( base[2].x + b ) / 2;
924 b = base[1].x = ( base[0].x + b ) / 2;
925 base[2].x = ( a + b ) / 2;
927 base[4].y = base[2].y;
929 a = base[3].y = ( base[2].y + b ) / 2;
930 b = base[1].y = ( base[0].y + b ) / 2;
931 base[2].y = ( a + b ) / 2;
936 gray_render_conic( RAS_ARG_ const FT_Vector* control,
937 const FT_Vector* to )
946 levels = ras.lev_stack;
949 arc[0].x = UPSCALE( to->x );
950 arc[0].y = UPSCALE( to->y );
951 arc[1].x = UPSCALE( control->x );
952 arc[1].y = UPSCALE( control->y );
957 dx = FT_ABS( arc[2].x + arc[0].x - 2 * arc[1].x );
958 dy = FT_ABS( arc[2].y + arc[0].y - 2 * arc[1].y );
962 if ( dx < ONE_PIXEL / 4 )
965 /* short-cut the arc that crosses the current band */
966 min = max = arc[0].y;
969 if ( y < min ) min = y;
970 if ( y > max ) max = y;
973 if ( y < min ) min = y;
974 if ( y > max ) max = y;
976 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
984 } while ( dx > ONE_PIXEL / 4 );
993 gray_split_conic( arc );
996 levels[top] = levels[top - 1] = level - 1;
1001 gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1005 } while ( top >= 0 );
1010 gray_split_cubic( FT_Vector* base )
1015 base[6].x = base[3].x;
1018 base[1].x = a = ( base[0].x + c ) / 2;
1019 base[5].x = b = ( base[3].x + d ) / 2;
1021 base[2].x = a = ( a + c ) / 2;
1022 base[4].x = b = ( b + c ) / 2;
1023 base[3].x = ( a + b ) / 2;
1025 base[6].y = base[3].y;
1028 base[1].y = a = ( base[0].y + c ) / 2;
1029 base[5].y = b = ( base[3].y + d ) / 2;
1031 base[2].y = a = ( a + c ) / 2;
1032 base[4].y = b = ( b + c ) / 2;
1033 base[3].y = ( a + b ) / 2;
1038 gray_render_cubic( RAS_ARG_ const FT_Vector* control1,
1039 const FT_Vector* control2,
1040 const FT_Vector* to )
1046 arc = ras.bez_stack;
1047 arc[0].x = UPSCALE( to->x );
1048 arc[0].y = UPSCALE( to->y );
1049 arc[1].x = UPSCALE( control2->x );
1050 arc[1].y = UPSCALE( control2->y );
1051 arc[2].x = UPSCALE( control1->x );
1052 arc[2].y = UPSCALE( control1->y );
1056 /* Short-cut the arc that crosses the current band. */
1057 min = max = arc[0].y;
1077 if ( TRUNC( min ) >= ras.max_ey || TRUNC( max ) < ras.min_ey )
1082 /* Decide whether to split or draw. See `Rapid Termination */
1083 /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */
1085 /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */
1088 TPos dx, dy, dx_, dy_;
1089 TPos dx1, dy1, dx2, dy2;
1093 /* dx and dy are x and y components of the P0-P3 chord vector. */
1094 dx = dx_ = arc[3].x - arc[0].x;
1095 dy = dy_ = arc[3].y - arc[0].y;
1097 L = FT_HYPOT( dx_, dy_ );
1099 /* Avoid possible arithmetic overflow below by splitting. */
1103 /* Max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1). */
1104 s_limit = L * (TPos)( ONE_PIXEL / 6 );
1106 /* s is L * the perpendicular distance from P1 to the line P0-P3. */
1107 dx1 = arc[1].x - arc[0].x;
1108 dy1 = arc[1].y - arc[0].y;
1109 s = FT_ABS( dy * dx1 - dx * dy1 );
1114 /* s is L * the perpendicular distance from P2 to the line P0-P3. */
1115 dx2 = arc[2].x - arc[0].x;
1116 dy2 = arc[2].y - arc[0].y;
1117 s = FT_ABS( dy * dx2 - dx * dy2 );
1122 /* Split super curvy segments where the off points are so far
1123 from the chord that the angles P0-P1-P3 or P0-P2-P3 become
1124 acute as detected by appropriate dot products. */
1125 if ( dx1 * ( dx1 - dx ) + dy1 * ( dy1 - dy ) > 0 ||
1126 dx2 * ( dx2 - dx ) + dy2 * ( dy2 - dy ) > 0 )
1129 /* No reason to split. */
1134 gray_split_cubic( arc );
1139 gray_render_line( RAS_VAR_ arc[0].x, arc[0].y );
1141 if ( arc == ras.bez_stack )
1150 gray_move_to( const FT_Vector* to,
1151 gray_PWorker worker )
1156 /* record current cell, if any */
1158 gray_record_cell( RAS_VAR );
1160 /* start to a new position */
1161 x = UPSCALE( to->x );
1162 y = UPSCALE( to->y );
1164 gray_start_cell( RAS_VAR_ TRUNC( x ), TRUNC( y ) );
1173 gray_line_to( const FT_Vector* to,
1174 gray_PWorker worker )
1176 gray_render_line( RAS_VAR_ UPSCALE( to->x ), UPSCALE( to->y ) );
1182 gray_conic_to( const FT_Vector* control,
1183 const FT_Vector* to,
1184 gray_PWorker worker )
1186 gray_render_conic( RAS_VAR_ control, to );
1192 gray_cubic_to( const FT_Vector* control1,
1193 const FT_Vector* control2,
1194 const FT_Vector* to,
1195 gray_PWorker worker )
1197 gray_render_cubic( RAS_VAR_ control1, control2, to );
1203 gray_render_span( int y,
1205 const FT_Span* spans,
1206 gray_PWorker worker )
1209 FT_Bitmap* map = &worker->target;
1212 /* first of all, compute the scanline offset */
1213 p = (unsigned char*)map->buffer - y * map->pitch;
1214 if ( map->pitch >= 0 )
1215 p += (unsigned)( ( map->rows - 1 ) * map->pitch );
1217 for ( ; count > 0; count--, spans++ )
1219 unsigned char coverage = spans->coverage;
1224 /* For small-spans it is faster to do it by ourselves than
1225 * calling `memset'. This is mainly due to the cost of the
1228 if ( spans->len >= 8 )
1229 FT_MEM_SET( p + spans->x, (unsigned char)coverage, spans->len );
1232 unsigned char* q = p + spans->x;
1235 switch ( spans->len )
1237 case 7: *q++ = (unsigned char)coverage;
1238 case 6: *q++ = (unsigned char)coverage;
1239 case 5: *q++ = (unsigned char)coverage;
1240 case 4: *q++ = (unsigned char)coverage;
1241 case 3: *q++ = (unsigned char)coverage;
1242 case 2: *q++ = (unsigned char)coverage;
1243 case 1: *q = (unsigned char)coverage;
1254 gray_hline( RAS_ARG_ TCoord x,
1262 /* compute the coverage line's coverage, depending on the */
1263 /* outline fill rule */
1265 /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
1267 coverage = (int)( area >> ( PIXEL_BITS * 2 + 1 - 8 ) );
1268 /* use range 0..256 */
1270 coverage = -coverage;
1272 if ( ras.outline.flags & FT_OUTLINE_EVEN_ODD_FILL )
1276 if ( coverage > 256 )
1277 coverage = 512 - coverage;
1278 else if ( coverage == 256 )
1283 /* normal non-zero winding rule */
1284 if ( coverage >= 256 )
1288 y += (TCoord)ras.min_ey;
1289 x += (TCoord)ras.min_ex;
1291 /* FT_Span.x is a 16-bit short, so limit our coordinates appropriately */
1295 /* FT_Span.y is an integer, so limit our coordinates appropriately */
1296 if ( y >= FT_INT_MAX )
1305 /* see whether we can add this span to the current list */
1306 count = ras.num_gray_spans;
1307 span = ras.gray_spans + count - 1;
1310 (int)span->x + span->len == (int)x &&
1311 span->coverage == coverage )
1313 span->len = (unsigned short)( span->len + acount );
1317 if ( ras.span_y != y || count >= FT_MAX_GRAY_SPANS )
1319 if ( ras.render_span && count > 0 )
1320 ras.render_span( ras.span_y, count, ras.gray_spans,
1321 ras.render_span_data );
1323 #ifdef FT_DEBUG_LEVEL_TRACE
1330 FT_TRACE7(( "y = %3d ", ras.span_y ));
1331 span = ras.gray_spans;
1332 for ( n = 0; n < count; n++, span++ )
1333 FT_TRACE7(( "[%d..%d]:%02x ",
1334 span->x, span->x + span->len - 1, span->coverage ));
1335 FT_TRACE7(( "\n" ));
1338 #endif /* FT_DEBUG_LEVEL_TRACE */
1340 ras.num_gray_spans = 0;
1341 ras.span_y = (int)y;
1343 span = ras.gray_spans;
1348 /* add a gray span to the current list */
1350 span->len = (unsigned short)acount;
1351 span->coverage = (unsigned char)coverage;
1353 ras.num_gray_spans++;
1358 #ifdef FT_DEBUG_LEVEL_TRACE
1360 /* to be called while in the debugger -- */
1361 /* this function causes a compiler warning since it is unused otherwise */
1363 gray_dump_cells( RAS_ARG )
1368 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1373 printf( "%3d:", yindex );
1375 for ( cell = ras.ycells[yindex]; cell != NULL; cell = cell->next )
1376 printf( " (%3ld, c:%4ld, a:%6d)", cell->x, cell->cover, cell->area );
1381 #endif /* FT_DEBUG_LEVEL_TRACE */
1385 gray_sweep( RAS_ARG_ const FT_Bitmap* target )
1389 FT_UNUSED( target );
1392 if ( ras.num_cells == 0 )
1395 ras.num_gray_spans = 0;
1397 FT_TRACE7(( "gray_sweep: start\n" ));
1399 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1401 PCell cell = ras.ycells[yindex];
1406 for ( ; cell != NULL; cell = cell->next )
1411 if ( cell->x > x && cover != 0 )
1412 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1415 cover += cell->cover;
1416 area = cover * ( ONE_PIXEL * 2 ) - cell->area;
1418 if ( area != 0 && cell->x >= 0 )
1419 gray_hline( RAS_VAR_ cell->x, yindex, area, 1 );
1425 gray_hline( RAS_VAR_ x, yindex, cover * ( ONE_PIXEL * 2 ),
1429 if ( ras.render_span && ras.num_gray_spans > 0 )
1430 ras.render_span( ras.span_y, ras.num_gray_spans,
1431 ras.gray_spans, ras.render_span_data );
1433 #ifdef FT_DEBUG_LEVEL_TRACE
1435 if ( ras.num_gray_spans > 0 )
1441 FT_TRACE7(( "y = %3d ", ras.span_y ));
1442 span = ras.gray_spans;
1443 for ( n = 0; n < ras.num_gray_spans; n++, span++ )
1444 FT_TRACE7(( "[%d..%d]:%02x ",
1445 span->x, span->x + span->len - 1, span->coverage ));
1446 FT_TRACE7(( "\n" ));
1449 FT_TRACE7(( "gray_sweep: end\n" ));
1451 #endif /* FT_DEBUG_LEVEL_TRACE */
1458 /*************************************************************************/
1460 /* The following function should only compile in stand-alone mode, */
1461 /* i.e., when building this component without the rest of FreeType. */
1463 /*************************************************************************/
1465 /*************************************************************************/
1468 /* FT_Outline_Decompose */
1471 /* Walk over an outline's structure to decompose it into individual */
1472 /* segments and Bézier arcs. This function is also able to emit */
1473 /* `move to' and `close to' operations to indicate the start and end */
1474 /* of new contours in the outline. */
1477 /* outline :: A pointer to the source target. */
1479 /* func_interface :: A table of `emitters', i.e., function pointers */
1480 /* called during decomposition to indicate path */
1484 /* user :: A typeless pointer which is passed to each */
1485 /* emitter during the decomposition. It can be */
1486 /* used to store the state during the */
1487 /* decomposition. */
1490 /* Error code. 0 means success. */
1493 FT_Outline_Decompose( const FT_Outline* outline,
1494 const FT_Outline_Funcs* func_interface,
1498 #define SCALED( x ) ( ( (x) << shift ) - delta )
1501 FT_Vector v_control;
1510 int n; /* index of contour in outline */
1511 int first; /* index of first point in contour */
1512 char tag; /* current point's state */
1519 return FT_THROW( Invalid_Outline );
1521 if ( !func_interface )
1522 return FT_THROW( Invalid_Argument );
1524 shift = func_interface->shift;
1525 delta = func_interface->delta;
1528 for ( n = 0; n < outline->n_contours; n++ )
1530 int last; /* index of last point in contour */
1533 FT_TRACE5(( "FT_Outline_Decompose: Outline %d\n", n ));
1535 last = outline->contours[n];
1537 goto Invalid_Outline;
1538 limit = outline->points + last;
1540 v_start = outline->points[first];
1541 v_start.x = SCALED( v_start.x );
1542 v_start.y = SCALED( v_start.y );
1544 v_last = outline->points[last];
1545 v_last.x = SCALED( v_last.x );
1546 v_last.y = SCALED( v_last.y );
1548 v_control = v_start;
1550 point = outline->points + first;
1551 tags = outline->tags + first;
1552 tag = FT_CURVE_TAG( tags[0] );
1554 /* A contour cannot start with a cubic control point! */
1555 if ( tag == FT_CURVE_TAG_CUBIC )
1556 goto Invalid_Outline;
1558 /* check first point to determine origin */
1559 if ( tag == FT_CURVE_TAG_CONIC )
1561 /* first point is conic control. Yes, this happens. */
1562 if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
1564 /* start at last point if it is on the curve */
1570 /* if both first and last points are conic, */
1571 /* start at their middle and record its position */
1573 v_start.x = ( v_start.x + v_last.x ) / 2;
1574 v_start.y = ( v_start.y + v_last.y ) / 2;
1582 FT_TRACE5(( " move to (%.2f, %.2f)\n",
1583 v_start.x / 64.0, v_start.y / 64.0 ));
1584 error = func_interface->move_to( &v_start, user );
1588 while ( point < limit )
1593 tag = FT_CURVE_TAG( tags[0] );
1596 case FT_CURVE_TAG_ON: /* emit a single line_to */
1601 vec.x = SCALED( point->x );
1602 vec.y = SCALED( point->y );
1604 FT_TRACE5(( " line to (%.2f, %.2f)\n",
1605 vec.x / 64.0, vec.y / 64.0 ));
1606 error = func_interface->line_to( &vec, user );
1612 case FT_CURVE_TAG_CONIC: /* consume conic arcs */
1613 v_control.x = SCALED( point->x );
1614 v_control.y = SCALED( point->y );
1617 if ( point < limit )
1625 tag = FT_CURVE_TAG( tags[0] );
1627 vec.x = SCALED( point->x );
1628 vec.y = SCALED( point->y );
1630 if ( tag == FT_CURVE_TAG_ON )
1632 FT_TRACE5(( " conic to (%.2f, %.2f)"
1633 " with control (%.2f, %.2f)\n",
1634 vec.x / 64.0, vec.y / 64.0,
1635 v_control.x / 64.0, v_control.y / 64.0 ));
1636 error = func_interface->conic_to( &v_control, &vec, user );
1642 if ( tag != FT_CURVE_TAG_CONIC )
1643 goto Invalid_Outline;
1645 v_middle.x = ( v_control.x + vec.x ) / 2;
1646 v_middle.y = ( v_control.y + vec.y ) / 2;
1648 FT_TRACE5(( " conic to (%.2f, %.2f)"
1649 " with control (%.2f, %.2f)\n",
1650 v_middle.x / 64.0, v_middle.y / 64.0,
1651 v_control.x / 64.0, v_control.y / 64.0 ));
1652 error = func_interface->conic_to( &v_control, &v_middle, user );
1660 FT_TRACE5(( " conic to (%.2f, %.2f)"
1661 " with control (%.2f, %.2f)\n",
1662 v_start.x / 64.0, v_start.y / 64.0,
1663 v_control.x / 64.0, v_control.y / 64.0 ));
1664 error = func_interface->conic_to( &v_control, &v_start, user );
1667 default: /* FT_CURVE_TAG_CUBIC */
1669 FT_Vector vec1, vec2;
1672 if ( point + 1 > limit ||
1673 FT_CURVE_TAG( tags[1] ) != FT_CURVE_TAG_CUBIC )
1674 goto Invalid_Outline;
1679 vec1.x = SCALED( point[-2].x );
1680 vec1.y = SCALED( point[-2].y );
1682 vec2.x = SCALED( point[-1].x );
1683 vec2.y = SCALED( point[-1].y );
1685 if ( point <= limit )
1690 vec.x = SCALED( point->x );
1691 vec.y = SCALED( point->y );
1693 FT_TRACE5(( " cubic to (%.2f, %.2f)"
1694 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
1695 vec.x / 64.0, vec.y / 64.0,
1696 vec1.x / 64.0, vec1.y / 64.0,
1697 vec2.x / 64.0, vec2.y / 64.0 ));
1698 error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
1704 FT_TRACE5(( " cubic to (%.2f, %.2f)"
1705 " with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
1706 v_start.x / 64.0, v_start.y / 64.0,
1707 vec1.x / 64.0, vec1.y / 64.0,
1708 vec2.x / 64.0, vec2.y / 64.0 ));
1709 error = func_interface->cubic_to( &vec1, &vec2, &v_start, user );
1715 /* close the contour with a line segment */
1716 FT_TRACE5(( " line to (%.2f, %.2f)\n",
1717 v_start.x / 64.0, v_start.y / 64.0 ));
1718 error = func_interface->line_to( &v_start, user );
1727 FT_TRACE5(( "FT_Outline_Decompose: Done\n", n ));
1731 FT_TRACE5(( "FT_Outline_Decompose: Error %d\n", error ));
1735 return FT_THROW( Invalid_Outline );
1738 #endif /* _STANDALONE_ */
1741 typedef struct gray_TBand_
1747 FT_DEFINE_OUTLINE_FUNCS(func_interface,
1748 (FT_Outline_MoveTo_Func) gray_move_to,
1749 (FT_Outline_LineTo_Func) gray_line_to,
1750 (FT_Outline_ConicTo_Func)gray_conic_to,
1751 (FT_Outline_CubicTo_Func)gray_cubic_to,
1757 gray_convert_glyph_inner( RAS_ARG )
1760 volatile int error = 0;
1762 #ifdef FT_CONFIG_OPTION_PIC
1763 FT_Outline_Funcs func_interface;
1764 Init_Class_func_interface(&func_interface);
1767 if ( ft_setjmp( ras.jump_buffer ) == 0 )
1769 error = FT_Outline_Decompose( &ras.outline, &func_interface, &ras );
1771 gray_record_cell( RAS_VAR );
1774 error = FT_THROW( Memory_Overflow );
1781 gray_convert_glyph( RAS_ARG )
1783 gray_TBand bands[40];
1784 gray_TBand* volatile band;
1785 int volatile n, num_bands;
1786 TPos volatile min, max, max_y;
1790 /* Set up state in the raster object */
1791 gray_compute_cbox( RAS_VAR );
1793 /* clip to target bitmap, exit if nothing to do */
1794 clip = &ras.clip_box;
1796 if ( ras.max_ex <= clip->xMin || ras.min_ex >= clip->xMax ||
1797 ras.max_ey <= clip->yMin || ras.min_ey >= clip->yMax )
1800 if ( ras.min_ex < clip->xMin ) ras.min_ex = clip->xMin;
1801 if ( ras.min_ey < clip->yMin ) ras.min_ey = clip->yMin;
1803 if ( ras.max_ex > clip->xMax ) ras.max_ex = clip->xMax;
1804 if ( ras.max_ey > clip->yMax ) ras.max_ey = clip->yMax;
1806 ras.count_ex = ras.max_ex - ras.min_ex;
1807 ras.count_ey = ras.max_ey - ras.min_ey;
1809 /* set up vertical bands */
1810 num_bands = (int)( ( ras.max_ey - ras.min_ey ) / ras.band_size );
1811 if ( num_bands == 0 )
1813 if ( num_bands >= 39 )
1821 for ( n = 0; n < num_bands; n++, min = max )
1823 max = min + ras.band_size;
1824 if ( n == num_bands - 1 || max > max_y )
1831 while ( band >= bands )
1833 TPos bottom, top, middle;
1839 long cell_start, cell_end, cell_mod;
1842 ras.ycells = (PCell*)ras.buffer;
1843 ras.ycount = band->max - band->min;
1845 cell_start = sizeof ( PCell ) * ras.ycount;
1846 cell_mod = cell_start % sizeof ( TCell );
1848 cell_start += sizeof ( TCell ) - cell_mod;
1850 cell_end = ras.buffer_size;
1851 cell_end -= cell_end % sizeof ( TCell );
1853 cells_max = (PCell)( (char*)ras.buffer + cell_end );
1854 ras.cells = (PCell)( (char*)ras.buffer + cell_start );
1855 if ( ras.cells >= cells_max )
1858 ras.max_cells = cells_max - ras.cells;
1859 if ( ras.max_cells < 2 )
1862 for ( yindex = 0; yindex < ras.ycount; yindex++ )
1863 ras.ycells[yindex] = NULL;
1868 ras.min_ey = band->min;
1869 ras.max_ey = band->max;
1870 ras.count_ey = band->max - band->min;
1872 error = gray_convert_glyph_inner( RAS_VAR );
1876 gray_sweep( RAS_VAR_ &ras.target );
1880 else if ( error != ErrRaster_Memory_Overflow )
1884 /* render pool overflow; we will reduce the render band by half */
1887 middle = bottom + ( ( top - bottom ) >> 1 );
1889 /* This is too complex for a single scanline; there must */
1890 /* be some problems. */
1891 if ( middle == bottom )
1893 #ifdef FT_DEBUG_LEVEL_TRACE
1894 FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
1899 if ( bottom-top >= ras.band_size )
1902 band[1].min = bottom;
1903 band[1].max = middle;
1904 band[0].min = middle;
1910 if ( ras.band_shoot > 8 && ras.band_size > 16 )
1911 ras.band_size = ras.band_size / 2;
1918 gray_raster_render( gray_PRaster raster,
1919 const FT_Raster_Params* params )
1921 const FT_Outline* outline = (const FT_Outline*)params->source;
1922 const FT_Bitmap* target_map = params->target;
1923 gray_PWorker worker;
1926 if ( !raster || !raster->buffer || !raster->buffer_size )
1927 return FT_THROW( Invalid_Argument );
1930 return FT_THROW( Invalid_Outline );
1932 /* return immediately if the outline is empty */
1933 if ( outline->n_points == 0 || outline->n_contours <= 0 )
1936 if ( !outline->contours || !outline->points )
1937 return FT_THROW( Invalid_Outline );
1939 if ( outline->n_points !=
1940 outline->contours[outline->n_contours - 1] + 1 )
1941 return FT_THROW( Invalid_Outline );
1943 worker = raster->worker;
1945 /* if direct mode is not set, we must have a target bitmap */
1946 if ( !( params->flags & FT_RASTER_FLAG_DIRECT ) )
1949 return FT_THROW( Invalid_Argument );
1952 if ( !target_map->width || !target_map->rows )
1955 if ( !target_map->buffer )
1956 return FT_THROW( Invalid_Argument );
1959 /* this version does not support monochrome rendering */
1960 if ( !( params->flags & FT_RASTER_FLAG_AA ) )
1961 return FT_THROW( Invalid_Mode );
1963 /* compute clipping box */
1964 if ( !( params->flags & FT_RASTER_FLAG_DIRECT ) )
1966 /* compute clip box from target pixmap */
1967 ras.clip_box.xMin = 0;
1968 ras.clip_box.yMin = 0;
1969 ras.clip_box.xMax = target_map->width;
1970 ras.clip_box.yMax = target_map->rows;
1972 else if ( params->flags & FT_RASTER_FLAG_CLIP )
1973 ras.clip_box = params->clip_box;
1976 ras.clip_box.xMin = -32768L;
1977 ras.clip_box.yMin = -32768L;
1978 ras.clip_box.xMax = 32767L;
1979 ras.clip_box.yMax = 32767L;
1982 gray_init_cells( RAS_VAR_ raster->buffer, raster->buffer_size );
1984 ras.outline = *outline;
1987 ras.band_size = raster->band_size;
1988 ras.num_gray_spans = 0;
1990 if ( params->flags & FT_RASTER_FLAG_DIRECT )
1992 ras.render_span = (FT_Raster_Span_Func)params->gray_spans;
1993 ras.render_span_data = params->user;
1997 ras.target = *target_map;
1998 ras.render_span = (FT_Raster_Span_Func)gray_render_span;
1999 ras.render_span_data = &ras;
2002 return gray_convert_glyph( RAS_VAR );
2006 /**** RASTER OBJECT CREATION: In stand-alone mode, we simply use *****/
2007 /**** a static object. *****/
2012 gray_raster_new( void* memory,
2013 FT_Raster* araster )
2015 static gray_TRaster the_raster;
2017 FT_UNUSED( memory );
2020 *araster = (FT_Raster)&the_raster;
2021 FT_MEM_ZERO( &the_raster, sizeof ( the_raster ) );
2028 gray_raster_done( FT_Raster raster )
2031 FT_UNUSED( raster );
2034 #else /* !_STANDALONE_ */
2037 gray_raster_new( FT_Memory memory,
2038 FT_Raster* araster )
2041 gray_PRaster raster = NULL;
2045 if ( !FT_ALLOC( raster, sizeof ( gray_TRaster ) ) )
2047 raster->memory = memory;
2048 *araster = (FT_Raster)raster;
2056 gray_raster_done( FT_Raster raster )
2058 FT_Memory memory = (FT_Memory)((gray_PRaster)raster)->memory;
2064 #endif /* !_STANDALONE_ */
2068 gray_raster_reset( FT_Raster raster,
2072 gray_PRaster rast = (gray_PRaster)raster;
2077 if ( pool_base && pool_size >= (long)sizeof ( gray_TWorker ) + 2048 )
2079 gray_PWorker worker = (gray_PWorker)pool_base;
2082 rast->worker = worker;
2083 rast->buffer = pool_base +
2084 ( ( sizeof ( gray_TWorker ) +
2085 sizeof ( TCell ) - 1 ) &
2086 ~( sizeof ( TCell ) - 1 ) );
2087 rast->buffer_size = (long)( ( pool_base + pool_size ) -
2088 (char*)rast->buffer ) &
2089 ~( sizeof ( TCell ) - 1 );
2090 rast->band_size = (int)( rast->buffer_size /
2091 ( sizeof ( TCell ) * 8 ) );
2095 rast->buffer = NULL;
2096 rast->buffer_size = 0;
2097 rast->worker = NULL;
2104 gray_raster_set_mode( FT_Raster raster,
2108 FT_UNUSED( raster );
2113 return 0; /* nothing to do */
2117 FT_DEFINE_RASTER_FUNCS(ft_grays_raster,
2118 FT_GLYPH_FORMAT_OUTLINE,
2120 (FT_Raster_New_Func) gray_raster_new,
2121 (FT_Raster_Reset_Func) gray_raster_reset,
2122 (FT_Raster_Set_Mode_Func)gray_raster_set_mode,
2123 (FT_Raster_Render_Func) gray_raster_render,
2124 (FT_Raster_Done_Func) gray_raster_done
2131 /* Local Variables: */